Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

An undirected graph has the vertices D, P, Q, Y and the edges Y–P, Y–D, P–D, Q–D. A walk may repeat vertices and edges. How many walks of length exactly 3 go from Y to P?

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

Correct answer: A. 3

The (i, j) entry of the k-th power of the adjacency matrix counts walks of length k, giving 3. Simple paths from Y to P give only 2, and walks of length 2 give 1.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic