Topological Ordering and Dependency Scheduling

Construct an order by repeatedly taking zero-indegree vertices and use unfinished work to detect a dependency cycle.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. The indegree tracks unresolved prerequisites
  2. A stalled remainder contains a cycle

The indegree tracks unresolved prerequisites

Represent a prerequisite by an edge from the completed task to its dependent. Initially queue vertices with no incoming edge. When taking a vertex, remove its outgoing edges conceptually by decrementing their targets’ remaining indegrees. A target becomes available when that count reaches zero. The invariant is that the queue contains exactly the remaining vertices with no unresolved prerequisite, apart from dequeue bookkeeping. Multiple available vertices can be chosen in any order. A heap can supply deterministic lexical ordering at additional cost.

A stalled remainder contains a cycle

If vertices remain and all have an incoming edge from within the remainder, repeatedly follow a predecessor. Finiteness forces a repeat, producing a directed cycle. Conversely, no member of a directed cycle can be the first member removed. The same ordering is obtained by reverse exits in depth-first search. DAG shortest paths and dependency-aware dynamic programming use the order to make every input value available before it is needed.[1][2]

References

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