Registry Synced

DAGs and Topological Sorting

768 words
4 min read

Reading compass

Now ยท ๐ŸŽฏ Learning Objectives

DAGs and Topological Sorting

๐ŸŽฏ Learning Objectives

By the end of this topic, you will be able to:
  1. Define a Directed Acyclic Graph (DAG)
  2. Perform topological sorting using Kahn's algorithm and DFS
  3. Apply DAGs to model dependencies and scheduling
  4. Detect cycles in directed graphs

๐Ÿ“‹ Prerequisites


๐Ÿ“– Core Content

20.1 What is a DAG?

A Directed Acyclic Graph (DAG) is a directed graph with no cycles. This means you can never start at a vertex, follow directed edges, and return to it. Real-world DAGs:
  • Course prerequisites (Math 1 โ†’ Math 2 โ†’ Advanced ML)
  • Build dependencies (compile A before B which depends on A)
  • Git version history (commits form a DAG)
  • Family tree (ancestor โ†’ descendant)
๐Ÿ’ก Why this matters: DAGs model any system with dependencies and partial order. Understanding DAGs is essential for dependency management, build systems, and scheduling.

20.2 Properties of DAGs

  • Every DAG has at least one source (vertex with in-degree 0)
  • Every DAG has at least one sink (vertex with out-degree 0)
  • A DAG can be topologically sorted (linear ordering respecting edge directions)
  • A directed graph is a DAG iff it has a topological ordering

20.3 Topological Sorting

A topological sort is a linear ordering of vertices such that for every directed edge uโ†’vu \to v, uu comes before vv in the ordering. (Diagram) Topological order: [Mathย 1,Statsย 1,Mathย 2,MLย Foundation][\text{Math 1}, \text{Stats 1}, \text{Math 2}, \text{ML Foundation}]

20.4 Kahn's Algorithm (BFS-based)

Algorithm:
  1. Compute in-degree of all vertices
  2. Enqueue all vertices with in-degree 0
  3. While queue not empty:
    • Dequeue vertex vv, add to result
    • For each neighbor ww of vv: decrement in-degree of ww
    • If in-degree of ww becomes 0, enqueue ww
  4. If result size < number of vertices โ†’ graph has a cycle! Time complexity: O(V+E)O(V + E)

20.5 DFS-based Topological Sort

Algorithm:
  1. Run DFS on the graph
  2. Add vertex to a stack after all its neighbors are processed (post-order)
  3. Pop from stack to get topological order

20.6 Cycle Detection

If Kahn's algorithm terminates with fewer than โˆฃVโˆฃ|V| vertices sorted, the graph has a cycle. Example: 1โ†’21 \to 2, 2โ†’32 \to 3, 3โ†’13 \to 1 has no vertex with in-degree 0 โ†’ cycle detected!

๐Ÿ“ Key Formulas โ€” Summary Table

ConceptDescription
DAGDirected graph with no cycles
Topological sortLinear ordering uu before vv for all uโ†’vu \to v
Kahn's algorithmBFS-based, uses in-degree
DFS topologicalPost-order DFS traversal
Cycle detectionKahn's: incomplete sort = cycle
SourceVertex with in-degree 0
SinkVertex with out-degree 0

โš ๏ธ Common Pitfalls

Pitfall 1: Topological Sort โ‰  Unique

Multiple valid topological orderings can exist for the same DAG (any ordering respecting the partial order is valid).

Pitfall 2: Only DAGs Have Topological Sorts

A graph with a cycle has NO topological ordering (since a cycle would require vv before uu and uu before vv).

Pitfall 3: Confusing Sources and Sinks

Sources have in-degree 0 (no prerequisites). Sinks have out-degree 0 (no dependents).

๐Ÿ“ Practice Questions

Q1: Is a DAG with a single vertex a DAG?
Yes โ€” no cycle possible with one vertex.
Yes\boxed{\text{Yes}} Q2: Find a topological order for 1โ†’21\to2, 1โ†’31\to3, 2โ†’42\to4, 3โ†’43\to4.
[1,2,3,4][1, 2, 3, 4] or [1,3,2,4][1, 3, 2, 4]
[1,2,3,4]ย orย [1,3,2,4]\boxed{[1,2,3,4] \text{ or } [1,3,2,4]} Q3: Does a complete directed graph on 3 vertices contain a cycle?
Yes โ€” it's a directed cycle of length 3.
Yes\boxed{\text{Yes}} Q4: What data structure does Kahn's algorithm use?
Queue.
Queue\boxed{\text{Queue}} Q5: In Kahn's algorithm, if result has fewer than โˆฃVโˆฃ|V| elements, what does it mean?
The graph has a cycle.
Cycleย detected\boxed{\text{Cycle detected}} Q6: Give an application of topological sorting.
Course prerequisite ordering, build dependency resolution, task scheduling.
Dependencyย resolution\boxed{\text{Dependency resolution}} Q7: How many sources must a DAG have?
At least one.
Atย leastย 1\boxed{\text{At least 1}} Q8: Does every DAG have a unique topological ordering?
No โ€” multiple valid orderings typically exist.
No\boxed{\text{No}}

๐Ÿ”— Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.