270 likes | 281 Views
Learn about permuted sorting, complexity classes, and what "" truly means in computer science. Explore examples and procedures to deepen your understanding of sorting problems.
E N D
Lecture 22: P = NP? David Evans http://www.cs.virginia.edu/evans CS200: Computer Science University of Virginia Computer Science
Menu • Permuted Sorting • What Really Means • Complexity Classes CS 200 Spring 2003
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? CS 200 Spring 2003
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? CS 200 Spring 2003
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))))) CS 200 Spring 2003
permute-sort (define (permute-sort cf lst) (car (filter (lambda (lst) (is-sorted? cf lst)) (all-permutations lst)))) CS 200 Spring 2003
> (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! CS 200 Spring 2003
(define (permute-sort cf lst) (car (filter (lambda (lst) (is-sorted? cf lst)) (all-permutations lst)))) How much work is permute-sort? • 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? CS 200 Spring 2003
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!) CS 200 Spring 2003
Notation • O(x)– it is no more thanxwork (upper bound) • (x) – work scales as x (tight bound) • (x) – it is at leastxwork (lower bound) If O(x) and (x) are true, then (x) is true. CS 200 Spring 2003
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. CS 200 Spring 2003
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)? Yes, c = 1 works fine. 10x is O (x)? Yes, c = .09 works fine. No, no matter what c we pick, cx2 > x for big enough x x2 is O (x)? CS 200 Spring 2003
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 < CS 200 Spring 2003
xis (x) Yes, pick c = 2 10xis (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 notO(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. Examples CS 200 Spring 2003
Tight Bound: (Theta) f(x) is (g (x)) iff: f(x) is O (g (x)) and f(x) is (g (x)) CS 200 Spring 2003
Examples • 10xis (x) • Yes, since 10xis (x) and 10x is O(x) • Doesn’t matter that you choose different c values for each part; they are independent • x2is/is not (x)? • No, since x2is not O(x) • x is/is not (x2)? • No, since x2is not (x) CS 200 Spring 2003
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. CS 200 Spring 2003
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 CS 200 Spring 2003
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. CS 200 Spring 2003
From Lecture 12 Orders of Growth peg board game simulating universe bubblesort insertsort-tree CS 200 Spring 2003
From Lecture 12 Orders of Growth peg board game “intractable” “tractable” simulating universe
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. CS 200 Spring 2003
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. CS 200 Spring 2003
Intractable Problems n! 2n time since “Big Bang” P 2022 today n2 n log n log-log scale CS 200 Spring 2003
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. CS 200 Spring 2003
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) CS 200 Spring 2003
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? CS 200 Spring 2003