Pseudo Code
Lesson 4 of 9 in Coddy's Kruskal's Algorithm - Graph Algorithms course.
kruskal(n, edges):
parent[i] = i for all i # each vertex its own set
total = 0
repeat until all edges considered:
pick the unused edge (u, v, w) with the smallest w
ru = find(u); rv = find(v)
if ru != rv: # no cycle
parent[ru] = rv # union
total += w
return total
find(x):
while parent[x] != x: x = parent[x]
return x- Picking the smallest unused edge each round (min-selection) avoids needing a separate sort.
- find walks up to the root; union is a single assignment of one root to the other.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Kruskal's Algorithm - Graph Algorithms
2The Algorithm
How it works?Pseudo CodeImplementation (Part 1)Implementation (Part 2)