Video failed to load

Delete Edges by Moving Through Time: Rollback DSU and Interval Decomposition

645 words
3 minutes
Delete Edges by Moving Through Time: Rollback DSU and Interval Decomposition

A disjoint-set union structure handles edge insertion efficiently, but deleting a graph edge is not the inverse of merging two components. A component may remain connected through another route. When all operations are known in advance, a segment tree over time turns deletion into leaving a recursive scope.

Model the lifetime first#

Number the query times 0,…,q−10,\ldots,q-1. If an edge is added at time ll and removed at time rr, it is active on [l,r)[l,r). An edge never removed ends at qq. Store an edge’s lifetime as an interval before building the time tree.

For a simple graph, normalize endpoints and track one active insertion per pair. If parallel edges are legal, either give each edge an ID or track a multiplicity. Closing the whole lifetime on the first removal would incorrectly delete remaining copies.

Each lifetime decomposes into O(log⁡q)O(\log q) canonical segment-tree nodes. During a depth-first traversal, entering a node adds its stored edges; exiting restores the previous state. At leaf tt, the root-to-leaf path contains exactly the edges active at time tt.

Time interval 0 to 8

0 to 4

4 to 8

0 to 2

2 to 4

4 to 6

6 to 8

Time interval 0 to 8

0 to 4

4 to 8

0 to 2

2 to 4

4 to 6

6 to 8

For example, lifetime [2,6)[2,6) is stored at nodes [2,4)[2,4) and [4,6)[4,6). It is present at times two through five and absent at six. Half-open intervals make the removal boundary explicit.

Rollback is a transaction, not arbitrary deletion#

Use union by size and no path compression. A successful merge changes only a root’s parent and another root’s size. Save both old values in a stack. A snapshot is the stack length, and rollback restores changes until that length is reached.

wsh::RollbackDSU dsu(vertex_count);
auto checkpoint = dsu.snapshot();
for (auto [u, v] : edges_at_this_node) dsu.unite(u, v);
// Recurse into children, or answer the leaf query.
dsu.rollback(checkpoint);

The companion header provides this structure. It records only successful merges. That is safe because rollback uses a stack-length checkpoint, not a count of attempted unions. If your API instead undoes one operation, failed unions need their own markers.

Why omit path compression?#

Path compression changes multiple parent pointers during a find. It can be made reversible by logging every mutation, but the usual amortized analysis no longer transfers automatically across branches that repeatedly restore old paths. Union by size alone guarantees depth O(log⁡n)O(\log n): every time a vertex gains an ancestor, its containing component at least doubles.

Therefore each find and union costs O(log⁡n)O(\log n) in the worst case, and rollback costs constant time per successful merge undone. With mm lifetimes, the total is O(mlog⁡qlog⁡n+q)O(m\log q\log n+q) plus the cost of queries, and interval storage is O(mlog⁡q)O(m\log q).

example#

Use vertices zero through three. Edge (0,1)(0,1) lives on [0,4)[0,4), (1,2)(1,2) on [1,3)[1,3), and (2,3)(2,3) on [2,5)[2,5). Vertices zero and three are connected only at time two. At time three, the middle edge has disappeared even though each outer edge is still present.

This is a useful test because a reversed-processing shortcut is insufficient when additions and removals are interleaved. It also catches an inclusive-endpoint bug, which would incorrectly report connectivity at time three.

Extending to bipartiteness#

Maintain the xor parity from a node to its root. An edge requires opposite endpoint colors. When roots differ, attach one and set its root parity to satisfy the constraint. When roots agree, compare the implied parity and update a contradiction counter if necessary.

Crucially, a contradictory edge changes state even though no merge occurs. Log that counter change too. At a time leaf, the graph is bipartite exactly when the counter is zero. Reusing the connectivity-only history stack without logging failed constraints will produce answers that leak across sibling time intervals.

Exercise. Add a self-loop for one time interval, then remove it. Verify that bipartiteness is false only inside that interval and that connectivity is unaffected. Then add a second parallel copy of a normal edge to test lifetime bookkeeping separately from the DSU.

Support & Share

If this article helped you, please share or support!

Sponsor
Delete Edges by Moving Through Time: Rollback DSU and Interval Decomposition
https://blog.wsh.moe/posts/rollback-time-tree/
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