Randomized Quicksort Randomized Global Min Cut

Download Report

Transcript Randomized Quicksort Randomized Global Min Cut

CSL758
Instructors: Naveen Garg
Kavitha Telikepalli
Scribe: Manish Singh
Vaibhav Rastogi
February 7 & 11, 2008.
The Quick Sort Problem
 To 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.
Randomized Quick Sort algorithm
 Assuming all elements are distinct
 We 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 x
 R = numbers greater than x
 Recursively sort L and R.
 Return LxR
Analysis of Randomized Quick Sort
 The running time of this algorithm is variable.
 Running time = # of comparisons in this algorithm.
 Expected Running Time, E[ X ]   xi *Pr[ X  xi ]
 Let S be the sorted sequence of the n input numbers.
S
S
S S
S= S S
 Let Xij = 1 if Si and Sj are compared in the algo
1
2
i
j
n-1
= 0 otherwise
n
 Running time = # of comparisons =  X ij
i 1 j i
n
Analysis cont…
n
n
 The expected running time = E[ X ij ] =  E[ X ij ]
i 1 j i
i 1 j i
 Now, E[ X ij ] = 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, 35
 In this 18 and 19 will always be compared.
 2 and 35 will be compared only if compared at root.
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].
S1 S2
Si
Sj
Sn-1 Sn
 To elements get compared only if they have ancestor
relationship in the tree.
 Pr[Picking Si or Sj] = 
j  i 




j  i  j  i 
1st pivot
2nd pivot
4th pivot
3rd pivot
5th pivot
Analysis cont…
 Thus the expected runtime:
n n


E[ Xij ]  
   O(n log n)
i  j i
i  j i j  i 
i  j  j
n
n
 

Since,    ...   ln n
 
n
 This algorithm will always give the right answer though the
running time may be different. This is an example of Las
Vegas algorithms.
The global min cut problem
 Input: A connected
undirected multigraph
G(V, E).
 Output: A minimum
cardinality subset of
edges whose removal
makes G disconnected.
a
b
c
d
x
First Idea
 Recall 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.
n
n
 Time:   (time for max flow)    O(mn)  O(mn )
 
 
 Since G is undirected we could fix s and iterate t over
all other vertices.
 Time: (n )(time for max flow)  O(mn )
Another idea
 Reduce G to G1 with one vertex less such that
1.mincut of G  mincut of G
2.with high probability, mincut of G  mincut of G.
 Contract a random edge. Each edge is contracted with
probability
c
d
a

.
n
G Contract a G1
random edge
d
b
c
(a,b)
Observation 1
 Any 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.
Observation 2
 Fix our favorite global
mincut C, with C  k.
 Let e be the edge
contracted.
 If e is not in C then
 C is also a cut in G1.
 Using Observation 1,
mincut of G1 = mincut
of G.
A
VA
e
C  ( A,V  A)
Observation 2 cont…
Pr[One of the k edges is chosen as e] 
k
E
deg(u)  k , since k is the global mincut value
 E   deg(u)  nk
uV
k 

E n
Thus, Pr[mincut of G  mincut of G]
k

 Pr[e is not one of the k edges]  
  ,
E
n
which is quite a high probability.
The algorithm
Repeat
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.
Bounding the probability of
correctness
 Let 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.
 Pr[ F is C ]  Pr[ E  E  E   En ]
 Pr[ E ]Pr[ E | E ]Pr[ E | E  E ]
Pr[ En | E   En ]
Bounding the probability of
correctness
 We know that, Pr[ E ]  
 n

.
n
n
 Because C remains the mincut we can also bound the
conditional following conditional probabilities as we
bounded Pr(E). Thus,

n
Pr[ E  | E ]  

n  n 
Pr[ Ei | E 

n  i
 Ei  ]  

n  i n  i
Bounding the probability of
correctness
 Pr[ F is C ] is the product of these conditional
probabilities. So we have,
n   n   n  i 
Pr[ F is C ] 
·
n n  n  i 



· 
 
  n(n ) n
 This probability bound is too low. It can be improved
by repeating the algorithm N times and returning the
least cut.

 Failure Probability  (
 N 
n
)

for
N

.

n
e

Running time
 Choosing a random edge.
deg(u)
.
 Choose a vertex u with probability
m

 Now choose one of u’s with probability
deg(u)
 Pr[edge (u, v) is selected] 
 Time: O(n).
.
deg(u) 
deg(v) 


 .
m deg(u)
m deg(v) m
Running time
 Contracting edges: Use adjacency matrix.
u
u+v
v
u
u+v
v
 Add the rows and columns of u and v.
 O(n) time.
Running time
 The basic algorithm has n   iterations.
 Each iteration in the basic algorithm involves selecting
and contracting a random edge. O(n) time.

n
 The basic algorithm is repeated
times.

n
 Therefore running time = (n  )O(n).
