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

    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 8
    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 ?

    Question 9
    Level 1: Warm-up

    During a Depth First Search on a directed graph with 6 vertices and 10 edges, the edge classification yields:

    • Tree edges: 4
    • Back edges: 3
    • Forward edges: 1
    • Cross edges: ?

    How many Cross edges are there?

    Question 10
    Level 1: Warm-up

    During a Depth First Search on a simple undirected graph with 8 vertices, the algorithm discovers exactly 7 Tree edges. What is the maximum possible number of Back edges that can be generated in this graph?

    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.

    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 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 8 · 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

    Question 9 · Programming, Data Structures and Algorithms NAT

    During a Depth First Search on a directed graph with 6 vertices and 10 edges, the edge classification yields:

    • Tree edges: 4
    • Back edges: 3
    • Forward edges: 1
    • Cross edges: ?

    How many Cross edges are there?

    Correct Answer:

    2.00

    Step-by-Step Solution

    Key idea: The total number of edges equals the sum of all edge types.

    Step 1: Recall that every edge in a directed graph DFS is classified as exactly one type: Tree, Back, Forward, or Cross.

    Step 2: Set up the equation: Total edges = Tree + Back + Forward + Cross

    Step 3: Substitute known values:

    Step 4: Solve for Cross:

    Answer: 2

    Question 10 · Programming, Data Structures and Algorithms MCQ

    During a Depth First Search on a simple undirected graph with 8 vertices, the algorithm discovers exactly 7 Tree edges. What is the maximum possible number of Back edges that can be generated in this graph?

    1. A.

      20

    2. B.

      21

    3. C.

      27

    4. D.

      28

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: In an undirected graph, every edge is either a Tree edge or a Back edge. The maximum number of edges in a simple graph limits the Back edges.

    Step 1: Recall that for a simple undirected graph with vertices, the maximum number of edges is .

    Step 2: Calculate max edges for : .

    Step 3: Recall that every edge is classified as either Tree or Back. So, .

    Step 4: We are given . To maximize Back edges, we must maximize Total edges.

    Step 5: Max Back edges = Max Total edges - Tree edges = .

    Answer: Option B

    More notes in this unit