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.

As <strong>in</strong> the examples above from an airport, the key used for comparisons is often<br />

more than a s<strong>in</strong>gle numerical value, such as price, length, weight, or speed. That is,<br />

a key can sometimes be a more complex property that cannot be quantified with a<br />

s<strong>in</strong>gle number. For example, the priority of st<strong>and</strong>by passengers is usually<br />

determ<strong>in</strong>ed by tak<strong>in</strong>g <strong>in</strong>to account a host of different factors, <strong>in</strong>clud<strong>in</strong>g frequentflyer<br />

status, the fare paid, <strong>and</strong> check-<strong>in</strong> time. In some applications, the key for an<br />

object is part of the object itself (for example, it might be an <strong>in</strong>stance variable<br />

stor<strong>in</strong>g the list price of a book, or the weight of a car). In other applications, the key<br />

is not part of the object but the object gets assigned its key by the application (for<br />

example, the quality rat<strong>in</strong>g given to a stock by a f<strong>in</strong>ancial analyst, or the priority<br />

assigned to a st<strong>and</strong>by passenger by a gate agent).<br />

Compar<strong>in</strong>g Keys with Total Orders<br />

A priority queue needs a comparison rule that will never contradict itself. In order<br />

for a comparison rule, which we denote by ≤, to be robust <strong>in</strong> this way, it must<br />

def<strong>in</strong>e a total order relation, which is to say that the comparison rule is def<strong>in</strong>ed<br />

for every pair of keys <strong>and</strong> it must satisfy the follow<strong>in</strong>g properties:<br />

• Reflexive property: k ≤ k.<br />

• Antisymmetric property: if k 1 ≤ k 2 <strong>and</strong> k 2 ≤ k 1 , then k 1 = k 2 .<br />

• Transitive property: if k 1 ≤ k 2 <strong>and</strong> k 2 ≤ k3, then k 1 ≤ k 3 .<br />

Any comparison rule, ≤, that satisfies these three properties will never lead to a<br />

comparison contradiction. In fact, such a rule def<strong>in</strong>es a l<strong>in</strong>ear order<strong>in</strong>g<br />

relationship among a set of keys; hence, if a (f<strong>in</strong>ite) collection of elements has a<br />

total order def<strong>in</strong>ed for it, then the notion of a smallest key, k m<strong>in</strong> , is well def<strong>in</strong>ed, as<br />

a key <strong>in</strong> which k m<strong>in</strong> ≤ k, for any other key k <strong>in</strong> our collection.<br />

A priority queue is a collection of elements, called values, each hav<strong>in</strong>g an<br />

associated key that is provided at the time the element is <strong>in</strong>serted. A key-value<br />

pair <strong>in</strong>serted <strong>in</strong>to a priority queue is called an entry of the priority queue. The<br />

name "priority queue" comes from the fact that keys determ<strong>in</strong>e the "priority" used<br />

to pick entries to be removed. The two fundamental methods of a priority queue P<br />

are as follows:<br />

• <strong>in</strong>sert(k,x): Insert a value x with key k <strong>in</strong>to P.<br />

• removeM<strong>in</strong>(): Return <strong>and</strong> remove from P an entry with the smallest<br />

key, that is, an entry whose key is less than or equal to that of every other entry<br />

<strong>in</strong> P.<br />

By the way, some people refer to the removeM<strong>in</strong> method as the<br />

"extractM<strong>in</strong>" method, so as to stress that this method simultaneously removes<br />

<strong>and</strong> returns an entry P. There are many applications where the <strong>in</strong>sert <strong>and</strong><br />

451

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

Saved successfully!

Ooh no, something went wrong!