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
Questions 11–15 of 16
Which one of the following statements is TRUE about the graph?
Answer: (D)
The intervals are , and .
P and Q. The interval of lies inside that of , so was discovered while was still open. So is a descendant of in the DFS tree, and they are in the same component. The middle drawing (2-cycle) shows one such graph: an edge between and .
R. is discovered at 14, after both and finished (at 12 and 10). Suppose had an edge to or . Then while that vertex was open, was still unvisited, so DFS would have visited before that vertex finished. That did not happen, so has no edge to or . This is why the triangle drawing is crossed out: with the edges and , would be visited inside the call of . is a component by itself (the loop on in the left drawing is just one possible picture).
So there are two components, and .
Answer: (D)
Answer: (D)
Key fact: in a DFS of an undirected graph, every non-tree edge joins a vertex to one of its ancestors (there are no cross edges).
Let be a leaf of the DFS tree . Take any neighbour of . If were unvisited when DFS reached , DFS would go to it and would get a child, which is false. If had already finished, it would have visited itself. So is visited but not finished, which means it is an ancestor of on the root-to- path.
The figure shows this for two neighbours K and L of (drawn as U); all are on the current DFS path. Since , has its parent and at least one more neighbour, which is a higher ancestor. Let be the highest neighbour of . The tree path from down to , closed by the edge , is a cycle. All other neighbours of lie on that path (they are ancestors below ). So (D) is always true.
(A), (B), (C) can fail. Take triangles and joined by the edge . DFS from (order b, a, u, c, d, v) has leaves and , each of degree 2. They have no common neighbour, so (A) fails. No cycle contains both, since is a bridge, so (C) fails. For (B), take with parts and . DFS order a, x, b, y, z gives leaves and of degree 2, and removing any single vertex leaves them connected, so (B) fails.
Answer: (D)Answer: (D)
The figures show a DFS on a directed example graph with 8 vertices. Solid lines are tree edges; dashed and dotted lines are cross, forward and back edges, which are not tree edges. The 6 tree edges (a-b, a-c, b-d, d-e, e-f, g-h) form 2 DFS trees, one rooted at a and one at g, and . In general, n - k counts the trees in the DFS forest. That count equals the number of connected components when the graph is undirected, which is how the question is read. (In a directed graph like the figure, the 2 trees do not match the strongly connected components.)
In an undirected graph, one DFS tree covers exactly one connected component, because DFS from a root reaches every vertex of its component and no other.
Let there be components, and let component have vertices. A tree on vertices has edges, so component gives tree edges.
Total:
So .
Among the following sequences
(I) a b e g h f
(II) a b f e h g
(III) a b f h g e
(IV) a f g h b e
Which are depth first traversals of the above graph?
Answer: (D)
DFS moves from the current vertex to an unvisited neighbour. It goes back (backtracks) only when the current vertex has no unvisited neighbour left.
I : , , , , are all edges to new vertices. Valid.
II : and are not joined by an edge. Also still has unvisited neighbours and , so DFS cannot leave by backtracking. Not valid.
III : , , , , are all edges to new vertices. Valid.
IV : , , , , are all edges to new vertices. Valid.
So I, III and IV are DFS traversals. Options (A) and (C) include II, and (B) leaves out III.
Answer: (D)
Answer: (B)
If is visited before , there are two cases:
1. is on an earlier level, so .
2. and are on the same level and came first in the queue, so .
Together, .
(A) is wrong because equality can happen, for example two neighbours of . (C) is wrong because a vertex on a later level is never visited before one on an earlier level.
Answer: (B)
Keep practising