Lab Program — Data Structures
Offline Ready Preset Driven Step Controls Accessible

Kruskal's Algorithm

Finding the Minimum Spanning Tree using a Greedy approach with Union-Find

O(E log E) Time Complexity
O(V + E) Space Complexity
Greedy Strategy
1956 Published
Try the Visualizer
01

What is Kruskal's Algorithm?

MST

Minimum Spanning Tree

A subset of edges that connects all vertices in a weighted, undirected graph with the minimum possible total weight, without forming any cycles.

GR

Greedy Approach

Kruskal's algorithm sorts all edges by weight and picks the smallest edge that doesn't form a cycle. It repeats until V−1 edges are selected.

UF

Union-Find (DSU)

Uses the Disjoint Set Union data structure with path compression and union by rank to efficiently detect cycles in near-constant time.

02

How It Works — Step by Step

1

Sort All Edges

Arrange all edges in non-decreasing order of their weight using a sorting algorithm (O(E log E)).

2

Initialize Union-Find

Create a parent array where each vertex is its own parent. This forms V disjoint sets initially.

3

Pick Smallest Edge

Take the next smallest edge. Check if adding it would form a cycle using the find() operation.

4

Add or Reject

If no cycle → add the edge to MST and perform union(). If it forms a cycle → reject it.

5

Repeat Until Done

Continue until MST has exactly V − 1 edges. The total weight of selected edges is the minimum cost.

03

Complexity Analysis

Time Complexity Breakdown

Sorting Edges
O(E log E)
Find Operations
O(E · α(V))
Union Operations
O(V · α(V))
Total
O(E log E)

α(V) = Inverse Ackermann function ≈ constant for all practical inputs

TC
Best Case
O(E log E)
Same as worst case — sorting dominates
TC
Worst Case
O(E log E)
Sorting E edges is the bottleneck step
SC
Space
O(V + E)
Parent array (V) + edge list (E)
04

Kruskal's vs Prim's — Comparison

Property Kruskal's Prim's
Approach Edge-based (global) Vertex-based (local)
Data Structure Union-Find (DSU) Priority Queue / Min-Heap
Time Complexity O(E log E) O(E log V)
Best For Sparse graphs (E ≈ V) Dense graphs (E ≈ V²)
Works on Disconnected? Yes (finds MSF) No
Needs Sorting? Yes No
05

Kruskal's Algorithm — Test Cases

Test Case 1 — Simple Triangle (3 nodes)
Nodes: A, B, C
Edges: A-B=1, B-C=2, A-C=4
Steps: Pick A-B (1) → Pick B-C (2) → Skip A-C (cycle)
MST: A-B-C | Total Cost: 3
Test Case 2 — Standard 4-Node Graph
Nodes: A, B, C, D
Edges: A-B=1, A-C=3, B-C=2, B-D=4, C-D=2
Steps: Pick A-B (1) → Pick B-C (2) → Pick C-D (2) → Skip B-D (cycle)
MST: A-B-C-D | Total Cost: 5
Test Case 3 — All Equal Weights
Nodes: 1, 2, 3, 4
Edges: 1-2=5, 2-3=5, 3-4=5, 1-4=5, 1-3=5
Steps: Any 3 edges can form the MST (no cycles allowed)
MST Cost: 15 (multiple valid MSTs)
Test Case 4 — Disconnected Graph
Nodes: A, B, C, D
Edges: A-B=2, C-D=3 (no edges between the pairs)
Result: No single MST is possible
Algorithm returns a spanning forest
Test Case 5 — Dense Graph (5 nodes)
Nodes: 1,2,3,4,5
Edges: 1-2=2, 1-3=3, 2-3=1, 2-4=4, 3-5=5, 4-5=1, 3-4=2
Sorted: 2-3=1, 4-5=1, 1-2=2, 3-4=2, 1-3=3, 2-4=4, 3-5=5
MST Cost: 6 (edges: 2-3, 4-5, 1-2, 3-4)
Test Case 6 — Linear / Path Graph
Nodes: A-B-C-D-E
Edges: A-B=5, B-C=3, C-D=8, D-E=2
Result: Only one spanning tree possible
MST Cost: 18

Key Rules to Remember

Condition Behavior
N nodes MST has exactly N − 1 edges
Cycle formed Skip that edge
Disconnected graph Result is a forest, not a single MST
Tie in weights Either edge can be chosen
06

Kruskal's Algorithm — Real-World Applications

NET

Network Design (Computer Networks)

Connect computers/servers with minimum cable length for LAN/WAN and data centers.

Example: Connect office floors using the least ethernet cable.
PWR

Electrical Power Grids

Lay power lines between cities and substations at minimum cost.

Example: Connect villages to the grid using minimum wire.
H2O

Water Supply & Pipeline Systems

Design pipelines across neighborhoods with minimal pipe length.

Example: Plan municipal water pipelines across wards.
RD

Road & Transportation Planning

Connect cities and hubs with minimum total road distance or cost.

Example: Plan highways between state capitals cheaply.
AIR

Airlines & Logistics

Build a minimum-cost route network for flights and deliveries.

Example: Connect cities with cheapest flight routes.
BIO

Biology & Bioinformatics

Cluster genes and build phylogenetic trees using MST-based clustering.

Example: Group related species from DNA similarity.
IMG

Image Processing & Computer Vision

Segment images by grouping similar pixels using MST clustering.

Example: Isolate tumor regions in MRI scans.
TEL

Telecommunication Networks

Lay fiber optics and connect towers at minimum cost.

Example: Connect 5G towers with minimum fiber.
ML

Machine Learning — Clustering

Perform hierarchical clustering and customer segmentation.

Example: Group similar customers for recommendations.
GAME

Game Development

Generate connected maps and efficient NPC path networks.

Example: Create connected cave systems procedurally.

Quick Summary Table

Domain Real Entity Edge Weight
Networking Computers / Servers Cable length / latency
Power Grid Cities / Substations Wire cost
Roads Cities / Towns Distance / cost
Water Supply Neighborhoods Pipe cost
Airlines Airports Ticket cost / distance
Biology Species / Genes Genetic difference
Telecom Cell Towers Fiber cable cost
ML Clustering Data Points Similarity distance
07

Interactive Visualizer

Graph Input

Enter 2-12 vertices
Enter 1-30 edges
Used for Prim only
Builder Tools
Canvas interactions: click to add nodes, click two nodes to create an edge, click a label to edit weight.
Graph Checks