Minimal spanning tree: Constructing the minimum cost spanning tree for connected networks
1. Prim algorithm
The prim algorithm starts from the direction of the vertex of the graph, first determines a vertex, finds the connection cost of the vertex to any other vertex, and then updates the connectivity cost to the next node based on the newly determined node.
Data Structure declaration:
struct graph{ int Vertexes[max]; int Arc[max][max]; int sum_vertexes,sum_edges;};
Structure diagram:
void Creategraph (Graph &graph) { memset (graph.arc,0,sizeof (GRAPH.ARC)); int x,y,weight; cout<< "Total number of input vertices" <<endl; cin>>graph.sum_vertexes; cout<< "Total number of input edges" <<endl; cin>>graph.sum_edges; for (int i=0; i<graph.sum_vertexes; i++) { cout<< "enter ID for" <<i+1<< "vertex" <<endl; cin>>graph.vertexes[i]; } for (int i=0; i<graph.sum_edges; i++) { cout<< "Enter the beginning and end weight of the" <<i+1<< "Edge" <<endl; cin>>x>>y>>weight; graph.arc[x][y]=graph.arc[y][x]=weight;} }
Prim algorithm:
void Prim (graph graph) {int Adjvex[max]; int Lowcost[max]; Lowcost[0]=-1; adjvex[0]=0; Initialize for (int i=1; i<graph.sum_vertexes; i++) {lowcost[i]=graph.arc[0][i]; adjvex[i]=0; }//lowcost: 1 means that the vertex has been accessed, 0 means that the two vertices are not connected, and the other represents the weight for (int i=1; i<graph.sum_vertexes; i++) {//Weight <65535 int min=65535; int k; for (int j=1; j<graph.sum_vertexes; J + +) {if (lowcost[j]!=-1&&lowcost[j]!=0&&lowcost[j ]<min) {min=lowcost[j]; K=j; }} Lowcost[k]=-1; cout<<adjvex[k]<< "" <<k<<endl; Update Lowcost and Adjvex for (int j=1; j<graph.sum_vertexes; J + +) {if (lowcost[j]!=-1&&graph. arc[k][j]!=0&& (graph.arc[k][j]<lowcost[j]| | lowcost[j]==0)) {lowcost[j]=graph.arc[k][j]; Adjvex[j]=k; } } }}
2. Kruskal algorithm
Prim algorithm from the point of view, Kruskal from the side of the direction of consideration, each edge of the graph in accordance with the connection cost in accordance with the way from small to large, and then add the edge to the results one at a time, in order to avoid forming a ring, add a judgment mechanism.
Data Structure declaration:
struct edge{ int start; int stop; int weight;}; struct graph{ Edge Edge[max]; int sum_edges;};
Structure diagram:
void Creategraph (Graph &graph) { int x,y,w; cout<< "Total number of input edges" <<endl; cin>>graph.sum_edges; for (int i=0;i<graph.sum_edges;i++) { cout<< "Enter the beginning end weight of the" <<i+1<< "Edge" <<endl; cin>>x>>y>>w; graph.edge[i].start=x; graph.edge[i].stop=y; graph.edge[i].weight=w;} }
Kruskal algorithm
void Sortedges (Graph &graph) { Edge temp; for (int i=0;i<graph.sum_edges;i++) {for (int j=i+1;j<graph.sum_edges;j++) { if (graph.edge[ I].weight>graph.edge[j].weight) { temp=graph.edge[i]; GRAPH.EDGE[I]=GRAPH.EDGE[J]; graph.edge[j]=temp;}}}} int Find (int *parent,int t) { while (parent[t]!=0) t=parent[t]; return t;} void Kruskal (graph graph) { int m,n; int Parent[max]; Initializes the for (int i=0;i<graph.sum_edges;i++) parent[i]=0; for (int i=0;i<graph.sum_edges;i++) { m=find (parent,graph.edge[i].start); N=find (parent,graph.edge[i].stop); if (m!=n) { parent[m]=n; cout<<graph.edge[i].start<< " " <<graph.edge[i].stop<< " <<graph.edge[i" .weight<<endl; }}}
or write code to let two disconnected vertices in the diagram between the connection cost of 0, the result in the implementation of the judgment will add some conditions to judge, if you use positive infinity, it is more convenient ~ ~ ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Data structure] minimum spanning tree