TExcelTAD

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 G=(V,E)G = (V, E) is a set of vertices that are pairwise adjacent. A vertex cover is a set CC of vertices that touches every edge: for each (u,v)∈E(u, v) \in E, u∈Cu \in C or v∈Cv \in C. As decision problems:

CLIQUE={⟨G,k⟩:G has a clique of size k},VERTEX-COVER={⟨G,k⟩:G has a vertex cover of size k}.\begin{aligned} \text{CLIQUE} &= \{ \langle G, k \rangle : G \text{ has a clique of size } k \}, \\ \text{VERTEX-COVER} &= \{ \langle G, k \rangle : G \text{ has a vertex cover of size } k \}. \end{aligned}

Both are in NP: the certificate is the vertex set, checked in O(V2)O(V^2) time. Brute force tries (Vk)\binom{V}{k} sets, which is polynomial for every fixed kk but exponential when kk grows with VV (for k=V/2k = V/2 it is about 2V/V2^V/\sqrt{V}).

3-CNF-SAT reduces to CLIQUE

Let ϕ=C1∧⋯∧Ck\phi = C_1 \land \cdots \land C_k be a 3-CNF formula with clauses Cr=(ℓ1r∨ℓ2r∨ℓ3r)C_r = (\ell^r_1 \lor \ell^r_2 \lor \ell^r_3). Build a graph GG with:

  • one vertex virv^r_i for each literal occurrence ℓir\ell^r_i: 3k3k vertices, in kk groups of three;
  • an edge between virv^r_i and vjsv^s_j whenever they are in different clauses (r≠sr \ne s) and their literals are consistent, that is, ℓir\ell^r_i is not the negation of ℓjs\ell^s_j.

The instance is ⟨G,k⟩\langle G, k \rangle. The graph has O(k2)O(k^2) edges and is built in polynomial time.

Claim. ϕ\phi is satisfiable if and only if GG has a clique of size kk.

Proof

(⇒\Rightarrow) Take a satisfying assignment and pick in each clause one true literal. The kk 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 kk.

(⇐\Leftarrow) A clique of size kk 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 ϕ\phi 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 kk-clique by brute force and reads the assignment off it.

python · try it yourself
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 G‾\overline{G} of G=(V,E)G = (V, E) has the same vertices and exactly the edges that GG lacks.

Claim. GG has a clique of size kk if and only if G‾\overline{G} has a vertex cover of size ∣V∣−k\lvert V \rvert - k.

Proof. Let SS be a clique of GG. Every edge (u,v)(u, v) of G‾\overline{G} is a non-edge of GG, so uu and vv are not both in SS: at least one of them is in V∖SV \setminus S, which is therefore a vertex cover of G‾\overline{G} of size ∣V∣−k\lvert V \rvert - k. Conversely, if CC covers G‾\overline{G}, then no edge of G‾\overline{G} joins two vertices of V∖CV \setminus C, so any two vertices of V∖CV \setminus C are adjacent in GG: a clique of size ∣V∣−∣C∣\lvert V \rvert - \lvert C \rvert.

The reduction ⟨G,k⟩↦⟨G‾,∣V∣−k⟩\langle G, k \rangle \mapsto \langle \overline{G}, \lvert V \rvert - k \rangle takes O(V2)O(V^2) time, so VERTEX-COVER is NP-complete. The same argument shows that INDEPENDENT-SET (a set of pairwise non-adjacent vertices) is NP-complete: SS is independent in GG exactly when V∖SV \setminus S is a vertex cover of GG, and exactly when SS is a clique of G‾\overline{G}. The program checks both relations on random graphs.

python · try it yourself
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 GG and a minimum vertex cover in G‾\overline{G} 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 n1−ϵn^{1 - \epsilon} unless P = NP. And vertex cover is fixed-parameter tractable: a cover of size kk can be found in O(2k⋅(V+E))O(2^k \cdot (V + E)) time by branching on the two endpoints of any uncovered edge, fast whenever kk 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?

  1. All pairs
  2. Pairs in the same clause
  3. Pairs in different clauses whose literals are not negations of each other
  4. Pairs with the same variable

A 3-CNF formula has 5 clauses. What clique size does the reduction ask for?

  1. 3
  2. 5
  3. 15
  4. 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?

  1. 4
  2. 5
  3. 6
  4. 10

Why is checking every kk-subset not a polynomial algorithm for CLIQUE?

  1. Checking one subset takes exponential time
  2. (Vk)\binom{V}{k} is exponential when kk is part of the input, for example k=V/2k = V/2
  3. It misses some cliques
  4. It is polynomial; CLIQUE is in P