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.

8.4.2 Location-Aware Entries<br />

In order to implement methods remove, replaceKey, <strong>and</strong> replaceValue of<br />

an adapt able priority queue P, we need a mechanism for f<strong>in</strong>d<strong>in</strong>g the position of an<br />

entry of P. Namely, given the entry e of P passed as an argument to one of the above<br />

methods, we need to f<strong>in</strong>d the position stor<strong>in</strong>g e <strong>in</strong> the the data structure imple ment<strong>in</strong>g<br />

P (for example, a doubly l<strong>in</strong>ked list or a heap). This position is called the location of<br />

the entry.<br />

Instead of search<strong>in</strong>g for the location of a given entry e, we augment the entry object<br />

with an <strong>in</strong>stance variable of type Position stor<strong>in</strong>g the location. This im<br />

plementation of an entry that keeps track of its position is called a location-aware<br />

entry. A summary description of the the use of location-aware entries for the sorted<br />

list <strong>and</strong> heap implementations of an adaptable priority queue is provided below. We<br />

denote the number of entries <strong>in</strong> the priority queue at the time an operation is per<br />

formed, with n.<br />

• Sorted list implementation. In this implementation, after an entry is <strong>in</strong>serted, we<br />

set the location of the entry to refer to the position of the list conta<strong>in</strong><strong>in</strong>g the entry.<br />

Also, we update the location of the entry whenever it changes position <strong>in</strong> the list.<br />

Operations remove(e) <strong>and</strong> replaceValue(e,x) take O(1) time, s<strong>in</strong>ce we can<br />

obta<strong>in</strong> the position p of entry e <strong>in</strong> O(1) time follow<strong>in</strong>g the location reference stored<br />

with the entry. Instead, operation replaceKey(e, k) runs <strong>in</strong> O(n) time, because<br />

the modification of the key of entry e may require mov<strong>in</strong>g the entry to a different<br />

position <strong>in</strong> the list to preserve the order<strong>in</strong>g of the keys. The use of location-aware<br />

entries <strong>in</strong>creases the runn<strong>in</strong>g time of the st<strong>and</strong>ard priority queue operations by a<br />

constant factor.<br />

• Heap implementation. In this implementation, after an entry is <strong>in</strong>serted, we set<br />

the location of the entry to refer to the node of the heap conta<strong>in</strong><strong>in</strong>g the entry. Also,<br />

we update the location of the entry whenever it changes node <strong>in</strong> the heap (for<br />

example, because of the swaps <strong>in</strong> a down-heap or up-heap bubbl<strong>in</strong>g). Operation<br />

replaceValue(e,x) takes O(1) time s<strong>in</strong>ce we can obta<strong>in</strong> the position p of entry e<br />

<strong>in</strong> O(1) time follow<strong>in</strong>g the location reference stored with the entry. Operations<br />

remove(e) <strong>and</strong> replaceKey(e,k) run <strong>in</strong>stead <strong>in</strong> O(logn) (details are explored <strong>in</strong><br />

Exercise C-8.22). The use of location-aware entries <strong>in</strong>creases the runn<strong>in</strong>g time of<br />

operations <strong>in</strong>sert <strong>and</strong> removeM<strong>in</strong> by a constant factor overhead.<br />

The use of location-aware entries for the unsorted list implementation is explored <strong>in</strong><br />

Exercise C-8.21.<br />

Performance of Adaptable Priority Queue<br />

Implementations<br />

498

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

Saved successfully!

Ooh no, something went wrong!