Graph Theory37 sections · 1633 units
Open in Course

Query Example

(Finding 13th ancestor)

Say k=13k = 13 and you are at node vv. Binary: 13=1101213 = 1101_2. Bit 33 is set: jump 23=82^3 = 8 steps. Now v = up[v][3], k=5k = 5. Bit 22 is set: jump 22=42^2 = 4 steps.

Now v = up[v][2], k=1k = 1. Bit 00 is set: jump 20=12^0 = 1 step. Now v = up[v][0], k=0k = 0. You have jumped 8+4+1=138 + 4 + 1 = 13 steps in three jumps. Each jump uses one precomputed entry from the upup table, so no extra traversal is needed.