Data Structures & AlgorithmsModerateCodeforces Div 2 C · GATE CS

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

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

Correct answer: A. 7

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic