Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

A directed graph on vertices 0 to 3 has adjacency matrix [[0, 1, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1], [1, 0, 0, 0]] (adj[i][j] = 1 means an edge i→j). How many directed Hamiltonian paths (paths visiting every vertex exactly once, in any starting vertex) exist?

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

Correct answer: C. 5

dp[mask][v] counts paths covering the vertex set mask and ending at v, extended one edge at a time. Counting every path that uses only some vertices, or every possible permutation (24), ignores the edges that the graph actually has.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic