🪓 Kruskal
This page provides introduction to Kruskal Algorithm.
Overview
Kruskal's algorithm is a minimum spanning tree (MST) algorithm. It finds a subgraph with edges, in which all vertices are connected. A minimum spanning tree is a tree in which the sum of the edge weights is minimized.
Run Krushkal
Let's apply the Kruskal's algorithm to the directed graph shown below to find minimum spanning tree.
We begin with sorting edges by weights in ascending order. Here we are using the Union Find algorithm to determine whether two vertices are already connected.
Now, we iterate over the edges and add an edge to the MST if adding it does not connect already connected components. Let's start with the first edge, . Adding this edge to the MST does not connect already connected components, so we include it in the MST.
Edges that are part of the MST are marked in green.
Similarly, let's iterate over the remaining edges and add them to the MST if they do not connect already connected nodes.
Applying algorithm to edge .
Applying algorithm to edge .
Applying algorithm to edge .
Applying algorithm to edge .
Applying algorithm to edge .
Applying algorithm to edge .
Applying algorithm to edge .
Applying algorithm to edge .
The final MST looks as follows:
- Java
class Edge {
char src;
char dest;
int distance;
Edge(char src, char dest, int distance) {
this.src = src;
this.dest = dest;
this.distance = distance;
}
@Override
public int compareTo(Edge other) {
return Integer.compare(this.weight, other.weight);
}
}
public List<Edge> kruskal(int nodes, List<Edge> graph) {
// sorting edges by weight
Collections.sort(graph);
// union find parent arr
int[] parentArr = new int[nodes];
List<Edge> mst = new ArrayList<>();
for(Edge e: graph) {
int from = e.src - 'A';
int to = e.dest - 'A';
// check connected
if(!isConnected(from, to, parentArr)) {
mst.add(new Edge(e.src, e.dest, e.weight));
}
}
private boolean isConnected(int from, int to, int[] parentArr) {
int fromParent = find(from, parentArr);
int toParent = find(to, parentArr);
if(fromParent == toParent) return true;
else {
parentArr[fromParent] = toParent;
return false;
}
}
private int find(int node, int[] parentArr) {
if(parentArr[node] == node) return node;
int parent = parentArr[node];
return parentArr[parent] = find(parent, parentArr);
}
}
Complexity
Let's say there are nodes and edges in a graph.