#define OK 1 #define BAD 0 #define MVNum 100 // ? love favorite #define INF 0x3F3F3F3F using Status = int; using VerTexType = char; using ArcType = int; typedefstruct { VerTexType vertices[MVNum]; ArcType arcs[MVNum][MVNum]; int vertexnum{MVNum}; int arcnum{ 0 }; //? Low cost }AMGraph;
/*****************************************************************//** * \file MTS.cpp * \brief For Ms.Winter * 3/24/2023 * \author 86158 * \date March 2023 *********************************************************************/ // Now we come to the MTS //? Most Cost Spannig Tree
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<cstdio> #include<string> usingnamespace std; // Macro #define OK 1 #define BAD 0 #define MVNum 100 // ? love favorite #define INF 0x3F3F3F3F using Status = int; using VerTexType = char; using ArcType = int; typedefstruct { VerTexType vertices[MVNum]; ArcType arcs[MVNum][MVNum]; int vertexnum{MVNum}; int arcnum{ 0 }; //? Low cost }AMGraph;
inlineintlocate(const AMGraph& G, int v) { for (int i = 0; i < G.vertexnum; i++) if (v == G.vertices[i]) return i; return-1; } Status CreateUDN(AMGraph& G) { memset(G.arcs, INF, sizeof(G.arcs)); for (int i = 0; i < G.vertexnum; i++) G.arcs[i][i] = 0; // It must been done ! cout << "first please input the arcnum and vernum of the graph"; cin >> G.vertexnum >> G.arcnum; cout << "Now please enter the vertices first"; for (int i = 0; i < G.vertexnum; i++) cin >> G.vertices[i]; VerTexType v1 = 0, v2 = 0; ArcType weight{ -1 }; int i{ 0 }, j{ 0 }; cout << "Now please enter all the arcs and weight"; for (int j = 0; j < G.arcnum; j++) { cin >> v1 >> v2 >> weight; i = locate(G, v1); j = locate(G, v2); G.arcs[i][j] = weight; // UDN G.arcs[j][i] = weight; // UDN } return OK; }
// 这个就是Prim算法的特殊数据结构,就是你使用这个算法就必须要想起的一个结构 // // ADT typedefstructcloseedge { int adjvex; int lowcast; //? low cast };
// 关键思路的分析 ,就是把点作为一个集合,从所有与这个集合相连的点中挑选 // 运用一个数组存储对应的顶点的最小开销,与此同时,再使用一个数组来记录对应的边的 // 另一个顶点 然后 为了判定一个点是否已经在集合内,我们将其cast 设置为0 // 并且最开始的时候我们的矩阵里使用的是一个INF 数据设计很巧妙 // The prim from a blogger voidPrim(AMGraph& G, int v)//? We choose a start point v0 { // First we use the closeedge closeedge C[MVNum]; // Special data struct // Init it with the v point int min_casts = 0; for (int i = 0; i < G.vertexnum; i++) { C[i].adjvex = v; C[i].lowcast = G.arcs[v][i]; } cout << "对应的边是"; for (int i = 1; i < G.vertexnum; ++i) { int temp = 0; int min_cast = INF; // Just the case for (int j = 0; j < G.vertexnum; j++) { if (C[j].lowcast != 0 && C[j].lowcast < min_cast) { temp = j; min_cast = C[j].lowcast; } } cout << " ( " << temp << "," << C[temp].adjvex << " )"; min_casts += min_cast; C[temp].lowcast = 0; // You've seen what the use just to see if used!!! for (int k = 0; k < G.vertexnum; k++) { if (C[k].lowcast != 0 && G.arcs[temp][k] < C[k].lowcast) { C[k].lowcast = G.arcs[temp][k]; C[k].adjvex = k; // Now you see what the adjvex mean } } } cout << "All casts are " << min_casts << endl; } // we could see the o(n^2) // We could also use the KrustalAL other method