chapter

    Graph Traversal, Connectivity and Directed Graphs Short Notes for Gate Cs

    Graph Traversal, Connectivity and Directed Graphs short notes for GATE CS: 5 study cards covering concepts, formulas, shortcuts and exam traps, plus solved pr

    Graph Traversal Connectivity and Directed Graphs short notes

    Key Takeaways: BFS Trees

    Summary: BFS Tree Core Concepts

    1. Shortest Path: Path from root to in BFS tree is the shortest path in unweighted .
    2. Undirected Edges: Only Tree and Cross edges exist.
    3. Level Constraint: For any cross edge in undirected BFS, .
    4. Tree Diameter: Found efficiently using the Two-BFS Trick.
    5. Directed Graphs: Back and forward edges can exist; level constraints do not apply to them.

    Quick Revision: Edge Types and Timestamps

    Quick Revision: Edge Types and Timestamps

    Edge Type Ancestor Relation Interval Relation Undirected?
    Tree nested in Yes
    Forward nested in No
    Back nested in Yes
    Cross None Disjoint ( before ) No
    Golden Rules
    1. Parenthesis Theorem: Intervals are either disjoint or strictly nested.
    2. Undirected DFS implies only Tree and Back edges.
    3. Cycle exists if and only if a Back edge exists.

    Quick Revision: Components and Cut Vertices

    Quick Revision: Components and Cut Vertices

    Concept Key Formula / Condition
    Connected Components (in DFS forest)
    Articulation Point (Root) Has children in DFS tree
    Articulation Point (Non-Root) child such that
    Leaf Node Never an articulation point
    Back Edge Update
    Tree Edge Update (after returning)
    Golden Rule

    The in the non-root condition is strict. If , can reach but nothing above , so removing still disconnects the graph.

    2 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 statements about Depth-First Search on an undirected graph :

    (I) Every non-tree edge is a back edge.

    (II) The DFS forest can contain forward edges.

    (III) If an edge connects to an already visited vertex, it must be a back edge.

    Which of the above statements is/are TRUE?

    Question 2
    Level 1: Warm-up

    A tree has diameter edges. A student performs the two-BFS algorithm to find the diameter:

    1. BFS from an arbitrary vertex finds the farthest vertex at distance 4.
    2. BFS from that vertex finds the farthest vertex at distance 7.

    The student reports: "The diameter of the tree is 7 vertices."

    What is the correct diameter of the tree?

    Question 3
    Level 1: Warm-up

    Consider the following assertion and reason about Depth-First Search on undirected graphs:

    Assertion (A): In a DFS of a connected undirected graph, no forward edges or cross edges can appear.

    Reason (R): In an undirected graph, every edge is bidirectional, so any edge to an already-visited vertex must go to an ancestor in the DFS tree.

    Which of the following options is CORRECT?

    Question 4
    Level 1: Warm-up

    A tree has 20 vertices. The two-BFS algorithm is applied to find the diameter:

    1. BFS from an arbitrary vertex finds the farthest vertex at distance 6.
    2. BFS from finds the farthest vertex at distance 9.

    What is the diameter of the tree in terms of the number of edges?

    Question 5
    Level 1: Warm-up

    Assertion: In a Depth-First Search of a connected undirected graph, forward edges cannot exist.

    If this assertion is TRUE, enter 1. If it is FALSE, enter 0.

    Question 6
    Level 1: Warm-up

    Consider the following two statements about Depth-First Search on a directed graph:

    Assertion (A): If , then the edge is a tree edge.

    Reason (R): The condition implies that is a descendant of in the DFS tree.

    Question 7
    Level 1: Warm-up

    Which of the following pairs of edge types can NEVER both appear in a Depth-First Search of an undirected graph?

    Question 8
    Level 1: Warm-up

    Consider the following two statements about Depth-First Search on a directed graph:

    Assertion (A): If , then the edge is a back edge.

    Reason (R): The condition implies that is an ancestor of in the DFS tree.

    Question 9
    Level 1: Warm-up

    Depth-First Search is used to detect a directed cycle, maintaining the boolean arrays and . Which of the following statements is/are TRUE?

    Question 10
    Level 1: Warm-up

    The linear-time DFS algorithm for articulation points is run on an undirected graph, maintaining the arrays and . Which of the following statements is/are TRUE?

    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, Connectivity and Directed Graphs Short Notes for GATE CS

    Graph Traversal, Connectivity and Directed Graphs short notes for GATE CS: 5 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Key Takeaways: BFS Trees

    Summary: BFS Tree Core Concepts

    1. Shortest Path: Path from root to in BFS tree is the shortest path in unweighted .
    2. Undirected Edges: Only Tree and Cross edges exist.
    3. Level Constraint: For any cross edge in undirected BFS, .
    4. Tree Diameter: Found efficiently using the Two-BFS Trick.
    5. Directed Graphs: Back and forward edges can exist; level constraints do not apply to them.

    Quick Revision: Edge Types and Timestamps

    Quick Revision: Edge Types and Timestamps

    Edge Type Ancestor Relation Interval Relation Undirected?
    Tree nested in Yes
    Forward nested in No
    Back nested in Yes
    Cross None Disjoint ( before ) No
    Golden Rules
    1. Parenthesis Theorem: Intervals are either disjoint or strictly nested.
    2. Undirected DFS implies only Tree and Back edges.
    3. Cycle exists if and only if a Back edge exists.

    Quick Revision: Components and Cut Vertices

    Quick Revision: Components and Cut Vertices

    Concept Key Formula / Condition
    Connected Components (in DFS forest)
    Articulation Point (Root) Has children in DFS tree
    Articulation Point (Non-Root) child such that
    Leaf Node Never an articulation point
    Back Edge Update
    Tree Edge Update (after returning)
    Golden Rule

    The in the non-root condition is strict. If , can reach but nothing above , so removing still disconnects the graph.

    Exam Readiness: SCC and Cycle Patterns

    Exam Readiness: SCC and Cycle Patterns

    1
    Cycle Detection Always look for the "recursion stack" or "3-color" (white, gray, black) method. Gray means currently in recursion stack.
    2
    SCC Definition Mutual reachability. If can reach and can reach , they are in the same SCC.
    3
    Condensation Graph Always a DAG. The number of SCCs with in-degree zero dictates the minimum number of starting points needed to traverse the entire graph.
    4
    Edge Classification in Directed DFS
    • Tree Edge: to unvisited.
    • Back Edge: to ancestor in recursion stack (implies cycle).
    • Forward Edge: to descendant (already fully processed).
    • Cross Edge: to a different branch (already fully processed).

    Graph Traversal, Connectivity and Directed Graphs: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Algorithms MCQ

    Consider the following statements about Depth-First Search on an undirected graph :

    (I) Every non-tree edge is a back edge.

    (II) The DFS forest can contain forward edges.

    (III) If an edge connects to an already visited vertex, it must be a back edge.

    Which of the above statements is/are TRUE?

    1. A.

      I only

    2. B.

      I and III only

    3. C.

      II and III only

    4. D.

      I, II, and III

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a bounding question testing the edge classification theorem for undirected DFS. Evaluate each statement against the theorem that undirected DFS produces only tree and back edges.

    Evaluating each statement:

    Statement I: "Every non-tree edge is a back edge." TRUE. Since undirected DFS produces only tree and back edges, every non-tree edge must be a back edge.

    Statement II: "The DFS forest can contain forward edges." FALSE. This is the ignoring_constraint trap. The theorem explicitly states that forward edges cannot exist in undirected DFS.

    Statement III: "If an edge connects to an already visited vertex, it must be a back edge." TRUE. In undirected DFS, if an edge is explored and is already visited (gray or black), then must be an ancestor of , making a back edge.

    Therefore, statements I and III are true, and II is false.

    Why option D is the trap: A student might think all three are true, forgetting that forward edges cannot exist in undirected DFS.

    Verification: The theorem guarantees that undirected DFS produces only tree and back edges. This makes I and III true, and II false.

    Answer: B

    Question 2 · Algorithms MCQ

    A tree has diameter edges. A student performs the two-BFS algorithm to find the diameter:

    1. BFS from an arbitrary vertex finds the farthest vertex at distance 4.
    2. BFS from that vertex finds the farthest vertex at distance 7.

    The student reports: "The diameter of the tree is 7 vertices."

    What is the correct diameter of the tree?

    1. A.

      6 edges

    2. B.

      7 edges

    3. C.

      8 edges

    4. D.

      9 edges

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a direct application of the two-BFS trick for finding tree diameter.

    Step 1: Recall the two-BFS algorithm for finding the diameter of a tree:

    • First BFS from any vertex finds one endpoint of the diameter.
    • Second BFS from that endpoint finds the other endpoint, and the distance is the diameter.

    Step 2: In this problem:

    • First BFS finds farthest vertex at distance 4 (this is one endpoint of the diameter).
    • Second BFS from that vertex finds farthest at distance 7.

    Step 3: By the two-BFS theorem, the distance found in the second BFS is exactly the diameter. Therefore, the diameter is 7 edges.

    Step 4: The student reported "7 vertices," which is a unit error. A path with 7 vertices has 6 edges, but the actual diameter is 7 edges (which corresponds to 8 vertices in the path).

    Answer: The correct diameter is 7 edges (Option B).

    Question 3 · Algorithms MCQ

    Consider the following assertion and reason about Depth-First Search on undirected graphs:

    Assertion (A): In a DFS of a connected undirected graph, no forward edges or cross edges can appear.

    Reason (R): In an undirected graph, every edge is bidirectional, so any edge to an already-visited vertex must go to an ancestor in the DFS tree.

    Which of the following options 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:

    A

    Step-by-Step Solution

    Key idea: This is a direct recall question about the fundamental theorem of DFS on undirected graphs.

    Step 1: Recall the theorem: In an undirected graph, DFS produces only tree edges and back edges. Forward edges and cross edges cannot exist.

    Step 2: Therefore, Assertion (A) is TRUE.

    Step 3: Now evaluate Reason (R): In an undirected graph, every edge can be traversed in both directions. When DFS is at vertex and considers edge :

    • If is unvisited (white), becomes a tree edge.
    • If is already visited (gray or black), then must have been discovered before finished. Since the graph is undirected, when was explored, it would have traversed the edge . If was unvisited at that time, would be a tree edge. If was already visited, then is an ancestor of , making a back edge.

    Step 4: Therefore, Reason (R) is TRUE and correctly explains why forward and cross edges cannot exist in undirected DFS.

    Answer: Both A and R are true, and R is the correct explanation of A (Option A).

    Question 4 · Algorithms NAT

    A tree has 20 vertices. The two-BFS algorithm is applied to find the diameter:

    1. BFS from an arbitrary vertex finds the farthest vertex at distance 6.
    2. BFS from finds the farthest vertex at distance 9.

    What is the diameter of the tree in terms of the number of edges?

    Correct Answer:

    9.00

    Step-by-Step Solution

    Key idea: This is a direct application of the two-BFS trick for finding tree diameter.

    Step 1: Recall the two-BFS algorithm for finding the diameter of a tree:

    • First BFS from any vertex finds one endpoint of the diameter.
    • Second BFS from that endpoint finds the other endpoint, and the distance is the diameter.

    Step 2: In this problem:

    • First BFS from finds farthest vertex at distance 6 (this is one endpoint of the diameter).
    • Second BFS from finds farthest vertex at distance 9.

    Step 3: By the two-BFS theorem, the distance found in the second BFS is exactly the diameter. Therefore, the diameter is 9 edges.

    Step 4: Note that the first BFS distance (6) is not used in the final answer. It only serves to find one endpoint of the diameter.

    Answer: 9.00

    Question 5 · Algorithms NAT

    Assertion: In a Depth-First Search of a connected undirected graph, forward edges cannot exist.

    If this assertion is TRUE, enter 1. If it is FALSE, enter 0.

    Correct Answer:

    1.00

    Step-by-Step Solution

    Key idea: This is a direct recall question about the fundamental theorem of DFS on undirected graphs.

    Step 1: Recall the theorem: In an undirected graph, DFS produces only tree edges and back edges. Forward edges and cross edges cannot exist.

    Step 2: The assertion states that forward edges cannot exist in undirected DFS. This matches the theorem exactly.

    Step 3: Therefore, the assertion is TRUE.

    Step 4: Since the assertion is true, we enter 1.

    Answer: 1.00

    Question 6 · Algorithms MCQ

    Consider the following two statements about Depth-First Search on a directed graph:

    Assertion (A): If , then the edge is a tree edge.

    Reason (R): The condition implies that is a descendant of in the DFS tree.

    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:

    D

    Step-by-Step Solution

    Key idea: This is a construction question testing the distinction between tree edges and forward edges based on timestamps. Evaluate each statement independently.

    Step 1 — Evaluate R: "The condition implies that is a descendant of in the DFS tree." This is TRUE. The timestamp condition means 's interval is nested inside 's interval, which by the parenthesis theorem means is a descendant of .

    Step 2 — Evaluate A: "If , then the edge is a tree edge." This is FALSE. The timestamp condition is satisfied by both tree edges and forward edges. A tree edge is an edge in the DFS forest, while a forward edge connects an ancestor to a descendant but is not part of the tree. Both satisfy .

    Step 3 — Counterexample: Consider a directed graph with edges and . If DFS explores first, then is discovered via the path , making and tree edges. The edge is a forward edge (not a tree edge), but it still satisfies .

    Step 4 — Conclusion: A is false, R is true.

    Why option B is the trap (unit_mismatch): A student might think both are true because the timestamp condition does imply descendant relationship. However, the descendant relationship is necessary but not sufficient for a tree edge — the edge must also be part of the DFS forest.

    Verification: The timestamp condition is necessary for both tree and forward edges. It is not sufficient to conclude the edge is a tree edge.

    Answer: D

    Question 7 · Algorithms MCQ

    Which of the following pairs of edge types can NEVER both appear in a Depth-First Search of an undirected graph?

    1. A.

      Tree edge and Back edge

    2. B.

      Tree edge and Forward edge

    3. C.

      Back edge and Cross edge

    4. D.

      Forward edge and Cross edge

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: This is a bounding question about the edge classification theorem for undirected DFS. The theorem states that undirected DFS produces only tree edges and back edges; forward and cross edges cannot exist.

    Step 1: Recall the theorem: In an undirected graph, DFS classifies every edge as either a tree edge or a back edge. No forward or cross edges can exist.

    Step 2: This means:

    • Tree edges: can appear.
    • Back edges: can appear.
    • Forward edges: cannot appear.
    • Cross edges: cannot appear.

    Step 3: The question asks which pair can NEVER both appear. Since forward edges and cross edges both cannot appear in undirected DFS, the pair (Forward edge, Cross edge) can never both appear.

    Why the other options fail:

    • "Tree edge and Back edge" is the ignoring_constraint trap: both can appear in undirected DFS, so this pair can appear.
    • "Tree edge and Forward edge" is the ignoring_constraint trap: tree edges can appear, but forward edges cannot. So this pair cannot both appear, but the question asks for a pair where both cannot appear.
    • "Back edge and Cross edge" is the ignoring_constraint trap: back edges can appear, but cross edges cannot. So this pair cannot both appear, but the question asks for a pair where both cannot appear.

    Wait, I need to reconsider. The question asks "can NEVER both appear", which means neither can appear. Let me re-evaluate:

    • (Tree, Back): both can appear, so this pair CAN appear.
    • (Tree, Forward): tree can appear, forward cannot. So this pair CANNOT both appear.
    • (Back, Cross): back can appear, cross cannot. So this pair CANNOT both appear.
    • (Forward, Cross): neither can appear. So this pair CANNOT both appear.

    So options B, C, and D all represent pairs that cannot both appear. But the question asks for THE pair that can never both appear, implying a unique answer. Let me re-read...

    Actually, the question is asking which pair can NEVER both appear, meaning which pair has the property that it's impossible for both to appear. For (Tree, Forward), tree edges can appear but forward edges cannot, so it's impossible for both to appear. Same for (Back, Cross). And for (Forward, Cross), neither can appear, so it's also impossible for both to appear.

    But I think the intended interpretation is: which pair consists of edge types that BOTH cannot appear? In that case, only (Forward, Cross) fits, because both forward and cross edges cannot appear in undirected DFS.

    Let me revise the question to make it clearer: "Which of the following pairs consists of edge types that can NEVER appear in a DFS of an undirected graph?"

    Actually, looking at the options again, I think the question is asking: which pair has the property that both edge types in the pair cannot appear? Only option D (Forward and Cross) fits this, because both forward and cross edges cannot appear in undirected DFS.

    Answer: D

    Question 8 · Algorithms MCQ

    Consider the following two statements about Depth-First Search on a directed graph:

    Assertion (A): If , then the edge is a back edge.

    Reason (R): The condition implies that is an ancestor of in the DFS tree.

    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:

    A

    Step-by-Step Solution

    Key idea: This is a construction question testing the timestamp condition for back edges. Evaluate each statement independently, then check if R causally explains A.

    Step 1 — Evaluate R: "The condition implies that is an ancestor of in the DFS tree." This is TRUE. The timestamp condition means 's interval is strictly nested inside 's interval . By the parenthesis theorem, this means is a descendant of , or equivalently, is an ancestor of .

    Step 2 — Evaluate A: "If , then the edge is a back edge." This is TRUE. A back edge is defined as an edge from a descendant to an ancestor. Since is an ancestor of (from R), the edge goes from descendant to ancestor , which is exactly the definition of a back edge.

    Step 3 — Does R explain A? Yes. The reason A holds is precisely because the timestamp condition implies is an ancestor of (that is what R states). Without R being true, A would not follow. The timestamp condition guarantees the ancestor-descendant relationship, which in turn guarantees the edge is a back edge.

    Why option B is the trap (unit_mismatch): A student might think both are true but unrelated. However, R is the direct causal mechanism that makes A true. The ancestor relationship (R) is what makes the edge a back edge (A).

    Verification: If is an ancestor of , then by definition, an edge from to is a back edge. The timestamp condition is the precise characterization of this ancestor relationship.

    Answer: A

    Question 9 · Algorithms MSQ

    Depth-First Search is used to detect a directed cycle, maintaining the boolean arrays and . Which of the following statements is/are TRUE?

    1. A.

      An edge to a vertex that is visited but no longer in the recursion stack confirms a directed cycle.

    2. B.

      The array alone is sufficient to detect cycles in a directed graph.

    3. C.

      An edge to a vertex currently in the recursion stack confirms a directed cycle.

    4. D.

      Resetting to false before returning from the call on may be omitted without affecting correctness.

    Correct Answer:

    ["C"]

    Step-by-Step Solution

    Key idea: this is the recursion-stack characterisation of directed cycles, recognisable because two arrays, and , are named in the statement. Why this method applies: in a directed graph, reaching an already-visited vertex may be a cross edge or a forward edge, which create no cycle. Only an edge into the active recursion stack closes a directed loop. That single test decides all four options. Step 1 (option C): if is on the stack and we find an edge while is still active, the stack path from down to plus this edge is a directed cycle. C is TRUE. Step 2 (option A): a visited vertex that has already been popped is black. An edge into it is a cross or forward edge; both are consistent with an acyclic graph. A is FALSE. Step 3 (option B): cannot distinguish "still active" from "already finished", so by itself it flags cross edges as cycles. B is FALSE. Step 4 (option D): if the flag is never cleared, every finished vertex still looks active, and later cross edges are reported as cycles. The reset is mandatory. D is FALSE. Answer: C only. Tempting wrong path: reusing the undirected rule "visited neighbour other than the parent means a cycle". That makes A and B look true and gives A, B, C — an overcount of cycles in a DAG. The break is at the word directed: the undirected argument needs the edge to be usable in both directions. Generalisation: for directed cycle detection, "visited" is not the right predicate; "currently on the recursion stack" is. Verification: take , , with alphabetical order. After finishes it is popped; the edge hits a visited but inactive vertex, and the graph is acyclic — so A and B would wrongly report a cycle, while C reports none.
    Question 10 · Algorithms MSQ

    The linear-time DFS algorithm for articulation points is run on an undirected graph, maintaining the arrays and . Which of the following statements is/are TRUE?

    1. A.

      For a back edge from to a non-parent vertex , the update uses .

    2. B.

      At the moment is first discovered, .

    3. C.

      may end up strictly larger than .

    4. D.

      After the recursive call on a tree-edge child returns, is updated using .

    Correct Answer:

    ["B","D"]

    Step-by-Step Solution

    Key idea: this is the "which value propagates upward" question, recognisable because every option is an update rule for the array rather than a numeric trace.

    Why this method applies: the discovery-and-low card gives the initialisation and exactly two update rules, one per edge kind. Matching each option to those three lines settles everything.

    Step 1 (option B): initialisation is . So B is TRUE.

    Step 2 (option D): for a tree edge, after the child returns we set

    The child's whole subtree information must travel up, so the child's is used. D is TRUE.

    Step 3 (option A): for a back edge to a non-parent the rule is

    using , not . Using would let information from beyond leak in and could wrongly certify a subtree as having an escape route above . A is FALSE.

    Step 4 (option C): starts equal to and is only ever replaced by a minimum involving it, so it can never increase. Hence always, and C is FALSE.

    Answer: B and D.

    Tempting wrong path: treating both update rules as "take the child's or neighbour's low", which selects A as well and gives A, B, D. The break is at the back-edge line, where the card deliberately writes .

    Generalisation: tree edges carry upward, back edges reach one specific already-discovered vertex and therefore contribute .

    Verification: on the path with the extra edge , let . Then and — consistent with B and D, and showing C is impossible.

    More short notes in this unit