23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Exercise (C-1 1.31) for modify<strong>in</strong>g r<strong>and</strong>omized quick-select to get a determ<strong>in</strong>istic<br />

selection algorithm that runs <strong>in</strong> O(n) worst-case time. The existence of this<br />

determ<strong>in</strong>istic algorithm is mostly of theoretical <strong>in</strong>terest, however, s<strong>in</strong>ce the constant<br />

factor hidden by the big-Oh notation is relatively large <strong>in</strong> this case. Suppose we are<br />

given an unsorted sequence S of n comparable elements together with an <strong>in</strong>teger k ><br />

[1,n]. At a high level, the quick-select algorithm for f<strong>in</strong>d<strong>in</strong>g the kth smallest element<br />

<strong>in</strong> S is similar <strong>in</strong> structure to the r<strong>and</strong>omized quicksort algorithm described <strong>in</strong><br />

Section 11.2.1. We pick an element x from S at r<strong>and</strong>om <strong>and</strong> use this as a "pivot" to<br />

subdivide S <strong>in</strong>to three subsequences L, E, <strong>and</strong> G, stor<strong>in</strong>g the elements of S less than<br />

x, equal to x, <strong>and</strong> greater than x, respectively. This is the prune step. Then, based on<br />

the value of k, we then determ<strong>in</strong>e which of these sets to recur on. R<strong>and</strong>omized<br />

quick-select is described <strong>in</strong> Code Fragment 11.11. Algorithm quickSelect(S,k):<br />

Input: Sequence S of n comparable elements, <strong>and</strong> an <strong>in</strong>teger k [1,n] Output: The<br />

kth smallest element of S if n = 1 then return the (first) element of S. pick a r<strong>and</strong>om<br />

(pivot) element x of S <strong>and</strong> divide S <strong>in</strong>to three sequences: •L, stor<strong>in</strong>g the elements <strong>in</strong><br />

S less than x •E, stor<strong>in</strong>g the elements <strong>in</strong> S equal to x •G, stor<strong>in</strong>g the elements <strong>in</strong> S<br />

greater than x. if k≤|L| then quickSelect(L,k) else if k≤ |L| + |E| then return x {each<br />

element <strong>in</strong> E is equal to x} else quickSelect(G,k − |L| — |E|) {note the new selection<br />

parameter}<br />

11.7.3 Analyz<strong>in</strong>g R<strong>and</strong>omized Quick-Select<br />

Show<strong>in</strong>g that r<strong>and</strong>omized quick-select runs <strong>in</strong> O(n) time requires a simple<br />

probabilistic argument. The argument is based on the l<strong>in</strong>earity of expectation,<br />

which states that if X <strong>and</strong> Y are r<strong>and</strong>om variables <strong>and</strong> c is a number, then<br />

E(X + Y)=E(X)+E(Y) <strong>and</strong> E(cX)=cE(X),<br />

where we use E(Z) to denote the expected value of the expression Z.<br />

Let t (n) be the runn<strong>in</strong>g time of r<strong>and</strong>omized quick-select on a sequence of size n.<br />

S<strong>in</strong>ce this algorithm depends on r<strong>and</strong>om events, its runn<strong>in</strong>g time, t(n), is a r<strong>and</strong>om<br />

variable. We want to bound E(t(n)), the expected value of t(n). Say that a recursive<br />

<strong>in</strong>vocation of our algorithm is "good" if it partitions S so that the size of L <strong>and</strong> G is<br />

at most 3n/4. Clearly, a recursive call is good with probability 1/2. Let g(n) denote<br />

the number of consecutive recursive calls we make, <strong>in</strong>clud<strong>in</strong>g the present one,<br />

before we get a good one. Then we can characterize t (n) us<strong>in</strong>g the follow<strong>in</strong>g<br />

recurrence equation:<br />

t(n)≤bn·g(n) + t(3n/4),<br />

where b ≥ 1 is a constant. Apply<strong>in</strong>g the l<strong>in</strong>earity of expectation for n > 1, we get<br />

E (t(n)) ≤E(bn·g(n) + t(3n/4)) =bn·E (g(n)) + E (t(3n/4)).<br />

S<strong>in</strong>ce a recursive call is good with probability 1/2, <strong>and</strong> whether a recursive call is<br />

good or not is <strong>in</strong>dependent of its parent call be<strong>in</strong>g good, the expected value of g(n)<br />

728

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!