Introduction to Primm algorithm
Primm (Prim) algorithm is used to calculate the minimum spanning tree of weighted connected graphs.
Basic ideas
For figure G, V is the set of all vertices; now, set two new sets of U and T, where u is used to hold vertices in the smallest spanning tree of G, and t to hold the edges of G's smallest spanning tree. From all Uu,v (v-u) (v-u denotes all vertices of u), select the Least Weighted edge (U, V, the vertex v is added to the set U, and The Edge (U, v) is added to the set T, so repeated until the u=v, the minimum spanning tree is constructed, In this case, the collection t contains all the edges in the smallest spanning tree.
Primm algorithm diagram
The above figure G4 as an example to demonstrate the Primm (a minimal spanning tree is generated from the first vertex a from the Primm algorithm).
Initial State : V is the set of all vertices, that is, v={a,b,c,d,e,f,g};u and T are empty!
Step 1th : Add vertex A to the U.
At this point, u={a}.
Step 2nd : Add vertex B to the U.
After the previous operation, U={a}, v-u={b,c,d,e,f,g}; Therefore, the Edge (a,b) has the smallest weight value. Add vertex B to U; At this point, u={a,b}.
Step 3rd : Add the vertex F to the U.
After the previous operation, u={a,b}, v-u={c,d,e,f,g}; Therefore, the Edge (b,f) has the smallest weight value. Add vertex F to U; At this point, u={a,b,f}.
Step 4th : Add the vertex E to the U.
After the previous operation, u={a,b,f}, v-u={c,d,e,g}; Therefore, the Edge (f,e) has the smallest weight value. Add vertex E to U; At this point, u={a,b,f,e}.
Step 5th : Add vertex D to the U.
After the previous operation, u={a,b,f,e}, v-u={c,d,g}; Therefore, the Edge (e,d) has the smallest weight value. Add vertex D to U; At this point, u={a,b,f,e,d}.
Step 6th : Add vertex C to the U.
After the previous operation, u={a,b,f,e,d}, v-u={c,g}; Therefore, the Edge (d,c) has the smallest weight value. Add vertex C to U; At this point, u={a,b,f,e,d,c}.
Step 7th : Add vertex g to the U.
After the previous operation, u={a,b,f,e,d,c}, v-u={g}; Therefore, the Edge (f,g) has the smallest weight value. Add vertex g to U; At this point, u=v.
At this point, the minimum spanning tree construction is complete! It includes vertices in turn:A B F E D C G.
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/