0% found this document useful (0 votes)
19 views24 pages

NP Completeness

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views24 pages

NP Completeness

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

P, NP, NP-Completeness

General Problems, Input Size and


Time Complexity
• Time complexity of algorithms :
polynomial time algorithm ("efficient algorithm") v.s.
exponential time algorithm ("inefficient algorithm")
f(n) \ n 10 30 50

n 0.00001 sec 0.00003 sec 0.00005 sec

n5 0.1 sec 24.3 sec 5.2 mins

2n 0.001 sec 17.9 mins 35.7 yrs


“Hard” and “easy’ Problems
• Sometimes the dividing line between “easy” and “hard”
problems is a fine one. For example
– Find the shortest path in a graph from X to Y. (easy)
– Find the longest path in a graph from X to Y.(with no cycles)
(hard)
• View another way – as “yes/no” problems
– Is there a simple path from X to Y with weight <= M? (easy)
– Is there a simple path from X to Y with weight >= M? (hard)
– First problem can be solved in polynomial time.
– All known algorithms for the second problem (could) take
exponential time .
• Decision problem: The solution to the
problem is "yes" or "no". Most optimization
problems can be phrased as decision
problems (still have the same time
complexity).

Example :
Assume we have a decision algorithm X for
0/1 Knapsack problem with capacity M,
i.e. Algorithm X returns “Yes” or “No” to the
question
“Is there a solution with profit ≥ P subject to
knapsack capacity ≤ M?”
We can repeatedly run algorithm X for
various profits(P values) to find an optimal
solution.
Example : Use binary search to get the
optimal profit,
maximum of lg ∑pi runs.
(where M is the capacity of the knapsack optimization problem)

Min Bound Optimal Profit Max Bound


Search for the optimal solution
0
∑pi
|___________________|_________________|
Optimization & Decision Problems
• Decision problems
– Given an input and a question regarding a
problem, determine if the answer is yes or no
• Optimization problems
– Find a solution with the “best” value
• Optimization problems can be cast as decision
problems that are easier to study
– E.g.: Shortest path: G = unweighted directed graph
• Find a path between u and v that uses the fewest edges
• Does a path exist from u to v consisting of at most k
edges?
Algorithmic vs Problem Complexity
• The algorithmic complexity of a computation is
some measure of how difficult is to perform the
computation (i.e., specific to an algorithm)
• The complexity of a computational problem or
task is the complexity of the algorithm with the
lowest order of growth of complexity for solving
that problem or performing that task.
– e.g. the problem of searching an ordered list has at most
lgn time complexity.
• Computational Complexity: deals with
classifying problems by how hard they are.
Class of “P” Problems
• Class P consists of (decision) problems that are
solvable in polynomial time
• Polynomial-time algorithms
– Worst-case running time is O(nk), for some constant k
• Examples of polynomial time:
– O(n2), O(n3), O(1), O(n lg n)
• Examples of non-polynomial time:
– O(2n), O(nn), O(n!)
Tractable/Intractable Problems
• Problems in P are also called tractable
• Problems not in P are intractable or unsolvable
– Can be solved in reasonable time only for small inputs
– Or, can not be solved at all

• Are non-polynomial algorithms always worst than


polynomial algorithms?
- n1,000,000 is technically tractable, but really impossible - nlog
log log n
is technically intractable, but easy
Example of Unsolvable Problem
• Turing discovered in the 1930’s that there
are problems unsolvable by any algorithm.
• The most famous of them is the halting
problem
– Given an arbitrary algorithm and its input, will
that algorithm eventually halt, or will it
continue forever in an “infinite loop?”
Examples of Intractable
Problems
Intractable Problems
• Can be classified in various categories
based on their degree of difficulty, e.g.,
– NP
– NP-complete
– NP-hard
• Let’s define NP algorithms and NP
problems …
Nondeterministic and NP Algorithms
Nondeterministic algorithm = two stage procedure:
1) Nondeterministic (“guessing”) stage:
generate randomly an arbitrary string that can be thought of
as a candidate solution (“certificate”)

2) Deterministic (“verification”) stage:


take the certificate and the instance to the problem and
returns YES if the certificate represents a solution

NP algorithms (Nondeterministic polynomial)


verification stage is polynomial
Class of “NP” Problems
• Class NP consists of problems that could be
solved by NP algorithms
– i.e., verifiable in polynomial time
• If we were given a “certificate” of a solution,
we could verify that the certificate is correct in
time polynomial to the size of the input
• Warning: NP does not mean “non-polynomial”
E.g.: Hamiltonian Cycle
• Given: a directed graph G = (V, E), determine a simple
cycle that contains each vertex in V
– Each vertex can only be visited once
• Certificate:
hamilton
– Sequence: 〈 v1, v2, v3, …, v|V| 〉
ian

not
hamilton
ian
Is P = NP?
P
N
• Any problem in P is also in NP: P

P ⊆ NP
• The big (and open question) is whether NP
⊆ P or P = NP
– i.e., if it is always easy to check a solution,
should it also be easy to find a solution?
NP-Completeness (informally)
P NP-complete
• NP-complete problems are NP

defined as the hardest


problems in NP
• Most practical problems turn out to be
either P or NP-complete.
The Classes of P and NP
• The class P and Deterministic Turing
Machine
• Given a decision problem X, if there is a
polynomial time Deterministic Turing Machine
program that solves X, then X is belong to P
• Informally, there is a polynomial time
algorithm to solve the problem
• The class NP and Non-deterministic Turing
Machine
• Given a decision problem X.
If there is a polynomial time Non-deterministic
Turing machine program that solves X, then X
belongs to NP
• Given a decision problem X.
For every instance I of X,
(a) guess solution S for I, and
(b) check “is S a solution to I?”
If (a) and (b) can be done in polynomial time,
then X belongs to NP.
• Obvious : P ⊆ NP, i.e. A NP
(decision) problem in P does
not need “guess solution”.
The correct solution can be P
computed in polynomial time.

• Some problems which are in NP, but may not in P :


• 0/1 Knapsack Problem
• PARTITION Problem : Given a finite set of positive
integers Z.
Question : Is there a subset Z' of Z such that
Sum of all numbers in Z' = Sum of all numbers in
Z-Z' ?
i.e. ∑ Z' = ∑ (Z-Z')
1. Polynomial Transformation (" ∝ ")
• L1 ∝ L2 :
There is a polynomial time transformation
that transforms arbitrary instance of L1 to
some instance of L2.

• If L1 ∝ L2 then L2 is in P implies L1 is in P

(or L1 is not in P implies L2 is not in P)

• If L1 ∝ L2 and L2 ∝ L3 then L1 ∝ L3
• P: (Decision) problems solvable by deterministic
algorithms in polynomial time
• NP: (Decision) problems solved by non-deterministic
algorithms in polynomial time
• A group of (decision) problems,
including all of the ones we have NP-Complete
discussed (Satisfiability, 0/1
Knapsack, Longest Path, Partition)
have an additional important NP
property:
If any of them can be solved in P
polynomial time, then they all
can! problems are called NP-complete problems.
• These

You might also like