UNIT-8 NP-Hard and NP-Complete Problems: The Complexity Class P
UNIT-8 NP-Hard and NP-Complete Problems: The Complexity Class P
Definition We say that algorithm M decides the language A (resp. computes the function F ) in polynomial time, if there exists an integer k 1, such that the running time of M is O(nk ), for any input string of length n. It follows from Theorem model of computation$ .1.! that this notion of "polynomial time# does not depend on the
Theorem Consider the models of computation Java program, k -tape Turing machine, and one-tape Turing machine. If a language can be decided resp. a function can be computed! in polynomial time in one of these models, then it can be decided resp. computed! in polynomial time in all of these models. %ecause of this theorem, we can define the following two complexity classes$ P $& {A $ the language A is decida'le in polynomial time}, and (P $& {F $ the function F is computa'le in polynomial time}.
)ome examples
Palindromes *et "al 'e the language "al $& {w {a, b} $ w is a palindrome}. We ha+e seen that there exists a one,tape Turing machine that decides "al in O(n! ) time. Therefore, "al P.
T#$ C%&"'$(IT) C'*++ P ' r r ' r G1 ' (igure $ The graph G1 is ,-colorable- r stands for red- ' stands for blue. The graph G! is not ,-colorable. ' G!
)ome functions in (P The following functions are in the class (P$ F1 $ .- .- defined 'y F1 (x) $& x / 1,
! F! $ . .- defined 'y F! (x, y ) $& x / y , ! F0 $ . .- defined 'y F0 (x, y ) $& xy .
1ontext,free languages We ha+e shown in )ection 22 that e+ery context,free language is decida'le. The algorithm presented there, howe+er, does not run in polynomial time. In 134P 05-6, the following result will 'e shown (using a techni7ue called dynamic programming )$ Theorem $ 'et 8 be an alphabet, and let A 8 be a conte.t-free language. Then A P. 3'ser+e that, o'+iously, e+ery language in P is decida'le. The !,coloring pro'lem *et G 'e a graph with +ertex set V and edge set E . We say that G is ,-colorable, if it is possi'le to gi+e each +ertex of V a color such that 1. for each edge (u, v ) E , the +ertices u and v ha+e different colors, and !. only two colors are used to color all +ertices. We define the following language$ !Color $& {hGi $ the graph G is !, colora'le}, where hGi denotes the 'inary string that encodes the graph G. We claim that !Color P. In order to show this, we ha+e to construct an algorithm that decides in polynomial time, whether or not any gi+en graph is !,colora'le.
*et G 'e an ar'itrary graph with +ertex set V & {1, !, . . . , m}. The edge set of G is gi+en 'y an ad/acency matri.. This matrix, which we denote 'y E , is a two,dimensional array with m rows and m columns. (or all i and j with 1 i m and 1 j m, we ha+e 9 1 if (i, j ) is an edge of G, E (i, j ) & - otherwise. The length of the input G, i.e., the num'er of 'its needed to specify G, is e7ual to m! &$ n. We will present an algorithm that decides, in O(n) time, whether or not the graph G is !,colora'le. The algorithm uses the colors red and 'lue. It gi+es the first +ertex the color red. Then, the algorithm considers all +ertices that are connected 'y an edge to the first +ertex, and colors them 'lue. .ow the algorithm is done with the first +ertex: it mar;s this first +ertex. .ext, the algorithm chooses a +ertex i that already has a color, 'ut that has not 'een mar;ed. Then it considers all +ertices j that are connected 'y an edge to i. If j has the same color as i, then the input graph G is not !,colora'le. 3therwise, if +ertex j does not ha+e a color yet, the algorithm gi+es j the color that is different from i<s color. =fter ha+ing done this for all neigh'ors j of i, the algorithm is done with +ertex i, so it mar;s i. It may happen that there is no +ertex i that already has a color 'ut that has not 'een mar;ed. (In other words, each +ertex i that is not mar;ed does not ha+e a color yet.) In this case, the algorithm chooses an ar'itrary +ertex i ha+ing this property, and colors it red. (This +ertex i is the first +ertex in its connected component that gets a color.) This procedure is repeated until all +ertices of G ha+e 'een mar;ed. We now gi+e a formal description of this algorithm. >ertex i has 'een marked, if 1. i has a color, !. all +ertices that are connected 'y an edge to i ha+e a color, and 0. the algorithm has +erified that each +ertex that is connected 'y an edge to i has a color different from i<s color. The algorithm uses two arrays ! (1 . . . m) and a(1 . . . m), and a +aria'le M . The +alue of ! (i) is e7ual to the color (red or 'lue) of +ertex i: if i does not ha+e a color yet, then ! (i) & -. The +alue of a(i) is e7ual to 9 a(i) & 1 if +ertex i has 'een mar;ed, - otherwise. The +alue of M is e7ual to the num'er of mar;ed +ertices. The algorithm is presented in (igure .!. ?ou are encouraged to con+ince yourself of the correctness of this algorithm. That is, you should con+ince yourself that this algorithm returns ?@) if the graph G is !,colora'le, whereas it returns .3 otherwise. What is the running time of this algorithm2 (irst we count the num'er of iterations of the outer while,loop. In one iteration, either M increases 'y one, or a +ertex i, for which a(i) & -, gets the color red. In the latter case, the +aria'le M is increased during the next iteration of the outer while,loop. )ince, during the entire outer while,loop, the +alue of M is increased from Aero to m, it follows that there are at most !m iterations of the outer while,loop. (In fact, the num'er of iterations is e7ual to m plus the num'er of connected components of G minus one.) 3ne iteration of the outer while,loop ta;es O (m) time. Bence, the total running time of the algorithm is O(m! ), which is O (n). Therefore, we ha+e shown that !Color P.
=lgorithm !1olor for i $& 1 to m do f (i) $& -: a(i) $& - endfor: f (1) $& red: & $& -: while & & m do ( (ind the minimum index i for which +ertex i has not 'een mar;ed, 'ut has a color already ) bool $& false : i $& 1: while bool & false and i m do if a(i) & - and f (i) & - then bool $& true else i $& i / 1 endif: endwhile: ( If bool & true , then i is the smallest index such that a(i) & - and f (i) & -. If bool & false , then for all i, the following holds$ if a(i) & -, then f (i) & -: 'ecause & 0 m, there is at least one such i. ) if bool & true then for / $& 1 to m do if $ (i, / ) & 1 then if f (i) & f (/ ) then return .3 and terminate else if f (/ ) & then if f (i) & red then f (/ ) $& 'lue else f (/ ) $& red endif endif endif endif endfor: a(i) $& 1: & $& & / 1: else i $& 1: while a(i) & - do i $& i / 1 endwhile: ( an un+isited connected component starts at +ertex i ) f (i) $& red endif endwhile: return ?@)
@xample 1 *et G 'e a graph with +ertex set V and edge set E , and let k 1 'e an integer. We say that G is k ,colorable, if it is possi'le to gi+e each +ertex of V a color such that 1. for each edge (u, v ) E , the +ertices u and v ha+e different colors, and !. at most k different colors are used to color all +ertices. We define the following language$ k Color $& {hGi $ the graph G is k, colora'le}. We ha+e seen that for k & !, this pro'lem is in the class P. (or k 0, it is not ;nown whether there exists an algorithm that decides, in polynomial time, whether or not any gi+en graph is k ,colora'le. In other words, for k 0, it is not ;nown whether or not k Color is in the class P. @xample.! *et G 'e a graph with +ertex set V & {1, !, . . . , m} and edge set E . = #amilton cycle is a cycle in G that +isits each +ertex exactly once. (ormally, it is a se7uence v1 , v! , . . . , vm of +ertices such that 1. {v1 , v! , . . . , vm } & V , and
!. {(v1 , v! ), (v! , v0 ), . . . , (vm"1 , vm ), (vm , v1 )} E . We define the following language$ #C $& {hGi $ the graph G contains a Bamilton cycle}. It is not ;nown whether or not #C is in the class P. @xample.0 The sum of subset language is defined as follows$ +%+ $& {ha1 , a! , . . . , am , bi $ m, a1 , a! , . . . , am , b .and #$ {1, !, . . . , m}, iI ai & b}. P =lso in this case, no polynomial,time algorithm is ;nown that decides the language +%+ . That is, it is not ;nown whether or not +%+ is in the class P. @xample.6 =n integer x ! is a prime num'er, if there are no a, b . such that a & x, b & x, and x & ab. Bence, the language of all non,primes that are greater than or e7ual to two, is 2"rim $& {hxi $ x ! and x is not a prime num'er}. It is not o'+ious at all, whether or not 2"rim is in the class P. In fact, it was shown only in !--! that 2"rim is in the class P. 3'ser+ation The four languages above have the follo1ing in common3 If someone gives us a solution for any given input, then 1e can easily, i.e., in polynomial time, verify 1hether or not this solution is a correct solution. &oreover, for any input to each of these four problems, there e.ists a solution 1hose length is polynomial in the length of the input. *et us again consider the language k Color . *et G 'e a graph with +ertex set V & {1, !, . . . , m} and edge set E , and let k 'e a positi+e integer. We want to decide whether or not G is k , colora'le. = "solution# is a coloring of the nodes using at most k different colors. That is, a solution is a se7uence !1 , !! , . . . , !m . (Interpret this as$ +ertex i recei+es color !i , 1 i m). This se7uence is a correct solution if and only if 1. !i {1, !, . . . , k}, for all i with 1 !. for all i with 1 i i m, and j m, if (i, j ) E , then !i & !/ . m, and for all j with 1
If someone gi+es us this solution (i.e., the se7uence !1 , !! , . . . , !m ), then we can +erify in polyno, mial time whether or not these two conditions are satisfied. The length of this solution is O(m log k )$ for each i, we need a'out log k 'its to represent !i . Bence, the length of the solution is polynomial in the length of the input, i.e., it is polynomial in the num'er of 'its needed to represent the graph G and the num'er k . (or the Bamilton cycle pro'lem, a solution consists of a se7uence v1 , v! , . . . , vm of +ertices. This se7uence is a correct solution if and only if 1. {v1 , v! , . . . , vm } & {1, !, . . . , m} and !. {(v1 , v! ), (v! , v0 ), . . . , (vm"1 , vm ), (vm , v1 )} E . These two conditions can 'e +erified in polynomial time. 4oreo+er, the length of the solution is polynomial in the length of the input graph. 1onsider the sum of su'set pro'lem. correct solution if and only if = solution is a se7uence %1 , %! , . . . , %m . It is a
m, and
Bence, the set $ {1, !, . . . , m} in the definition of +%+ is the set of indices i for which %i & 1. =gain, these two conditions can 'e +erified in polynomial time, and the length of the solution is polynomial in the length of the input. (inally, let us consider the language 2"rim . *et x ! 'e an integer. The integers a and b form a "solution# for x if and only if 1. ! !. ! a & x, b & x, and
0. x & ab. 1learly, these three conditions can 'e +erified in polynomial time. 4oreo+er, the length of this solution, i.e., the total num'er of 'its in the 'inary representations of a and b, is polynomial in the num'er of 'its in the 'inary representation of x. *anguages ha+ing the property that the correctness of a proposed "solution# can 'e +erified in polynomial time, form the class .P$ Definition = language A 'elongs to the class .P, if there exist a polynomial ' and a language ( P, such that for e+ery string w, w A )* #+ $ ,+, '(,w,) and hw, +i (.
In words, a language A is in the class .P, if for e+ery string w, w A if and only if the following two conditions are satisfied$ 1. There is a "solution# +, whose length ,+, is polynomial in the length of w (i.e., ,+, where ' is a polynomial). '(,w,),
!. In polynomial time, we can +erify whether or not + is a correct "solution# for w (i.e., hw, +i ( and ( P). Bence, the language ( can 'e regarded to 'e the "+erification language#$ ( & {hw, +i $ + is a correct "solution# for w}. We ha+e gi+en already informal proofs of the fact that the languages k Color , #C , +%+ , and 2"rim are all contained in the class .P. %elow, we formally pro+e that 2"rim .P. To pro+e this claim, we ha+e to specify the polynomial ' and the language ( P. (irst, we o'ser+e that 2"rim & {hxi $ there exist a and b in . such that ! a & x, ! b & x and x & ab }. We define the polynomial ' 'y '(n) $& n / !, and the language ( as ( $& {hx, a, bi $ x !, ! a & x, ! ab}. b & x and x &
It is o'+ious that ( P$ (or any three positi+e integers x, a, and b, we can +erify in polynomial time whether or not hx, a, bi ( . In order to do this, we only ha+e to +erify whether or not x !, ! a & x, ! b & x, and x & ab. If all these four conditions are satisfied, then hx, a, bi ( . If at least one of them is not satisfied, then hx, a, bi - ( .
It remains to show that for all x .$ hxi 2"rim )* #a, b $ ,ha, bi, ,hxi, / ! and hx, a, bi (.
(Cemem'er that ,hxi, denotes the num'er of 'its in the 'inary representation of x: ,ha, bi, denotes the total num'er of 'its of a and b, i.e., ,ha, bi, & ,hai, / ,hbi,.) *et x 2"rim . It follows from ( .1) that there exist a and b in ., such that ! a & x, ! b & x, and x & ab. )ince x & ab ! . ! & 6 !, it follows that hx, a, bi ( . Bence, it remains to show that ,ha, bi, ,hxi, / !. The 'inary representation of x contains blog x% / 1 'its, i.e., ,hxi, & blog x% / 1. We ha+e ,ha, bi, & ,hai, / ,hbi, & (blog a% / 1) / (blog b% / 1) log a / log b / ! & log ab / ! & log x / ! blog x% / 0 & ,hxi, / !. This pro+es one direction of ( .!). To pro+e the other direction, we assume that there are positi+e integers a and b, such that ,ha, bi, ,hxi, / ! and hx, a, bi ( . Then it follows immediately from ( .1) and the definition of the language ( , that x 2"rim . Bence, we ha+e pro+ed the other direction of ( .!). This completes the proof of the claim that 2"rim .P.
P is contained in .P
Intuiti+ely, it is clear that P .P, 'ecause a language is in P, if for e+ery string w, it is possi'le to compute the "solution# + in polynomial time, in .P, if for e+ery string w and for any gi+en "solution# +, it is possi'le to verify in polynomial time whether or not + is a correct solution for w (hence, we do not need to compute the solution + oursel+es, we only ha+e to +erify it). We gi+e a formal proof of this$ Theorem $P .P. Proof. *et A P. We will pro+e that A .P. Define the polynomial ' 'y '(n) $& - for all n .- , and define ( $& {hw, /i $ w A}. )ince A P, the language ( is also contained in P. It is easy to see that w A )* #+ $ ,+, This completes the proof. '(,w,) & - and hw, +i (.
=lgorithm .onPrime ( decides whether or not hxi 2"rim ) if x & - or x & 1 or x & ! then return .3 and terminate else a $& !: while a & x do if x mod a & then return ?@) and terminate else a $& a / 1 endif endwhile: return .3 endif (igure .0$ *n algorithm that decides 1hether or not a number x is contained in the language 2"rim .
Bow do we decide whether or not any gi+en string w 'elongs to the language A2 If we can find a string + that satisfies the right,hand side in ( .0), then we ;now that w A. 3n the other hand, if there is no such string +, then w - A. Bow much time do we need to decide whether or not such a string + exists2 (or example, let A 'e the language 2"rim , and let x .. The algorithm in (igure .0 decides whether or not hxi 2"rim . It is clear that this algorithm is correct. *et n 'e the length of the 'inary representation of x, i.e., n & blog x% / 1. If x 0 ! and x is a prime num'er, then the while,loop ma;es x " ! iterations. Therefore, since n " 1 & blog x% log x, the running time of this algorithm is at least x " ! !n"1 " !, i.e., it is at least e.ponential in the length of the input. We now pro+e that e+ery language in .P can 'e decided in exponential time. *et A 'e an ar'itrary language in .P. *et ' 'e the polynomial, and let ( P 'e the language such that for all strings w, w A )* #+ $ ,+, '(,w,) and hw, +i (. ( .6) The following algorithm decides, for any gi+en string w, whether or not w A. It does so 'y loo;ing at all possi'le strings + for which ,+, '(,w,)$ for all + with ,+, '(,w,) do if hw, +i ( then return ?@) and terminate endif endfor: return .3
The correctness of the algorithm follows from ( .6). What is the running time2 We assume that w and + are represented as 'inary strings. *et n 'e the length of the input, i.e., n & ,w,. Bow many 'inary strings + are there whose length is at most '(,w,)2 =ny such + can 'e descri'ed 'y a se7uence of length '(,w,) & '(n), consisting of the sym'ols "-#, "1#, and the 'lan; sym'ol. Bence, there are at most 0p(n) many 'inary strings + with ,+, '(n). Therefore, the p(n) for,loop ma;es at most 0 iterations. )ince ( P, there is an algorithm and a polynomial 1 , such that this algorithm, when gi+en any input string 2 , decides in 1 (,2,) time, whether or not 2 ( . This input 2 has the form hw, +i, and we ha+e ,2, & ,w, / ,+, ,w, / '(,w,) & n / '(n). It follows that the total running time of our algorithm that decides whether or not w A, is 'ounded from a'o+e 'y 0p(n) . 1 (n / '(n)) !!p(n) . 1 (n / '(n)) !!p(n) . !4(n/p(n)) & !p5 (n) ,
where '3 is the polynomial that is defined 'y '3 (n) $& !'(n) / 1 (n / '(n)). If we define the class @DP as @DP $& {A $ there exists a polynomial ', such that A can 'e decided in time !p(n) } , then we ha+e pro+ed the following theorem. Theorem $ .P @DP.
)ummary
P .P. It is not ;nown whether P is a proper su'class of .P, or whether P & .P. This is one of the most important open pro'lems in computer science. If you can sol+e this pro'lem, then you will get one million dollars: not from us, 'ut from the 1lay 4athematics Institute, see http://www.claymath.org/prizeproblems/index.htm 4ost people 'elie+e that P is a proper su'class of .P. .P @DP, i.e., each language in .P can 'e decided in exponential time. It is not ;nown whether .P is a proper su'class of @DP, or whether .P & @DP. It follows from P .P and .P @DP, that P @DP. It can 'e shown that P is a proper su'set of @DP, i.e., there exist languages that can 'e decided in exponential time, 'ut that cannot 'e decided in polynomial time. P is the class of those languages that can 'e decided efficiently, i.e., in polynomial time. )ets that are not in P, are not efficiently decida'le.
.on,deterministic algorithms
The a''re+iation .P stands for .on,deterministic Polynomial time. The algorithms that we ha+e considered so far are deterministic, which means that at any time during the computation, the next computation step is uni7uely determined. In a non-deterministic algorithm, there are one or more possi'ilities for 'eing the next computation step, and the algorithm chooses one of them.
To gi+e an example, we consider the language +%+ , see @xample am , and b 'e elements of .- . Then ha1 , a! , . . . , am , bi +%+
.0.0. *et m, a1 , a! , . . . ,
The following non,deterministic algorithm decides the language +%+ $ =lgorithm )3)(m, a1 , a! , . . . , am , b)$ + $& -: for i $& 1 to m do + $& + , + $& + / ai endfor: if + & b then return ?@) else return .3 endif The line means that either executed. Pm*et us assume that ha1 , a! , . . . , am , bi +%+ . Then there are %1 , %! , . . . , %m {-, 1} such that i m$ In the i,th i&1 %i ai & b. =ssume our algorithm does the following, for each i with 1 iteration, if %i & -, then it executes the instruction "+ $& +#, if %i & 1, then it executes the instruction "+ $& + / ai #. Then after the for,loop, we ha+e + & b, and the algorithm returns ?@): hence, the algorithm has correctly found out that ha1 , a! , . . . , am , bi +%+ . In other words, in this case, there e.ists at least one accepting computation. 3n the other hand, if ha1 , a! , . . . , am , bi - +%+ , then the algorithm always returns .3, no matter which of the two instructions is executed in each iteration of the for,loop. In this case, there is no accepting computation. Definition *et M 'e a non,deterministic algorithm. We say that M accepts a string w, if there exists at least one computation that, on input w, returns ?@). Definition We say that a non,deterministic algorithm M decides a language A in time 4 , if for e+ery string w, the following holds$ w A if and only if there exists at least one computation that, on input w, returns ?@) and that ta;es at most 4 (,w,) time. The non,deterministic algorithm that we ha+e seen a'o+e decides the language +%+ in linear time$ *et ha1 , a! , . . . , am , bi +%+ , and let n 'e the length of this input. Then n & ,ha1 i, / ,ha! i, / . . . / ,ham i, / ,hbi, m. (or this input, there is a computation that returns ?@) and that ta;es O (m) & O (n) time. we define the notion of polynomial time for non-deterministic algorithms.. + $& + , + $& + / ai the instruction "+ $& +# or the instruction "+ $& + / ai # is