Sub topic 12
Worked Reduction: Independent Set to Vertex Cover
This chapter works through one reduction from start to finish. It assumes you have read the earlier chapters on polynomial-time many-one reductions, NP-hardness, and NP-completeness. It also assumes you can read Python and know basic graph terms such as vertex, edge, and adjacency. Throughout, graphs are undirected and simple, meaning they have no self-loops and no repeated edges.
A reduction from problem A to problem B is a polynomial-time function that turns any instance of A into an instance of B with the same yes/no answer. The goal here is to build one, prove it correct, and see exactly what it tells us about hardness.
The Two Problems
Both problems ask about a set of vertices in a graph G = (V, E) with n = |V| vertices.
| Problem | Instance | Question |
|---|---|---|
| Independent Set | Graph G, integer k | Is there a set S of at least k vertices such that no edge has both endpoints in S? |
| Vertex Cover | Graph G, integer k | Is there a set C of at most k vertices such that every edge has at least one endpoint in C? |
The two problems point in opposite directions. Independent Set wants a large set that avoids edges. Vertex Cover wants a small set that touches every edge. This difference is what makes the reduction work.
The Key Lemma: Complements
Lemma. For any graph G = (V, E) and any S ⊆ V, S is an independent set if and only if V \ S is a vertex cover.
Proof (⇒). Suppose S is independent. Take any edge (u, v). Both endpoints cannot be in S, so at least one of them is in V \ S. Then V \ S covers that edge. Since the edge was arbitrary, V \ S is a vertex cover.
Proof (⇐). Suppose V \ S is a vertex cover. Take any edge (u, v). At least one endpoint lies in V \ S, so at most one lies in S. No edge has both endpoints in S, so S is independent. ∎
Sizes follow directly: if |S| ≥ k then |V \ S| ≤ n − k, and the reverse holds too.
The Reduction
The reduction keeps the graph unchanged and changes only the number:
f(G, k) = (G, n − k)
Correctness, yes direction. If G has an independent set S with |S| ≥ k, the lemma says V \ S is a vertex cover of size n − |S| ≤ n − k. So (G, n − k) is a yes-instance of Vertex Cover.
Correctness, no direction. Here we argue by contrapositive. If (G, n − k) is a yes-instance of Vertex Cover, some cover C has |C| ≤ n − k. Then V \ C is independent and has size at least k, so (G, k) was a yes-instance of Independent Set.
Running time. Computing n − k means counting vertices and doing one subtraction. Copying the graph takes O(n + |E|) time. Both are polynomial in the input size.
Checking It in Code
The script below implements the reduction. It also includes brute-force deciders for both problems, which take exponential time and are only there for testing. It checks that the answer is preserved on several small graphs for every budget, including out-of-range budgets.
from itertools import combinations
def is_vertex_cover(edges, cover):
cover = set(cover)
return all(u in cover or v in cover for u, v in edges)
def is_independent_set(edges, subset):
subset = set(subset)
return not any(u in subset and v in subset for u, v in edges)
def independent_set_to_vertex_cover(vertices, edges, k):
"""Maps an Independent Set instance (G, k) to a Vertex Cover instance (G, n - k)."""
return vertices, edges, len(vertices) - k
def has_independent_set(vertices, edges, k):
"""Brute force: is there an independent set of size at least k?"""
for r in range(max(k, 0), len(vertices) + 1):
for combo in combinations(vertices, r):
if is_independent_set(edges, combo):
return True
return False
def has_vertex_cover(vertices, edges, k):
"""Brute force: is there a vertex cover of size at most k?"""
for r in range(0, min(k, len(vertices)) + 1):
for combo in combinations(vertices, r):
if is_vertex_cover(edges, combo):
return True
return False
graphs = {
"path": ([1, 2, 3, 4, 5], [(1, 2), (2, 3), (3, 4), (4, 5)]),
"triangle": (["a", "b", "c"], [("a", "b"), ("b", "c"), ("a", "c")]),
"star": ([0, 1, 2, 3], [(0, 1), (0, 2), (0, 3)]),
"no_edges": ([1, 2, 3], []),
}
for name, (vertices, edges) in graphs.items():
for k in range(-1, len(vertices) + 2):
original = has_independent_set(vertices, edges, k)
mapped = has_vertex_cover(*independent_set_to_vertex_cover(vertices, edges, k))
assert original == mapped, (name, k)
print(f"{name}: answers preserved for every k")The reduction function never searches for a set. It only rewrites the instance. A reduction that solved the problem along the way would usually take exponential time and so would not count.
Edge Cases
- k > n. No independent set can have more than n vertices, so the answer is no. The mapped budget n − k is negative, and no cover has negative size, so the answer is still no. This works because the problems use "at least k" and "at most k". If your textbook writes Vertex Cover with "exactly k", you need to handle out-of-range k separately, for example by mapping to a fixed no-instance.
- k ≤ 0. The empty set is independent, so the answer is yes. The mapped budget is at least n, and V itself is always a cover, so the answer is again yes.
- Graphs with no edges. Every subset is independent and the empty set is a cover. The test above includes this case.
- Isolated vertices. They always belong in a maximum independent set and never need to be in a minimum cover, which matches the complement relationship.
Which Way Hardness Flows
The reduction shows Independent Set ≤ₚ Vertex Cover. Hardness moves along the arrow toward the target problem. Independent Set is known to be NP-complete (via 3-SAT, as covered in the tour of classic problems). So every NP problem reduces to Independent Set, and composing with f shows every NP problem reduces to Vertex Cover. That makes Vertex Cover NP-hard.
To conclude that Vertex Cover is NP-complete, you also need Vertex Cover ∈ NP. The certificate is the cover C. A verifier checks |C| ≤ k and runs is_vertex_cover, which takes O(|E|) set lookups.
The same map, (G, k) ↦ (G, n − k), also reduces Vertex Cover to Independent Set, so the two problems are polynomially equivalent. Most reductions only go one way. This one is unusually clean.
Exercise
A Clique in G is a set of vertices that are all pairwise adjacent. Give a polynomial-time reduction from Independent Set to Clique and state why it is correct.
Show answer
Map (G, k) to (G', k), where G' is the complement graph: same vertices, with an edge between u and v exactly when they are not adjacent in G. A set S has no edges inside it in G exactly when every pair in S is adjacent in G', so S is independent in G if and only if S is a clique in G'. Building G' takes O(n^2) time.
Exercise
A student shows Vertex Cover ≤ₚ Independent Set using the map (G, k) ↦ (G, n − k) and concludes that Independent Set is NP-hard. What else must already be known for this argument to be valid?
Show answer
Vertex Cover must already be known to be NP-hard, for example from a reduction out of 3-SAT. A reduction moves hardness from the source problem to the target problem, so the source's hardness has to be established first. Otherwise the argument is circular if Vertex Cover's hardness was itself derived from Independent Set.
Exercise
In the 5-vertex path 1-2-3-4-5, what does the reduction output for the Independent Set instance (G, 3), and what certificate witnesses the answer?
Show answer
It outputs (G, 2). The answer is yes. The independent set {1, 3, 5} has size 3, and its complement {2, 4} is a vertex cover of size 2.