Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ

Similar presentations


Presentation on theme: "CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ"— Presentation transcript:

1 CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ
Maps and Dictionaries Map ADT Linked list implementation of Maps 5/16/2019 CS210-Summer 2005, Lecture 15

2 Adaptable Priority Queues
Suppose we have an online trading system where orders to purchase and sell a given 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’<p is added (the execution is complete if s’>s) A sell order (p,s) is executed when a buy order (p’,s’) with price p’>p is added 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? 5/16/2019 CS210-Summer 2005, Lecture 15

3 Methods of the Adaptable PQ ADT
remove(e): Remove and return entry e. replaceKey(e,k): Replace with k and return the key of entry e of P; an error condition occurs if k is invalid (that is, k cannot be compared with other keys). replaceValue(e,x): Replace with x and return the value of entry e of P. 5/16/2019 CS210-Summer 2005, Lecture 15

4 Example Operation Output P insert(5,A) e1 (5,A)
insert(3,B) e2 (3,B),(5,A) insert(7,C) e3 (3,B),(5,A),(7,C) min() e2 (3,B),(5,A),(7,C) key(e2) 3 (3,B),(5,A),(7,C) remove(e1) e1 (3,B),(7,C) replaceKey(e2,9) 3 (7,C),(9,B) replaceValue(e3,D) C (7,D),(9,B) remove(e2) e2 (7,D) 5/16/2019 CS210-Summer 2005, Lecture 15

5 Locating Entries In order to implement the operations remove(k), replaceKey(e), and replaceValue(k), we need fast ways of locating an entry e in a priority queue. We can always just search the entire data structure to find an entry e, but there are better ways for locating entries. 5/16/2019 CS210-Summer 2005, Lecture 15

6 Location-Aware Entries
A locator-aware entry identifies and tracks the location of its (key, value) object within a data structure Main idea: Since entries are created and returned from the data structure itself, it can return location-aware entries, thereby making future updates easier 5/16/2019 CS210-Summer 2005, Lecture 15

7 List Implementation A location-aware list entry 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 trailer header nodes/positions 2 c 4 c 5 c 8 c entries 5/16/2019 CS210-Summer 2005, Lecture 15

8 Heap Implementation A location-aware heap entry is an object storing
key value position of the entry in the underlying heap In turn, each heap position stores an entry Back pointers are updated during entry swaps 2 d 4 a 6 b 8 g 5 e 9 c 5/16/2019 CS210-Summer 2005, Lecture 15

9 Performance Using location-aware entries we can achieve the following running times: Method Unsorted List Sorted List Heap size, isEmpty O(1) O(1) O(1) insert O(1) O(n) O(log n) min O(n) O(1) O(1) removeMin O(n) O(1) O(log n) remove O(1) O(1) O(log n) replaceKey O(1) O(n) O(log n) replaceValue O(1) O(1) O(1) 5/16/2019 CS210-Summer 2005, Lecture 15

10 Maps and Dictionaries The primary use of map or dictionary is to store elements so that they can be located quickly using keys. The motivation for such searches is that each element stores additional useful information besides the search key, but the only way to get at that information is to use the search key. Example: Bank account information. 5/16/2019 CS210-Summer 2005, Lecture 15

11 Maps and Dictionaries Like priority queues, maps and dictionaries store key-value pairs, called entries. Maps require that each key be unique, while dictionaries allow multiple entries to have the same key, just like priority queues. Total order relation is always required for keys in priority queues, it is optional for dictionaries. 5/16/2019 CS210-Summer 2005, Lecture 15

12 The Map ADT A map stores key-value pairs (k, v), which we call entries, where k is the key and v is its corresponding value. The map ADT requires each key be unique. We allow both the keys and the values stored in a map to be of any Object type. 5/16/2019 CS210-Summer 2005, Lecture 15

13 Map ADT size(): Return the number of entries in M.
isEmpty(): Test whether M is empty get(k): if the map has an entry e with key k, then returns the value of e, else returns null put(k, v): if map does not have entry with key k, then inserts the entry (k, v) to M and return null. Otherwise replace with v the existing value of the entry with key k and return the old value. remove(k): remove from M the entry with key k and returns its value. If M has no such entry then return null. keys(): returns an iterator of the keys stored in M. values(): returns an iterator of the values associated with keys stored in M. 5/16/2019 CS210-Summer 2005, Lecture 15

14 Map ADT When get(k), put(k, v) and remove(k) are performed on a map that has no entry with key equal to k, we use the convention of returning null. What is the disadvantage of doing this? 5/16/2019 CS210-Summer 2005, Lecture 15

15 Maps in the java.util.package
java.util package includes an interface for the Map ADT. Interface java.util.Map does not have any methods to directly return iterators of a Maps keys or values, but it does have methods to return a set of keys or values, which can in turn provide an iterator. 5/16/2019 CS210-Summer 2005, Lecture 15

16 A Simple List-Based Map implementation
A simple way of implementing a map is to store its n entries in a list S, implemented as a doubly linked list. get(k), put(k,v) and remove(k), involves simple scan down S looking for an entry with key k. Each of the above methods take O(n) time on a Map with n entries, because each method involves searching through the entire list in the worst case. 5/16/2019 CS210-Summer 2005, Lecture 15


Download ppt "CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ"

Similar presentations


Ads by Google