Skip to main content

Kruskal

This page provides introduction to Kruskal Algorithm.

Overview

Kruskal's algorithm is a minimum spanning tree (MST) algorithm. It finds a subgraph with n1\text{n} - 1 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.

kruskal-1.svg

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.

kruskal-2.svg

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, AD\text{AD}. Adding this edge to the MST does not connect already connected components, so we include it in the MST.

kruskal-3.svg
info

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 BC\text{BC}.

kruskal-4.svg

Applying algorithm to edge CD\text{CD}.

kruskal-5.svg

Applying algorithm to edge EF\text{EF}.

kruskal-6.svg

Applying algorithm to edge AB\text{AB}.

kruskal-7.svg

Applying algorithm to edge BD\text{BD}.

kruskal-8.svg

Applying algorithm to edge CF\text{CF}.

kruskal-9.svg

Applying algorithm to edge CE\text{CE}.

kruskal-10.svg

Applying algorithm to edge DE\text{DE}.

kruskal-11.svg

The final MST looks as follows:

kruskal-12.svg
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 N\text{N} nodes and E\text{E} edges in a graph.

Time Complexity

  • Sorting the edges by weight takes O(E log E)\text{O(E log E)} time.
  • Initializing the Disjoint Set for V\text{V} vertices takes O(V)\text{O(V)} time.
  • Processing each edge involves find() and union() operations, both of which run in O(log V)\text{O(log V)} time, and since we process at most E\text{E} edges, this step contributes O(E log V)\text{O(E log V)} to the complexity.

Thus, the overall time complexity is:

O(E log E)+O(E log V)\text{O(E log E)} + \text{O(E log V)}
info

Since in a connected graph, E\text{E} is at most O(V2)\text{O}(\text{V}^{2}) for a complete graph, the worst case time complexity becomes O(V2log V)\text{O}(\text{V}^{2} \text{log V}).

Space Complexity

The space complexity of Kruskal’s algorithm includes

  • O(E)\text{O(E)} for storing the edge list.
  • O(V)\text{O(V)} for union and find parent array.
  • O(V)\text{O(V)} for storing the minimum spanning Tree (MST).

Thus, the overall space complexity is:

O(E + V)\text{O(E + V)}
info

This makes Kruskal’s algorithm well suited for sparse graphs where EV\text{E} \approx \text{V}, but for dense graphs (EV2)(\text{E} \approx \text{V}{2}) prim’s algorithm may be more efficient.