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.

at the time the method is executed. Moreover, these methods run <strong>in</strong> time<br />

proportional to n even <strong>in</strong> the best case, s<strong>in</strong>ce they each require search<strong>in</strong>g the entire<br />

list to f<strong>in</strong>d a m<strong>in</strong>imum-key entry. That is, us<strong>in</strong>g the notation of Section 4.2.3, we<br />

can say that these methods run <strong>in</strong> θ(n) time. F<strong>in</strong>ally, we implement methods size<br />

<strong>and</strong> isEmpty by simply return<strong>in</strong>g the output of the correspond<strong>in</strong>g methods<br />

executed on list S.<br />

Thus, by us<strong>in</strong>g an unsorted list to implement a priority queue, we achieve<br />

constant-time <strong>in</strong>sertion, but l<strong>in</strong>ear-time search <strong>and</strong> removal.<br />

8.2.2 Implementation with a Sorted List<br />

An alternative implementation of a priority queue P also uses a list S, except that<br />

this time let us store the entries sorted by key. Specifically, we represent the priority<br />

queue P by us<strong>in</strong>g a list S of entries sorted by nondecreas<strong>in</strong>g keys, which means that<br />

the first element of S is an entry with the smallest key.<br />

Fast Removals <strong>and</strong> Slow Insertions<br />

We can implement method m<strong>in</strong> <strong>in</strong> this case simply by access<strong>in</strong>g the first element<br />

of the list with the first method of S. Likewise, we can implement the<br />

removeM<strong>in</strong> method of P as S.remove(S.first()). Assum<strong>in</strong>g that S is<br />

implemented with a doubly l<strong>in</strong>ked list, operations m<strong>in</strong> <strong>and</strong> removeM<strong>in</strong> <strong>in</strong> P<br />

take O(1) time. Thus, us<strong>in</strong>g a sorted list allows for simple <strong>and</strong> fast<br />

implementations of priority queue access <strong>and</strong> removal methods.<br />

This benefit comes at a cost, however, for now method <strong>in</strong>sert of P requires that<br />

we scan through the list S to f<strong>in</strong>d the appropriate position to <strong>in</strong>sert the new entry.<br />

Thus, implement<strong>in</strong>g the <strong>in</strong>sert method of P now takes O(n) time, where n is the<br />

number of entries <strong>in</strong> P at the time the method is executed. In summary, when<br />

us<strong>in</strong>g a sorted list to implement a priority queue, <strong>in</strong>sertion runs <strong>in</strong> l<strong>in</strong>ear time<br />

whereas f<strong>in</strong>d<strong>in</strong>g <strong>and</strong> remov<strong>in</strong>g the m<strong>in</strong>imum can be done <strong>in</strong> constant time.<br />

Compar<strong>in</strong>g the Two List-Based Implementations<br />

Table 8.2 compares the runn<strong>in</strong>g times of the methods of a priority queue realized<br />

by means of a sorted <strong>and</strong> unsorted list, respectively. We see an <strong>in</strong>terest<strong>in</strong>g tradeoff<br />

when we use a list to implement the priority queue ADT. An unsorted list<br />

allows for fast <strong>in</strong>sertions but slow queries <strong>and</strong> deletions, while a sorted list allows<br />

for fast queries <strong>and</strong> deletions, but slow <strong>in</strong>sertions.<br />

Table 8.2: Worst-case runn<strong>in</strong>g times of the<br />

methods of a priority queue of size n, realized by<br />

461

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

Saved successfully!

Ooh no, something went wrong!