Data Structures & AlgorithmsModerateGATE CS · ICPC regional

A directed acyclic graph has the vertices C, D, G, J, O, S, T and the directed edges D→J, G→C, G→O, S→O, C→J, D→O, O→J, D→T, C→O, S→C, O→T, C→T, S→D. How many edges does a longest directed path in this graph contain?

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

Correct answer: B. 3

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic