Clique and Vertex Cover
The first NP-complete problems were logical. The next two are about graphs, and the reduction that connects them to logic is the model for many later ones: build a graph whose structure mirrors the formula, so that a large clique is a satisfying assignment. Then a second, almost effortless reduction through the complement graph shows that vertex cover is NP-complete too.
The problems
A clique in an undirected graph is a set of vertices that are pairwise adjacent. A vertex cover is a set of vertices that touches every edge: for each , or . As decision problems:
Both are in NP: the certificate is the vertex set, checked in time. Brute force tries sets, which is polynomial for every fixed but exponential when grows with (for it is about ).
3-CNF-SAT reduces to CLIQUE
Let be a 3-CNF formula with clauses . Build a graph with:
- one vertex for each literal occurrence : vertices, in groups of three;
- an edge between and whenever they are in different clauses () and their literals are consistent, that is, is not the negation of .
The instance is . The graph has edges and is built in polynomial time.
Claim. is satisfiable if and only if has a clique of size .
Proof
() Take a satisfying assignment and pick in each clause one true literal. The chosen vertices lie in different clauses, and no two of them are complementary (both are true), so all pairs are adjacent: a clique of size .
() A clique of size has at most one vertex per clause (vertices in the same clause are not adjacent), so exactly one in each. Its literals are pairwise consistent, so we can set each of them true without contradiction (other variables arbitrarily). Then every clause has a true literal and is satisfied.
So CLIQUE is NP-complete. Notice the shape: one gadget (a group of three vertices) per clause, and edges that encode the global consistency constraint. The program builds the graph for a small formula, finds a -clique by brute force and reads the assignment off it.
from itertools import combinations
# literals: 1 = x1, -1 = not x1, ...
phi = [[1, -2, 3], [-1, 2, 3], [1, 2, -3], [-1, -2, -3]]
k = len(phi)
vertices = [(r, lit) for r, clause in enumerate(phi) for lit in clause]
edges = {frozenset((u, v)) for u, v in combinations(vertices, 2) if u[0] != v[0] and u[1] != -v[1]}
print(f"{len(vertices)} vertices, {len(edges)} edges, looking for a clique of size {k}")
def is_clique(S):
return all(frozenset((u, v)) in edges for u, v in combinations(S, 2))
cliques = [S for S in combinations(vertices, k) if is_clique(S)]
print(f"number of {k}-cliques:", len(cliques))
S = cliques[0]
assignment = {abs(lit): lit > 0 for _, lit in S}
print("first clique:", S)
print("assignment from it:", assignment,
" satisfies phi:", all(any(assignment.get(abs(l), False) == (l > 0) for l in c) for c in phi))12 vertices, 42 edges, looking for a clique of size 4
number of 4-cliques: 12
first clique: ((0, 1), (1, 2), (2, 1), (3, -3))
assignment from it: {1: True, 2: True, 3: False} satisfies phi: True
(Variables that the clique does not mention can be set either way; here every variable is mentioned.)
Vertex cover through the complement
The complement of has the same vertices and exactly the edges that lacks.
Claim. has a clique of size if and only if has a vertex cover of size .
Proof. Let be a clique of . Every edge of is a non-edge of , so and are not both in : at least one of them is in , which is therefore a vertex cover of of size . Conversely, if covers , then no edge of joins two vertices of , so any two vertices of are adjacent in : a clique of size .
The reduction takes time, so VERTEX-COVER is NP-complete. The same argument shows that INDEPENDENT-SET (a set of pairwise non-adjacent vertices) is NP-complete: is independent in exactly when is a vertex cover of , and exactly when is a clique of . The program checks both relations on random graphs.
import random
from itertools import combinations
def max_clique(n, E):
for k in range(n, 0, -1):
if any(all(frozenset(p) in E for p in combinations(S, 2)) for S in combinations(range(n), k)):
return k
return 0
def min_cover(n, E):
for k in range(n + 1):
if any(all(set(e) & set(C) for e in E) for C in combinations(range(n), k)):
return k
rng = random.Random(21)
print(" n edges max clique of G min cover of complement n - clique")
for trial in range(6):
n = rng.randint(5, 9)
E = {frozenset(p) for p in combinations(range(n), 2) if rng.random() < 0.5}
Ec = {frozenset(p) for p in combinations(range(n), 2)} - E
w, c = max_clique(n, E), min_cover(n, Ec)
print(f"{n:2} {len(E):6} {w:16} {c:24} {n - w:11}") n edges max clique of G min cover of complement n - clique
6 8 3 3 3
6 5 3 3 3
5 7 3 2 2
9 17 4 5 5
8 15 3 5 5
6 5 3 3 3
The last two columns agree on every graph: a maximum clique in and a minimum vertex cover in are complements of each other.
Where these problems appear
Cliques model groups of mutually compatible items: communities in social networks, sets of jobs that may run together, consistent hypotheses. Vertex cover models placing guards, cameras or monitors so that every link is watched. Vertex cover is also the standard example of two ways around NP-hardness. It has a simple 2-approximation, shown in Coping with NP-Hardness, while maximum clique cannot be approximated within unless P = NP. And vertex cover is fixed-parameter tractable: a cover of size can be found in time by branching on the two endpoints of any uncovered edge, fast whenever is small, a theme of Parameterized & Fine-Grained Complexity.
Test yourself
In the reduction from 3-CNF-SAT to CLIQUE, which pairs of literal vertices are joined by an edge?
- All pairs
- Pairs in the same clause
- Pairs in different clauses whose literals are not negations of each other
- Pairs with the same variable
A 3-CNF formula has 5 clauses. What clique size does the reduction ask for?
- 3
- 5
- 15
- It depends on the number of variables
A graph on 10 vertices has a maximum clique of size 4. What is the minimum vertex cover of its complement?
- 4
- 5
- 6
- 10
Why is checking every -subset not a polynomial algorithm for CLIQUE?
- Checking one subset takes exponential time
- is exponential when is part of the input, for example
- It misses some cliques
- It is polynomial; CLIQUE is in P