Module 19 of 39

Disjoint sets: union-find and path compression

Use disjoint sets to join groups and ask whether two items belong to the same group.

Suppose a map begins with isolated towns. Roads are added one at a time. After each addition, we want to know whether two towns can already reach each other. Re-running a whole graph search for every question may do more work than needed. A disjoint-set structure keeps track of groups that have been joined.

Background for this page. A group is a set of items treated as connected. A parent reference leads toward a representative root. You should be able to follow a chain of references. Related explanations: Arrays, linked lists, stacks, and queues · Minimum spanning trees: Kruskal, Prim, and safe edges.

It supports three operations. Make-set(x) creates a group containing only x. Find(x) returns a representative of x’s group. Union(x, y) joins the two groups, if they are not already one group. To test whether two towns are connected through added roads, compare their representatives. The representative is an internal label; it need not be a special town.

A representative answers connectivity after links are added.

A simple implementation could store a list of members for every group. Joining two groups might require updating many members. A more effective implementation stores a forest of parent links. Each tree represents one group, and its root is the representative. Union by rank or size attaches a smaller or shallower tree beneath a larger or deeper one. Path compression makes a find operation shorten the parent links it followed, so later finds travel less far.

# Follow parent links to the group's root.
find(x):
    if parent[x] is not x:
        # Shorten the path for future questions.
        parent[x] = find(parent[x])
    return parent[x]

Used together, union by rank and path compression give a very small amortized cost per operation: O(α(n)), where α is the inverse Ackermann function. It grows extraordinarily slowly, but it is not literally a constant in the mathematical bound. The deeper proof is beyond this first pass. The practical point is that a long sequence of joins and finds is efficient.

Disjoint sets answer whether items have become connected as links are added. They do not directly support removing a road and then splitting a group correctly. They also do not tell us the actual route between two towns. Those are different questions requiring more information.

Trace path compression

Suppose D points to C, C points to B, and B points to root A. Finding D follows D, C, B, A. During the return, path compression changes the intermediate links so D, C, and B point directly to A. The next find of D follows one link.

Union must first find both roots. If they are equal, the items already share a group and no merge is needed. Otherwise, union by size attaches the smaller root beneath the larger root and updates the new root’s size. Attaching an arbitrary internal node instead of a root can break the representation or fail to join the whole groups correctly.

Compression changes the forest’s shape without changing which items share a representative.

Practice. Start with four separate groups {A}, {B}, {C}, and {D}. Apply union(A, B) and union(C, D). Is A connected to D? What changes after union(B, C)?

Answer. At first A and D have different representatives, so they are not connected. After union(B, C), the two groups join; all four items now have the same representative, so A and D are connected.

Try another case. After a road is removed, can different roots be inferred from the compressed links?

Reasoning. No. The structure saved connectivity after additions, not the evidence needed to split a component after deletion.

View the full course and choose another topic.

Image preview

Fit