Prim algorithm:if n = (v. {E}) is a connected network, and Te is a collection of edges in the smallest spanning tree on N. The algorithm starts with u={u0} (u0 belongs to V), te={}, and repeatedly runs the following operations: in the all U belongs to the u,v belongs to the v-u side (u,v) belongs to E to find a cost the least side (U0,V0) into the set TE, at the same time v0 into U, until u=v, There must be a n-1 edge in Te, then t= (v. {TE}) is the smallest spanning tree of N. To implement this algorithm, an auxiliary array Closedge is attached to record the lowest-cost edge from U to v-u.
For each vertex vi belongs to V-u, there is a corresponding component in the auxiliary array Closedge[i-1], which contains two domains, the Lowcost domain stores the right of that edge, and the Vex domain stores the vertices in U that the edge is attached to.
Consider for example the following non-net:
Adjacency Matrix:
Its minimum spanning tree is:
Prim algorithm process (the graph is represented by an adjacency matrix, if starting from Vertex a):
1. First initialize the auxiliary array and the vertex u into the U-set:
2. Iterate n-1 times, N is the number of vertices, each iteration calls the Mininum method to return a minimum k value, then outputs the edge of the spanning tree, and the K vertex is included in the U-set, and the secondary array is updated last.
For example, the first cycle. K = 2 (very clearly the minimum weight is 1=min{6,1,5}, the sequence number is 2, that is, K = 2), the edge of the output spanning tree (a,c), the adjacency matrix K vertex into the U-set (vertex C), will be lowcost 0, at this time the corresponding auxiliary array such as the following:
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2hkamo=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/center ">
The following will update the auxiliary array, that is, the elements of the auxiliary array and the adjacent table K line, if the corresponding value of the adjacency table is small, then replace the auxiliary array value.
At this point the first cycle ends, the following enters the second cycle, when k = 5 ...
implementation:
/**********************************************prim algorithm to find the minimum spanning tree by rowandjj2014/7/1********************************* /#include <iostream>using namespace std;//---------------------------------------#define Max_ Vertex_num 20//Edge max. # define Infinty 65535//represents infinity typedef struct _arc_{int adj;//vertex relationship type char *info;//arc information}arc,adjmatri x[max_vertex_num][max_vertex_num];typedef struct _graph_{char vexs[max_vertex_num];//vertex vector adjmatrix arcs;//adjacency matrix int vexnum,arcnum;//number of vertices, arcs}graph;//----------------------------------------typedef struct _ARRAY_//Auxiliary array {Char adjvex;//storage vertex int lowcost;//storage weights}minside[max_vertex_num];//----------------------------------------//figure operation INT Locatevex (graph G,char u); bool Createan (graph *g);//construct the non-network void Display (graph G);//Print the non-net//prim algorithm void Minispantree_prim ( Graph g,char u);//Use the PRIMM algorithm to construct the minimum spanning tree T of net G from the U-vertex, the output t of each edge int mininum (minside sz,graph G);//The minimum positive value for closedge.lowcost//------ ---------------------------------int Locatevex (Graph g,char u) {int i; for (i = 0; i < G.vexnum; i++) {if (g.vexs[i] = = u) {return i; }} return-1;} BOOL Createan (Graph *g) {int i,j,k; int incinfo; char va,vb;//vertex int w;//right char *info; Char str[20]; cout<< "Please enter the number of vertices of the non-net g, the number of edges, whether the edge contains other information (yes: 1, no: 0): \ n"; cin>>g->vexnum; cin>>g->arcnum; cin>>incinfo; cout<< "The value of the input vertex:"; Initialize vertex for (i = 0; i < g->vexnum; i++) {cin>>g->vexs[i]; }//Initialize adjacency matrix for (i = 0; i < g->vexnum; i++) {for (j = 0; J < g->vexnum; J + +) { G->arcs[i][j].adj = Infinty; G->arcs[i][j].info = NULL; }} cout<< "Enter two vertices and weights for each edge in turn" <<endl; Initialize Edge for (k = 0; k < g->arcnum; k++) {cin>>va; cin>>vb; cin>>w; i = Locatevex (*g,va); j = Locatevex (*G,VB); G->arcs[i][j].adj = W; G->arcs[j][i].adj = W; if (incinfo)//ARC Information {cout<< "Enter information about this edge:"; cin>>str; w = strlen (str) +1; if (w) {info = (char *) malloc (sizeof (char) * (w+1)); strcpy (INFO,STR); G->arcs[i][j].info = G->arcs[j][i].info = info; }}} return true; void Display (Graph G) {int i,j; cout<< "vertex:"; for (i = 0; i < G.vexnum; i++) {cout<<g.vexs[i]<< ""; } cout<< "\ n adjacency matrix: \ n"; for (i = 0; i < G.vexnum; i++) {for (j = 0; J < G.vexnum; J + +) {Cout<<g.arcs[i][j] .adj<< ""; } cout<<endl; } Cout<<endl;} ---------------------------------------------------------int mininum (minside sz,graph G) {int i = 0,J; The int min;//stores the smallest weight int k;//The element in the array where the smallest value is stored while (! Sz[i].lowcost)//Ignore the element {i++, lowcost is 0; } min = Sz[i].lowcost; K = i; For (j= i+1; j< G.vexnum; J + +) {if (Sz[j].lowcost > 0 && min > Sz[j].lowcost)//ignore element of Lowcost = 0 {min = sz[j ].lowcost; K = J; }} return k;} void Minispantree_prim (Graph g,char u) {minside closedge;//auxiliary array int i,j,k; K = Locatevex (g,u); if (k==-1) {return; }//Initialize auxiliary array for (i = 0; i < G.vexnum; i++) {if (k! = i) {Closedge[i].adjvex = u;//copy Top Point closedge[i].lowcost = g.arcs[k][i].adj;//copy Right}} closedge[k].lowcost = 0;//Initial, the K vertex is included in the U-set cout& lt;< "The minimum cost spanning tree's edges are: \ n"; for (i = 1; i < g.vexnum; i++) The minimum spanning tree of//n vertices is the n-1 edge {k = Mininum (closedge,g); cout<<closedge[k].adjvex<< "--->" <<g.vexs[k]<<endl;//outputs the edge of the spanning tree closedge[k].lowcost = 0;//k vertex incorporates U-set//update auxiliary array for (j = 0; J < G.vexnum; J + +) {if (closedge[j].lowcost > G.arcs[k][j].adj) { Closedge[j].adjvex = g.vexs[k];//Update top name closedge[j].lowcost = g.arcs[k][j].adj;//Update Right} }} Cout<<endl;} ---------------------------------------------------------int main () {Graph g; Createan (&G); Display (g); Minispantree_prim (g, ' a '); return 0;}
Test:
Prim algorithm of minimum spanning tree