Algorithm Code: & #160; C ++ Code & #160; 1234567891011121314151617181920212223242526272829303132333435363738394041424344
Algorithm code:
C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
|
/* Generate the minimum spanning tree using the Prim algorithm */
Void MiniSpanTree_Prim (MGraph MG)
{
Int min, I, j, k;
Int adjvex [MAXVEX];/* save the corresponding vertex subscript */
Int lowcost [MAXVEX];/* save the weight of the edge between related vertices */
Lowcost [0] = 0;/* initialize the first weight to 0, that is, add v0 to the spanning tree */
/* The value of lowcost is 0. here, the vertex of this topic has been added to the spanning tree */
Adjvex [0] = 0;/* initialize the first vertex subscript to 0 */
Cout <"the smallest side of the spanning tree is:" <endl;
For (I = 1; I <MG. numVertexes; I ++)
{
Lowcost [I] = MG. arc [0] [I];/* store the edge weight of v0 vertex into an array */
Adjvex [I] = 0;/* subscripts whose initialization is v0 */
}
For (I = 1; I <MG. numVertexes; I ++)
{
Min = INFINITY;/* The minimum initialization weight is ∞ ,*/
J = 1;
K = 0;
While (j <MG. numVertexes)/* loop all vertices */
{
If (lowcost [j]! = 0 & lowcost [j] <min)/* if the weight is not 0 and the weight is less than min */
{
Min = lowcost [j];/* changes the current weight to the minimum value */
K = j;/* store the subscript of the current minimum value to k */
}
J ++;
}
Cout <"(" <adjvex [k] <"," <k <")" <""; /* print the edge with the smallest weight in the edge of the current vertex */
Lowcost [k] = 0;/* Set the weight of the current vertex to 0, indicating that the task has been completed */
For (j = 1; j <MG. numVertexes; j ++)/* loop all vertices */
{
/* If the subscript is k vertex, each edge weight is smaller than the previous vertex, which is not added to the generated tree weight */
If (lowcost [j]! = 0 & MG. arc [k] [j] <lowcost [j])
{
Lowcost [j] = MG. arc [k] [j];/* store a smaller weight value to the corresponding lowcost position */
Adjvex [j] = k;/* store the vertex whose subscript is k into adjvex */
}
}
}
Cout <endl;
} |
1. 1 ~ in the program ~ 16 rows are initialization operations, of which 7th ~ Eight Lines of adjvex [0] = 0 indicates that the vertex v0 is used now (in fact, it doesn't matter from that point on, assuming it starts from v0 ), lowcost [0] = 0 indicates that v0 has been included in the minimum spanning tree. if the value in the lowcost array is set to 0, the vertex under this base is included in the minimum spanning tree.
2. 11th ~ The 15 rows indicate reading the first row of data in the adjacent matrix. Therefore, the lowcost array is {0, 10,655 35, 65535,655 35, 11,655 35, 65535,655 35}, while the adjvex array is all 0. The initialization is complete.
3. 17th ~ The 49 rows have been cyclically 8 times, and I has been accumulated from 1 to 8. the whole cycle is the process of constructing the minimum spanning tree.
4. 24th ~ 33 rows. after the cycle, min = 10, k = 1. Note that 26 rows of if statements are used to determine lowcost [j]! = 0 indicates that the vertex of the generated tree is no longer used for searching the minimum weight.
Rows 5 and 35th, because k = 1 and adjvex [1] = 0, the print result is (0, 1), indicating that the v0 and v1 sides are the first side of the minimum spanning tree, for example, the first thumbnail.
6. for rows 36th, since k = 1, lowcost [k] = 0 means that vertex v1 is included in the minimum spanning tree. at this time, the lowcost array is {0, 0, 65535,655 35, 65535, 11,655 35, 65535,655 35}
7. 38th ~ In rows 47, j loops from 1 to 8. because k = 1, find the weights of the v1 row of the adjacent matrix, and compare them with the values of the lowcost array, if the value is smaller, modify the lowcost value and save the k value to the adjvex array. So eventually lowcost = {0, 18,655 35, 65535, 11, 16,655 35, 12 }. The value of the adjvex array is {0, 0, 1, 0, 0, 0, 1, 0 }. The if judgment here also indicates that v0 and v1 are equal to the vertex of the spanning tree and do not participate in the ratio of the minimum weight.
The above is the first cycle, which corresponds to the first small graph of I = 1. because it is complicated to describe the entire process with words, the following shows the spanning tree graph after I is a loop of different values, the so-called figure is a thousand words. you can understand the idea of the prim algorithm by simulating a loop eight times.
That is, the edges of the minimum spanning tree are: (0, 1), (0, 5), (1, 8), (8, 2), (1, 6), (6, 7), (7, 4), (7, 3)
Finally, Let's summarize the definition of the purm algorithm:
Suppose N = (V {E}) is a connected network, and TE is the set of the minimum spanning tree on N. The algorithm starts with U = {u0} (uo V), TE =. Repeat the following operations:
In the edge (U, v) E of uU, V-u, find the smallest edge (u0, v0) and merge it into the set TE. at the same time, v0 is merged into the U until U = v. In this case, the TE must have n-1 edges, and T = (V, {TE}) is the minimum spanning tree of N.
A legend is attached below to simulate the generation process of the minimum spanning tree of the graph:
| Legend |
Description |
Not optional |
Optional |
Selected |
|
This is the original weighted connected graph. The number on one side of each edge indicates its weight. |
- |
- |
- |
|
VertexDIs selected as the starting point. VertexA,B,EAndFThrough a single edge andD.AYesdistanceDThe nearest vertex, soAAnd corresponding edgeADHighlighted. |
C, G |
A, B, E, F |
D |
|
The next vertex is the distance.DOrAThe nearest vertex.BDistanceDIs 9, distanceA7,E15,FIs 6. Therefore,FDistanceDOrARecentlyFAnd corresponding edgeDFHighlighted. |
C, G |
B, E, F |
A, D |
|
The algorithm continues to repeat the preceding steps. DistanceA7 verticesBHighlighted. |
C |
B, E, G |
A, D, F |
|
In the current situation, you canC,EAndG.CDistanceB8,EDistanceB7,GDistanceFIs 11.ERecentlyEAnd corresponding edgeBEHighlighted. |
None |
C, E, G |
A, D, F, B |
|
Here, the available vertex is onlyCAndG.CDistanceE5,GDistanceEIs 9, so selectCAnd edgeECHighlighted together. |
None |
C, G |
A, D, F, B, E |
|
VertexGIs the only remaining vertex.FIs 11, distanceE9,ERecent, so highlightedGAnd corresponding edgeEG. |
None |
G |
A, D, F, B, E, C |
|
Now, all vertices have been selected, and the green part of the graph is the minimum spanning tree of the connected graph. In this example, the sum of the minimum spanning tree weights is 39. |
None |
None |
A, D, F, B, E, C, G |
The nested loop in the algorithm code shows that the time complexity of this algorithm is O (n ^ 2 ).
Compared with the prim and cruise Kar algorithms, the cruise Kar algorithm mainly targets edges and is more efficient when the number of edges is small. Therefore, it has a greater advantage for sparse graphs, that is, it is better when the number of edges is very large.