Consider a directed graph , where and . Suppose the adjacency list of each vertex is in decreasing order of vertex number, and depth-first search (DFS) is performed at vertex 0. The number of vertices that will be discovered after vertex 50 is ______ (Answer in integer)
GATE 2025 DA Q.65: solution and answer
Answer and solution
ANSWER
The number of vertices discovered after vertex 50 is 75.
Official answer: 75Source: Official answer key, GATE 2025 DA (IIT Roorkee)
Each vertex has edges only to and (those that exist in ). In decreasing order the adjacency list of is , so DFS always tries first.
DFS from therefore descends the even chain . Vertex has no out-edges, so DFS backtracks.
The even vertices discovered after on this descent are , which is vertices.
Back at : its list is . Vertex is already discovered, and is not, so is discovered. Its neighbours (, and which does not exist) give nothing new, so DFS returns to and then to .
At every even vertex on the way back, the first neighbour is already discovered and the second neighbour is not, so is discovered. Its neighbours and are already discovered, so DFS returns. This happens at .
Hence the odd vertices are discovered one by one during the backtrack, in that order. All of them are discovered after the descent has passed , so all odd vertices come after vertex .
Vertices discovered after : .
KEY IDEA
With adjacency lists in decreasing order, DFS first runs straight down the even chain to 100, then discovers each odd vertex while backtracking, so the discovery order is fixed by 'always take the largest neighbour first'.
WATCH OUT FOR
Counting only the even vertices 52 to 100 (25), or forgetting that the odd vertices below 50 (1 to 49) are also discovered after 50, because they are only reached during backtracking, long after the descent passed 50.
From the GATE 2025 DA paper. Every question of this paper · Download the full paper · All 28 Graph algorithms questions, with notes
More Graph algorithms questions
- GATE 2024 CS1 Q.60 · NAT · 2 marksThe number of edges present in the forest generated by the DFS traversal of an undirected graph G with 100…
- GATE 2024 CS2 Q.51 · MSQ · 2 marksLet G be an undirected connected graph in which every edge has a positive integer weight.
- GATE 2024 CS2 Q.59 · NAT · 2 marksThe number of distinct minimum-weight spanning trees of the following graph is.
- GATE 2024 DA Q.14 · MCQ · 1 markConsider performing depth-first search (DFS) on an undirected and unweighted graph G starting at vertex s.
