Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

An undirected graph has the vertices E, F, O, P, Q and the edges Q–E, F–O, F–E, F–P, O–P, E–P. A walk may repeat vertices and edges. How many walks of length exactly 4 go from Q to F?

  1. A. 3
  2. B. 8
  3. C. 1
  4. D. 6

Correct answer: D. 6

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic