34 NP-Completeness

Document last modified: 

Nondeterministic algorithm

Nondeterministic algorithms have two phases:

  1. Nondeterministic guessing (think of as some random process) phase, each time the algorithm is run the resulting guess may differ.
     
  2. Deterministic verification phase, checks that the guess is a solution, returning true or false, or looping infinitely.

Example: Nondeterministic graph coloring

Use 4 colors to color 5 vertices with all adjacent vertices a different color.

Guesses (vertex order 1-5):

 

The First NP-Complete Problem

Circuit-Satisfiability - given a Boolean circuit constructed from AND, OR and NOT gates, determine whether there is any set of Boolean inputs to the circuit that causes an output of 1.

Solution - See truth table at bottom right.

Question - To develop some intuition on determinism and NP-completeness, do the following on Figure (b) below:

  1. How would you solve for one output solution for x1, x2 and x3?
     
  2. Try to guess and verify one solution for x1, x2 and x3 other than the blue solution to (a). Your guess is nondeterministic but the verification is deterministic.
     
  3. What is the complexity in verifying a given input produces an output of 1? Consider the number of operations (gates) that must be evaluated.
     
  4. What is the worst-case complexity in guessing the input to produce an output of 1?
 

Figure 34.8 (a) and (b) from Cormen

Circuit (b) = ((¬x3∧ x1∧ x2)∨¬¬x3)∧ ((x1∨x2)∧¬¬x3)∧ (¬x3∧ x1∧ x2)

x1 x2 x3 Output
0 0 0 ?
0 0 1 ?
0 1 0 ?
0 1 1 ?
1 0 0 ?
1 0 1 ?
1 1 0 1
1 1 1 ?

Verifying

Given assignments to x1, x2, and x3, verification can easily be done in O(nk) or polynomial time.

if ((¬x3∧ x1∧ x2)∨¬¬x3)∧ ((x1∨x2)∧¬¬x3)∧ (¬x3∧ x1∧ x2) then print 1 else print 0

In (b) there are 3 variables and 15 operations, verification requires at most 15 operations or less than 33 = 27.

Solving is O(2n) since may need to guess every possible input.

 

NP-completeness and the class P and NP

class P - Polynomial or O(nk) for k a constant

Consists of those problems that are solvable in polynomial time by a deterministic algorithm.

Its worst case efficiency is O(p(n)) where p(n) is a polynomial.

Note that for O notation, problems solvable in logarithmic or linear time are polynomial.

Example: O(n2) sort.

class NP - Nondeterministic Polynomial

Consists of those problems that are verifiable in polynomial time.

If given a solution, could verify the solution is correct in time polynomial based on the size of the input to the problem.

Example: We just demonstrated graph coloring can be verified correct in polynomial time.

Example: Hamiltonian Cycle Problem - Find a cycle in a graph visiting each vertex exactly once.

Example at right: 2 → 3 → 4 → 5 → 1

Given: an undirected graph G = (V, E)

Certificate would be a sequence of all |V| vertices <v1, v2, ..., v|V|>

To verify:

  • check that each edge in the sequence (vi, vi+1) ∈ E(G), ∀ i: 1 ≤ i ≤ |V| - 1
  • and the edge (v|V|, v1) ∈ E(G).

Verification can be done in polynomial time, Ω(|V|)

P | NP

class NPC - NP Complete

Consists of problems in NP and are as hard as any problem in NP

If any problem in NPC can be solved in polynomial time, then all problems in NPC can be solved in polynomial time.

 

Abstract Problems

abstract problem Q - a binary relation that associates each instance with a solution

Let set I be a set of problem instances

Let set S be a set of problem solutions

Then Q is a binary relation on I and S, Q | I x S

I x S can have empty or non-unique solutions

Example

A Path problem instance is the input: a triple (G, u, v), where G is a directed graph and u and v are vertices in the graph.

At right: G =( V, E ), u=s, v=x.

 

Path's I - is the set of all problem instances of the Path type. 

I = {i1, i2, ... }, where each ij is of the form (G, u, v).

(G, s, x) represents one instance of I.

 

Path's S - is the set of all Path solutions.  S = {s1, s2, ... }, where each sj is of the form <v1, v2,  ..., sn >, a sequence of vertices in the graph, or <|> if no path exists.

For the graph, one instance of S is the path <s, y, z, x>

For the graph, one instance not of S is the path <x, s>

 

The Path Abstract Problem (PAP):

abstract decision problem D - a binary relation that associates each problem instance with a yes/no solution

Let I be a set of decision problem instances

Let S be the set {0, 1}

Then D is a binary relation on I and S, D | I X S and every tuple in D has the form (ij, sk) with sk = either 0 or 1

Example

The Path abstract decision problem is a four-tuple (G, u, v, k), where G is a graph and u and v are vertices in the graph, and k is the upper limit on the number of edges in the path from u to v.

Path's I - is the set of all problem instances of the Path type.  I = {i1, i2, ... }, where each ij is of the form (G, u, v, k

Path's S - is the set of all Path solutions.  S = {0, 1}

The Path Abstract Decision Problem (PADP):

Summary

To develop a method for showing a problem is NP-complete, we need a general representation of problems (abstract) and that have a yes/no answer (decision).

 

Algorithms that verify membership in a language

x - a problem instance (input string)

y - a certificate (solution string)

A - an algorithm, verifies x if there exists a certificate y such that A(x, y) = 1

Language verified by A is:

L = {x ∈ {0, 1}* : ∃ certificate y ∈ {0, 1}* such that A(x, y) = 1}

{0, 1}* is a binary encoding, e.g. 100000012 = 'A'

Algorithm A verifies language L if for any string x ∈ L, there is a certificate y that A can use to prove that x ∈ L.

For any string, x ⊆ L, there is no certificate proving that x ∈ L.

 

Example: Hamiltonian graph cycle (HAM-CYCLE) is a simple cycle that contains each vertex.

The certificate is the list of vertices in the Hamiltonian graph cycle, enough information for verification

(e.g. vertices <1, 2, 3, 4, 5>)

If not Hamiltonian, there is no list that can fool the verification algorithm.

Algorithm A would not be fooled by <2, 1, 3, 4, 5>

L is just those input strings of vertices which are a Hamiltonian cycle.

{<1, 2, 3, 4, 5>, <2, 3, 4, 5, 1>, ..., <5, 4, 3, 2, 1>, ...}

Example:

For one instance of HAM-CYCLEL where:

V= {1, 2, 3, 4, 5}

E = {(1,2), (2,1), (1,5), (5,1),...}

G = (V,  E)

AHAM-CYCLE( G, <1, 2, 3, 4, 5>) = 1

AHAM-CYCLE( G, <1, 2>)  ¹ 1

 

 

Complexity class NP - Nondeterministic Polynomial time

A class of languages that can be verified by a polynomial-time algorithm.

L ∈ NP iff ∃ a 2-input polynomial-time algorithm A and constant c such that:

L = {x ∈ {0, 1}* : ∃ a certificate y with |y| = O( |x|c ) such that A(x, y) = 1}

The upper bound of the solution length, |y|, is polynomial

x is the instance (input) and y is the solution to be verified

Algorithm A verifies L in polynomial time

Example: Proving PATHL ∈ NP

PATH problem:

xi = (G, u, v, k), where

  • G is the graph,
  • u is the source vertex,
  • v is the target vertex,
  • k is an integer specifying the maximum allowable length of a path from u to v.

Given PATH problem instance:

x1 = (G, r, y, 5),

certificate p = < r, s, w, x, y >

use APATH (x1, p) to verify that path p's length is k

APATH (x, p) verifies p is a path in x's G of length k or less in polynomial time by:

APATH(x, p) verifies PATHL as follows:

  1. Verify x is a proper encoding of <G, u, v, k>
  2. Verify each v ∈ p is unique, i.e. p is a set.
  3. Verifies first vertex in list is: u ∈ x.
  4. Verifies last vertex in list is: v ∈ x.
  5. Verify length[p] k
  6. for i ← 1 to length[p]-1 do verify (vi, vi+1) ∈ G.E
  7. If steps 1-6 verify, APATH (x, p) = 1

Therefore PATHL ∈ NP, since APATH(x, p) can produce either a 0 or a 1 in polynomial time.

But we already knew that PATH ∈ P, and P ∈ NP, i.e., anything that can be computed in polynomial time can be verified in polynomial time.

 

Reducibility - tool for proving languages belong to class P

Problem Q can be reduced to problem Q' if any instance of Q can be easily rephrased (i.e. polynomial time) as an instance of Q'.

Example:   10002*00112 = 8*3     solving binary multiplication is no harder than decimal multiplication.

The solution to Q' provides a solution to Q.

L1p L2

Language L1 is polynomial time reducible to language L2 if there exists a polynomial time function:

f : {0, 1}* → {0, 1}* such that  "x ∈ {0, 1}*

x ∈ {0, 1}* iff f( x ) ∈ L2
 

f is the reduction function, converting an instance x for A1 to an instance f( x) for A2.

F is the reduction algorithm, the polynomial time algorithm that computes f.

For decision problem represented by L1

for any x ∈ L1, f( x ) ∈ L2

F computes reduction function f.

A2 decides L2 in polynomial time.

A1 algorithm decides x ∈ L1 using F to transform x into f( x ), then using A2 to decide if f( x ) ∈ L2

If f( x ) ∈ L2 then x ∈ L1

 

NP-completeness

Language L ⊆ {0, 1}* is NP-complete if:
  1. L ∈ NP      (meaning L is polynomial time verifiable)
     
  2. L' ≤p L for every L' ∈ NP  (meaning all L' are polynomial time reducible to L)

NP-hard - Language L that satisfies property 2 only (satisfying property 1 means L is polynomial time verifiable, NP-hard means it is not polynomial time verifiable).

Prove that NP-complete class has property that if any NP-complete problem can be solved in polynomial time, every NP problem can be solved in polynomial time.

Theorem 34.4

Equivalent statements:

  1. If any NP-complete problem is polynomial time solvable, then P=NP (all NP problems can be solved in polynomial time).
     
  2. If any NP problem is not polynomial time solvable, then no NP-complete problem is polynomial time solvable .

Proof

Let L ∈ P and also that L ∈ NPC (the class of NP-complete languages).

For any L' ∈ NP, by property 2 of NP definition, L' ≤p L.

By Lemma 34.3, have L' ∈ P, proving the first statement.

The second statement is the contrapositive of the first (i.e. the contrapositive of p → q is ~q→ ~p).

 

A First NP-Complete problem

NP-completeness of circuit-satisfiability relies on direct proof that L ≤p CIRCUIT-SAT for every language L ∈ NP.

CIRCUIT-SAT provides the known NP-complete problem to prove a different problem NP-complete.

 

A language L ⊆ {0, 1}* is NPC if:

  1. L ∈ NP and
     

  2. L' p L, ∀ L' ∈ NP

Seems must show all L' ∈ NP have to be reduced to L to show L ∈ NPC. 

Not so.

Transitivity, by reducing a known NP-complete language L' to L, implicitly reduces every language in NP to L.

NP-hard

A language L that satisfies L' ≤p L, ∀ L' ∈ NP (means every language in NP can be reduced in polynomial time to L).

Lemma 34.8

L is a language such that L' p L for some L' ∈ NPC

  • L is NP-hard.

  • If L ∈ NP then L ∈ NPC.

Proof

L'' p L', ∀ L'' ∈ NP                     property 2 in NPC definition L' ∈ NPC

L is NP-hard                               By transitivity, L'' p L'p L, so L' ≤p L, ∀ L' ∈ NP.
                                                 Satisfies property 2 of NPC.

L ∈ NPC                                     If L ∈ NP then satisfies property 1 of NPC definition.

Means that by reducing a known NP-complete language L' to L, we implicitly can reduce every language in NP to L.

 

The General Method for proving L ∈ NPC

  1. Prove L ∈ NP by showing that a solution can be verified in polynomial time.
     

  2. Select a known NPC language L' (e.g. CIRCUIT-SAT)
     

  3. Describe an algorithm F that computes a function f that maps every instance of x ∈ {0, 1}* of L' to an instance f(x) ∈ L
     

  4. Prove that the function f satisfies x ∈ L' iff f(x) ∈ L, ∀ x ∈ {0, 1}*
     

  5. Prove that F runs in polynomial time (F is the algorithm that computes the function f)

Example - Prove Formula satisfiability is NPC

SAT = { <φ>: φ is an instance of a Boolean formula}

φ is composed of:

Example Formula: φ = ((x1 → x2) v ¬((¬x1 ↔ x3) v x4)) ^ ¬x2

n = 4, m = 8

Overall Steps for Showing SAT ∈ NPC

  1. Show SAT ∈ NP

    ASAT(φ, y) = 1            Verify certificate y as solution to φ

    ASAT runs in polynomial time by just replacing each xi with its corresponding value from y and evaluating expression. Simulate φ to verify certificate.

  2. Show CIRCUIT-SAT p SAT

    1. Select a known NPC language L' (i.e. choose CIRCUIT-SAT)
       

    2. Describe an algorithm F that computes a function f, mapping every instance of x ∈ {0, 1}* of CIRCUIT-SAT to an instance f(x) ∈ SAT

      See below.

    3. Prove that the function f satisfies: x ∈ CIRCUIT-SAT iff  f(x) ∈ SAT, ∀ x ∈ {0, 1}*
       

    4. Prove that F computes f in polynomial time

CIRCUIT-SAT p SAT has been shown.

 

SAT ∈ NP and CIRCUIT-SAT p SAT

Hence SAT ∈ NPC

By Lemma 34.8, if L is a language such that L' p L for some L' ∈ NPC, then L is NP-hard. Also, if L ∈ NP then L ∈ NPC.

That is: if SAT is a language such that CIRCUIT-SAT p SAT for CIRCUIT-SAT ∈ NPC, then SAT is NP-hard. Also, if SAT ∈ NP then SAT ∈ NPC.

 

What is the function f ?
  • Convert the circuit to a φ

    Example: φ = (x1^x2^x3 v x3)^(x1^x2^x3)^(x1 v x2 v x3)

  • Label each wire in the circuit x1, x2, x3, ..., xn, these become φ's Boolean variables.
     
  • Write a formula for each gate expressed in terms of all the wires incident on that gate.
    For example:

       (x4 ↔ ¬x3)    i.e., x4 is true iff ¬x3 is true

Figure 34-10 from Cormen

 

What This Reduction Shows - CIRCUIT-SAT p SAT

Already shown SAT ∈ NP

Reduction shows CIRCUIT-SAT p SAT

Hence SAT ∈ NPC

So we can construct a machine as follows:
  • A1 is CIRCUIT-SAT ∈ NPC
  • A2 is SAT
  • F is the polynomial-time reduction algorithm that computes the function f
  • x is a problem instance of CIRCUIT-SAT
  • f(x) is a problem instance of SAT

Figure 34.5 from Cormen