The Primm (Prim) algorithm, like the Kruskal algorithm, is an algorithm for finding the smallest spanning tree of weighted connected graphs.
Basic Ideas
For figure G, V is a collection of all vertices; now set two new sets U and T, where u is used to hold vertices in the smallest spanning tree of G, and T holds the edges in the smallest spanning tree of G. From all u? U,v? (v-u) (V-u represents the edges of all vertices of u) Select the least-weighted edge (U, v), add the vertex V to the set U, add the Edge (U, v) to the set T, and repeat until u=v, the minimum spanning tree is constructed, and the collection T contains all the edges in the smallest spanning tree.
Primm algorithm Diagram
Take G4, for example, to demonstrate the Primm (starting with the first vertex a, generating the smallest spanning tree from the Primm algorithm).
Initial State: V is a collection of all vertices, that is, v={a,b,c,d,e,f,g};u and T are empty!
1th Step: Adds vertex A to the U.
At this point, u={a}.
2nd Step: adds vertex b to U.
After the previous operation, U={a}, v-u={b,c,d,e,f,g}; Therefore, the Edge (A, B) has the least weight. Add vertex B to U; At this point, u={a,b}.
3rd Step: Adds vertex f to U.
After the previous operation, u={a,b}, v-u={c,d,e,f,g}; Therefore, the Edge (B,F) has the least weight. Add vertex F to U; At this point, u={a,b,f}.
4th Step: Adds vertex e to U.
After the previous operation, u={a,b,f}, v-u={c,d,e,g}; Therefore, the Edge (f,e) has the least weight. Add vertex E to U; At this point, u={a,b,f,e}.
5th Step: adds vertex D to U.
After the previous operation, u={a,b,f,e}, v-u={c,d,g}; Therefore, the Edge (E,D) has the least weight. Add vertex D to U; At this point, u={a,b,f,e,d}.
6th Step: adds vertex c to U.
After the previous operation, u={a,b,f,e,d}, v-u={c,g}; Therefore, the Edge (D,C) has the least weight. Add vertex C to U; At this point, u={a,b,f,e,d,c}.
7th Step: Adds vertex g to U.
After the previous operation, u={a,b,f,e,d,c}, v-u={g}; Therefore, the Edge (F,G) has the least weight. Add vertex g to U; At this point, u=v.
At this point, the minimum spanning tree construction is complete! It includes vertices in order:A B F E D C G.
code description for the PRIMM algorithm
Taking "adjacency Matrix" as an example to illustrate the Primm algorithm, the "adjacency table" Implementation of the graph in the following will give the corresponding source code.
1. Basic definition
//adjacency Matrixtypedefstruct_graph{CharVexs[max];//Vertex Collection intVexnum;//number of vertices intEdgnum;//Number of sides intMatrix[max][max];//adjacency Matrix}graph, *pgraph;//structure of the Edgetypedefstruct_edgedata{CharStart//the beginning of the side CharEnd//the end of the edge intWeight//the weight of the edge}edata;
Graph is the corresponding structure of the adjacency matrix.
Vexs is used to save vertices, Vexnum is the number of vertices, edgnum is the number of edges, and the matrix is a two-dimensional array to hold the matrix information. For example, matrix[i][j]=1 means "vertex I (i.e. vexs[i])" and "Vertex J (i.e. Vexs[j])" are adjacency points; matrix[i][j]=0, they are not adjacency points.
Edata are the structures that correspond to the edges of adjacent matrices.
2. Primm algorithm
#include <stdio.h>#include<stdlib.h>#include<malloc.h>#include<string.h>#defineMAX 100#defineINF (~ (0x1<<31))typedefstructgraph{CharVexs[max]; intVexnum; intEdgnum; intMatrix[max][max];} Graph,*Pgraph;typedefstructedgedata{Charstart; Charend; intweight;} EData;Static intGet_position (Graph G,Charch) { inti; for(i=0; i<g.vexnum; i++) if(g.vexs[i]==ch)returni; return-1;} Graph*create_graph () {Charvexs[]= {'A','B','C','D','E','F','G'}; intmatrix[][7]= { {0, A, Inf,inf,inf, -, -}, { A,0,Ten, Inf,inf,7, INF}, {INF,Ten,0,3,5,6, INF}, {inf,inf,3,0,4, Inf,inf}, {inf,inf,5,4,0Inf8}, { -,7,6Inf2,0,9}, { -, Inf,inf,inf,8,9,0} }; intvlen=sizeof(VEXS)/sizeof(vexs[0]); inti,j; Graph*PG; if((pg= (graph*) malloc (sizeof(Graph))) ==NULL)returnNULL; memset (PG,0,sizeof(PG)); PG->vexnum=Vlen; for(i=0; i<pg->vexnum; i++) PG->vexs[i]=Vexs[i]; for(i=0; i<pg->vexnum; i++) for(j=0; j<pg->vexnum; J + +) PG->matrix[i][j]=Matrix[i][j]; for(i=0; i<pg->vexnum; i++) { for(j=0; j<pg->vexnum; J + +) { if(i!=j&&pg->matrix[i][j]!=INF) PG->edgnum++; }} PG->edgnum/=2; returnPG;}voidprint_graph (graph G) {inti,j; printf ("Matrix Graph: \ n"); for(i=0; i<g.vexnum; i++) { for(j=0; j<g.vexnum; J + +) printf ("%10d", G.matrix[i][j]); printf ("\ n"); }}edata*get_edges (Graph G) {EData*edges; Edges= (edata*) malloc (g.edgnum*sizeof(EData)); inti,j; intindex=0; for(i=0; i<g.vexnum; i++) { for(j=i+1; j<g.vexnum; J + +) { if(g.matrix[i][j]!=INF) {Edges[index].start=G.vexs[i]; Edges[index].end=G.vexs[j]; Edges[index].weight=G.matrix[i][j]; Index++; } } } returnedges;}voidPrim (Graph G,intstart) { intmin,i,j,k,m,n,sum; intindex=0; CharPrim[max]; intWeight[max]; Prim[index++]=G.vexs[start]; for(i=0; i<g.vexnum; i++) Weight[i]=G.matrix[start][i]; Weight[start]=0; for(i=0; i<g.vexnum; i++) { //I used to control the number of cycles, adding a node at a time, but since start has been added, when I is start it skips if(start==i)Continue; J=0; K=0; Min=INF; for(k=0; k<g.vexnum; k++) { if(weight[k]&&weight[k]<min) {min=Weight[k]; J=K; }} sum+=min; Prim[index++]=G.vexs[j]; WEIGHT[J]=0; for(k=0; k<g.vexnum; k++) { if(weight[k]&&g.matrix[j][k]<Weight[k]) weight[k]=G.matrix[j][k]; } } //calculate weights for the minimum spanning treesum =0; for(i =1; I < index; i++) {min=INF; //get the position of prims[i] in Gn =get_position (G, prim[i]); //in vexs[0...i], find the vertex with the smallest weight of J. for(j =0; J < I; J + +) {m=get_position (G, prim[j]); if(g.matrix[m][n]<min) min=G.matrix[m][n]; } Sum+=min; } printf ("PRIM (%c) =%d:", G.vexs[start], sum); for(i =0; I < index; i++) printf ("%c", Prim[i]); printf ("\ n");}intMain () {Graph*PG; PG=create_graph (); Print_graph (*PG); Prim (*PG,0);}
Operation Result:
Primm Algorithm Introduction