Lec10 Randomization
Lec10 Randomization
RANDOMIZATION ALGORITHM
1
Learning Outcomes
2
Randomized Algorithm
Make use of randomness in their computation (in other words,
it flips coins during execution to determine what to do next)
E.g.: quicksort that use a random pivot
Work not only on the input but also on random choices made
by the algorithm
Input
Output
Random
bits/number
During execution, takes random choices depending on those
random numbers
Output can vary when the algorithm run multiple times on
the same input
Random seed of a training data before feed into a3
Randomized Algorithm
Example (recall what you have learned in probability)
Toss a coin
The output are: {HH, HT, TH, TT}, where each with
probability ¼
Random variable: assign a number to each elementary
event. For e.g., define X as the number of heads: X(HH)
= 2, X(HT) = X(TH) = 1, X(TT) = 0
We can count the expected value of random variable:
6
Randomized Algorithm
Randomized algorithm A
¼ ½
7
Where does the randomness come from?
9
Difficulties
10
Domains involve Randomized Algorithm
11
Randomized Algorithm
12
Randomized Algorithm
Expected bounds represent the average case across all
random numbers in the algorithm
Easier to get
E.g.: 100 workers, each uses average 2 hours to
complete an assignment, the expected cost is
100*2=200 hours
High probability bound
Harder to prove
E.g.: 100 workers with each uses average 2 hours to
complete an assignment, we cannot say that the
maximum time among all workers is 2 hours. It is
possible that 1 worker uses 100 hours, but the other use
1 hour to complete the assignment which give the
average as 2 hours.
13
Classes of Randomized Algorithms
Las Vegas
Always correct; expected running time “probably
fast”
Fails with some probability but we can tell when it
fails
Can run again till it succeeds
E.g.: randomized Quicksort
Las Vegas is preferable
14
Classes of Randomized Algorithms
15
Randomized Quick Sort
16
Randomized Quick Sort
Worst case occurs when:
Input sorted or reverse sorted
Partition around min or max element
One side of partition always has no element
Worst case recursion tree with one site empty, we get arithmetic
progression:
n(n-1)/2
17
Randomized Quick Sort
19
Randomized Quick Sort
We focus on the comparison.
Randomized-Partition (A, p, r)
Call partition function, select
i = Random (p, r) pivot, proceed with other
exchange A[r] with A[i] partitions
Return Partition (A, p, r)
Randomized-Quicksort (A, p, r)
If p< r
q = Randomized-Partition (A, p, r)
Randomized-Quicksort (A, p, q – 1)
Randomized-Quicksort (A, q+1, r)
Partition (A, p, r)
x = A[r]
i=p–1
for j = p to r-1 x is the pivot element. This
if A[j] < = x for loop, will make
i=i+1 comparison with each A[j]
exchange A[i] with A[j] element
exchange A[i+1] with A[r]
return i+1
20
Randomized Quick Sort Analysis 1
21
Randomized Quick Sort Analysis 1
Let T(n) = random variable of the running time of randomized quick
sort on input size n
Assume random numbers are independent
All splits are equally likely to occur with probability 1/n
Assume the elements are distinct
Thus with probability 1/n
we have seen this before in divide and conquer.
It is n log n.
Recurrence:
By induction, it is true for k<n, need to show
22
Randomized Quick Sort Analysis 2
Present it as a tree:
24
Randomized Quick Sort Analysis 2
25
Randomized Quick Sort Analysis 2
To compute pij
si is compared with sj iff si or sj is chosen as pivot element
before any other sl, i < l < j
Since each element is chosen as the pivot with same
probability, pij = , where j-i+1 represent the total number of
elements to be selected
If si is selected, then sj is not selected. Other non pivot
elements will be inserted into the tree
When the other element is selected (not the root of
subtree), it is randomly chosen from at least |j - i| + 1
elements.
pij = p(si is chosen as pivot) + p(sj is chosen as pivot) = +
=
26
Randomized Quick Sort Analysis 2
A cut = partition of the vertex set V into two disjoint nonempty sets V,
where V = V1 V2 and V1 V2 = Ø
E.g.: V1 = {A,C}; V2 ={B,D,E,F}
Need to remove 5 crossing edges
Size of the cut = number of edges crossing
28
We aim to minimize the size of cut
Karger’s Min Cut Algorithm
29
Example
Assume the random generator selects
a edge ‘a’ (probability 1/5)
0 1
c 0,1
b d d
c
b
2 3
e
2 3
e
Assume the random
generator selects edge
‘d’
(probability ¼ + ¼ = ½
as there are
c two edges
connected {0,1} and
b b
0,1, {3}) 0,1,
2 2
e 3 e 3
g
e
c
d
h
a b,e f
If the selected random edge is {b,e}, then
there is no way to split {a,b,c,d} and
{e,f,g,h}. Same thing happen if edge g
{d,e} is selected
c
d
h
31
Exercise
c
2 4 f
a
d
g 6
1
b
e
3 5 h
34
Analyse Karger’s Min Cut Algorithm
35
Analyse Karger’s Min Cut Algorithm
36
Karger’s Min Cut Algorithm