19. Data Structures for Disjoint Sets
Exercises
19.1-1
Use the Data Structure Visualizations tool that preinitializes 16 disjoint sets as a starting point. Each member is denoted by an integer. For this example, just assign zero based indices to vertices. For example, 'a' would be represented with 0, 'b' with 1, etc. You would only use the first 11 elements (0..10). To process an edge convert vertices to indices. For example, the first edge would be a union between 3 and 8. You should end up with 3 disjoint sets (notice that 2 ('c') is a singleton set).
19.1-2
Available in the latest revision of the IM.
19.1-3
Available in the latest revision of the IM.
19.2-1
Available in the latest revision of the IM.
19.2-2
Available in the latest revision of the IM.
19.2-3
Available in the latest revision of the IM.
19.2-4
Available in the latest revision of the IM.
19.2-5
Available in the latest revision of the IM.
19.2-6
Available in the latest revision of the IM.
19.3-1
Available in the latest revision of the IM.
19.3-2
Available in the latest revision of the IM.
19.3-3
Available in the latest revision of the IM.
19.3-4 🌟
Available in the latest revision of the IM.
★ 19.3-5
Available in the latest revision of the IM.
19.4-1
Available in the latest revision of the IM.
19.4-2
Available in the latest revision of the IM.
19.4-3
Available in the latest revision of the IM.
19.4-4
Available in the latest revision of the IM.
19.4-5
Available in the latest revision of the IM.
19.4-6
Available in the latest revision of the IM.
★ 19.4-7
Available in the latest revision of the IM.
Problems
19-1 Offline minimum
Available in the latest revision of the IM.
19-2 Depth determination
Available in the latest revision of the IM.
19-3 Tarjan’s offline lowest-common-ancestors algorithm
a.
The LCA procedure executes a depth-first search traversal of the rooted tree . Consequently, each node is visited exactly once, and any given pair is examined only twice, once in each direction. During the first examination, the other element of the pair is still WHITE (not yet visited). Only upon the second examination are both elements colored BLACK, at which point their lowest common ancestor is printed.
b.
This follows from the DFS traversal. Lines 1–2 create a singleton set before recursing into any children. Children at the same level, sharing a parent, are processed sequentially. Line 5 merges each child’s set with the parent’s set before the next child is processed; therefore, the number of sets remains fixed throughout the level.
Because siblings are merged and only the nodes on the active DFS path remain unmerged, the only existing sets correspond exactly to and its ancestors. By definition, the number of ancestors is the depth of .
c.
At the exact moment LCA(u) is evaluating the pairs, the tree is divided into disjoint sets. For any fully processed (BLACK) node , FIND-SET(v).ancestor points to the lowest node on the current active recursion stack that is an ancestor of . Because node is the one currently being processed, the active recursion stack consists perfectly of and all of its ancestors. Therefore, the lowest node on the stack that is also an ancestor of is, by definition, the lowest common ancestor of and .
Let be the lowest common ancestor of and . Because line 10 only triggers when is currently executing and is already BLACK, we know finished its execution before .
This leaves exactly two possible structural scenarios:
Scenario 1: is a proper descendant of In this case, the lowest common ancestor is itself ().
Because is a descendant, the DFS traveled down from into one of its child branches and completely processed .
As the recursion unspooled back up that branch toward , line 5 (UNION) continuously merged the child sets into the parent sets.
Line 6 immediately reset the ancestor pointer of those newly merged sets to the parent.
Because we are currently inside
LCA(u), the child branch containing has just finished. The set containing has been merged into 's set, and its ancestor has been set to .Thus,
FIND-SET(v).ancestorcorrectly returns .
Scenario 2: and are in different branches of In this case, sits somewhere above .
The DFS reached first, then traveled down one child's branch and completely processed (coloring it BLACK).
The recursion returned to . At this moment, line 5 merged the entire branch containing into 's set, and line 6 explicitly set the ancestor of that combined set to .
The DFS then moved to a different child of and traveled down until it reached .
While we are evaluating pairs inside
LCA(u), the recursive callLCA(w)is still active on the stack waiting for 's branch to finish. Because has not finished, it has not yet been merged into its own parent.Therefore, the highest union that has participated in stopped exactly at .
Thus,
FIND-SET(v).ancestorcorrectly returns .
d.
We have a sequence of disjoint-set operations resulting in time. This dominates the overall running time, since the rest of the DFS traversal takes less time.
Last updated