Graph Traversal (BFS and DFS): GATE CS Previous Year Questions

Graph Traversal (BFS and DFS) · Algorithms

16 questions · asked in 11 of 27 years (2000–2026)

MCQ 12 · MSQ 2 · NAT 21-mark 12 · 2-mark 4

2000Questions per year2026

Questions 1–5 of 16

Q1GATE 2026 (Session 1)2 marksMSQ
Consider the following pseudocode for depth-first search (DFS) algorithm which takes a directed graph G(V,E)G(V, E) as input, where d[v]d[v] and f[v]f[v] are the discovery time and finishing time, respectively, of the vertex v∈Vv \in V. DFS(G):unmark all v∈Vt←0for each v∈Vif v is unmarkedt←Explore(G,v,t)end ifend forExplore(G,v,t):mark vt←t+1d[v]←tfor each (v,w)∈Eif w is unmarkedt←Explore(G,w,t)end ifend fort←t+1f[v]←treturn t\begin{array}{|l|l|} \hline \begin{array}{l} \textbf{DFS}(G): \\ \text{unmark all } v \in V \\ t \leftarrow 0 \\ \text{for each } v \in V \\ \quad \text{if } v \text{ is unmarked} \\ \qquad t \leftarrow \text{Explore}(G,v,t) \\ \quad \text{end if} \\ \text{end for} \end{array} & \begin{array}{l} \textbf{Explore}(G,v,t): \\ \text{mark } v \\ t \leftarrow t+1 \\ d[v] \leftarrow t \\ \text{for each } (v,w) \in E \\ \quad \text{if } w \text{ is unmarked} \\ \qquad t \leftarrow \text{Explore}(G,w,t) \\ \quad \text{end if} \\ \text{end for} \\ t \leftarrow t+1 \\ f[v] \leftarrow t \\ \text{return } t \end{array} \\ \hline \end{array}

Suppose that the input directed graph G(V,E)G(V, E) is a directed acyclic graph (DAG). For an edge (u,v)∈E(u, v) \in E, which of the following options will NEVER be correct?

One or more options are correct.

Q2GATE 2025 (Session 2)1 markMSQ
Which of the following statements regarding Breadth First Search (BFS) and Depth First Search (DFS) on an undirected simple graph GG is/are TRUE?

One or more options are correct.

Q3GATE 2025 (Session 2)2 marksNAT
Consider the following algorithm someAlgo that takes an undirected graph GG as input.

someAlgo(G):\texttt{someAlgo(G)}:
1. Let vv be any vertex in GG. Run BFS on GG starting at vv. Let uu be a vertex in GG at maximum distance from vv as given by the BFS.
2. Run BFS on GG again with uu as the starting vertex. Let zz be the vertex at maximum distance from uu as given by the BFS.
3. Output the distance between uu and zz in GG.

The output of someAlgo(T)\texttt{someAlgo(T)} for the tree shown in the given figure is __________. (Answer in integer)

Q4GATE 20232 marksNAT
Let U={1,2,3}U = \{1, 2, 3\}. Let 2U2^U denote the powerset of UU. Consider an undirected graph GG whose vertex set is 2U2^U. For any A,B∈2U,(A,B)A,B \in 2^U, (A,B) is an edge in GG if and only if (i) A≠BA \neq B, and (ii) either A⊆BA \subseteq B or B⊆AB \subseteq A. For any vertex AA in GG, the set of all possible orderings in which the vertices of GG can be visited in a Breadth First Search (BFS) starting from AA is denoted by B(A)B(A).
If ϕ\phi denotes the empty set, then the cardinality of B(ϕ)B(\phi ) is ____.

Q5GATE 2014 (Session 1)1 markMCQ−0.33 if wrong
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 adjacency matrix?

Keep practising

Topics in Algorithms

All CS topics ›