Fully Dynamic Connectivity in Polylogarithmic Time
The full problem: a graph on vertices whose edges are inserted and deleted arbitrarily, with queries CONNECTED. Forests are easy with Euler-tour trees. General graphs are hard because of deletions of tree edges: when an edge of a spanning forest disappears, the two halves may still be connected through some non-tree edge, the replacement edge, and finding it seems to require looking at many edges.
The algorithm of Holm, de Lichtenberg and Thorup (2001), usually called HDT, solves this with
deterministically. This lesson is an overview: it explains the level structure and the amortization argument, and runs the level logic with plain adjacency sets in place of Euler-tour trees, so the program shows correctness and counts promotions rather than achieving the time bound.
Earlier approaches
| Result | Update time | Notes |
|---|---|---|
| recompute components | baseline | |
| Frederickson (1985) | worst case | topology trees on a spanning forest |
| Eppstein, Galil, Italiano and Nissenzweig (1997) | worst case | sparsification on top of Frederickson |
| Henzinger and King (1995) | expected amortized | randomized; introduced Euler-tour trees and levels |
| Holm, de Lichtenberg and Thorup (2001) | amortized | deterministic |
| Thorup (2000) | expected amortized | randomized |
| Wulff-Nilsen (2013) | amortized | deterministic |
| Kapron, King and Mountjoy (2013) | polylogarithmic worst case | Monte Carlo (may err with small probability) |
The lower bound of Pătrașcu and Demaine (2006) says cell probes are needed per operation (amortized, for the slower of updates and queries), so HDT is within a factor of optimal.
Levels
Each edge has a level . New edges start at level 0, and levels only increase. Let be the graph of edges with level at least , so . The algorithm maintains a spanning forest of and, for each , the forest of tree edges with level at least , each in its own Euler-tour tree structure. Two invariants hold at all times:
- Size: every connected component of has at most vertices. (So levels above are never needed.)
- Maximality: is a maximum spanning forest of when edges are weighted by their levels; in particular is a spanning forest of . Equivalently, if a non-tree edge has level , then and are connected in .
A query asks whether and are in the same tree of : one Euler-tour tree query, , or if the Euler-tour trees are B-trees of degree .
Updates
Insert : give it level 0. If and are not connected in , add it to (to 's Euler-tour trees). Otherwise it is a non-tree edge.
Delete a non-tree edge: remove it from the incidence lists. Done.
Delete a tree edge of level : remove it from . The tree of that contained it splits into and . By maximality, any replacement edge has level at most . Search for one level by level, from down to :
for i = l down to 0
let T be the smaller of the two trees T_u, T_v of F_i // |T| <= half of the old tree, so <= n / 2^(i+1)
raise every level-i tree edge of T to level i + 1 // keeps the size invariant at level i + 1
for each level-i non-tree edge (x, y) with x in T
if y is not in T // a replacement edge
make (x, y) a tree edge: add it to F_0, ..., F_i; stop
else
raise (x, y) to level i + 1 // both ends in T: pay for the scanRaising the edges of to level is allowed because has at most half the vertices of a component of , so at most , and the size invariant holds for . Raising a non-tree edge with both ends in keeps maximality, since its ends are now connected in .
Finding the edges fast. Each Euler-tour tree node of a vertex carries two flags: "this vertex has incident level- tree edges" and "this vertex has incident level- non-tree edges", and every BST node knows whether any node in its subtree is flagged. Then each edge to raise or scan is found in time by descending along flagged subtrees.
The amortized bound
Every scanned non-tree edge either is the replacement (once per deletion) or is raised one level. Every tree edge processed is raised. An edge's level never decreases and is at most , so each edge is raised at most times over its lifetime, and each raise costs (Euler-tour tree updates at one level). Charging the raises to the insertion of the edge:
The remaining work per deletion is levels times for the cut and the replacement, also .
The level logic in a program
The program keeps as adjacency sets and finds components by depth-first search, which costs far more than Euler-tour trees but runs exactly the same level decisions. It performs about 4500 random insertions and deletions on 64 vertices, checks every query against a search of the whole graph, and checks the size invariant every 500 steps.
import random
class LevelConnectivity:
"""Holm-de Lichtenberg-Thorup levels, with plain adjacency sets instead of Euler-tour trees."""
def __init__(self, n):
self.n, self.L = n, max(1, (n - 1).bit_length()) # levels 0..L-1 suffice: n / 2^L < 1
self.level, self.tree = {}, set()
self.F = [[set() for _ in range(n)] for _ in range(self.L + 1)] # F[i]: tree edges of level >= i
self.N = [[set() for _ in range(n)] for _ in range(self.L + 1)] # N[i][v]: non-tree edges of level i at v
self.promotions = 0
def _component(self, i, s):
seen, stack = {s}, [s]
while stack:
x = stack.pop()
for y in self.F[i][x]:
if y not in seen: seen.add(y); stack.append(y)
return seen
def connected(self, u, v):
return v in self._component(0, u) # an ETT query in the real structure
def insert(self, u, v):
e = (min(u, v), max(u, v))
self.level[e] = 0
if self.connected(u, v):
self.N[0][u].add(v); self.N[0][v].add(u)
else:
self.tree.add(e); self.F[0][u].add(v); self.F[0][v].add(u)
def delete(self, u, v):
e = (min(u, v), max(u, v))
l = self.level.pop(e)
if e not in self.tree:
self.N[l][u].discard(v); self.N[l][v].discard(u); return
self.tree.discard(e)
for i in range(l + 1):
self.F[i][u].discard(v); self.F[i][v].discard(u)
for i in range(l, -1, -1):
Tu, Tv = self._component(i, u), self._component(i, v)
T = Tu if len(Tu) <= len(Tv) else Tv # the smaller side: at most half
for x in T: # its level-i tree edges move up
for y in self.F[i][x]:
f = (min(x, y), max(x, y))
if x < y and self.level[f] == i:
self.level[f] = i + 1; self.promotions += 1
self.F[i + 1][x].add(y); self.F[i + 1][y].add(x)
for x in sorted(T): # scan level-i non-tree edges at T
for y in sorted(self.N[i][x]):
self.N[i][x].discard(y); self.N[i][y].discard(x)
f = (min(x, y), max(x, y))
if y not in T: # a replacement edge: reconnect at level i
self.tree.add(f)
for j in range(i + 1):
self.F[j][x].add(y); self.F[j][y].add(x)
return
self.level[f] = i + 1; self.promotions += 1 # both ends in T: push it up
self.N[i + 1][x].add(y); self.N[i + 1][y].add(x)
def check_invariant(self): # components of F[i] have <= n / 2^i vertices
for i in range(self.L + 1):
seen = set()
for s in range(self.n):
if s not in seen:
c = self._component(i, s); seen |= c
assert len(c) <= self.n >> i or len(c) == 1, (i, len(c))
rng = random.Random(2001)
n = 64
g, edges = LevelConnectivity(n), set()
adj = [set() for _ in range(n)]
def dfs(u, v):
seen, stack = {u}, [u]
while stack:
x = stack.pop()
for y in adj[x]:
if y not in seen: seen.add(y); stack.append(y)
return v in seen
updates = 0
for step in range(6000):
r = rng.random()
if r < 0.5 and len(edges) < 150:
u, v = rng.randrange(n), rng.randrange(n)
e = (min(u, v), max(u, v))
if u == v or e in edges: continue
g.insert(u, v); edges.add(e); adj[u].add(v); adj[v].add(u); updates += 1
elif r < 0.8 and edges:
e = sorted(edges)[rng.randrange(len(edges))]
g.delete(*e); edges.discard(e); adj[e[0]].discard(e[1]); adj[e[1]].discard(e[0]); updates += 1
else:
u, v = rng.randrange(n), rng.randrange(n)
assert g.connected(u, v) == dfs(u, v)
if step % 500 == 0: g.check_invariant()
print(f"{updates} updates on {n} vertices, every query agrees with DFS, invariant holds")
print(f"highest level used: {max(g.level.values())} (levels allowed: 0..{g.L})")
print(f"edge promotions: {g.promotions}, i.e. {g.promotions / updates:.2f} per update (each edge at most {g.L} times)")4549 updates on 64 vertices, every query agrees with DFS, invariant holds
highest level used: 3 (levels allowed: 0..6)
edge promotions: 1293, i.e. 0.28 per update (each edge at most 6 times)
The levels stay low because random graphs rarely force many raises, and each edge is raised far fewer than times on average. The amortized bound charges the worst case: an adversary can make edges climb all the way.
import math
# Amortized cost of HDT per update, in units of ETT operations of cost lg n each:
# every edge can be promoted at most lg n times, and a promotion costs O(lg n).
for n in [2 ** 10, 2 ** 20, 2 ** 30]:
lg = math.log2(n)
print(f"n = 2^{int(lg)}: levels {int(lg) + 1:2}, promotions per edge <= {int(lg):2}, "
f"update ~ lg^2 n = {lg ** 2:5.0f}, query ~ lg n / lg lg n = {lg / math.log2(lg):4.1f}, "
f"sqrt(n) = {math.sqrt(n):7.0f}")n = 2^10: levels 11, promotions per edge <= 10, update ~ lg^2 n = 100, query ~ lg n / lg lg n = 3.0, sqrt(n) = 32
n = 2^20: levels 21, promotions per edge <= 20, update ~ lg^2 n = 400, query ~ lg n / lg lg n = 4.6, sqrt(n) = 1024
n = 2^30: levels 31, promotions per edge <= 30, update ~ lg^2 n = 900, query ~ lg n / lg lg n = 6.1, sqrt(n) = 32768
For a million vertices, Euler-tour operations against for sparsification, and the gap grows quickly with . In practice the constants of HDT are moderate, and implementations (Iyer, Karger, Rahul and Thorup, 2001) found it competitive with simpler heuristics on hard inputs.
Note: Only connectivity is polylogarithmic. Dynamic versions of other problems behave differently: dynamic minimum spanning forest has deterministic amortized updates (HDT, same paper), dynamic 2-edge connectivity amortized, and for some problems, such as dynamic reachability in directed graphs, polylogarithmic updates are believed impossible under fine-grained hardness conjectures.
Test yourself
Why does HDT raise the tree edges of the **smaller** tree to level ?
- To shorten the search
- Because has at most half the vertices of its component of , so the size invariant still holds at level
- Because the larger tree is already at level
- To make the forest unique
Where does the amortized update bound come from?
- Each deletion scans edges
- Each edge is raised at most times, each raise costs in the Euler-tour trees, and raises are charged to insertions
- The Euler-tour trees have depth
- Queries cost
Which statement about lower bounds for dynamic connectivity is correct?
- per operation is possible
- Pătrașcu and Demaine proved cell probes per operation (amortized, for the slower of updates and queries)
- There is no known lower bound
- The lower bound is