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.

Formally, we say a price-performance pair (a, b) dom<strong>in</strong>ates a pair (c, d) if a < c<br />

<strong>and</strong> b > d. A pair (a, b) is called a maximum pair if it is not dom<strong>in</strong>ated by any<br />

other pairs. We are <strong>in</strong>terested <strong>in</strong> ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>g the set of maxima of a collection C<br />

of price-performance pairs. That is, we would like to add new pairs to this<br />

collection (for example, when a new car is <strong>in</strong>troduced), <strong>and</strong> we would like to<br />

query this collection for a given dollar amount d to f<strong>in</strong>d the fastest car that costs<br />

no more than d dollars.<br />

We can store the set of maxima pairs <strong>in</strong> an ordered dictionary, D, ordered by cost,<br />

so that the cost is the key field <strong>and</strong> performance (speed) is the value field. We can<br />

then implement operations add(c,p), which adds a new cost-performance pair<br />

(c,p), <strong>and</strong> best(c), which returns the best pair with cost at most c, as shown <strong>in</strong><br />

Code Fragment 9.12.<br />

Code Fragment 9.12: The methods for ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>g<br />

a set of maxima, as implemented with an ordered<br />

dictionary D.<br />

573

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

Saved successfully!

Ooh no, something went wrong!