Download presentation
Presentation is loading. Please wait.
Published bySamson Ward Modified over 8 years ago
1
Adaptable Priority Queues1 3 a 5 g 4 e © 2013 Goodrich, Tamassia, Goldwasser
2
Adaptable Priority Queues2 Items and Priority Queues An item stores a (key, value) pair Item fields: _key: the key associated with this item _value: the value paired with the key associated with this item Priority Queue ADT: add(k, x) inserts an item with key k and value x remove_min() removes and returns the item with smallest key min() returns, but does not remove, an item with smallest key len(P), is_empty() © 2013 Goodrich, Tamassia, Goldwasser
3
Adaptable Priority Queues3 Example Online trading system where orders to purchase and sell a stock are stored in two priority queues (one for sell orders and one for buy orders) as (p,s) entries: The key, p, of an order is the price The value, s, for an entry is the number of shares A buy order (p,s) is executed when a sell order (p’,s’) with price p’ s) A sell order (p,s) is executed when a buy order (p’,s’) with price p’>p is added (the execution is complete if s’>s) What if someone wishes to cancel their order before it executes? What if someone wishes to update the price or number of shares for their order? © 2013 Goodrich, Tamassia, Goldwasser
4
Adaptable Priority Queues4 Methods of the Adaptable Priority Queue ADT remove(loc): Remove from P and return item e for locator loc. update(loc,k,v): Replace the key- value pair for locator, loc, with (k,v). © 2013 Goodrich, Tamassia, Goldwasser
5
Adaptable Priority Queues5 Locators A locator-aware item identifies and tracks the location of its (key, value) object within a data structure Intuitive notion: Coat claim check Valet claim ticket Reservation number Main idea: Since items are created and returned from the data structure itself, it can return location-aware items, thereby making future updates easier © 2013 Goodrich, Tamassia, Goldwasser
6
Adaptable Priority Queues6 List Implementation A location-aware list item is an object storing key value position (or rank) of the item in the list In turn, the position (or array cell) stores the entry Back pointers (or ranks) are updated during swaps trailer header nodes/positions items 2 c 4 c 5 c 8 c © 2013 Goodrich, Tamassia, Goldwasser
7
Adaptable Priority Queues7 Heap Implementation A location-aware heap item is an object storing key value position of the item in the underlying heap In turn, each heap position stores an item Back pointers are updated during item swaps 4 a 2 d 6 b 8 g 5 e 9 c © 2013 Goodrich, Tamassia, Goldwasser
8
Adaptable Priority Queues8 Performance Improved times thanks to location-aware items are highlighted in red MethodUnsorted ListSorted ListHeap len, is_empty O(1)O(1)O(1) add O(1)O(n)O(log n) min O(n)O(1)O(1) remove_min O(n)O(1)O(log n) remove O(1)O(1)O(log n) update O(1)O(n)O(log n) © 2013 Goodrich, Tamassia, Goldwasser
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.