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.

Ai(1)= Ai −1 (2)<br />

for i≥1<br />

Ai(n)= Ai− 1 (Ai(n − 1)) for i ≥ 1 <strong>and</strong> n ≥ 2.<br />

In other words, the Ackermann functions def<strong>in</strong>e a progression of functions:<br />

• A 0 (n) = 2n is the multiply-by-two function<br />

• A 1 (n) = 2 n is the power-of-two function<br />

• A2(n) =Math (with n 2's) is the tower-of-twos function<br />

• <strong>and</strong> so on.<br />

We then def<strong>in</strong>e the Ackermann function as A(n) = An(n), which is an <strong>in</strong>credibly<br />

fast grow<strong>in</strong>g function. Likewise, the <strong>in</strong>verse Ackermann function,<br />

α(n) = m<strong>in</strong>{m: A(m) ≥ n},<br />

is an <strong>in</strong>credibly slow grow<strong>in</strong>g function. It grows much slower than the log* n<br />

function (which is the <strong>in</strong>verse of A 2 (n)), for example, <strong>and</strong> we have already noted<br />

that log* n is a very slow-grow<strong>in</strong>g function.<br />

11.7 Selection<br />

There are a number of applications <strong>in</strong> which we are <strong>in</strong>terested <strong>in</strong> identify<strong>in</strong>g a s<strong>in</strong>gle<br />

element <strong>in</strong> terms of its rank relative to an order<strong>in</strong>g of the entire set. Examples <strong>in</strong>clude<br />

identify<strong>in</strong>g the m<strong>in</strong>imum <strong>and</strong> maximum elements, but we may also be <strong>in</strong>terested <strong>in</strong>,<br />

say, identify<strong>in</strong>g the median element, that is, the element such that half of the other<br />

elements are smaller <strong>and</strong> the rema<strong>in</strong><strong>in</strong>g half are larger. In general, queries that ask for<br />

an element with a given rank are called order statistics.<br />

Def<strong>in</strong><strong>in</strong>g the Selection Problem<br />

In this section, we discuss the general order-statistic problem of select<strong>in</strong>g the kth<br />

smallest element from an unsorted collection of n comparable elements. This is<br />

known as the selection problem. Of course, we can solve this problem by sort<strong>in</strong>g<br />

the collection <strong>and</strong> then <strong>in</strong>dex<strong>in</strong>g <strong>in</strong>to the sorted sequence at <strong>in</strong>dex k−1. Us<strong>in</strong>g the<br />

best comparison-based sort<strong>in</strong>g algorithms, this approach would take O(nlogn) time,<br />

which is obviously an overkill for the cases where k = 1 or k = n (or even k = 2, k =<br />

3, k = n − 1, or k = n − 5), because we can easily solve the selection problem for<br />

these values of k <strong>in</strong> O(n) time. Thus, a natural question to ask is whether we can<br />

achieve an O(n) runn<strong>in</strong>g time for all values of k (<strong>in</strong>clud<strong>in</strong>g the <strong>in</strong>terest<strong>in</strong>g case of<br />

f<strong>in</strong>d<strong>in</strong>g the median, where k =floorln/2 floorr;).<br />

11.7.1 Prune-<strong>and</strong>-Search<br />

726

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

Saved successfully!

Ooh no, something went wrong!