chapter

    Graph Traversal: Bfs and Dfs Short Notes for Gate Da

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

    Graph Traversal Bfs and Dfs short notes

    Quick Recap: Reachability

    Core Principles

    Asymmetry: In directed graphs, does not imply .

    Reverse Graph (): Flipping edges turns "reachable from" into "can reach".

    SCC Intersection: .

    Algorithm Equivalence: BFS and DFS discover the exact same reachable set from a source.

    Path vs Existence: Reachability only guarantees a path exists. Only BFS guarantees the shortest path in unweighted graphs.

    Scope: One traversal = single-source reachability. traversals = transitive closure.

    Quick Recap: DFS Edge Classification

    Core Principles

    Parenthesis Theorem: Time intervals are either strictly nested or completely disjoint.

    Tree Edge: (and discovered via ).

    Back Edge: (indicates a cycle).

    Forward Edge: (but not discovered via ).

    Cross Edge: (disjoint intervals).

    Undirected Rule: DFS on undirected graphs yields only Tree and Back edges.

    Quick Recap: Visual Tracing Checklist

    Visual Tracing Checklist

    Workspace: Always use a trace table and explicitly draw the Queue (BFS) or Stack (DFS).
    Degree Check: Count the degrees of all vertices before starting to ensure no edges are missed.
    Ordering: Pre-sort adjacency lists if the question specifies an ordering constraint.
    BFS Signature: Expands level-by-level. All distance vertices are visited before distance .
    DFS Signature: Dives deep. Explores a single path as far as possible before backtracking.
    Strictness: Follow the exact neighbor order specified; any deviation ruins the trace.

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

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

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

    Quick Recap: Reachability

    Core Principles

    Asymmetry: In directed graphs, does not imply .

    Reverse Graph (): Flipping edges turns "reachable from" into "can reach".

    SCC Intersection: .

    Algorithm Equivalence: BFS and DFS discover the exact same reachable set from a source.

    Path vs Existence: Reachability only guarantees a path exists. Only BFS guarantees the shortest path in unweighted graphs.

    Scope: One traversal = single-source reachability. traversals = transitive closure.

    Quick Recap: DFS Edge Classification

    Core Principles

    Parenthesis Theorem: Time intervals are either strictly nested or completely disjoint.

    Tree Edge: (and discovered via ).

    Back Edge: (indicates a cycle).

    Forward Edge: (but not discovered via ).

    Cross Edge: (disjoint intervals).

    Undirected Rule: DFS on undirected graphs yields only Tree and Back edges.

    Quick Recap: Visual Tracing Checklist

    Visual Tracing Checklist

    Workspace: Always use a trace table and explicitly draw the Queue (BFS) or Stack (DFS).
    Degree Check: Count the degrees of all vertices before starting to ensure no edges are missed.
    Ordering: Pre-sort adjacency lists if the question specifies an ordering constraint.
    BFS Signature: Expands level-by-level. All distance vertices are visited before distance .
    DFS Signature: Dives deep. Explores a single path as far as possible before backtracking.
    Strictness: Follow the exact neighbor order specified; any deviation ruins the trace.

    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 short notes in this unit