1. 程式人生 > 實用技巧 >NP completeness(NP完整性)(Introduction to Algorithms, 演算法導論,CLRS)學習筆記

NP completeness(NP完整性)(Introduction to Algorithms, 演算法導論,CLRS)學習筆記

NP completeness

  • Here we use binary string in our problems;
  • Call an instance of a problem language, and x ∈ { 0 , 1 } ∗ x\in\{0,1\}^* x{0,1} means the input of the language x is encoded in binary; A ( X ) = 1 / 0 A(X)=1/0 A(X)=1/0 means there is a verification algorithm A A A that accepts /rejects the language L L
    L
    .
  • x x x in a language is a certain instance(in binary string), remember!!!
  • y y y is one certificate for a instance. A ( x , y ) = 1 A(x,y)=1 A(x,y)=1( A A A is a verification algorithm running in polynomial time) means y y y is a certificate for instance x x x.

1. Definition of P P P

The language L L

L could be accept by an algorithm A A A in polynomial time.

2. Definition of NP

2.1 The H A M − C Y C L E HAM-CYCLE HAMCYCLE problem and verification

  • Verification: A A A verifies c c c is a solution to x x x.

  • HAM-CYCLE: An undirected graph G is hamiltonian if it contains a simple cycle containing every vertex of G.

  • Verifying HAM-CYCLE

2.2 Verifying a language

2.3 The complexity of NP

  • Definition: L = { x ∈ { 0 , 1 } ∗ ∣ L=\{x\in\{0,1\}^*| L={x{0,1} there is a y ∈ { 0 , 1 } ∗ y\in\{0,1\}^* y{0,1} with ∣ y ∣ = O ( ∣ x ∣ c ) |y|=O(|x|^c) y=O(xc) such that A ( x , y ) = 1 } A(x,y)=1\} A(x,y)=1}
  • If L ∈ P L\in P LP then L ∈ N P L\in NP LNP: we can easily find a verification algorithm for a P class language which runs in polynomial time.

2.4 Polynomial-time reducibility

  • L 1 ≤ P L 2 ∧ L 2 ∈ P − − > L 1 ∈ P L_1\le_{P}L_2\land L_2\in P-->L_1\in P L1PL2L2P>L1P
  • If there is a language L 2 L_2 L2 which could be solved in polynomial time, then a language L 1 L_1 L1 which is less harder than L 2 L_2 L2, could also be solved in polynomial time.
  • If there is a string x x x of yes instance in L 1 L_1 L1 and L 1 L_1 L1 is less harder than L 2 L_2 L2, and there is a polynomial computable function f ( x ) f(x) f(x), then f ( x ) f(x) f(x) is a yes instance in L 2 L_2 L2.

3. NP-complete languages

  • Language L L L is np-complete if :

    • L ∈ N P L\in NP LNP
    • L ′ ≤ P L L'\le_{P}L LPL for every L ′ ∈ N P L'\in NP LNP
  • If some language of NPC belongs to P then P = NP.

  • Proof: use reducibility

4. Circuit satisfiability

  • Given an assignment for circuit C C C, and we hope the output to be 1.
  • Show CIRCUIT-SAT is NP-complete.

4.1 Show CIRCUT-SAT ∈ \in NP

Verification algorithm A A A checking a certificate y y y can be easily made to run in polynomial time.

run A < x , y > = 1 A<x,y>=1 A<x,y>=1 in polynomial time.

4.2 Show that CIRCUIT-SAT is NP-hard

  • To prove CIRCUIT-SAT is harder than NP

    • Given any language L ∈ N P L\in NP LNP, we need to give a polynomial-time reduction from L L L to CIRCUIT-SAT.

    • In other words, we need to find a polynomial-time algorithm A A A computing a function f : { 0 , 1 } ∗ − − > { 0 , 1 } ∗ f:\{0,1\}^*-->\{0,1\}^* f:{0,1}>{0,1}, such that: x ∈ L ⇔ f ( x ) = < C ( x ) > ∈ x\in L\Leftrightarrow f(x)=<C(x)>\in xLf(x)=<C(x)> CIRCUIT-SAT.

      x x x is the string of L L L and f ( x ) f(x) f(x) is the yes instance of CIRCUIT-SAT.

    • Each y y y with ∣ y ∣ = O ( ∣ x ∣ c ) |y|=O(|x|^c) y=O(xc) defines an input to C ( x ) C(x) C(x). And A ( x , y ) = 1 A(x,y)=1 A(x,y)=1 if and only if y y y is a satisfying assignment.

    Proof:

    • There is a constant k k k such that A A A can run in O ( n k ) O(n^k) O(nk) where n = ∣ x ∣ n=|x| n=x.

    • The machine executing A A A has a certain configuration(memory, CPU and so on) at each time step.

    • When executing A A A on ( x , y ) (x,y) (x,y), the machine goes through a series of configuration c 0 , . . . , c T ( n ) c_0,...,c_{T(n)} c0,...,cT(n), where T ( n ) T(n) T(n) is the time for A ( x , y ) A(x,y) A(x,y).

    • Each time step, the machine take some input and update the configuration to give input to the next time step.

    • The reduction algorithm f f f constructs the combination of circuit(paste them together), that computes all configurations produced by a given initial configuration.

    • What reduction algorithm f f f do: computes C = f ( x ) C=f(x) C=f(x) that is satisfiable if and only if there exists a certificate y y y such that A ( x , y ) = 1 A(x,y)=1 A(x,y)=1.

    Core part

    • When f f f obtains an input x x x, it first computes n = ∣ x ∣ n=|x| n=x and constructs a combination circuit C ′ C' C consisting of T ( n ) T(n) T(n) copies of M M M.
    • Algorithm f f f modifies circuit C ′ C' C slightly to construct the circuit C = f ( x ) C=f(x) C=f(x). It wires the input x x x and set up the configuration, and the remaining input is the certificate y y y.
    • The machine would ignore all outputs expect the last one c T ( n ) c_{T(n)} cT(n).
    • Prove two properties:
      • F F F correctly computes a reduction function f f f. Suppose a certificate y y y of length O ( n k ) O(n^k) O(nk) such that A ( x , y ) = 1 A(x,y)=1 A(x,y)=1, and if y y y exists C C C is satisfiable.
      • F F F runs in polynomial time. To represent the configuration, it takes n = ∣ x ∣ n=|x| n=x bits. The length of certificate y y y: O ( n k ) O(n^k) O(nk). The combination circuit M M M has size polynomial in the length of a configuration, O ( n k ) O(n^k) O(nk), the size of M M M is polynomial in n n n. Thus, the circuit C C C consists of at most t = O ( n k ) t=O(n^k) t=O(nk) copies of M M M, which allows the reduction algorithm F F F to construct C C C in polynomial time.
      • Thus, CIRCUIT-SAT is at least as hard as any language in NP.

5. The SAT problem

  • A boolean formula ϕ \phi ϕ consists of boolean variables and boolean connectives and parentheses(and).
  • A satisfiable assignment: ϕ = 1 \phi=1 ϕ=1;
  • To show SAT ∈ \in NPC:
    • Show that SAT ∈ \in NP;
    • Show that CIRCUIT-SAT ≤ P \le_P P SAT.

5.1 Show SAT ∈ \in NP.

  • Construct a verification algorithm A A A taking inputs x x x and y y y;
  • x x x is the boolean formula ϕ \phi ϕ and y y y is the certificate;
  • A A A would return 1 or 0 and we can easily make it run in polynomial time;
  • SAT ∈ \in NP.

5.2 Show CIRCUIT-SAT ≤ p \le_p p SAT

Given a circuit C C C, we can transfer it into ϕ \phi ϕ:

  • We can construct sub-formulas according to each gate in C C C in ϕ \phi ϕ
  • Assign variables in ϕ \phi ϕ with the value corresponding to that in C C C
  • Combine these sub-formulas with ∧ \land .
  • ϕ \phi ϕ can be constructed in polynomial time;
  • C C C is satisfiable if and only if ϕ \phi ϕ is satisfiable:
    • < C > ∈ <C>\in <C> CIRCUIT-SAT ⇔ < ϕ > ∈ \Leftrightarrow<\phi> \in <ϕ> SAT.

6. 3-CNF formulas

A set of boolean variable, each clause is the OR of exactly 3 distinct literals, example:

Poof:

  • 3-CNF-SAT ∈ \in NP;
  • SAT ≤ P \le_P P 3-CNF-SAT.

7. How to prove a problem ∈ \in NPC

General idea:

  • Prove a problem L ∈ L\in L NP;

  • Prove L ′ ≤ P L L'\le_P L LPL, where L ′ L' L is NPC.

subset-sum

Given a set S S S of positive integers and given integer target t > 0 t>0 t>0.

Is there a subset S ′ S' S of S summing to t t t.