N P-Hard and N P-Complete Problems
N P-Hard and N P-Complete Problems
Basic concepts
• Solvability of algorithms
– There are algorithms for which there is no known solution, for example, Turing’s Halting Problem
∗ Decision problem
∗ Given an arbitrary deterministic algorithm A and a finite input I
∗ Will A with input I ever terminate, or enter an infinite loop?
∗ Alan Turing proved that a general algorithm to solve the halting problem for all possible program-input pairs
cannot exist
– Halting problem cannot be solved by any computer, no matter how much time is provided
∗ In algorithmic terms, there is no algorithm of any complexity to solve this problem
• Efficient algorithms
– Efficiency measured in terms of speed
– For some problems, there is no known efficient solution
– Distinction between problems that can be solved in polynomial time and problems for which no polynomial time
algorithm is known
• Problems classified to belong to one of the two groups
1. Problems with solution times bound by a polynomial of a small degree
– Most searching and sorting algorithms
– Also called tractable algorithms
– For example, ordered search (O(lg n)), polynomial evaluation (O(n)), sorting (O(n log n))
2. Problems with best known algorithms not bound by a polynomial
– Hard, or intractable, problems
– Traveling salesperson (O(n2 2n )), knapsack (O(2n/2 ))
– None of the problems in this group has been solved by any polynomial time algorithm
– N P-complete problems
∗ No efficient algorithm for an N P-complete problem has ever been found; but nobody has been able to
prove that such as algorithm does not exist
– P=6 NP
∗ Famous open problem in Computer Science since 1971
• Theory of N P-completeness
– Show that many of the problems with no polynomial time algorithms are computationally related
– The group of problems is further subdivided into two classes
N P-complete. A problem that is N P-complete can be solved in polynomial time iff all other N P-complete
problems can also be solved in polynomial time
N P-hard. If an N P-hard problem can be solved in polynomial time then all N P-complete problems can also be
solved in polynomial time
– All N P-complete problems are N P-hard but some N P-hard problems are known not to be N P-complete
N P-complete ⊂ N P-hard
• P vs N P problems
– The problems in class P can be solved in O(N k ) time, for some constant k (polynomial time)
NP-Hard and NP-Complete Problems 2
Definition 1 Any problem for which the answer is either zero or one is called a decision problem. An algorithm for a
decision problem is termed a decision algorithm.
Definition 2 Any problem that involves the identification of an optimal (either minimum or maximum) value of a given
cost function is known as an optimization problem. An optimization algorithm is used to solve an optimization problem.
NP-Hard and NP-Complete Problems 3
– Optimization problems
∗ Each feasible solution has an associated value; the goal is to find a feasible solution with the best value
∗ SHORTEST PATH problem
· Given an undirected graph G and vertics u and v
· Find a path from u to v that uses the fewest edges
· Single-pair shortest-path problem in an undirected, unweighted graph
– Decision problems
∗ The problem gives an answer as “yes” or “no”
∗ Decision problem is assumed to be easier (or no harder) to solve compared to the optimization problem
∗ Decision problem can be solved in polynomial time if and only if the corresponding optimization problem can
· If the decision problem cannot be solved in polynomial time, the optimization problem cannot be solved
in polynomial time either
– N P-complete problems confined to the realm of decision problems
∗ Cast an optimization problem as a related decision problem by imposing a bound on the value to be optimized
∗ PATH problem as related to SHORTEST PATH problem
· Given a directed graph G, vertices u and v, and an integer k, is there a path from u to v with at most k
edges?
∗ Relationship between an optimization problem and its related decision problem
· Try to show that the optimization problem is “hard”
· Or that the decision problem is “easier” or “no harder”
· We can solve PATH by solving SHORTEST PATH and then comparing the number of edges to k
· If an optimization problem is easy, its decision problem is easy as well
· In NP-completeness, if we can provide evidence that a decision problem is hard, we can also provide
evidence that its related optimization problem is hard
– Reductions
∗ Showing that one problem is no harder or no easier than another also applicable when both problems are
decision problems
∗ N P-completeness proof – general steps
· Consider a decision problem A; we’ll like to solve it in polynomial time
· Instance: input to a particular problem; for example, in PATH, an instance is a particular graph G, two
particular variables u and v in G, and a particular integer k
· Suppose that we know how to solve a different decision problem B in polynomial time
· Suppose that we have a procedure that transforms any instance α of A into some instance β of B with
following characteristics:
Transformation take polynomial time
Both answers are the same; the answer for α is a “yes” iff the answer for β is a “yes”
∗ The above procedure is called a polynomial time reduction algorithm and provides us a way to solve problem
A in polynomial time
1. Given an instance α of A, use a polynomial-time reduction algorithm to transform it to an instance β of B
2. Run polynomial-time decision algorithm for B on instance β
3. Use the answer for β as the answer for α
∗ Using polynomial-time reductions to show that no polynomial-time algorithm can exist for a particular problem
B
· Suppose we have a decision problem A for which we already know that no polynomial-time algorithm can
exist
· Suppose that we have a polynomialtime reduction transforming instances of A to instances of B
· Simple proof that no polynomial-time algorithm can exist for B
NP-Hard and NP-Complete Problems 4
• Nondeterministic algorithms
– Deterministic algorithms
∗ Algorithms with uniquely defined results
∗ Predictable in terms of output for a certain input
– Nondeterministic algorithms are allowed to contain operations whose outcomes are limited to a given set of possi-
bilities instead of being uniquely defined
– Specified with the help of three new O(1) functions
1. choice ( S )
∗ Arbitrarily chooses one of the elements of set S
∗ x = choice(1,n) can result in x being assigned any of the integers in the range [1, n], in a completely
arbitrary manner
∗ No rule to specify how this choice is to be made
2. failure()
∗ Signals unsuccessful completion of a computation
∗ Cannot be used as a return value
3. success()
∗ Signals successful completion of a computation
∗ Cannot be used as a return value
∗ If there is a set of choices that leads to a successful completion, then one choice from this set must be made
– A nondeterministic algorithm terminates unsuccessfully iff there exist no set of choices leading to a success signal
– A machine capable of executing a nondeterministic algorithm as above is called a nondeterministic machine
– Nondeterministic search of x in an unordered array A with n ≥ 1 elements
∗ Determine an index j such that A[j] = x or j = −1 if x 6∈ A
algorithm nd_search ( A, n, x )
{
// Non-deterministic search
// Input: A: Array to be searched
// Input: n: Number of elements in A
// Input: x: Item to be searched for
// Output: Returns -1 if item does not exist, index of item otherwise
algorithm nd_sort ( A, n )
{
NP-Hard and NP-Complete Problems 5
if ( B[j] != 0 ) failure();
B[j] = A[i];
}
// Verify order
write ( B );
success();
}
– Complexity of nd_sort is Θ(n)
∗ Best-known deterministic sorting algorithm has a complexity of Ω(n lg n)
– Deterministic interpretation of nondeterministic algorithm
∗ Possible by allowing unbounded parallelism in computation
∗ Imagine making n copies of the search instance above, all running in parallel and searching at different index
values for x
· The first copy to reach success() terminates all other copies
· If a copy reaches failure(), only that copy is terminated
∗ In abstract terms, nondeterministic machine has the capability to recognize the correct solution from a set of
allowable choices, without making copies of the program
• Possible to construct nondeterministic algorithms for many different choice sequences leading to successful completions
(see nd_sort)
– If the numbers in A are not unique, many different permutations will result into sorted sequence
– We’ll limit ourselves to problems that result in a unique output, or decision algorithms
∗ A decision algorithm will output 0 or 1
∗ Implicit in the signals success() and failure()
– Output from a decision algorithm is uniquely defined by input parameters and algorithm specification
• An optimization problem may have many feasible solutions
– The problem is to find out the feasible solution with the best associated value
– N P-completeness applies directly not to optimization problems but to decision problems
– Corresponding decision problem is to determine whether G has a clique of size at least k for some given k
– Let us denote the deterministic decision algorithm for the clique decision problem as dclique(G, k)
– If |V | = n, the size of a maximal clique can be found by
for ( k = n; dclique ( G, k ) != 1; k-- );
– If time complexity of dclique is f (n), size of maximal clique can be found in time g(n) ≤ nf (n)
∗ Decision problem can be solved in time g(n)
– Maximal clique problem can be solved in polynomial time iff the clique decision problem can be solved in polyno-
mial time
• Example: 0/1 knapsack
P P
– Is there a 0/1 assignment of values to xi , 1 ≤ i ≤ n, such that pi xi ≥ r and wi xi ≤ m, for given m and r,
and nonnegative pi and wi
– If the knapsack decision problem cannot be solved in deterministic polynomial time, then the optimization problem
cannot either
• Comment on uniform parameter n to measure complexity
– If this decision problem cannot be solved by an algorithm of complexity p(n) for some polynomial p(), then it
cannot be solved by an algorithm of complexity p(|V |)
• 0/1 knapsack
– Input size q (q > n) for knapsack decision problem is
X
q= (blog2 pi c + blog2 wi c) + 2n + blog2 mc + blog2 rc + 2
1≤i≤n
P P
– If the input is given in unary notation, then input size s = pi + wi + m + r
– Knapsack decision and optimization problems can be solved in time p(s) for some polynomial p() (dynamic pro-
gramming algorithm)
– However, there is no known algorithm with complexity O(p(n)) for some polynomial p()
Definition 3 The time required by a nondeterministic algorithm performing on any given input is the minimum number
of steps needed to reach a successful completion if there exists a sequence of choices leading to such a completion. In
case successful completion is not possible, then the time required is O(1). A nondeterministic algorithm is of complexity
O(f (n)) if for all inputs of size n, n ≥ n0 , that result in a successful completion, the time required is at most cf (n) for
some constants c and n0 .
if bit m in s is 1
write ( "A subset sums to m" );
else
write ( "No subset sums to m" );
}
∗ Bits are numbered from 0 to m from right to left
∗ Bit i will be 0 if and only if no subsets of A[j], 1 ≤ j ≤ n sums to i
∗ Bit 0 is always 1 and bits are numbered 0, 1, 2, . . . , m right to left
∗ Number of steps for this algorithm is O(n)
∗ Each step moves m + 1 bits of data and would take O(m) time on a conventional computer
∗ Assuming one unit of time for each basic operation for a fixed word size, the complexity of deterministic
algorithm is O(nm)
W = 0;
P = 0;
for ( i = 1; i <= n; i++ )
{
x[i] = choice ( 0, 1 );
W += x[i] * w[i];
P += x[i] * p[i];
}
if ( ( W > m ) || ( P < r ) )
failure();
else
success();
}
– The for loop selects or discards each of the n items
– It also recomputes the total weight and profit coresponding to the selection
– The if statement checks to see the feasibility of assignment and whether the profit is above a lower bound r
– The time complexity of the algorithm is O(n)
– If the input length is q in binary, time complexity is O(q)
• Maximal clique
– Nondeterministic algorithm for clique decision problem
– Begin by trying to form a set of k distinct vertices
– Test to see if they form a complete subgraph
• Satisfiability
– Let x1 , x2 , . . . denote a set of boolean variables
– Let x̄i denote the complement of xi
– A variable or its complement is called a literal
– A formula in propositional calculus is an expression that is constructed by connecting literals using the operations
and (∧) and or (∨)
– Examples of formulas in propositional calculus
∗ (x1 ∧ x2 ) ∨ (x3 ∧ x¯4 )
∗ (x3 ∨ x¯4 ) ∧ (x1 ∨ x¯2 )
– Conjunctive normal form (CNF)
∗ A formula is in CNF iff it is represented as ∧ki=1 ci , where ci are clauses represented as ∨lij ; lij are literals
– Disjunctive normal form (DNF)
∗ A formula is in DNF iff it is represented as ∨ki=1 ci , where ci are clauses represented as ∧lij
NP-Hard and NP-Complete Problems 9
– Satisfiability problem is to determine whether a formula is true for some assignment of truth values to the variables
∗ CNF-satisfiability is the satisfiability problem for CNF formulas
– Polynomial time nondeterministic algorithm that terminates successfully iff a given propositional formula E(x1 , . . . , xn )
is satisfiable
∗ Nondeterministically choose one of the 2n possible assignments of truth values to (x1 , . . . , xn )
∗ Verify that E(x1 , . . . , xn ) is true for that assignment
algorithm eval ( E, n )
{
// Determine whether the propositional formula E is satisfiable.
// Variable are x1, x2, ..., xn
if ( E ( x1, ..., xn ) )
success();
else
failure();
}
∗ The nondeterministic time to choose the truth value is O(n)
∗ The deterministic evaluation of the assignment is also done in O(n) time
• The classes N P-hard and N P-complete
– Polynomial complexity
∗ An algorithm A is of polynomial complexity if there exists a polynomial p() such that the computation time of
A is O(p(n)) for every input of size n
Definition 4 P is the set of all decision problems solvable by deterministic algorithms in polynomial time. N P is
the set of all decision problems solvable by nondeterministic algorithms in polynomial time.
– Since deterministic algorithms are a special case of nondeterministic algorithms, P ⊆ N P
– An unsolved problem in computer science is: Is P = N P or is P =
6 N P?
– Cook formulated the following question: Is there any single problem in N P such that if we showed it to be in P,
then that would imply that P = N P? This led to Cook’s theorem as follows:
Theorem 1 Satisfiability is in P if and only if P = N P.
• Reducibility
– Show that one problem is no harder or no easier than another, even when both problems are decision problems
Definition 5 Let A and B be problems. Problem A reduces to B (written as A ∝ B) if and only if there is a way to
solve A by a deterministic polynomial time algorithm using a deterministic algorithm that solves B in polynomial
time.
∗ If we have a polynomial time algorithm for B, then we can solve A in polynomial time
∗ Reducibility is transitive
· A∝B∧B ∝C ⇒A∝C
Definition 6 Given two sets A and B ∈ N and a set of functions F : N → N , closed under composition, A is
called reducible to B (A ∝ B) if and only if
∃f ∈ F | ∀x ∈ N , x ∈ A ⇔ f (x) ∈ B
NP-Hard and NP-Complete Problems 10
– Procedure is called polynomial-time reduction algorithm and it provides us with a way to solve problem A in
polynomial time
∗ Also known as Turing reduction
∗ Given an instance α of A, use a polynomial-time reduction algorithm to transform it to an instance β of B
∗ Run the polynomial-time decision algorithm on instance β of B
∗ Use the answer of β as the answer for α
∗ Reduction from squaring to multiplication
· All we know is to add, subtract, and take squares
· Product of two numbers is computed by
2 × a × b = (a + b)2 − a2 − b2
· Reduction in the other direction: if we can multiply two numbers, we can square a number
∗ Computing (x + 1)2 from x2
· For efficiency sake, we want to avoid multiplication
∗ Turing reductions compute the solution to one problem, assuming the other problem is easy to solve
– Polynomial-time many-one reduction
∗ Converts instances of a decision problem A into instances of a decision problem B
∗ Written as A ≤m B; A is many-one reducible to B
∗ If we have an algorithm N which solves instances of B, we can use it to solve instances of A in
· Time needed for N plus the time needed for reduction
· Maximum of space needed for N and the space needed for reduction
∗ Formally, suppose A and B are formal languages over the alphabets Σ and Γ
· A many-one reduction from A to B is a total computable function f : Σ∗ → Γ∗ with the property
ω ∈ A ⇔ f (ω) ∈ B, ∀ω ∈ Σ∗
∀s ∈ S ∀A ∈ N A ≤ S ⇔ A ∈ S
· Most well-studied complexity classes are closed under some type of many-one reducibility, including P
and N P
∗ Square to multiplication reduction, again
· Add the restriction that we can only use square function one time, and only at the end
· Even if we are allowed to use all the basic arithmetic operations, including √multiplication, no reduction
exists in general, because we may have to compute an irrational number like 2 from rational numbers
· Going in the other direction, however, we can certainly square a number with just one multiplication, only
at the end
· Using this limited form of reduction, we have shown the unsurprising result that multiplication is harder in
general than squaring
∗ Many-one reductions map instances of one problem to instances of another
· Many-one reduction is weaker than Turing reduction
· Weaker reductions are more effective at separating problems, but they have less power, making reductions
harder to design
– Use polynomial-time reductions in opposite way to show that a problem is N P-complete
NP-Hard and NP-Complete Problems 11
∗ Use polynomial-time reduction to show that no polynomial-time algorithm can exist for problem B
∗ A ⊂ N is called hard for S if
∀s ∈ S s ≤ A
A ⊂ N is called complete for S if A is hard for S and A is in S
∗ Proof by contradiction
· Assume that a known problem A is hard to solve
· Given a new problem B, similar to A
· Assume that B is solvable in polynomial time
· Show that every instance of problem A can be solved in polynomial time by reducing it to problem B
· Contradiction
– Cannot assume that there is absolutely no polynomial-time algorithm for A
Definition 7 A problem A is N P-hard if and only if satisfiability reduces to A (satisfiability ∝ A). A problem A is
N P-complete if and only if A is N P-hard and A ∈ N P.
Definition 8 Two problems A and B are said to be polynomially equivalent if and only if A ∝ B and B ∝ A.
– To show that a problem B is N P-hard, it is adequate to show that A ∝ B, where A is some problem already known
to be N P-hard
– Since ∝ is a transitive relation, it follows that if satisfiability ∝ A and A ∝ B, then satisfiability ∝ B
– To show that an N P-hard decision problem is N P-complete, we have just to exhibit a polynomial time nondeter-
ministic algorithm for it
Polynomial time
• Problems that can be solved in polynomial time are regarded as tractable problems
NP-Hard and NP-Complete Problems 12