=========================================================================== CSC 363 Lecture Summary for Week 10 Fall 2009 =========================================================================== Examples of NP-completeness: - SUBSET-SUM is NPc: Already known in NP. For NP-hardness, show 3SAT <=p SS: Given formula F = (a1 \/ b1 \/ c1) /\ ... /\ (ar \/ br \/ cr) where ai,bi,ci in {x1,~x1,...,xs,~xs}, construct numbers as follows: . For j = 1,...,s, number xj = 1 followed by s-j 0s followed by r digits where k-th next digit equals 1 if xj appears in clause C_k, 0 otherwise; number ~xj = 1 followed by s-j 0s followed by r digits where k-th next digit equals 1 if ~xj appears in clause C_k, 0 otherwise. . For j = 1,...,r, number Cj = 1 followed by r-j 0s and number C'j = 2 followed by r-j 0s. . Target t = s 1s followed by r 4s. Clearly, this can be constructed in polytime. Example of reduction for F = (x1 \/ ~x2 \/ ~x4) /\ (x2 \/ ~x3 \/ x1) /\ (~x3 \/ x4 \/ ~x2): S = { x1 = 1000110, ~x1 = 1000000, x2 = 0100010, ~x2 = 0100101, x3 = 0010000, ~x3 = 0010011, x4 = 0001001, ~x4 = 0001100, C1 = 0000100, C'1 = 0000200, C2 = 0000010, C'2 = 0000020, C3 = 0000001, C'3 = 0000002 } t = 1111444 Note: slightly different from book to ensure S contains all distinct numbers (book's reduction constructs S with repeated numbers, so S not really a set). If F is satisfiable, then there is a setting of variables such that each clause of F contains at least one true literal. Consider the subset S' = {numbers that correspond to true literals}. By construction, \sum_{x (- S'} x = s 1s followed by r digits, each one of which is either 1, 2, or 3 (because each clause contains at least one true literal). This means it is possible to add suitable numbers from {C1,C'1,...,Cr,C'r} so that the last r digits of the sum are equal to 4, i.e., there is a subset S' such that \sum_{x (- S'} x = t. If there is a subset S' of S such that \sum_{x (- S'} x = t, then S' must contain exactly one of {xj,~xj} for j = 1,...,n, because that is the only way for the numbers in S' to add to the target (with a 1 in the first s digits). Then, F is satisfied by setting each variable according to the numbers in S': for each clause j, the corresponding digit in the target is equal to 4 but the numbers Cj and C'j together only add up to 3 in that digit; this means that the selection of numbers in S' must include some literal with a 1 in that digit, i.e., clause j contains at least one true literal. ----------------- Self-reducibility ----------------- Note: This material is not in the textbook. This summary will therefore be slightly more detailed than usual. Problem of deciding language A sometimes called "decision problem": given input x, solution = yes/no answer. But many problems are more naturally "search problems": given input x, find solution y. Examples: - Given prop. formula F, find satisfying assignment, if one exists. - Given graph G, integer k, find a clique of size k in G, if one exists. - Given graph G, find a Ham. path in G, if one exists. - Given set of numbers S, target t, find subset of S whose sum equals t, if one exists. - etc. Many languages come from natural search problems. Clearly, efficient solution to search problem would give efficient solution to corresponding decision problem. So proof that decision problem is NP-hard implies that search problem is "hard" as well, and does not have efficient solution. But exactly how much more difficult are search problems? Perhaps surprisingly, many (but not all) are only polynomially more difficult than corresponding decision problem, in the following sense: any efficient solution to the decision problem can be used to solve the search problem efficiently. This is called "self-reducibility". Example 1: CLIQUE-SEARCH Input: Undirected graph G, positive integer k. Output: A clique of size k in G, if one exists (special value None if there is no such clique in G). - Idea: For each vertex in turn, remove it iff resulting graph still contains a k-clique. - Details: Assume we have an algorithm CL(G,k) that returns true iff G contains a clique of size k. We construct an algorithm to solve CLIQUE-SEARCH as follows. CLS(G,k): if not CL(G,k): return None # no k-clique in G for each vertex v (- V: # remove v and its incident edges G' = G - v # i.e., V' = V - {v} and E' = E - { (u,v) : u (- V } # if there is still a k-clique, v not required so leave it out if CL(G',k): G = G' return V - Correctness: CL(G,k) remains true at every iteration so at the end, V contains every vertex in a k-clique of G. At the same time, every other vertex will be taken out because it is not required, so V will contain no other vertex. Hence, the value returned is a k-clique of G. - Runtime: Each vertex of G examined once, and one call to CL for each one, plus linear amount of additional work (removing edges). Total is O(n*t(n,m) + n*(n+m)) where t(n,m) is runtime of CL on graphs with n vertices and m edges; this is polytime if t(n,m) is polytime. - What happens if G contains more than one k-clique? Algorithm will remove vertices from all but the "last" k-clique, depending on the order that vertices are processed. General technique to prove self-reducibility: - assume existence of algorithm to solve decision problem, - write algorithm to solve search problem by making calls to decision problem algorithm (possibly many calls on many different inputs), - argue search algorithm is correct, - make sure that search problem algorithm runs in polytime if decision problem algorithm does -- argue at most polynomially many calls to subroutine are made and at most polytime spent outside those calls. WARNING! The argument for self-reducibility is NOT that "any algorithm that solves the decision problem must include a part that solves the search problem", as this is in fact not always true. For example, there is an algorithm that can determine whether or not an integer has any factors (i.e., whether or not it belongs to COMPOSITES) without actually finding any of the factors (through some fairly involved number theory about properties of prime numbers). Example 2: HAMPATH-SEARCH Input: Graph G. Output: A Ham. path in G. - Idea 1: For each vertex in turn, remove it iff resulting graph still contains a Ham. path. - Problem: Every vertex must be in the path anyway, and this does not say where to put each vertex (which edges to use to travel through this vertex). - Idea 2: For each edge in turn, remove it iff resulting graph still contains a Ham. path -- same as for CLIQUE above, including the arguments for correctness and runtime, except considering edges one-by-one instead of vertices. Example 3: VERTEX-COVER-SEARCH Input: Graph G, integer k. Output: A vertex cover of size k, if one exists (None otherwise). - Idea 1: Remove vertices one-by-one as long as resulting graph still contains a vertex cover of size k. - Problem: If G contains a VC of size k, then G-v (remove v and all incident edges) also contains a VC of size k, whether or not v is in the cover (unless k=n, trivial to solve)! - Idea 2: Check if G-v contains a VC of size (k-1). - Algorithm: VCS(G,k): if not VC(G,k): return None C = {} # the vertices in a vertex cover of G for each vertex v (- V, and while k > 0: if VC(G-v, k-1): C = C u {v}; G = G - v; k = k - 1 return C - Correctness: Loop invariant: G contains a VC of size k. At each iteration, . if G-v contains a VC of size k-1 (C), then G contains a VC of size k that includes v (C u {v}); . if G contains a VC of size k that includes v (C), then G-v contains a VC of size k-1 (C - {v}) -- so by contrapositive: if G-v does not contain a VC of size k-1, then v does not belong to any VC of size k in G). - Runtime: O(n*t(n,m) + n*(n+m)) -- for each vertex v, we perform one call to VC in time t(n,m) and compute G-v in time O(n+m). Optimization problems: - Some search problems with one or more numerical parameters naturally occur in practice in the form of optimization problems, e.g., . MAX-CLIQUE . MIN-VERTEX-COVER . etc. As in the case of vertex cover above, optimization problem can also be self-reducible by using decision problem algorithm to find optimal value of relevant parameter(s). Example: MAX-CLIQUE Idea: Given G, perform binary search in range [1,n] by making calls to CL(G,k) for various values of k, in order to find maximum value k such that G contains a k-clique but no (k+1)-clique. Then, use search algorithm to find k-clique. (Note: Linear search for value of k would also be OK in this case, because search is in the range [1,n] and input size = n.) Similar idea would work for MIN-VERTEX-COVER, etc.