Lecture 10 - NP - Complete Problems
Lecture 10 - NP - Complete Problems
ANALYSIS OF
ALGORITHMS
Chapter # 10 :
NP- Complete Problems
• Introduction / Basics
2. The Class P
3. The Class NP
4. NP-Complete Problems
Introduction
4
Polynomial problems: A problem is said to be
polynomial if there exists an algorithm that solves the
problem in time T(n)=O(nk), where k is a constant.
Exponential problems: A problem is said to be
exponential if no polynomial-time algorithm can be
developed for it. ( i.e., T(n)=O(an) ).
In-tractable problems: are
the one which are non-
computable. There exist no
efficient algorithms to solve
them. Usually brute-force
search is used.
Examples: Traveling Salesman
Problem, Subset Sum, Bin Packing,
Job Shop Scheduling, Satisfiability...
Cont…
5
Deterministic algorithms: An algorithm A is deterministic if,
when presented with an instance of the problem Q, it has only
one choice in each step throughout its execution. Thus if A is
run again and again on the same instance, its output never
changes.
Non-deterministic algorithms: An algorithm that defines
multiple ways of processing the same input, without any
specification of which one will be taken (e.g., randomised
algorithms). They can be converted into a deterministic
algorithm, possibly with exponential slow down.
E.g.,: Primality testing problem: given a natural number larger than one,
determine whether it is prime.
Solution: A nondeterministic algorithm for this problem is the following.
Pick any integer k such that 2 ≤ k ≤ √(n).
If k is a divisor of n, stop with answer no; otherwise stop with don't know.
Cont…
6
Complexity Classes
The class P is the class of problems that can be solved in
time that is polynomial in the size of the input, n.
The problems in P are considered “tractable”.
The class NP is the class of problems with solutions that
can be verified in polynomial time.
How to prove that a problem is in NP ?
1. Show that the problem is in P, or
2. Write a program solving the problem using the steps that runs in
polynomial time, or
3. Write 2 polynomial programs that: (1) generate a possible
solution S and (2) tests if S is a solution to the problem.
NP-Complete problems: the problems having the common
property that if one of them is solvable in polynomial time,
then all the others are solvable in polynomial time.
Cont…
7
Example of an NP problem:
The Hamiltonian Cycle (HC) problem
Input: A graph G
Question: Does G have a Hamiltonian Cycle?
Hamiltonian cycle is a
cycle in graph that visits
each vertex exactly once.
is NP P ?
• It is an open problem.
• we think P ≠ NP, but no one has proven it one way
or the other (despite enormous effort).
• It is known that if P ≠ NP, there must exist NP
problems that neither are in P nor are NPC.
1. NP-Complete Problems
9
Example:
If we have an algorithm A that solves the decision problem for
graph coloring, we can find the chromatic number of a graph
G using binary search and Algorithm A as a subroutine.
Hence, the chromatic number of G can be found using only
O(log n) calls to algorithm A.
Deterministic Algorithms
15
Definition:
The class of decision problems P consists of those
whose yes/no solution can be obtained using a
deterministic algorithm that runs in polynomial
time of steps, i.e. O(nk), where k is a non-negative
integer and n is the input size.
Examples
17
in polynomial time
Cont…
20
Definition: Let A be a nondeterministic algorithm for a
problem . We say that A accepts an instance I of if
and only if on input I, there exists a guess that leads to
a yes answer.
Does it mean that if an algorithm A on a given input I
leads to an answer of no for a certain guess, that it does
not accept it?
What is the running time of a non-deterministic
algorithm?
It is simply the sum of the two running times: the one for
the guessing phase, and that for the verification phase. So
it is O(ni) + O(nj) = O(nk), for k≥0 … polynomial time.
3. The Class NP
21
Definition:
The class of decision problems NP consists of those
decision problems for which there exists a
nondeterministic algorithm that runs in
polynomial time.
Definition:
Let and ’ be two decision problems. We say that
’ reduces to in polynomial time, denoted by
’poly , if there exists a deterministic algorithm A
that behaves as follows:
When A is presented with an instance I’ of problem ’,
it transforms it into an instance I of problem in
polynomial time such that the answer to I’ is yes if and
only if the answer to I is yes.
SAT
one endpoint of every edge of
the graph.
3-CNF-SAT