© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved CSC 505, Fall 2000: Week 10 Objectives: • understand problem complexity and classes defined by it • understand relationships among decision, witness, evaluation, and optimization problems • understand what it means to reduce one problem to another • understand the meaning of membership in the class NP, and NP-completeness

Page 1 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved A complexity class (a class of problems that are related based on the efficiency -- time or space -- of the best possible algorithms for them)

Class P is the class of all decision problems that can be solved in time polynomial in the input size. Questions:

1. Why polynomial? (i.e. why is P an important class?)

2. Why decision problems?

3. How do we count time and input size?

Page 2 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved MST as a decision problem/language MST decision problem: Given a weighted,undirected graph G = (V,E,w) and an integer k, does G have a whose total weight is k?

LMST = { strings z | z encodes a graph G = (V,E,w) and an integer k and G has a spanning tree T with w(T) k }

1s @ 45 @ @ G = s @s 238 z = 1#10#100##1#11#101##10#11#1000###111 Do the details of the encoding matter?

Page 3 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Rules for this particular encoding

alphabet = {0,1,#} xed, nite hinstancei ::= hgraphi###hinti G and k hgraphi ::= hedgei{##hedgei} list of edges hedgei ::= hvertexi#vertexi#hinti 2 ends and weight hvertexi ::= hinti vertex names are ints hinti ::= hbiti{hbiti} ints are in binary hbiti ::= 0 | 1

12s 8 s ¨ ¨¨ 659¨ ¨¨ Another example: G = s¨ s k =18 347 1#10#1000##1#11#110##11#10#1001##11#100#111##10#100#101###10010

Page 4 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Types of combinatorial problems Optimization a combinatorial object (set, sequence, .. . ) of optimum (min/max) weight Example: Output a minimum spanning tree T of G = (V,E,w). Evaluation the weight of an optimum object Example: Output w(T ), the weight of a minimum spanning tree T of G = (V,E,w). Decision yes/no (does an object of a certain weight or better exist) Example: Given G = (V,E,w) and an integer k, does there exist a spanning tree T such that w(T) k? Witness a certicate for the decision problem, i.e. proof of a yes answer if such proof exists Example: Given G = (V,E,w) and an integer k, output a spanning tree T with w(T) k or report that none exists.

Page 5 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Are the problem types related?

If you can solve the optimization problem (in polynomial time), you can solve the evaluation, decision, and witness problems (in polynomial time). (Obvious. Why?)

If you can solve the decision problem, you can solve the evaluation problem. (Binary search on the interval of pos- sible solution values)

If you can solve both the evaluation and the witness prob- lem, you can solve the optimization problem. (Easy. How?)

The missing link is how to solve the witness problem using the decision problem.

Page 6 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Self-reducibility

w Graph G has a spanning tree u v with weight k . Can you determine if edge (u,v) is in such a spanning tree?

Your only resource is a “black box” that allows you to input a graph and a weight and will output “yes” if there is a spanning tree with that weight or less, no otherwise. You can perform modifications on G but you can’t “look” at it (i.e. you can put a modified form of G into the box).

Page 7 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Using the decision problem to solve the witness problem function MST-Witness(G, k) is . returns a spanning tree of G with weight k, . if one exists. Pick an arbitrary u ∈ V [G] for each v ∈ Adj[u] do if MST-Decision(G/uv, k w(uv)) then return MST-Witness(G/uv, k w(uv)) . G/uv is G with u and v joined into one vertex. ERROR: no such tree end MST-Witness Time bound, i.e. number of times MST-Decision is called?

Page 8 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Non-deterministic “algorithm” for MST-Decision

function MST-Decision(G, k) is . returns true i a spanning tree of G with weight k exists Choose n 1 edges to form a subset T E[G] Use DFS (for example) to check that T is a spanning tree (quit when a cycle is found or there’s more than one tree). Add up edge costs and check that the total is k. if T passes both tests then return true else return false end MST-Witness

Guess a certicate and then check the validity of the certicate.

Page 9 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved What is nondeterminism and what is NP?

How nondeterminism works. If there exists a choice that leads to a ”yes” answer, the algorithm answers ”yes” (true) for that instance. “magic” — no computational device works this way.

Why nondeterminisim is useful. Algorithms are simple. Algorithms can be simulated by deterministic ones. It is a useful way to classify problems.

N P is the class of all decision problems that can be solved by a nondeterministic algorithm (guess a certicate and check) in polynomial time (certicate has polynomial size; checking takes polynomial time). Compare with text!

Page 10 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved P and NP: what’s all the fuss about?

Traveling salesperson problem NP Polynomial algorithm? ?? Non-polynomial lower bound? At least as hard as...

P Minimum spanning-tree problem

TSP: Given an n x n distance matrix D (where D[i,j] = distance from city i to city j) and an integer K, does there exist a permutation p of {1,...,n} so that D[p(n),p(1)] + sum i from 1 to n–1 of D[p(i),p(i+1)] is at most K?

Page 11 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Reduction: a confusing concept A reduces to B means A can besolvedin terms of B, or A is at least as easy as B. In divide-and-conquer, greedy, dynamic and programming: re- duce (an instance of) a problem to (a smaller instance of) itself. Witness problem can be solved in terms of decision problem. An instance of transitive closure is reduced to several instances of matrix multiplication and 3 smaller instances of transitive closure. So an O(n)-time algorithm for matrix multiplication with 2 implies an O(n)-time algorithm for transitive closure.

But A reduces to B can also mean “B is at least as hard as A”. If transitive closure can’t be solved in O(n) time for some 2, neither can matrix multiplication! If A is known to be “hard” (NP-complete) and A reduces (in polynomial time) to B, then B is also hard (NP-complete).

Page 12 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Reduction: technical details

(Decision) problem P1 reduces in polynomial time to prob- lem P2, notation P1 P P2, if there exists a polynomial- time reduction algorithm F that transforms an arbitrary instance of P1 to an instance of P2 so that F (x) has a yes answer in P2 if and only if x has a yes answer in P1.

Simple example: PATH P SR. An instance of PATH is a directed, unweighted graph G = (V,E) and two vertices s and t — answer yes if there is a path from s to t in G. An instance of SR (shortest route) is an nn distance ma- trix D = {D[i, j]}, two “cities” s and t, and an integer B — answerP yes if there is a sequence s = x0, x1, ...,xk = t k1 so that i=0 D[xi,xi+1] B. How does the reduction algorithm work?

Page 13 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Proof of correctness for the reduction

Let F (G, s, t) be dened by the following:

1. Number V [G] from 1 to n, where n = |V [G]|.

2. For each pair i, j, if ij ∈ E[G], let D[i, j] =0, otherwise let D[i, j] = 1. Let B = 0 and output D,s,t,B.

Claim: There is a path from s to t in G i there is a path with sum of all edges = 0 from s to t using the distance matrix D.

Page 14 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Another reduction: TSP to Longest simple path

TSP P LSP. An instance of LSP (longest simple path) is a weighted, G = (V,E,w), two vertices s and t, and an integer bound B — answer yes if G has a path P from s to t with w(P ) B. Let F(D,K) be dened by the following: 1. Let V = {1, ...,n 1} ∪{s, t} and E = {uv | 1 u, v n 1} ∪{su | 1 u n 1} ∪{ut | 1 u n 1}. 2. Let dˆbe the largest distance in D and let w(u, v) = dˆ D[u, v] if 1 u, v n 1. Let w(s,u) = dˆ D[n,u] and let w[u, t] = dˆ D[u,n]. 3. Let B = n dˆ K and output G, s, t, and B.

Page 15 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Example of the TSP to LSP reduction Counterclockwise tour: 1 23 1 3–>1–>2–>3 6 Cost = 1 0 1 2 2 TSP: 1 2 4 0 5 3 4 Clockwise tour: 3 6 3 0 5 3 3–>2–>1–>3 Cost = 2 Corresponding 1 4 High-Low path: LSP: 0 s–>1–>2–>t 5 stCost = 2 3 as 3 3 as source 1 target Low-High path: 2 s–>2–>1–>t Cost = Transform: cycle to path, ≤ to ≥

Page 16 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Correctness of the reduction? Need to prove: 1. Certicate for TSP instance D,K implies certicate for LSP instance F (D,K). Let be a permutation that certies D,K and assume wlog that (1) = n (since onlyP the cyclic order matters). n1 This means that D[(n), n]+ i=1 D[(i), (i+ 1)] K. From the construction of the LSP instance, we can see that the path s (2)(3) ...(n) t has total weight nX1 (dˆD[(n), n])+ (dˆ D[(i), (i+ 1)]) ndˆK = B i=1

2. The converse: Certicate for LSP instance F(D,K) implies certicate for TSP instance D,K.

Suppose s = v1 ...vk = tPis a simplepath from s to t k1 with weight B, that is, i=1 w(vi, vi+1) B. There’s a problem here: we’d like to transform his path into a permutation. What might prevent that?

Page 17 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved A possible problem with the proof

1 23 1 1 1 0 22 2 2 Let K = 5 2 0 2 3 2 2 3 1 2 0 2 2 2 1 0 Then B = 3 x 2 – 5 = 1 1 0 and s 1 t is a path with s t weight ≥ B. 0 3 as 3 as 0 source 0 target 2

Page 18 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Adjusting the transformation The largest reasonable value for K is ndˆ (Otherwise we can transform to a trivial yes-instance). Suppose in F (D,K) we let w(u, v) = ndˆ+ 1 D[u, v] and B = n2dˆ+ n K.

weight of a certicate path B = n2dˆ+ n K n2dˆ+ n ndˆ > (n 1)(ndˆ+ 1) weight of a path with < n edges

Thus, a certicate path for F (D,K) must have at least n edges, and, being a simple path, must visit every vertex.

Page 19 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved The bad example revisited

1 23 1 1 1 0 22 2 2 Let K = 5 2 0 2 3 2 2 3 1 2 0 2 2 2 1 5 w(u,v) = 3 x 2 + 1 – D[u,v] 6 5 s t Then B = 9 x 2 + 3 – 5 = 16 5 3 as 5 3 as A certificate has weight source 5 target at least 21 – K and the edges 2 have weight 7 – a, 7 – b, and 7 – c. Thus, a + b + c ≤ K.

Page 20 of 21

© from notes written mostly by Dr. Matt Stallmann: All Rights Reserved Summary of polynomial reduction

Proving that A P B: Specify in detail how any instance x of A is to be trans- formed into an instance f(x) of B. This should be like a recipe, so that there’s no ambiguity and no doubt that it can be done in polynomial time.

Show how a certicate y for x can be converted to a certi- cate for f(x).

Show how a certicate z for f (x) can be converted to a certicate for x. This is important.

Page 21 of 21