A Hamiltonian Path in a graph is a path that visits each vertex exactly once. Given a graph G=(V,E)G = (V, E)G=(V,E), the Hamiltonian Path Problem (HPP) asks whether there exists such a path in GGG. If the path starts and ends at the same vertex, it is called a Hamiltonian Cycle.
Step 1: Proving Hamiltonian Path is in NP
To show that HPP is in NP, we need to prove that a given solution can be verified in polynomial time.
- Suppose we are given a certificate (a sequence of vertices).
- We check if:
- The sequence contains all vertices exactly once.
- There exists an edge between each consecutive vertex in the sequence.
- This verification takes at most O(V²) time (checking edges in an adjacency matrix) or O(V + E) time (if using adjacency lists).
Since verification is polynomial, HPP belongs to NP.
Step 2: Proving NP-Hardness
To prove HPP is NP-Hard, we reduce a known NP-Complete problem to it. We use the Hamiltonian Cycle Problem (HCP), which is already NP-Complete.
Given a graph G=(V,E)G = (V, E)G=(V,E) where we want to check for a Hamiltonian Cycle, we construct a new graph G′G'G′ by removing an arbitrary vertex v∈Vv \in Vv∈V.
Claim: GGG has a Hamiltonian Cycle if and only if G′G'G′ has a Hamiltonian Path.
- If GGG has a Hamiltonian Cycle, removing vvv gives a Hamiltonian Path in G′G'G′.
- If G′G'G′ has a Hamiltonian Path, adding vvv back reconstructs a Hamiltonian Cycle in GGG.
This transformation is done in polynomial time, proving that HPP is at least as hard as HCP.
Since HCP is NP-Complete, and we have reduced it to HPP in polynomial time, HPP is NP-Hard.