Prim algorithm for constructing minimal spanning treeSuppose g= (v,e) is a connected net, where V is the set of all the vertices in the net, and E is the set of all the weighted edges in the net. Sets two new sets of U and T, where the collection U is used to hold the vertices of the smallest spanning tree of G, and the collection T is used to hold the edges in the smallest spanning tree of G. The initial value of the set U is u={u0} (assuming that the minimum spanning tree is constructed from the vertex u0), the initial value of the set T is t={}. The idea of the prim algorithm is to look for a vertex in the connected network to fall into the U-set, the other vertex falls into the v-u set of the vertex into the U-set, and then continue to look for one vertex in the U-sets and the other vertex in the v-u set and the least weight of the edge into the T set; At this point, the collection T contains all the edges of the smallest spanning tree. Overview:to implement the prim algorithm, you need to set up two one-dimensional arrays lowcost and Closevertex, where the array lowcost the weights of the edges that have the smallest weights in the edges of the collection v-u and the vertices in the collection U, and once lowcost[i] Set to 0, the vertex i is added to the collection u, that is, the vertex is no longer a vertex looking for the next least-weighted edge (only in the V-u collection), otherwise the loop will be formed, that is, the array lowcost has two functions: one is to record the weight of the edge, and the second is to identify the vertex in the U Array Closevertex also has two functions: one is to hold the vertices attached to the edge in the collection U, even if closevertex[i] the value of J, that is, the Edge (i,j) in the vertex j in the set U, and the second is to save the construction of the minimum spanning tree process generated by each edge, A value of J, such as Closevertex[i], indicates that the Edge (I,J) is an edge of the smallest spanning tree. We first set the initial state u={u0} (U0 is the starting vertex), then lowcost[0] is 0 to indicate that the vertex u0 has been added to the U set, the array lowcost the other array element values are vertex u0 to the rest of the vertex edge of the weight (no edge connected to take a maximum value), Initialize array closevertex[i] All array element values are 0 (that is, assume that all vertices include u0 have an edge with u0). Then the Edge (Ui,uk) with the least weight is selected, and each edge is selected Ui->u,uk->v-u K] is set to 0, which means the vertex UK has been added to the collection U. Since the UK enters the set v-u from the set, the vertices in the two sets are changed, It is therefore necessary to modify the contents of the array lowcost and arrays Closevertex in accordance with these changes. The edges in the final array Closevertex form a minimal spanning tree.
Reference code:
1#include <stdio.h>2 #defineMaxnode 303 #defineMaxcost 327674 5 voidPrim (intgm[][6],intClosevertex[],intN//The minimum spanning tree of the connected network is established from the vertex of the storage sequence number 0, and the GM is the adjacency matrix, n is the number of vertices, and finally the minimum spanning tree is stored in the array Closevertex.6 {7 intLowcost[maxnode];8 intI,j,k,mincost;9 for(i=1; i<n;i++)//InitializeTen { Onelowcost[i]=gm[0][i];//side (U0,ui) of weights sent Lowcost[i] Aclosevertex[i]=0;//assume vertex UI to vertex u0 has an edge - } -lowcost[0]=0;//from the vertex u0 with the ordinal 0, the minimum spanning tree is generated, at which point the u0 has entered the U-set theclosevertex[0]=0; - for(i=1; i<n;i++)//generate a minimum spanning tree with n-1 edges in n vertices (total n-1) - { -Mincost=maxcost;//Moxcost is a large constant value. +j=1; k=0; - while(j<n)//finding the least-weighted edges that have not been found + { A if(lowcost[j]!=0&&lowcost[j]<mincost) at { -MINCOST=LOWCOST[J];//note the weight of the minimum weight edge -K=j;//Note the vertex number of the minimum weight edge in the V-u set - - } -j + +;//Keep looking . in } -printf"Edge: (%d,%d), wight:%d\n", k,closevertex[k],mincost);//Edge and weight of the output minimum spanning tree tolowcost[k]=0;//vertex k enters U-set + for(j=1; j<n;j++) - if(lowcost[j]!=0&&GM[K][J]<LOWCOST[J])//the Benquan value of the vertex K and the other vertex J (in the V-u set) if the vertex k enters the U-set the{//Change Lowcost[j] to this small value, and the edge of this minimum weight (j,k) into the Closevertex array *lowcost[j]=Gm[k][j]; $closevertex[j]=K;Panax Notoginseng } - } the } + A voidMain () the { + intClosevertex[maxnode];//An array that holds all the edges of the smallest spanning tree - intg[6][6]={{ -,6,1,5, -, -},{6, -,5, -,3, -},{1,5, -,5,6,4}, ${5, -,5, -, -,2},{ -,3,6, -, -,6},{ -, -,4,2,6, -}}; $Prim (G,closevertex,6);//generate minimum Spanning tree -}
Output:
The connection diagram and corresponding adjacency matrix are as follows:
The analysis process of executing the prim algorithm to generate the minimum spanning tree is shown in the table, and the weight of the "---" tag is the minimum weight found for each trip. The minimum spanning tree in the graph shows the growth of each step of the A~f table (1) ~ (6): (1) The initial state, (2) to (6) The n-1 growth process of generating the n-1 edge.
Each step of the minimum spanning tree grows as shown. The shaded vertices belong to the U-set, and the non-shaded vertices belong to the v-u set, and the dashed edge is unknown origin to meet one vertex belonging to the U set while the other vertex belongs to the edge of the v-u set, while the solid Edge is the edge in the smallest spanning tree found.
Prim algorithm for minimum spanning tree