15
DESIGN & ANALYSIS OF ALGORITHM 07 – MAP Informatics Department Parahyangan Catholic University

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

Embed Size (px)

Citation preview

DESIGN & ANALYSIS OF ALGORITHM07 – MAP

Informatics Department

Parahyangan Catholic University

MAP

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.

MAP’S OPERATIONS

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

MAP’S OPERATIONS

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)

NULL 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)).

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)

EXAMPLE

REPRESENTATION #1USING LINKED - LIST

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

REPRESENTATION #2USING HASH TABLE

Expected running time for get(k), put(k,v), and remove(k) is O(1)

But worst case is still O(n)

JAVA’S HASH TABLE IMPLEMENTATION

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

ORDERED MAP

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

ORDERED MAP’SADDITIONAL METHODS

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

ORDERED MAP’SADDITIONAL METHODS

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

REPRESENTATION #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

REPRESENTATION #4 (ORDERED MAP)USING BINARY SEARCH TREE

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

JAVA’S ORDERED MAP IMPLEMENTATION

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))