Kruskal's Algorithm

Build the cheapest network connecting all nodes by adding the smallest edges first, skipping any that form a cycle.

Kruskal's Algorithm Practice Problems

Medium

2 problems
  1. 103

    Min Cost to Connect All Points

    Find the cheapest way to connect every point on a plane using straight-line connections.

    medium
  2. 258

    Number of Operations to Make Network Connected

    Find the minimum cable moves needed to connect every computer in a network.

    medium

Hard

2 problems
  1. 259

    Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree

    Classify every edge as critical, pseudo-critical, or neither for a minimum spanning tree.

    hard
  2. 260

    Remove Max Number of Edges to Keep Graph Fully Traversable

    Find the maximum number of edges removable while keeping a graph traversable by two travelers.

    hard

How to practise Kruskal

Spotting one

Same family as Prim. Connect everything, every link has a price, make the total as small as possible.

Reach for Kruskal when the links arrive as a list you can sort, when most pairs cannot be joined directly, or when the question asks which links end up being used.

The whole idea in one line

Sort every possible link from cheapest to dearest, walk the list, and keep a link only if the two things it joins are not already connected.

That second half is where the difficulty lives. You need a fast way to ask are these two already joined, and that's exactly what union-find is for. Read that page first if you haven't.

Where to start

Min Cost to Connect All Points. The twist is that nobody gives you the prices. You get positions on a map and have to work out every pair yourself before there is anything to sort.

Then Number of Operations to Make Network Connected, which is really a counting question. If the machines sit in four separate islands you need three cables, so the answer is islands minus one and the work is counting islands.

Find Critical and Pseudo-Critical Edges and Remove Max Number of Edges are both a big step up and assume the basic method is automatic. Leave them until it is.

Common mistakes

Sorting only part of the list. Kruskal is correct because it always considers the cheapest remaining link next. Break that and it stops working.

Adding a link that closes a loop. If both ends are already connected, it costs money and buys nothing.

Writing a slow check for are these connected that walks the network every time. Right answer, far too slow on anything large.