Randomized Algorithms (1)
Randomized Algorithms (1)
Introduction
Types
- Las Vegas (Ex. Randomized Quick Sort)
- Monte Carlo (Ex. Karger's Min Cut)
Introduction
• A randomized algorithm is one that receives, in
addition to its input data, a stream of random
bits that it can use for the purpose of making
random choices.
3. QUICKSORT(A, p, q – 1) 4. for j = p to r – 1
5. if A[j] ≤ x
4. QUICKSORT(A, q + 1, r)
6. i=i+1
• To sort an array A with n
elements, the first call to 7. Exchange A[i] with A[j]
QUICKSORT is made with 8. Exchange A[i + 1] with A[r]
p = 0 and r = n – 1. 9. return i + 1
Example: 2, 8, 7, 1, 3, 5, 6, 4
p r p r
2 8 7 1 3 5 6 4 2 1 3 8 7 5 6 4
i j i j
p r p r
2 8 7 1 3 5 6 4 2 1 3 8 7 5 6 4
i j i j
p r p r
2 8 7 1 3 5 6 4 2 1 3 8 7 5 6 4
i j i j
p r p r
2 8 7 1 3 5 6 4 2 1 3 4 7 5 6 8
i j i j
p r
2 1 7 8 3 5 6 4
i j
Randomized Quick Sort (Las Vegas)
• RANDOMIZED-QUICKSORT(A, p, r)
1. if p < r
2. q = RANDOMIZED-PARTITION(A, p, r)
3. RANDOMIZED-QUICKSORT(A, p, q – 1)
4. RANDOMIZED-QUICKSORT(A, q + 1, r)
• RANDOMIZED-PARTITION(A, p, r)
5. i = RANDOM(p, r)
6. Exchange A[r] with A[i]
7. return PARTITION(A, p, r)
Example
0 1 2 3 4 5 6 7
5 3 8 9 4 7 6 1
• Generate a random number in between 0 and 7.
– Let the number be 5.
– Exchange A[5] with A[7]
0 1 2 3 4 5 6 7
5 3 8 9 4 1 6 7
Contd…
5 3 8 9 4 1 6 7 5 3 4 9 8 1 6 7
i pj r p i j r
5 3 8 9 4 1 6 7 5 3 4 1 8 9 6 7
pi j r p i j r
5 3 8 9 4 1 6 7 5 3 4 1 6 9 8 7
p i j r p i jr
5 3 8 9 4 1 6 7 5 3 4 1 6 7 8 9
p i j r p i jr
5 3 8 9 4 1 6 7
p i j r
5 3 4 1 6 7 8 9
Contd… 5 6 4 1 3 7 8 9
1 3 4 5 6 7 8 9
1 3 4 5 6 7 8 9
1 3 6 5 4 7 8 9
1 3 4 5 6 7 8 9
1 3 4 5 6 7 8 9
1 3 4 5 6 7 8 9
Exchanged 1 3 4 5 6 7 8 9
1 3 4 5 6 7 9 8
Final 1 3 4 5 6 7 8 9
1 3 4 5 6 7 8 9
Karger’s Min-cut (Monte Carlo)
• Minimum cut of an undirected graph G =
(V,E) is a partition of the nodes into two
groups V1 and V2, so that the number of edges
between V1 and V2 is minimized.
– V1 ∩ V2 = Ø and V1 𝖴 V2 = V
Example:
Size of minimum cut is two
with node partitions as
V1 = {a, b, e, f} and
V2 = {c, d, g, h}.
Contd…
1. Repeat until just two nodes remain:
2. Pick an edge of G at random and collapse
its two endpoints into a single node.
A C C
D B
ACD
C AC Select D-AC with
probability 2/4
i=2 B
D B
Ci Result is minimum Ci value
i Probability
(# of edges) (or Ci with higher
1 3 1/4 = 0.25 probability). Min cut is 2 with
2 2 2/4 = 0.5 vertex sets {A, C, D} and {B}.
Example – 2
a b c d
h g f e
i=1
1 a b c d 14 edges to choose from. Pick
b – c with probability 1/14.
h g f e
h g f e
h gf e
i=1
4 a bcgf d 10 edges to choose from. Pick
a – h with probability 1/10.
h e
h g f e
h g f
g f
i=2
4 ah bg c de 11 edges to choose from. Pick
c – f with probability 1/11.
h g f e