CodeGraph/docs
Section 5.3

Kahn's Topological Sort

An in-degree-based topological sorting algorithm combined with graph inversion to compute dependency-first initialization sequences.

Source: server/src/analytics/ordering/DependencyOrderingAnalyzer.ts

Conceptual Inversion: Dependency Graph vs Ordering Graph

In standard code graphs, an edge $A \to B$ represents "A depends on B". However, for execution or initialization order, $B$ must be initialized before $A$.

CodeGraph models this inversion mathematically:

  • A node's in-degree in the ordering graph equals the number of its direct dependencies that are currently unprocessed.
  • Nodes with in-degree === 0 have zero outstanding dependencies and are immediately safe to emit.
  • When a node is emitted, the in-degree of all nodes that depend on it is decremented by 1.