Data Structures & AlgorithmsModerateCodeforces Div 2 C · GATE CS

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

  1. A. 5
  2. B. 7
  3. C. 0
  4. D. 9

Correct answer: A. 5

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic