A disjoint-set union structure partitions known elements into groups and supports find and union. Union by size attaches the smaller root under the larger; path compression shortens later find paths. Over a sequence of operations, amortized cost is O(alpha(n)) per operation, where alpha is the inverse Ackermann function and grows extremely slowly. The structure answers whether two elements are in the same component. It does not recover the actual path between them, represent directed reachability, or automatically remove an edge. The element universe and behavior for an unknown ID must be defined by the caller.
Disjoint sets: merge connectivity without tracing every path
Operational case
A warehouse network has sites 0 through 3. Repairs open links 0-1 and 2-3, leaving two components. When link 1-2 opens, all four sites share a component. A connectivity dashboard can answer whether 0 and 3 are connected without walking the route map each time. If a link later closes, this DSU cannot split the group; the application must rebuild from active links or use a more complex changing-link connectivity method. This matters in operational dashboards where links can both appear and disappear.
Working Python program
parent = list(range(4))
size = [1] * 4
def find(site):
while parent[site] != site:
parent[site] = parent[parent[site]]
site = parent[site]
return site
def connect(left, right):
left_root, right_root = find(left), find(right)
if left_root == right_root:
return
if size[left_root] < size[right_root]:
left_root, right_root = right_root, left_root
parent[right_root] = left_root
size[left_root] += size[right_root]
connect(0, 1)
connect(2, 3)
print(find(0) == find(3))
connect(1, 2)
print(find(0) == find(3))Output
False
TrueTime, space, and tradeoff
Initialization uses O(n) time and space for n sites. Union and find have near-constant amortized behavior with both optimizations, but a single operation's exact path length is not literally constant. The sample uses fixed integer IDs and mutates parent links; a production wrapper should validate ID bounds. Do not use this representation alone when a caller needs a route or a historical answer at an earlier edge version. Keep active-link revision with the DSU snapshot to avoid claiming current connectivity from stale unions.
Common Mistakes
- Do not use DSU to answer directed reachability.
- Do not expect union to support removing an edge.
- Do not infer an actual route from a shared root.
Connected lessons
- Graphs
- DSA Tutorial
- Graphs: adjacency lists and breadth-first reachability
- Hash maps: keyed lookup with collision and load costs
- Queues: preserve arrival order without front shifts
- Project: choose structures for a dispatch board
- Project: audit depot connectivity and daily ranges
- Tree, graph, and range structure decisions
Disjoint-set rollback: restore an earlier connectivity checkpoint extends this operation.
Parity disjoint set: maintain same-or-different constraints extends this operation contract.
Moveable disjoint sets: transfer one shipment without splitting its old tree adds a related structure with a different operation boundary.
Successor disjoint sets: skip permanently retired slots examines a related structure with a different operation boundary.
