Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B

A directed acyclic graph has the vertices B, F, M, Q, R, V, W, X and the directed edges F→M, B→F, W→M, V→R, M→Q, F→Q, V→Q, B→V, B→X, B→R, M→X, W→Q, B→Q, Q→X. How many edges does a longest directed path in this graph contain?

  1. A. 5
  2. B. 7
  3. C. 3
  4. D. 4

Correct answer: D. 4

Take dp[x] = 1 + max(dp[y]) over arcs y→x, with dp = 0 for sources; the largest dp value is 4 edges. Following the first listed out-arc at each step gives only 4, and counting vertices instead of edges gives 5.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic