A minimum spanning tree (MST) is a subset of edges in a connected, undirected, weighted graph that connects all vertices without any cycles and has the smallest possible total edge weight.
Example: Electricity Distribution Planning
Imagine an electricity distribution company wants to supply electricity to different villages from a power substation.
The company needs to lay electrical cables connecting all villages. However, installing cables between villages has a cost depending on the distance, terrain, material and installation effort.
The objective is to connect every village to the electricity network while minimizing the total cable installation cost.
Add visualization here
This is a classic Minimum Spanning Tree problem.
Basic Intuition
-
An adjacency list is useful when we need to traverse the graph from a particular vertex. We need to process the entire collection of edges globally, irrespective of their source vertices. Therefore, an edge list is a more natural representation.
DSU-Based Approach: Kruskal's Algorithm
Implementation
import java.util.*;
class Edge {
int src;
int dest;
int weight;
Edge(int src, int dest, int weight) {
this.src = src;
this.dest = dest;
this.weight = weight;
}
}
class DSU {
int[] parent;
int[] size;
DSU(int n) {
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
// Find ultimate parent with path compression
int find(int node) {
if (parent[node] != node) {
parent[node] = find(parent[node]);
}
return parent[node];
}
// Merge two components using union by size
boolean union(int u, int v) {
int rootU = find(u);
int rootV = find(v);
// Same component: adding this edge creates a cycle
if (rootU == rootV) {
return false;
}
// Attach smaller component to larger component
if (size[rootU] < size[rootV]) {
parent[rootU] = rootV;
size[rootV] += size[rootU];
} else {
parent[rootV] = rootU;
size[rootU] += size[rootV];
}
return true;
}
}
public class KruskalMST {
static void findMST(int V, List<Edge> edges) {
// Step 1: Sort edges by increasing weight
edges.sort(Comparator.comparingInt(e -> e.weight));
// Step 2: Initialize DSU
DSU dsu = new DSU(V);
int mstCost = 0;
int edgesSelected = 0;
List<Edge> mstEdges = new ArrayList<>();
// Step 3: Process edges in increasing order
for (Edge edge : edges) {
// Step 4: Include edge only if it connects
// two different components
if (dsu.union(edge.src, edge.dest)) {
mstCost += edge.weight;
edgesSelected++;
mstEdges.add(edge);
// Step 5: MST requires exactly V - 1 edges
if (edgesSelected == V - 1) {
break;
}
}
}
// Step 6: Check whether the graph is connected
if (edgesSelected != V - 1) {
System.out.println("MST does not exist");
return;
}
// Print selected MST edges
System.out.println("Edges in MST:");
for (Edge edge : mstEdges) {
System.out.println(
edge.src + " - " + edge.dest
+ " : " + edge.weight
);
}
System.out.println("Minimum MST Cost: " + mstCost);
}
public static void main(String[] args) {
int V = 5;
List<Edge> edges = new ArrayList<>();
edges.add(new Edge(0, 1, 4));
edges.add(new Edge(0, 2, 2));
edges.add(new Edge(1, 2, 1));
edges.add(new Edge(1, 3, 5));
edges.add(new Edge(2, 3, 8));
edges.add(new Edge(2, 4, 10));
edges.add(new Edge(3, 4, 2));
edges.add(new Edge(1, 4, 7));
findMST(V, edges);
}
}