CSE 2331/5331
-
Upload
lacey-conley -
Category
Documents
-
view
27 -
download
0
description
Transcript of CSE 2331/5331
![Page 1: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/1.jpg)
CSE 2331/5331
CSE 2331/5331
Topic 5:
Quick sort Deterministic
Randomized
![Page 2: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/2.jpg)
Sorting Revisited !
Quick-sort Divide and conquer paradigm But in place sorting Worst case: Randomized quicksort:
Expected running time:
CSE 2331/5331
![Page 3: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/3.jpg)
CSE 2331/5331
Divide and Conquer
MergeSort ( A, r, s ) if ( r ≥ s) return; m = (r+s) / 2; A1 = MergeSort ( A, r, m ); A2 = MergeSort ( A, m+1, s ); Merge (A1, A2);
![Page 4: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/4.jpg)
CSE 2331/5331
Divide and Conquer
QuickSort ( A, r, s ) if ( r ≥ s) return; m = Partition ( A, r, s ); A1 = QuickSort ( A, r, m-1 ); A2 = QuickSort ( A, m+1, s );
Merge (A1, A2);
![Page 5: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/5.jpg)
CSE 2331/5331
Divide and Conquer
QuickSort ( A, r, s ) if ( r ≥ s) return; m = Partition ( A, r, s ); A1 = QuickSort ( A, r, m-1 ); A2 = QuickSort ( A, m+1, s );
Merge (A1, A2);
A[m]: pivot
![Page 6: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/6.jpg)
Partition
m = Partition(A, p, r) Afterwards
All elements in A[p, …, m-1] have value at most A[m] All elements in A[m, …, r] have value at least A[m]
CSE 2331/5331
![Page 7: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/7.jpg)
CSE 2331/5331
Partition ( A, p, r)
Plan: take A[r] as pivot
return m
In-place partition !
xy
![Page 8: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/8.jpg)
CSE 2331/5331
Partition ( A, p, r )
xy
Case 1: y > x
xz y
![Page 9: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/9.jpg)
CSE 2331/5331
Partition ( A, p, r )
xz y
Case 2: otherwise
xy z
![Page 10: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/10.jpg)
CSE 2331/5331
Complexity:O (p - r)
![Page 11: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/11.jpg)
CSE 2331/5331
Quicksort ( A, r, s)
QuickSort ( A, p, r ) if ( p ≥ r ) return; m = Partition ( A, p, r ); A1 = QuickSort ( A, p, m-1 ); A2 = QuickSort ( A, m+1, r );
In-place
Initial call is QuickSort(A, 1, n).
![Page 12: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/12.jpg)
CSE 2331/5331
Complexity
T(n) = T(m-1) + T(n-m) + n
Worst case: T(n) = T(0) + T(n-1) + n
= T(n-1) + n
Best case: T(n) = 2T(n/2) + n
![Page 13: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/13.jpg)
Remarks
If input array is sorted or reversely sorted: Quadratic time complexity
Mixing splits Good ones will dominate
CSE 2331/5331
![Page 14: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/14.jpg)
Balanced Partitioning
Imagine instead of splitting in the middle, we has a 9-to-1 split
O(n lg n) Any constant fraction α works the same:
When we have a mixture of good and bad
partitioning, the good ones dominates.
CSE 2331/5331
![Page 15: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/15.jpg)
Question
How can we have a good mix ?
CSE 2331/5331
Intuitively, we will use randomization to achieve that.
![Page 16: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/16.jpg)
Expected Complexity
CSE 2331/5331
Probabilistic method: Given a distribution for all possible inputs Derive expected time based on distribution
Randomized algorithm: Add randomness in the algorithm Analyze the expected behavior of the algorithm
![Page 17: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/17.jpg)
CSE 2331/5331
Probabilistic Analysis
Simple example: Assume there are only two types of inputs:
half are best cases, half are worst
Worst: T(n) = ( )n 2
Best: T(n) = ( n lg n )
For all possible inputs, average time A(n) = 1/2 n lg n + 1/2
= ( )n 2
n 2
Randomization will not assume input distribution
![Page 18: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/18.jpg)
CSE 2331/5331
Randomized-QuickSort ( A, p, r ) If ( p ≥ r) return; m = Randomized-Partition ( A, p, r ); A1 = Randomized-QuickSort ( A, p, m-1 ); A2 = Randomized-QuickSort ( A, m+1, r );
![Page 19: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/19.jpg)
CSE 780 Algorithms
Random-Partition ( A, r, s )
y
Randomly choose position s, take y = A[s] as pivot.
yx x
Next, run the samePartition (A, p, r)
![Page 20: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/20.jpg)
Time Complexity?
Intuitively, with constant probability, the pivot will cause a balanced partition.
How to make this more rigorous?
CSE 2331/5331
![Page 21: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/21.jpg)
CSE 2331/5331
Randomized-QuickSort ( A, p, r ) If ( p ≥ r) return; s = Randomized-Partition ( A, p, r ); A1 = Randomized-QuickSort ( A, p, s-1 ); A2 = Randomized-QuickSort ( A, s+1, r );
![Page 22: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/22.jpg)
= expected running time of Randomized-QuickSort(A, 1, n)
Assume input array has no duplicates
CSE 2331/5331
![Page 23: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/23.jpg)
Randomized-QuickSort ( A, 1, n ) If ( 1 ≥ n) return; s = Randomized-Partition ( A, 1, n ); A1 = Randomized-QuickSort ( A, 1, s-1 ); A2 = Randomized-QuickSort ( A, s+1, n );
CSE 2331/5331
![Page 24: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/24.jpg)
= expected running time of Randomized-QuickSort(A, 1, n)
Assume input array has no duplicates
CSE 2331/5331
One can use substitution method to show that ET(n) = O(n lg n)
![Page 25: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/25.jpg)
Another Way of Analysis
CSE 2331/5331
![Page 26: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/26.jpg)
CSE 2331/5331
![Page 27: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/27.jpg)
Yet Another Way of Analysis
Using the indicator random variable
Requires global understanding of the algorithm
The key is to identify the right indicator random variable Similar to identify the right events to compute
expectation.
CSE 2331/5331
![Page 28: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/28.jpg)
CSE 2331/5331
Indicator Random Variable
Goal: count the expected number of heads in n coin flips
Straightforward: X : number of heads in n coin flips E[X] = ∑ k Prob[X = k]
New approach: X[j] = I { the j’th flip is a head }
= 1 if j’th flip is a head
0 otherwise
Indicator randomvariable
![Page 29: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/29.jpg)
CSE 2331/5331
Indicator Random Variable cont.
Lemma: Let X = I { claim A }. Then,
E[X] = Prob[claim A is true].
![Page 30: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/30.jpg)
CSE 2331/5331
Counting Heads
Y : number of heads in n coin flips
th flip is head ]
![Page 31: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/31.jpg)
CSE 2331/5331
Rand-QuickSort
Given A, suppose z1 , z2 , …. , zn are its elements in sorted order.
Indicator random variable: X(i,j) = 1 if is compared to
0 otherwise
z i z j
Complexity = O(# comparisons Y) + O(n) Y = ∑ ∑ X(i,j)
i j
Why?
![Page 32: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/32.jpg)
CSE 2331/5331
Pr[i,j]
Expected Complexity
E[Y] = E [ ∑ ∑ X(i,j) ] = ∑ ∑ E [ X(i,j) ]
= ∑ ∑ Pr { zi is compared to zj }
Z[i, j] : { zi , zi+1…. zj }
![Page 33: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/33.jpg)
CSE 2331/5331
Key Observation
If two elements in different subarray Will never be compared from then on
If any first chosen as pivot, X[i, j] = 0
X[i,j] = 1 if and only if zi or zj is the first among Z[i,j] as privot
Pr[i,j] = Pr[ zi or zj is the first pivot chosen from Z[i,j] ] = Pr [ zi is the first pivot chosen from Z[i,j] ] + Pr[ zj is the first pivot chosen from Z[i,j] ] = 1 / (j - i + 1) + 1 / (j - i + 1) = 2 / ( j - i + 1)
Why?
![Page 34: CSE 2331/5331](https://reader036.fdocuments.in/reader036/viewer/2022062422/5681345e550346895d9b43a8/html5/thumbnails/34.jpg)
CSE 2331/5331
Expected Complexity
E[X] = ∑ ∑ 2 / ( j - i + 1 )
…
= O ( n lg n )