Skip to content
Official PYQ · 2003

GATE CS 2003 Q21 — Graph Traversals (BFS, DFS)

Explain with AI

Consider the following graph: 

Among the following sequences:

  1. abeghf
  2. abfehg
  3. abfhge
  4. afghbe

Which are the depth-first traversals of the above graph?

Choose one option
View worked solution

Answer: D

The given graph has the vertex set V={a,b,e,f,g,h}V = \{a, b, e, f, g, h\} with the following edge connections:

  • aa is connected to b,fb, f
  • bb is connected to a,e,f,ha, e, f, h
  • ee is connected to b,gb, g
  • ff is connected to a,b,g,ha, b, g, h
  • gg is connected to e,f,he, f, h
  • hh is connected to b,f,gb, f, g

Let's analyze each sequence using Depth-First Search (DFS) rules:

  1. I. a→b→e→g→h→fa \to b \to e \to g \to h \to f: Valid. From aa, we visit bb, then ee, then gg, then hh, and finally ff. All steps follow valid unvisited adjacent edges.
  2. II. a→b→f→e→h→ga \to b \to f \to e \to h \to g: Invalid. After visiting ff, we backtrack to bb to visit ee. From ee, the only unvisited neighbor is gg. In DFS, we must explore the unvisited neighbor gg before backtracking. Thus, we cannot jump to hh from ee or backtrack to bb to reach hh while gg remains unvisited.
  3. III. a→b→f→h→g→ea \to b \to f \to h \to g \to e: Valid. From a→b→f→h→ga \to b \to f \to h \to g, the only unvisited vertex left is ee, which is directly connected to the current vertex gg.
  4. IV. a→f→g→h→b→ea \to f \to g \to h \to b \to e: Valid. From a→f→g→h→ba \to f \to g \to h \to b, the only unvisited vertex left is ee, which is directly connected to the current vertex bb.

Thus, sequences I, III, and IV are valid depth-first traversals.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Graph Traversals (BFS, DFS) questions