• edited by
32,973 views
63 63 votes

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

5 Answers

Best answer
101 101 votes

Total $21$ nodes are there. $2$ nodes require back track here in this question.

So, max recursion depth is $21-2= 19$

(Do $\textsf{DFS}$ from extreme ends such that max recursion depth will occur. i.e., take leftmost top node as initial node for $\textsf{DFS}$ as shown in below image)

Note:- Backtrack means it reduces recursion depth in stack.

• edited by
18 18 votes
19. apply DFS.
15 15 votes

Many Would be confused why the recursion depth is not 18 when we have 19 nodes in the recursion call stack, the reason for this is hidden in the definition of recursion depth.

 

Definition of Recursion Depth : The maximum depth of recursion refers to the number of levels of activation of a procedure which exist during the deepest call of the procedure.

 

As we can clearly see that recursion depth is Number of levels not the Height of tree so, answer would be 19 not 18

1 1 vote

The best answer is enough so here I m just posting about the difference between tree depth & recursion depth



1. Tree/Graph Depth

When we say a node is at depth 4 from root:

 
A → B → C → D → E

Depth of E = 4 edges

because

depth=#edges from root


2. Recursion Depth

Suppose DFS does:

 
DFS(A)
  DFS(B)
    DFS(C)
      DFS(D)
        DFS(E)

At the deepest point, stack contains:

 
A
B
C
D
E

That's 5 function calls.

So recursion depth = 5, not 4.


Relationship

For a simple chain:

 
A → B → C → D → E
  • Number of edges = 4
  • Number of vertices = 5
  • Recursion depth = 5

Hence:

Recursion Depth=Path Length (edges)+1

Answer:
Position:
Show:

Related questions

66 66 votes
9 answers 9 answers
18.8k
18.8k views
go_editor asked Sep 28, 2014
18,790 views
Consider the tree arcs of a BFS traversal from a source node $W$ in an unweighted, connected, undirected graph. The tree $T$ formed by the tree arcs is a data structure f...
51 51 votes
5 answers 5 answers
19.6k
19.6k views
go_editor asked Sep 26, 2014
19,572 views
Let $G$ be a graph with $n$ vertices and $m$ edges. What is the tightest upper bound on the running time of Depth First Search on $G$, when $G$ is represented as an adjac...
9 9 votes
5 answers 5 answers
8.9k
8.9k views
go_editor asked Sep 28, 2014
8,918 views
In the context of modular software design, which one of the following combinations is desirable?High cohesion and high couplingHigh cohesion and low couplingLow cohesion ...
71 71 votes
10 answers 10 answers
24.3k
24.3k views
Akash Kanase asked Feb 12, 2016
24,336 views
Breadth First Search (BFS) is started on a binary tree beginning from the root vertex. There is a vertex $t$ at a distance four from the root. If $t$ is the $n^{\text{th}...