complete the graph, weights 1 and 2, budget |V|

UHC ≤p TSP

UHC≤pTSPUHC\le_p TSP, so TSPTSP is NP-hard (and with membership in NP, NP-complete).

The construction

Algorithm R′′R''. On input G=(V,E)G=(V,E) — a simple undirected graph:

  1. Let G^=(V,E^)\hat G=(V,\hat E) be the complete graph on VV.
  2. Define w:E^→[0,∞)w:\hat E\to[0,\infty) by w(e)={1e∈E2e∉Ew(e)=\begin{cases}1 & e\in E\\ 2 & e\notin E\end{cases}
  3. Return (G^,  w,  ∣V∣)\left(\hat G,\;w,\;|V|\right).

Why from UHCUHC. One could start from a Hamiltonian path — there are reductions between all versions — but TSPTSP asks about a cycle in an undirected graph, so UHCUHC is the nearest problem. The same principle recurs: start from the closest problem.

On step 1. Once VV is fixed there is a unique complete graph on it; no choice is involved. In practice step 1 means "write down all the edges", including those absent from GG.

Visualization

Guided construction

Hamiltonian Cycle → TSP

Step 1 of 6
Hamiltonian Cycle to TSP on five verticesA pentagon of five original cost-1 edges is completed by a pentagram of five cost-2 chords; the pentagon itself is a tour of cost five, exactly the budget B = n = 5.Source G — 5 vertices, 5 edges12345
Original edge of G — cost 1

Correctness — both directions

UHC ∈ ⟹ TSP ∈

Suppose G∈UHCG\in UHC; then GG has a Hamiltonian cycle CC. Since E⊆E^E\subseteq\hat E, that same CC is a cycle in G^\hat G, and Hamiltonian there too — the vertex set did not change, only edges were added.

A Hamiltonian cycle on ∣V∣|V| vertices has exactly ∣V∣|V| edges, each of them an edge of EE and therefore of weight 11. So w(C)=∣V∣⋅1=∣V∣≤∣V∣,w(C)=|V|\cdot1=|V|\le|V| , hence (G^,w,∣V∣)∈TSP(\hat G,w,|V|)\in TSP.

TSP ∈ ⟹ UHC ∈

The lecture deliberately gave two versions of this direction — to show there is nothing sacred about proving "in ⇒\Rightarrow in" versus "out ⇒\Rightarrow out". Both are correct.

Version A (by contradiction). Suppose G∉UHCG\notin UHC, and let CC be any Hamiltonian cycle in G^\hat G (one exists, as G^\hat G is complete). CC has exactly ∣V∣|V| edges, each of weight at least 11. Moreover CC contains an edge outside EE — otherwise all its edges would lie in EE and CC would be a Hamiltonian cycle of GG, contradicting the hypothesis. That edge has weight 22. So w(C)≥(∣V∣−1)⋅1+2=∣V∣+1>∣V∣.w(C)\ge(|V|-1)\cdot1+2=|V|+1>|V| . This holds for every Hamiltonian cycle in G^\hat G, so none has weight ≤∣V∣\le|V|, i.e. (G^,w,∣V∣)∉TSP(\hat G,w,|V|)\notin TSP.

Version B (direct, and two lines shorter). Suppose (G^,w,∣V∣)∈TSP(\hat G,w,|V|)\in TSP; then some Hamiltonian cycle CC in G^\hat G has w(C)≤∣V∣w(C)\le|V|. Since every edge weighs at least 11 and CC has exactly ∣V∣|V| edges, w(C)≥∣V∣w(C)\ge|V|. The two inequalities give w(C)=∣V∣w(C)=|V| exactly. Now w(C)w(C) is a sum of ∣V∣|V| terms each ≥1\ge1 totalling exactly ∣V∣|V|; were any term >1>1 the sum would exceed ∣V∣|V|. So each of the ∣V∣|V| edges weighs exactly 11, and by the definition of ww each lies in EE. Hence CC is a Hamiltonian cycle of GG, i.e. G∈UHCG\in UHC.

Cost of the reduction

TimeO(|V|²)
SpaceO(|V|²)
Output sizeO(|V|²)

This analysis needs care. The work is a pass over all of E^\hat E, writing a weight for each — Θ(∣E^∣)\Theta(|\hat E|). The temptation is to say "that's linear" — but linear in what? It is linear in the size of the new graph, whereas running time must be bounded in the size of the input.

The difficulty: we do not know how many edges GG had; conceivably very few, even none. So ∣E^∣|\hat E| cannot be bounded via ∣E∣|E|. What is certain is ∣E^∣=(∣V∣2)≤∣V∣2|\hat E|=\binom{|V|}{2}\le|V|^2, and the vertex count is unchanged by the reduction and bounded by the input size. So the running time is O(∣V∣2)=O(∣x∣2)O(|V|^2)=O(|x|^2) — quadratic in the input size.

What it buys

TSPTSP is NP-complete. And a bonus: weights valued in {1,2}\{1,2\} satisfy the triangle inequality, since for all u,x,vu,x,v we have w(u,v)≤2=1+1≤w(u,x)+w(x,v)w(u,v)\le2=1+1\le w(u,x)+w(x,v). So the very same reduction shows TSPTSP stays NP-hard in its metric version — the version that does admit an approximation algorithm (double tree, ratio 22).

Pitfalls

  • Why {1,2}\{1,2\} and not {0,1}\{0,1\}. The first natural construction proposed in class was weight 00 on original edges and 11 on new ones, with K=0K=0. That construction is entirely correct and it works — but it forfeits the bonus: with weights 00 and 11 the triangle inequality fails (1≰0+01\not\le0+0), so it proves nothing about the metric version.

  • "Linear" in output size is not "linear" in input size. This is the point the lecturer dwelt on: ∣E^∣|\hat E| cannot be bounded via ∣E∣|E|, since GG might have been nearly edgeless. The bound must go through ∣V∣|V|.

The chain

Where this was read from

  • Lesson 12משפט 8: UHC≤pTSPUHC \le_p TSP, ולכן TSPTSP היא NP-hard
  • Guided exercisehamiltonian-cycle-to-tsp
  • 2026 '1 מועד 1An algorithm returning a complete weighted graph with xx on existing edges and yy on missing ones — which claim about the reduction holds.