Algorithm Design and Analysisudel.edu/~amotong/teaching/algorithm design...Randomized Algorithms...
Transcript of Algorithm Design and Analysisudel.edu/~amotong/teaching/algorithm design...Randomized Algorithms...
Algorithm Design and Analysis
Summer 2018 Amo G. Tong 1
Lecture 4Randomized Algorithmsโข Randomized Quick Sort
โข Randomized Selection
Summer 2018 Amo G. Tong 2
โข Quick Sort:โข Take an element ๐ฅ = ๐ด[๐] in ๐ด[1,โฆ , ๐].
โข Partition into two subarrays ๐ด[1,โฆ , ๐ โ 1] and ๐ด[๐ + 1,โฆ , ๐] such that each element of ๐ด[1,โฆ , ๐ โ 1] is less than or equal to ๐ฅ which is less than or equal to each element of ๐ด[๐ + 1,โฆ , ๐].
โข Sort the subarrays recursively.
โข No need to combine.
Randomized Algorithms
Summer 2018 Amo G. Tong 3
Input: an array ๐ด[1,โฆ , ๐] of numbers
โข Quick Sort:
Randomized Algorithms
Summer 2018 Amo G. Tong 4
Input: an array ๐ด[1,โฆ , ๐] of numbers
PARTITION (๐ด, ๐, ๐)1 select an element ๐ฅ = ๐ด[๐] in ๐ด[๐. . ๐]2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
โข Quick Sort:
Randomized Algorithms
Summer 2018 Amo G. Tong 5
Input: an array ๐ด[1,โฆ , ๐] of numbers
0 9 5 2 1 ๐ = 0, exchange 5 with 1
0 1 2 5 9
PARTITION (๐ด, ๐, ๐)1 select an element ๐ฅ = ๐ด[๐] in ๐ด[๐. . ๐]2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
โข Quick Sort:
Randomized Algorithms
Summer 2018 Amo G. Tong 6
Input: an array ๐ด[1,โฆ , ๐] of numbers
0 9 5 2 10 9 1 2 5
๐ = 0, exchange 5 with 1
๐ = 1, exchange 0 with 0
0 1 2 5 9
PARTITION (๐ด, ๐, ๐)1 select an element ๐ฅ = ๐ด[๐] in ๐ด[๐. . ๐]2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
โข Quick Sort:
Randomized Algorithms
Summer 2018 Amo G. Tong 7
Input: an array ๐ด[1,โฆ , ๐] of numbers
0 9 5 2 10 9 1 2 50 9 1 2 5
๐ = 0, exchange 5 with 1
๐ = 1, exchange 0 with 0
๐ = 1
0 1 2 5 9
PARTITION (๐ด, ๐, ๐)1 select an element ๐ฅ = ๐ด[๐] in ๐ด[๐. . ๐]2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
โข Quick Sort:
Randomized Algorithms
Summer 2018 Amo G. Tong 8
Input: an array ๐ด[1,โฆ , ๐] of numbers
0 9 5 2 10 9 1 2 50 9 1 2 50 9 1 2 5
๐ = 0, exchange 5 with 1
๐ = 1, exchange 0 with 0
๐ = 1
๐ = 2, exchange 9 with 1
0 1 2 5 9
PARTITION (๐ด, ๐, ๐)1 select an element ๐ฅ = ๐ด[๐] in ๐ด[๐. . ๐]2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
โข Quick Sort:
Randomized Algorithms
Summer 2018 Amo G. Tong 9
Input: an array ๐ด[1,โฆ , ๐] of numbers
0 9 5 2 10 9 1 2 50 9 1 2 50 9 1 2 50 1 9 2 5
๐ = 0, exchange 5 with 1
๐ = 1, exchange 0 with 0
๐ = 1
๐ = 2, exchange 9 with 1
๐ = 3, exchange 9 with 2
0 1 2 5 9
PARTITION (๐ด, ๐, ๐)1 select an element ๐ฅ = ๐ด[๐] in ๐ด[๐. . ๐]2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
โข Quick Sort:
Randomized Algorithms
Summer 2018 Amo G. Tong 10
Input: an array ๐ด[1,โฆ , ๐] of numbers
0 9 5 2 10 9 1 2 50 9 1 2 50 9 1 2 50 1 9 2 50 1 2 9 5
๐ = 0, exchange 5 with 1
๐ = 1, exchange 0 with 0
๐ = 1
๐ = 2, exchange 9 with 1
๐ = 3, exchange 9 with 2
exchange 9 with 5
0 1 2 5 9
PARTITION (๐ด, ๐, ๐)1 select an element ๐ฅ = ๐ด[๐] in ๐ด[๐. . ๐]2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
โข Quick Sort:
Randomized Algorithms
Summer 2018 Amo G. Tong 11
Input: an array ๐ด[1,โฆ , ๐] of numbers
0 9 5 2 10 9 1 2 50 9 1 2 50 9 1 2 50 1 9 2 50 1 2 9 50 1 2 5 9
๐ = 0, exchange 5 with 1
๐ = 1, exchange 0 with 0
๐ = 1
๐ = 2, exchange 9 with 1
๐ = 3, exchange 9 with 2
exchange 9 with 5
Return 4
0 1 2 5 9
PARTITION (๐ด, ๐, ๐)1 select an element ๐ฅ = ๐ด[๐] in ๐ด[๐. . ๐]2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
โข Quick Sort:
Randomized Algorithms
Summer 2018 Amo G. Tong 12
Input: an array ๐ด[1,โฆ , ๐] of numbers
QUICKSORT (๐ด, ๐, ๐)1 if ๐ < ๐2 ๐ =PARTITION(A, p, r)3 QUICKSORT (๐ด, ๐, ๐ โ 1);4 QUICKSORT (๐ด, ๐ + 1, ๐);5 end
PARTITION (๐ด, ๐, ๐)1 select an element ๐ฅ = ๐ด[๐] in ๐ด[๐. . ๐]2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
0 9 5 2 1
0 1 2 5 9
โข Quick Sort:
Randomized Algorithms
Summer 2018 Amo G. Tong 13
Input: an array ๐ด[1,โฆ , ๐] of numbers
QUICKSORT (๐ด, ๐, ๐)1 if ๐ < ๐2 ๐ =PARTITION(A, p, r)3 QUICKSORT (๐ด, ๐, ๐ โ 1);4 QUICKSORT (๐ด, ๐ + 1, ๐);5 end
PARTITION (๐ด, ๐, ๐)1 select an element ๐ฅ = ๐ด[๐] in ๐ด[๐. . ๐]2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
Which element should we take?
โข Quick Sort:โข Take an element ๐ฅ in ๐ด[1,โฆ , ๐].
โข Partition into two subarrays ๐ด1[1, โฆ , ๐] and ๐ด2[1, โฆ , ๐] such that each element of ๐ด1 is less than or equal to x which is less than or equal to each element of ๐ด2. (๐ + ๐ = ๐ โ 1)
โข Sort the subarrays recursively.
โข No need to combine.
โข ๐ฅ decides the size of the two subarrays.โข ๐(๐) = ๐(๐) + ๐(๐) + ฮ(๐);
Randomized Algorithms
Summer 2018 Amo G. Tong 14
โข ๐ฅ decides the size of the two subarrays.โข ๐ ๐ = ๐ ๐ + ๐ ๐ + ฮ ๐ .
โข If the array is always evenly partitioned:โข ๐ฅ=median
Randomized Algorithms
Summer 2018 Amo G. Tong 15
โข ๐ฅ decides the size of the two subarrays.โข ๐ ๐ = ๐ ๐ + ๐ ๐ + ฮ ๐ .
โข If the array is always evenly partitioned:โข ๐ฅ=median
โข ๐ ๐ = ๐ ๐/2 + ๐ ๐/2 + ฮ ๐ .
โข ๐(๐) = ฮ(๐ log ๐)
Randomized Algorithms
Summer 2018 Amo G. Tong 16
โข ๐ฅ decides the size of the two subarrays.โข ๐ ๐ = ๐ ๐ + ๐ ๐ + ฮ ๐ .
โข If the array is always evenly partitioned:โข ๐ฅ=median
โข ๐ ๐ = ๐ ๐/2 + ๐ ๐/2 + ฮ ๐ .
โข ๐(๐) = ฮ(๐ log ๐)
โข If the array is always partitioned with a fixed proportion ๐:โข x= the ๐๐ -th smallest element
โข ๐(๐) = ๐(๐๐) + ๐((1 โ ๐)๐) + ฮ(๐) where 0 < ๐ < 1;
โข ๐(๐) = ฮ(๐ log ๐)
Randomized Algorithms
Summer 2018 Amo G. Tong 17
โข ๐ฅ decides the size of the two subarrays.โข ๐ ๐ = ๐ ๐ + ๐ ๐ + ฮ ๐ .
โข If one of the subarray is always empty:โข ๐ฅ= the maximum or the minimum
โข ๐(๐) = ๐(๐ โ 1) + ๐(0) + ฮ(๐)
โข ๐(๐) = ฮ(๐2)
Randomized Algorithms
Summer 2018 Amo G. Tong 18
โข ๐ฅ decides the size of the two subarrays.โข ๐ ๐ = ๐ ๐ + ๐ ๐ + ฮ ๐ .
โข If one of the subarray is always empty:โข ๐ฅ= the maximum or the minimum
โข ๐(๐) = ๐(๐ โ 1) + ๐(0) + ฮ(๐)
โข ๐(๐) = ฮ(๐2)
โข If one of the subarray has a constant length ๐:โข x= the a-th smallest element
โข ๐(๐) = ๐(๐ โ 1 โ ๐) + ๐(๐) + ฮ(๐) where 0 < ๐ < ๐;
โข ๐(๐) =? ? (try it yourself)
Randomized Algorithms
Summer 2018 Amo G. Tong 19
โข If we always select the last element as the pivot:
Randomized Algorithms
Summer 2018 Amo G. Tong 20
โข If we always select the last element as the pivot:โข Worst-case running time:
โข ๐(๐) = ๐(๐ โ 1) + ๐(0) + ฮ(๐)
โข ๐(๐) = ๐(๐2)
Randomized Algorithms
Summer 2018 Amo G. Tong 21
โข If we always select the last element as the pivot:โข Worst-case running time:
โข ๐(๐) = ๐(๐ โ 1) + ๐(0) + ฮ(๐)
โข ๐(๐) = ๐(๐2)
โข Expected running time:โข Assume each of the possible permutations appears with the same
probability.
Randomized Algorithms
Summer 2018 Amo G. Tong 22
โข If we always select the last element as the pivot:โข Worst-case running time:
โข ๐(๐) = ๐(๐ โ 1) + ๐(0) + ฮ(๐)
โข ๐(๐) = ๐(๐2)
โข Expected running time:โข Assume each of the possible permutations appears with the same
probability.
โข With probability 1/n, the last element is the ๐-th smallest.
Randomized Algorithms
Summer 2018 Amo G. Tong 23
โข If we always select the last element as the pivot:โข Worst-case running time:
โข ๐(๐) = ๐(๐ โ 1) + ๐(0) + ฮ(๐)
โข ๐(๐) = ๐(๐2)
โข Expected running time:โข Assume each of the possible permutations appears with the same
probability.
โข With probability 1/n, the last element is the ๐-th smallest.
โข If the last element is the ๐-th smallest, ๐ ๐ = T(๐ โ 1) + ๐(๐ โ ๐) + ๐๐
Randomized Algorithms
Summer 2018 Amo G. Tong 24
โข If we always select the last element as the pivot:โข Worst-case running time:
โข ๐(๐) = ๐(๐ โ 1) + ๐(0) + ฮ(๐)
โข ๐(๐) = ๐(๐2)
โข Expected running time:โข Assume each of the possible permutations appears with the same
probability.
โข With probability 1/n, the last element is the ๐-th smallest.
โข If the last element is the ๐-th smallest, ๐ ๐ = T(๐ โ 1) + ๐(๐ โ ๐) + ๐๐
โข The expected value is
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐ธ[๐ ๐ โ 1 ] + ๐ธ[๐ ๐ โ ๐ ] + ๐๐)
Randomized Algorithms
Summer 2018 Amo G. Tong 25
Randomized Algorithms
Summer 2018 Amo G. Tong 26
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐(๐ โ 1) + ๐(๐ โ ๐) + ๐๐)โ ๐ธ ๐ ๐ = O(n log n)
Proof: Let ๐ ๐ โค ๐ for ๐ < 2.
Randomized Algorithms
Summer 2018 Amo G. Tong 27
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐(๐ โ 1) + ๐(๐ โ ๐) + ๐๐)โ ๐ธ ๐ ๐ = O(n log n)
Proof: Let ๐ ๐ โค ๐ for ๐ < 2.Inductive hypothesis: ๐ธ ๐ ๐ โค ๐ ๐ log๐
Randomized Algorithms
Summer 2018 Amo G. Tong 28
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐(๐ โ 1) + ๐(๐ โ ๐) + ๐๐)โ ๐ธ ๐ ๐ = O(n log n)
Proof: Let ๐ ๐ โค ๐ for ๐ < 2.Inductive hypothesis: ๐ธ ๐ ๐ โค ๐ ๐ log๐
Basic: ๐ธ[๐(2)] =1
2(๐(0) + ๐(1) + ๐ โ 2) +
1
2(๐(1) + ๐(0) + ๐ โ 2) = 2๐ + 2๐.
We need 2๐ + 2๐ < 2๐.
Randomized Algorithms
Summer 2018 Amo G. Tong 29
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐(๐ โ 1) + ๐(๐ โ ๐) + ๐๐)โ ๐ธ ๐ ๐ = O(n log n)
Proof: Let ๐ ๐ โค ๐ for ๐ < 2.Inductive hypothesis: ๐ธ ๐ ๐ โค ๐ ๐ log๐
Basic: ๐ธ[๐(2)] =1
2(๐(0) + ๐(1) + ๐ โ 2) +
1
2(๐(1) + ๐(0) + ๐ โ 2) = 2๐ + 2๐.
We need 2๐ + 2๐ < 2๐.Induction: Suppose ๐ธ ๐ ๐ โค ๐ ๐ log ๐ for all 2 < ๐ < ๐.
Randomized Algorithms
Summer 2018 Amo G. Tong 30
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐(๐ โ 1) + ๐(๐ โ ๐) + ๐๐)โ ๐ธ ๐ ๐ = O(n log n)
Proof: Let ๐ ๐ โค ๐ for ๐ < 2.Inductive hypothesis: ๐ธ ๐ ๐ โค ๐ ๐ log๐
Basic: ๐ธ[๐(2)] =1
2(๐(0) + ๐(1) + ๐ โ 2) +
1
2(๐(1) + ๐(0) + ๐ โ 2) = 2๐ + 2๐.
We need 2๐ + 2๐ < 2๐.Induction: Suppose ๐ธ ๐ ๐ โค ๐ ๐ log ๐ for all 2 < ๐ < ๐.
๐ฌ[๐ป(๐)] = ฯ๐=๐๐ ๐
๐(๐ฌ[๐ป ๐ โ ๐ ] + ๐ฌ[๐ป ๐ โ ๐ ] + ๐๐)
Randomized Algorithms
Summer 2018 Amo G. Tong 31
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐(๐ โ 1) + ๐(๐ โ ๐) + ๐๐)โ ๐ธ ๐ ๐ = O(n log n)
Proof: Let ๐ ๐ โค ๐ for ๐ < 2.Inductive hypothesis: ๐ธ ๐ ๐ โค ๐ ๐ log๐
Basic: ๐ธ[๐(2)] =1
2(๐(0) + ๐(1) + ๐ โ 2) +
1
2(๐(1) + ๐(0) + ๐ โ 2) = 2๐ + 2๐.
We need 2๐ + 2๐ < 2๐.Induction: Suppose ๐ธ ๐ ๐ โค ๐ ๐ log ๐ for all 2 < ๐ < ๐.
๐ธ[๐(๐)] = ฯ๐=1๐ 1
๐(๐ธ[๐ ๐ โ 1 ] + ๐ธ[๐ ๐ โ ๐ ] + ๐๐)
=๐
๐(๐ฯ๐=๐
๐โ๐๐ฌ[๐ป ๐ ] + ๐๐๐)
Randomized Algorithms
Summer 2018 Amo G. Tong 32
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐(๐ โ 1) + ๐(๐ โ ๐) + ๐๐)โ ๐ธ ๐ ๐ = O(n log n)
Proof: Let ๐ ๐ โค ๐ for ๐ < 2.Inductive hypothesis: ๐ธ ๐ ๐ โค ๐ ๐ log๐
Basic: ๐ธ[๐(2)] =1
2(๐(0) + ๐(1) + ๐ โ 2) +
1
2(๐(1) + ๐(0) + ๐ โ 2) = 2๐ + 2๐.
We need 2๐ + 2๐ < 2๐.Induction: Suppose ๐ธ ๐ ๐ โค ๐ ๐ log ๐ for all 2 < ๐ < ๐.
๐ธ[๐(๐)] = ฯ๐=1๐ 1
๐(๐ธ[๐ ๐ โ 1 ] + ๐ธ[๐ ๐ โ ๐ ] + ๐๐)
=1
๐(2ฯ๐=0
๐โ1๐ธ[๐ ๐ ] + ๐๐2)
=๐
๐(๐ฯ๐=๐
๐โ๐๐ฌ[๐ป ๐ ] + ๐๐ป(๐) + ๐๐ป(๐) + ๐๐๐)
Randomized Algorithms
Summer 2018 Amo G. Tong 33
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐(๐ โ 1) + ๐(๐ โ ๐) + ๐๐)โ ๐ธ ๐ ๐ = O(n log n)
Proof: Let ๐ ๐ โค ๐ for ๐ < 2.Inductive hypothesis: ๐ธ ๐ ๐ โค ๐ ๐ log๐
Basic: ๐ธ[๐(2)] =1
2(๐(0) + ๐(1) + ๐ โ 2) +
1
2(๐(1) + ๐(0) + ๐ โ 2) = 2๐ + 2๐.
We need 2๐ + 2๐ < 2๐.Induction: Suppose ๐ธ ๐ ๐ โค ๐ ๐ log ๐ for all 2 < ๐ < ๐.
๐ธ[๐(๐)] = ฯ๐=1๐ 1
๐(๐ธ[๐ ๐ โ 1 ] + ๐ธ[๐ ๐ โ ๐ ] + ๐๐)
=1
๐(2ฯ๐=0
๐โ1๐ธ[๐ ๐ ] + ๐๐2)
=1
๐(2ฯ๐=2
๐โ1๐ธ[๐ ๐ ] + 2๐(0) + 2๐(1) + ๐๐2)
โค๐
๐(๐ฯ๐=๐
๐โ๐๐ ๐ ๐ฅ๐จ๐ ๐) + (๐๐ + ๐๐๐)/๐
Randomized Algorithms
Summer 2018 Amo G. Tong 34
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐(๐ โ 1) + ๐(๐ โ ๐) + ๐๐)โ ๐ธ ๐ ๐ = O(n log n)
Proof: Let ๐ ๐ โค ๐ for ๐ < 2.Inductive hypothesis: ๐ธ ๐ ๐ โค ๐ ๐ log๐
Basic: ๐ธ[๐(2)] =1
2(๐(0) + ๐(1) + ๐ โ 2) +
1
2(๐(1) + ๐(0) + ๐ โ 2) = 2๐ + 2๐.
We need 2๐ + 2๐ < 2๐.Induction: Suppose ๐ธ ๐ ๐ โค ๐ ๐ log ๐ for all 2 < ๐ < ๐.
๐ธ[๐(๐)] = ฯ๐=1๐ 1
๐(๐ธ[๐ ๐ โ 1 ] + ๐ธ[๐ ๐ โ ๐ ] + ๐๐)
=1
๐(2ฯ๐=0
๐โ1๐ธ[๐ ๐ ] + ๐๐2)
=1
๐(2ฯ๐=2
๐โ1๐ธ[๐ ๐ ] + 2๐(0) + 2๐(1) + ๐๐2)
โค2๐
๐(ฯ๐=2
๐โ1 ๐ log ๐) + (4๐ + ๐๐2)/๐
(see additional reading material)
โค2b
๐(
๐2
2log ๐ โ
๐2
4+
1
4) + (4๐ + ๐๐2)/๐
Randomized Algorithms
Summer 2018 Amo G. Tong 35
We need this to be โค ๐ ๐ log ๐.
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐(๐ โ 1) + ๐(๐ โ ๐) + ๐๐)โ ๐ธ ๐ ๐ = O(n log n)
Proof: Let ๐ ๐ โค ๐ for ๐ < 2.Inductive hypothesis: ๐ธ ๐ ๐ โค ๐ ๐ log๐
Basic: ๐ธ[๐(2)] =1
2(๐(0) + ๐(1) + ๐ โ 2) +
1
2(๐(1) + ๐(0) + ๐ โ 2) = 2๐ + 2๐.
We need 2๐ + 2๐ < 2๐.Induction: Suppose ๐ธ ๐ ๐ โค ๐ ๐ log ๐ for all 2 < ๐ < ๐.
๐ธ[๐(๐)] = ฯ๐=1๐ 1
๐(๐ธ[๐ ๐ โ 1 ] + ๐ธ[๐ ๐ โ ๐ ] + ๐๐)
=1
๐(2ฯ๐=0
๐โ1๐ธ[๐ ๐ ] + ๐๐2)
=1
๐(2ฯ๐=2
๐โ1๐ธ[๐ ๐ ] + 2๐(0) + 2๐(1) + ๐๐2)
โค2๐
๐(ฯ๐=2
๐โ1 ๐ log ๐) + (4๐ + ๐๐2)/๐
(see additional reading material)
โค2b
๐(
๐2
2log ๐ โ
๐2
4+
1
4) + (4๐ + ๐๐2)/๐
โข If we always select the last element as the pivot:โข Worst-case running time:
โข ๐(๐) = ๐(๐ โ 1) + ๐(0) + ฮ(๐)
โข ๐(๐) = ๐(๐2)
โข Expected running time:โข Assume each of the possible permutations appears with the same
probability.
โข The expected running time is
๐ธ ๐ ๐ = ฮ(๐ log ๐)
Randomized Algorithms
Summer 2018 Amo G. Tong 36
โข If we always select the last element as the pivot:โข Worst-case running time:
โข ๐(๐) = ๐(๐ โ 1) + ๐(0) + ฮ(๐)
โข ๐(๐) = ๐(๐2)
โข Expected running time:โข Assume each of the possible permutations appears with the same
probability.
โข The expected running time is
๐ธ ๐ ๐ = ฮ(๐ log ๐)
Randomized Algorithms
Summer 2018 Amo G. Tong 37
This is not always true.We want a ฮ(๐ log ๐)algorithm which does not rely on this assumption
โข Randomized Quick Sort;โข Select the pivot uniformly at random from the given array.
Randomized Algorithms
Summer 2018 Amo G. Tong 38
โข Randomized Quick Sort;โข Select the pivot uniformly at random from the given array.
โข With probability 1/๐, ๐ด[๐] is selected as the pivot.
โข With probability 1/๐, the ๐-th smallest element is selected.
Randomized Algorithms
Summer 2018 Amo G. Tong 39
โข Randomized Quick Sort;โข Select the pivot uniformly at random from the given array.
โข With probability 1/๐, ๐ด[๐] is selected as the pivot.
โข With probability 1/๐, the ๐-th smallest element is selected.
Randomized Algorithms
Summer 2018 Amo G. Tong 40
RANDOM-PARTITION (๐ด, ๐, ๐)1 randomly select an element x = ๐ด[๐] in ๐ด[๐. . ๐],
where each element has the same probability to be selected.2 exchange A[k] with A[r];3 ๐ = ๐ โ 14 for ๐ = ๐ to ๐ โ 15 if ๐ด ๐ โค ๐ฅ6 ๐ = ๐ + 17 exchange ๐ด[๐] with ๐ด[๐]8 end9 end10 exchange ๐ด[๐ + 1] with ๐ด[๐]11 return ๐ + 1;
โข Randomized Quick Sort;โข Select the pivot uniformly at random from the given array.
โข With probability 1/๐, ๐ด[๐] is selected as the pivot.
โข With probability 1/๐, the ๐-th smallest element is selected.
Randomized Algorithms
Summer 2018 Amo G. Tong 41
QUICKSORT (๐ด, ๐, ๐)1 if ๐ < ๐2 ๐ =PARTITION(A, p, r)3 QUICKSORT (๐ด, ๐, ๐ โ 1);4 QUICKSORT (๐ด, ๐ + 1, ๐);5 end
RANDOM-QUICKSORT (๐ด, ๐, ๐)1 if ๐ < ๐2 ๐ =RANDOM-PARTITION(A, p, r)3 RANDOM-QUICKSORT (๐ด, ๐, ๐ โ 1);4 RANDOM-QUICKSORT (๐ด, ๐ + 1, ๐);5 end
โข Randomized Quick Sort;โข Select the pivot uniformly at random from the given array.
โข With probability 1/๐, ๐ด[๐] is selected as the pivot.
โข With probability 1/๐, the ๐-th smallest element is selected.
โข Expected running time:โข No assumption on the input.
โข The expected running time is
๐ธ ๐ ๐ = ฯ๐=1๐ 1
๐(๐ธ[๐ ๐ โ 1 ] + ๐ธ[๐ ๐ โ ๐ ] + ๐๐)
Randomized Algorithms
Summer 2018 Amo G. Tong 42
The same as that in page 24.
โข Selection:
Randomized Algorithms
Summer 2018 Amo G. Tong 43
Input: an array ๐ด[1,โฆ , ๐] of distinct numbers and an integer ๐, 1 โค ๐ โค ๐.
Output: the ๐-th smallest element in ๐ด[1,โฆ , ๐] .
โข Selection:
Randomized Algorithms
Summer 2018 Amo G. Tong 44
Pseudocode:RANDOMIZED-SELECT(๐ด, ๐, ๐, ๐)1 if ๐ == ๐2 return A[p]3 end4 q=RANDOM-PARTITION(๐ด, ๐, ๐)5 ๐ = ๐ โ ๐ + 1;6 if ๐ == ๐7 return ๐ด[๐]8 end9 if ๐ < ๐10 return RANDOM-SELECT(๐ด, ๐, ๐ โ 1, ๐)11 else12 return RANDOM-SELECT(๐ด, ๐ + 1, ๐, ๐ โ ๐)13 end
โข Selection:
Randomized Algorithms
Summer 2018 Amo G. Tong 45
Pseudocode:RANDOMIZED-SELECT(๐ด, ๐, ๐, ๐)1 if ๐ == ๐2 return A[p]3 end4 ๐ =RANDOM-PARTITION(๐ด, ๐, ๐)5 ๐ = ๐ โ ๐ + 1;6 if ๐ == ๐7 return ๐ด[๐]8 end9 if ๐ < ๐10 return RANDOMIZED-SELECT(๐ด, ๐, ๐ โ 1, ๐)11 else12 return RANDOMIZED-SELECT(๐ด, ๐ + 1, ๐, ๐ โ ๐)13 end
ฮ(๐)
๐(max(๐ โ ๐, ๐ โ ๐))
T(๐)
โข Randomized selection;โข Select the pivot uniformly at random from the given array.
โข With probability1/๐, ๐ด[๐] is selected as the pivot.
โข With probability1/๐, the ๐-th smallest element is selected.
โข Running time:โข If the ๐-th smallest element is selected as the pivot, the running time is
๐ ๐ = ๐ max ๐ โ 1, ๐ โ ๐ + ฮ(๐)
โข So the expected running time is:
๐ธ ๐ ๐ =
๐=0
๐โ11
๐(๐ธ[๐ max ๐ โ 1, ๐ โ ๐ ] + ฮ(๐) )
Randomized Algorithms
Summer 2018 Amo G. Tong 46
Randomized Algorithms
Summer 2018 Amo G. Tong 47
๐ธ ๐ ๐
=
๐=1
๐1
๐(๐ธ[๐ max ๐ โ 1, ๐ โ ๐ ] + ฮ(๐) )
=
๐=1
๐/2 โ11
๐(๐ธ[๐ max ๐ โ 1, ๐ โ ๐ ] + ฮ(๐) ) +
๐= ๐/2 โ1
๐1
๐(๐ธ[๐ max ๐ โ 1, ๐ โ ๐ ] + ฮ(๐) )
=
๐=1
๐/2 โ11
๐(๐ธ[๐ ๐ โ ๐ ] + ฮ(๐) ) +
๐= ๐/2
๐1
๐(๐ธ[๐ ๐ โ 1 ] + ฮ(๐) )
=
๐= ๐/2
๐2
๐(๐ธ[๐ ๐ โ 1 ] + ฮ(๐) )
๐ธ ๐ ๐ =
๐= ๐/2
๐2
๐(๐ธ[๐ ๐ โ 1 ] + ฮ(๐) )
Prove ๐ธ[๐(๐)] = ๐(๐)
โข Expected running time:โข The algorithm is not randomized. The expectation is taken over the
distribution of the inputs.
Randomized Algorithms
Summer 2018 Amo G. Tong 48
โข Expected running time:โข The algorithm is not randomized. The expectation is taken over the
distribution of the inputs.
โข The algorithm is randomized. The expected running time is ๐ถ(๐(๐)) for any given input.
Randomized Algorithms
Summer 2018 Amo G. Tong 49
โข Expected running time:โข The algorithm is not randomized. The expectation is taken over the
distribution of the inputs.
โข The algorithm is randomized. The expected running time is ๐(๐(๐)) for any given input.
โข The algorithm is randomized and the input is also sampled.
Randomized Algorithms
Summer 2018 Amo G. Tong 50
โข Expected running time:โข The algorithm is not randomized. The expectation is taken over the
distribution of the inputs.
โข The algorithm is randomized. The expected running time is ๐(๐(๐)) for any given input.
โข The algorithm is randomized and the input is also sampled.โข Consider the Randomized Quick Sort with an input uniformly sampled
from {all the permutations over {1, โฆ , ๐}}.
โข What is the running time? (exercise)
Randomized Algorithms
Summer 2018 Amo G. Tong 51