Contents

Polynomial-Time Reductions Contents. ■ Polynomial-time reductions.

from special case to general case. – COMPOSITE reduces to FACTOR – -COVER reduces to SET-COVER

■ Reduction by simple equivalence. – PRIMALITY reduces to COMPOSITE, and vice versa – reduces to CLIQUE, and vice versa

■ Reduction from general case to special case. – SAT reduces to 3-SAT – 3-COLOR reduces to PLANAR-3-COLOR

■ Reduction by encoding with gadgets. – 3-CNF-SAT reduces to CLIQUE – 3-CNF-SAT reduces to HAM-CYCLE – 3-CNF-SAT reduces to 3-COLOR

Princeton University • COS 423 • Theory of Algorithms • Spring 2001 • Kevin Wayne 2

Polynomial-Time Reduction Polynomial-Time Reduction

Intuitively, problem X reduces to problem Y if: Purpose. Classify problems according to relative difficulty.

■ Any instance of X can be "rephrased" as an instance of Y. ≤ Design algorithms. If X P Y and Y can be solved in polynomial-time, Formally, problem X polynomial reduces to problem Y if arbitrary then X can be solved in polynomial time. instances of problem X can be solved using: ≤ ■ Polynomial number of standard computational steps, plus Establish intractability. If X P Y and X cannot be solved in polynomial-time, then X cannot be solved in polynomial time. ■ Polynomial number of calls to oracle that solves problem Y. – computational model supplemented by special piece of ≤ ≤ ≡ hardware that solves instances of Y in a single step Anti-symmetry. If X P Y and Y P X, we use notation X P Y.

≤ ≤ ≤ Remarks. Transitivity. If X P Y and Y P Z, then X P Z.

■ ■ We pay for time to write down instances sent to black box ⇒ Proof idea: compose the two algorithms.

instances of Y are of polynomial size. ■ Given an oracle for Z, can solve instance of X:

■ Note: Cook-Turing reducibility (not Karp or many-to-one). – run the algorithm for X using a oracle for Y X T ■ ≤ ≤ – each time oracle for Y is called, simulate it in a polynomial Notation: X P Y (or more precisely P Y ). number of steps by using algorithm for Y, plus oracle calls to Z

3 4 Polynomial-Time Reduction Compositeness and Primality

Basic strategies. COMPOSITE: Given the decimal representation of an integer x, does x

■ Reduction by simple equivalence. have a nontrivial factor?

■ Reduction from special case to general case. PRIME: Given the decimal representation of an integer x, is x prime?

■ Reduction from general case to special case.

■ ≡ Reduction by encoding with gadgets. Claim. COMPOSITE P PRIME. ≤ ■ COMPOSITE P PRIME. ≤ ■ PRIME P COMPOSITE.

COMPOSITE (x) PRIME (x)

IF (PRIME(x) = TRUE) IF (COMPOSITE(x) = TRUE) RETURN FALSE RETURN FALSE ELSE ELSE RETURN TRUE RETURN TRUE

5 6

Vertex Cover Vertex Cover

VERTEX COVER: Given an undirected graph G = (V, E) and an integer VERTEX COVER: Given an undirected graph G = (V, E) and an integer k, is there a subset of vertices S ⊆ V such that |S| ≤ k, and if (v, w) ∈ E k, is there a subset of vertices S ⊆ V such that |S| ≤ k, and if (v, w) ∈ E then either v ∈ S, w ∈ S or both. then either v ∈ S, w ∈ S or both.

Ex. Ex.

■ Is there a vertex cover of size 4? 1 6 ■ Is there a vertex cover of size 4? 1 6

YES. 2 7 YES. 2 7

■ Is there a vertex cover of size 3? 3 8 3 8 NO.

4 9 4 9

5 10 5 10

7 8 Clique Vertex Cover and Clique

≡ CLIQUE: Given N people and their pairwise relationships. Is there a Claim. VERTEX COVER P CLIQUE. group of S people such that every pair in the group knows each other. ■ Given an undirected graph G = (V, E), its complement is G' = (V, E'), where E' = { (v, w) : (v, w) ∉ E}. Ex. ■ G has a clique of size k if and only if G' has a vertex cover of size ■ People: a, b, c, d, e, . . . , k. |V| - k.

■ Friendships: (a, e), (a, f), (a, g), . . ., (h, k).

■ Clique size: S = 4. a b c

k d d u v u v YES Instance j e z w z w

i f y x y x

h g G G' Friendship Graph Clique = {u, v, x, y} Vertex cover = {w, z}

9 10

Vertex Cover and Clique Vertex Cover and Clique

≡ ≡ Claim. VERTEX COVER P CLIQUE. Claim. VERTEX COVER P CLIQUE.

■ Given an undirected graph G = (V, E), its complement is G' = (V, E'), ■ Given an undirected graph G = (V, E), its complement is G' = (V, E'), where E' = { (v, w) : (v, w) ∉ E}. where E' = { (v, w) : (v, w) ∉ E}.

■ G has a clique of size k if and only if G' has a vertex cover of size ■ G has a clique of size k if and only if G' has a vertex cover of size |V| - k. |V| - k.

Proof. ⇒ u v Proof. ⇐ u v ■ Suppose G has a clique S with |S| = k. ■ Suppose G' has a cover S' with |S'| = |V| - k. z w z w ■ Consider S' = V –S. ■ Consider S = V –S'. y x ■ |S'| = |V| - k. ■ Clearly |S| = k. y x

■ To show S' is a cover, consider any ■ To show S is a clique, consider some edge (v, w) ∈ E'. edge (v, w) ∈ E'.

– then (v, w) ∉ E u v – if (v, w) ∈ E', then either v ∈ S', w ∈ S', or both u v – at least one of v or w is not in S – by contrapositive, if v ∉ S' and w ∉ S', z w (since S forms a clique) then (v, w) ∈ E z w – at least one of v or w is in S' – thus S is a clique in G y x y x – hence (v, w) is covered by S'

11 12 Polynomial-Time Reduction Compositeness Reduces to Factoring

Basic strategies. COMPOSITE: Given an integer x, does x have a nontrivial factor?

■ Reduction by simple equivalence. FACTOR: Given two integers x and y, does x have a nontrivial factor

■ Reduction from special case to general case. less than y?

■ Reduction from general case to special case. ≤ ■ Reduction by encoding with gadgets. Claim. COMPOSITE P FACTOR.

Proof. Given an oracle for FACTOR, we solve COMPOSITE.

■ Is 350 composite?

■ Does 350 have a nontrivial factor less than 350? COMPOSITE (x)

IF (FACTOR(x, x) = TRUE) RETURN TRUE ELSE RETURN FALSE

13 14

Primality Testing and Factoring Set Cover

We established: SET COVER: Given a set U of elements, a collection S1, S2, . . . , Sm of ≤ ≤ ■ PRIME P COMPOSITE P FACTOR. subsets of U, and an integer k, does there exist a collection of at most k of these sets whose union is equal of U? Natural question: ≤ Sample application. ■ Does FACTOR P PRIME ? ■ n available pieces of software. ■ Consensus opinion = NO.

■ Set U of n capabilities that we would like our system to have. ⊆ State-of-the-art. ■ The ith piece of software provides the set Si U of capabilities.

■ ■ PRIME in randomized P and conjectured to be in P. Goal: achieve all n capabilities using small number of pieces of software. ■ FACTOR not believed to be in P.

Ex. U = {1, 2, 3, . . . , 12}, k = 3. RSA cryptosystem. ■ S = {1, 2, 3, 4, 5, 6} S = {5, 6, 8, 9} ■ Based on dichotomy between two problems. 1 2 ■ S = {1, 4, 7, 10} S = {2, 5, 7, 8, 11} ■ To use, must generate large primes efficiently. 3 4 ■ S5 = {3, 6, 9, 12} S6 = {10, 11} ■ Can break with efficient factoring algorithm. YES: S3, S4, S5.

15 16 Vertex Cover Reduces to Set Cover Vertex Cover Reduces to Set Cover

SET COVER: Given a set U of elements, a collection S1, S2, . . . , Sn' of SET COVER: Given a set U of elements, a collection S1, S2, . . . , Sn' of subsets of U, and an integer k, does there exist a collection of at most subsets of U, and an integer k, does there exist a collection of at most k of these sets whose union is equal to U? k of these sets whose union is equal to U?

VERTEX COVER: Given an undirected graph G = (V, E) and an integer VERTEX COVER: Given an undirected graph G = (V, E) and an integer k, is there a subset of vertices S ⊆ V such that |S| ≤ k, and if (v, w) ∈ E k, is there a subset of vertices S ⊆ V such that |S| ≤ k, and if (v, w) ∈ E then either v ∈ S, w ∈ S or both. then either v ∈ S, w ∈ S or both.

≤ ≤ Claim. VERTEX-COVER P SET-COVER. Claim. VERTEX-COVER P SET-COVER.

Proof. Given black box that solves instances of SET-COVER. Proof. Given black box that solves instances of SET-COVER.

■ Let G = (V, E), k be an instance of VERTEX-COVER. U = {1, 2, 3, 4, 5, 6, 7} ■ Create SET-COVER instance: G = (V, E) a b k = 2 ∈ k = 2 S = {3, 7} S = {2, 4} – k = k, U = E, Sv = {e E : e incident to v } e7 e e a b e2 3 4 Sc = {3, 4, 5, 6} Sd = {5} ■ Set-cover of size at most k if and only if vertex cover of size at f e6 c Se = {1} Sf= {1, 2, 6, 7} most k. e e Vertex Cover 1 5 e d Set Cover 17 18

Polynomial-Time Reduction Factoring and Finding Factors

Basic strategies. FACTOR: Given two integers x and y, does x have a nontrivial factor

■ Reduction by simple equivalence. less than y?

■ Reduction from special case to general case. FACTORIZE: Given an integer x, find its prime factorization.

■ Reduction from general case to special case. ≡ ■ Reduction by encoding with gadgets. Claim. FACTORIZE P FACTOR. ≤ Proof: FACTOR P FACTORIZE.

■ Reduction from special case to general case.

FACTOR (x)

S = prime factorization S ← FACTORIZE(x) is of polynomial size d ← smallest factor in S IF (d < y) RETURN TRUE ELSE RETURN FALSE

19 20 Factoring and Finding Factors Satisfiability

xi FACTOR: Given two integers x and y, does x have a nontrivial factor Literal: A Boolean variable or its negation. or xi C less than y? x = ∨ x ∨ FACTORIZE: Given an integer x, find its prime factorization. Clause: A disjunction of literals. j 1 2 x3 B C C Claim. FACTORIZE ≡ FACTOR. FACTORIZE(x) Conjunctive normal form: A Boolean formula C P = ∧ ∧ ∧ C ≤ that is the conjunction of clauses. 1 2 3 4 Proof: FACTORIZE P FACTOR. IF (FACTOR(x, x) = NO) ■ Reduction from general S ← S ∪ {x} case to special case. RETURN CNF-SAT: Given propositional formula in conjunctive normal form, does it have a satisfying truth assignment? left = 1, right = x WHILE (right > left + 1) mid = (left + right) / 2 ()∨ ∨ ∧ ()∨ ∨ ∧ ()∨ ∧ ()∨ ∨ find smallest factor x1 x2 x3 x1 x2 x3 x2 x3 x1 x2 x3 via binary search IF (FACTOR(x, mid) = TRUE) right = mid ELSE YES instance left = mid x1 = true S = global variable S ← S ∪ {left} x1 = true containing set of factors FACTORIZE(x / left) x1 = false

21 22

SAT Reduces to 3-SAT SAT Reduces to 3-SAT

3-CNF-SAT: CNF-SAT, where each clause has 3 distinct literals. 3-CNF-SAT: CNF-SAT, where each clause has 3 distinct literals.

Claim. CNF-SAT ≤ 3-CNF-SAT. Claim.C CNF-SAT ≤ 3-CNF-SAT. P x P ■ x l ≥ Case 3: clause Cj contains exactly 3 terms. ■ Case 4: clausex Cj contains 4 terms. C x ■ x j – introduce l - 1 extra Booleanx variables Case 2: clause Cj containsx exactly 2 terms. x – l C – add 1 new term, and replaceC Cj with 2 clauses replace Cj with clauses j x x C x C x = ∨ ⇒ ' = x ∨ ∨ y = ∨ ∨ ∨ ∨ ∨ ⇒j ' =y ∨ y∨ 3 7 j 1 3 7 1 3 4 5 6 9 C 1 1 x 1 1 ' x ' = ∨ ∨ y j =y ∨ ∨y j2 3 7 C 2 1 x 3 2 ' j = y ∨ ∨y C 3 2 x 4 3 C ' = y ∨ ∨ y ■ j 4 3 5 4 Case 1: clausex Cj contains exactly 1 term. C x C ' = y ∨ ∨ y – add 4 new terms, and replace Cj with 4 clauses j 5 4 6 5 j x x C y ' = ∨ ∨ x ' j6 5 9 9 = ⇒ j = x ∨ ∨y 3 C 1 3 y 1 2 ' = ∨ ∨ j 2 x 3 1 y 2 C y ' = ∨ ∨ j 3 x3 1 y 2 y ' = ∨ ∨ j 4 3 1 y2 23 24 C j C j t j t j t j SAT Reduces to 3-SAT t j SAT Reduces to 3-SAT t j t j t j t j C C l ≥ j ■ l ≥ j ■ Case 4: clause Cj contains 4 terms. t Case 2: clause Cj contains 4 terms. t C j C j j t j j t j = ∨ ∨ ∨ L∨ ⇒ ' y= ∨ y∨ = ∨ ∨ ∨ L∨ ⇒ ' y= ∨ y∨ 1 2 3 l C 1 1 1 1 1 2 3 l C 1 1 1 1 j t j j t j ' =y ∨ y∨ ' =y ∨ y∨ C 2 1 2 2 C 2 1 2 2 j t j j t j ' =y ∨ ∨y ' =y ∨ ∨y k = 4 C 3 2 3 3 C 3 2 3 3 j t j j t j ' = y ∨ ∨y ' = y ∨ ∨y 4 3 t 4 4 4 3 t 4 4 ' = ∨ j ∨ ' = ∨ j ∨ 5 4 5 y 5 5 4 5 y 5 C j C j M yM M M M yM M M Claim. CNF-SAT instance is satisfiable t j Claim. CNF-SAT instance is satisfiable t j ' = ∨ ∨ t ' = ∨ ∨ t if and only if 3-CNF-SAT instance is. l l−1 l j l if and only if 3-CNF-SAT instance is. l l−1 l j l

⇒ ⇐ Proof. Suppose SAT instance is satisfiable. set TRUE Proof. Suppose 3-SAT instance is satisfiable. ■ If SAT assignment sets tjk = 1, 3-SAT assignment sets: ■ If 3-SAT assignment sets tjk = 1, SAT assignment sets tjk = 1.

– ■ tjk = 1 Consider clause Cj . We claim tjk = 1 for some k. – ≥ l l ym = 1 for all m < k; ym = 0 for all m k – each of - 1 new Boolean variables yj can only make one of new clauses true

– the remaining clause must be satisfied by an original term tjk 25 26

Polynomial-Time Reduction Clique

Basic strategies. CLIQUE: Given N people and their pairwise relationships. Is there a group of C people such that every pair in the group knows each other. ■ Reduction by simple equivalence.

■ Reduction from special case to general case. Ex. ■ Reduction from general case to special case. ■ People: a, b, c, d, e, . . . , k. ■ Reduction by encoding with gadgets. ■ Friendships: (a, e), (a, f), (a, g), . . ., (h, k).

■ Clique size: C = 4. a b c

k d YES Instance j e

i f

h g

Friendship Graph

27 28 Satisfiability Reduces to Clique Satisfiability Reduces to Clique

≤ ≤ Claim. CNF-SAT P CLIQUE. Claim. CNF-SAT P CLIQUE.

■ Given instance of CNF-SAT, create a person for each literal in each ■ Given instance of CNF-SAT, create a person for each literal in each clause. clause.

■ Two people know each other except if: – they come from the same clause first clause – they represent a literal and its negation

x' y z x' y z

x' x x' x

y' y' y' y' (x' + y + z) (x + y' + z) (y + z) (x' + y' + z') (x' + y + z) (x + y' + z) (y + z) (x' + y' + z') C = 4 clauses z' z C = 4 clauses z' z

y z y z 29 30

Satisfiability Reduces to Clique Satisfiability Reduces to Clique

≤ ≤ Claim. CNF-SAT P CLIQUE. Claim. CNF-SAT P CLIQUE.

■ Given instance of CNF-SAT, create a person for each literal in each ■ Given instance of CNF-SAT, create a person for each literal in each clause. clause.

■ Two people know each other except if: ■ Two people know each other except if: – they come from the same clause – they come from the same clause – they represent a literal and its negation – they represent a literal and its negation

■ Clique of size C ⇒ satisfiable assignment. ■ Clique of size C ⇒ satisfiable assignment.

– set variable in clique to true ■ Satisfiable assignment ⇒ clique of size C. – (x, y, z) = (true, true, false) x' y z – (x, y, z) = (true, true, false) x' y z – choose one true literal from each clause x' x x' x

y' y' y' y' (x' + y + z) (x + y' + z) (y + z) (x' + y' + z') (x' + y + z) (x + y' + z) (y + z) (x' + y' + z') C = 4 clauses z' z C = 4 clauses z' z

y z y z 31 32 Polynomial-Time Reductions Problem Genres

CNF-SAT Basic genres.

: SET-PACKING, INDEPENDENT SET.

: SET-COVER, VERTEX-COVER. 3-CNF-SAT CLIQUE ■ Constraint satisfaction problems: SAT, 3-SAT.

Dick Karp (1972) ■ Sequencing problems: HAMILTONIAN-CYCLE, TSP.

■ Partitioning problems: 3D-, 3-COLOR.

■ Numerical problems: SUBSET-SUM, KNAPSACK, FACTOR. 3-COLOR DIR-HAM-CYCLE IND-SET VERTEX-COVER

PLANAR-3- HAM-CYCLE SET-COVER SUBSET-SUM COLOR

TSP PARTITION

SCHEDULE KNAPSACK 33 34

Hamiltonian Cycle Hamiltonian Cycle

HAMILTONIAN-CYCLE: given an undirected graph G = (V, E), does HAMILTONIAN-CYCLE: given an undirected graph G = (V, E), does there exists a simple cycle C that contains every vertex in V. there exists a simple cycle C that contains every vertex in V.

1 1'

2 2'

3 3'

4 4'

5

YES: vertices and faces of a dodecahedron. NO: with odd number of nodes.

35 36 Finding a Hamiltonian Cycle Finding a Hamiltonian Cycle

HAM-CYCLE: given an undirected graph G = (V, E), does there exist a HAM-CYCLE: given an undirected graph G = (V, E), does there exist a simple cycle C that contains every vertex in V. simple cycle C that contains every vertex in V.

FIND-HAM-CYCLE: given an undirected graph G = (V, E), output a FIND-HAM-CYCLE: given an undirected graph G = (V, E), output a Hamiltonian cycle if one exists, otherwise output any cycle. Hamiltonian cycle if one exists, otherwise output any cycle.

≡ ≡ Claim. HAM-CYLCE P FIND-HAM-CYCLE. Claim. HAM-CYLCE P FIND-HAM-CYCLE.

Proof. ≤ Proof. ≥ FIND-HAM-CYCLE (G) P HAM-CYCLE (G) P IF (HAM-CYCLE(G) = FALSE) C ← FIND-HAM-CYCLE(G) RETURN FALSE IF (C is Hamiltonian) RETURN TRUE A ← E ELSE FOR EACH e ∈ E RETURN FALSE IF (HAM-CYCLE(V, A - {e}) = TRUE) A ← A - {e}

RETURN unique cycle remaining in G

37 38

Directed Hamiltonian Cycle Directed Hamiltonian Cycle

DIR-HAM-CYCLE: given a directed graph G = (V, E), does there exists Claim. G has a Hamiltonian cycle if and only if G' does. a simple directed cycle C that contains every vertex in V. Proof. ⇒ Claim. DIR-HAM-CYCLE ≤ HAM-CYCLE. P ■ Suppose G has a directed Hamiltonian cycle C.

■ Then G' has an undirected Hamiltonian cycle. Proof.

■ Given a directed graph G = (V, E), construct an undirected graph G' Proof. ⇐ with 3n vertices. ■ Suppose G' has an undirected Hamiltonian cycle C'. aout ■ C' must visit nodes in G' using one of following two orders: din a . . . , G, R, B, G, R, B, G, R, B, . . . d . . . , R, G, B, R, G, B, R, G, B, . . . b v bout vin v vout

■ Blue nodes in C' make up directed Hamiltonian cycle C in G, or e e c in reverse of one. cout G G'

39 40 3-SAT Reduces to Directed Hamiltonian Cycle 3-SAT Reduces to Directed Hamiltonian Cycle

≤ Claim. 3-CNF-SAT P DIR-HAM-CYCLE. Proof: Given 3-CNF-SAT instance with n variables xi and k clauses Cj. n ■ Construct G to have 2 Hamiltonian cycles.

■ ⇔ Why not reduce from some other problem? ■ Intuition: traverse path i from left to right set variable xi = 1. ! Need to find another problem that is sufficiently close. (could reduce from VERTEX-COVER) s ! If don't succeed, start from 3-CNF-SAT since its combinatorial structure is very basic. ! Downside: reduction will require certain level of complexity.

n

t

3k + 3 41 42

3-SAT Reduces to Directed Hamiltonian Cycle 3-SAT Reduces to Directed Hamiltonian Cycle

Proof: Given 3-CNF-SAT instance with n variables xi and k clauses Cj. Proof: Given 3-CNF-SAT instance with n variables xi and k clauses Cj.

■ Add node and 6 edges for each clause. ■ Add node and 6 edges for each clause. C C x x x = x = V V x 1 1 V 2 V x3 2 1 2 3

s s

x1 x1

x2 x2

x3 x3

t t

43 44 3-SAT Reduces to Directed Hamiltonian Cycle 3-SAT Reduces to Directed Hamiltonian Cycle

Claim. 3-CNF-SAT instance is satisfiable if and only if corresponding Claim. 3-CNF-SAT instance is satisfiable if and only if corresponding graph G has a Hamiltonian cycle. graph G has a Hamiltonian cycle.

Proof. ⇒ Proof. ⇒

■ Suppose 3-SAT instance has satisfying assignment x*. ■ Suppose G has a Hamiltonian cycle C.

■ Then, define Hamiltonian cycle in G as follows: ■ If C enters clause node Cj , it must depart on mate edge.

– if x*i = 1, traverse path Pi from left to right – thus, nodes immediately before and after Cj are connected by an edge e in G – if x*i = 0, traverse path Pi from right to left – removing C from cycle, and replacing it with edge e yields – for each clause Cj , there will be at least one path Pi in which we j Hamiltonian cycle on G - {C } are going in "correct" direction to splice node Cj into tour j ■ Continuing in this way, we are left with Hamiltonian cycle C' in

G - {C1 , C2 , . . . , Ck}.

■ Set x*i = 1 if path Pi if traversed from left to right, and 0 otherwise.

■ Since C visits each clause node Cj , at least one of the paths is traversed in "correct" direction, and each clause is satisfied.

45 46

Polynomial-Time Reductions Implications Reduction

CNF-SAT Proof that a problem is as hard as CNF-SAT is usually taken as signal to abandon hope of finding an efficient algorithm.

3-CNF-SAT CLIQUE

Dick Karp (1972)

3-COLOR DIR-HAM-CYCLE IND-SET VERTEX-COVER

PLANAR-3- HAM-CYCLE SET-COVER SUBSET-SUM COLOR

TSP PARTITION INTEGER PROGRAMMING

SCHEDULE KNAPSACK 47 48 Implications Reduction Implications Reduction

Proof that a problem is as hard as CNF-SAT is usually taken as signal Proof that a problem is as hard as CNF-SAT is usually taken as signal to abandon hope of finding an efficient algorithm. to abandon hope of finding an efficient algorithm.

49 50

Problem Genres 3-Colorability

Basic genres. 3-COLOR: Given an undirected graph does there exists a way to color the nodes R, G, and B such no adjacent nodes have the same color? ■ Packing problems: SET-PACKING, INDEPENDENT SET.

■ Covering problems: SET-COVER, VERTEX-COVER.

■ Constraint satisfaction problems: SAT, 3-SAT.

■ Sequencing problems: HAMILTONIAN-CYCLE, TSP.

■ Partitioning problems: 3-COLOR.

■ Numerical problems: SUBSET-SUM, KNAPSACK, FACTOR.

YES instance

51 52 3-Colorability 3-Colorability

≤ ≤ Claim. 3-CNF-SAT P 3-COLOR. Claim. 3-CNF-SAT P 3-COLOR. Proof: Given 3-SAT instance with n variables xi and k clauses Cj. Proof: Given 3-SAT instance with n variables xi and k clauses Cj.

■ Create instance of 3-COLOR G = (V, E) as follows. ■ Step 2:

■ Step 1: – for each clause, add "gadget" of 6 new nodes and 13 new edges – create triangle R (false), G (true), or B – create nodes for each literal and connect to B ! Each literal colored R or G. – create nodes for each literal, and connect literal to its negation ! Each literal colored opposite of its negation. B

x x x 1 2 3 C T F x = x Step 1 1 1 V 2 V x3 B Step 2

T F x1 x1 x2 x2 x3 x3 xn xn 53 54

3-Colorability 3-Colorability

≤ ≤ Claim. 3-CNF-SAT P 3-COLOR. Claim. 3-CNF-SAT P 3-COLOR. Proof: Given 3-SAT instance with n variables xi and k clauses Cj. Proof: Given 3-SAT instance with n variables xi and k clauses Cj.

■ Step 2: ■ Step 2: – for each clause, add "gadget" of 6 new nodes and 13 new edges – for each clause, add "gadget" of 6 new nodes and 13 new edges – if 3-colorable, top row must have at least one green (true) node – if top row has green (true) node, then 3-colorable ! Otherwise, middle row all blue. ! Color vertex below green node red, and one below that blue. ! Bottom row alternates between green and red ⇒ ! Color remaining middle row nodes blue. contradiction. ! Color remaining bottom nodes red or green, as forced. B B

x x x x x x 1 2 3 C 1 2 3 C x x = x = x 1 1 V 2 V x3 1 1 V 2 V x3

Step 2 Step 2

T F T F

55 56 Planar 3-Colorability Planar 3-Colorability

PLANAR-3-COLOR. PLANAR-3-COLOR.

■ Given a planar map, can it be colored using 3 colors so that no ■ Given a planar map, can it be colored using 3 colors so that no adjacent regions have the same color? adjacent regions have the same color?

YES instance. NO instance.

57 58

Planarity Planarity Testing

Planarity. A graph is planar if it can be embedded on the plane (or Kuratowski's Theorem. An undirected graph G is non-planar if and sphere) in such a way that no two edges graph. only if it contains a subgraph homeomorphic to K5 or K3,3.

■ Applications: VLSI circuit design, computer graphics. Brute force: O(n6).

■ Step 1. Contract all nodes of degree 2.

■ Step 2. Check all subsets of 5 nodes to see if they form a K5.

■ Step 3. Check all subsets of 6 nodes to see if they form a K3,3.

Cleverness: O(n). Planar K5: non-planar K3,3: non-planar ■ Step 1. DFS.

■ Step 2. Tarjan. Kuratowski's Theorem. An undirected graph G is non-planar if and only if it contains a subgraph homeomorphic to K5 or K3,3.

homeomorphic to K3,3

59 60 Planar 3-Colorability Planar 4-Colorability

≤ Claim. 3-COLOR P PLANAR-3-COLOR. PLANAR-4-COLOR: Given a planar map, can it be colored using 4 colors so that no adjacent regions have the same color? Proof sketch: Given instance of 3-COLOR, draw graph in plane, letting edges cross if necessary. Intuition.

■ Replace each edge crossing with the following planar gadget W. ■ If PLANAR-3-COLOR is hard, then so is PLANAR-4-COLOR and – in any 3-coloring of W, opposite corners have the same color PLANAR-5-COLOR.

– any assignment of colors to the corners in which opposite ■ Don't always believe your intuition! corners have the same color extends to a 3-coloring of W

61 62

Planar 4-Colorability Problem Genres

PLANAR-2-COLOR. Basic genres.

■ Solvable in linear time. ■ Packing problems: SET-PACKING, INDEPENDENT SET.

■ Covering problems: SET-COVER, VERTEX-COVER.

PLANAR-3-COLOR. ■ Constraint satisfaction problems: SAT, 3-SAT.

■ NP-complete. ■ Sequencing problems: HAMILTONIAN-CYCLE, TSP.

■ Partitioning problems: 3D-MATCHING.

PLANAR-4-COLOR. ■ Numerical problems: SUBSET-SUM, KNAPSACK, FACTOR.

■ Solvable in O(1) time.

Theorem (Appel-Haken, 1976). Every planar map is 4-colorable.

■ Resolved century-old open problem.

■ Used 50 days of computer time to deal with many special cases.

■ First major theorem to be proved using computer.

63 64 Subset Sum Subset Sum Treat as base k+1 integer

≤ SUBSET-SUM: Given a set X of integers and a target integer t, is there Claim. VERTEX-COVER P SUBSET-SUM. a subset S ⊆ X whose elements sum to exactly t. Proof. Given instance G, k of VERTEX-COVER, create following instance of SUBSET-SUM. Example: X = {1, 4, 16, 64, 256, 1040, 1041, 1093, 1284, 1344}, t = 3754. e1 e2 e3 e4 e5 e6 decimal 1 2 ■ YES: S = {1, 16, 64, 256, 1040, 1093, 1284}. x1 1 1 0 1 0 0 0 5,184 k = 3 x 1 0 1 0 0 1 0 4,356 e3 e1 e2 e5 2 Remark. x3 1 0 0 0 1 1 0 4,116 4 e 5 e 3 ■ With arithmetic problems, input integers are encoded in binary. 6 4 x4 1 0 0 1 0 0 1 4,161

■ Polynomial reduction must be polynomial in binary encoding. x5 1 1 1 0 1 0 1 5,393 e1 e2 e3 e4 e5 e6 y1 0 1 0 0 0 0 0 1,024 v1 1 0 1 0 0 0 y2 0 0 1 0 0 0 0 256 v2 0 1 0 0 1 0 y3 0 0 0 1 0 0 0 64 v3 0 0 0 1 1 0 y4 0 0 0 0 1 0 0 16 v4 0 0 1 0 0 1 y5 0 0 0 0 0 1 0 4 v5 1 1 0 1 0 1 y6 0 0 0 0 0 0 1 1 Node-arc incidence matrix t 3 2 2 2 2 2 2 15,018

65 k 66

Subset Sum Subset Sum

Claim. G has vertex cover of size k if and only if there is a subset S Claim. G has vertex cover of size k if and only if there is a subset S that sums to exactly t. that sums to exactly t.

Proof. ⇒ Proof. ⇐ e1 e2 e3 e4 e5 e6 decimal e1 e2 e3 e4 e5 e6 decimal ■ Suppose G has a vertex cover C ■ Suppose subset S sums to t. x1 1 1 0 1 0 0 0 5,184 x1 1 1 0 1 0 0 0 5,184 of size k. ■ ∩ x2 1 0 1 0 0 1 0 4,356 Let C = S {x1, . . . , xn}. x2 1 0 1 0 0 1 0 4,356 ■ Let S = C ∪ { y : |e ∩ C| = 1 } – j j x3 1 0 0 0 1 1 0 4,116 each edge has three 1's, so x3 1 0 0 0 1 1 0 4,116 – most significant bits add up no carries possible x4 1 0 0 1 0 0 1 4,161 x4 1 0 0 1 0 0 1 4,161 to k – |C| = k x5 1 1 1 0 1 0 1 5,393 x5 1 1 1 0 1 0 1 5,393 – remaining bits add up to 2 – at least one xi must y1 0 1 0 0 0 0 0 1,024 y1 0 1 0 0 0 0 0 1,024 contribute to sum for ej y2 0 0 1 0 0 0 0 256 y2 0 0 1 0 0 0 0 256

y3 0 0 0 1 0 0 0 64 y3 0 0 0 1 0 0 0 64

1 2 y4 0 0 0 0 1 0 0 16 1 2 y4 0 0 0 0 1 0 0 16

y5 0 0 0 0 0 1 0 4 y5 0 0 0 0 0 1 0 4 e3 e1 e2 e5 e3 e1 e2 e5 y6 0 0 0 0 0 0 1 1 y6 0 0 0 0 0 0 1 1 4 e 5 e 3 4 e 5 e 3 6 4 t 3 2 2 2 2 2 2 15,018 6 4 t 3 2 2 2 2 2 2 15,018

k 67 k 68 Partition Polynomial-Time Reductions

SUBSET-SUM: Given a set X of integers and a target integer t, is there CNF-SAT a subset S ⊆ X whose elements sum to exactly t.

⊆ PARTITION: Given a set X of integers, is there a subset S X such 3-CNF-SAT CLIQUE that a∑S a = ∑ a . a ∈ ∈X \ S Dick Karp (1972)

Claim. SUBSET-SUM ≤ PARTITION. P 3-COLOR DIR-HAM-CYCLE IND-SET VERTEX-COVER Proof. Let (X, t) be an instance of SUBSET-SUM. = ■ Define W to be sum of integers in X: W ∑a∈X a .

■ Create instance of PARTITION: X' = X ∪ {2W - t} ∪ {W + t}.

■ SUBSET-SUM instance is yes if and only if PARTITION instance is. PLANAR-3- HAM-CYCLE SET-COVER SUBSET-SUM – in any partition of X' COLOR ! Each half of partition sums to 2W. ! Two new elements can't be in same partition. TSP PARTITION INTEGER ! Discard new elements ⇒ subset of X that sums to t. PROGRAMMING

LOAD-BALANCE KNAPSACK 69 70