Lecture 22: P = NP? CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Menu • Permuted Sorting • What Really Means • Complexity Classes 19 March 2003 CS 200 Spring 2003 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 March 2003 is-sorted? is-sorted? is-sorted? CS 200 Spring 2003 is-sorted? is-sorted? 3 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 4 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 5 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 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 March 2003 CS 200 Spring 2003 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 March 2003 CS 200 Spring 2003 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 March 2003 CS 200 Spring 2003 9 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 10 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 11 O Examples 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)? 10x is O (x)? x2 is O (x)? 19 March 2003 Yes, c = 1 works fine. Yes, c = .09 works fine. No, no matter what c we pick, cx2 > x for big enough x CS 200 Spring 2003 12 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 13 Examples • 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: There is a positive constant c such that c * f(x) < g(x) for all but a finite number of x values. – Yes, pick c = 2 • 10x is (x) – Yes, pick c = 1 • Is x2 (x)? • x is O(x) – Yes, pick c = .5 • 10x is O(x) – Yes, pick c = .09 • x2 is not O(x) – Yes! 19 March 2003 CS 200 Spring 2003 14 Tight Bound: (Theta) 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 15 Examples • 10x is (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 16 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 March 2003 CS 200 Spring 2003 17 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 18 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 19 From Lecture 12 Orders of Growth 70000 peg board game 60000 logn n nlogn n^2 n^3 2^n 50000 40000 30000 20000 10000 0 1 19 March 2003 2 3 4 5 6 7 8 9 10 11 CS 200 Spring 2003 12 13 14 15 16 simulating universe bubblesort insertsort-tree 20 Orders of Growth From Lecture 12 1200000 1000000 logn n nlogn n^2 n^3 2^n peg board game 800000 “intractable” 600000 400000 “tractable” 200000 simulating universe 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 22 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 23 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 March 2003 4 8 16 CS 200 Spring 2003 32 64 128 log-log scale 24 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 25 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 26 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 27