Data Structures & AlgorithmsModerateGATE CS · ICPC regional

The directed acyclic graph has the vertices C, E, J, K, L, M, Q, Z and the arcs Q→C, Q→M, C→J, L→J, L→K, Q→K, E→J, M→C, M→L, M→E, Q→E, L→C. How many distinct directed paths go from Q to J? A path is a sequence of consecutive arcs; a single arc counts as a path.

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

Correct answer: C. 6

Count with ways[x] = sum of ways[y] over arcs y→x, starting at ways[Q] = 1 and processing vertices in a topological order. This gives 6 paths; using only paths of at most two arcs would give 2.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic