Minimum Spanning Tree: Prim's Algorithm
I. Problem Description
A Minimum Spanning Tree (MST) is a spanning tree of a weighted, connected graph that minimizes the total sum of edge weights. Prim's Algorithm is a greedy algorithm used to construct an MST. Its core idea is to start from a single vertex and iteratively grow the tree by always adding the minimum-weight edge that connects a vertex in the tree to a vertex outside the tree, until all vertices are included.
II. Detailed Algorithm Steps
-
Initialization
- Choose any vertex from the graph as the starting point and add it to the MST set (denoted as
MST). - Maintain an array
key[]to record the minimum edge weight from each vertex to theMST. Initially, the starting vertex'skeyvalue is 0, and all others are infinity (indicating no connection yet). - Maintain an array
parent[]to record the parent node of each vertex in theMST(used to construct the tree structure).
- Choose any vertex from the graph as the starting point and add it to the MST set (denoted as
-
Greedy Selection and Expansion
- Repeat the following steps until all vertices are added to the
MST:
a. From the vertices not yet inMST, select the vertexuwith the minimumkeyvalue (i.e., the vertex currently closest to theMST).
b. Adduto theMST.
c. For each adjacent vertexvofu:
Ifvis not inMSTand the weight of edge(u, v)is less than the currentkey[v], then updatekey[v] = weight(u, v)and setparent[v] = u.
- Repeat the following steps until all vertices are added to the
-
Termination Condition
- The algorithm ends when all vertices are added to
MST. At this point, theparent[]array defines the edges of the minimum spanning tree.
- The algorithm ends when all vertices are added to
III. Example Demonstration
Consider the following weighted graph (vertices A, B, C, D with edge weights as shown):
A --2-- B
| / |
3 1 4
| / |
C --5-- D
Execution Process:
- Start from A.
key[A]=0,keyvalues for other vertices are ∞. - Add A to
MST. Updatekeyvalues for adjacent vertices B and C:key[B]=2,key[C]=3. - Select vertex B (with minimum
key=2) and add it toMST. Updatekey[D]=4(adjacent to B). Check vertex C: edge weight B-C (1) is less than currentkey[C]=3, so updatekey[C]=1. - Select vertex C (with minimum
key=1) and add it toMST. Check adjacent vertex D: edge weight C-D (5) is greater than currentkey[D]=4, so no update. - Finally, add D to
MST. The resulting spanning tree edges are A-B, B-C, and B-D, with total weight = 2+1+4 = 7.
IV. Algorithm Complexity and Optimization
- Time Complexity:
- Naive implementation (adjacency matrix + linear search for min
key): O(V²), suitable for dense graphs. - Optimized implementation (adjacency list + min-heap): O(E log V), suitable for sparse graphs.
- Naive implementation (adjacency matrix + linear search for min
- Space Complexity: O(V) (for storing
keyandparentarrays).
V. Comparison with Kruskal's Algorithm
- Prim's algorithm is vertex-based and suitable for dense graphs; Kruskal's algorithm is edge-sorting based and suitable for sparse graphs.
- Both guarantee an optimal solution but employ different greedy strategies: Prim always maintains a single tree, while Kruskal may maintain multiple subtrees simultaneously (merging them via a union-find data structure).