Presentation is loading. Please wait.

Presentation is loading. Please wait.

D ESIGN & A NALYSIS OF A LGORITHM 07 – M AP Informatics Department Parahyangan Catholic University.

Similar presentations


Presentation on theme: "D ESIGN & A NALYSIS OF A LGORITHM 07 – M AP Informatics Department Parahyangan Catholic University."— Presentation transcript:

1 D ESIGN & A NALYSIS OF A LGORITHM 07 – M AP Informatics Department Parahyangan Catholic University

2 M AP A map allows us to store elements so they can be located quickly using keys. Specifically, a map stores key-value pairs (k,v), which we call entries, where k is the key and v is its corresponding value. Map requires that each key be unique.

3 M AP ’ S O PERATIONS size() Return the number of entries in M isEmpty() Test whether M is empty get(k) If M contains an entry (k,v), then return v, else return null put(k,v) if M does not have an entry with key equal to k, then add entry (k,v) to M and return null; else, replace the existing value with v and return the old value

4 M AP ’ S O PERATIONS remove(k) If M have an entry with key equal to k, remove that entry from M and return its value; else return null keySet() Returns an iterable collection containing all the keys stored in M (so keySet().iterator() returns an iterator of keys) values() Returns an iterable collection containing all the values stored in M (so values().iterator() returns an iterator of values) entrySet() Returns an iterable collection containing all the key-value entries in M (so entrySet().iterator() returns an iterator of entries)

5 N ULL AS A SENTINEL get(k), put(k,v), and remove(k) returns null when map M does not have an entry with key equal to k A special value such as this is known as a sentinel The disadvantage with using null as a sentinel is that we cannot store an entry with value null (i.e., entry (k, null) ).

6 put(5,A)  null put(7,B)  null put(5,C)  A get(5)  C get(4)  null remove(5)  C put(3,D)  null entrySet()  {(7,B),(3,D)} keySet()  {7,3} values()  {B,D} (5,A) (7,B) (5,C) (3,D) E XAMPLE

7 R EPRESENTATION #1 USING L INKED - L IST A simple way of implementing a map is to store its n entries in a doubly linked list S Fundamental operations get(k), put(k,v), and remove(k) involves simple scans on S  O(n) time

8 R EPRESENTATION #2 USING H ASH T ABLE Expected running time for get(k), put(k,v), and remove(k) is O(1) But worst case is still O(n)

9 J AVA ’ S H ASH T ABLE I MPLEMENTATION The Java Collections Framework provides a hash table implementation in the class java.util.HashMap This class implements the java.util.Map interface, hence it performs all the methods of Map, as well as some other methods such as clear(), which removes all the entries in the map This class implements hash table using separate chaining method

10 O RDERED M AP In some applications, simply looking up values based on associated keys is not enough We often also wants to keep the entries in a map sorted according to some total order In an ordered map, we want to perform the usual map operations, but also maintain an order relation for the keys in our map and use this order in some of the map operations

11 O RDERED M AP ’ S A DDITIONAL M ETHODS firstEntry(k) Returns the entry with smallest key value; If the map is empty, then it returns null lastEntry(k) Returns the entry with largest key value; If the map is empty, then it returns null

12 O RDERED M AP ’ S A DDITIONAL M ETHODS ceilingEntry(k) Returns the entry with the least key value ≥ k ; If there is no such entry, then it returns null floorEntry(k) Returns the entry with the greatest key value ≤ k ; If there is no such entry, then it returns null higherEntry(k) Returns the entry with the least key value > k ; If there is no such entry, then it returns null lowerEntry(k) Returns the entry with the greatest key value < k ; If there is no such entry, then it returns null

13 R EPRESENTATION #3 ( ORDERED MAP ) USING ORDERED ARRAY LIST We store the map’s entries in an array list S in increasing order of keys An ordered array list allows faster searching than a sorted linked list (i.e., Binary search v.s. sequential search)  O(lg n) Update operations put(k,v) and remove(k) may need to shift all the entries in the array list  takes O(n) time

14 R EPRESENTATION #4 ( ORDERED MAP ) USING B INARY S EARCH T REE Other alternative is to use Binary Search Tree to store map’s entries Fundamental operations get(k), put(k,v), and remove(k) are simply search, insert, and delete BST operation  takes O(h) time For random keys, expected tree height is O(lg n), so all fundamental operations are expected to be O(lg n) time

15 J AVA ’ S O RDERED M AP I MPLEMENTATION The Java Collections Framework provides an ordered map implementation in the class java.util.TreeMap This class implements java.util.NavigableMap interface, which includes all java.util.sortedMap ’s operations The implementation uses Red-Black Tree, which is a balanced tree (thus the tree’s height is guaranteed to be O(lg n))


Download ppt "D ESIGN & A NALYSIS OF A LGORITHM 07 – M AP Informatics Department Parahyangan Catholic University."

Similar presentations


Ads by Google