您的位置:首页 > 编程语言 > C语言/C++

C++ 图的深度搜索广度搜索和最小生成树代码实现

2015-12-17 21:22 736 查看

头文件

Node.h

#pragma once
#include<iostream>
using namespace std;
class  Node
{
public:
int value;
int weight;
Node *next;
Node();
};


Node_list.h

#pragma once
#include"Node.h"
class Node_list
{
public:
int score;
int vertex;
Node *first;
Node_list();
};


Graph.h

#pragma once
#include"Node_list.h"
#include<queue>
class Graph
{
public:
int *visited;
int NodeNum;
void Creat();
void DFS(int);
void BFS(int);
void Creatvisit();
int Find(int);
Node_list *nodelist;
};


MGraph.h

#pragma once
#include<iostream>
#include"Graph.h"
using namespace std;
class MGraph
{
public:
int **mGraphArry;
int *adjvex;
int *lowcost;
int *vertex;
int Pos;
void GetMGraphArry(Graph);
void ShowMGraphArry();
void GetMinTree();
~MGraph();
};

源文件

Node.cpp

#include"Node.h"
Node::Node()
{
value = -1;
weight = 0;
next = NULL;
}


Node_list.cpp

#include"Node_list.h"
Node_list::Node_list()
{
vertex = 0;
score = 0;
first = new Node();
}


MGraph.cpp

#include"MGraph.h"
void MGraph::GetMGraphArry(Graph graph)
{
Pos = graph.NodeNum;
mGraphArry = new int*[graph.NodeNum];
vertex = new int[graph.NodeNum];
for (int i = 0; i < graph.NodeNum; i++)
{
vertex[i] = graph.nodelist[i].vertex;
}
for (int i = 0; i < graph.NodeNum; i++)
{
mGraphArry[i] = new int[graph.NodeNum];
}
for (int i = 0; i < graph.NodeNum; i++)
{
for (int j = 0; j < graph.NodeNum; j++)
{

if (i == j)
{
mGraphArry[i][j] = 0;
}
else
mGraphArry[i][j] = 1000000;
}
}
for (int i = 0; i < graph.NodeNum; i++)
{
Node *Node;
Node = graph.nodelist[i].first->next;
while (Node)
{
mGraphArry[i][Node->value]=Node->weight;
Node = Node->next;
}
}
}

void MGraph::ShowMGraphArry()
{
for (int i = 0; i < Pos; i++)
{
for (int j = 0; j < Pos; j++)
{
cout << mGraphArry[i][j] << " ";
}
cout << endl;
}
}
void MGraph::GetMinTree()
{
adjvex = new int[Pos];
lowcost = new int[Pos];
for (int i = 0; i < Pos; i++)
{
adjvex[i] = vertex[0];
lowcost[i] = mGraphArry[0][i];
}

for (int i = 1; i < Pos; i++)
{
int min = 10000000;
int pos=0;
for (int j = 0; j < Pos; j++)
{
if (lowcost[j] && lowcost[j] < min)
{
min = lowcost[j];
pos = j;
}
}
cout << "(" << adjvex[pos] << "," << vertex[pos] << ")" << endl;
lowcost[pos] = 0;
for (int t = 0; t < Pos; t++)
{
if (mGraphArry[pos][t]&&mGraphArry[pos][t] < lowcost[t])
{
adjvex[t] = vertex[pos];
lowcost[t] = mGraphArry[pos][t];
}
}
}
}

MGraph::~MGraph()
{
for (int i = 0; i <Pos; i++)
{
delete mGraphArry[i];
}
delete vertex;
}


Graph.cpp

#include"Graph.h"
void Graph::Creat()
{
cout << "输入节点个数:";
cin >> NodeNum;
nodelist = new Node_list[NodeNum];
cout << "输入各个点的值:" << endl;
for (int i = 0; i < NodeNum; i++)
{
cin >> nodelist[i].vertex;
//nodelist[i].score=i;
}
cout << "请以此输入出节点 入节点 权值" << endl;
int NUM1, NUM2,wight;
while (cin>>NUM1>>NUM2>>wight&&(NUM1+NUM2))
{
Node *p;
int Pos1=0,Pos2=0;
for (int i = 0; i < NodeNum; i++)
{
if (nodelist[i].vertex == NUM1)
{
Pos1 = i;
break;
}
}
for (int i = 0; i < NodeNum; i++)
{
if (nodelist[i].vertex == NUM2)
{
Pos2 = i;
break;
}
}
p = nodelist[Pos1].first;
while (p->next != NULL)
{
p = p->next;
}
Node *s = new Node();
s->value = Pos2;
s->weight = wight;
p->next = s;
}

}

void Graph::Creatvisit()
{
visited = new int[NodeNum];
for (int i = 0; i < NodeNum; i++)
{
visited[i] = 0;
}
}

void Graph::DFS(int n)
{
cout << nodelist
.vertex << " ";
visited
= 1;
Node *w = nodelist
.first->next;
while (w != NULL)
{
if (visited[w->value] == 0)
{
DFS(w->value);
}
w = w->next;
}
}

void Graph::BFS(int n)
{
queue<Node*> queue1;
cout << nodelist
.vertex << " ";
visited
= 1;
queue1.push(nodelist
.first);
while (!queue1.empty())
{
Node *v = queue1.front();
queue1.pop();
Node *w = v->next;
while (w != NULL)
{
if (visited[w->value] == 0)
{
cout << nodelist[w->value].vertex << " ";
visited[w->value] = 1;
queue1.push(nodelist[w->value].first);
}
w = w->next;
}
}
}

int Graph::Find(int NUM)
{
int Pos1 = 0;
for (int i = 0; i < NodeNum; i++)
{
if (nodelist[i].vertex == NUM)
{
Pos1 = i;
return Pos1;
}
}
}


Main,cpp

#include"Graph.h"
#include"MGraph.h"
int main()
{
Graph graph;
MGraph mGraph;
graph.Creat();
graph.Creatvisit();
int Num;
cout << "DFS输入开始节点:";
cin >> Num;
graph.DFS(graph.Find(Num));
cout << endl;
graph.Creatvisit();
cout << "BFS输入开始节点:";
cin >> Num;
graph.BFS(graph.Find(Num));
cout << endl;
cout << "链接矩阵" << endl;
mGraph.GetMGraphArry(graph);
mGraph.ShowMGraphArry();
cout << endl;
cout << "最小生成树" << endl;
mGraph.GetMinTree();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: