Data Structures & AlgorithmsHardGATE CS

In the DAG with edges A→C, A→F, A→G, B→A, B→C, B→G, E→C, E→D, F→E, F→G, G→D, how many distinct directed paths are there from B to D, and how many edges does the longest one have?

  1. A. 4 paths, longest 3
  2. B. 5 paths, longest 4
  3. C. 3 paths, longest 4
  4. D. 4 paths, longest 4

Correct answer: D. 4 paths, longest 4

Process vertices in reverse topological order: paths(u) = Σ paths(v) over edges u→v with paths(D) = 1, giving 4; the same order with max(1 + len(v)) gives the longest path, 4 edges.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic