Minimum Spanning Tree Algorithms
Understand how to find the Minimum Spanning Tree of a graph using Greedy strategies. Compare Prim's vertex-based growth approach with Kruskal's edge-based sorting approach.
Learning Goals
- Define the properties of a Minimum Spanning Tree (MST).
- Compare Prim's and Kruskal's algorithms in terms of logic and data structures.
- Trace Prim's algorithm using a Priority Queue.
- Trace Kruskal's algorithm using edge sorting and Disjoint Sets (Union-Find) to detect cycles.
Minimum Spanning Trees (MST): Prim vs. Kruskal
(Directly answers the theoretical comparisons from the 2024 Q7a exam)
A Spanning Tree is a subgraph that includes all vertices of the original graph but has exactly edges and no cycles. A Minimum Spanning Tree (MST) is the spanning tree where the total sum of edge weights is minimized.
Both Prim's and Kruskal's algorithms use the Greedy Method to find the MST, but they build it in entirely different ways:
| Feature | Prim's Algorithm | Kruskal's Algorithm |
|---|---|---|
| Growth Strategy | Vertex-based. Starts from a single arbitrary vertex and grows a single continuous tree outward. | Edge-based. Sorts all edges globally and picks the smallest ones, often creating disconnected "forests" that eventually merge. |
| Data Structures | Uses a Priority Queue (Min-Heap) to find the smallest edge connected to the growing tree. | Uses Sorting for edges and a Disjoint Set (Union-Find) to detect if adding an edge creates a cycle. |
| Best Used For | Dense graphs (where the number of edges is close to ). | Sparse graphs (where the number of edges is close to ). |
| Time Complexity | using a binary heap. | due to the initial edge sorting. |
Trace: Prim's Algorithm
- 1Step 1
Consider this undirected, weighted graph:
- 2Step 2
Select an arbitrary starting node. Let's pick A.
- MST Vertices:
{A} - Edges in PQ (connected to A):
(A-B: 1), (A-C: 4) - Total Cost: 0
- MST Vertices:
- 3Step 3
Extract the minimum edge from the PQ. That is
A-B: 1. Since B is not in the MST, we add it.- MST Vertices:
{A, B} - We add B's outward edges to the PQ:
(A-C: 4), (B-C: 2), (B-D: 5) - Total Cost: 1
- MST Vertices:
- 4Step 4
Extract the minimum edge from the PQ. That is
B-C: 2. Since C is not in the MST, we add it.- MST Vertices:
{A, B, C} - We add C's outward edges to the PQ:
(A-C: 4), (B-D: 5), (C-D: 3) - Total Cost: 1 + 2 = 3
- MST Vertices:
- 5Step 5
Extract the minimum edge. That is
C-D: 3. Since D is not in the MST, we add it.- MST Vertices:
{A, B, C, D}. All vertices are now included! - Total Cost: 3 + 3 = 6.
(Note: If we had extracted
A-C: 4later, we would reject it because both A and C are already in the MST, meaning it would form a cycle). Final MST Edges:A-B,B-C,C-D.
- MST Vertices:
Trace: Kruskal's Algorithm
- 1Step 1
Using the exact same graph, Kruskal's begins by completely ignoring the vertices and sorting every single edge in ascending order of weight:
A-B (1)B-C (2)C-D (3)A-C (4)B-D (5)
- 2Step 2
We iterate through the sorted list and use a Union-Find data structure to check for cycles.
- Pick
A-B (1). A and B are in different sets. Safe! Add to MST. (Cost: 1) - Pick
B-C (2). C is in a different set. Safe! Add to MST. (Cost: 1 + 2 = 3).
- Pick
- 3Step 3
- Pick
C-D (3). D is in a different set. Safe! Add to MST. (Cost: 3 + 3 = 6). - Pick
A-C (4). Wait! Our Union-Find structure knows that A and C are already in the exact same connected component (via A-B-C). Adding this edge would create a cycle (A-B-C-A). Reject it.
- Pick
- 4Step 4
- Pick
B-D (5). B and D are already in the same component. Reject it. We have exactly edges in our MST. Final Cost: 6. Both algorithms successfully built the exact same Minimum Spanning Tree!
- Pick
Knowledge Check
Which algorithm is used to find a Minimum Spanning Tree?