---
description: Special thanks to Arin for writing this page!
---

# Kruskal's Algorithm

{% hint style="warning" %}
Before reading, review [Minimum Spanning Trees]() and [Union Find (Disjoint Sets)](../../abstract-data-types/union-find-disjoint-sets) as they both make Kruskal's algorithm possible!
{% endhint %}

## Conceptual Overview

Kruskal's algorithm is another optimal way to construct a **minimum spanning tree**. It's benefits are that it is conceptually very simple, and easy to implement. The idea is that first we sort all the edges of the graph in order of increasing weight. Then, add the smallest edge to the MST we are constructing unless this creates a cycle in the MST. Repeat until V - 1 edges total.

## Detailed Breakdown

In order to optimally check if adding an edge to our MST creates a cycle, we will use a **WeightedQuickUnion** object. (See [Union Find (Disjoint Sets)](../../abstract-data-types/union-find-disjoint-sets) for a recap on what this is.) This is used because checking if a cycle exists using a WeightedUnionFind object boils down to one `isConnected()` call, which we know takes $$\Theta(\log(N))$$.

To run the algorithm, we start by adding all the edges into a [PriorityQueue](../../abstract-data-types/collections/stacks-and-queues). This gives us our edges in sorted order. Now, we iterate through the PriorityQueue by removing the edge with highest priority, checking if adding it forms a cycle, and adding it to our MST if it doesn't form a cycle.

Let's see an example of Kruskal's Algorithm in action!

Here, we start with a simple graph and have sorted all of its edges into a priority queue.

![](https://628945320-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6l_SlXhV46y96GQwmG%2F-M79umQuTxU1C0lIWX72%2F-M79xUYhsIOvTcz4gaKg%2Fimage.png?alt=media\&token=7bcb754e-670b-47bc-8042-d299bce221f7)

Since the edge **DE** is the shortest, we'll add that to our UnionFind first. In the process, we'll **remove DE from the priority queue.**

![](https://628945320-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6l_SlXhV46y96GQwmG%2F-M79umQuTxU1C0lIWX72%2F-M79yzDmu5WUGZW2GKBd%2Fimage.png?alt=media\&token=4add75d2-8968-43b7-b086-4cb7f93deb6d)

We'll do the same thing with the next shortest path, **DC.**

![](https://628945320-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6l_SlXhV46y96GQwmG%2F-M79umQuTxU1C0lIWX72%2F-M79z6J-E0X-Y64uZDpV%2Fimage.png?alt=media\&token=1e9b5b59-fb95-4f8f-9bdf-a34642652942)

Now, let's move on to **AB.** Notice that this time, connecting A and B creates another **disjoint set!** Unlike Prim's Algorithm, Kruskal's Algorithm does not guarantee that a solution will form a tree structure until the very end.

![](https://628945320-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6l_SlXhV46y96GQwmG%2F-M79umQuTxU1C0lIWX72%2F-M79zKxaMAwkHco7x4e4%2Fimage.png?alt=media\&token=64d62757-12a2-4508-abcf-c966bbcc8330)

Now, let's connect **BC.**

![](https://628945320-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6l_SlXhV46y96GQwmG%2F-M79umQuTxU1C0lIWX72%2F-M79zTICYkpyWgRPj1mc%2Fimage.png?alt=media\&token=8a52243e-7fda-47e4-a4d7-3b231516d9ad)

Since **CE** and **BD** would both form cycles if connected, **we are done 😄** Here's the final tree:

![](https://628945320-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6l_SlXhV46y96GQwmG%2F-M79umQuTxU1C0lIWX72%2F-M79zmVwUKXejnIrpTZO%2Fimage.png?alt=media\&token=55df08d0-b2ba-4b2f-9bdd-b7acdc64a912)

## PseudoCode

```java
public class Kruskals() {

    public Kruskals() {
        PQ edges = new PriorityQueue<>();
        ArrayList<Edge> mst = new ArrayList<>();
    }

    public void doKruskals(Graph G) {
        for (e : G.edges()) {
            PQ.add(e);
        }
        WeightedQU uf = new WeightedQU(G.V());
        Edge e = PQ.removeSmallest();
        int v = e.from();
        int w = e.to();
        if (!uf.isConnected(v, w)) {
            uf.union(v, w);
            mst.add(e);
        }

    }
}
```

## Runtime Analysis

Left as an exercise to the reader 😉

{% hint style="info" %}
Someone's been reading too much [LADR](https://www.springer.com/gp/book/9783319110790)...\
(The answer is $$\Theta(E\log(E))$$by the way. Try to convince yourself why!)
{% endhint %}
