Data Structures & AlgorithmsHardProduct-company MCQ pattern

DFS is run on a directed graph with edges A→B, A→C, B→D, C→D, C→A, D→B, E→C, E→D, starting from A, taking vertices and neighbours in alphabetical order and restarting from the smallest unvisited vertex. How many back edges are found?

  1. A. 0
  2. B. 2
  3. C. 1
  4. D. 3

Correct answer: B. 2

Tree edges: A→B, B→D, A→C, then E starts a new tree. D→B (B is still on the stack) and C→A (A is still on the stack) are back edges, so 2; C→D, E→C and E→D are cross edges. Each back edge marks a cycle: B⇄D and A→C→A.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic