4.6.26 · D3Theory of Computation

Worked examples — NP-complete problems — 3-SAT, Vertex Cover, Clique, Hamiltonian Path, TSP, Subset Sum

4,712 words21 min readBack to topic

Every symbol above is now defined; nothing below uses one before this box.


The scenario matrix

Every worked example below is tagged with the cell it fills. Read this table first so you can see the whole map before we zoom into any one square. The A-cells run degenerate → yes → no → clique-side, i.e. simplest first.

Cell Reduction / Object Kind of input What it stress-tests
A0 VC ↔ IS duality on the empty graph degenerate / trivial , boundary case
A1 VC ↔ IS duality a "YES" graph complement identity
A2 VC ↔ IS duality a "NO" instance (no small cover) when the size bound is impossible
A3 Clique ↔ IS ↔ VC full trio via edge-flip clique-in- = IS-in- = VC-in-
B1 3-SAT Clique satisfiable formula one-true-literal-per-clause = clique
B2 3-SAT Clique unsatisfiable formula ( forced) why no -clique appears
C1 3-SAT Subset Sum build the number table digit-column encoding, no carries
D1 Ham-Cycle TSP graph has a cycle cheap tour = real cycle, budget
D2 Ham-Cycle TSP graph lacks a cycle why the cheapest tour exceeds
E1 Subset Sum pseudo-polynomial trap large-number instance value vs bit-length
F1 Word problem (real world) scheduling / covering reading a story into VC

Eleven cells, eleven examples (A0 is the degenerate warm-up, placed first). Every "Forecast:" is revisited in that example's Verify block so you can score your guess.


Example 0 — Cell A0 · the degenerate empty graph

Forecast: guess and for a graph with zero edges before reading on.

  1. Smallest cover of no edges. A vertex cover must touch every edge; there are none, so the empty set already covers "all" (zero) edges. . Why this step? Covering is a "for every edge…" condition — vacuously true when there are no edges.
  2. Largest independent set. needs no edge inside it; with , every vertex can join. , so . Why this step? Independence forbids internal edges, but there are none to forbid, so all vertices coexist.
  3. Test the duality at the boundary. . Why this step? We check the identity survives the degenerate extreme — cover empty, independent set full.

Verify: , , and ✔ (numeric, checked in code). Forecast check: if you guessed , correct — the empty graph is the extreme where the cover vanishes and the independent set swallows everything. ✔


Example 1 — Cell A1 · the duality on a "YES" graph

Forecast: before reading on — guess the size of the independent set you'll get, and the size of the largest clique. (Hint: .)

Figure 1 (below) shows this graph: amber dots are the cover , cyan dots are the leftover independent set .

Figure — NP-complete problems — 3-SAT, Vertex Cover, Clique, Hamiltonian Path, TSP, Subset Sum
  1. List what must be touched. Edges are . Why this step? A vertex cover is defined as "a set touching every edge", so we must first know the edge list precisely.
  2. Pick the busy vertex . sits on — three of the five edges (the amber hub in Figure 1). Why this step? High-degree vertices cover the most edges per pick; greedy start.
  3. What is left uncovered? After : edges and remain. Why this step? You cannot stop until every edge is touched, so we track the remainder.
  4. Cover and with two more vertices. Add (kills ) and (kills ). Cover , size 3. Why this step? and share no vertex, so they genuinely need two separate picks.
  5. Read the complement independent set. (the cyan dots in Figure 1). Why this step? The duality identity says the leftovers of a cover form an independent set.
  6. Read the clique side of the trio. The vertices have all three edges present — a clique of size 3. Why this step? This is the clique member of "Clique ↔ VC ↔ IS": a set that is pairwise adjacent in , completing the trio the matrix promised.

Verify:

  • Cover check: (has ) ✔, (has ) ✔, (has ) ✔, (has ) ✔, (has ) ✔. All five covered.
  • Independence check: is there an edge between and ? Scan the list — no . So is independent. ✔
  • Clique check: all present, so is a -clique. ✔
  • Arithmetic (numeric, code-checked): . ✔
  • Forecast check: independent set size , largest clique size . If you guessed those, well done. ✔

Example 2 — Cell A2 · a "NO" instance (the bound is impossible)

Forecast: guess yes or no first.

  1. A size-1 cover means one vertex touches every edge. Why this step? That's the literal definition with — one chosen vertex must sit on all five edges.
  2. Find the max degree. Degrees: has ; has ; has ; has ; has . Why this step? The best single vertex covers exactly its degree-many edges; nothing can cover more.
  3. Compare to edge count. Highest degree is (vertex ), but there are edges. One vertex covers at most . Why this step? If the best possible single pick still misses edges, no size-1 cover exists.

Verify: even the best vertex leaves and uncovered — edges orphaned; edges (numeric, code-checked). So NO, minimum vertex cover here (in fact the true minimum is , found in Ex. 1). ✔ Forecast check: the answer is NO; the degree bound rules out cleanly.


Example 3 — Cell A3 · the full Clique ↔ IS ↔ VC trio via the complement graph

Forecast: guess — clique of size 3 in a graph with only 2 edges: possible or not?

Figure 2 (below): left = (cyan edges ); right = (amber dashed edges ).

Figure — NP-complete problems — 3-SAT, Vertex Cover, Clique, Hamiltonian Path, TSP, Subset Sum
  1. Recall the identity. has a clique of size has an independent set of size . Why this step? A clique in is a set all-connected in , i.e. all-disconnected in — that is precisely an independent set of . We will now hunt in .
  2. Build . On vertices there are possible edges. uses , so has the other four: . Why this step? The complement contains exactly the non-edges of (the amber dashed lines in Figure 2).
  3. Search for an independent set of size 3 — ALL four subsets. There are exactly three-element subsets; we test each:
    • : contains → has an edge → not independent.
    • : contains → has an edge → not independent.
    • : contains → has an edge → not independent.
    • : contains → has an edge → not independent. Every one of the four fails, so no independent 3-set exists in . Why this step? Enumerating all subsets makes the "no" watertight — no case is skipped.
  4. Cross-check via the vertex cover of . By duality, an IS of size in would mean a VC of size in . But 's edges have max degree (each of ), and edges, so no single vertex covers all four — no size-1 cover, confirming no size-3 IS. Why this step? This is the VC member of the trio, giving an independent second route to the same verdict.
  5. Translate back. No independent set of size 3 in ⇒ no clique of size 3 in . Why this step? The identity from step 1 turns the result into the answer.

Verify: the largest independent set in is size (e.g. : is ? no — so is independent in , matching the clique in ). Hence max clique in : NO. All four 3-subsets contained a -edge (numeric enumeration, code-checked). ✔ Forecast check: impossible — a 3-clique needs each member joined to the other two, but 's densest structure is a single edge. ✔


Example 4 — Cell B1 · 3-SAT Clique on a satisfiable formula

Forecast: guess how many vertices the gadget has, and whether a size-3 clique exists.

Figure 3 (below): three rows = three clauses; the amber triangle is the chosen size-3 clique that gives .

Figure — NP-complete problems — 3-SAT, Vertex Cover, Clique, Hamiltonian Path, TSP, Subset Sum
  1. Make vertices. clauses × literals vertices, grouped in triples , , . Why this step? One vertex per literal-occurrence is the fixed recipe.
  2. Add edges = "compatible + different clause". Connect iff they are in different clauses AND are not a pair. Why this step? An edge means "these two literals can be true at the same time and belong to distinct clauses" — exactly what a clause-by-clause selection needs.
  3. Look for a 3-clique = one vertex per clause, all compatible. Try from , from , from (the amber triangle in Figure 3). Check pairs: (C1)–(C2) different clause, not negations ✔; (C1)–(C3) ✔; (C2)–(C3) ✔. Triangle found. Why this step? A size- clique must pick one per clause (no intra-clause edges), and mutual edges guarantee no contradiction.
  4. Read the assignment. Chosen literals ⇒ set ; is free (say ). Why this step? The clique tells us which literal makes each clause true.

Verify: with :

Formula satisfied (code-checked), matching the found 3-clique. ✔ Forecast check: vertices, and yes, a size-3 clique exists. ✔


Example 5 — Cell B2 · 3-SAT Clique on an UNsatisfiable formula

Forecast: guess — can any vertex in clause 1 connect to any vertex in clause 2?

  1. Enumerate vertices. (all the literal ), (all ). Why this step? Repeated literals still get their own vertices — the recipe is per occurrence.
  2. Try to draw any cross-clause edge. Every vertex is ; every vertex is . An edge needs "not a negation pair" — but every cross pair is vs . So zero cross edges. Why this step? Contradicting literals are exactly what the construction refuses to connect.
  3. Count possible cliques. A size-2 clique needs one vertex from each clause (intra-clause edges don't exist either, since same clause). With no cross edges, no 2-clique exists. Why this step? No edge ⇒ no clique of size .

Verify: logically requires (clause 1) and (clause 2) simultaneously — impossible (a purely logical verdict). Cross edges drawn (numeric, code-checked). Both the logic and the empty-edge gadget say UNSAT. ✔ Forecast check: no cross-clause edge can be drawn (every pair is vs ). ✔


Example 6 — Cell C1 · 3-SAT Subset Sum number table

Forecast: using the fixed layout [var | clause], guess the target's two digits.

Number var- clause- decimal value
(means , appears positively) 1 1 11
(means ) 1 0 10
slack (top-up ) 0 2 2
target 1 3 13
  1. Variable column must equal 1. Pick exactly one of . Why this step? Target digit forces "each variable chosen once" — true XOR false, never both, never neither.
  2. Clause column must equal 3. appears positively in , so a true () contributes to the clause column; the single slack contributes ; total . Max column sum here is , so no carry corrupts the variable column — the layout stays clean. Why this step? Target digit = "at least one literal satisfied (), then slack tops up to ."
  3. Assemble the subset by the fixed layout. Choose : var column , clause column , so the number is . Why this step? We read the columns in the pre-fixed order [var|clause], so the value is unambiguously , never .
  4. Check the FALSE branch is rejected — this is why slacks are capped. Choose : var column ✔ but clause column . Value . Why this step? means false, so clause is unsatisfied; the slack alone () cannot reach , so the target correctly refuses it.

Verify: ✔ (true assignment accepted); ✔ (false assignment rejected). Both numeric, code-checked. The accepting subset corresponds to satisfying . ✔ Forecast check: the target's digits are (var) and (clause), value . ✔


Example 7 — Cell D1 · Ham-Cycle TSP, graph WITH a cycle

Forecast: guess the cheapest tour cost, and whether it is .

Figure 4 (below): cyan solid edges cost (real), amber dashed diagonals cost (missing).

Figure — NP-complete problems — 3-SAT, Vertex Cover, Clique, Hamiltonian Path, TSP, Subset Sum
  1. Complete the graph. On vertices, missing edges are the two diagonals . Real edges get weight ; diagonals get weight . Why this step? TSP runs on a complete weighted graph, so we must price every pair.
  2. Set budget . Why this step? A tour is a cycle over all vertices returning home, using exactly edges. If all are real (weight 1) the cost is exactly ; that's the cheapest achievable.
  3. Trace the tour . Weights (the cyan loop in Figure 4). Why this step? This tour uses only real edges, so its cost equals the budget — the "yes" witness.

Verify: cost ✔ (numeric, code-checked), and every vertex visited exactly once then return ✔. The tour uses only weight-1 (real) edges, so it is a Hamiltonian cycle of . Any tour touching a diagonal would cost . ✔ Forecast check: cheapest tour , exactly at budget. ✔


Example 8 — Cell D2 · Ham-Cycle TSP, graph WITHOUT a cycle

Forecast: guess the minimum tour cost.

  1. Price the complete graph. Real edges (weight 1). Missing: (weight 2). Why this step? Same recipe — every non-edge costs .
  2. Any tour of 4 vertices uses 4 edges and must return to start. The path has only 3 real edges forming a line, not a loop; endpoint has only one real edge (), so its second tour-edge must be a weight-2 one. Why this step? In a cycle every vertex needs two tour-edges; cannot supply a second real one.
  3. Best case cost. Tour : ; any other ordering forces at least one weight-2 edge too, and checking all orderings confirms is the minimum. Why this step? We forced weight-2 edge; this arrangement uses exactly one, so it is optimal.

Verify: the minimum over all tour orderings is (numeric, code-checked by brute force). Since , NO tour fits the budget, hence NO Hamiltonian cycle in . ✔ Consistent: a path graph genuinely has no Hamiltonian cycle. Forecast check: cheapest tour , one above budget. ✔


Example 9 — Cell E1 · the pseudo-polynomial trap

Forecast: guess which subset works (or whether none does).

  1. Spot the big number. and . Subtract: . Why this step? The target is barely above one element; if we include it, the remainder must come from the small numbers.
  2. Try to hit remainder 7 from . All of are even, so every subset sum of them is even; is odd, so it is unreachable. Why this step? A parity check instantly rules out the "include the big number" branch.
  3. Try excluding the big number. Then we need from , whose largest possible sum is . Impossible. Why this step? We must check both branches (include / exclude the big element) to be sure.

Verify: reachable sums are (small only) and (with the big number). is in neither list (numeric, code-checked by full enumeration). NO subset sums to .Forecast check: no subset works — parity + magnitude both block it. ✔


Example 10 — Cell F1 · word problem → Vertex Cover (real-world scheduling)

Forecast: the pentagon has 5 corridors — guess the minimum number of guarded rooms.

Figure 5 (below): the pentagon of rooms; amber dots are the chosen guard posts .

Figure — NP-complete problems — 3-SAT, Vertex Cover, Clique, Hamiltonian Path, TSP, Subset Sum
  1. Translate the story into a graph. Rooms = vertices , corridors = edges , "watch every corridor" = cover every edge ⇒ this is Vertex Cover on a 5-cycle. Why this step? Recognising the abstract problem is the whole point of a reduction — the real-world story is just a costume over the graph problem.
  2. Lower bound (how few can possibly work). A 5-cycle has edges; one guard covers at most corridors (each room touches 2), so we need guards. Why this step? Total edges divided by max-edges-per-vertex gives an unavoidable minimum.
  3. Build a size-3 cover. Post guards at (amber in Figure 5): ; ; . Covered corridors: = all 5. Why this step? We match the lower bound with an explicit set, proving is optimal.

Verify: guard set touches every corridor ✔; and meets the lower bound, so no smaller set exists (numeric, code-checked). 3 guards. ✔ Its complement is an independent set of size , matching . Forecast check: the answer is , not (a 5-cycle's minimum cover is ). ✔


Recall Self-test (reveal answers)

Empty graph on 3 vertices — sizes of min cover and max independent set? ::: , ; duality . Size-1 cover on the 5-edge graph of Ex.1 — possible? ::: No; max degree edges. In the 3-SAT→Clique gadget, why no edge between and ? ::: Choosing both is a contradiction; they can't be true together. How much must a clause's slack numbers sum to, and why? ::: Exactly , so slacks alone can never satisfy an unsatisfied clause. TSP budget for Ham-Cycle reduction on vertices? ::: (a tour is a cycle using exactly edges). Why is Subset Sum's DP not polynomial? ::: is exponential in its bit-length ; it's pseudo-polynomial. Pentagon museum minimum guards? ::: 3.