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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

the size of the <strong>in</strong>put for this call. Say that this call is "good" if the pivot chosen is<br />

such that subsequences L <strong>and</strong> G have size at least n/4 <strong>and</strong> at most 3n/4 each;<br />

otherwise, a call is "bad."<br />

Now, consider the implications of our choos<strong>in</strong>g a pivot uniformly at r<strong>and</strong>om. Note<br />

that there are n/2 possible good choices for the pivot for any given call of size n of<br />

the r<strong>and</strong>omized quick-sort algorithm. Thus, the probability that any call is good is<br />

1/2. Note further that a good call will at least partition a list of size n <strong>in</strong>to two lists<br />

of size 3n/4 <strong>and</strong> n/4, <strong>and</strong> a bad call could be as bad as produc<strong>in</strong>g a s<strong>in</strong>gle call of<br />

size n − 1.<br />

Now consider a recursion trace for r<strong>and</strong>omized quick-sort. This trace def<strong>in</strong>es a<br />

b<strong>in</strong>ary tree, T, such that each node <strong>in</strong> T corresponds to a different recursive call on<br />

a subproblem of sort<strong>in</strong>g a portion of the orig<strong>in</strong>al list.<br />

Say that a node v <strong>in</strong> T is <strong>in</strong> size group i if the size of v's subproblem is greater than<br />

(3/4) i + 1 n <strong>and</strong> at most (3/4) i n. Let us analyze the expected time spent work<strong>in</strong>g on<br />

all the subproblems for nodes <strong>in</strong> size group i. By the l<strong>in</strong>earity of expectation<br />

(Proposition A.19), the expected time for work<strong>in</strong>g on all these subproblems is the<br />

sum of the expected times for each one. Some of these nodes correspond to good<br />

calls <strong>and</strong> some correspond to bad calls. But note that, s<strong>in</strong>ce a good call occurs<br />

with probability 1/2, the expected number of consecutive calls we have to make<br />

before gett<strong>in</strong>g a good call is 2. Moreover, notice that as soon as we have a good<br />

call for a node <strong>in</strong> size group i, its children will be <strong>in</strong> size groups higher than i.<br />

Thus, for any element x from <strong>in</strong> the <strong>in</strong>put list, the expected number of nodes <strong>in</strong><br />

size group i conta<strong>in</strong><strong>in</strong>g x <strong>in</strong> their subproblems is 2. In other words, the expected<br />

total size of all the subproblems <strong>in</strong> size group i is 2n. S<strong>in</strong>ce the nonrecursive work<br />

we perform for any subproblem is proportional to its size, this implies that the<br />

total expected time spent process<strong>in</strong>g subproblems for nodes <strong>in</strong> size group i is<br />

O(n).<br />

The number of size groups is log 4/3 n, s<strong>in</strong>ce repeatedly multiply<strong>in</strong>g by 3/4 is the<br />

same as repeatedly divid<strong>in</strong>g by 4/3. That is, the number of size groups is O(logn).<br />

Therefore, the total expected runn<strong>in</strong>g time of r<strong>and</strong>omized quick-sort is O(nlogn).<br />

(See Figure 11.13.)<br />

Figure 11.13: A visual time analysis of the quicksort<br />

tree T. Each node is shown labeled with the size of<br />

its subproblem.<br />

703

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

Saved successfully!

Ooh no, something went wrong!