02 Lec
02 Lec
Introduction to Randomized
Algorithms: QuickSort
Lecture 2
August 27, 2020
Today
Randomized Algorithms – Two types
Las Vegas
Monte Carlo
Randomized Quick Sort
Introduction to Randomized
Algorithms
Input x Output y
Deterministic Algorithm
Input x Output y
Deterministic Algorithm
random bits r
Input x Output yr
Randomized Algorithm
QuickSort ?
1 Pick a pivot element from array
2 Split array into 3 subarrays: those smaller than pivot, those
larger than pivot, and the pivot itself.
3 Recursively sort the subarrays, and concatenate them.
Randomized QuickSort
1 Pick a pivot element uniformly at random from the array
2 Split array into 3 subarrays: those smaller than pivot, those
larger than pivot, and the pivot itself.
3 Recursively sort the subarrays, and concatenate them.
R , 2, 3 , U , 5
,
6, -
. .
,
100
-
I
too
0 2=3
-
,
-
= I \
Of 2 3, too
-
. -
witu¥dihG .
Problem
Given three n ⇥ n matrices A, B, C is AB = C ?
Problem
Given three n ⇥ n matrices A, B, C is AB = C ?
Deterministic algorithm:
1 Multiply A and B and check if equal to C .
2 Running time? O(n3 ) by straight forward approach. O(n2.37 )
with fast matrix multiplication (complicated and impractical).
' ' "
E'
"
a
I -
an:c .
Randomized algorithm:
1 Pick a random n ⇥ 1 vector r .
-ux I
2 Return the answer of the equality ABr = Cr .
3 Running time?
.
It
Chandra (UIUC) CS498ABD 8 Fall 2020 8 / 33
Example: Verifying Matrix Multiplication
Problem
Given three n ⇥ n matrices A, B, C is AB = C ?
Randomized algorithm:
1 Pick a random n ⇥ 1 vector r .
2 Return the answer of the equality ABr = Cr .
3 Running time? O(n2 )!
Randomized algorithm:
1 Pick a random n ⇥ 1 vector r .
2 Return the answer of the equality ABr = Cr .
3 Running time? O(n2 )!
Theorem
If AB = C then the algorithm will always say YES. If AB 6= C then
the algorithm will say YES with probability at most 1/2. Can repeat
the algorithm 100 times independently to reduce the probability of a
false positive to 1/2100 .
Chandra (UIUC) CS498ABD 8 Fall 2020 8 / 33
Why randomized algorithms?
1 Many many applications in algorithms, data structures and
computer science!
2 In some cases only known algorithms are randomized or
randomness is provably necessary.
3 Often randomized algorithms are (much) simpler and/or more
efficient.
4 Several deep connections to mathematics, physics etc.
5 ...
6 Lots of fun!
Randomized algorithms:
1 Algorithm uses random bits in addition to input.
2 Analyze algorithms average performance over the given input
where the average is over the random bits that the algorithm
uses.
3 On each input behaviour of algorithm is random. Analyze
worst-case over all inputs of the (average) performance.
Chandra (UIUC) CS498ABD 10 Fall 2020 10 / 33
Types of Randomized Algorithms
Typically one encounters the following types:
1 Las Vegas randomized algorithms: for a given input x
output of algorithm is always correct but the running time is a
random variable. In this case we are interested in analyzing the
expected running time.
Randomized QuickSort
1 Pick a pivot element uniformly at random from the array.
2 Split array into 3 subarrays: those smaller than pivot, those
larger than pivot, and the pivot itself.
3 Recursively sort the subarrays, and concatenate them.
1 array: 16, 12, 14, 20, 5, 3, 18, 19, 1 I 1,3, 5, 12,14, 16, 18,19
, 20
Too Big!!
Too Big!!
t.iq#I
of rank i in A, else zero.
n
X ⇣ ⌘
Q(A) = n + Xi · Q(Aileft ) + Q(Airight ) .
i =1
T
E
Chandra (UIUC) CS498ABD
random variables
17 Fall 2020 17 / 33
Analysis via Recurrence
1 Given array A of size n, let Q(A) be number of comparisons of
randomized QuickSort on A.
2 Note that Q(A) is a random variable.
3 Let Aileft and Airight be the left and right arrays obtained if rank i
element chosen as pivot.
Lemma
Random variables Xi is independent of random variables Q(Aileft ) as
well as Q(Airight ), i.e.
⇥ i
⇤ ⇥ i
⇤
h Xi · Q(Aleft )i = E[Xi ] EhQ(Aleft ) i
E
E Xi · Q(Airight ) = E[Xi ] E Q(Airight )
Proof.
This is because the algorithm, while recursing on Q(Aileft ) and
Q(Airight ) uses new random coin tosses that are independent of the
coin tosses used to decide the first pivot. Only the latter decides
value of Xi .
t
y
laid !
" n - i⇐
. ,
h
vi n
X 1
i t
)
¥0
E Q(A) n +
i =1
n
(T (i
=
1) + T (n i )) .
EE
n
X 1
T (n) n + (T (i 1) + T (n i ))
i =1
n
with base case T (1) = 0.
id
IE ht (Th Dt Tcn
'
TIME
- -
n t
,
n
X 1
T (n) n + (T (i 1) + T (n i ))
i =1
n = =
=
n
X 1
T (n) n + (T (i 1) + T (n i ))
i =1
n
with base case T (1) = 0.
Lemma
T (n) = O(n log n).
Proof.
(Guess and) Verify by induction.
7 5 9 1 3 4 8 6
=) 1 3 7 5 9 4 8 6
Decision if to compare 5 to 8 is moved to subproblem.
7 5 9 1 3 4 8 6
=) 1 3 7 5 9 4 8 6
Decision if to compare 5 to 8 is moved to subproblem.
2 If pivot too large (say 9 [rank 8]):
7 5 9 1 3 4 8 6 7 5 1 3 4 8 6 9
=)
Decision if to compare 5 to 8 moved to subproblem.
7 5 9 1 3 4 8 6 1 3 4 5 7 9 8 6
=)
7 5 9 1 3 4 8 6 1 3 4 5 7 9 8 6
=)
2 If pivot is 8 (rank 7). Bingo!
7 5 9 1 3 4 8 6 7 5 1 3 4 6 8 9
=)
7 5 9 1 3 4 8 6 1 3 4 5 7 9 8 6
=)
2 If pivot is 8 (rank 7). Bingo!
7 5 9 1 3 4 8 6
=) 7 5 1 3 4 6 8 9
3 If pivot in between the two numbers (say 6 [rank 5]):
7 5 9 1 3 4 8 6
=) 5 1 3 4 6 7 8 9
5 and 8 will never be compared to each other.
Conclusion:
Ri ,j happens if and only if:
i th or j th ranked element is the first pivot out of
i th to j th ranked elements.
Question: What is the probability that John wore a red tie on the
first day he wore a tie?
Proof.
Let a1 , . . . , ai , . . . , a←
j , . . . , an be elements of A in sorted order.
Let S = {ai , ai +1 , . . . , aj }
Observation: If pivot is chosen outside S then all of S either in left
array or right array.
Observation: ai and aj separated when a pivot is chosen from S for
the first time. Once separated no comparison.
Observation: ai is compared with aj if and only if either ai or aj is
chosen as a pivot from S at separation...
Chandra (UIUC) CS498ABD 28 Fall 2020 28 / 33
A Slick Analysis of QuickSort
Continued...
Lemma
h i
0
2
Pr Rij = j i +1
.
Proof.
Let a1 , . . . , ai , . . . , aj , . . . , an be sort of A. Let
S = {ai , ai +1 , . . . , aj }
Observation: ai is compared with aj if and only if either ai or aj is
chosen as a pivot from S at separation.
Observation: Given that pivot is chosen from S the probability that
it is ai or aj is exactly 2/|S| = 2/(j i + 1) since the pivot is
chosen uniformly at random from the array.
I
(B) Hn = ⇥(log log n). Aqa , Ae ,
,
- - r
p N l
(C) Hn = ⇥( log n).
-
E Rhi
(D) Hn = ⇥(log n).
(E) Hn = ⇥(log2 n).
:÷
in
n
I
E ZE .
e-= ' e
2 An =L
2h Hn E 2n inn
=
h i X X
E Q(A) = E[Xij ] = Pr[Rij ] .
1i <j n 1i <j n
Lemma
2
Pr[Rij ] = j i +1
.
Lemma
2
Pr[Rij ] = j i +1
.
h i X h i X 2
E Q(A) = Pr Rij =
1i <j n 1i <j n
j i +1
Lemma
2
Pr[Rij ] = j i +1
.
h i X 2
E Q(A) =
1i <j n
j i +1
Lemma
2
Pr[Rij ] = j i +1
.
h i X 2
E Q(A) =
1i <j n
j i +1
n
X1 n
X 2
=
i =1 j =i +1
j i +1
Lemma
2
Pr[Rij ] = j i +1
.
h i n n
X1 X 2
E Q(A) =
i =1 j =i +1
j i +1
Lemma
2
Pr[Rij ] = j i +1
.
h i n n
X1 X 1
E Q(A) = 2
i =1 i <j
j i +1
Lemma
2
Pr[Rij ] = j i +1
.
h i n n
X1 X 1
E Q(A) = 2
i =1 i <j
j i +1
Lemma
2
Pr[Rij ] = j i +1
.
h i n n
X1 X n
X1 nX
i +1
1 1
E Q(A) = 2 2
i =1 i <j
j i +1 i =1 =2
Lemma
2
Pr[Rij ] = j i +1
.
h i n n
X1 X n
X1 nX
i +1
1 1
E Q(A) = 2 2
i =1 i <j
j i +1 i =1 =2
n
X1 X
2 (Hn i +1 1) 2 Hn
i =1 1i <n
Lemma
2
Pr[Rij ] = j i +1
.
h i n n
X1 X n
X1 nX
i +1
1 1
E Q(A) = 2 2
i =1 i <j
j i +1 i =1 =2
n
X1 X
2 (Hn i +1 1) 2 Hn
i =1 1i <n