Menu
Coddy logo textTech

How it works?

Lesson 3 of 9 in Coddy's Kruskal's Algorithm - Graph Algorithms course.

Union-find keeps a parent array. Each vertex starts as its own root. Two operations drive everything:

  • find(x): follow parent links until you reach a root (a vertex that is its own parent). Two vertices are connected when they share a root.
  • union(a, b): point one root at the other, merging the two sets.

Kruskal's Algorithm:

  1. Process edges from smallest weight to largest.
  2. For each edge, find the roots of its endpoints. If they differ, the edge joins two separate pieces: union them and add its weight to the total.
  3. If the roots are the same, the edge would form a cycle, so skip it.

After processing every edge, the chosen edges form the MST (for a connected graph, exactly n - 1 of them).

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Kruskal's Algorithm - Graph Algorithms