๐ Kruskalโs Algorithm โ Complete In-Depth Explanation
**Kruskalโs Algorithm** is a **greedy algorithm** used to find the **Minimum Spanning Tree (MST)** of a weighted, **connected, and undirected graph**. The **MST** is a subset of the graphโs edges that connects all vertices with the **minimum total edge weight** and contains **no cycles**.
๐ Why Use Kruskalโs Algorithm?
- โ **Greedy approach ensures global optimization.**
- ๐ก **Works well with sparse graphs (O(E log E) time complexity).**
- ๐ **Finds the Minimum Spanning Tree efficiently.**
- ๐ **Used in network design, clustering, and image segmentation.**
๐ Understanding the Minimum Spanning Tree (MST)
A **Minimum Spanning Tree (MST)** is a **subset of a graphโs edges** that connects all vertices together with the **minimum total edge weight** and **without forming cycles**.
๐น **Properties of an MST**
- โ The MST has **V - 1 edges** (where V is the number of vertices).
- ๐ The MST has the **minimum total edge weight**.
- ๐ The MST contains **no cycles**.
- ๐ The MST **may not be unique** (multiple MSTs may exist).
๐ Steps in Kruskalโs Algorithm
๐ **Step 1: Sort All Edges by Weight**
Sort all edges in **ascending order** based on their weight.
๐ **Step 2: Initialize the MST**
Start with an empty set of edges in the MST.
๐ **Step 3: Add the Minimum Edge Without Forming a Cycle**
Use the **Disjoint Set Union (DSU)** (Union-Find) data structure to check for cycles.
๐ **Step 4: Stop When MST Has (V - 1) Edges**
Once the MST has **V - 1 edges**, the algorithm stops.
๐ Example Graph
Graph:
2
A-----B
| |
1 | | 3
| |
C-----D
4
Sorted Edges by Weight:
(C-A, 1), (A-B, 2), (B-D, 3), (C-D, 4)
MST Output:
2
A-----B
|
1 |
|
C
๐ Implementation of Kruskalโs Algorithm (O(E log E))
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
vector edges;
};
struct DisjointSet {
vector parent, rank;
DisjointSet(int n) {
parent.resize(n);
rank.resize(n, 0);
for (int i = 0; i < n; i++) parent[i] = i;
}
int find(int x) {
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
void unite(int x, int y) {
int rootX = find(x), rootY = find(y);
if (rootX != rootY) {
if (rank[rootX] > rank[rootY]) parent[rootY] = rootX;
else if (rank[rootX] < rank[rootY]) parent[rootX] = rootY;
else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
};
void kruskal(Graph &graph) {
sort(graph.edges.begin(), graph.edges.end(), [](Edge a, Edge b) {
return a.weight < b.weight;
});
DisjointSet ds(graph.V);
vector mst;
int mstCost = 0;
for (Edge e : graph.edges) {
if (ds.find(e.src) != ds.find(e.dest)) {
ds.unite(e.src, e.dest);
mst.push_back(e);
mstCost += e.weight;
}
}
cout << "Minimum Spanning Tree Cost: " << mstCost << endl;
for (Edge e : mst) {
cout << e.src << " -- " << e.dest << " == " << e.weight << endl;
}
}
โณ Time Complexity Analysis
Step | Time Complexity |
---|---|
Sorting Edges | O(E log E) |
Union-Find (DSU) Operations | O(E log V) |
Total Complexity | O(E log E) |
๐ Real-World Applications of Kruskalโs Algorithm
- ๐ก **Network Design** โ Laying down **minimum-cost communication cables.**
- ๐ **Road Systems** โ Designing **minimum-cost road networks.**
- ๐ **Computer Clustering** โ Connecting **servers with minimal latency.**
- ๐ฐ **Electric Grid Design** โ Optimizing **power transmission networks.**