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.

So the runn<strong>in</strong>g time for perform<strong>in</strong>g all the accesses <strong>in</strong> this case is O(n 2 ). Thus, the<br />

move-to-front implementation has faster access times for this scenario. This benefit<br />

comes at a cost, however.<br />

Implement<strong>in</strong>g the Move-to-Front Heuristic <strong>in</strong> <strong>Java</strong><br />

In Code Fragment 6.21, we give an implementation of a favorite list us<strong>in</strong>g the<br />

move-to-front heuristic. We implement the move-to-front approach <strong>in</strong> this case by<br />

def<strong>in</strong><strong>in</strong>g a new class, FavoriteListMTF, which extends the FavoriteList<br />

class <strong>and</strong> then overrides the def<strong>in</strong>itions of the moveUp <strong>and</strong> top methods. The<br />

moveUp method <strong>in</strong> this case simply removes the accessed element from its<br />

present position <strong>in</strong> the l<strong>in</strong>ked list <strong>and</strong> then <strong>in</strong>serts this element back <strong>in</strong> this list at<br />

the front. The top method, on the other h<strong>and</strong>, is more complicated.<br />

The Trade-Offs with the Move-to-Front Heuristic<br />

Now that we are no longer ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>g the favorite list as a list of entries ordered<br />

by their value's access counts, when we are asked to f<strong>in</strong>d the k most accessed<br />

elements, we need to search for them. In particular, we can implement method<br />

top(k) as follows:<br />

1. We copy the entries of our favorite list <strong>in</strong>to another list, C, <strong>and</strong> we create<br />

an empty list, T.<br />

2. We scan list C k times. In each scan, we f<strong>in</strong>d an entry of C with the largest<br />

access count, remove this entry from C, <strong>and</strong> <strong>in</strong>sert its value at the end of T.<br />

3. We return list T.<br />

This implementation of method top takes O(kn) time. Thus, when k is a constant,<br />

method top runs <strong>in</strong> O(n) time. This occurs, for example, when we want to get the<br />

"top ten" list. However, if k is proportional to n, then top runs <strong>in</strong> O(n2) time. This<br />

occurs, for example, when we want a "top 25%" list.<br />

Still, the move-to-front approach is just a heuristic, or rule of thumb, for there are<br />

access sequences where us<strong>in</strong>g the move-to-front approach is slower than simply<br />

keep<strong>in</strong>g the favorite list ordered by access counts. In addition, it trades off the<br />

potential speed of perform<strong>in</strong>g accesses that possess locality of reference, for a<br />

slower report<strong>in</strong>g of the top elements.<br />

6.5.3 Possible Uses of a Favorites List<br />

In Code Fragment 6.22, we use an example application of our favorite list<br />

implementations to solve the problem of ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>g the most popular URLs <strong>in</strong> a<br />

simulated sequence of Web page accesses. This program accesses a set of URLs <strong>in</strong><br />

360

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

Saved successfully!

Ooh no, something went wrong!