Video failed to load

Partial Persistence Without Copying Every DSU Version

632 words
3 minutes
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 tt, follow a parent pointer only when attached_at[v] <= t.

find_at(v, t):
while attached_at[v] <= t:
v = parent[v]
return v

This 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.

attached at time 2

attached at time 3

Vertex 3

Vertex 2

Vertex 0

Query at time 2

Stop at vertex 2

Query at time 3

Continue to vertex 0

attached at time 2

attached at time 3

Vertex 3

Vertex 2

Vertex 0

Query at time 2

Stop at vertex 2

Query at time 3

Continue to vertex 0

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 vv at time tt, find its historical root rr, then binary-search the latest entry in rr‘s history whose timestamp is at most tt. 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 O(log⁡n)O(\log n). A historical query follows a prefix of such a chain, so find_at is also O(log⁡n)O(\log n). A size-history binary search adds O(log⁡n)O(\log n) time. There are at most n−1n-1 successful merges, hence only O(n)O(n) 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 u,vu,v and a threshold kk. Ask for the earliest time when the union of their connected components contains at least kk vertices. At time tt, 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 O(log⁡qlog⁡n)O(\log q\log n) 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!

Sponsor
Partial Persistence Without Copying Every DSU Version
https://blog.wsh.moe/posts/persistent-union-find/
Author
Wsh
Published at
2026-10-24

Comments

LEAVE A NOTE

Join the conversation

Markdown supported

Be kind. Good conversations start with you.

Conversation

Loading conversation…

Comments powered by Waline

Profile Image of the Author
Wsh
Nice to meet you! Hello, I'm Wsh.
Announcement
Any news or updates will be shared here, so check back anytime! Thanks for stopping by, meow >.<
Categories
Tags
Site Statistics
Posts
27
Categories
7
Tags
69
Total Words
124,615
Running Days
0 days
Last Activity
0 days ago
Site Info
Build Platform
Vercel
Blog Version
Firefly v6.16.8
License
CC BY-NC-SA 4.0
Table of Contents