Class 37: P = NP ? CS200: Computers, Programs and Computing University of Virginia David Evans Computer Science http://www.cs.virginia.edu/evans.

Download Report

Transcript Class 37: P = NP ? CS200: Computers, Programs and Computing University of Virginia David Evans Computer Science http://www.cs.virginia.edu/evans.

Class 37:
P = NP ?
CS200: Computers, Programs and Computing
University of Virginia
David Evans
Computer Science
http://www.cs.virginia.edu/evans
Complexity and Computability
• We’ve learned how to measure the
complexity of any procedure ()
• We’ve learned how to show some
problems are undecidable
• But…we haven’t learned how to
measure the complexity of
problems
19 April 2004
CS 200 Spring 2004
2
Permuted Sorting
• A (possibly) really dumb way to sort:
– Find all possible orderings of the list
(permutations)
– Check each permutation in order, until you
find one that is sorted
• Example: sort (3 1 2)
All permutations:
(3 1 2) (3 2 1) (2 1 3) (2 3 1) (1 3 2) (1 2 3)
is-sorted?
19 April 2004
is-sorted? is-sorted?
is-sorted?
CS 200 Spring 2004
is-sorted?
is-sorted?
3
permute-sort
(define (permute-sort cf lst)
(car
(filter (lambda (lst) (is-sorted? cf lst))
(all-permutations lst))))
19 April 2004
CS 200 Spring 2004
4
is-sorted?
(define (is-sorted? cf lst)
(or (null? lst)
(= 1 (length lst))
(and (cf (car lst) (cadr lst))
(is-sorted? cf (cdr lst)))))
19 April 2004
CS 200 Spring 2004
5
all-permutations
(define (all-permutations lst)
(flat-one
(map
(lambda (n)
(if (= (length lst) 1)
(list lst) ; The permutations of (a) are ((a))
(map
(lambda (oneperm)
(cons (nth lst n) oneperm))
(all-permutations (exceptnth lst n)))))
(intsto (length lst)))))
19 April 2004
CS 200 Spring 2004
6
> (time (permute-sort <= (rand-int-list 5)))
cpu time: 10 real time: 10 gc time: 0
(4 14 14 45 51)
> (time (permute-sort <= (rand-int-list 6)))
cpu time: 40 real time: 40 gc time: 0
(6 29 39 40 54 69)
> (time (permute-sort <= (rand-int-list 7)))
cpu time: 261 real time: 260 gc time: 0
(6 7 35 47 79 82 84)
> (time (permute-sort <= (rand-int-list 8)))
cpu time: 3585 real time: 3586 gc time: 0
(4 10 40 50 50 58 69 84)
> (time (permute-sort <= (rand-int-list 9)))
Crashes!
19 April 2004
CS 200 Spring 2004
7
How much
work is
permute-sort?
(define (permute-sort cf lst)
(car
(filter (lambda (lst) (is-sorted? cf lst))
(all-permutations lst))))
• We evaluated is-sorted? once for each
permutation of lst.
• How much work is is-sorted??
(n)
• How many permutations of the list are
there?
19 April 2004
CS 200 Spring 2004
8
Number of permutations
(map
(lambda (n)
(if (= (length lst) 1) (list lst)
(map (lambda (oneperm) (cons (nth lst n) oneperm))
(all-permutations (exceptnth lst n)))))
(intsto (length lst)))
• There are n = (length lst) values in the first map,
for each possible first element
• Then, we call all-permutations on the list without
that element (length = n – 1)
• There are n * n – 1 * … * 1 permutations
• Hence, there are n! lists to check: (n!)
19 April 2004
CS 200 Spring 2004
9
Procedures and Problems
• So far we have been talking about
procedures (how much work is permutesort?)
• We can also talk about problems: how
much work is sorting?
• A problem defines a desired output for a
given input. A solution to a problem is a
procedure for finding the correct output for
all possible inputs.
19 April 2004
CS 200 Spring 2004
10
The Sorting Problem
• Input: a list and a comparison function
• Output: a list such that the elements
are the same elements as the input
list, but in order so that the
comparison function evaluates to true
for any adjacent pair of elements
19 April 2004
CS 200 Spring 2004
11
Problems and Procedures
• If we know a procedure that is that is (f (n))
that solves a problem then we know the
problem is O(f (n)).
• The sorting problem is O (n!) since we know
a procedure (permute-sort) that solves it in
 (n!)
• Is the sorting problem is (n!)?
No, we would need to prove there is no
better procedure.
19 April 2004
CS 200 Spring 2004
12
Problem Complexity
• How complex is the pegboard puzzle?
– We know a solution that is (2n)
– So, the problem is O(2n)
• But, we can’t say it is (2n) unless we
know there is no better solution
• We can say it is (n) since the we know
we can’t do better than a solution that
considers n moves
19 April 2004
CS 200 Spring 2004
13
Deterministic Computing
• All our computing models so far are
deterministic: you know exactly what to do
every step
– TM: only one transition rule can match any
machine configuration, only one way to follow
that transition rule
– Lambda Calculus: always -reduce the
outermost redex
19 April 2004
CS 200 Spring 2004
14
Nondeterministic Computing
• Allows computer to try different options at
each step, and then use whichever one
works best
• Make a Finite State Machine nondeterministic: doesn’t change computing time
• Make a Turing Machine non-deterministic:
might change computing time
• No one knows whether it does or not
• This is the biggest open problem in Computer
Science
19 April 2004
CS 200 Spring 2004
15
Making FSM’s Nondeterministic
0
0
0
1
1
2
1
3
#
HALT
19 April 2004
CS 200 Spring 2004
16
Nondeterministic FSM
0, 1
0
1
Input Read
2
3
Possible States
0
00
001
{ 1, 2 }
{ 1, 2 }
{ 1, 3 }
0010
{ 1, 2 }
00101
{ 1, 3 }
00101#
{ HALT }
19 April 2004
1
#
HALT
CS 200 Spring 2004
17
Power of NFSM
• Can NFSMs computing anything FSMs
cannot compute?
No – you can always convert an NFSM to a FSM by
making each possible set of states in the NFSM into one
state in the new FSM. (Number of states may increase as 2s)
• Can NFSMs compute faster than FSMs?
No – both read input one letter at a time, and transition
automatically to the next state. (Both are always (n) where
n is the number of symbols in the input.)
19 April 2004
CS 200 Spring 2004
18
Nondeterministic TMs
• Multiple transitions possible at every step
• Follow all possible steps:
– Each possible execution maintains its own
state and its own infinite tape
– If any possible execution halts (in an
accepting state), the NTM halts, and the tape
output of that execution is the NTM output
19 April 2004
CS 200 Spring 2004
19
PS7 Community Prize
http://www.people.virginia.edu/~en9j/ps7/user-page.html
19 April 2004
CS 200 Spring 2004
20
With Great Communities,
Comes Great Responsibility
• Evong will collect the course evaluations:
– SEAS official evaluation (bubble form with
general comments space) used for determining
whether or not to offer CS200 again and
whether or not to fire me
– My own course improvement survey (lots of
specific questions for improving future CS200s)
• Make sure to give her your form on the last
day of class or earlier
19 April 2004
CS 200 Spring 2004
21
From the Course Pledge
I will provide useful feedback. I realize
this is an evolving course and it is
important that I let the course staff know
what they need to improve the course. I
will not wait until the end of the course to
make the course staff aware of any
problems. … I will fill out all course
evaluation surveys honestly and
thoroughly.
19 April 2004
CS 200 Spring 2004
22
Nondeterministic TMs
• Multiple transitions possible at every step
• Follow all possible steps:
– Each possible execution maintains its own
state and its own infinite tape
– If any possible execution halts (in an
accepting state), the NTM halts, and the tape
output of that execution is the NTM output
19 April 2004
CS 200 Spring 2004
23
Power of NTM
• Can NTMs computing anything TMs
cannot compute?
No – you can simulate a NTM with a regular TM:
keep track of all possible states on the tape
keep track of all possible tapes on the tape
(really long, but its an infinitely long tape)
• Can NTMs compute faster than TMs?
Maybe! No one has proved it one way or the other.
19 April 2004
CS 200 Spring 2004
24
This is amazing!
• An NTM is a machine that every time it has to
make a decision can just make both and use
whichever one turns out to be right later
• Equivalently: whenever an NTM has to make a
decision, it always makes the right one!
• We don’t know if a computer that always makes
the right decision is really better than one that
has to try all possible decisions
19 April 2004
CS 200 Spring 2004
25
Complexity Class P
Class P: problems that can be solved in
polynomial time by a deterministic TM.
O (nk) for some constant k.
Easy problems like sorting, making a
photomosaic using duplicate tiles,
simulating the universe are all in P.
19 April 2004
CS 200 Spring 2004
26
Complexity Class NP
Class NP: problems that can be solved in
polynomial time by a nondeterministic TM
If we could try all possible solutions at once, we
could identify the solution in polynomial time.
All problems in P are in NP.
Hard problems like the smiley puzzle and
the pegboard puzzle sorting are all in NP
(but might not be in P).
19 April 2004
CS 200 Spring 2004
27
Is the Pegboard Puzzle in P?
No one knows!
We can’t find a O(nk) solution.
We can’t prove one doesn’t
exist.
19 April 2004
CS 200 Spring 2004
28
Intractable Problems
1E+30
1E+28
time
since
“Big
Bang”
n!
1E+26
2n
1E+24
1E+22
1E+20
1E+18
1E+16
1E+14
P
1E+12
1E+10
2022
today
1E+08
1E+06
n2
n log n
10000
100
1
2
19 April 2004
4
8
16
CS 200 Spring 2004
32
64
128
log-log scale
29
P = NP?
• Is there a polynomial-time solution to the
“hardest” problems in NP?
• No one knows the answer!
• The most famous unsolved problem in
computer science and math
• Listed first on Millennium Prize Problems
– win $1M if you can solve it
– (also an automatic A+ in this course)
19 April 2004
CS 200 Spring 2004
30
Charge
• PS8 is due less than one week from now!
• Next time:
– How to prove a problem is in NP
– How to prove a problem is one of the hardest
problems in NP
– Why the peg board puzzle, perfect
photomosaic problem, drug discovery problem,
traveling salesperson problem, map coloring
problem, etc. are all really the same problem
19 April 2004
CS 200 Spring 2004
31