Randomized Algorithms - Deterministic and Randomized Quicksort
Randomized Quicksort Randomized Global Min Cut
description
Transcript of Randomized Quicksort Randomized Global Min Cut
![Page 1: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/1.jpg)
CSL758Instructors: Naveen Garg
Kavitha Telikepalli
Scribe: Manish SinghVaibhav Rastogi
February 7 & 11, 2008.
![Page 2: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/2.jpg)
The Quick Sort ProblemTo sort a given set of numbers
In traditional quick sort algorithm, we pick a particular index element as pivot for splitting. Worst Case: O(n2
) Average Case: O(n log n)
A good pivot can be selected using median finding algorithm but the total complexity will again be O(n2
).So, what if we pick a random element uniformly
as pivot and do the partition. We will show that this takes expected O(n log n) time.
![Page 3: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/3.jpg)
Randomized Quick Sort algorithmAssuming all elements are distinctWe pick a random element x as the pivot and
partition the input set S into two sets L and R such:L = numbers less than xR = numbers greater than x
Recursively sort L and R.Return LxR
![Page 4: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/4.jpg)
Analysis of Randomized Quick SortThe running time of this algorithm is variable.Running time = # of comparisons in this algorithm.Expected Running Time, Let S be the sorted sequence of the n input
numbers.S =
Let Xij = 1 if Si and Sj are compared in the algo = 0 otherwiseRunning time = # of comparisons =
S1 S2 Si SnSn-1Sj
n
i ijijX
1
[ ] *Pr[ ]iiE X x X x
![Page 5: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/5.jpg)
Analysis cont…The expected running time = = Now, = 1 * Pr[Si and Sj are compared in
our alg.] + 0 * Pr[Si and Sj are not compared]Suppose we have a set of numbers:
2, 7, 15, 18, 19, 23, 35In this 18 and 19 will always be compared.2 and 35 will be compared only if compared at
root.
][1
n
i ijijXE
n
i ijijXE
1
][][ ijXE
![Page 6: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/6.jpg)
Analysis cont…Pr[Si and Sj are compared in our algo] =
Pr[the first element chosen as pivot in set {si,si+1,….,sj} is either si or sj].
To elements get compared only if they have ancestor relationship in the tree.
Pr[Picking Si or Sj] =
S1 S2 Si SnSn-1Sj
3rd pivot
1st pivot
2nd pivot
4th pivot 5th pivot
j i j i j i
![Page 7: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/7.jpg)
Analysis cont…Thus the expected runtime:
This algorithm will always give the right answer though the running time may be different. This is an example of Las Vegas algorithms.
[ ] ( log )n n n n
iji j i i j i i j
E X O n nj i j
Since, ... lnn
n
![Page 8: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/8.jpg)
The global min cut problemInput: A connected
undirected multigraph G(V, E).
Output: A minimum cardinality subset of edges whose removal makes G disconnected.
a b
c d
x
![Page 9: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/9.jpg)
First IdeaRecall the max flow problem
An s-t min cut is the min cut which makes s and t disconnected.
Run a max flow algorithm on G for all (s, t) pairs and return the smallest of the output cuts.
Time:Since G is undirected we could fix s and
iterate t over all other vertices.Time:
(time for max flow) ( ) ( )n n
O mn O mn
( )(time for max flow) ( )n O mn
![Page 10: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/10.jpg)
Reduce G to G1 with one vertex less such that1. 2.
Contract a random edge. Each edge is contracted with probability
Contract a random edge
Another idea
mincut of mincut of G G
with high probability, mincut of mincut of .G G
G G1
.n
d c
a b
d c
(a,b)
![Page 11: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/11.jpg)
Observation 1Any cut of G1 is also a cut of G, with a and b
on the same side.If mincut of G1 < mincut of G
This mincut of G1 should be the mincut of G, by the above observation.
Thus, mincut of G1 ≥ mincut of G.
![Page 12: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/12.jpg)
Observation 2Fix our favorite
global mincut C, with
Let e be the edge contracted.
If e is not in C thenC is also a cut in G1.Using Observation
1, mincut of G1 = mincut of G.
.C k
e
A V A
( , )C A V A
![Page 13: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/13.jpg)
Observation 2 cont…
which is quite a high probability.
deg( ) , since is the global mincut value
deg
Pr[One of the edges is chosen as ]
mincut of ]
Pr[ is not one of the
( )
Thus, Pr[mi
ncut of
ed es ,g ]
u V
kE
u k k
E u nk
kE
k e
k kE
e
nG
n
G
![Page 14: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/14.jpg)
The algorithmRepeat
Choose an edge of G uniformly at random and contract it. Let the resulting graph be called G.
Until there are only two vertices in G.Output the set of edges between the two
vertices as our candidate mincut.
![Page 15: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/15.jpg)
Pr[ is ] Pr[ ]Pr[ ]Pr[ | ]Pr |Pr
]| ][
[n
n n
F C EE
E
E E EE E E E E
EE
Bounding the probability of correctnessLet C be our favorite mincut and F be the cut
output by the algorithm.Pr[F is C]=Pr[no edge of C is contracted in
any iteration].Let Ei be the event that no edge of C got
contracted in the ith iteration.
![Page 16: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/16.jpg)
Bounding the probability of correctnessWe know that,Because C remains the mincut we can also
bound the conditional following conditional probabilities as we bounded Thus,
Pr[ ] .nn n
E
Pr( ).E
|
Pr[
[
|
Pr ]
]i i
nEn n
n iE E En i n
E
i
![Page 17: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/17.jpg)
Bounding the probability of correctness is the product of these conditional
probabilities. So we have,
This probability bound is too low. It can be improved by repeating the algorithm N times and returning the least cut.
Pr[ is ]F C
· ·
( )Pr[ is ] n n n i
n n n i n nC
nF
(Failure Probability ) for .N nNn e
![Page 18: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/18.jpg)
Running timeChoosing a random edge.
Choose a vertex u with probability Now choose one of u’s with probability
Time: O(n).
deg( ).um
.deg( )u
Pr[edge ( , ) is select deg( ) deg( ) .deg( ) deg( )
ed] u vu m
um
vm v
![Page 19: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/19.jpg)
Running timeContracting edges: Use adjacency matrix.
Add the rows and columns of u and v.O(n) time.
u
u
v
v
u+v
u+v
![Page 20: Randomized Quicksort Randomized Global Min Cut](https://reader033.fdocuments.us/reader033/viewer/2022042514/56815b90550346895dc9954a/html5/thumbnails/20.jpg)
Running timeThe basic algorithm has iterations.Each iteration in the basic algorithm involves
selecting and contracting a random edge. O(n) time.
The basic algorithm is repeated times.
Therefore running time =
n
n
( ) ( ).n n O n