Introduction To Randomized Algorithm
Introduction To Randomized Algorithm
Randomized Algorithms
Nguyen An Khuong,
Chapter 2
Introduction to Randomized Contents
Introduction
Algorithms Miller-Rabin
PRIMARILY TEST
QUICKSORT
Advanced Algorithms (055127) on Ngày 6 tháng 9 năm 2023 MIN-CUT
SELECTION Problem
Introduction
Deterministic algorithm
Divide-and-conquorer with
random pivoting
Random sampling strategy
for selection
Discrete RVs
References
Nguyen An Khuong,
Faculty of Computer Science and Engineering
University of Technology, VNU-HCM
2.1
Introduction to
Contents Randomized Algorithms
Nguyen An Khuong,
1 Introduction
Introduction
3 QUICKSORT Miller-Rabin
PRIMARILY TEST
QUICKSORT
SELECTION Problem
2.2
Introduction to
Deterministic algorithms vs randomized algorithms Randomized Algorithms
Nguyen An Khuong,
• Deterministic:
Introduction
Goal: To prove that the algorithm solves the problem correctly
Miller-Rabin
(always) and quickly (typically, the number of steps should be PRIMARILY TEST
MIN-CUT
In other words, for all inputs, output is good.
SELECTION Problem
• Randomized: Introduction
Deterministic algorithm
Divide-and-conquorer with
input Algorithm output random pivoting
Random sampling strategy
for selection
Discrete RVs
References
random bits
In addition to input, algorithm takes a source of random
numbers and makes random choices during execution.
Behavior can vary even on a fixed input.
For all inputs, with good probability, output is good.
2.3
Introduction to
Randomized Algorithms vs Probabilistic Analysis of Algorithms Randomized Algorithms
Nguyen An Khuong,
Contents
Introduction
random bits Miller-Rabin
PRIMARILY TEST
Goal: Design algorithm + analysis to show that this behavior QUICKSORT
is likely to be good on every input? MIN-CUT
Discrete RVs
2.4
Introduction to
Types of randomized algorithms: Monte Carlo vs Las Vegas Randomized Algorithms
Nguyen An Khuong,
answer, its running time is a random (small time with good Miller-Rabin
PRIMARILY TEST
probability) variable whose expectation is bounded (say by a QUICKSORT
polynomial). Examples: Quicksort, Hashing,... MIN-CUT
References
1 LV =⇒ MC. Fix a time T. If the algorithm terminates before
T , we output the answer, otherwise we output 1.
2 MC does not always imply LV. The implications holds when
verifying a solution can be done much faster than finding one.
In that case, we test the output of MC algorithm and stop
only when a correct solution is found.
2.5
Introduction to
Advantages of randomized algorithms Randomized Algorithms
Nguyen An Khuong,
References
on a subset)
• Symmetry breaking (e.g. what to do when two packets arrive
at the same time?)
• Probabilistic existence proofs (Erdos, Lovasz... if a random
object satisfies a property with non-zero probability, we just
proved that such an object exists.)
2.6
Introduction to
Scope of randomized algorithms Randomized Algorithms
Nguyen An Khuong,
Contents
Introduction
• Proposed in 70’s.
Miller-Rabin
• Miller and Rabin gave two versions of the same algorithm to PRIMARILY TEST
QUICKSORT
test whether a number n is prime or not. MIN-CUT
• Whereas Rabin’s algorithm works with a randomly chosen SELECTION Problem
References
2.8
Introduction to
Miller-Rabin Algorithm Randomized Algorithms
Nguyen An Khuong,
Contents
SELECTION Problem
2 Randomly choose a ∈ Zn . Introduction
3 Test if an−1 = 1( mod n). If no, output COMPOSITE. Deterministic algorithm
i Divide-and-conquorer with
4 Compute ui = as×2 ( mod n) for 0 ≤ i < t. random pivoting
Random sampling strategy
5 If there is an i such that ui = 1 and ui−1 ̸= ±1, output for selection
2.9
Introduction to
Correctness of Miller-Rabin Algorithm Randomized Algorithms
Nguyen An Khuong,
Contents
Introduction
• Observation: Pr [test is correct | n is prime ] = 1.
a∈Zn Miller-Rabin
PRIMARILY TEST
• THEOREM Pr [test outputs PRIME | n is composite ] QUICKSORT
a∈Zn
MIN-CUT
1 1 1
≤ 4 + 4 = 2 SELECTION Problem
References
2.10
Introduction to
Time Complexity of Miller-Rabin Algorithm Randomized Algorithms
Nguyen An Khuong,
Contents
and taking modulo n each time all with log n bits numbers QUICKSORT
Discrete RVs
• Hence the time complexity of the algorithm is O(log n). References
2.11
Introduction to
The algorithm Randomized Algorithms
Nguyen An Khuong,
Miller-Rabin
Algorithm (Hoare, 1962) PRIMARILY TEST
QUICKSORT
SELECTION Problem
2. Pick some element x = xi in S called the pivot. Introduction
Deterministic algorithm
3. Reorder S in such a way that for every number xj ̸= x in S, if Divide-and-conquorer with
random pivoting
xj < x, then xj is moved to a list S1 , else if xj > x then xj is Random sampling strategy
Discrete RVs
4. Apply this algorithm recursively to the list of elements in S1 References
and to the list of elements in S2 .
5. Return the sorted list S1 , x, S2 .
2.12
Introduction to
Quicksort Demonstration Randomized Algorithms
Contents
Introduction
Miller-Rabin
PRIMARILY TEST
QUICKSORT
MIN-CUT
SELECTION Problem
Introduction
Deterministic algorithm
Divide-and-conquorer with
random pivoting
Random sampling strategy
for selection
Discrete RVs
References
2.13
Introduction to
Quicksort’s Average Running Time Analysis Randomized Algorithms
quicksort. Miller-Rabin
PRIMARILY TEST
• Recall that the input is a sequence S = (x1 , ..., xn ) of distinct QUICKSORT
elements, and that (y1 , ..., yn ) has the same elements sorted in MIN-CUT
indicator variables Xi,j , with Xi,j = 1 iff yi and yj are ever Divide-and-conquorer with
random pivoting
j=2 i=1
j−1
n X
X
E(X) = E(Xi,j ).
j=2 i=1
• Furthermore, since Xi,j is an indicator variable, we have
E(Xi,j ) = P (yi and yj are ever compared). 2.14
Introduction to
Quicksort’s Average Running Time Analysis (cont’d) Randomized Algorithms
Nguyen An Khuong,
QUICKSORT
chosen in the sublist Yi,j = {yi , yi+1 , ..., yj } is yi (or that the
MIN-CUT
next pivot chosen is yj , but the two probabilities are equal).
SELECTION Problem
• Since the pivot is one of the values in {yi , yi+1 , ..., yj } and Introduction
Deterministic algorithm
since each of these is equally likely to be chosen (by Divide-and-conquorer with
random pivoting
hypothesis), we have Random sampling strategy
for selection
1
P (yi is chosen as the next pivot in Yi,j ) = j−i+1 . Discrete RVs
2.15
Introduction to
Quicksort’s Average Running Time Analysis (cont’d) Randomized Algorithms
j−1
n X j−1
n X Nguyen An Khuong,
X X 2
• It follows that E(X) = E(Xi,j ) =
j=2 i=1 j=2 i=1
j−i+1
j
n X
X 1 Contents
=2 , (set k = j − i + 1)
j=2 k=2
k Introduction
Miller-Rabin
n X n PRIMARILY TEST
X 1
=2 QUICKSORT
k MIN-CUT
k=2 j=k
n n n SELECTION Problem
X n−k+1 X 1 X Introduction
=2 = 2(n + 1) +2 (−1) Deterministic algorithm
k k Divide-and-conquorer with
k=2 k=2 k=2 random pivoting
n
X 1 Random sampling strategy
for selection
= 2(n + 1) − 4n.
k Discrete RVs
k=1
References
• The quantity Hn = 1 + 1/2 + 1/3 + ... + 1/n is called the
“nth harmonic number”, and is in the rangeR [ln n, 1 + ln n]
n
(this can be seen by considering the integral 1 x1 dx).
• Therefore,
E(X) ∈ [2(n + 1) ln n − 4n, (2(n + 1) ln n − 4n) + (2 − 2n)].
• In other words, E(X) ∈ Θ(n ln n).
2.16
Introduction to
Approximation of the nth harmonic number Hn Randomized Algorithms
Nguyen An Khuong,
1 1 1
• The exact expression for Hn := 1 + + + · · · + is not
2 3 n
known, but we can estimate Hn as below.
• Let us consider the area under the curve x1 when x varies from Contents
1 to n. Introduction
1 1 1 1 Miller-Rabin
• We note that Hn − = 1 + + + · · · + is an PRIMARILY TEST
n 2 3 n−1 QUICKSORT
overestimation of this area by rectangles. See below.
MIN-CUT
SELECTION Problem
Introduction
Deterministic algorithm
Divide-and-conquorer with
random pivoting
Random sampling strategy
for selection
Discrete RVs
References
Rn1
• So, 1 x
dx < Hn − n1 .
2.17
Introduction to
Approximation of the nth harmonic number Hn (cont’d) Randomized Algorithms
1 1 1
• Moreover Hn − 1 = + + · · · + is an underestimation Nguyen An Khuong,
2 3 n
of the area as below.
Contents
Introduction
Miller-Rabin
PRIMARILY TEST
QUICKSORT
MIN-CUT
SELECTION Problem
Introduction
Deterministic algorithm
Divide-and-conquorer with
random pivoting
Random sampling strategy
for selection
Nguyen An Khuong,
which minimizes the number of edges going from this subset S Miller-Rabin
PRIMARILY TEST
to the rest of the vertices. QUICKSORT
• We denote the cut value for set S by |E(S, S̄)| (where MIN-CUT
• The problem of finding a minimum cut with a given source and Deterministic algorithm
Divide-and-conquorer with
sink (s − t min cut) is probably more familiar. random pivoting
Random sampling strategy
Discrete RVs
solution. References
• We can compute a s − t minimum cut in O(n3 ).
• By running this algorithm for all (s, t) pairs, we get a solution
for the global minimum cut in O(n5 ).
2.19
Introduction to
The Algorithm Randomized Algorithms
Nguyen An Khuong,
• Contracting an edge means that we remove that edge and Discrete RVs
• Let’s say that the algorithm “fails" in step i if in that step, we Nguyen An Khuong,
Introduction
1 X 2·m
|E(S, S̄)| ≤ min d(u) ≤ d(u) = . Miller-Rabin
u n n PRIMARILY TEST
u∈V
QUICKSORT
• So, |E(S,
m
S̄)|
≤ n2 . MIN-CUT
• We fail in the first step if we pick an edge in the minimum cut, SELECTION Problem
Introduction
n References
2
P(fail in 2nd step | success in 1st step) ≤
n−1
···
2
P(fail in ith step | success till (i − 1)th step) ≤
n−i+1
2.21
Introduction to
Analyzing the probability of the correct answer (cont’d) Randomized Algorithms
• Let Zi be the event that the algorithm succeeds in step i. Nguyen An Khuong,
Thus, we have
n−2
P(Z1 ) ≥
n Contents
n−3 Introduction
P(Z2 |Z1 ) ≥
n−1 Miller-Rabin
PRIMARILY TEST
··· QUICKSORT
n−i−1 MIN-CUT
Discrete RVs
= P(Z1 ) · P(Z2 |Z1 ) · P(Zn−2 |Z1 ∩ Z2 ∩ · · · ∩ Zn−3 )
References
n−2 n−3 n−4 n−5 2 1
≥ · · · ··· ·
n n−1 n−2 n−3 4 3
1 1 2 1
= · · ·
n n−1 1 1
2
≥ 2.
n
2.22
Introduction to
Analyzing the probability of the correct answer (cont’d) Randomized Algorithms
Nguyen An Khuong,
Miller-Rabin
k PRIMARILY TEST
2
P(Success at least once) ≥ 1 − 1 − 2 QUICKSORT
n MIN-CUT
SELECTION Problem
1
• A classical inequality yields (1 − z) z ≤ 1e . Introduction
Deterministic algorithm
Discrete RVs
• We get:
References
log δ1
1
P(Success) ≥ 1 − =1−δ
e
• After running all these k trials, we choose the one with
minimum cut value.
2.23
Introduction to
Complexity Randomized Algorithms
Nguyen An Khuong,
Contents
Introduction
Discrete RVs
References
2.24
Introduction to
Min-Cut: Can we do better? Randomized Algorithms
Nguyen An Khuong,
2/n. Introduction
• Moreover, earlier stages take more time compared to later ones. QUICKSORT
MIN-CUT
• Idea: Let us redistribute our iterations. Since earlier ones are
SELECTION Problem
more accurate and slower, why not do less of them at the Introduction
Deterministic algorithm
beginning, and increasingly more as the number of edges Divide-and-conquorer with
2.25
Introduction to
A better MIN-CUT Randomized Algorithms
Nguyen An Khuong,
Contents
1: function Algo2 Introduction
2: Repeat Twice: √ Miller-Rabin
PRIMARILY TEST
3: Run contraction from n → n/ √ 2 QUICKSORT
4: Recursively call Algo2 on n/ 2 set MIN-CUT
5: return the last two vertices for the best cut among the two SELECTION Problem
6: end function Introduction
Deterministic algorithm
Divide-and-conquorer with
random pivoting
Discrete RVs
n
T (n) = 2 O(n2 ) + T √ References
2
2
2 n
= 2cn + 2 · 2 · c · √ + ···
2
= O(n2 log n)
2.26
Introduction to
Success Probability Randomized Algorithms
Nguyen An Khuong,
2 2 Introduction
2 Miller-Rabin
1 n PRIMARILY TEST
=1− 1− p √
2 2 QUICKSORT
2 MIN-CUT
n 1 n
=p √ − p √ SELECTION Problem
2 4 2 Introduction
Deterministic algorithm
References
1 1 1 1
f (x−1)−f (x) = − = ≈ = f (x−1)2
x−1 x x(x − 1) (x − 1)2
• Hence, we have p(n) = O log1 n . Thus, to get 3/4
probability of success, we need O(n2 log2 n) time (which is
much better than before, so we’re happy). 2.27
Introduction to
Extension: The number of MIN-CUT Randomized Algorithms
Miller-Rabin
PRIMARILY TEST
QUICKSORT
MIN-CUT
SELECTION Problem
Theorem Introduction
Deterministic algorithm
An undirected graph G on n nodes has at most Cn2 global min-cuts. Divide-and-conquorer with
random pivoting
Random sampling strategy
for selection
Discrete RVs
Proof:
• Suppose there are r global min-cut c1 , ..., cr ; References
Nguyen An Khuong,
OUTPUT: the k-th smallest item in general case (or the median of Introduction
QUICKSORT
For example, given a set S = {18, 15, 27, 13, 1, 7, 25}, the MIN-CUT
objective is the median of S. Note: SELECTION Problem
• A feasible strategy is to sort S first, and then report the k-th Discrete RVs
References
one, which takes O(n log n) time.
• Divide-and-conquer becomes more powerful when combined
with randomization: Possible to develop a faster algorithm, say
the deterministic linear algorithm (16n comparisons) by Blum
et al.
2.29
Introduction to
A general divide-and-conquer paradigm Randomized Algorithms
3: S − = {}; Introduction
Miller-Rabin
4: for j = 1 to n do PRIMARILY TEST
6: S + = S + ∪ {sj }; MIN-CUT
S − = S − ∪ {sj };
Introduction
8: Deterministic algorithm
Nguyen An Khuong,
Contents
Introduction
Miller-Rabin
PRIMARILY TEST
QUICKSORT
MIN-CUT
SELECTION Problem
Introduction
Intuition: Deterministic algorithm
Divide-and-conquorer with
1 At first, an element ai is chosen to split S into two parts random pivoting
Random sampling strategy
S + = {aj : aj ≥ ai }, and S − = {aj : aj < ai }. for selection
Discrete RVs
2 We can determine whether the k-th median is in S + or S − . References
2.31
Introduction to
How to choose a splitter? Randomized Algorithms
QUICKSORT
T (n) ≤ T ((1 − ϵ)n) + O(n) MIN-CUT
Discrete RVs
e.g.: ϵ = 14 : References
2.32
Introduction to
BFPRT algorithm: a linear deterministic algorithm Randomized Algorithms
Nguyen An Khuong,
Contents
Introduction
• Still using the idea of choosing splitter. The ideal splitter is the
Miller-Rabin
median; however, finding the median is exactly our objective. PRIMARILY TEST
QUICKSORT
• Thus, just try to get “something close to the median”, say
MIN-CUT
within n4 from the median. SELECTION Problem
finding the median of the “whole set”, find a median of a Divide-and-conquorer with
random pivoting
References
2.33
Introduction to
Median of medians algorithm [Blum, 1973] Randomized Algorithms
Nguyen An Khuong,
“Median of medians” algorithm:
MIN-CUT
SELECTION Problem
Introduction
Deterministic algorithm
Divide-and-conquorer with
random pivoting
Random sampling strategy
for selection
Discrete RVs
References
Analysis:
T (n) = T ( n5 ) + T ( 7n 6n
10 ) + 5 at most 24n comparisons.
(here, 10 comes from the fact that at least 3n
7n
10 can be deleted by
using M as the splitter. )
2.34
Introduction to
Randomized divide-and-conquorer Randomized Algorithms
3: S − = {};; Introduction
Miller-Rabin
4: for j = 1 to n do PRIMARILY TEST
6: S + = S + ∪ {sj }; MIN-CUT
S − = S − ∪ {sj };
Introduction
8: Deterministic algorithm
Nguyen An Khuong,
e.g.: ϵ = 14 :
Contents
Introduction
Miller-Rabin
PRIMARILY TEST
QUICKSORT
MIN-CUT
SELECTION Problem
Introduction
Deterministic algorithm
Divide-and-conquorer with
random pivoting
Random sampling strategy
for selection
Discrete RVs
2.36
Introduction to
Randomized divide-and-conquorer cont’d Randomized Algorithms
Nguyen An Khuong,
Theorem
The expected running time of Select(n,k) is O(n). Contents
Introduction
Proof. Miller-Rabin
PRIMARILY TEST
• Let ϵ = 1
4
.
We’ll say that the algorithm is in phase j when the size QUICKSORT
SELECTION Problem
• Let X be the number of steps. And Xj be the number of steps in Introduction
Discrete RVs
number of iterations to find a centered splitter is: 2.
References
• Each iteration costs cn( 43 )j
steps since there are at most n( 43 )j
elements in phase j. Thus, E(Xj ) ≤ 2cn( 34 )j .
• E(X) = E(X0 + X1 + ....) ≤ j 2cn( 43 )j ≤ 8cn.
P
2.37
Introduction to
A “random sampling” algorithm [Floyd & Rivest, 1975] Randomized Algorithms
Nguyen An Khuong,
Introduction
Miller-Rabin
1: randomly sample r elements (with replacement) from S = PRIMARILY TEST
References
H = {si : si > b};
2.38
Introduction to
Example Randomized Algorithms
Nguyen An Khuong,
Contents
Introduction
Miller-Rabin
PRIMARILY TEST
QUICKSORT
MIN-CUT
SELECTION Problem
Introduction
Deterministic algorithm
Divide-and-conquorer with
random pivoting
Random sampling strategy
for selection
Discrete RVs
Two requirements of M : References
2.39
Introduction to
Time-comlexity analysis Randomized Algorithms
Nguyen An Khuong,
3 MIN-CUT
− 41
Setting r = n , and δ = n . The time bound of Step 4 changes
4
SELECTION Problem
to: Introduction
Deterministic algorithm
• Step 4: O(δn log(δn)) = o(n). Divide-and-conquorer with
random pivoting
• The best known deterministic algorithm: 3n. But too Discrete RVs
References
complicated.
• A lower bound: 2n.
2.40
Introduction to
Error probability analysis I Randomized Algorithms
Nguyen An Khuong,
Theorem
1
With probability 1 − O(n− 4 ), the RandomSamplingSelect
Contents
algorithm reports the median in the first pass. Thus, the running
Introduction
time is only 2n + o(n). Miller-Rabin
PRIMARILY TEST
QUICKSORT
MIN-CUT
SELECTION Problem
Introduction
Deterministic algorithm
Divide-and-conquorer with
random pivoting
Random sampling strategy
for selection
References
Case 1:
Define index variable xi = 1 when the i-th sample is less than the
median, and xi = 0 otherwise. Let X = x1 + ... + xr be the
number of samples that is less than the median. We have:
E(xi ) = 12 and σ 2 (xi ) = 14 .
2.41
Introduction to
Error probability analysis II Randomized Algorithms
Nguyen An Khuong,
Contents
E(X) = 12 r and σ 2 (X) = 41 r. Introduction
Miller-Rabin
n 1−δ PRIMARILY TEST
Pr(|L| ≥ 2) = Pr(X ≤ 2 r) (2) QUICKSORT
SELECTION Problem
σ 2 (X)
≤ (4) Introduction
Discrete RVs
2.42
Introduction to
Random Variables Randomized Algorithms
Miller-Rabin
individual random outcomes, PRIMARILY TEST
MIN-CUT
• In this case it is a discrete-valued random variable that can
SELECTION Problem
take values 0, 1, ..., n, where n is the number of replications. Introduction
Deterministic algorithm
• A random variable X has a probability distribution that can Divide-and-conquorer with
random pivoting
be described using point probabilities fX (x) = p(X = x), Random sampling strategy
for selection
• Standard deviation
p (độ lệch chuẩn):
σ = SD(X) = V (X).
2.43
Introduction to
Expected Value: An Example Randomized Algorithms
Nguyen An Khuong,
Introduction
Miller-Rabin
Outcome Payroll x Probability p(X = x) PRIMARILY TEST
1 QUICKSORT
Death 10,000 1000 MIN-CUT
2
Disability 5000 1000
SELECTION Problem
Introduction
customer:
1 2 997
E(X) = $10, 000( ) + $5000( ) + $0( ) = $20.
1000 1000 1000
2.44
Introduction to
Variance: An Example Randomized Algorithms
Nguyen An Khuong,
• Of course, the expected value $20 will not always happen in Contents
Introduction
reality. Miller-Rabin
• There will be variability. Let’s calculate! PRIMARILY TEST
QUICKSORT
• So MIN-CUT
1 2 997
V (X) = 99802 ( 1000 )+49802 ( 1000 )+(−20)2 ( 1000 ) = 149, 600. SELECTION Problem
√ Introduction
• and SD(X) = 149, 600 ≈ $386.78 Deterministic algorithm
Divide-and-conquorer with
random pivoting
Random sampling strategy
for selection
Comment
Discrete RVs
The company expects to pay out $20, and make $30. However, the References
2.45
Introduction to
Randomized Algorithms
Nguyen An Khuong,
Contents
Cormen, T.H., Leiserson, C.E., Rivest, R.L., and Stein, C. Introduction
Introduction to Algorithms, 3rd ed. The MIT Press, 2009. Miller-Rabin
PRIMARILY TEST
Dasgupta, S., Papadimitriou, C.H. , and Vazirani, U.V. QUICKSORT
Algorithms. McGraw Hill, 2006. MIN-CUT
SELECTION Problem
Motwani, R and Prabhakar Raghavan, R. Randomized Introduction
References
2.46