Recovering a Path from Predecessors

A search can store one predecessor per discovered vertex and reconstruct a path only for a requested target.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Walk backwards first
  2. Handle the source and missing target
  3. Read predecessor links backward and reverse the result
  4. Check source identity and unreachable targets separately

Walk backwards first

If D records C and C records A, tracing from D yields D, C, A. Reverse that sequence to report A, C, D. This is a concrete witness for reachability.

Handle the source and missing target

A route to the source itself has one vertex and no predecessor steps. A never-discovered target has no path under the search model. Preserve a distinct discovered state so both cases are represented. See reachability.[1]

Read predecessor links backward and reverse the result

A search reaches E through C and reaches C from A. The stored predecessor links point from E to C to A. Starting at E follows those links until the source and then reverses the collected order. See breadth-first search for where a predecessor can first be recorded.

predecessor[E] = C
predecessor[C] = A
Walk backward: E,C,A
Route forward: A,C,E

Check source identity and unreachable targets separately

A route from the source to itself can consist of one vertex and no edges. An unreachable target has no predecessor chain to the source. When checking stored output, assert that each restored consecutive pair is an actual edge in the original direction.

References

  1. Eric Lehman, F. Thomson Leighton and Albert R. Meyer, Mathematics for Computer Science ↩