cs 312: algorithm analysis
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/1.jpg)
CS 312: Algorithm Analysis
Lecture #12: Average Case Analysis of Quicksort
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick
![Page 2: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/2.jpg)
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
![Page 3: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/3.jpg)
Objectives
To sketch the proof of the average case analysis of Quicksort
![Page 4: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/4.jpg)
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 β¦
![Page 5: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/5.jpg)
Quicksort: Average Case Analysis What is the Sample Space?
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
![Page 6: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/6.jpg)
Quicksort: Average Case Analysis What is the Random Variable?
Values ofRandomVariable
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Runtime on given input
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
![Page 7: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/7.jpg)
Quicksort: Average Case Analysis What is the probability distribution?
Probability
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
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
Values ofRandomVariable
Uniform
![Page 8: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/8.jpg)
Quicksort: Average Case Analysis Without loss of generality, Choose first element as pivot
Probability PivotValue
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
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
1
1
2
2
3
3
Value ofRandomVariable
![Page 9: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/9.jpg)
Quicksort: Average Case Analysis Do the pivot
Probability PivotLocation
1 2 3
1 3 2
1 2 3
1 2 3
1 2 3
2 1 3
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
1st
1st
2nd
2nd
3rd
3rd
Value ofRandomVariable
![Page 10: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/10.jpg)
Quicksort: Average Case Analysis Update the values of the RV to reflect the location of
the pivotProbability Pivot
Location
1/n!
1/n!
1/n!
1/n!
1/n!
1/n!
t(0)+t(2) + g(n)
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)
1st
1st
2nd
2nd
3rd
3rd
Where is theaverage time to sortan array of size m
And
is thelinear time to pivot
1 2 3
1 3 2
1 2 3
1 2 3
1 2 3
2 1 3
Value ofRandomVariable
![Page 11: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/11.jpg)
Quicksort: Average Case Analysis How many of each unique value of the RV are there?
Probability PivotLocation
1/n!
1/n!
1/n!
1/n!
1/n!
1/n!
t(0)+t(2) + g(n)
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)
1st
1st
2nd
2nd
3rd
3rd
1 2 3
1 3 2
1 2 3
1 2 3
1 2 3
2 1 3
(n-1)!
(n-1)!
(n-1)!
Value ofRandomVariable
![Page 12: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/12.jpg)
Quicksort: Average Case Analysis Group similar samples into events.
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)
1 2 3
1 2 3
1 2 3
Value ofRandomVariable
![Page 13: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/13.jpg)
Probability Mass Function The probability mass function of random variable is just :
In general, for pivot position :
π₯π‘ (0)+π‘ (πβ1)+π(π) π‘ (πβ1)+π‘(0)+π(π)π‘ (π/2)+π‘ (π /2)+π(π)
1/nπ (π₯)
β¦ β¦
![Page 14: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/14.jpg)
Quicksort: Average Case Analysis What is the Expected, or Average, value of , the
running time of Quicksort?
1
1( ) ( 1) ( )n
p
g n t p t n pn
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)
1
1 ( 1) ( ) ( )n
p
t p t n p g nn
( ) [ ] ( )x
t n E X p x x
![Page 15: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/15.jpg)
1
1
1 1
1( ) [ ] ( ) ( 1) ( )
1( ) ( 1) ( )
1( ) ( 1) ( )
n
p
n
p
n n
p p
t n E X g n t p t n pn
g n t p t n pn
g n t p t n pn
Quicksort: Average Case Analysis
![Page 16: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/16.jpg)
Quicksort: Average Case Analysis
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)
1 2 3
1 2 3
1 2 3
Value ofRandomVariable
![Page 17: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/17.jpg)
1
1
1 1
1 1
0 0
1 1
0 0
1
0
1( ) [ ] ( ) ( 1) ( )
1( ) ( 1) ( )
1( ) ( 1) ( )
1( ) ( ) ( )
2 2( ) ( ) ( 1) ( )
2Thus, ( ) ( ) (
n
p
n
p
n n
p p
n n
k k
n n
k k
n
k
t n E X g n t p t n pn
g n t p t n pn
g n t p t n pn
g n t k t kn
g n t k n t kn n
t n t kn
1)n
Quicksort: Average Case Analysis
![Page 18: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/18.jpg)
Quicksort: Average Case Analysis1
0
2( ) ( ) ( 1)n
k
t n t k nn
2( ) (0) ... ( 2) ( 1) ( 1)t n t t n t n nn
2( 1) (0) ... ( 2) ( 2)1
t n t t n nn
1 2 ( 1)( 2)( 1) (0) ... ( 2)n n nt n t t nn n n
1 2 ( 1)( 2)( ) ( 1) ( 1) ( 1)n n nt n t n t n nn n n
1 2 ( 1) ( 1)( 2)( ) ( 1)n n n n nt n t nn n n
![Page 19: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/19.jpg)
Quicksort: Average Case Analysis
1 2( 1)( ) ( 1)n nt n t nn n
1 2 2( 1)( ) ( 1)n nt n t nn n n
1 2 ( 1) ( 1)( 2)( ) ( 1)n n n n nt n t nn n n
![Page 20: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/20.jpg)
Quicksort: Average Case Analysis1 2( 1)( ) ( 1)n nt n t nn n
( ) ( 1) 2( 1)1 ( 1)
t n t n nn n n n
( )Let ( )
1t ny nn
2( 1)( ) ( 1)( 1)ny n y n
n n
1
2( 1)( )( 1)
n
i
iy ni i
1
1as , ( ) 2 2ln( 1)
n
i
n y n ni
( ) ( 1) ( ) 2( 1) ln ( log )t n n y n nn O nn
![Page 21: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/21.jpg)
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
![Page 22: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/22.jpg)
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.
![Page 23: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/23.jpg)
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)
![Page 24: CS 312: Algorithm Analysis](https://reader036.vdocuments.net/reader036/viewer/2022062811/56815ef0550346895dcdaf28/html5/thumbnails/24.jpg)
Assignment
Homework: HW #8.5 Some more probability exercises