DSA Notes
Master Kruskal
How Kruskal's Works
Kruskal's algorithm builds the MST greedily:
- Sort ALL edges by weight (ascending)
- Process edges in order: add each edge to MST if it doesn't create a cycle
- Stop when MST has V-1 edges
Cycle detection is done efficiently using a Disjoint Set Union (DSU) — two vertices are in the same component if they are already connected in the partial MST.
| Sorted edges | [(D-C,1),(A-D,2),(B-C,3),(A-B,4)] |
| Step 1 | Add D-C (w=1): no cycle. MST={D-C} |
| Step 2 | Add A-D (w=2): no cycle. MST={D-C,A-D} |
| Step 3 | Add B-C (w=3): no cycle. MST={D-C,A-D,B-C} |
| Step 4 | Skip A-B (w=4): A and B already connected (A-D-C-B) |
Kruskal's for Named Vertices
Maximum Spanning Tree
Time Complexity Analysis
| Sorting edges | O(E log E) |
| find | O(α(V)) amortized per call |
| union | O(α(V)) amortized per call |
| Total DSU | O(E α(V)) ≈ O(E) in practice |
| Total | O(E log E) — dominated by sorting |
| Phase | Time Complexity |
|---|---|
| Sort edges | O(E log E) |
| DSU find/union per edge | O(α(V)) amortized |
| Total | O(E log E) |
| Space | O(V + E) |
α(V) is the inverse Ackermann function — it grows so slowly that for all practical purposes it is a constant (≤ 4 for any realistic input size).
Kruskal's vs Prim's — When to Use Which
Both algorithms produce a correct MST, but they have different strengths depending on graph density.
Kruskal's
- Sort E edges: O(E log E)
- Best for: SPARSE graphs (E ≈ V)
- Easy to implement with edge list
- Naturally handles disconnected components
Prim's
- Extract-min V times: O(V log V + E log V) with binary heap
- Best for: DENSE graphs (E ≈ V²) — use adjacency matrix O(V²)
- Better when graph is stored as adjacency matrix
Rule of thumb: Use Kruskal's when edges are given as a list (sparse graph or when edges are pre-sorted). Use Prim's when working with an adjacency matrix or very dense graph.
Detecting Disconnected Graphs
Kruskal's gracefully handles disconnected graphs — you just won't get a spanning TREE, you'll get a spanning FOREST (one tree per connected component).
Real-World Applications
1. Network Cable Routing
When laying network cables across offices or cities, Kruskal's finds the minimum cable length to connect all nodes:
2. Cluster Analysis (Maximum Spanning Tree)
In machine learning, maximum spanning trees are used to find natural clusters in data. Edges represent similarity — you want to keep the highest-similarity connections:
3. Approximation for TSP
The minimum spanning tree provides a 2-approximation for the Travelling Salesman Problem. Start with an MST, perform a DFS traversal, and visit each city once — this tour is at most 2× the optimal TSP solution.
Step-by-Step Trace on a Larger Graph
| Edges | (0,1,4),(0,2,2),(0,3,8),(1,4,5),(2,3,7),(2,4,3),(3,4,6) |
| Sorted | [(0,2,2),(2,4,3),(0,1,4),(1,4,5),(3,4,6),(2,3,7),(0,3,8)] |
| Step 1: Edge (0,2,w=2) | Components: {0,2}, {1}, {3}, {4} → ADD |
| Step 2: Edge (2,4,w=3) | Components: {0,2,4}, {1}, {3} → ADD |
| Step 3: Edge (0,1,w=4) | Components: {0,1,2,4}, {3} → ADD |
| Step 4: Edge (1,4,w=5) | 1 and 4 already connected → SKIP |
| Step 5: Edge (3,4,w=6) | Components: {0,1,2,3,4} — all! → ADD |
Summary
Kruskal's algorithm in three steps:
- Sort all edges by weight: O(E log E)
- Use DSU to greedily add edges that don't create cycles: O(E α(V))
- Stop when V-1 edges are added (spanning tree complete)
Total time: O(E log E) — ideal for sparse graphs.
Key properties:
- Works on disconnected graphs (produces a spanning forest)
- Maximum spanning tree: sort edges in descending order
- The DSU makes cycle detection essentially O(1) amortized per edge
- Greedy correctness is proven by the Cut Property of MSTs
*Next Lesson: Topological Sorting*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Kruskal.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, graphs, kruskals, algorithm
Related Data Structures & Algorithms Topics