chapter

    Graph Traversal: Bfs and Dfs Notes for Gate Da

    Graph Traversal: Bfs and Dfs notes for GATE DA: 25 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Graph Traversal Bfs and Dfs notes

    Chapter Roadmap: Graph Traversal

    Chapter Roadmap: Graph Traversal

    1. Reachability in Directed Graphs Forward and reverse reachability, SCCs 2. DFS Discovery and Edge Classification Tree edges, back edges, cross edges 3. Graph Traversal on Given Diagrams Visual tracing and structural properties

    What you will master

    • How directed edges break the symmetry of connectivity.
    • Using reverse graphs to find strongly connected components.
    • Classifying edges discovered during Depth First Search.
    • Accurately tracing traversals on complex visual diagrams.

    Reachability in Directed Graph Traversals

    Reachability in Directed Graph Traversals

    Understanding how one-way edges change the fundamental nature of graph exploration.

    The Core Shift

    In an undirected graph, a traversal from a source vertex naturally discovers its entire connected component. The path can be traversed in both directions.

    In a directed graph, edges are one-way streets. A traversal from vertex only discovers vertices that are reachable from by following the directed edges. It does not guarantee that those discovered vertices can reach back to .

    Directed Reachability vs Undirected Connectivity

    Symmetry vs. Asymmetry

    Undirected Connectivity (Symmetric) If there is a path from to , there is inherently a path from to . The reachable set from is exactly the connected component containing .
    Directed Reachability (Asymmetric) A directed path from to does not imply a path from to .
    • We say is reachable from .
    • The set of all such vertices is the forward reachable set from .
    • This set can be strictly smaller than the weakly connected component containing .

    22 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

    Which of the following statements correctly defines the Strongly Connected Component (SCC) of a vertex in a directed graph?

    Question 2
    Level 1: Warm-up

    Which of the following statements about the edges produced by a Depth First Search on a simple undirected graph is ALWAYS true?

    Question 3
    Level 1: Warm-up

    Which of the following edge types is mathematically impossible to produce during a Depth First Search on any simple undirected graph?

    Question 4
    Level 1: Warm-up

    During a Depth First Search on a directed graph, the presence of which edge type is both necessary and sufficient to conclude that the graph contains at least one directed cycle?

    Question 5
    Level 1: Warm-up

    A student performs a Depth First Search on a simple undirected graph and reports the following edge classification:

    • Tree edges: 3
    • Back edges: 2
    • Forward edges: 1
    • Cross edges: 0

    What is the MINIMUM number of edges that the student MUST have misclassified?

    Question 6
    Level 1: Warm-up

    Which of the following sets of discovery and finish timestamps is mathematically IMPOSSIBLE for a valid Depth First Search on a directed graph?

    Question 7
    Level 1: Warm-up

    Consider an undirected graph with 5 vertices. A Depth First Search is performed. Four students report the following edge classifications. Which of these reports are valid (i.e., satisfy the properties of DFS on undirected graphs)?

    Question 8
    Level 1: Warm-up

    Consider the following scenarios during Depth First Search on a simple undirected graph:

    P. The DFS produces exactly 3 tree edges and 2 back edges.

    Q. The DFS produces exactly 4 tree edges and 1 forward edge.

    R. The DFS produces exactly 2 tree edges and 3 cross edges.

    S. The DFS produces exactly 5 tree edges and 0 back edges.

    How many of these scenarios are IMPOSSIBLE?

    Question 9
    Level 1: Warm-up

    Which of the following statements about the intersection of the forward and backward reachable sets from a vertex in a directed graph is ALWAYS true?

    Question 10
    Level 1: Warm-up

    During a Depth First Search on a simple undirected graph, the edge is explored from and vertex is found to be already visited. Which of the following must be true about ?

    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.

    Graph Traversal: Bfs and Dfs Notes for GATE DA

    Graph Traversal: Bfs and Dfs notes for GATE DA: 25 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Chapter Roadmap: Graph Traversal

    Chapter Roadmap: Graph Traversal

    1. Reachability in Directed Graphs Forward and reverse reachability, SCCs 2. DFS Discovery and Edge Classification Tree edges, back edges, cross edges 3. Graph Traversal on Given Diagrams Visual tracing and structural properties

    What you will master

    • How directed edges break the symmetry of connectivity.
    • Using reverse graphs to find strongly connected components.
    • Classifying edges discovered during Depth First Search.
    • Accurately tracing traversals on complex visual diagrams.

    Reachability in Directed Graph Traversals

    Reachability in Directed Graph Traversals

    Understanding how one-way edges change the fundamental nature of graph exploration.

    The Core Shift

    In an undirected graph, a traversal from a source vertex naturally discovers its entire connected component. The path can be traversed in both directions.

    In a directed graph, edges are one-way streets. A traversal from vertex only discovers vertices that are reachable from by following the directed edges. It does not guarantee that those discovered vertices can reach back to .

    Directed Reachability vs Undirected Connectivity

    Symmetry vs. Asymmetry

    Undirected Connectivity (Symmetric) If there is a path from to , there is inherently a path from to . The reachable set from is exactly the connected component containing .
    Directed Reachability (Asymmetric) A directed path from to does not imply a path from to .
    • We say is reachable from .
    • The set of all such vertices is the forward reachable set from .
    • This set can be strictly smaller than the weakly connected component containing .

    The Reverse Graph

    Analyzing Backward Reachability

    To answer the question "Which vertices can reach ?", we use the reverse graph, denoted as .

    , where

    Simply flip the direction of every edge.

    The Key Property:
    A directed path exists from to in if and only if a directed path exists from to in the original graph .

    Therefore, running a traversal (BFS or DFS) from in yields the exact set of vertices that can reach in .

    Graph Traversal: Bfs and Dfs: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Programming, Data Structures and Algorithms MCQ

    Which of the following statements correctly defines the Strongly Connected Component (SCC) of a vertex in a directed graph?

    1. A.

      The union of forward and backward reachable sets from

    2. B.

      The intersection of forward and backward reachable sets from

    3. C.

      The set of all vertices that can reach

    4. D.

      The set of all vertices reachable from

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a direct definition recall question about SCCs.

    Step 1: Recall the definition of Strongly Connected Component.

    Step 2: An SCC is a maximal set of vertices where every vertex is reachable from every other vertex.

    Step 3: For vertex , this means:

    • can reach them (forward reachable)
    • They can reach (backward reachable)

    Step 4: Therefore, SCC() = forward reachable set backward reachable set.

    Answer: The intersection of forward and backward reachable sets from

    Question 2 · Programming, Data Structures and Algorithms MCQ

    Which of the following statements about the edges produced by a Depth First Search on a simple undirected graph is ALWAYS true?

    1. A.

      It can produce Cross edges if the graph is disconnected.

    2. B.

      It can produce Forward edges if the graph contains cycles.

    3. C.

      It will only produce Tree and Back edges.

    4. D.

      It will only produce Tree and Cross edges.

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a bounding question testing the fundamental rule of DFS on undirected graphs.

    Step 1: Recall the theorem for undirected graphs. When DFS is performed on an undirected graph, every edge is explored in both directions.

    Step 2: If an edge is explored and is already visited, must be an ancestor of . If were in a different branch, the edge would have been traversed from to earlier, making a descendant of .

    Step 3: Therefore, any non-tree edge in an undirected graph must be a Back edge. Forward and Cross edges are mathematically impossible.

    Step 4: Evaluate the options. Option C correctly states that only Tree and Back edges are produced, regardless of cycles or connectivity.

    Answer: It will only produce Tree and Back edges.

    Question 3 · Programming, Data Structures and Algorithms MCQ

    Which of the following edge types is mathematically impossible to produce during a Depth First Search on any simple undirected graph?

    1. A.

      Tree edge

    2. B.

      Back edge

    3. C.

      Forward edge

    4. D.

      Both A and B

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a bounding question testing the fundamental rule of DFS on undirected graphs.

    Step 1: Recall the theorem for undirected graphs. When DFS explores an edge in an undirected graph, the reverse edge also exists.

    Step 2: If is already visited when is explored, must be an ancestor of . If were in a different branch, the edge would have been traversed from to earlier.

    Step 3: Therefore, any non-tree edge in an undirected graph must be a Back edge. Forward and Cross edges are mathematically impossible.

    Step 4: Evaluate the options. Forward edge is the only correct choice among the single types.

    Answer: Forward edge

    Question 4 · Programming, Data Structures and Algorithms MCQ

    During a Depth First Search on a directed graph, the presence of which edge type is both necessary and sufficient to conclude that the graph contains at least one directed cycle?

    1. A.

      Back edge

    2. B.

      Tree edge

    3. C.

      Forward edge

    4. D.

      Cross edge

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is an observation question testing the fundamental relationship between Back edges and cycles.

    Step 1: Recall the definition of a Back edge. It is an edge where is an ancestor of in the DFS tree.

    Step 2: If is an ancestor of , there is a tree path from down to . The Back edge completes this into a cycle: .

    Step 3: Conversely, if the graph has a cycle, DFS must produce at least one Back edge. No other edge type guarantees a cycle.

    Step 4: Therefore, Back edges are both necessary and sufficient for cycles.

    Answer: Back edge

    Question 5 · Programming, Data Structures and Algorithms NAT

    A student performs a Depth First Search on a simple undirected graph and reports the following edge classification:

    • Tree edges: 3
    • Back edges: 2
    • Forward edges: 1
    • Cross edges: 0

    What is the MINIMUM number of edges that the student MUST have misclassified?

    Correct Answer:

    1.00

    Step-by-Step Solution

    Key idea: In an undirected graph, DFS produces ONLY Tree edges and Back edges. Forward and Cross edges are mathematically impossible.

    Step 1: Recall the rule for undirected graphs: DFS yields only Tree and Back edges. No Forward or Cross edges can exist.

    Step 2: Check the student's report:

    • Tree edges: 3 (possible ✓)
    • Back edges: 2 (possible ✓)
    • Forward edges: 1 (IMPOSSIBLE ✗)
    • Cross edges: 0 (possible ✓)

    Step 3: The Forward edge is impossible in an undirected graph. It must be misclassified (likely as a Tree or Back edge).

    Step 4: Count misclassified edges: At least 1 edge (the Forward edge) must be wrong.

    Answer: 1

    Question 6 · Programming, Data Structures and Algorithms MCQ

    Which of the following sets of discovery and finish timestamps is mathematically IMPOSSIBLE for a valid Depth First Search on a directed graph?

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: The Parenthesis Theorem states that DFS time intervals must be either completely disjoint or strictly nested. Partial overlap is impossible.

    Step 1: Check Option A: , , . and are nested inside , and disjoint from each other. Valid.

    Step 2: Check Option B: , , . All are strictly nested. Valid.

    Step 3: Check Option C: , . starts before , but finishes before finishes (). This is a partial overlap. Invalid.

    Step 4: Check Option D: , , . All are completely disjoint. Valid.

    Answer: Option C

    Question 7 · Programming, Data Structures and Algorithms MSQ

    Consider an undirected graph with 5 vertices. A Depth First Search is performed. Four students report the following edge classifications. Which of these reports are valid (i.e., satisfy the properties of DFS on undirected graphs)?

    1. A.

      Tree: 4, Back: 2, Forward: 0, Cross: 0

    2. B.

      Tree: 4, Back: 3, Forward: 0, Cross: 0

    3. C.

      Tree: 4, Back: 1, Forward: 1, Cross: 0

    4. D.

      Tree: 3, Back: 3, Forward: 0, Cross: 0

    Correct Answer:

    ["A","B"]

    Step-by-Step Solution

    Key idea: In an undirected graph, DFS produces only tree edges and back edges. Forward and cross edges are impossible.

    Step 1: Recall the undirected graph rule: DFS on an undirected graph yields only tree edges and back edges. Therefore, forward edges = 0 and cross edges = 0.

    Step 2: For a connected graph with vertices, the number of tree edges is always . Here , so tree edges = 4.

    Step 3: Check each report:

    • Option A: Tree = 4, Back = 2, Forward = 0, Cross = 0. Valid (tree = 4, no forward/cross).
    • Option B: Tree = 4, Back = 3, Forward = 0, Cross = 0. Valid (tree = 4, no forward/cross).
    • Option C: Tree = 4, Back = 1, Forward = 1, Cross = 0. Invalid (forward edges must be 0).
    • Option D: Tree = 3, Back = 3, Forward = 0, Cross = 0. Invalid (tree edges must be 4, not 3).

    Answer: Options A and B are valid.

    Question 8 · Programming, Data Structures and Algorithms NAT

    Consider the following scenarios during Depth First Search on a simple undirected graph:

    P. The DFS produces exactly 3 tree edges and 2 back edges.

    Q. The DFS produces exactly 4 tree edges and 1 forward edge.

    R. The DFS produces exactly 2 tree edges and 3 cross edges.

    S. The DFS produces exactly 5 tree edges and 0 back edges.

    How many of these scenarios are IMPOSSIBLE?

    Correct Answer:

    2.00

    Step-by-Step Solution

    Key idea: This is a boundary-case question testing the undirected graph rule for DFS edge classification.

    Step 1: Recall the undirected graph theorem:

    • In an undirected graph, DFS produces only tree edges and back edges.
    • Forward edges and cross edges are mathematically impossible.

    Step 2: Evaluate each scenario:

    • Scenario P: "3 tree edges and 2 back edges."
    • Only tree and back edges are present.
    • This is POSSIBLE (e.g., a graph with 5 vertices and 5 edges forming a tree plus 2 extra edges creating cycles).
    • Scenario Q: "4 tree edges and 1 forward edge."
    • Forward edges are impossible in undirected graphs.
    • This is IMPOSSIBLE.
    • Scenario R: "2 tree edges and 3 cross edges."
    • Cross edges are impossible in undirected graphs.
    • This is IMPOSSIBLE.
    • Scenario S: "5 tree edges and 0 back edges."
    • Only tree edges are present (no back edges).
    • This is POSSIBLE (e.g., a tree graph with 6 vertices has exactly 5 tree edges and 0 back edges).

    Step 3: Count impossible scenarios:

    • Q and R are impossible.
    • Count = 2.

    Answer: 2.00

    Common trap: Thinking forward or cross edges are possible in undirected graphs.

    Question 9 · Programming, Data Structures and Algorithms MCQ

    Which of the following statements about the intersection of the forward and backward reachable sets from a vertex in a directed graph is ALWAYS true?

    1. A.

      The intersection is empty if has no incoming edges.

    2. B.

      The intersection contains at least the vertex itself.

    3. C.

      The intersection size is equal to the number of tree edges in the DFS.

    4. D.

      The intersection includes all vertices in the weakly connected component of .

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a statement truth question about SCC intersection properties.

    Step 1: The SCC of is defined as .

    Step 2: By definition, can reach itself (path of length 0), so .

    Step 3: Similarly, can reach itself, so .

    Step 4: Therefore, is always in the intersection, meaning the intersection is never empty and contains at least .

    Step 5: Evaluate options: A is false (intersection always has ). B is true. C is false (no relation to tree edges). D is false (weak component can be larger).

    Answer: The intersection contains at least the vertex itself.

    Question 10 · Programming, Data Structures and Algorithms MCQ

    During a Depth First Search on a simple undirected graph, the edge is explored from and vertex is found to be already visited. Which of the following must be true about ?

    1. A.

      is a descendant of in the DFS tree

    2. B.

      is an ancestor of in the DFS tree

    3. C.

      belongs to a different DFS tree from

    4. D.

      has a larger discovery time than

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a bounding question testing the structural reason why undirected DFS only produces Tree and Back edges.

    Step 1: The graph is undirected, so edge implies edge also exists.

    Step 2: Since is already visited when is explored from , DFS must have reached earlier.

    Step 3: Because the graph is undirected, when DFS first visited , it would have explored the edge . If was unvisited at that time, DFS would have discovered as a child of , making a descendant of .

    Step 4: This means is an ancestor of in the DFS tree. The edge is therefore a Back edge.

    Step 5: The other options are impossible: cannot be a descendant (it was visited before explored this edge), cannot be in a different tree (the undirected edge connects them), and must have a smaller discovery time (visited earlier).

    Answer: is an ancestor of in the DFS tree

    More notes in this unit