Practice Discrete Math

Algorithms / Topological Sort Dag

Least You Need to Know: Topological Sort, DAG Dependencies, and Prerequisite Order

Topological sorting gives an order that respects directed dependencies: every edge points from something that must come earlier to something that may come later. It only works when the graph is a **DAG**.

The least you need to know

Key notation

u → v u must come before v
indegree(v) number of incoming prerequisite edges into v
DAG directed acyclic graph

Tiny worked example

  • Suppose courses have prerequisite edges `A → B` meaning A before B.
  • Any course with indegree 0 can be taken now.
  • Remove it, reduce indegrees of its outgoing neighbors, and continue.
  • If every course is removed, the produced order respects all prerequisites.

Common mistakes

How to recognize this kind of problem

Start practice