Skip to main content
Back to the DSA atlas
Graphs Data structure Advanced

DSU Rollback

Add an undo stack to Union-Find so merges can be reversed during offline divide-and-conquer or time-travel style processing.

union-findrollbackrare

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

Rollback stack

merge 1-2
merge 2-3

Current connected groups

{1,2,3} {4}

Union-Find that can undo

Every successful merge records just enough information to restore parent and size data later. That makes divide-and-conquer-on-time and offline dynamic connectivity possible.

Family

Graphs

Nodes, edges, traversal, connectivity, and shortest paths.

Builds on

1 topic

Read these first if you want the surrounding context.

Unlocks

0 next topics

Use these follow-ups when you want to apply the current idea in a richer setting.

Learning paths

1

This topic appears in curated progressions so you can keep moving without guessing the next step.

Problem

Ordinary Union-Find is great at merging components, but it cannot undo merges efficiently.

Some offline connectivity problems need exactly that ability.

Core idea

Whenever a union actually changes the structure, push enough information onto a stack to restore the previous parents and sizes later.

Then rollback() simply pops and restores that state.

Why it matters

DSU rollback is the standard bridge from static connectivity to offline dynamic connectivity.

It often appears with:

  • divide and conquer on time
  • segment trees over active time intervals
  • batch add/remove edge processing

Complexity

OperationTime
Unionamortized near-constant without full path compression
RollbackO(1)O(1) per undone merge

Implementations usually avoid aggressive path compression because it is hard to undo cleanly.

Key takeaways

  • DSU rollback is Union-Find plus an undo log
  • The main sacrifice is that you cannot rely on ordinary destructive path compression
  • This is rare advanced graph/offline material, but it unlocks whole classes of dynamic connectivity problems
  • It is conceptually related to persistence, but implemented as reversible mutation rather than structural sharing

Practice problems

ProblemDifficultyKey idea
Dynamic Connectivity Offline references🔴 HardSegment tree on time plus rollback DSU
Connect and Disconnect references🔴 HardUndoable unions over time
Rollback DSU tutorials🔴 HardStack-based reversion

Relation to other topics

  • Union-Find is the base structure that rollback extends
  • Mo’s Algorithm is another offline reordering technique where online answers are replaced by clever scheduling
  • Persistent Segment Tree offers a different history-preserving style through structural sharing instead of explicit rollback

Build on these first

These topics supply the mental model or underlying structure that this page assumes.

Related directions

These topics live nearby conceptually, even if they are not direct prerequisites.

More from Graphs

Stay in the same family when you want parallel variations of the same mental model.

Paths that include this topic

Follow one of these sequences if you want a guided next step instead of open-ended browsing.

Offline toolkit

See how reordering queries, rolling back state, and preserving versions can replace online complexity.

From the blog

Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.