Partial Persistence Without Copying Every DSU Version
Rollback lets a traversal revisit earlier states by undoing operations. Partial persistence answers old-version queries while the newest state continues to grow. These interfaces solve different problems. Here we derive a historical DSU for insertion-only connectivity, then use it as a component in more demanding offline queries.
Observe what can change only once
With union by size and no path compression, a root may be attached under another root. Once attached, it never becomes a root again in the insertion-only process. Its parent pointer is assigned exactly once.
Record parent[v] and attached_at[v]. A root that has never been attached has timestamp infinity. To find a historical root at time , follow a parent pointer only when attached_at[v] <= t.
find_at(v, t): while attached_at[v] <= t: v = parent[v] return vThis query does not mutate anything. A pointer established in the future is simply invisible to the old version.
timeline
Start with five isolated vertices. At time one, merge zero with one. At time two, merge two with three. At time three, merge one with two. At time four, merge three with four.
Suppose ties consistently keep the smaller representative. Vertex three attaches to two at time two, and two attaches to zero at time three. Querying find_at(3, 2) follows the first pointer but stops before the second. At time three, it follows both. Historical connectivity comes from comparing these roots.
The representative identity is an implementation detail. The partition of vertices, not the particular root number, is the observable result.
Recovering historical component sizes
The parent timestamps are enough for connectivity but not size. A root’s size may grow many times before it is attached. Store a sorted vector of pairs (time,size) for each root, initially (0,1). Every successful union appends the new size to the surviving root’s history.
To get the size of vertex at time , find its historical root , then binary-search the latest entry in ‘s history whose timestamp is at most . Do not read the current size array: that answers a different version.
If an attempted union joins already connected vertices, decide whether your public version counter still advances. Either convention works, but all timestamps and external query times must use the same convention. Assigning time only to successful unions while the input numbers all operations is a common indexing error.
Time and space bounds
Union by size ensures every current parent chain has length . A historical query follows a prefix of such a chain, so find_at is also . A size-history binary search adds time. There are at most successful merges, hence only parent assignments and total history entries, aside from storage for external queries.
This structure does not permit updates to arbitrary past versions. That would be full persistence and needs a different representation. It also does not support edge deletion. Parent timestamps alone cannot represent a component splitting apart.
A harder use: when did two growing components become large enough?
Suppose a query gives vertices and a threshold . Ask for the earliest time when the union of their connected components contains at least vertices. At time , compute their historical roots. If equal, count one component; otherwise add the two historical sizes.
The predicate is monotone because insertion-only components only merge. Binary-search time, using the persistent DSU to evaluate each midpoint. This gives per query after preprocessing. Check the final version first; the threshold may never be reached.
Exercise. Implement that predicate and test the case where the two vertices start in distinct components of sizes two and three, then become connected. The union size should not suddenly double after their roots become equal.
Support & Share
If this article helped you, please share or support!





























































Conversation
Comments powered by Waline