Data Structures & AlgorithmsHardGATE CS · ICPC regional

The directed acyclic graph has the vertices A, I, L, P, Q, W and the arcs W→P, I→A, W→I, L→I, Q→A, L→W, L→Q, L→A. How many distinct directed paths go from L to A? A path is a sequence of consecutive arcs; a single arc counts as a path.

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

Correct answer: A. 4

Count with ways[x] = sum of ways[y] over arcs y→x, starting at ways[L] = 1 and processing vertices in a topological order. This gives 4 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