Lecture 22: P = NP? CS200: Computer Science University of Virginia

Slides:



Advertisements
Similar presentations
Part VI NP-Hardness. Lecture 23 Whats NP? Hard Problems.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Class 38: Intractable Problems (Smiley Puzzles.
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 11: 1% Pure Luck Make-up lab hours:
Algorithm analysis and design Introduction to Algorithms week1
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
Cs3102: Theory of Computation Class 24: NP-Completeness Spring 2010 University of Virginia David Evans.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
David Evans CS588: Security and Privacy University of Virginia Computer Science Lecture 15: Complexity Notes This is selected.
What Every Biologist, Chemist, and Poet Should Know about Computer Science David Evans UVaCompBio 25 April
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
David Evans Class 19: Golden Ages and Astrophysics CS200: Computer Science University of Virginia Computer Science.
Class 21: Introducing Complexity David Evans cs302: Theory of Computation University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 15: Intractable Problems (Smiley.
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
Pancakes, Puzzles, and Polynomials: Cracking the Cracker Barrel Game Christopher Frost Michael Peck.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Intractable Problems (Smiley Puzzles.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
David Evans Lecture 41: P = NP? I will have extra office hours after class today (1-3pm). To be eligible to present Monday,
Part VI NP-Hardness.
Lecture 13: Quicksorting CS200: Computer Science
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
The Growth of Functions
Enough Mathematical Appetizers!
Computation.
CS 2210 Discrete Structures Algorithms and Complexity
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Class 14: Intractable Problems CS150: Computer Science
Lecture 11: All Sorts CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 13: Cost of Sorts CS150: Computer Science
CS 2210 Discrete Structures Algorithms and Complexity
Applied Discrete Mathematics Week 6: Computation
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 10: Quicker Sorting CS150: Computer Science
Lecture 24: Metalinguistics CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Class 33: Making Recursion M.C. Escher, Ascending and Descending
Lecture 9: The Great Lambda Tree of Knowledge and Power
Class 24: Computability Halting Problems Hockey Team Logo
Enough Mathematical Appetizers!
Class 34: Models of Computation CS200: Computer Science
Enough Mathematical Appetizers!
Class 26: Modeling Computing CS150: Computer Science
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Algorithms CSCI 235, Spring 2019 Lecture 36 P vs
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 23: Computability CS200: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

David Evans http://www.cs.virginia.edu/evans 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

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

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

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

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

Crashes! > (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

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

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

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

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

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

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

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

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

 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

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

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

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

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

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

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

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

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

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