Kruskal’s Algorithm is a greedy algorithm used to construct a Minimum Spanning Tree (MST) from a weighted, undirected graph. The goal of the MST is to connect all vertices with the minimum total edge weight, without forming any cycles.
Union-Find
(Disjoint Set Union) data structure.
Consider a graph with 5 vertices labeled A, B, C, D, and E and the following weighted edges:
(A, B)
with weight 2(A, C)
with weight 3(B, C)
with weight 1(B, D)
with weight 4(C, D)
with weight 5(C, E)
with weight 6(D, E)
with weight 7Step-by-Step Process:
(B, C)
: 1,
(A, B)
: 2,
(A, C)
: 3,
(B, D)
: 4,
(C, D)
: 5,
(C, E)
: 6,
(D, E)
: 7
}
](B, C)
(weight 1):(A, B)
(weight 2):(A, C)
(weight 3):(B, D)
(weight 4):(C, D)
(weight 5):(C, E)
(weight 6):(D, E)
(weight 7):(B, C)
: 1(A, B)
: 2(B, D)
: 4(C, E)
: 6Kruskal’s Algorithm constructs the Minimum Spanning Tree by:
This greedy approach guarantees that the final tree spans all vertices with the minimum possible total weight.
(Sorting the edges dominates; union-find operations are nearly constant time.)
(Space is used to store the edge list and the union-find structure.)
https://drawtocode.vercel.app/problems/kruskal’s-algorithm
Loading component...
Loading component...
INPUT: edges = [ Edge(0, 1, 2), Edge(0, 2, 3), Edge(1, 2, 1), Edge(1, 3, 4), Edge(2, 3, 5), Edge(2, 4, 6), Edge(3, 4, 7) ]
OUTPUT: B-C : 1, A-B : 2, B-D :4 , C-E:6
public static void main(String[] args) {
V = 4;
E = 5;
edges.add(new Edge(0, 1, 10));
edges.add(new Edge(0, 2, 6));
edges.add(new Edge(0, 3, 5));
edges.add(new Edge(1, 3, 15));
edges.add(new Edge(2, 3, 4));
kruskalMST();
} //Function End
public static void kruskalMST() {
Edge result[] = new Edge[V - 1];
int e = 0;
int i = 0;
Collections.sort(edges, new Comparator<Edge>() {
public int compare(Edge a, Edge b) {
return a.weight - b.weight;
}
});
Subset subsets[] = new Subset[V];
for (i = 0; i < V; ++i) {
subsets[i] = new Subset();
subsets[i].parent = i;
subsets[i].rank = 0;
}//Loop End
i = 0;
while (e < V - 1 && i < edges.size()) {
Edge next_edge = edges.get(i++);
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y) {
result[e++] = next_edge;
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank){
subsets[xroot].parent = yroot;
}//If End
else{
if (subsets[xroot].rank > subsets[yroot].rank){
subsets[yroot].parent = xroot;
}//If End
else{
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}//Else End
}//Else End
}//If End
}//Loop End
System.out.println("Following are the edges in the constructed MST");
for (i = 0; i < e; ++i) {
char from = (char) ('A' + result[i].src);
char to = (char) ('A' + result[i].dest);
System.out.println(from + " - " + to + "\t" + result[i].weight);
}//Loop End
}//function end
static int find(Subset subsets[], int i) {
if (subsets[i].parent != i) {
subsets[i].parent = find(subsets, subsets[i].parent);
}//If End
return subsets[i].parent;
}//function end
static int V, E;
static List<Edge> edges = new ArrayList<>();
static class Edge {
int src, dest, weight;
public Edge(int src, int dest, int weight) {
this.src = src;
this.dest = dest;
this.weight = weight;
} //Constructor End
}//Class End
static class Subset {
int parent, rank;
}//Class End