Title: P, NP, and NP-Complete Problems
1P, NP, and NP-Complete Problems
2The class P consists of all problems that can be
solved in polynomial time, O(Nk), by
deterministic computers (the ones that you have
used all your life!). Examples
- Adding two numbers
- Extracting the element with the highest priority
from a heap - Looking for an element in an array
- Looking for the MST
O(1) constant
O(log N) logarithmic (thus, O(N) because N ?
log N )
O(N) lineal
O(N log N) (thus, O(N2) )
3What does Deterministic Computer Means?(Idea)
(For details see CSE 318)
At every computational cycle we have the
so-called state of the computation
3. Program ltinstructiongt .
State (1. memory, 2. location of memory being
pointed at, 3. program, 4. current
instruction)
4What does Deterministic Computer Means?(Idea- II)
In a deterministic computer we can determine in
advance for every computational cycle, the output
state by looking at the input state
Computer Memory
Program ltinstructiongt .
5Our Favorite Example
//input an array A1..N and an element el that
is in the array //output the position of el in A
search(el, A, i) if (Ai el) then return
i else return search(el, A, i1)
Complexity O(N)
6Djikstras Shortest Path Algorithm
D
3
B
2
6
E
A
3
4
C
H
If the source is A, which edge is selected in the
next iteration?
Complexity O(N log N) ( N number of edges
vertices)
7What does NonDeterministic Computer Means?
In a nondeterministic computer we may have more
than one output state.
Computer Memory
Program ltinstructiongt .
The computer chooses the correct one
(nondeterministic choice)
8Nondeterministic Computers Seem More powerful
Than Deterministic Ones
Informally Calls an Oracle who makes the right
choice
nonDeterministicSearch(el, A) return
i
i ? ohOracle(el, A, 1)
9But They Are Not More Powerful
ohOracle(el, A, i) if (Ai el) then
return i else return ohOracle(el, A,
i1)
nonDeterministicSearch(el, A) i ?
ohOracle(el, A, 1) return i
Key result Every program for a nondeterministic
computer can be simulated by a deterministic one
(CSE 318)
Thus, they solve the same problems
10But What About the Complexity?
search(el, A, i, N) if (Ai el) then
return i else return search(el, A,
i1,N)
OracleSearch(el, A, N) i ? Oracle(el, A,
N) return i
Complexity O(N)
Complexity O(1)
11Formal Definition Nondeterministic Algorithms
A nondeterministic algorithm for a problem A is a
two-stage procedure. In the first phase, a
procedure makes a guess about the possible
solution for A. In the second phase, a procedure
checks if the guessed solution is indeed a
solution for A
Phase2(i,el, A) return
Phase1(el, A) return i
i ? random(1..N)
Ai el
Note the actual solution must be included among
the possible guesses of phase 1
12NP Complexity
The class NP consists of all problems that can be
solved in polynomial time by nondeterministic
algorithms
(that is, both phase 1 and phase 2 run in
polynomial time)
If A is a problem in P then A is a problem in NP
because
Phase 1 use the polynomial algorithm that solves
A Phase 2 write a constant time procedure that
always returns true
The key question is are there problems in NP that
are not in P or is P NP?
We dont know the answer to the previous question
13NP Complexity (II)
How to proof that a problem A is in NP
- 1. Show that A is in P, or
- 2. Write a nondeterministic algorithm solving A
that runs in polynomial time
14Example
Showing that searching for an element in an array
is in P
- 1. Write the procedure search(el, A, i) (Slide 5)
which runs in lineal time, or - 2. Write a non-deterministic algorithm solving
search (Slide 11 both Phase 1 and Phase 2 run in
constant time)
15Homework
- Both sections write a nickname so I can post
your grades on the web and you remain anonym - Section 010
- Section 9.4 3.b, 4, 6
- Construct a nondeterministic algorithm computing
the MST for an input graph (pseudocode). For
phase 2 assume that the minimum cost is known in
advance. What is the complexity of this
algorithm? - Construct a nondeterministic algorithm solving
the traveling salesman person problem
(pseudocode). For phase 2 assume that the minimum
cost is known in advance. What is the complexity
of this algorithm? - Section 011
- Section 9.4 3.a, 5.a, 5.b
- Construct a nondeterministic algorithm computing
the shortest path from a node u to a node v in an
input graph (pseudocode). For phase 2 assume that
the minimum cost is known in advance. What is the
complexity of this algorithm? - Construct a nondeterministic algorithm solving
the Knapsack problem (pseudocode). For phase 2
assume that the minimum cost is known in advance.
What is the complexity of this algorithm?