GraphsAnimated
Kruskal's Algorithm
Add edges cheapest first unless they form a cycle (union-find).
Time
O(E log E)
Extra space
O(V)
Use it for
Minimum spanning tree on sparse graphs.
How it works
- Sort all edges from cheapest to most expensive.
- Take the next edge. If it connects two nodes that are not yet connected, keep it; if they are already connected, it would make a cycle, so skip it.
- A union-find structure answers "already connected?" almost instantly.
- Stop after n − 1 edges. Those edges are a minimum spanning tree.
Watch it run
Loading animation…