For a Minimum Spanning Tree.
All edges are sorted in increasing order.
Essentially the idea is to take the “cheapest” edge you can as long as that edge does not form a cycle.
- Sort all edges by weight (ascending)
- Initialize a forest
- Iterate over edges from smallest to largest
- If an edge connects two different trees, add it to the MST and merge those trees
- Stop when you’ve added n-1 edges or run out of edges
Note
You want to use a Disjoint Union Set / Union-find to make step 3 fast