1 / 24

CS 312: Algorithm Analysis

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License . CS 312: Algorithm Analysis. Lecture #12: Average Case Analysis of Quicksort. Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick. Announcements. Homework #8

dom
Download Presentation

CS 312: Algorithm Analysis

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. This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. CS 312: Algorithm Analysis Lecture #12: Average Case Analysis of Quicksort Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick

  2. Announcements • Homework #8 • Due now • Project #3 • You sent me your chosen theorem, right? • Help session: Tuesday at 5pm in 1066 TMCB • Scope: 1-2 pages

  3. Objectives • To sketch the proof of the average case analysis of Quicksort

  4. Average Case Analysis • Given an array of unique items. • How does Quicksort perform on average? • Specify sample space: set of all permutations of the list • Assume that each permutation is equally likely. i.e., the probability distribution 𝑝(𝑥) over possible permutations is uniform • Specify Random Variable to be the run-time of Quicksort as a function of . • Compute the expected value of according to the distribution • Let’s sketch the proof now …

  5. Quicksort: Average Case Analysis • What is the Sample Space?

  6. Quicksort: Average Case Analysis Runtime on given input • What is the Random Variable? Values ofRandom Variable t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step

  7. Quicksort: Average Case Analysis Uniform • What is the probability distribution? Values ofRandom Variable Probability 1/n! 1/n! 1/n! 1/n! 1/n! 1/n! t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step

  8. Quicksort: Average Case Analysis • Without loss of generality, Choose first element as pivot Value ofRandom Variable Pivot Value Probability 1/n! 1/n! 1/n! 1/n! 1/n! 1/n! t(left of pivot)+t(right of pivot) + linear pivot step 1 1 2 2 3 3 t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step

  9. Quicksort: Average Case Analysis • Do the pivot Value ofRandom Variable Pivot Location Probability 1/n! 1/n! 1/n! 1/n! 1/n! 1/n! t(left of pivot)+t(right of pivot) + linear pivot step 1st 1st 2nd 2nd 3rd 3rd t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step t(left of pivot)+t(right of pivot) + linear pivot step

  10. Quicksort: Average Case Analysis • Update the values of the RV to reflect the location of the pivot Value ofRandom Variable Pivot Location Probability 1/n! 1/n! 1/n! 1/n! 1/n! 1/n! t(0)+t(2) + g(n) 1st 1st 2nd 2nd 3rd 3rd Where is the average time to sort an array of size m And is thelinear time to pivot t(0)+t(2) + g(n) t(1)+t(1) + g(n) t(1)+t(1) + g(n) t(2)+t(0) + g(n) t(2)+t(0) + g(n)

  11. Quicksort: Average Case Analysis • How many of each unique value of the RV are there? Value ofRandom Variable Pivot Location Probability (n-1)! (n-1)! (n-1)! 1/n! 1/n! 1/n! 1/n! 1/n! 1/n! t(0)+t(2) + g(n) 1st 1st 2nd 2nd 3rd 3rd t(0)+t(2) + g(n) t(1)+t(1) + g(n) t(1)+t(1) + g(n) t(2)+t(0) + g(n) t(2)+t(0) + g(n)

  12. Quicksort: Average Case Analysis • Group similar samples into events. Value ofRandom Variable Probability (n-1)!/n! = 1/n (n-1)!/n! = 1/n (n-1)!/n! = 1/n t(0)+t(2) + g(n) t(1)+t(1) + g(n) t(2)+t(0) + g(n)

  13. Probability Mass Function • The probability mass function of random variable is just : • In general, for pivot position : … … 1/n

  14. Quicksort: Average Case Analysis • What is the Expected, or Average, value of , the running time of Quicksort? Probability that will be chosen as the pivot Cost of each possible pivot selection Cost of sorting the smallest elements (before the pivot) Cost of sorting the remaining elements (after the pivot)

  15. Quicksort: Average Case Analysis

  16. Quicksort: Average Case Analysis Value ofRandom Variable Probability (n-1)!/n! = 1/n (n-1)!/n! = 1/n (n-1)!/n! = 1/n t(0)+t(2) + g(n) t(1)+t(1) + g(n) t(2)+t(0) + g(n)

  17. Quicksort: Average Case Analysis

  18. Quicksort: Average Case Analysis

  19. Quicksort: Average Case Analysis

  20. Quicksort: Average Case Analysis

  21. Summary: Average Case Analysis • We have walked through the process for conducting an average case analysis: • Characterize your input • Sample space • Probability Distribution • Measure • Define your random variable: average running time of algorithm • Compute the expected value of this RV • Algebra • Requires cleverness with recurrence relations! • Asymptotic Order of Growth

  22. Fair Game? • You could pick this proof for the proof project and fill in the blanks • If you do so, include a section that lays out the high-level method for doing average case analysis in general! • Make clear which parts of the proof accomplish these steps.

  23. Probability theory • I expect you to know the definitions for our terms from probability theory: • Sample space • Sample • Event • probability distribution • random variable • value of a random variable • expected value of a random variable (with respect to a given probability distribution)

  24. Assignment Homework: HW #8.5 • Some more probability exercises

More Related