Quiz 2

DAGs and Topological Sorting

768 words
4 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# 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.

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 uvu \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: 121 \to 2, 232 \to 3, 313 \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 uvu \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 121\to2, 131\to3, 242\to4, 343\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
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.