|
ADS
Algorithms and Data Structures
|
The headers of include/ADS/mfset realize the MFSET of section 10.2 of the book, the union of disjoint sets, on lists (section 10.2.1) and on a forest of parents (section 10.2.2) with the union by rank and the compression of the paths (sections 10.2.3 and 10.2.4), and, beyond the book, two engineered forests (one in a single vector of integers, with the union by size and the halving of the paths, and the algorithm of Rem with splicing) and a forest whose merges can be undone, with the connectivity of a graph whose edges come and go. This page collects the definitions and the notation that the headers share (see Definitions and notation), the realizations with the costs of their operations (see Realizations and their costs), and the arguments behind the bounds of the heuristics (see Unions by size and by rank and Compression, halving and splicing of the paths) and of the undo (see Undo of the merges and offline connectivity). The pseudocode of each operation is on the page of its header, in the comment of its function, together with the section of the book, or the paper, that it follows.
An MFSET (merge-find set) is a partition of the elements \( \{ 0 , \ldots , n - 1 \} \) into disjoint components, which starts with the \( n \) singletons \( \{ 0 \} , \ldots , \{ n - 1 \} \) and changes by two operations: \( \mathrm{find}( x ) \) gives the representative of the component of \( x \), an element of it that stays the same until the component changes, so that \( x \) and \( y \) are in the same component if and only if \( \mathrm{find}( x ) = \mathrm{find}( y ) \); and \( \mathrm{merge}( x , y ) \) replaces the components of \( x \) and of \( y \) with their union. Components are never split. The MFSET follows the connected components of a graph whose edges arrive one at a time, i.e., the cycles that the algorithm of Kruskal must refuse (see Minimum spanning trees), and the classes of an equivalence relation given by its pairs.
| Symbol | Meaning |
|---|---|
| \( n \), \( m \) | the number of elements, and the number of operations of a sequence of finds and merges |
| \( p[ x ] \) | the parent of \( x \) in the forest, \( p[ x ] = x \) (or a negative number in UnionFind) at a root, the representative |
| \( \mathit{rank}[ r ] \) | the rank of the root \( r \), an upper bound to the height of its tree |
| \( \mathit{size}[ r ] \) | the number of nodes of the tree of the root \( r \) |
| \( \alpha( m , n ) \) | the inverse of the function of Ackermann, at most 4 for any \( m \) and \( n \) that fit in a computer |
| Realization | Representation | find() | merge() | \( m \) operations |
|---|---|---|---|---|
| ListMFSet | a list per component, each element with the head of its list | \( O( 1 ) \) | \( O( n ) \), the shorter list relabeled | \( O( m + n \log n ) \) |
| MFSet, union NONE, no compression | a forest of parents | \( O( n ) \) | that of two finds | \( O( m n ) \) |
| MFSet, union RANK or SIZE | as above | \( O( \log n ) \) | that of two finds | \( O( m \log n ) \) |
| MFSet by rank or size with compression, UnionFind | as above, or one vector with the sizes at the roots | \( O( \log n ) \) | that of two finds | \( O( m \, \alpha( m , n ) ) \) |
| RemUnionFind | a forest with \( p[ x ] \ge x \), linked by index | \( O( n ) \) | one interleaved climb | \( O( m \log_{1 + m / n} n ) \), \( O( m \, \alpha( m , n ) ) \) expected on a random numbering |
| RollbackUnionFind | a forest by rank, no compression, and a stack of the merges | \( O( \log n ) \) in the worst case | that of two finds; undo() in \( O( 1 ) \) | \( O( m \log n ) \) |
On lists, find() reads the head that each element keeps, and merge() appends one list to the other and relabels the elements of the appended one; on a forest, find() climbs from \( x \) to the root of its tree, and merge() makes one root a child of the other, after the two finds. The two realizations trade one cost for the other, and the heuristics below bring the forest to a cost per operation that is constant in practice.
Lemma 1 (the relabeling of the shorter list, Hopcroft and Ullman). If merge() relabels the elements of the shorter list, an element is relabeled at most \( \lfloor \log_2 n \rfloor \) times, and any sequence of \( m \) operations costs \( O( m + n \log n ) \).
Indeed, an element is relabeled only when its list is the shorter of the two, and then the list it lands in is at least twice as long as its old one; a list has at most \( n \) elements, so that this happens at most \( \log_2 n \) times per element. The finds cost \( O( 1 ) \) each.
On a forest with no heuristic, \( n - 1 \) merges may build a path of \( n \) nodes, whose deepest node costs \( n - 1 \) steps to every find. The union by rank keeps for each root \( r \) a rank, 0 for a singleton, and puts the root of lower rank under the other one, the rank growing by 1 only when the two ranks are equal; the union by size puts the tree with fewer nodes under the other one.
Lemma 2 (Theorem 10.1 of the book). With the union by rank, a tree whose root has rank \( r \) has at least \( 2^r \) nodes and height at most \( r \); hence \( r \le \log_2 n \), and find() and merge() take \( O( \log n ) \).
Indeed, by induction on the merges: a singleton has rank 0 and one node; a merge of two roots of different ranks keeps the rank of the higher root and adds nodes to its tree, whose height stays at most that rank, since the lower tree goes one level down; a merge of two roots of equal rank \( r \) gives rank \( r + 1 \) and height at most \( r + 1 \) to a tree of at least \( 2^r + 2^r = 2^{r+1} \) nodes. With the union by size the same bound holds by the argument of Lemma 1: a node goes one level down only when its tree joins one at least as large, i.e., when the tree that holds it at least doubles, so that its depth is at most \( \log_2 \mathit{size}[ r ] \).
A find that has climbed from \( x \) to its root \( r \) may make each node of the path a child of \( r \) (the compression of section 10.2.4), in a second pass, so that the next finds from those nodes cost one step. The halving makes every other node of the path a child of its grandparent instead, in the same pass that climbs, and the splicing of Rem replaces a parent with a larger parent of the other element while two climbs go on together; both shorten every path they walk by about half.
Theorem 3 (Tarjan, Tarjan and van Leeuwen). With the union by rank or by size, and the compression or the halving of the paths, a sequence of \( m \ge n \) operations on \( n \) elements costs
\[ O( m \, \alpha( m , n ) ) , \tag{1} \]
and the bound is tight for each of these combinations (Tarjan; Tarjan and van Leeuwen).
The argument, which the book sketches and Tarjan and van Leeuwen give in full, charges each step of a find either to the operation, at most a constant number of steps per operation, or to the node, whose parent then changes to one of much larger rank; since the ranks along a path increase, and a node of rank \( r \) is one of at most \( n / 2^r \), the charges to the nodes sum to \( O( m \, \alpha( m , n ) ) \) over the levels of the inverse of the function of Ackermann. The bound (1) is \( O( m ) \) in practice, since \( \alpha \) is at most 4 for every input that fits in a computer; a single find may still cost \( \Theta( \log n ) \).
Of the two engineered forests of the area, UnionFind keeps (1) and saves memory and a pass, while RemUnionFind saves the sizes and gives up (1) in the worst case. UnionFind keeps the parents and, at each root, minus the size of its tree in one vector of integers, so that a find touches one array, and halves the paths in the single pass of the climb. RemUnionFind replaces the union by rank with the link by index: every parent is at least its child, \( p[ x ] \ge x \), so that a path increases strictly to its root, and merge() climbs from \( x \) and \( y \) together, always from the node of smaller parent, which, if it is a root, is linked under the parent of the other node; otherwise its parent is replaced by the larger parent of the other one, which keeps \( p[ x ] \ge x \). The climb stops as soon as the two components are joined, or the two nodes reach the same parent, often well below the roots; its postcondition, that the two have one root, climbs with no halving, so that the contracts leave the forest that the algorithm makes. The link by index alone gives the worst-case bound \( O( m \log_{1 + m / n} n ) \) (Tarjan and van Leeuwen), and the bound (1) in expectation when the elements are numbered in a random order (Goel, Khanna, Larkin and Tarjan); the experiments of Patwary, Blair and Manne found it among the fastest in practice.
Beyond the book, RollbackUnionFind undoes its merges in the reverse of their order: each merge() changes at most three words, the parent of the root that it hangs and the size and the rank of the other root, and pushes them on a stack, from which undo() puts them back in \( O( 1 ) \); rollback( t ) undoes the merges back to the checkpoint \( t \), the number of merges done when checkpoint() gave it. The forest keeps the union by rank and gives up any change of the paths: a compression would change \( \Theta( \log n ) \) parents per find, all to be recorded, and its amortized bound (1) does not survive the undo, since a sequence that rolls back the merges and does them again can make the same long find pay each time. With no compression, Lemma 2 bounds every find by \( O( \log n ) \) in the worst case, not only amortized, and an undo restores exactly the forest before the merge, heights and ranks included; Westbrook and Tarjan show that some forms of compression, adapted to the undo, give \( O( \log n / \log \log n ) \) amortized time per operation, optimal for the separable algorithms on pointers; the simpler forest keeps its \( O( \log n ) \) for each operation.
The undo makes it possible to answer offline the questions of connectivity on a graph whose edges come and go (offlineConnectivity()), when the whole sequence of \( k \) insertions, deletions and questions is known in advance. Each edge lives on an interval of times \( [ s , e ) \), from its insertion to its deletion (or to the end), and the \( q \) questions are the leaves of a segment tree, whose node of range \( [ a , b ) \) holds the list \( L \) of the edges that live on all of \( [ a , b ) \) and not on all of the range of its parent. A visit in depth of the segment tree merges the edges of \( L \) when it enters a node and rolls them back when it leaves it.
Lemma 4 (the segment tree over the times). An interval of times is held by at most \( \max( 1 , 2 \lceil \log_2 q \rceil ) \) nodes of the segment tree, and when the visit reaches the leaf of a question the forest holds exactly the edges alive at its time; hence the answers are those of a union-find built again at each question, and the time is, in expectation over the hash table that pairs each deletion with its insertion,
\[ O( k \log q \log n ) = O( k \log k \log n ) . \tag{2} \]
Indeed, at each level of the segment tree at most two nodes hold part of the interval without holding all of it, as in the decomposition of a range by a segment tree, so that the interval is split into at most two nodes per level. The edges merged at the leaf are those of its ancestors, i.e., those whose interval holds a range around the leaf, which are the edges alive at its time, and the rollback when leaving a node restores the forest of its parent. Each edge is merged and undone \( O( \log q ) \) times, at \( O( \log n ) \) each by Lemma 2, which gives (2), with \( O( k \log k ) \) space for the lists. The divide and conquer on the times is the one of Eppstein for the dynamic minimum spanning tree; with the contraction of the graph at each level (Eppstein, and Peng, Sandlund and Sleator) the bound falls to \( O( k \log n ) \), at the price of a far longer code, which the rollback avoids.