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.

Let us briefly analyze the performance of a dictionary implemented with an un<br />

ordered list. Beg<strong>in</strong>n<strong>in</strong>g with the memory usage, we note that the space required<br />

for a list-based dictionary with n entries is O(n), s<strong>in</strong>ce the l<strong>in</strong>ked list data structure<br />

has memory usage proportional to its size. In addition, with this implementation<br />

of the dictionary ADT, we can realize operation <strong>in</strong>sert(k, v) easily <strong>and</strong> efficiently,<br />

just by a s<strong>in</strong>gle call to the add Last method on S, which simply adds the new<br />

entry to the end of the list. Thus, we achieve O(1) time for the <strong>in</strong>sert(k, v)<br />

operation on the dictionary D.<br />

Unfortunately, this implementation does not allow for an efficient execution of<br />

the f<strong>in</strong>d method. A f<strong>in</strong>d(k) operation requires, <strong>in</strong> the worst case, scann<strong>in</strong>g<br />

through the entire list S, exam<strong>in</strong><strong>in</strong>g each of its n entries. For example, we could<br />

use an iterator on the positions <strong>in</strong> S, stopp<strong>in</strong>g as soon as we encounter an entry<br />

with key equal to k (or reach the end of the list). The worst case for the runn<strong>in</strong>g<br />

time of this method clearly occurs when the search is unsuccessful, <strong>and</strong> we reach<br />

the end of the list hav<strong>in</strong>g exam<strong>in</strong>ed all of its n entries. Thus, the f<strong>in</strong>d method runs<br />

<strong>in</strong> O(n) time.<br />

Similarly, time proportional to n is needed <strong>in</strong> the worst case to perform a<br />

remove(e) operation on D, if we assume that entries do not keep track of their<br />

positions <strong>in</strong> S. Thus the runn<strong>in</strong>g time for perform<strong>in</strong>g operation remove(e) is<br />

O(n). Alternatively, if we use location-aware entries that store their position <strong>in</strong> S,<br />

then we can perform operation remove(e) <strong>in</strong> O(1) time. (See Section 9.5.1.)<br />

The operation f<strong>in</strong>d All always requires scann<strong>in</strong>g through the entire list S, <strong>and</strong><br />

therefore its runn<strong>in</strong>g time is O(n). More precisely, us<strong>in</strong>g the big-Theta notation<br />

(Section 4.2.3), we say that operation f<strong>in</strong>d All runs <strong>in</strong> Θ(n) time s<strong>in</strong>ce it takes<br />

time proportional to n <strong>in</strong> both the best <strong>and</strong> worst case.<br />

In conclusion, implement<strong>in</strong>g a dictionary with an unordered list provides for fast<br />

<strong>in</strong>sertions, but at the expense of slow searches <strong>and</strong> removals. Thus, we should<br />

only use this implementation where we either expect the dictionary to always be<br />

small or we expect the number of <strong>in</strong>sertions to be large relative to the number of<br />

searches <strong>and</strong> removals. Of course, archiv<strong>in</strong>g database <strong>and</strong> operat<strong>in</strong>g system<br />

transactions are precisely situations such as this.<br />

Nevertheless, there are many other scenarios where the number of <strong>in</strong>sertions <strong>in</strong> a<br />

dictionary will be roughly proportional to the number of searches <strong>and</strong> removals,<br />

<strong>and</strong> <strong>in</strong> these cases the list implementation is clearly <strong>in</strong>appropriate. The unordered<br />

dictionary implementation we discuss next can often be used, however, to achieve<br />

fast <strong>in</strong>sertions, removals, <strong>and</strong> searches <strong>in</strong> many such cases.<br />

9.3.2 Hash Table Dictionary Implementation<br />

We can use a hash table to implement the dictionary ADT, much <strong>in</strong> the same way as<br />

we did for the map ADT. The ma<strong>in</strong> difference, of course, is that a dictionary allows<br />

550

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

Saved successfully!

Ooh no, something went wrong!