Consider the following sequence of nodes for the undirected graph given below.…
2008
Consider the following sequence of nodes for the undirected graph given below.
a b e f d g c
a b e f c g d
a d g e b c f
a d b c g e f
A Depth First Search (DFS) is started at node a. The nodes are listed in the order they are first visited. Which all of the above is (are) possible output(s)?

- A.
1 and 3 only
- B.
2 and 3 only
- C.
2, 3 and 4 only
- D.
1, 2, and 3
Attempted by 158 students.
Show answer & explanation
Correct answer: B
Answer: the possible DFS outputs are the sequences "a b e f d g c" and "a d g e b c f".
Reasoning (how to get each valid sequence):
To obtain "a b e f d g c": choose at node a the neighbor b before d. At b choose e before other neighbors. From e go to f. From f choose d and then from d go to g. After finishing that branch, backtrack and visit c last. This obeys DFS rules (always move to an unvisited neighbor; backtrack only when none remain).
To obtain "a d g e b c f": choose at node a the neighbor d before b. From d move to g first, then backtrack and visit e, then continue to b. From b go to c, and finally to f. This is also a valid DFS pre-order given an appropriate ordering of each node's adjacency list.
Why the other two sequences are not possible:
"a b e f c g d" is not possible because it requires visiting c (from f) and g before visiting d, while the connectivity of the graph ensures that d would have been discovered earlier in the DFS branch containing f (or would need backtracking that contradicts the required order). The necessary interleaving of discoveries cannot occur in a standard DFS pre-order.
"a d b c g e f" is not possible because after going a -> d -> b, DFS must explore b's reachable subtree before returning; the particular ordering that places g and e after c but before f conflicts with the immediate discovery rule and required backtracking points, so no adjacency ordering yields that exact visit sequence.
Summary: valid DFS pre-orders depend on the order in which each node explores its neighbors. Sequences "a b e f d g c" and "a d g e b c f" can be produced by appropriate neighbor orderings; the other two cannot because they violate DFS discovery/backtracking constraints given the graph's connections.
A video solution is available for this question — log in and enroll to watch it.