240 likes | 357 Views
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
E N D
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 • Due now • Project #3 • You sent me your chosen theorem, right? • Help session: Tuesday at 5pm in 1066 TMCB • Scope: 1-2 pages
Objectives • To sketch the proof of the average case analysis of Quicksort
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 …
Quicksort: Average Case Analysis • What is the Sample Space?
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
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
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
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
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)
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)
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)
Probability Mass Function • The probability mass function of random variable is just : • In general, for pivot position : … … 1/n
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)
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)
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
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.
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)
Assignment Homework: HW #8.5 • Some more probability exercises