Download presentation
Presentation is loading. Please wait.
Published byDerya Karaduman Modified over 6 years ago
1
David Evans http://www.cs.virginia.edu/evans
Lecture 22: P = NP? CS200: Computer Science University of Virginia Computer Science David Evans
2
Menu Permuted Sorting What Really Means Complexity Classes
19 March 2003 CS 200 Spring 2003
3
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? is-sorted? is-sorted? is-sorted? is-sorted? is-sorted? 19 March 2003 CS 200 Spring 2003
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))))) Is or a procedure or a special form? 19 March 2003 CS 200 Spring 2003
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 March 2003 CS 200 Spring 2003
6
permute-sort (define (permute-sort cf lst) (car
(filter (lambda (lst) (is-sorted? cf lst)) (all-permutations lst)))) 19 March 2003 CS 200 Spring 2003
7
Crashes! > (time (permute-sort <= (rand-int-list 5)))
cpu time: 10 real time: 10 gc time: 0 ( ) > (time (permute-sort <= (rand-int-list 6))) cpu time: 40 real time: 40 gc time: 0 ( ) > (time (permute-sort <= (rand-int-list 7))) cpu time: 261 real time: 260 gc time: 0 ( ) > (time (permute-sort <= (rand-int-list 8))) cpu time: 3585 real time: 3586 gc time: 0 ( ) > (time (permute-sort <= (rand-int-list 9))) Crashes! 19 March 2003 CS 200 Spring 2003
8
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 March 2003 CS 200 Spring 2003
9
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 March 2003 CS 200 Spring 2003
10
Notation O(x) – it is no more than x work (upper bound)
(x) – work scales as x (tight bound) (x) – it is at least x work (lower bound) If O(x) and (x) are true, then (x) is true. 19 March 2003 CS 200 Spring 2003
11
Real meaning of O f(x) is O (g (x)) means:
There is a positive constant c such that c * f(x) < g(x) for all but a finite number of x values. 19 March 2003 CS 200 Spring 2003
12
O Examples x is O (x2)? 10x is O (x)? x2 is O (x)?
f(x) is O (g (x)) means: There is a positive constant c such that c * f(x) < g(x) for all but a finite number of x values. x is O (x2)? Yes, c = 1 works fine. 10x is O (x)? Yes, c = .09 works fine. x2 is O (x)? No, no matter what c we pick, cx2 > x for big enough x 19 March 2003 CS 200 Spring 2003
13
Lower Bound: (Omega) f(x) is (g (x)) means:
There is a positive constant c such that c * f(x) > g(x) for all but a finite number of x values. Difference from O – this was < 19 March 2003 CS 200 Spring 2003
14
Examples x is O(x) 10x is O(x) Is x2 (x)? x2 is not O(x) x is (x)
f(x) is (g (x)) means: There is a positive constant c such that c * f(x) > g(x) for all but a finite number of x values. f(x) is O (g (x)) means: c * f(x) < g(x) Examples x is (x) Yes, pick c = 2 10x is (x) Yes, pick c = 1 Is x2 (x)? Yes! x is O(x) Yes, pick c = .5 10x is O(x) Yes, pick c = .09 x2 is not O(x) 19 March 2003 CS 200 Spring 2003
15
Tight Bound: (Theta) f(x) is O (g (x)) and f(x) is (g (x))
f(x) is (g (x)) iff: f(x) is O (g (x)) and f(x) is (g (x)) 19 March 2003 CS 200 Spring 2003
16
Examples x is/is not (x2)? 10x is (x) x2 is/is not (x)?
Yes, since 10x is (x) and 10x is O(x) Doesn’t matter that you choose different c values for each part; they are independent x2 is/is not (x)? No, since x2 is not O (x) x is/is not (x2)? No, since x2 is not (x) 19 March 2003 CS 200 Spring 2003
17
Procedures and Problems
So far we have been talking about procedures (how much work is permute-sort?) 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 March 2003 CS 200 Spring 2003
18
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 March 2003 CS 200 Spring 2003
19
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 March 2003 CS 200 Spring 2003
20
Orders of Growth From Lecture 12 peg board game simulating universe
bubblesort insertsort-tree 19 March 2003 CS 200 Spring 2003
21
Orders of Growth From Lecture 12 peg board game “intractable”
simulating universe
22
Complexity Class P Class P: problems that can be solved in polynomial time O (nk) for some constant k. Easy problems like sorting, making a photomosaic using duplicate tiles, simulating the universe are all in P. 19 March 2003 CS 200 Spring 2003
23
Complexity Class NP Class NP: problems that can be solved in nondeterministic polynomial time If we could try all possible solutions at once, we could identify the solution in polynomial time. 19 March 2003 CS 200 Spring 2003
24
Intractable Problems P n! 2n n2 n log n log-log scale time since “Big
Bang” 2n P 2022 today n2 n log n 19 March 2003 CS 200 Spring 2003 log-log scale
25
Moore’s Law Doesn’t Help
If the fastest procedure to solve a problem is O(2n) or worse, Moore’s Law doesn’t help much. Every doubling in computing power increases the problem size by 1. 19 March 2003 CS 200 Spring 2003
26
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 March 2003 CS 200 Spring 2003
27
Charge Friday: How do we know a problem is in NP
Is minesweeper harder than the Cracker Barrel problem? What are the “hardest” problems in NP? 19 March 2003 CS 200 Spring 2003
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.