chapter
    Graph Traversal, Connectivity and Directed Graphs PYQs for GATE CS

    Solve 10+ Graph Traversal, Connectivity and Directed Graphs previous year questions for GATE CS with answers and detailed solutions. Free sample questions bel

    Try a question

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

    Question 1
    2026 Slot Set1 PYQ
    Level 3: Exam Standard
    Consider the following pseudocode for depth-first search (DFS) algorithm which takes a directed graph as input, where and are the discovery time and finishing time, respectively, of the vertex .


      unmark all
      
      for each
        if is unmarked
          
        end if
      end for

      mark
      
      
      for each
        if is unmarked
          
        end if
      end for
      
      
      return


    Suppose that the input directed graph is a directed acyclic graph (DAG).

    For an edge , which of the following options will NEVER be correct?
    Question 2
    2025 Slot Set2 PYQ
    Level 3: Exam Standard

    Which of the following statements regarding Breadth First Search (BFS) and Depth First Search (DFS) on an undirected simple graph is/are TRUE?

    Question 3
    2025 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following algorithm someAlgo that takes an undirected graph as input.

    someAlgo(G)
    
        1. Let v be any vertex in G. Run BFS on G starting at
           v. Let u be a vertex in G at maximum distance from
           v as given by the BFS.
        2. Run BFS on G again with u as the starting vertex.
           Let z be the vertex at maximum distance from u as
           given by the BFS.
        3. Output the distance between u and z in G.

    The output of someAlgo() for the tree shown in the given figure is ___________. (Answer in integer)

    Question 4
    2025 Slot Set1 PYQ
    Level 3: Exam Standard

    Let be an undirected and unweighted graph with 100 vertices. Let denote the number of edges in a shortest path between vertices and in . Let the maximum value of , such that , be 30. Let be any breadth-first-search tree of . Which <b>ONE</b> of the given options is <b>CORRECT</b> for every such graph ?

    Question 5
    2024 Slot Set1 PYQ
    Level 3: Exam Standard

    Let be a directed graph and a depth first search (DFS) spanning tree in that is rooted at a vertex . Suppose is also a breadth first search (BFS) tree in , rooted at . Which of the following statements is/are TRUE for <i>every</i> such graph and tree ?

    Question 6
    2024 Slot Set1 PYQ
    Level 3: Exam Standard

    The number of edges present in the forest generated by the DFS traversal of an undirected graph with 100 vertices is 40. The number of connected components in is _________

    Question 7
    2023 PYQ
    Let . Let denote the powerset of . Consider an undirected graph whose vertex set is . For any , is an edge in if and only if (i) , and (ii) either or . For any vertex in , the set of all possible orderings in which the vertices of can be visited in a Breadth First Search (BFS) starting from is denoted by .

    If denotes the empty set, then the cardinality of is __________.
    Question 8
    2023 PYQ
    Level 4: Challenger
    Let be a simple, finite, undirected graph with vertex set . Let denote the maximum degree of and let denote the set of all possible colors. Color the vertices of using the following greedy strategy:




    Which of the following statements is/are TRUE?
    Question 9
    2021 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following directed graph:

    S
    Which of the following is/are correct about the graph?
    Question 10
    2021 Slot Set1 PYQ
    Level 3: Exam Standard
    An articulation point in a connected graph is a vertex such that removing the vertex and its incident edges disconnects the graph into two or more connected components.
    Let be a DFS tree obtained by doing DFS in a connected undirected graph .
    Which of the following options is/are correct?
    Free preview ends here

    Login to view the complete previous-year questions and solutions

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

    Solve 10+ Graph Traversal, Connectivity and Directed Graphs previous year questions for GATE CS with answers and detailed solutions. Free sample questions below.

    Chapter Roadmap: Graph Traversal and Connectivity

    Chapter Roadmap

    Graph Traversal, Connectivity and Directed Graphs

    1. Breadth-First Search Trees and Applications Shortest paths, level-by-level exploration, and tree diameter. Current
    2. Depth-First Search Edges and Timestamps Discovery and finish times, edge classification, and topological sorting.
    3. Connected Components and Articulation Points Graph connectivity, bridges, and critical nodes.
    4. Directed Graphs, Cycles and Strong Connectivity Directed acyclic graphs, strongly connected components, and Kosaraju algorithm.
    5. Greedy Graph Coloring Chromatic number, Welch-Powell algorithm, and upper bounds.

    By the end of this chapter, you will master the traversal techniques that form the backbone of graph problems in competitive exams.

    Breadth-First Search Trees and Applications

    Breadth-First Search Trees and Applications

    The definitive structure for finding shortest paths in unweighted graphs and solving distance-based puzzles.

    What you will learn here:

    • How BFS constructs a tree of shortest paths from a source
    • Edge classification rules specific to BFS traversal
    • The elegant two-BFS method for finding tree diameter
    • Solving complex subset-graph traversal problems
    Chapter: Graph Traversal, Connectivity and Directed Graphs → Topic 1 of 5

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

    Question 1 · Algorithms · 2026_Set1 MSQ
    Consider the following pseudocode for depth-first search (DFS) algorithm which takes a directed graph as input, where and are the discovery time and finishing time, respectively, of the vertex .


      unmark all
      
      for each
        if is unmarked
          
        end if
      end for

      mark
      
      
      for each
        if is unmarked
          
        end if
      end for
      
      
      return


    Suppose that the input directed graph is a directed acyclic graph (DAG).

    For an edge , which of the following options will NEVER be correct?
    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    ["B","D"]

    Step-by-Step Solution

    Key idea: This is a "DFS timestamp and DAG properties" question, recognisable because it asks which timestamp relationships are impossible for an edge in a Directed Acyclic Graph.

    Step 1: Understand the Parenthesis Theorem.

    For any two vertices and in a DFS traversal, their discovery/finish intervals and must be either completely disjoint or perfectly nested. They can never partially overlap.

    Step 2: Evaluate Option D.

    represents partially overlapping intervals. This violates the Parenthesis Theorem and is impossible in ANY DFS traversal, regardless of whether the graph is a DAG. Thus, Option D will NEVER be correct.

    Step 3: Evaluate Option B.

    represents perfectly nested intervals where is an ancestor of . This means the edge goes from a descendant to an ancestor, which is the definition of a back edge. A back edge implies the existence of a cycle. Since the input graph is a DAG (Directed Acyclic Graph), it cannot contain any cycles, and therefore cannot contain any back edges. Thus, Option B will NEVER be correct.

    Step 4: Evaluate Options A and C.

    Option A () represents nested intervals where is an ancestor of . This corresponds to a tree edge or forward edge, which are perfectly valid in a DAG.

    Option C () represents disjoint intervals where is completely finished before is discovered. This corresponds to a cross edge, which is also valid in a DAG.

    Answer: B, D

    Question 2 · Algorithms · 2025_Set2 MSQ

    Which of the following statements regarding Breadth First Search (BFS) and Depth First Search (DFS) on an undirected simple graph is/are TRUE?

    1. A.

      A DFS tree of is a Shortest Path tree of .

    2. B.

      Every non-tree edge of with respect to a DFS tree is a forward/back edge.

    3. C.

      If is a non-tree edge of with respect to a BFS tree, then the distances from the source vertex to and in the BFS tree are within of each other.

    4. D.

      Both BFS and DFS can be used to find the connected components of .

    Correct Answer:

    ["C","D"]

    Step-by-Step Solution

    Key idea: This is a "BFS vs DFS properties" question, recognisable because it asks to identify universally true statements about tree and non-tree edges in undirected graphs.

    Step 1: Evaluate Option A.

    "A DFS tree of G is a Shortest Path tree of G."

    This is FALSE. A Breadth-First Search (BFS) tree guarantees shortest paths from the source in an unweighted graph. A DFS tree does not; it prioritizes depth over distance.

    Step 2: Evaluate Option B.

    "Every non-tree edge of G with respect to a DFS tree is a forward/back edge."

    This is FALSE. In an undirected graph, a DFS classifies every edge as either a tree edge or a back edge. Forward edges and cross edges do not exist in undirected DFS. Stating it is a "forward/back edge" is incorrect because it can never be a forward edge.

    Step 3: Evaluate Option C.

    "If (u,v) is a non-tree edge of G with respect to a BFS tree, then the distances from the source vertex s to u and v in the BFS tree are within of each other."

    This is TRUE. In a BFS tree, vertices are explored level by level. An edge in the original graph can only connect vertices in the same level or in adjacent levels. Therefore, their distances from the source differ by at most 1.

    Step 4: Evaluate Option D.

    "Both BFS and DFS can be used to find the connected components of G."

    This is TRUE. Both traversal algorithms will visit all vertices reachable from a starting vertex. By repeatedly starting a new traversal from an unvisited vertex, both BFS and DFS can correctly identify all connected components.

    Answer: C, D

    Question 3 · Algorithms · 2025_Set2 NAT
    Consider the following algorithm someAlgo that takes an undirected graph as input.

    someAlgo(G)
    
        1. Let v be any vertex in G. Run BFS on G starting at
           v. Let u be a vertex in G at maximum distance from
           v as given by the BFS.
        2. Run BFS on G again with u as the starting vertex.
           Let z be the vertex at maximum distance from u as
           given by the BFS.
        3. Output the distance between u and z in G.

    The output of someAlgo() for the tree shown in the given figure is ___________. (Answer in integer)

    Correct Answer:

    6

    Step-by-Step Solution

    Key idea: This is a "tree diameter via two-BFS" question, recognisable because the algorithm runs BFS twice, each time picking the vertex at maximum distance, and outputs the final distance.

    Step 1: Understand the algorithm.

    The given algorithm is the standard linear-time method to find the diameter of a tree. The first BFS finds one endpoint of a longest path (a diameter), and the second BFS finds the other endpoint and the length of this path.

    Step 2: Trace the tree structure from the given figure.

    The vertices and edges form a tree. Let's trace the longest path.

    Starting from the rightmost leaf (v14 at 650, 172), the path to the leftmost leaf (v4 at 65, 245) is:

    v14 → v13 → v11 → v8 → v2 → v3 → v4.

    Step 3: Count the edges in this path.

    1. v14 to v13
    2. v13 to v11
    3. v11 to v8
    4. v8 to v2
    5. v2 to v3
    6. v3 to v4

    This path has exactly 6 edges. Checking other leaf-to-leaf paths (e.g., v14 to v6 or v14 to v7) also yields a maximum length of 6.

    Step 4: Conclude the output.

    Since the algorithm computes the tree diameter, and the diameter of this tree is 6, the output is 6.

    Answer: 6

    Question 4 · Algorithms · 2025_Set1 MCQ

    Let be an undirected and unweighted graph with 100 vertices. Let denote the number of edges in a shortest path between vertices and in . Let the maximum value of , such that , be 30. Let be any breadth-first-search tree of . Which <b>ONE</b> of the given options is <b>CORRECT</b> for every such graph ?

    1. A.

      The height of is exactly 15.

    2. B.

      The height of is exactly 30.

    3. C.

      The height of is at least 15.

    4. D.

      The height of is at least 30.

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a "BFS tree height vs graph diameter" question, recognisable because it relates the maximum distance in the graph (diameter) to the height of an arbitrary BFS tree.

    Step 1: Define the given parameters.

    The graph has a maximum distance (diameter) of 30. Let and be the vertices such that .

    Let be a BFS tree rooted at some arbitrary vertex .

    Step 2: Apply the triangle inequality.

    In any graph, the shortest path distance satisfies the triangle inequality:

    Substituting the known diameter:

    Step 3: Bound the maximum distance from .

    For the sum of two non-negative integers to be at least 30, at least one of them must be at least 15. Therefore:

    Step 4: Relate to BFS tree height.

    In a BFS tree rooted at , the distance from to any vertex in the tree is exactly the shortest path distance in .

    The height of is the maximum distance from to any vertex in . Thus:

    Step 5: Evaluate the options.

    The height can be exactly 15 (e.g., if is a path of 31 vertices and is the middle vertex). It is not necessarily 30 or greater than 30. Thus, "at least 15" is the only statement guaranteed to be true for every such graph and every BFS tree.

    Answer: C

    Question 5 · Algorithms · 2024_Set1 MSQ

    Let be a directed graph and a depth first search (DFS) spanning tree in that is rooted at a vertex . Suppose is also a breadth first search (BFS) tree in , rooted at . Which of the following statements is/are TRUE for <i>every</i> such graph and tree ?

    1. A.

      There are no back-edges in with respect to the tree

    2. B.

      There are no cross-edges in with respect to the tree

    3. C.

      There are no forward-edges in with respect to the tree

    4. D.

      The only edges in are the edges in

    Correct Answer:

    ["C"]

    Step-by-Step Solution

    Key idea: This is a "DFS and BFS tree equivalence" question, recognisable because it states that a single tree serves as both the DFS and BFS spanning tree for a directed graph , and asks what this implies about non-tree edges.

    Step 1: Analyze the implications of being a BFS tree.

    In a BFS tree rooted at , the depth of any vertex is the length of the shortest path from to . For any edge in , BFS guarantees that .

    Step 2: Analyze the implications of being a DFS tree.

    In a DFS tree, non-tree edges can be back edges, forward edges, or cross edges.

    Step 3: Evaluate Option C (Forward edges).

    Suppose there is a forward edge in . By definition of a DFS forward edge, is a proper descendant of in . This means the path in from to has length .

    Therefore, .

    However, since is an edge in , the BFS property requires .

    This is a contradiction ( is false). Thus, there can be NO forward edges. Option C is TRUE.

    Step 4: Evaluate Options A, B, and D with counterexamples.

    • Back edges (Option A): Consider with edges , , . DFS tree from is . BFS tree from is also . The edge is a back edge. So A is FALSE.
    • Cross edges (Option B): Consider with edges , , . DFS tree from (visiting then ) is . BFS tree is also . The edge is a cross edge. So B is FALSE.
    • Only tree edges (Option D): The counterexamples above show non-tree edges can exist. So D is FALSE.

    Answer: C

    Question 6 · Algorithms · 2024_Set1 NAT

    The number of edges present in the forest generated by the DFS traversal of an undirected graph with 100 vertices is 40. The number of connected components in is _________

    Correct Answer:

    60

    Step-by-Step Solution

    Key idea: This is a "counting connected components via DFS forest" question, recognisable because it provides the total number of vertices and the number of tree edges in a DFS forest.

    Step 1: Recall the relationship between vertices, tree edges, and components.

    When DFS is run on an undirected graph , it produces a DFS forest. Each tree in this forest corresponds to exactly one connected component.

    Step 2: Apply the tree edge formula.

    For any tree with vertices, there are exactly tree edges.

    If the graph has connected components, and the -th component has vertices, the total number of vertices is .

    The total number of tree edges is .

    Step 3: Rearrange to solve for .

    Step 4: Substitute the given values.

    We are given and .

    Answer: 60

    Question 7 · Algorithms · 2023 NAT
    Let . Let denote the powerset of . Consider an undirected graph whose vertex set is . For any , is an edge in if and only if (i) , and (ii) either or . For any vertex in , the set of all possible orderings in which the vertices of can be visited in a Breadth First Search (BFS) starting from is denoted by .

    If denotes the empty set, then the cardinality of is __________.
    Question 8 · Algorithms · 2023 MSQ
    Let be a simple, finite, undirected graph with vertex set . Let denote the maximum degree of and let denote the set of all possible colors. Color the vertices of using the following greedy strategy:




    Which of the following statements is/are TRUE?
    1. A.

      This procedure results in a proper vertex coloring of .

    2. B.

      The number of colors used is at most .

    3. C.

      The number of colors used is at most .

    4. D.

      The number of colors used is equal to the chromatic number of .

    Correct Answer:

    ["A","B"]

    Step-by-Step Solution

    Key idea: This is a "greedy graph coloring properties" question, recognisable because it describes the standard sequential greedy coloring algorithm and asks for its theoretical guarantees.

    Step 1: Evaluate Option A.

    "This procedure results in a proper vertex coloring of G."

    By definition, the algorithm assigns to each vertex the smallest positive integer that is not used by any of its already-colored neighbors. This ensures no two adjacent vertices share the same color. Thus, it always produces a proper vertex coloring. Option A is TRUE.

    Step 2: Evaluate Option B.

    "The number of colors used is at most ."

    When coloring vertex , it has at most neighbors. Therefore, at most colors are forbidden for . Since we choose from the infinite set , there is always at least one available color in the set . Thus, the algorithm never needs more than colors. Option B is TRUE.

    Step 3: Evaluate Option C.

    "The number of colors used is at most ."

    This is FALSE. Consider a complete graph . Here, . However, every vertex is connected to every other vertex, so the greedy algorithm (regardless of ordering) will use exactly colors. Since , it exceeds .

    Step 4: Evaluate Option D.

    "The number of colors used is equal to the chromatic number of G."

    This is FALSE. The greedy algorithm's performance heavily depends on the vertex ordering. For example, a bipartite graph (chromatic number 2) can be forced to use colors if the vertices are ordered such that each new vertex is connected to all previously colored vertices in the opposite partition.

    Answer: A, B

    Question 9 · Algorithms · 2021_Set2 MSQ
    Consider the following directed graph:

    S
    Which of the following is/are correct about the graph?
    1. A.

      The graph does not have a topological order.

    2. B.

      A depth-first traversal starting at vertex classifies three directed edges as back edges.

    3. C.

      The graph does not have a strongly connected component.

    4. D.

      For each pair of vertices and , there is a directed path from to .

    Correct Answer:

    ["A","B"]

    Step-by-Step Solution

    Key idea: This is a "directed graph properties and DFS classification" question, recognisable because it asks to evaluate structural properties (topological order, SCCs) and DFS edge classifications on a specific grid-like directed graph.

    Step 1: Analyze Option A (Topological Order).

    A directed graph has a topological order if and only if it is a Directed Acyclic Graph (DAG).

    Let's check for cycles. Consider the vertices in the top two rows, columns 2 and 3:

    R1C2 → R2C2 → R2C3 → R1C3 → R1C2.

    This forms a directed cycle of length 4. Since the graph contains a cycle, it is not a DAG and does not have a topological order. Option A is TRUE.

    Step 2: Analyze Option C (Strongly Connected Components).

    Every directed graph has at least one Strongly Connected Component (SCC). Even a single vertex with no cycles forms an SCC of size 1. Therefore, the statement "does not have an SCC" is fundamentally false. Option C is FALSE.

    Step 3: Analyze Option D (Universal Reachability).

    This option claims the graph is strongly connected (a path exists between every pair of vertices). However, vertex R1C4 is a sink (it has incoming edges but no outgoing edges). No path can start from R1C4 to reach any other vertex. Thus, the graph is not strongly connected. Option D is FALSE.

    Step 4: Analyze Option B (DFS Back Edges).

    A standard DFS starting from S (which is R4C1, the bottom-left vertex) explores the graph.

    The graph contains three disjoint fundamental cycles that will each yield exactly one back edge in a standard left-to-right, top-to-bottom DFS traversal:

    1. R4C1 → R4C2 → R3C2 → R3C1 → R4C1 (yields back edge R3C1 → R4C1)
    2. R4C3 → R4C4 → R3C4 → R3C3 → R4C3 (yields back edge R3C3 → R4C3)
    3. R1C2 → R2C2 → R2C3 → R1C3 → R1C2 (yields back edge R2C2 → R2C3)

    A careful trace of DFS confirms exactly three back edges are classified. Option B is TRUE.

    Answer: A, B

    Question 10 · Algorithms · 2021_Set1 MSQ
    An articulation point in a connected graph is a vertex such that removing the vertex and its incident edges disconnects the graph into two or more connected components.
    Let be a DFS tree obtained by doing DFS in a connected undirected graph .
    Which of the following options is/are correct?
    1. A.

      Root of can never be an articulation point in .

    2. B.

      Root of is an articulation point in if and only if it has 2 or more children.

    3. C.

      A leaf of can be an articulation point in .

    4. D.

      If is an articulation point in such that is an ancestor of in and is a descendent of in , then all paths from to in must pass through .

    Correct Answer:

    ["B","D"]

    Step-by-Step Solution

    Key idea: This is an "articulation point properties" question, recognisable because it asks to evaluate structural conditions for articulation points within a DFS tree of an undirected graph.

    Step 1: Evaluate Option A and B (Root conditions).

    The root of a DFS tree is an articulation point if and only if it has two or more children. If it has only one child, removing the root leaves the rest of the tree connected. If it has children, those subtrees have no cross edges between them (otherwise they would have been discovered in a single subtree), so removing the root disconnects them.

    Thus, Option A is FALSE, and Option B is TRUE.

    Step 2: Evaluate Option C (Leaf conditions).

    A leaf in a DFS tree has no descendants. Removing a leaf only removes that single vertex; it cannot disconnect any other pair of vertices in the graph. Therefore, a leaf can never be an articulation point. Option C is FALSE.

    Step 3: Evaluate Option D (Path conditions).

    If is an articulation point, its removal disconnects the graph. In a DFS tree, if is an ancestor of and is a descendant of , any path between and in the original graph must pass through . If there were a path bypassing , it would imply a back edge or cross edge connecting a descendant of to an ancestor of (or another branch), which would have been traversed during DFS, making not an articulation point. Option D is TRUE.

    Answer: B, D

    More previous year questions (pyqs) in this unit