Algorithm Search⌘K

Proofs: Ladder decomposition

These are the human proofs of Ladder decomposition, at full length.

The ladder algorithm returns the level ancestor full

This proof shows: correctness, termination.

Let the tree have vertices 0,…,n−10, \ldots, n - 1, n≥1n \ge 1, and root rr. Write depth(x)depth(x) for the number of edges from rr to xx, TxT_x for the subtree of xx, and h(x)h(x) for the number of vertices on a longest downward path from xx, so h(x)=1h(x) = 1 for a leaf and h(x)=1+max⁡ch(c)h(x) = 1 + \max_c h(c) over the children cc otherwise. For 0≤d≤depth(x)0 \le d \le depth(x), LA(x,d)\mathrm{LA}(x, d) is the unique ancestor of xx at depth dd. Lines L1 to L11 are those of the pseudocode on the page.

Lemma 1 (heights). After line L2, hh and longlong are as defined: long(v)long(v) is a child of vv with h(long(v))=h(v)−1h(long(v)) = h(v) - 1, for every non-leaf vv.

Proof. Reverse BFS order lists every vertex after all its children, so when vv is handled the values of its children are final. □\square

Lemma 2 (partition). Every vertex xx lies on the long path of exactly one head, and the long path of a head tt has exactly h(t)h(t) vertices, with depths depth(t),depth(t)+1,…,depth(t)+h(t)−1depth(t), depth(t) + 1, \ldots, depth(t) + h(t) - 1.

Proof. Walk up from xx while the current vertex is the long child of its parent; the walk stops at a head tt, and xx is on the long path of tt. Any head whose long path contains xx is found by this walk, because the long path of a head goes down through long children only, and a head is not the long child of its parent, so it is unique. Along the long path, hh decreases by exactly 1 per step (Lemma 1) and the path ends at a leaf, where h=1h = 1, so it has h(t)h(t) vertices, one per depth. □\square

Lemma 3 (ladders). For each head tt with long path PP, k=∣P∣=h(t)k = |P| = h(t), the ladder built in lines L5 and L6 is the list of the ancestors of the bottom vertex of PP at the depths top,top+1,…,depth(t)+k−1top, top + 1, \ldots, depth(t) + k - 1, where top=depth(t)−min⁡(k,depth(t))=max⁡(0,depth(t)−h(t))top = depth(t) - \min(k, depth(t)) = \max(0, depth(t) - h(t)). Hence for every x∈Px \in P and every dd with top≤d≤depth(x)top \le d \le depth(x), entry d−topd - top of the ladder is LA(x,d)\mathrm{LA}(x, d).

Proof. AA lists the min⁡(k,depth(t))\min(k, depth(t)) nearest proper ancestors of tt, topmost first, which are the ancestors of tt at depths top,…,depth(t)−1top, \ldots, depth(t) - 1; PP continues with one vertex per depth from depth(t)depth(t) down (Lemma 2), each the child of the one before. So the ladder lists one ancestor of the bottom vertex per depth, starting at depth toptop, and entry jj has depth top+jtop + j. For x∈Px \in P and d≤depth(x)d \le depth(x), the ancestor of the bottom vertex at depth dd is also the ancestor of xx at depth dd, since xx is itself on that vertical path. □\square

Lemma 4 (jump pointers). After line L7, up[j][v]up[j][v] is the 2j2^j-th ancestor of vv when 2j≤depth(v)2^j \le depth(v).

Proof. By induction on jj: up[0][v]up[0][v] is the parent, and the 2j2^j-th ancestor is the 2j−12^{j-1}-th ancestor of the 2j−12^{j-1}-th ancestor. When 2j≤depth(v)2^j \le depth(v) both intermediate steps stay inside the tree, so the clamping of the root at line L1 is never used. The table has ⌈lg⁡n⌉≥⌊lg⁡k⌋+1\lceil \lg n \rceil \ge \lfloor \lg k \rfloor + 1 levels for every k≤n−1k \le n - 1. □\square

Theorem. LEVEL-ANCESTOR(v,d)(v, d) returns LA(v,d)\mathrm{LA}(v, d) if 0≤d≤depth(v)0 \le d \le depth(v) and none otherwise, and all steps terminate.

Proof. Line L8 handles the invalid dd. Let k=depth(v)−d≥0k = depth(v) - d \ge 0; if k=0k = 0, line L9 returns v=LA(v,d)v = \mathrm{LA}(v, d). Otherwise let i=⌊lg⁡k⌋i = \lfloor \lg k \rfloor, so 2i≤k<2i+12^i \le k < 2^{i+1}, and u=up[i][v]u = up[i][v], the ancestor of vv at depth depth(v)−2i≥ddepth(v) - 2^i \ge d (Lemma 4). Then LA(v,d)=LA(u,d)\mathrm{LA}(v, d) = \mathrm{LA}(u, d), and depth(u)−d=k−2i<2idepth(u) - d = k - 2^i < 2^i.

The path from uu down to vv has 2i+12^i + 1 vertices, so h(u)≥2i+1h(u) \ge 2^i + 1. Let tt be the head of the long path containing uu. Since uu is on that path below tt, h(t)=h(u)+(depth(u)−depth(t))≥h(u)h(t) = h(u) + (depth(u) - depth(t)) \ge h(u). Now

depth(t)−d≤depth(u)−d<2i<h(u)≤h(t),depth(t) - d \le depth(u) - d < 2^i < h(u) \le h(t),

so top(u)=max⁡(0,depth(t)−h(t))≤dtop(u) = \max(0, depth(t) - h(t)) \le d. Also d≤depth(u)d \le depth(u). By Lemma 3, entry d−top(u)d - top(u) of the ladder of uu is LA(u,d)=LA(v,d)\mathrm{LA}(u, d) = \mathrm{LA}(v, d), which line L11 returns.

Termination: the BFS visits each vertex once, the walks of lines L4 and L5 have at most kk steps for a path of kk vertices, line L7 is a bounded double loop, and a query does a fixed number of steps. ■\blacksquare

Back to Ladder decomposition