chapter
    Directed Acyclic Graphs and Topological Ordering Short Notes for GATE DA

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

    directed acyclic graphs and topological ordering short notes

    Exam Readiness: Topological Sort Checklist

    Exam Readiness Checklist

    Applicability: Only valid for Directed Acyclic Graphs (DAGs).
    Kahn's Algorithm: Uses a Queue and in-degrees. Best for cycle detection and counting sorts.
    DFS Algorithm: Uses a Stack and post-order traversal. Best for concise recursive implementation.
    Complexity: Both methods run in time and auxiliary space.
    Cycle Check: If the final sorted list length , a cycle is present.
    Multiple Sorts: Arise from independent vertices (no path between them). Verify options by checking every directed edge .

    Try a question

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

    Question 1
    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 2
    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 3
    Level 1: Warm-up

    Which of the following is NOT a valid property of every topological ordering of a DAG?

    Question 4
    Level 1: Warm-up

    According to the formal definition of topological sort, the first vertex in any valid topological ordering of a directed graph must satisfy a specific condition regarding its in-degree. The maximum possible value of this in-degree is:

    Question 5
    Level 1: Warm-up

    In the DFS-based topological sort algorithm, the timing of when a vertex is pushed onto the stack is crucial for correctness. A vertex is pushed onto the stack:

    Question 6
    Level 1: Warm-up

    Kahn's algorithm is executed on a directed graph with vertices. The algorithm terminates and the result list contains exactly vertices. The number of vertices that were never enqueued during the execution is ______.

    Question 7
    Level 1: Warm-up

    A topological ordering of a directed graph is a linear arrangement of its vertices such that every directed edge points forward. If a graph has 3 vertices and directed edges and , the total number of valid topological orderings is:

    Question 8
    Level 1: Warm-up

    Consider the Kahn's algorithm trace for a DAG with vertices A, B, C, D and edges A->B, A->C, B->D, C->D.

    Assertion (A): Vertex D is enqueued into the queue only after both B and C have been processed.

    Reason (R): The initial in-degree of D is 2, and it is reduced by 1 each time a predecessor is processed.

    Question 9
    Level 1: Warm-up

    A DAG has vertices , , with directed edges and . Match each vertex pair with the nature of its relative order across all valid topological sorts of this DAG.

    Question 10
    Level 1: Warm-up

    Consider a directed acyclic graph with vertices and directed edges , , and .

    Match the vertex pairs in Column I with the existence of a directed path between them (Column II).

    Column I: (i) and , (ii) and , (iii) and

    Column II: (P) A directed path exists between them, (Q) No directed path exists between them

    Free preview ends here

    Login to view the complete short 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.

    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 Short Notes for GATE DA

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

    Exam Readiness: Topological Sort Checklist

    Exam Readiness Checklist

    Applicability: Only valid for Directed Acyclic Graphs (DAGs).
    Kahn's Algorithm: Uses a Queue and in-degrees. Best for cycle detection and counting sorts.
    DFS Algorithm: Uses a Stack and post-order traversal. Best for concise recursive implementation.
    Complexity: Both methods run in time and auxiliary space.
    Cycle Check: If the final sorted list length , a cycle is present.
    Multiple Sorts: Arise from independent vertices (no path between them). Verify options by checking every directed edge .

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

    Question 1 · 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 2 · 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 3 · Programming, Data Structures and Algorithms MCQ

    Which of the following is NOT a valid property of every topological ordering of a DAG?

    1. A.

      Every vertex of the DAG appears exactly once in the ordering

    2. B.

      For every directed edge , vertex appears before vertex

    3. C.

      The first vertex in the ordering has in-degree in the original graph

    4. D.

      The last vertex in the ordering has in-degree in the original graph

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: this is a definition-boundary question — check each property against the formal definition of topological sort. Step 1: Recall the definition. A topological ordering is a linear ordering of all vertices such that for every edge , comes before . Step 2: Evaluate each option. - (A) "Every vertex appears exactly once" — true by definition of a linear ordering of all vertices. - (B) "For every edge , before " — this is the defining property itself. True. - (C) "First vertex has in-degree " — true. If the first vertex had an incoming edge from some , then would need to appear before it, contradicting it being first. - (D) "Last vertex has in-degree " — false. The last vertex must have out-degree (no edges leaving it, otherwise the target would need to come after it, but it is last). Its in-degree can be anything. Answer: D Common trap: confusing in-degree with out-degree for the last vertex. The first vertex has in-degree ; the last vertex has out-degree . Verification: Consider the DAG . The only topological sort is . The last vertex has in-degree , confirming (D) is false.
    Question 4 · Programming, Data Structures and Algorithms MCQ

    According to the formal definition of topological sort, the first vertex in any valid topological ordering of a directed graph must satisfy a specific condition regarding its in-degree. The maximum possible value of this in-degree is:

    1. A.

      0

    2. B.

      1

    3. C.

    4. D.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a direct-recall question on the boundary condition of topological sorting.

    By definition, for every edge , must appear before . If the first vertex had an in-degree , it would have an incoming edge from some vertex , meaning must appear before it. This contradicts it being the first vertex.

    Step 1: Recall the definition of topological sort.

    Step 2: The first vertex cannot have any predecessors.

    Step 3: Therefore, its in-degree must be exactly 0. The maximum possible value is 0.

    Answer: 0.

    Question 5 · Programming, Data Structures and Algorithms MCQ

    In the DFS-based topological sort algorithm, the timing of when a vertex is pushed onto the stack is crucial for correctness. A vertex is pushed onto the stack:

    1. A.

      When it is first visited during the DFS

    2. B.

      Before any of its neighbors are visited

    3. C.

      After all its reachable descendants have been completely explored

    4. D.

      When its in-degree becomes zero

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a direct-recall question on the DFS-based topological sort procedure.

    The DFS method uses post-order traversal. A vertex is pushed onto the stack only after the recursive DFS has finished exploring all of its descendants. This ensures dependencies are placed below the vertex that depends on them.

    Step 1: Recall the DFS topological sort algorithm.

    Step 2: The algorithm visits neighbors first, then pushes the current vertex.

    Step 3: This is post-order traversal.

    Answer: After all its reachable descendants have been completely explored.

    Question 6 · Programming, Data Structures and Algorithms NAT

    Kahn's algorithm is executed on a directed graph with vertices. The algorithm terminates and the result list contains exactly vertices. The number of vertices that were never enqueued during the execution is ______.

    Correct Answer:

    2.00

    Step-by-Step Solution

    Key idea: this is a cycle-detection-by-count question — vertices not in the result were never enqueued because their in-degree never reached zero.

    Step 1: Kahn's algorithm enqueues a vertex only when its in-degree becomes . Every enqueued vertex is eventually dequeued and added to the result list.

    Step 2: The result list has vertices, meaning exactly vertices were enqueued and processed.

    Step 3: Total vertices . Vertices never enqueued .

    Answer: 2.00

    Common trap: assuming the unprocessed vertices must form a single cycle of length . They could be in separate cycles or one larger cycle involving already-processed vertices' edges. The count is still regardless.

    Verification: processed unprocessed total vertices. Consistent.

    Question 7 · Programming, Data Structures and Algorithms MCQ

    A topological ordering of a directed graph is a linear arrangement of its vertices such that every directed edge points forward. If a graph has 3 vertices and directed edges and , the total number of valid topological orderings is:

    1. A.

      1

    2. B.

      2

    3. C.

      3

    4. D.

      6

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a direct-application question on the definition of topological ordering.

    The edges and enforce the strict order before , and before .

    Step 1: Identify the constraints: must precede , and must precede .

    Step 2: The only linear arrangement satisfying both constraints is .

    Step 3: Count the valid orderings. There is exactly 1.

    Answer: 1.

    Question 8 · Programming, Data Structures and Algorithms MCQ

    Consider the Kahn's algorithm trace for a DAG with vertices A, B, C, D and edges A->B, A->C, B->D, C->D.

    Assertion (A): Vertex D is enqueued into the queue only after both B and C have been processed.

    Reason (R): The initial in-degree of D is 2, and it is reduced by 1 each time a predecessor is processed.

    1. A.

      Both A and R are true, and R correctly explains A

    2. B.

      Both A and R are true, but R does not explain A

    3. C.

      A is true but R is false

    4. D.

      A is false but R is true

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is an assertion-reason question testing the mechanics of Kahn's algorithm.

    Step 1: Evaluate Assertion (A). Vertex D has incoming edges from B and C. In Kahn's algorithm, a vertex is enqueued only when its in-degree reaches 0. Since D has two predecessors, it will only be enqueued after both B and C are processed and their outgoing edges are removed. Assertion is TRUE.

    Step 2: Evaluate Reason (R). The initial in-degree of D is indeed 2 (from B and C). Each time B or C is processed, the in-degree of D decreases by 1. Reason is TRUE.

    Step 3: Check the link. The reason (in-degree reduction) is the exact mechanism that causes the assertion (D is enqueued only after both are processed). R correctly explains A.

    Answer: Both A and R are true, and R correctly explains A.

    Question 9 · Programming, Data Structures and Algorithms MCQ

    A DAG has vertices , , with directed edges and . Match each vertex pair with the nature of its relative order across all valid topological sorts of this DAG.

    1. A.

      P,Q: flexible; P,R: fixed; Q,R: fixed

    2. B.

      P,Q: fixed; P,R: flexible; Q,R: flexible

    3. C.

      P,Q: fixed; P,R: fixed; Q,R: flexible

    4. D.

      P,Q: flexible; P,R: flexible; Q,R: fixed

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: this is a vertex-independence question — two vertices have a flexible relative order if and only if no directed path connects them.

    Step 1: List the directed edges: and .

    Step 2: Check each pair for a directed path between them.

    • and : No edge or path connects to or to . They are independent, so their relative order is flexible.
    • and : Edge exists, so must come before . Fixed.
    • and : Edge exists, so must come before . Fixed.

    Step 3: The matching is P,Q: flexible; P,R: fixed; Q,R: fixed.

    Answer: A

    Common trap: misreading edge direction and concluding instead of , which would incorrectly mark P,R as flexible.

    Verification: The two valid topological sorts are and . In both, precedes and precedes , confirming P,R and Q,R are fixed while P,Q swap.

    Question 10 · Programming, Data Structures and Algorithms MCQ

    Consider a directed acyclic graph with vertices and directed edges , , and .

    Match the vertex pairs in Column I with the existence of a directed path between them (Column II).

    Column I: (i) and , (ii) and , (iii) and

    Column II: (P) A directed path exists between them, (Q) No directed path exists between them

    1. A.

      (i) P, (ii) P, (iii) Q

    2. B.

      (i) P, (ii) Q, (iii) P

    3. C.

      (i) Q, (ii) Q, (iii) P

    4. D.

      (i) Q, (ii) P, (iii) Q

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a path existence question, recognisable because it asks whether a directed path connects specific pairs of vertices in a DAG. The rule of independence for topological sorts relies entirely on this: if no directed path exists, the vertices can swap relative order.

    Exam route: Quickly trace paths from each source vertex. means reaches . and have no connecting edges. is a direct path.

    Learning route:

    Step 1: Understand the definition. A directed path is a sequence of directed edges connecting two vertices. It can be a single edge or a chain of multiple edges.

    Step 2: Check pair (i) and . The edges are and . Following the arrows, forms a directed path. Match: (i) P.

    Step 3: Check pair (ii) and . The edges from go only to . The edges to come only from . There is no sequence of directed edges connecting and in either direction. Match: (ii) Q.

    Step 4: Check pair (iii) and . The edge is a direct path of length 1. Match: (iii) P.

    Combined matching: (i) P, (ii) Q, (iii) P.

    Answer: B

    More short notes in this unit