Depth-First Search and Edge Classification

Use entry and exit times to distinguish ancestry, recognize back edges and reason about directed cycles.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. The recursion stack is the active path
  2. Find a cycle or certify an order

The recursion stack is the active path

On visiting a vertex, note its entry time, explore its undiscovered outgoing neighbors, and note its exit. An ancestor and descendant have nested time intervals. Vertices in separate completed branches have disjoint intervals. In a directed graph, an edge to an active ancestor closes a cycle with the stack path. An edge to an already finished vertex does not give that conclusion. Its target can be a descendant or in a separate branch.

Find a cycle or certify an order

If a directed search finishes without a back edge, every edge goes from a later exit to an earlier exit. If an unvisited vertex is reached, it must finish before its caller. If the target had finished earlier, the claim already holds. Sorting by decreasing exit therefore yields a topological order. For large input graphs an explicit stack must remember the next adjacency edge to inspect, not just vertices. Strongly connected components also interpret exit order, there on the graph of components.[1][2]

References

  1. Jeff Erickson, Algorithms ↩
  2. MIT OpenCourseWare, Design and Analysis of Algorithms: Lecture Notes ↩