Two classic graph problems show how a small change in the question separates an easy problem from a hard one.
Hamiltonian Circuit
A Hamiltonian cycle (circuit) of a graph is a closed path that visits every vertex exactly once and returns to the start. Contrast this with an Euler circuit, which uses every edge once and can be tested in polynomial time (all degrees even and graph connected). Deciding whether a Hamiltonian cycle exists is NP-complete: no polynomial algorithm is known. The problem models route planning and is closely related to the travelling salesman problem.
Backtracking for Hamiltonian Circuit
Fix vertex 0 as the start (the cycle can be rotated), and build the path x[0] = 0, x[1], ..., x[n − 1]. For position k, try each vertex v such that (i) v is adjacent to the previous vertex x[k − 1], and (ii) v is not already in the path; for the last position k = n − 1 also require that v is adjacent to x[0], closing the cycle. If no vertex fits, backtrack. The worst case examines all (n − 1)! orderings, so the time is O(n!); adjacency checks prune many. A graph with a vertex of degree 1 cannot have a Hamiltonian cycle.
Vertex Cover
A vertex cover of an undirected graph is a set of vertices such that every edge has at least one endpoint in the set. The minimum vertex cover is the smallest such set; deciding whether a cover of size at most k exists is NP-complete. Applications include placing guards or sensors so that every corridor (edge) is watched. A backtracking search tries including or excluding each vertex and prunes branches that already exceed the best cover found or cannot cover the remaining edges. A simple 2-approximation exists: repeatedly pick any uncovered edge, add both endpoints to the cover and delete all edges they touch. The result is at most twice the optimal size, because an optimal cover must contain one endpoint of each edge picked.
Definition: Hamiltonian circuit. A cycle in a graph that passes through every vertex exactly once and returns to its starting vertex.
Table 4.4: Related graph problems
| Problem | Question | Complexity |
|---|---|---|
| Euler circuit | Use every edge once | Polynomial |
| Hamiltonian circuit | Visit every vertex once | NP-complete |
| Vertex cover | Cover all edges with k vertices | NP-complete |
| Minimum spanning tree | Cheapest connecting tree | Polynomial |
Example 4.4: Hamiltonian circuit and vertex cover
Problem: Graph on vertices 0–4 with edges 0-1, 0-3, 1-2, 1-3, 1-4, 2-4, 3-4. Find a Hamiltonian circuit. For the graph with edges 1-2, 1-3, 2-3, 3-4, 4-5 find a minimum vertex cover.
Solution:
- From 0 go to 1; from 1 the neighbours are 0, 2, 3, 4: take 2; from 2 take 4; from 4 take 3; 3 is adjacent to 0, so close the cycle.
- Circuit: 0 → 1 → 2 → 4 → 3 → 0 (5 edges).
- Vertex cover: {1, 2, 4} (or {1, 3, 4}) covers all five edges. No cover of 2 vertices exists: edges 1-2 and 4-5 are disjoint, so one vertex from each is needed, and each such pair (1,4), (1,5), (2,4), (2,5) leaves some edge uncovered.
- The 2-approximation picks edge 1-2, then 3-4, giving the cover {1, 2, 3, 4} of size 4 ≤ 2 × 3.
Result: Hamiltonian circuit 0-1-2-4-3-0; minimum vertex cover size 3.
from itertools import combinations
adj = {0: [1, 3], 1: [0, 2, 3, 4], 2: [1, 4],
3: [0, 1, 4], 4: [1, 2, 3]}
def ham(path):
if len(path) == len(adj):
return path + [0] if 0 in adj[path[-1]] else None
for v in adj[path[-1]]:
if v not in path:
r = ham(path + [v])
if r:
return r
print("Hamiltonian:", ham([0]))
E = [(1, 2), (1, 3), (2, 3), (3, 4), (4, 5)]
for k in range(1, 6):
cov = [c for c in combinations(range(1, 6), k)
if all(u in c or v in c for u, v in E)]
if cov:
print("min vertex cover size", k, cov[0])
break
Output
Hamiltonian: [0, 1, 2, 4, 3, 0]
min vertex cover size 3 (1, 2, 4)
Common mistakes
- Confusing a Hamiltonian circuit (every vertex once) with an Euler circuit (every edge once).
- Forgetting to check the edge from the last vertex back to the start.
- Treating a vertex cover as a set of edges; it is a set of vertices.
- Assuming the 2-approximation gives the minimum cover.
Key takeaways
- Hamiltonian circuit visits each vertex once; NP-complete; backtracking worst case O(n!).
- Euler circuit is easy: connected with all degrees even.
- Vertex cover: vertices touching every edge; minimum cover is NP-hard.
- Picking both ends of uncovered edges gives a 2-approximation.
Practice questions
- How does a Hamiltonian circuit differ from an Euler circuit?
- What is a vertex cover?
- What is the worst-case time of backtracking for Hamiltonian circuit?
Answers to the practice questions
- Hamiltonian visits every vertex exactly once; Euler uses every edge exactly once.
- A set of vertices such that every edge has at least one endpoint in it.
- O(n!).
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit IV
