普里姆(Prim)算法
来源:互联网 发布:知乎的问题怎么删除 编辑:程序博客网 时间:2024/05/17 09:35
/*********************普里姆(Prim)算法******************///网类template<class Type>class NetGraph{private: Type vexs[MAX];//顶点表 int arc[MAX][MAX];//邻接矩阵 int numVertexes, numEdges;//当前网中节点、边数量 int getPosition(Type el);//获取数据el在顶点表中的位置 Type readType();//外界读取一个数据public: NetGraph();//创建网图 ~NetGraph(){}//析构函数 void print();//打印邻接矩阵 void MiniSpanTree_Prim();//Prim算法生成最小生成树};//获取数据el在顶点表中的位置template<class Type>int NetGraph<Type>::getPosition(Type el){ int i; for (i = 0; i < this->numVertexes; i++) { if (this->vexs[i] == el) return i; } return -1;}//外界读取一个数据template<class Type>Type NetGraph<Type>::readType(){ Type ch; cout << "Input a edge data: "; cin >> ch; return ch;}//创建网图(手动输入)template<class Type>NetGraph<Type>::NetGraph(){ int i,j; Type c1, c2; int p1, p2; int weight;//用于存储权值 cout << "Input Vertexes number: "; cin >> this->numVertexes; cout << "Input Edge number: "; cin >> this->numEdges; //检测顶点、边输入合法性 if (this->numVertexes < 1 || this->numEdges<1 || this->numEdges> this->numVertexes*(this->numVertexes - 1)) { cout << "Error input!" << endl; return; } //顶点初始化 for (i = 0; i < this->numVertexes; i++) { cout << "Input a Type data" << i + 1 << ": "; cin >> this->vexs[i]; } //邻接矩阵初始化 for (i = 0; i < this->numVertexes; i++) { for (j = 0; j < this->numVertexes; j++) { if (i == j) this->arc[i][j] = 0; else this->arc[i][j] = 65535; } } //边初始化 for (i = 0; i < this->numEdges; i++) { c1 = readType(); c2 = readType(); p1 = getPosition(c1); p2 = getPosition(c2); if (p1 == -1 || p2 == -1 || p1 == p2) { cout << "Earror Input!" << endl; return; } cout << "Input a weight value: "; cin >> weight; this->arc[p1][p2] = weight; this->arc[p2][p1] = weight; }}//打印邻接矩阵template<class Type>void NetGraph<Type>::print(){ cout << "******************************" << endl; cout << "打印邻接矩阵" << endl; int i, j; for (i = 0; i < this->numVertexes; i++) { for (j = 0; j < this->numVertexes; j++) cout << this->arc[i][j] << '\t'; cout << endl; }}//Prim算法生成最小生成树template<class Type>void NetGraph<Type>::MiniSpanTree_Prim(){ int min, i, j, k; int adjvex[MAX];//保存相关顶点下标 int lowcost[MAX];//保存相关顶点间边的权值 lowcost[0] = 0;//初始化第一个权值为0,即第一个元素加入生成树 adjvex[0] = 0;//初始化第一个顶点下表为0 for (i = 1; i < this->numVertexes; i++) { lowcost[i] = this->arc[0][i];//将第一个元素与之有关的权值存入数组 adjvex[i] = 0;//初始化都为第一个元素的下标 } for (i = 0; i < this->numVertexes; i++) { min = 65535;//初始化最小权值为65535 j = 1; k = 0; while (j < this->numVertexes) { if (lowcost[j] != 0 && lowcost[j] < min) { min = lowcost[j]; k = j; } j++; } cout << "(" << adjvex[k] << ", " << k << ") "; lowcost[k] = 0;//当前顶点的权值设置为0,表示此顶点已经完成任务 for (j = 1; j < this->numVertexes; j++) { if (lowcost[j] != 0 && this->arc[k][j] < lowcost[j]) { lowcost[j] = this->arc[k][j]; adjvex[j] = k; } } }}//测试程序int main(){ NetGraph<char> graph; graph.print(); graph.MiniSpanTree_Prim(); return 0;}
0 0
- 普里姆(Prim)算法
- 普里姆算法(Prim)
- 普里姆(prim)算法
- 普里姆(Prim)算法
- prim算法(普里姆算法)
- Prim算法(普里姆算法)
- Prim算法(普里姆算法)加点法-Prim算法
- 最小生成树prim(普里姆)算法
- 最小生成树-普里姆算法(Prim)
- 普里姆(Prim)算法 Java实现
- 普里姆(Prim)算法
- 普里姆算法-prim
- 普里姆算法(Prim)
- 普里姆(Prim)算法
- 普里姆(Prim)算法
- Prim算法 (改进)
- hdu1863(prim算法)
- poj_1258(Prim算法)
- SVN下载的文档存在?
- 批量添加数据(Form表单提交)
- Two Sum
- 需要牢记的JavaWeb基础
- Java中基本数据类型有哪些?
- 普里姆(Prim)算法
- 野指针
- 561. Array Partition I
- C++第三次作业——年月日||定期存款利息计算器||多分数段函数求值
- 简述while语句和do-while语句的特点与区别
- 蒟蒻养成记——数列变化
- 58到家数据库30条军规解读
- 『 Python笔记』 lambda表达式和函数式编程
- 《深入理解Java虚拟机》个人读书总结——JAVA虚拟机内存