Data Structures & AlgorithmsVery hardICPC regional · GATE CS

Problem: In the undirected graph on vertices 0–4 with edges 0-1, 0-2, 1-2, 1-3, 2-3, 3-4, how many Hamiltonian paths exist (paths that visit every vertex exactly once; a path and its reverse count as different paths)?

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

Correct answer: D. 8

Backtracking over all orders finds 8 directed Hamiltonian paths (4 undirected ones, each counted in both directions; every path must end or start at vertex 4, whose only neighbour is 3). Counting a path and its reverse once gives 4, counting only paths that start at 0 gives 2, and 5! = 120 ignores edges.

More Coding Interview Problems questions

Exams that ask this topic