chapter

    Directed Acyclic Graphs and Topological Ordering Notes for Gate Da

    Directed Acyclic Graphs and Topological Ordering notes for GATE DA: 8 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice

    Directed Acyclic Graphs and Topological Ordering notes

    Chapter Roadmap: Directed Acyclic Graphs and Topological Ordering

    Chapter Roadmap

    Directed Acyclic Graphs & Topological Ordering

    1
    The Foundation
    Directed Acyclic Graphs (DAGs) and real-world task scheduling intuition.
    2
    The Core Mechanism
    Formal definition of Topological Ordering and why cycles make it impossible.
    3
    The Algorithms
    Kahn's Algorithm (in-degree and queue) and DFS-based approach (post-order and stack).
    4
    Exam Mastery
    Identifying multiple valid sorts, detecting hidden cycles, and complexity analysis.

    Topic Hero: Topological Ordering of DAGs

    Topic Hero

    Topological Ordering of DAGs

    Topological ordering is a way to line up the vertices of a directed acyclic graph so that every directed edge points forward.

    Imagine getting dressed: you must put on your socks before your shoes. A topological sort gives you a valid sequence to complete all tasks without violating any prerequisites.

    Formal Definition of Topological Sort

    Formal Definition

    A topological sorting of a directed graph is a linear ordering of its vertices such that for every directed edge , vertex comes before vertex in the ordering.

    Critical Condition
    A topological ordering exists if and only if the graph has no directed cycles. Therefore, it is strictly defined for Directed Acyclic Graphs (DAGs).
    Key Properties:
    • The first vertex in the ordering must have an in-degree of $0$.
    • A DAG can have multiple valid topological orderings.
    • If a graph contains a cycle, no valid topological ordering exists.

    5 more cards in this chapter

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    Level 1: Warm-up
    Consider the following assertion and reason about Kahn's algorithm for topological sorting: Assertion (A): When Kahn's algorithm is executed on a directed acyclic graph (DAG) with vertices, exactly vertices are enqueued into the queue during the entire execution of the algorithm. Reason (R): This happens because each vertex's in-degree is decremented exactly once during the algorithm, causing it to be enqueued when its in-degree reaches $0$. Which of the following is correct?
    Question 2
    Level 1: Warm-up

    In a task scheduling problem modeled as a directed graph, a valid sequence of tasks that respects all prerequisites can be found using topological sorting. This is strictly possible only if the dependency graph is:

    Question 3
    Level 1: Warm-up

    In Kahn's algorithm for topological sorting, a count of processed vertices is maintained. If the algorithm terminates and the count is strictly less than the total number of vertices , it indicates a structural property of the graph. The number of vertices that were never processed is exactly:

    Question 4
    Level 1: Warm-up
    Consider the following assertion and reason about DFS-based topological sort: Assertion (A): In DFS-based topological sort, a vertex is pushed onto the stack only after all its descendants have been fully explored. Reason (R): This post-order approach ensures that a vertex appears before all vertices it depends on. Which of the following is correct?
    Question 5
    Level 1: Warm-up

    Which of the following scenarios is IMPOSSIBLE in any valid topological ordering of a directed acyclic graph (DAG) with at least 2 vertices?

    Question 6
    Level 1: Warm-up
    Consider the following statements about topological ordering of a directed acyclic graph (DAG): Statement P: For every directed edge in the graph, vertex must appear before vertex in any valid topological ordering. Statement Q: The first vertex in any valid topological ordering of a DAG must have in-degree equal to $0$. Statement R: If a directed graph contains a cycle, it can still have a valid topological ordering. Statement S: Two vertices with no directed path between them can appear in different relative orders in different valid topological orderings of the same DAG. Which of the above statements are TRUE?
    Question 7
    Level 1: Warm-up

    In the DFS-based topological sort algorithm, a stack is used to record vertices in post-order. When the algorithm is applied to a directed acyclic graph (DAG) with $8$ vertices, how many times is a vertex pushed onto the stack during the entire execution of the algorithm?

    Question 8
    Level 1: Warm-up

    Which of the following statements about a valid topological ordering of a directed acyclic graph (DAG) is ALWAYS true?

    Question 9
    Level 1: Warm-up

    Consider a directed acyclic graph (DAG) with vertices and directed edges , , , , and . In any valid topological ordering of this graph, what is the position (1-indexed) of vertex $5$?

    Question 10
    Level 1: Warm-up

    Consider a directed acyclic graph (DAG) with vertices and directed edges , , , , and . In any valid topological ordering of this graph, how many vertices must appear before vertex $5$?

    Free preview ends here

    Login to view the complete notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Directed Acyclic Graphs and Topological Ordering Notes for GATE DA

    Directed Acyclic Graphs and Topological Ordering notes for GATE DA: 8 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Chapter Roadmap: Directed Acyclic Graphs and Topological Ordering

    Chapter Roadmap

    Directed Acyclic Graphs & Topological Ordering

    1
    The Foundation
    Directed Acyclic Graphs (DAGs) and real-world task scheduling intuition.
    2
    The Core Mechanism
    Formal definition of Topological Ordering and why cycles make it impossible.
    3
    The Algorithms
    Kahn's Algorithm (in-degree and queue) and DFS-based approach (post-order and stack).
    4
    Exam Mastery
    Identifying multiple valid sorts, detecting hidden cycles, and complexity analysis.

    Topic Hero: Topological Ordering of DAGs

    Topic Hero

    Topological Ordering of DAGs

    Topological ordering is a way to line up the vertices of a directed acyclic graph so that every directed edge points forward.

    Imagine getting dressed: you must put on your socks before your shoes. A topological sort gives you a valid sequence to complete all tasks without violating any prerequisites.

    Formal Definition of Topological Sort

    Formal Definition

    A topological sorting of a directed graph is a linear ordering of its vertices such that for every directed edge , vertex comes before vertex in the ordering.

    Critical Condition
    A topological ordering exists if and only if the graph has no directed cycles. Therefore, it is strictly defined for Directed Acyclic Graphs (DAGs).
    Key Properties:
    • The first vertex in the ordering must have an in-degree of $0$.
    • A DAG can have multiple valid topological orderings.
    • If a graph contains a cycle, no valid topological ordering exists.

    Method 1: Kahn's Algorithm (In-Degree)

    Method 1: Kahn's Algorithm (In-Degree)

    This is the most intuitive, level-by-level method for topological sorting.

    The Algorithm:
    1. Compute the in-degree (number of incoming edges) for all vertices.
    2. Initialize a Queue and enqueue all vertices with an in-degree of $0$.
    3. Initialize a count of visited vertices to $0$.
    4. While the Queue is not empty:
      • Dequeue a vertex $u$ and append it to the topological order.
      • Increment count by $1$.
      • For each neighbor $v$ of $u$, decrease its in-degree by $1$.
      • If the in-degree of $v$ becomes $0$, enqueue $v$.
    5. If count is not equal to $V$ (total vertices), the graph contains a cycle.
    Complexity:
    Time: $\mathcal{O}(V + E)$ | Space: $\mathcal{O}(V)$ for the queue and in-degree array.

    Directed Acyclic Graphs and Topological Ordering: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Programming, Data Structures and Algorithms MSQ
    Consider the following assertion and reason about Kahn's algorithm for topological sorting: Assertion (A): When Kahn's algorithm is executed on a directed acyclic graph (DAG) with vertices, exactly vertices are enqueued into the queue during the entire execution of the algorithm. Reason (R): This happens because each vertex's in-degree is decremented exactly once during the algorithm, causing it to be enqueued when its in-degree reaches $0$. Which of the following is correct?
    1. A.

      Both A and R are true, and R is the correct explanation of A.

    2. B.

      Both A and R are true, but R is NOT the correct explanation of A.

    3. C.

      A is true, but R is false.

    4. D.

      A is false, but R is true.

    Correct Answer:

    ["C"]

    Step-by-Step Solution

    Key idea: This is an assertion-reason question testing understanding of Kahn's algorithm mechanics, specifically how in-degrees are decremented and vertices are enqueued.

    Step 1: Evaluate Assertion (A). In Kahn's algorithm on a DAG, we start by enqueuing all vertices with in-degree 0. Then, as we process each vertex, we decrement in-degrees of its neighbors, and enqueue any neighbor whose in-degree becomes 0. Since the graph is a DAG (no cycles), every vertex will eventually have its in-degree reduced to 0 and be enqueued. Therefore, exactly vertices are enqueued. Assertion (A) is TRUE.

    Step 2: Evaluate Reason (R). The reason claims "each vertex's in-degree is decremented exactly once." This is FALSE. A vertex's in-degree is decremented once for each incoming edge. If vertex has in-degree (meaning edges point to ), then 's in-degree will be decremented times — once when each of its predecessors is processed. Only when the final decrement brings the in-degree to 0 is enqueued.

    Step 3: The correct explanation for (A) is: In a DAG, every vertex is reachable from some vertex with in-degree 0, and processing all predecessors will eventually reduce every vertex's in-degree to 0, causing it to be enqueued. The reason (R) incorrectly states that each in-degree is decremented only once, which is an overcounting error.

    Step 4: Therefore, A is true but R is false.

    Answer: C (A is true, R is false)

    Common trap: Students may think "each vertex is enqueued once, so its in-degree is decremented once." But enqueuing happens once per vertex, while in-degree decrement happens once per incoming edge. These are different counts.

    Question 2 · Programming, Data Structures and Algorithms MCQ

    In a task scheduling problem modeled as a directed graph, a valid sequence of tasks that respects all prerequisites can be found using topological sorting. This is strictly possible only if the dependency graph is:

    1. A.

      A directed acyclic graph (DAG)

    2. B.

      A strongly connected graph

    3. C.

      A bipartite graph

    4. D.

      A complete graph

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a direct-recall question on the fundamental prerequisite for topological sorting.

    Topological sorting is defined as a linear ordering of vertices such that for every directed edge , comes before . If the graph contains a directed cycle, this condition cannot be satisfied because a vertex would need to appear before itself.

    Step 1: Recall the definition of topological sorting and its existence condition.

    Step 2: The existence of a topological ordering is equivalent to the graph being a Directed Acyclic Graph (DAG).

    Step 3: Strongly connected, bipartite, and complete graphs can all contain cycles and thus do not guarantee a topological ordering.

    Answer: A directed acyclic graph (DAG).

    Question 3 · Programming, Data Structures and Algorithms MCQ

    In Kahn's algorithm for topological sorting, a count of processed vertices is maintained. If the algorithm terminates and the count is strictly less than the total number of vertices , it indicates a structural property of the graph. The number of vertices that were never processed is exactly:

    1. A.

      The number of edges

    2. B.

      The number of vertices minus the count

    3. C.

      The count of processed vertices

    4. D.

      Zero

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a direct-recall question on the cycle detection mechanism in Kahn's algorithm.

    Kahn's algorithm processes vertices by adding them to the output when their in-degree becomes 0. If the graph has a cycle, the vertices in the cycle will never reach in-degree 0 and will not be processed.

    Step 1: The total number of vertices is .

    Step 2: The algorithm successfully processes count vertices.

    Step 3: The number of unprocessed vertices is simply the total minus the processed: .

    Answer: The number of vertices minus the count.

    Question 4 · Programming, Data Structures and Algorithms MCQ
    Consider the following assertion and reason about DFS-based topological sort: Assertion (A): In DFS-based topological sort, a vertex is pushed onto the stack only after all its descendants have been fully explored. Reason (R): This post-order approach ensures that a vertex appears before all vertices it depends on. Which of the following is correct?
    1. A.

      Both A and R are true, and R is the correct explanation of A.

    2. B.

      Both A and R are true, but R is NOT the correct explanation of A.

    3. C.

      A is true, but R is false.

    4. D.

      A is false, but R is true.

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This tests understanding of the DFS-based topological sort algorithm, specifically the timing of when vertices are pushed to the stack and why.

    Step 1: Evaluate Assertion (A). In DFS-based topological sort, we perform a post-order traversal. This means we recursively explore all descendants of a vertex before pushing the vertex itself onto the stack. So Assertion (A) is TRUE.

    Step 2: Evaluate Reason (R). The reason claims this ensures "a vertex appears before all vertices it depends on." But this is backwards. If vertex depends on vertex (edge ), then must appear BEFORE . The post-order approach actually ensures that a vertex appears AFTER all vertices it depends on (its prerequisites), not before.

    Step 3: More precisely, when we push vertex onto the stack, all its descendants (vertices reachable from ) have already been pushed. When we reverse the stack to get the final ordering, will appear BEFORE its descendants. But 's descendants are the vertices that depend ON , not the vertices that depends on.

    Step 4: So Reason (R) has the direction wrong. It should say "a vertex appears AFTER all vertices it depends on" or "BEFORE all vertices that depend on it."

    Answer: C (A is true, R is false)

    Common trap: Students often confuse "depends on" with "depended on by." The post-order ensures correct ordering, but the reason statement reverses the dependency direction.

    Question 5 · Programming, Data Structures and Algorithms MCQ

    Which of the following scenarios is IMPOSSIBLE in any valid topological ordering of a directed acyclic graph (DAG) with at least 2 vertices?

    1. A.

      The first vertex in the ordering has in-degree equal to 0.

    2. B.

      The last vertex in the ordering has out-degree equal to 0.

    3. C.

      A vertex with in-degree equal to 1 appears at the first position in the ordering.

    4. D.

      Two vertices with no directed path between them appear in different relative orders in two different valid topological orderings.

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This tests understanding of the necessary conditions for vertices in a topological ordering — specifically, what properties the first and last vertices must have.

    Step 1: Recall the necessary conditions for topological ordering:

    • The first vertex must have in-degree 0 (no prerequisites, so it can start immediately)
    • The last vertex must have out-degree 0 (nothing depends on it coming earlier)

    Step 2: Evaluate each option:

    • Option A: First vertex has in-degree 0. This is REQUIRED, so it's always possible. ✓
    • Option B: Last vertex has out-degree 0. This is REQUIRED, so it's always possible. ✓
    • Option C: A vertex with in-degree 1 at position 1. This is IMPOSSIBLE. If a vertex has in-degree 1, it has a prerequisite (some vertex with edge ). That prerequisite must appear before in the ordering. So cannot be first. ✗
    • Option D: Independent vertices in different orders. This is possible and common. If there's no directed path between and , they can appear in either order. ✓

    Step 3: Only Option C describes an impossible scenario.

    Answer: C

    Common trap: Students might think any vertex can be first, forgetting that vertices with in-degree > 0 have prerequisites that must come before them.

    Question 6 · Programming, Data Structures and Algorithms MSQ
    Consider the following statements about topological ordering of a directed acyclic graph (DAG): Statement P: For every directed edge in the graph, vertex must appear before vertex in any valid topological ordering. Statement Q: The first vertex in any valid topological ordering of a DAG must have in-degree equal to $0$. Statement R: If a directed graph contains a cycle, it can still have a valid topological ordering. Statement S: Two vertices with no directed path between them can appear in different relative orders in different valid topological orderings of the same DAG. Which of the above statements are TRUE?
    1. A.

      Statement P only

    2. B.

      Statements Q and S only

    3. C.

      Statements P and R only

    4. D.

      Statements Q, R, and S

    Correct Answer:

    ["B"]

    Step-by-Step Solution

    Key idea: This is a multi-statement evaluation question testing the formal definition and properties of topological ordering. Each statement must be checked independently against the definition and necessary conditions.

    Step 1: Evaluate Statement P. The formal definition states: for every directed edge , vertex comes BEFORE vertex in the ordering (not before ). Statement P reverses this direction. Therefore, Statement P is FALSE.

    Step 2: Evaluate Statement Q. The first vertex in a topological ordering has no prerequisites that must come before it. This means it must have in-degree 0 (no incoming edges). This is a necessary condition. Therefore, Statement Q is TRUE.

    Step 3: Evaluate Statement R. A topological ordering exists if and only if the graph is a DAG (has no directed cycles). If a cycle exists, vertices in the cycle have circular dependencies and cannot be linearly ordered. Therefore, Statement R is FALSE.

    Step 4: Evaluate Statement S. If there is no directed path between vertices and , they are independent. Their relative order is not constrained by any edge, so they can appear as or in different valid orderings. This is the basis for multiple valid topological sorts. Therefore, Statement S is TRUE.

    Step 5: The true statements are Q and S only.

    Answer: B (Statements Q and S only)

    Common trap: Statement P contains a sign error — reversing the edge direction. Students may think "if , then depends on , so should come first," but this is backwards. The arrow points from prerequisite to dependent, so must come first.

    Question 7 · Programming, Data Structures and Algorithms NAT

    In the DFS-based topological sort algorithm, a stack is used to record vertices in post-order. When the algorithm is applied to a directed acyclic graph (DAG) with $8$ vertices, how many times is a vertex pushed onto the stack during the entire execution of the algorithm?

    Correct Answer:

    8.00

    Step-by-Step Solution

    Key idea: This tests understanding of the DFS-based topological sort algorithm mechanics, specifically when and how many times vertices are pushed to the stack.

    Step 1: Recall the DFS-based topological sort algorithm. We perform a standard DFS traversal, but instead of recording vertices when we first visit them, we record them when we finish exploring all their descendants (post-order).

    Step 2: Understand the push operation. A vertex is pushed onto the stack only after all its neighbors (descendants) have been fully explored and pushed. This ensures that dependencies appear before dependents when the stack is reversed.

    Step 3: Count the pushes. Each vertex is pushed onto the stack exactly once — when we finish exploring it and all its descendants. Even though we may visit a vertex's neighbors multiple times during DFS, the vertex itself is pushed only once.

    Step 4: For a DAG with 8 vertices, each of the 8 vertices will be pushed exactly once onto the stack.

    Step 5: Total pushes = 8.

    Answer: 8.00

    Common trap: Students may think vertices are pushed multiple times (once when discovered, once when finished), or that the number of pushes depends on the number of edges. But in post-order DFS, each vertex is pushed exactly once, regardless of graph structure.

    Question 8 · Programming, Data Structures and Algorithms MCQ

    Which of the following statements about a valid topological ordering of a directed acyclic graph (DAG) is ALWAYS true?

    1. A.

      For every directed edge , vertex must appear before vertex in the ordering.

    2. B.

      For every directed edge , vertex must appear before vertex in the ordering.

    3. C.

      The first vertex in the ordering must have out-degree equal to 0.

    4. D.

      The last vertex in the ordering must have in-degree equal to 0.

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a direct definition check on topological ordering — the fundamental property that every edge must point forward in the sequence.

    Step 1: Recall the formal definition. A topological ordering is a linear arrangement of vertices such that for every directed edge , vertex comes before vertex .

    Step 2: Evaluate each option against this definition:

    • Option A reverses the direction (says before ), which violates the definition.
    • Option B matches the definition exactly: before for edge .
    • Option C is incorrect. The first vertex must have in-degree 0 (no prerequisites), not out-degree 0. It can have outgoing edges to later vertices.
    • Option D is incorrect. The last vertex must have out-degree 0 (nothing depends on it coming later), not in-degree 0. It can have incoming edges from earlier vertices.

    Step 3: Only Option B is always true by definition.

    Answer: B

    Common trap: Students often reverse the edge direction in their head, thinking "if , then depends on , so should come first." This is backwards — means is a prerequisite for , so must come first.

    Question 9 · Programming, Data Structures and Algorithms NAT

    Consider a directed acyclic graph (DAG) with vertices and directed edges , , , , and . In any valid topological ordering of this graph, what is the position (1-indexed) of vertex $5$?

    Correct Answer:

    5.00

    Step-by-Step Solution

    Key idea: This is a dependency chain analysis question. Vertex 5 has incoming edges, meaning it depends on other vertices that must come before it in any topological ordering.

    Step 1: Identify all vertices that must appear before vertex 5. Since there is an edge , vertex 4 must come before vertex 5.

    Step 2: Now find what must come before vertex 4. There are edges and , so both vertices 2 and 3 must come before vertex 4.

    Step 3: Find what must come before vertices 2 and 3. There are edges and , so vertex 1 must come before both vertices 2 and 3.

    Step 4: Build the dependency chain: vertex 1 must come before vertices 2 and 3, which must come before vertex 4, which must come before vertex 5.

    Step 5: Count how many vertices must appear before vertex 5: vertices 1, 2, 3, and 4 must all come before vertex 5. That's 4 vertices.

    Step 6: Therefore, vertex 5 must be at position 5 (after all 4 other vertices).

    Answer: 5.00

    Common trap: Students may reverse the edge direction, thinking "edge means 5 points to 4, so 5 should come first." This is backwards — the arrow points from prerequisite to dependent, so 4 must come before 5.

    Question 10 · Programming, Data Structures and Algorithms NAT

    Consider a directed acyclic graph (DAG) with vertices and directed edges , , , , and . In any valid topological ordering of this graph, how many vertices must appear before vertex $5$?

    Correct Answer:

    4.00

    Step-by-Step Solution

    Key idea: This is a dependency chain analysis question. We need to find all vertices that are ancestors of vertex 5 in the DAG by tracing backwards through the edges.

    Step 1: Identify direct predecessors of vertex 5. Edge means vertex 4 must come before vertex 5 in any topological ordering.

    Step 2: Find predecessors of vertex 4. Edges and mean vertices 2 and 3 must come before vertex 4.

    Step 3: Find predecessors of vertices 2 and 3. Edges and mean vertex 1 must come before both vertices 2 and 3.

    Step 4: Build the complete dependency chain: vertex 1 must come before vertices 2 and 3, which must come before vertex 4, which must come before vertex 5.

    Step 5: Count all vertices that must appear before vertex 5: vertices 1, 2, 3, and 4. That's 4 vertices.

    Answer: 4.00

    Common trap: Students may reverse the edge direction, thinking "edge means 5 points to 4, so 5 should come first." This is backwards — the arrow points from prerequisite to dependent, so 4 must come before 5. If you reverse all edges, you would incorrectly count successors instead of predecessors.

    More notes in this unit