01 / 20

Kruskal's & Prim's
Algorithms

Discover two powerful greedy strategies for finding the minimum spanning tree of a weighted graph: one that sorts edges globally, and one that grows a tree from a single vertex.

Discrete Math

What is a Spanning Tree?

A spanning tree of a connected graph \( G = (V, E) \) is a subgraph that includes every vertex in \( V \) and is a tree: connected with no cycles. It uses exactly \( |V| - 1 \) edges.

Original Graph A B C D → Spanning Tree A B C D 4 vertices → 3 edges (no cycles, all connected)

The Minimum Spanning Tree

Given a weighted, connected, undirected graph \( G = (V, E, w) \), a minimum spanning tree (MST) is a spanning tree \( T \) whose total edge weight is minimized:

★ Network Design
Connect all cities with the cheapest total road or cable network.
★ Clustering
Remove the longest MST edges to partition data into natural clusters.

Why Greedy Works for MSTs

Both Kruskal's and Prim's algorithms rely on the Cut Property, which guarantees that a greedy choice of the lightest edge crossing any cut is always safe.

This means at every step, picking the cheapest available edge that doesn't violate a constraint (no cycle for Kruskal's, crosses the frontier for Prim's) is provably optimal.

Kruskal's Algorithm

Kruskal's algorithm builds the MST by examining all edges globally, from lightest to heaviest, adding each edge if it does not create a cycle.

1
Sort all edges by weight in non-decreasing order.
2
Initialize each vertex as its own component (forest of singletons).
3
For each edge (lightest first): if it connects two different components, add it to the MST and merge those components.
4
Stop when the MST has \( |V| - 1 \) edges.

Prim's Algorithm

Prim's algorithm grows the MST one vertex at a time, always adding the cheapest edge that connects the growing tree to a vertex not yet in the tree.

1
Pick any starting vertex and add it to the tree \( T \).
2
Find the minimum-weight edge from a vertex in \( T \) to a vertex not in \( T \).
3
Add that edge and vertex to \( T \).
4
Repeat until \( T \) contains all \( |V| \) vertices.

Kruskal's vs Prim's: Key Difference

Kruskal's
Edge-centric. Considers edges globally, sorted by weight. Starts as a forest and merges components.
Prim's
Vertex-centric. Grows one connected tree outward from a start vertex by always crossing the cheapest frontier edge.

How to Apply Either Algorithm

1
Draw the graph and label every edge with its weight.
2
Choose your algorithm: Kruskal's (sort edges) or Prim's (pick a start vertex).
3
At each step, apply the greedy rule: cheapest safe edge (Kruskal's: no cycle; Prim's: connects to frontier).
4
Track your progress: mark selected edges and count them. Stop at \( |V| - 1 \).
5
Compute total weight by summing the weights of all selected edges.

Worked Example: The Graph

Consider this weighted graph with 5 vertices and 7 edges. We will find the MST using both algorithms.

4 2 5 6 3 7 1 A B C D E

Kruskal's Trace

1 2 3 4 A B C D E
1
Add D-E (weight 1): merges {D} and {E}.
2
Add A-C (weight 2): merges {A} and {C}.
3
Add C-D (weight 3): merges {A,C} and {D,E}.
4
Add A-B (weight 4): merges B into the tree. Done! Total: \(1+2+3+4 = 10\).

Prim's Trace (start at A)

2 3 1 4 A B C D E
1
Tree = {A}. Cheapest frontier edge: A-C (weight 2). Add C.
2
Tree = {A,C}. Frontier edges: A-B(4), C-D(3), C-B(5), C-E(7). Pick C-D (3).
3
Tree = {A,C,D}. Frontier: A-B(4), D-E(1), D-B(6). Pick D-E (1).
4
Tree = {A,C,D,E}. Frontier: A-B(4), D-B(6). Pick A-B (4). Done! Total: \(2+3+1+4=10\).

Same MST, Different Order

Both algorithms selected the same four edges: D-E(1), A-C(2), C-D(3), A-B(4) with total weight \(10\). But the order of selection differed.

Step Kruskal's Prim's (from A)
1 D-E (1) A-C (2)
2 A-C (2) C-D (3)
3 C-D (3) D-E (1)
4 A-B (4) A-B (4)
Total 10 10

Harder Example: 6-Vertex Graph

7 9 14 2 10 4 11 6 3 A B C D E F

Solution: MST = Weight 22

2 3 4 6 7 A B C D E F
1
B-D(2): Merge {B} and {D}.
2
E-F(3): Merge {E} and {F}.
3
C-E(4): Merge {C} into {E,F}.
4
D-F(6): Merge {B,D} and {C,E,F}.
5
A-B(7): Merge {A} into the tree. Total: \(2+3+4+6+7 = \) 22.

Common Mistakes

✕ Creating a Cycle
In Kruskal's, adding an edge whose both endpoints are already in the same component creates a cycle. Always check components first.
✕ Wrong Frontier in Prim's
Only edges from a tree vertex to a non-tree vertex are candidates. Edges between two tree vertices are not frontier edges.
✕ Stopping Too Early
The MST needs exactly \( |V| - 1 \) edges. Stopping at fewer edges leaves the tree disconnected. Always count your edges.

Algorithm Comparison

Property Kruskal's Prim's
Approach Edge-centric (global sort) Vertex-centric (grow tree)
Data Structure Union-Find (Disjoint Set) Priority Queue (Min-Heap)
Time Complexity \( O(E \log E) \) \( O(E \log V) \)
Best for Sparse graphs (\( E \approx V \)) Dense graphs (\( E \approx V^2 \))
Starts with Forest of singletons Single source vertex
Result Same MST Same MST

Question 1 of 5

A connected graph has 6 vertices. How many edges does its minimum spanning tree contain?

Question 2 of 5

In Kruskal's algorithm, when should you reject an edge?

Question 3 of 5

Which of the following is true about Prim's algorithm?

Question 4 of 5

The Cut Property guarantees that:

Question 5 of 5

A graph has edges with weights: 1, 2, 3, 4, 5, 6, 7 and 5 vertices. Kruskal's algorithm adds edges of weights 1, 2, 3, then considers weight 4. If weight 4 creates a cycle, it skips it and adds weight 5. What is the total MST weight?