Data Structures & AlgorithmsHardGATE CS · ICPC regional

A directed acyclic graph has the vertices J, K, L, Q, R, S, X, Y and the directed edges S→R, S→Y, K→Y, L→K, R→Q, Y→X, K→X, J→Q, Y→J, J→X, X→Q, R→J, S→Q, S→J, L→X, L→Q. How many edges does a longest directed path in this graph contain?

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

Correct answer: A. 5

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic