Chap07 Handout4
Chap07 Handout4
Chapter 7
Time Complexity
CS 341: Foundations of CS II
Contents
• Time and space as resources
• Big O/little o notation, asymptotics
Marvin K. Nakayama • Time complexity
Computer Science Department
• Polynomial time (P)
New Jersey Institute of Technology
Newark, NJ 07102 • Nondeterministic polynomial time (NP)
• NP-completeness
• Complexity theory tries to answer this. • We will only examine time complexity in this course.
• Definition: The running time or time complexity of M is a 6n3 + 2n2 + 20n + 45 has four terms.
function f : N → N defined by the maximization: 6n3 most important when n is large.
f (n) = max ( number of time steps of M on input x ) Leading coefficient “6” does not depend on n, so only focus on n3.
|x|=n
• Terminology
f (n) is the running time of M .
M is an f (n)-time Turing machine.
CS 341: Chapter 7 7-9 CS 341: Chapter 7 7-10
Asymptotic Notation Some big-O examples
• Consider functions f and g , where • Example 1: Show f (n) = O(g(n)) for
1 3
f, g : N → R+ f (n) = 15n2 + 7n, g(n) =
n .
2
Let n0 = 16 and c = 2, so we have ∀ n ≥ n0:
• Definition: We say that
1
f (n) = O(g(n)) f (n) = 15n2 + 7n ≤ 16n2 ≤ n3 = 2 · n3 = c · g(n).
2
if there are two positive constants c and n0 such that For first ≤, if 7 ≤ n, then 7n ≤ n2 by multiplying both sides by n.
f (n) ≤ c · g(n) for all n ≥ n0. For second ≤, if 16 ≤ n, then 16n2 ≤ n3 (mult. by n2).
• We say that: • Example 2: 5n4 + 27n = O(n4).
“g(n) is an asymptotic upper bound on f (n).” Take n0 = 1 and c = 32. (Also n0 = 3 and c = 6 works.)
“f (n) is big-O of g(n).” But 5n4 + 27n is not O(n3): no values for c and n0 work.
• Basic idea: ignore constant factor differences:
2n3 + 52n2 + 829n + 2193 = O(n3).
2 = O(1) and sin(n) + 3 = O(1).
• Exponential fcns like 2n always eventually “overpower” polynomials. logb n = logb(2log2 n) = (log2 n)(logb 2)
For all constants a and k, polynomial f (n) = a · nk + · · · obeys: Changing base b changes value by only constant factor.
f (n) = O(2n). So when we say f (n) = O(log n), the base is unimportant.
• Note that log n = O(n).
For functions in n, we have
• In fact, log n = O(nd) for any d > 0.
nk = O(bn)
Polynomials overpower logarithms,
for all positive constants k, and b > 1. just like exponentials overpower polynomials.
• Thus, n log n = O(n2).
CS 341: Chapter 7 7-13 CS 341: Chapter 7 7-14
Big-O Properties More Remarks
• O(n2) + O(n) = O(n2) and O(n2)O(n) = O(n3)
• Definition:
• Sometimes we have A bound of nc, where c > 0 is a constant, is called polynomial.
f (n) = 2O(n). δ
A bound of 2(n ), where δ > 0 is a constant, is called exponential.
What does this mean?
Answer: f (n) has an asymptotic upper bound of 2cn for some • f (n) = O(f (n)) for all functions f .
constant c. • [log(n)]k = O(n) for all constants k.
• nk = O(2n) for all constants k.
• What does f (n) = 2O(log n) mean?
• Because n = 2log2 n, n is an exponential function of log n.
Recall the identities:
n = 2log2 n, • If f (n) and g(m) are polynomials, then g(f (n)) is polynomial in n.
nc = 2c log2 n = 2O(log2 n). Example: If f (n) = n2 and g(m) = m3, then
Thus, 2O(log n) means an upper bound of nc for some constant c. g(f (n)) = g(n2) = (n2)3 = n6.
Example: If • Simplify!
M1 = “On input string w ∈ {0, 1}∗: 1. Scan across tape and reject if 0 is found to the right of a 1.
1. Scan across tape and reject if 0 is found to the right of a 1.
2. Repeat the following if both 0s and 1s appear on tape: 0 0 0 1 1 1 . . .
• Scan across tape, crossing off single 0 and single 1.
Analysis:
3. If no 0s or 1s remain, accept;
otherwise, reject.” • Input string w is of length n.
• Scanning requires n steps.
Let’s now analyze M1’s run-time complexity. • Repositioning head back to beginning of tape requires n steps.
• We will examine each stage separately. • Total is 2n = O(n) steps.
• Suppose input string w is of length n.
0 0 0 1 1 1 . . .
0 0 0 1 1 1 . . .
0 0 0 1 1 1 . . .
Analysis:
After fourth pass, none remaining. • Result is 1011, which is reverse of binary representation of 13.
0 0 0 0 0 0 0 0 0 0 0 0 0
• Each pass checks one binary digit.
CS 341: Chapter 7 7-25 CS 341: Chapter 7 7-26
M2 = “On input string w ∈ {0, 1}∗: 2-Tape TM for A = { 0k 1k | k ≥ 0 }
1. Scan across tape and reject if 0 is found to the right of a 1.
M3 = “On input string w ∈ {0, 1}∗:
2. Repeat the following if both 0s and 1s appear on tape:
2.1 Scan across tape, checking whether total number of 0s and 1s is even or odd. 1. Scan across tape and reject if 0 is found to the right of a 1.
If odd, reject. 2. Scan across 0s to first 1, copying 0s to tape 2.
2.2 Scan across tape, crossing off every other 0 (starting with the leftmost), and
every other 1 (starting with the leftmost). 3. Scan across 1s on tape 1 until the end.
For each 1 on tape 1, cross off a 0 on tape 2.
3. If no 0s or 1s remain, accept; otherwise, reject.”
If no 0s left, reject.
Analysis: 4. If any 0s left, reject; otherwise, accept.”
• Each stage requires O(n) time. Before Stage 1 After Stage 2
• Stage 1 and 3 run once each.
Tape 1 0 0 0 1 1 1 . . . 0 0 0 1 1 1 . . .
• Stage 2.2 eliminates half of 0s and 1s: Stage 2 runs O(log2 n) times.
• Total for stage 2 is O(log2 n)O(n) = O(n log n). Tape 2 . . . 0 0 0 . . .
• Grand total: O(n) + O(n log n) = O(n log n), Can show that running time of M3 is O(n).
so language A ∈ TIME(n log n).
Equivalent • • •
1-tape TM Tape # 0 1 1 # 0 0 # 1 0 0 1 # · · ·
CS 341: Chapter 7 7-29 CS 341: Chapter 7 7-30
Review Thm 3.13: Simulating k-Tape TM M on 1-Tape TM S Complexity of Simulation
On input w = w1 · · · wn, the 1-tape TM S does the following: • For each step of k-tape TM M , 1-tape TM S performs two scans
• First S prepares initial string on single tape: Length of active portion of S ’s tape determines how long S takes to
• • • perform each scan.
# w1 w2 · · · wn # # # ···
In r steps, TM M can read/write in ≤ k × r different cells on its k
• For each step of M , TM S scans tape twice tapes.
1. Scans its tape from As M has t(n) runtime, at any point during M ’s execution,
first # (which marks left end of tape) to total # active cells on all of M ’s tapes ≤ k × t(n) = O(t(n)).
(k + 1)st # (which marks right end of tape) Thus, each of S ’s scans requires O(t(n)) time.
to read symbols under “virtual” heads • Overall runtime of S
2. Rescans to write new symbols and move heads
Initial tape arrangement: O(n) steps.
If S tries to move virtual head to the right onto #, then S simulates each of M ’s t(n) steps using O(t(n)) steps.
M is trying to move head onto unused blank cell.
Thus, total of t(n) × O(t(n)) = O(t2 (n)) steps.
So S has to write blank on tape and shift rest of tape right one
cell. Grand total: O(n) + O(t2(n)) = O(t2(n)) steps.
Definition:
• Let N be NTM that is a decider (no looping).
f (n) f (n)
• Running time of NTM N is function f : N → N , where
f (n) = max ( height of tree of configs for N on input x ) accept
|x|=n
the maximum number of steps that NTM N uses
on any branch of the computation
on any input x of size n. accept/reject reject
CS 341: Chapter 7 7-33 CS 341: Chapter 7 7-34
Simulating NTM N on 1-Tape DTM D Complexity of Simulating NTM N on 1-Tape DTM D
Requires Exponential Overhead
• Analyze NTM N ’s computation tree on input w with |w| = n
Theorem 7.11
Root is starting configuration.
• Let t(n) be a function with t(n) ≥ n. Each node has ≤ b children
• Any t(n)-time nondeterministic TM has an equivalent 2O(t(n))-time b = max number of legal choices given by N ’s transition fcn δ .
deterministic 1-tape TM. Each branch has length ≤ t(n).
Total number of leaves ≤ bt(n).
Proof Idea: Total number of nodes ≤ 2 × (max number of leaves) = O(bt(n)).
• Suppose N is NTM decider running in t(n) time. Time to travel from root to any node is O(t(n)).
• On each input w, NTM N ’s computation is a tree of configurations. • DTM’s runtime ≤ time to visit all nodes:
• Simulate N on 3-tape DTM D using BFS of N ’s computation tree: O(bt(n)) × O(t(n)) = 2O(t(n))
D tries all possible branches. • Simulating NTM by DTM requires 3 tapes by Theorem 3.16.
If D finds any accepting configuration, D accepts. • By Theorem 3.13, simulating 3-tape DTM on 1-tape DTM requires
If all branches reject, D rejects.
(2O(t(n)))2 = 22×O(t(n)) = 2O(t(n)) steps.
• For integers 1
binary is good
unary is bad (exponentially worse) 3 5
Example: Suppose input to TM is the number 18 in decimal. • Universe Ω = { G, s, t | G is directed graph with nodes s, t } of
if encoding in binary, 18 = 10010 instances (for a particular encoding scheme).
if encoding in unary, 18 = 111111111111111111 • Language of decision problem comprises YES instances:
• For graphs PATH = { G, s, t | G is directed graph with path from s to t } ⊆ Ω.
list of nodes and edges (good) • For graph G above, G, 1, 5 ∈ PATH, but G, 2, 1 ∈ PATH.
adjacency matrix (good)
CS 341: Chapter 7 7-41 CS 341: Chapter 7 7-42
PATH ∈ P Complexity of Brute-Force Algorithm for PATH
Brute-force algorithm:
Theorem 7.14
PATH ∈ P. • Input is G, s, t ∈ Ω, where G is directed graph with nodes s and t.
• Any path from s to t need not repeat nodes.
Brute-force algorithm: • Examine each potential path in G of length ≤ m (= # nodes in G).
• Input is instance G, s, t ∈ Ω Check if the path goes from s to t.
G is directed graph with nodes s and t. Complexity analysis:
• Let m be number of nodes in G. • There are roughly mm potential paths of length ≤ m.
≤ m2 edges. For each potential path length k = 2, 3, . . . , m,
m (or m2) roughly measures size of instance G, s, t . check all k! permutations of k distinct nodes from m k possibilities.
m m!
• Any path from s to t need not repeat nodes. k! = k × (k − 1) × (k − 2) × · · · × 1, k = k!(m−k)!
k √
• Examine each potential path in G of length ≤ m. Stirling’s approximation: k! ∼ ke 2πk.
Check if the path goes from s to t. • This is exponential in the number m of nodes.
What is complexity of this algorithm? • So brute-force algorithm’s runtime is exponential in size of input.
...
3
Store subproblem solutions in a table as they are generated.
Look up smaller subproblem solutions as needed when solving larger Substrings of length 3
subproblems. Substrings of length 2
n Substrings of length 1
DP for CFGs: Cocke-Younger-Kasami (CYK) algorithm. w1 w2 w3 ... wn
CS 341: Chapter 7 7-53 CS 341: Chapter 7 7-54
Dynamic Programming Table Filling in Dynamic Programming Table
1 2 3 n ∗ ∗
1 Complete string
• Suppose s = uv , B ⇒ u, C ⇒ v , and ∃ rule A → BC .
2 ∗ ∗
Then A ⇒ s because A ⇒ BC ⇒ uv = s.
...
3
Substrings of length 3 • Suppose that algorithm has determined which variables generate each
Substrings of length 2 substring of length ≤ k.
n Substrings of length 1
w1 w2 w3 ... wn • To determine if variable A can generate substring of length k + 1:
• For i ≤ j , (i, j)th entry contains those variables that can generate split substring into 2 non-empty pieces in all possible (k) ways.
substring wi wi+1 · · · wj For each split, algorithm examines rules A → BC
• For i > j , (i, j)th entry is unused. Each piece is shorter than current substring,
• DP starts by filling in all entries for substrings of length 1, so table tells how to generate each piece.
then all entries for length 2, Check if B generates first piece.
then all entries for length 3, etc. Check if C generates second piece.
If both possible, then add A to table.
• Idea: Use shorter lengths to determine how to construct longer lengths.
• Build table t so that for i ≤ j , entry t(i, j) contains variables that can CFG has rule Y → b, so put Y in t(1, 1).
generate substring starting in position i and ending in position j
• Fill in one diagonal at a time.
CS 341: Chapter 7 7-57 CS 341: Chapter 7 7-58
Ex. (cont.): CYK for Substrings of Length 1 Ex. (cont.): CYK for Substrings of Length 2
Chomsky CFG: S → XY | Y Z X → YX | a Chomsky CFG: S → XY | Y Z X → YX | a
Y → ZZ | b Z → XY | a Y → ZZ | b Z → XY | a
1 2 3 4 5 1 2 3 4 5
1 Y 1 Y S, X
2 X, Z 2 X, Z
3 X, Z 3 X, Z
4 Y 4 Y
5 X, Z 5 X, Z
string b a a b a string b a a b a
• t(2, 2): substring a starts in position 2 and ends in position 2. • t(1, 2): substring ba starts in position 1 and ends in position 2.
1 2 3 4 5 1 2 3 4 5
1 Y S, X 1 Y S, X
2 X, Z Y 2 X, Z Y
3 X, Z 3 X, Z S, Z
4 Y 4 Y S, X
5 X, Z 5 X, Z
string b a a b a string b a a b a
• t(2, 3): substring aa starts in position 2 and ends in position 3. • t(3, 4): substring ab starts in position 3 and ends in position 4.
∗ ∗
split aa = a a : split ab = a b : X, Z ⇒ a by t(3, 3); Y ⇒ b by t(4, 4).
∗ ∗ ∗
X, Z ⇒ a by t(2, 2); X, Z ⇒ a by t(3, 3). If rule RHS ∈ t(3, 3) ◦ t(4, 4) = {XY , ZY }, then LHS ⇒ ab:
∗ ∗
If rule RHS ∈ t(2, 2) ◦ t(3, 3) = {XX, XZ, ZX, ZZ}, then S ⇒ XY ⇒ ab, Z ⇒ XY ⇒ ab
∗
LHS ⇒ aa: • t(4, 5): similarly handle substring ba by adding LHS of rule to t(4, 5)
∗
Y ⇒ ZZ ⇒ aa if RHS ∈ t(4, 4) ◦ t(5, 5).
CS 341: Chapter 7 7-61 CS 341: Chapter 7 7-62
Ex. (cont.): CYK for Substrings of Length 3 Ex. (cont.): CYK for Substrings of Length 3
Chomsky CFG: S → XY | Y Z X → YX | a Chomsky CFG: S → XY | Y Z X → YX | a
Y → ZZ | b Z → XY | a Y → ZZ | b Z → XY | a
1 2 3 4 5 1 2 3 4 5
1 Y S, X — 1 Y S, X —
2 X, Z Y 2 X, Z Y Y
3 X, Z S, Z 3 X, Z S, Z Y
4 Y S, X 4 Y S, X
5 X, Z 5 X, Z
string b a a b a string b a a b a
• t(1, 3): substring baa starts in position 1 and ends in position 3. • t(2, 4): substring aab starts in position 2 and ends in position 4.
• For each rule, add LHS to t(1, 3) if • Add LHS of rule to t(2, 4) if RHS ∈ t(2, 2) ◦ t(3, 4) ∪ t(2, 3) ◦ t(4, 4).
∗ ∗
RHS ∈ t(1, 1) ◦ t(2, 3) ∪ t(1, 2) ◦ t(3, 3). split aab = a ab : X, Z ⇒ a by t(2, 2); S, Z ⇒ ab by t(3, 4);
∗
∗
split baa = b aa : Y ⇒ b by t(1, 1); Y ⇒ aa by t(2, 3);
∗ so if rule RHS ∈ t(2, 2) ◦ t(3, 4) = {XS, XZ, ZS, ZZ}, then LHS ⇒ aab:
∗ ∗
so if rule RHS ∈ t(1, 1) ◦ t(2, 3) = {Y Y }, then LHS ⇒ baa. Y ⇒ ZZ ⇒ a ab
∗ ∗ ∗ ∗
split baa = ba a : S, X ⇒ ba by t(1, 2); X, Z ⇒ a by t(3, 3); split aab = aa b : Y ⇒ aa by t(2, 3); Y ⇒ b by t(4, 4);
∗ ∗
if rule RHS ∈ t(1, 2) ◦ t(3, 3) = {SX, SZ, XX, XZ}, then LHS ⇒ baa. so if rule RHS ∈ t(2, 3) ◦ t(4, 4) = {Y Y }, then LHS ⇒ aab.
• t(1, 4): substring baab starts in position 1 and ends in position 4. • t(2, 5): substring aaba starts in position 2 and ends in position 5.
∗ ∗
• For each rule, add LHS to t(1, 4) if split a aba: X, Z ⇒ a by t(2, 2); Y ⇒ aba by t(3, 5);
∗
so if rule RHS ∈ t(2, 2) ◦ t(3, 5) = {XY , ZY }, then LHS ⇒ aaba:
RHS ∈ ∪3k=1 t(1, k) ◦ t(k + 1, 4). ∗ ∗
S ⇒ XY ⇒ a aba, Z ⇒ XY ⇒ a aba
∗ ∗
split b aab :
∗
Y ⇒ b by t(1, 1);
∗
Y ⇒ aab by t(2, 4); split aa ba: Y ⇒ aa by t(2, 3); S, X ⇒ ba by t(4, 5);
∗
∗
so if rule RHS ∈ t(1, 1) ◦ t(2, 4) = {Y Y }, then LHS ⇒ baab. so if rule RHS ∈ t(2, 3) ◦ t(4, 5) = {Y S, Y X}, then LHS ⇒ aaba:
∗
∗ ∗ X ⇒ Y X ⇒ aa ba
split ba ab : S, X ⇒ ba by t(1, 2); S, Z ⇒ ab by t(3, 4); ∗ ∗
∗
so if rule RHS ∈ t(1, 2) ◦ t(3, 4) = {SS, SZ, XS, XZ}, then LHS ⇒ baab. split aab a: Y ⇒ aab by t(2, 4); X, Z ⇒ a by t(5, 5);
∗
∗ ∗ so if rule RHS ∈ t(2, 4) ◦ t(5, 5) = {Y X, Y Z}, then LHS ⇒ aaba:
split baa b : Nothing ⇒ baa as t(1, 3) = ∅; Y ⇒ b by t(4, 4). ∗
X ⇒ Y X ⇒ aab a
CS 341: Chapter 7 7-65 CS 341: Chapter 7 7-66
Ex. (cont.): CYK for Substrings of Length 5 Overall CYK Algorithm to show every CFL ∈ P
Does the following CFG in Chomsky Normal Form generate baaba ?
D = “On input string w = w1 w2 · · · wn ∈ Σ∗:
S → XY | Y Z X → YX | a
1. For w = ε, if S → ε is a rule, accept; else reject. [w = ε case]
Y → ZZ | b Z → XY | a
2. For i = 1 to n, [examine each substring of length 1]
1 2 3 4 5 3. For each variable A,
1 Y S, X — — S, X, Z 4. Test whether A → b is a rule, where b = wi.
2 X, Z Y Y S, X, Z 5. If so, put A in table (i, i).
3 X, Z S, Z Y 6. For = 2 to n, [ is length of substring]
4 Y S, X 7. For i = 1 to n − + 1, [i is start position of substring]
5 X, Z 8. Let j = i + − 1, [j is end position of substring]
string b a a b a 9. For k = i to j − 1, [k is split position]
10. For each rule A → BC ,
• t(1, 5): substring baaba starts in position 1 and ends in position 5. 11. If table (i, k) contains B and table (k + 1, j) contains C ,
• For each rule, add LHS to t(1, 5) if put A in table (i, j).
RHS ∈ ∪3k=1 t(1, k) ◦ t(k + 1, 5).
12. If S is in table (1, n), accept; else, reject.”
3 7
• Question: How hard is it to decide HAMPATH?
• Definition: A Hamiltonian path in a directed graph G visits each • Suppose graph G has m nodes.
node exactly once, e.g., 1 → 3 → 5 → 4 → 2 → 6 → 7 → 8.
• Easy to come up with (exponential) brute-force algorithm
• Decision problem: Given a directed graph G with nodes s and t,
does G have a Hamiltonian path from s to t? Generate each of the (m − 2)! potential paths.
• Universe Ω = { G, s, t | directed graph G with nodes s, t }, and Check if any of these is Hamiltonian.
language is
• Currently unknown if HAMPATH is solvable in polynomial time.
HAMPATH = { G, s, t | G is a directed graph with a
Hamiltonian path from s to t } ⊆ Ω.
• If G is above graph, G, 1, 8 ∈ HAMPATH, G, 2, 8 ∈ HAMPATH.
Consider G, s, t ∈ HAMPATH, where graph G has m nodes. • a composite number is not prime.
Then (# edges in G) ≤ m(m − 1) = O(m2). • Decision problem: Given natural number x, is x composite?
Suppose G encoded as list of nodes, list of edges . • Universe Ω = { x | natural number x }, and language is
Suppose given list p1, p2, . . . , pm of nodes that is claimed to be
COMPOSITES = { x | x = pq , for integers p, q > 1 } ⊆ Ω.
Hamiltonian path in G from s to t.
Can verify claim by checking Remarks:
1. if each node in G appears exactly once in claimed path,
which takes O(m2) time, • Can easily verify that a number is composite.
2. if each pair (pi, pi+1) is edge in G, which takes O(m3) time. If someone claims a number x is composite and provides a divisor p,
So verification takes time O(m3), which is polynomial in m. just need to verify that x is divisible by p.
• In 2002, Agrawal, Kayal and Sexena proved that PRIMES ∈ P.
• Thus, verifying a given path is Hamiltonian may be easier than
determining its existence. But COMPOSITES = PRIMES , so COMPOSITES ∈ P.
CS 341: Chapter 7 7-73 CS 341: Chapter 7 7-74
Verifiability Examples of Verifiers and Certificates
• Some problems may not be polynomially verifiable. • For HAMPATH, a certificate for
Consider HAMPATH, which is complement of HAMPATH. G, s, t ∈ HAMPATH
No known way to verify G, s, t ∈ HAMPATH in polynomial time. is simply the Hamiltonian path from s to t.
• Definition: Verifier for language A is (deterministic) algorithm V , Can verify in time polynomial in | G, s, t | if path is Hamiltonian.
where
A = { w | V accepts w, c for some string c }
• For COMPOSITES, a certificate for
• String c used to verify string w ∈ A
x ∈ COMPOSITES
c is called a certificate, or proof, of membership in A.
is simply one of its divisors.
Certificate is only for YES instance, not for NO instance.
• We measure verifier runtime only in terms of length of w. Can verify in time polynomial in | x | that the given divisor actually
divides x
• A polynomial-time verifier runs in (deterministic) time that is
polynomial in |w|.
• Remark: Certificate c is only for YES instance, not for NO instance.
• Language is polynomially verifiable if it has polynomial-time verifier.
(x1 ∨ x2 ∨ x3) ∧ (x3 ∨ x5 ∨ x6) ∧ (x3 ∨ x6 ∨ x4) ∧ (x2 ∨ x1 ∨ x5) • has runtime that is polynomial in |w| for w ∈ Σ∗1.
• Decision problem 3SAT : Given a 3cnf-formula φ, is φ satisfiable?
Universe Ω = { φ | φ is 3cnf-formula }
Language of decision problem:
3SAT = { φ | φ is a satisfiable 3cnf-function } ⊆ Ω.
φ ∈ 3SAT iff each clause in φ has at least one literal assigned 1.
CS 341: Chapter 7 7-93 CS 341: Chapter 7 7-94
Polynomial-Time Mapping Reducible: A ≤P B Polynomial-Time Mapping Reducible: A ≤P B
Consider
Ω1 = Σ∗1 Ω2 = Σ∗2
• language A defined over alphabet Σ1; i.e., universe Ω1 = Σ∗1. f
• language B defined over alphabet Σ2; i.e., universe Ω2 = Σ∗2.
A f B
Definition: A is polynomial-time mapping reducible to B , written
A ≤P B
if there is a polynomial-time computable function w∈A ⇐⇒ f (w) ∈ B
f : Σ∗1 → Σ∗2 YES instance for problem A ⇐⇒ YES instance for problem B
such that, for every string w ∈ Σ∗1,
• converts questions about membership in A to membership in B
w ∈ A ⇐⇒ f (w) ∈ B. • conversion is done efficiently (i.e., in polynomial time).
φ ∈ 3SAT iff G, k ∈ CLIQUE
Corresponding Graph:
• Suppose φ is a 3cnf-function with k clauses, e.g.,
Clause 2
φ = (x1 ∨x2 ∨x3)∧(x3 ∨x5 ∨x6)∧(x3 ∨x6 ∨x4)∧(x2 ∨x1 ∨x5) x1 x2 x2
• Convert φ into a graph G as follows:
Each literal in φ corresponds to a node in G. x1 x1
Nodes in G are organized into k triples t1, t2, . . . , tk .
Triple ti corresponds to the ith clause in φ.
Clause 1 x1 x2 Clause 3
Add edges between each pair of nodes, except
within same triple
between contradictory literals, e.g., x1 and x1 x2 x2
• Resulting graph has k-clique based on true literal from each clause: • Add edges between each pair of nodes, except
within same triple
Clause 2
between contradictory literals, e.g., x1 and x1
x1 x2 x2
• k-clique in G
must have 1 node from each triple
x1 x1 cannot include contradictory literals
• If φ ∈ 3SAT , then choose node corresponding to satisfied literal in
each clause to get k-clique in G.
Clause 1 x1 x2 Clause 3
• If G, k ∈ CLIQUE , then literals corresponding to k-clique satisfy φ.
A1 A5
• Constructing graph G
G has 3k nodes
Adding edges entails considering each pair of nodes in G: Remarks:
3k 3k(3k − 1) • NP-Complete problems are the most difficult problems in NP.
= = O(k2)
2 2
• Definition: Language B is NP-Hard if B satisfies part 2 of
Time to construct G is polynomial in size of 3cnf-function φ.
NP-Complete.
A1 A5
• Recall Theorem 7.31: If A ≤P B and B ∈ P, then A ∈ P.
C
So A ≤P B and B ≤P C imply A ≤P C . • Show that SAT is NP-Hard: A ≤P SAT for every language A ∈ NP.
• Tedious to prove a language C is NP-Complete using definition: CLIQUE = { G, k | G is an undirected graph with a k-clique }
1. C ∈ NP, and
1 4 6
2. C is NP-Hard: For every language A ∈ NP, we have A ≤P C .
3
NP
• Recall Theorem 7.36: A3 A4
A2 2 5 7
If B is NP-Complete and B ≤P C for C ∈ NP, B
A1 A5
then C is NP-Complete. C Corollary 7.43
CLIQUE is NP-Complete.
• Typically prove a language C is NP-Complete by applying Thm 7.36
Proof.
1. Prove that language C ∈ NP.
2. Reduce a known NP-Complete problem B to C . • Theorem 7.24: CLIQUE ∈ NP.
At this point, have shown that SAT and 3SAT are NP-Complete. • Corollary 7.42: 3SAT is NP-Complete.
3. Show that reduction takes polynomial time. • Theorem 7.32: 3SAT ≤P CLIQUE .
• Thus, Theorem 7.36 implies CLIQUE is NP-Complete.
Know P ⊆ NP: poly-time DTM is also poly-time NTM. Cook-Levin Theorem: SAT is NP-Complete.
Unknown if P = NP or P = NP. 3SAT , CLIQUE , ILP, SUBSET-SUM, HAMPATH, etc. are all
NP-Complete