Prim algorithm for minimum spanning tree
Thought: Using subtree extension method
Divide the vertices into two categories:
Growing point-a vertex already on the spanning tree
Non-growing point--not long to vertices on the spanning tree
Use the edge table you want to select:
Each non-growing point has a pending edge in the table to be selected, one end attached to the non-growing point and the other end connected to the growing point
Steps:
Step 1) constructs the initial selected edge table, optionally a vertex v as the initial growing point, for each of the remaining non-growing point W (n-1), The Edge (w,v) is added to the edge table to be selected, if the Edge (W,V) does not exist, it is considered that the length of the Edge (w,v) is ∞.
Step 2) Cycle n-2 times, the number of non-growing points K from n-1 to 1.
① Select the tree edge.
Select the shortest edge (u,v) from the side table to be selected, where U is a non-growing point, V is the growing point, and the (U,V) is moved from the edge table to the edge set of the spanning tree, and you are the newly selected growing point.
② Modify the edge you want to select
For each remaining non-growing point W, compare the length of the selected Edge (W,x) with the Edge (W,u), where x is the original growing point, and U is the newly selected growing point, if (w,u) is shorter than the edge (w,x), use (W,u) instead (W,X), as the W's optional edge;
Example:
The above is the official legend, if you can see the following secular plot .......
b for the growing point, list all and B connection points weights.
B |
B |
B |
B |
B |
C |
A |
D |
E |
F |
14 |
4 |
∞ |
20 |
5 |
1. With the least weight as the growing point, we find that the weight of b-a is the least. A is a new growing point.
And the edges of the b-a are determined.
2.A for the growth point and b the original side of the comparison, if less than the original, on the exchange.
A-c 8 less than b-c swap
A-d 45 less than b-d swap
A-E ∞ greater than b-e do not swap
A-f 3 less than b-f swap
B |
A |
A |
B |
A |
A |
C |
D |
E |
F |
4 |
8 |
45 |
20 |
3 |
At the point where the weight is the least. F is the new growing point.
Similarly, F is compared with the original weight of a (as in the previous step)
B |
A |
A |
A |
F |
A |
F |
C |
D |
E |
4 |
3 |
8 |
45 |
13 |
At the point where the weight is the least. C is the new growing point.
B |
A |
A |
C |
C |
A |
F |
C |
D |
E |
4 |
3 |
8 |
28 |
9 |
At the point where the weight is the least. E is the new growing point.
B |
A |
A |
C |
E |
A |
F |
C |
E |
D |
4 |
3 |
8 |
9 |
10 |
End. The minimum spanning tree is drawn sequentially by node and weighted value.
Note the point:
1. To find a new growing point (the smallest of the weights).
2. After finding the new growing point, we should compare it with the new growing points and the weights of each node, and the original growth and node weights, which is less than the original exchange. Otherwise, maintain the original node and the weight value.
Implementation Method Analysis:
(1) The side-set storage form of graphs
Adjacent arrays are stored to facilitate comparison of edge lengths, and only the lower triangular portion of the adjacency matrix (no weighted graph) is stored.
(2) Storage form of edge table and tree edge set to be selected
As in the example above, the selected edge table and tree edge set share an array of length n-1, containing 3 fields: the growing point and the non-growing point name, and the edge length.
(3) Process steps (cycle n-2)
As in the example above, each time you select the shortest edge from the currently selected edge as the tree edge, change to the right end of the left segment of the array and modify the remaining selected edges.
Prim algorithm and Kruskal algorithm comparison:
1) The main idea--all of them are selected short side, but the choice method is different
The Kruskal algorithm selects short edges from the full image.
The prim algorithm selects short edges from the edge table to be selected.
2) Intuitive
Kruskal using subtree Merging method (intuitive)
Prim using Subtree extension method
3) How easy it is to achieve
Kruskal algorithm needs to judge the loop (the implementation is difficult)
Prim algorithm does not need to judge the loop
4) Time complexity
Kruskal algorithm execution time is mainly spent in judging the loop, the time required is not more than O (MLOGM), M is the number of sides
Prim algorithm execution time is mainly spent in n-2 times to modify the selected edge table, its time consumption is O (N2) Order, n is the number of vertices
The Kruskal algorithm is suitable for situations where the number of vertices is greater and the number of edges is low
The prim algorithm is suitable for cases where the number of vertices is small and the number of edges is large
Prim algorithm of 24 minimum spanning tree