1 / 27

David Evans cs.virginia/evans

Learn about permuted sorting, complexity classes, and what "" truly means in computer science. Explore examples and procedures to deepen your understanding of sorting problems.

lonc
Download Presentation

David Evans cs.virginia/evans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 22: P = NP? David Evans http://www.cs.virginia.edu/evans CS200: Computer Science University of Virginia Computer Science

  2. Menu • Permuted Sorting • What  Really Means • Complexity Classes 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? 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? 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))))) CS 200 Spring 2003

  6. permute-sort (define (permute-sort cf lst) (car (filter (lambda (lst) (is-sorted? cf lst)) (all-permutations lst)))) CS 200 Spring 2003

  7. > (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

  8. (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

  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!) CS 200 Spring 2003

  10. 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

  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. CS 200 Spring 2003

  12. 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

  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 < CS 200 Spring 2003

  14. 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

  15. 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

  16.  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

  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. 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 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. CS 200 Spring 2003

  20. From Lecture 12 Orders of Growth peg board game simulating universe bubblesort insertsort-tree CS 200 Spring 2003

  21. From Lecture 12 Orders of Growth peg board game “intractable” “tractable” 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. 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. CS 200 Spring 2003

  24. Intractable Problems n! 2n time since “Big Bang” P 2022 today n2 n log n log-log scale CS 200 Spring 2003

  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. 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) 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? CS 200 Spring 2003

More Related