7 routes42 sections324 units

Unit 3.5 · about 3 h

108 / 324

Union-find with path compression and union by rank

Connectivity queries that interleave with edge insertions defeat repeated traversal: each BFS costs a full scan while each union-find operation costs effectively constant time.

  • implement

You can now

  • Implement union-find with path compression and union by rank or size, and state the amortized inverse-Ackermann bound with its source.
  • Identify when union-find cannot help: deletions, directed reachability and queries that need paths, not just connectivity.
  • Choose the online union-find framing over one-shot BFS or DFS when edges arrive interleaved with queries.

Covers

  • Union-find, path compression and union by rank or size

Study, in this order

2 steps

  1. Union-Find: parent forests, path compression, and union by rank
    DSA Handbook - graphs

    The forest, the find, the union; Two heuristics that change the bound; Putting it all together (LC 684); LC 990: equality and inequality, two passes

    Depth · Skim LC 685's directed case-split on first pass; return after the MST unit if directed redundancy still bites.

  2. Disjoint Sets, Lecture Note 11
    Algorithms - graph chapters

    11.1 Reversed Trees (union by depth or rank, and the size-at-least-2-to-the-rank property); 11.3 Path Compression; 11.6 The Ackermann Function and its Inverse for the bound. Skim 11.2 Shallow Threaded Trees.

Focus on

  • Compare find(x) with find(y), never parent[x] with parent[y]: cousins share a root through a chain while their immediate parents differ.
  • Rank is an upper bound on height, not the height: compression shrinks trees without lowering ranks, and the size-at-least-2-to-the-rank property is what the log bound leans on.
  • Count components as the set of find(i) over all \(i\), never as the set of parent entries: stale intermediate pointers undercount.
  • Process all equalities before any inequality (LC 990): input order lies, because a later union can connect an earlier checked pair.

Reproduce closed-book

From a blank file with the course Python (3.13.14), process [[1,2],[1,3],[2,3]] and return [2,3] as the first redundant edge. Then union the chain 0-1 through 6-7 in order with union by rank and report maximum depth 1.

Traps

  • Rank without compression, or compression without rank: the adversarial chain union(0,1), union(1,2), ... builds depth \(n\) when attachment is one-sided, and compression never fires before the first find.
  • Recursive find on production-size chains: fine at LC 684 scale (\(n \le 1000\)), but the recursive form costs a frame per hop. Ship the iterative two-pass form past \(10^{5}\) elements.
  • String keys without make-set: integer constructors pre-fill every root, but email-keyed maps need an explicit make on first sighting or find raises KeyError.

Practice

LeetCode 684: Redundant Connection

Examples 1 ([[1,2],[1,3],[2,3]] answers [2,3]) and 2 (five-edge graph)

The first union returning False is the answer. After-attempt reference is the DSA Handbook 8.8 worked example and trace.

LeetCode 990: Satisfiability of Equality Equations

The two listed examples (a==b with b!=a answers False; the satisfiable set answers True)

Two passes: unions first, inequality checks second. After-attempt reference is the LC 990 section of DSA Handbook 8.8.

Self-test

  1. Derive the \(O(\log n)\) bound for union by rank alone.

  2. Why do two passes decide LC 990 correctly?

  3. Give two queries union-find cannot answer.

  4. What does \(\alpha(n) \le 4\) mean in practice?

Completion criterion

Stop when

Your blank-file DSU prints [2,3] on the redundant input with max chain depth 1, and LC 684 plus LC 990 pass with roots compared, never parents.

Mark passed, then open 3.6 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • DSA Handbook - graphs
    Handbook Academyprimarycourse
    Study all of Part 8 in order. For each algorithm, write the representation, invariant, forbidden input assumptions and independently checkable output before reading the implementation.
  • Algorithms - graph chapters
    Jeff Ericksonrequiredtheory
    Use the basic graph, depth-first search, minimum spanning tree, shortest path and flow chapters, and lecture note 11 Disjoint Sets for union-find, for correctness proofs and counterexamples to tempting wrong algorithms.

Also in this section

  • AlgoMaster DSA - graph slice
    AlgoMasterrequiredpractice
    Attempt Number of Islands, Course Schedule II and Network Delay Time cold. They cover traversal, dependency modeling and weighted shortest paths; add one narrative problem only if graph modeling remains weak.
  • Hypothesis - generated graph tests
    Hypothesissupportpractice
    Generate small sparse, dense, disconnected, cyclic, parallel-edge and weighted graphs. Compare optimized results with exhaustive or deliberately simple references and retain shrunk failures.
  • NeetCode - unseen graph problems
    NeetCodeoptionalassessment
    Use two unseen story problems without graph tags. Grade the model, assumptions, certificate and tests as well as the final answer.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels