Question

Suppose depth first search is executed on the graph below starting at some unknown vertex. Assume that a recursive call to visit a vertex is made only after first checking that the vertex has not been visited earlier. Then the maximum possible recursion depth (including the initial call) is _____.

Undirected graph with two 3x3 grids connected by an intermediate 2-vertex path and one pendant vertex on the right, total 20 vertices
Undirected graph for DFS depth analysis
Official answer and solution
Official answer
19
From
Official GATE 2014 answer key

Solution

Determine the maximum recursion depth by finding the longest simple path in the graph.

  1. Graph Structure and Vertex Count:

    • Left component: 3×33 \times 3 grid of 99 vertices.
    • The bottom-right vertex of the left grid connects via an edge to a bridge path.
    • Bridge path: a bottom vertex with a degree-1 pendant vertex above it (22 vertices).
    • Right component: 3×33 \times 3 grid of 99 vertices.
    • Pendant vertex on the right: degree-1 vertex connected to the middle-right vertex of the right grid (11 vertex).
    Total Vertices=9+2+9+1=21 vertices.\text{Total Vertices} = 9 + 2 + 9 + 1 = 21\text{ vertices}.
  2. DFS Recursion Depth and Simple Paths:
    The maximum possible recursion depth in a DFS traversal without backtracking corresponds to the number of vertices in the longest simple path in the graph.

  3. Analyzing Endpoints of a Simple Path:

    • A simple path has exactly two endpoints (start and finish).
    • The left 3×33 \times 3 grid has only ONE edge connecting it to the rest of the graph (a bridge edge at its bottom-right vertex). Once a simple path enters the left grid, it cannot exit without repeating vertices. Therefore, any simple path visiting the left grid must either start or end in the left grid.
    • This leaves only ONE endpoint available for the remainder of the graph.
    • The remainder of the graph contains two distinct degree-1 pendant vertices:
      1. The middle upright pendant vertex.
      2. The rightmost pendant vertex.
    • To visit a degree-1 vertex in a simple path, that vertex must be an endpoint of the path. Since there is only one remaining endpoint, a simple path can visit at most ONE of these two degree-1 vertices.
    • Furthermore, if the path visits the middle pendant vertex as an endpoint, it cannot reach the right 3×33 \times 3 grid. Thus, to maximize vertices visited, the path must skip the middle pendant vertex and continue to the right grid, ending at the rightmost pendant vertex.
    • In the right 3×33 \times 3 grid, a Hamiltonian path entering at the bottom-left and exiting to the right pendant at the middle-right vertex can visit 8 of the 9 grid vertices (or all 9 if properly routed), but at least two vertices overall in the graph cannot be included in any single simple path.
  4. Therefore, the longest simple path can visit at most 21−2=1921 - 2 = 19 vertices.

Hence, the maximum recursion depth is 1919.

Final answer

19

Common trap

Assuming DFS can visit all 21 vertices in a single recursive chain without backtracking.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 3
Question
23 of 65, printed as CS Q.13
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.