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(); }
相关文章推荐
- 总结
- 源程序分析
- 实战c++中的vector系列--vector<unique_ptr<>>作为函数的参数
- 实战c++中的vector系列--vector<unique_ptr<>>作为函数的参数
- 黑马程序员 - C语言 - 小明的艳遇
- 第一个C语言程序
- C语言:定义指向二维数组的指针变量
- C++ 希尔排序算法
- C++对于大型图片的加载缩放尝试
- 黑马程序员 - C语言 -学生信息管理系统
- 实战c++中的vector系列--vector<unique_ptr<>>初始化(所有权转移)
- 实战c++中的vector系列--vector<unique_ptr<>>初始化(所有权转移)
- C++省略参数(va_list va_start va_arg va_end)的简单应用
- 基于模板元编程技术的跨平台C++动态链接加载库
- C++ 折半插入排序算法
- 汉洛塔递归实现的思考(C语言)
- 汉洛塔递归实现的思考(C语言)
- 汉洛塔递归实现的思考(C语言)
- C语言:指针类型强制性转换
- 一些公司的2016年校招C/C++开发岗笔试题目(四)