Graph Theory37 sections · 1633 units
Open in Course

The Parent Array

Tracking relationships

You do not need a complex tree structure. You need an array called parent. parent[i] stores the parent of node ii. If parent[i] = i, then ii is a root (leader).

Initially, everyone is their own leader: parent[i] = i for all ii. This means nn separate groups.

As you union elements, you update parent pointers. The array represents the entire forest of groups. Space: O(n)O(n). Simple and effective.