1 / 31

Complexity of Sorting Problem: O(n!) or Θ(n!)

This article discusses the complexity of the sorting problem and whether there is a better solution than the known procedure of permute-sort.

brandyj
Download Presentation

Complexity of Sorting Problem: O(n!) or Θ(n!)

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. Class 37: P = NP ? David Evans http://www.cs.virginia.edu/evans CS200: Computers, Programs and Computing University of Virginia Computer Science

  2. Complexity and Computability • We’ve learned how to measure the complexity of any procedure () • We’ve learned how to show some problems are undecidable • But…we haven’t learned how to measure the complexity of problems CS 200 Spring 2004

  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 2004

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

  5. is-sorted? (define (is-sorted? cf lst) (or (null? lst) (= 1 (length lst)) (and (cf (car lst) (cadr lst)) (is-sorted? cf (cdr lst))))) CS 200 Spring 2004

  6. 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 2004

  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 2004

  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 2004

  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 2004

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

  11. 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 2004

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

  13. Problem Complexity • How complex is the pegboard puzzle? • We know a solution that is (2n) • So, the problem is O(2n) • But, we can’t say it is (2n) unless we know there is no better solution • We can say it is (n) since the we know we can’t do better than a solution that considers n moves CS 200 Spring 2004

  14. Deterministic Computing • All our computing models so far are deterministic: you know exactly what to do every step • TM: only one transition rule can match any machine configuration, only one way to follow that transition rule • Lambda Calculus: always -reduce the outermost redex CS 200 Spring 2004

  15. Nondeterministic Computing • Allows computer to try different options at each step, and then use whichever one works best • Make a Finite State Machine non-deterministic: doesn’t change computing time • Make a Turing Machine non-deterministic: might change computing time • No one knows whether it does or not • This is the biggest open problem in Computer Science CS 200 Spring 2004

  16. Making FSM’s Nondeterministic 0 0 0 1 3 1 2 1 # HALT CS 200 Spring 2004

  17. Nondeterministic FSM 0, 1 0 1 3 1 2 # HALT CS 200 Spring 2004

  18. Power of NFSM • Can NFSMs computing anything FSMs cannot compute? • Can NFSMs compute faster than FSMs? No – you can always convert an NFSM to a FSM by making each possible set of states in the NFSM into one state in the new FSM. (Number of states may increase as 2s) No – both read input one letter at a time, and transition automatically to the next state. (Both are always (n) where n is the number of symbols in the input.) CS 200 Spring 2004

  19. Nondeterministic TMs • Multiple transitions possible at every step • Follow all possible steps: • Each possible execution maintains its own state and its own infinite tape • If any possible execution halts (in an accepting state), the NTM halts, and the tape output of that execution is the NTM output CS 200 Spring 2004

  20. PS7 Community Prize http://www.people.virginia.edu/~en9j/ps7/user-page.html CS 200 Spring 2004

  21. With Great Communities, Comes Great Responsibility • Evong will collect the course evaluations: • SEAS official evaluation (bubble form with general comments space) used for determining whether or not to offer CS200 again and whether or not to fire me • My own course improvement survey (lots of specific questions for improving future CS200s) • Make sure to give her your form on the last day of class or earlier CS 200 Spring 2004

  22. From the Course Pledge I will provide useful feedback. I realize this is an evolving course and it is important that I let the course staff know what they need to improve the course. I will not wait until the end of the course to make the course staff aware of any problems. … I will fill out all course evaluation surveys honestly and thoroughly. CS 200 Spring 2004

  23. Nondeterministic TMs • Multiple transitions possible at every step • Follow all possible steps: • Each possible execution maintains its own state and its own infinite tape • If any possible execution halts (in an accepting state), the NTM halts, and the tape output of that execution is the NTM output CS 200 Spring 2004

  24. Power of NTM • Can NTMs computing anything TMs cannot compute? • Can NTMs compute faster than TMs? No – you can simulate a NTM with a regular TM: keep track of all possible states on the tape keep track of all possible tapes on the tape (really long, but its an infinitely long tape) Maybe! No one has proved it one way or the other. CS 200 Spring 2004

  25. This is amazing! • An NTM is a machine that every time it has to make a decision can just make both and use whichever one turns out to be right later • Equivalently: whenever an NTM has to make a decision, it always makes the right one! • We don’t know if a computer that always makes the right decision is really better than one that has to try all possible decisions CS 200 Spring 2004

  26. Complexity Class P Class P: problems that can be solved in polynomial time by a deterministic TM. 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 2004

  27. Complexity Class NP Class NP: problems that can be solved in polynomial time by a nondeterministic TM If we could try all possible solutions at once, we could identify the solution in polynomial time. All problems in P are in NP. Hard problems like the smiley puzzle and the pegboard puzzle sorting are all in NP (but might not be in P). CS 200 Spring 2004

  28. Is the Pegboard Puzzle in P? No one knows! We can’t find a O(nk) solution. We can’t prove one doesn’t exist. CS 200 Spring 2004

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

  30. 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 2004

  31. Charge • PS8 is due less than one week from now! • Next time: • How to prove a problem is in NP • How to prove a problem is one of the hardest problems in NP • Why the peg board puzzle, perfect photomosaic problem, drug discovery problem, traveling salesperson problem, map coloring problem, etc. are all really the same problem CS 200 Spring 2004

More Related