Graph Theory37 sections · 1633 units
Open in Course

The Find Operation

Finding the Leader

To find the leader of node uu:

1.1. Check if parent[u] == u.

If yes, uu is the leader. Return uu.

2.2. If no, move up: uu = parent[u], and repeat.

You climb the tree until you reach a root. The root is the leader of uu's group. This takes O(h)O(h) time where hh is tree height. Without optimizations, hh could be O(n)O(n). With path compression, hh stays tiny.

Space complexity is O(n)O(n) for the data structures used.