Graph Theory37 sections · 1633 units
Open in Course

Improvement 1 - Path Compression

The Magic Step

This is the secret sauce of DSU. When you run find(1) and walk up the chain to the leader (say, nn), why not make every visited node point directly to nn?

Next time you call find on any of those nodes, you reach the leader in one step. The tree flattens over time.

In code: parent[u] = find(parent[u]) during the recursive find. This simple change makes the amortized time nearly constant. Most DSU implementations use this.