Skip to main content
PreparationZone Logo
PreparationZone
Back to Fundamentals

Fundamentals · Hard · Free

What is topological sort? Where is it used (build systems, tasks)?

FresherFundamentalsData Structures & Algorithms
Model answer

Topological sort

Linear ordering of DAG where every edge u→v has u before v. Kahn's (BFS indegree) or DFS postorder.

  • Build systems task dependencies
  • Course prerequisite scheduling
  • Detect cycle if not all nodes processed
java
// Kahn: queue nodes with indegree 0, reduce neighbor indegree
Say DAG requirement—cycle means no valid topo order.