maps a map is an object that maps keys to values each key can map to at most one value, and a map...

42
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys Key Value Map Examples • Dictionaries: Words Their definitions. • Social security numbers Peopleʼs names

Upload: colin-simpson

Post on 24-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

MapsA map is an object that maps keys to values•Each key can map to at most one value, and a map cannot contain duplicate keys

Key Value

Map Examples • Dictionaries: Words Their definitions. • Social security numbers Peopleʼs names• IP addresses Host names

Page 2: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

SimpleMapJava has a sophisticated Map interface, which has an extensive collection of methods and “views”. Here we present a stripped-down version.

Page 3: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Most methods in SimpleMap match the Map API. One exception is keyIterator(), which is equivalent to calling keySet().iterator() in the API.

Page 4: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Implementation using ListsAn easy way to implement SimpleMap is to use a List of MapEntry objects, which are key/value pairs.

Page 5: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Put a key into the mapNeed to check if the key exists. We can use a linear scan.

Page 6: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Put the value of a keyNeed to check if the key exists. We can use a linear scan.

Page 7: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Remove a key from the map

Page 8: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Check if it contains a given key

Page 9: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Key iterator

Page 10: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Implementation using BSTIf the keys have a natural ordering, we can represent maps using a BSTSet consisting of MapEntry objects.

Page 11: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Page 12: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Page 13: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Page 14: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Page 15: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Lookup Table

Suppose the keys are integers, we could implement a Map by using a look up table, represented as an array of values, indexed by key: The value associated with key K would be stored in the array cell k.

234569806

:::

John Scott

Name[234569806] = “John Scott”

What is the time complexity? Space requirement?

Page 16: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Hash Table

1. For each key use a function, called a hash function, to compute an integer, called they key’s hash code.

Page 17: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Hash Table

2. Take the hash code modulo the array size to get an index into an array.

3. Store the key and the value in a linked list of entries at the array index.

Collision: Two different keys may end up with the same hash code, or two different hash codes result in the same array index.

Internal data structure?

Page 18: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Java HashCodeEvery Java Object has a default hashcode() method, which returns a 32-bit integer. For instance, the hashcode () method for the String class (as specified in the Java Standard), looks like this:

The two special things about the number 31:•It is prime, so that when the user mods out by another number, they have no common factors (unless it's a multiple of 31) •It is a Mersenne prime which is a prime number of the form 2n−1. Thus, the product can be done with one shift and one subtract.

Page 19: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Hashing Performance

Page 20: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Hash MapsThe standard implementation of hash tables in the Java libraries is called HashMap. The Set version is called HashSet.

It is crucial that if two keys are equal, then they must have the same hash code. Thus, if you override equals(), you must also override hashCode().

Page 21: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Rehashing and the Load Factor

When more and more elements are added, the table becomes fuller, collisions increase, the bucket lists get longer and longer, and performance deteriorates.

Solution: rehash the elements, when n/M exceeds some threshold L (called load factor)

Java sets L to be 0.75

Page 22: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Page 23: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Time Complexity

Page 24: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Priority Queue

Priority queue: used to prioritize entries. An entry with any key may be inserted at any time, but when peeking or removing, you get the one with the entry whose key is the lowest (or the highest).

Applications•Event simulations•Packet delivery•Printer queue

Priorities in Highest Priority out

Page 25: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Basic Methods for Priority Queue

• Add(): Add a new element to the queue

• peek() : Return the element with the smallest/largest key in the queue

• remove(): Return and remove the element with the smallest / largest key in the queue

Page 26: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Basic Methods for Priority Queue

• Add(): Add a new element to the queue

• peek() : Return the element with the smallest/largest key in the queue

• remove(): Return and remove the element with the smallest / largest key in the queue

Page 27: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Binary Heaps: An implementation of Priority Queues

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

Page 28: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

A binary heap is a complete binary tree whose entriessatisfy the “Heap-order property”: No child has a key that is smaller than its parent's key.

Page 29: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

A binary heap is a complete binary tree whose entriessatisfy the “Heap-order property”: No child has a key that is smaller than its parent's key.

Implementation:1)Using reference-based tree data structure2)Using an array

Page 30: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Method Implementation

E peek(): The heap-order property ensures that the entry with the minimum key is always at the top of the heap. Hence, we simply return the entry at the root node. If the heap is empty, return null or throw an exception.

Page 31: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

boolean add(E x)

We place the new entry x in the bottom level of the tree, at the first free spot from the left. (If the bottom level is full,start a new level with x at the far left.) In an array-based implementation, we place x in the first free location in the array.

Page 32: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

boolean add(E x)

We place the new entry x in the bottom level of the tree, at the first free spot from the left. (If the bottom level is full,start a new level with x at the far left.) In an array-based implementation, we place x in the first free location in the array.

The problem is, the new entry's key may violate the heap-order property.

Page 33: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Solution: having the entry percolate up the tree until the heap-order property is satisfied: Compare x's key with its parent's key; if x’s key is less, exchange x with its parent, then repeat the procedure with x's new parent.

Page 34: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

p ≤ s (where s is x's sibling), p ≤ l, and p ≤ r (where l and r are x's children). We only swap if x < p, which implies that x < s; after the swap, x is the parent of s. After the swap, p is the parent of l and r.

Page 35: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

E remove()

If the heap is empty, return null or throw an exception. Otherwise, begin by removing the entry at the root node and saving it for the return value. This leaves a hole at the root. We fill the hole with the last entry in the tree, which we call x, so that the tree remains complete.

The problem is, it is unlikely that x has the minimum key.

Page 36: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Solution: Percolate x down the heap

If x has a child whose key is smaller, swap x with the child having the minimum key. Next, compare x with its new children; if x still violates the heap-order property, again swap x with the child with the minimum key. Continue until x is less than or equal to its children, or reaches a leaf.

Page 37: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Solution: Percolate x down the heap

If x has a child whose key is smaller, swap x with the child having the minimum key. Next, compare x with its new children; if x still violates the heap-order property, again swap x with the child with the minimum key. Continue until x is less than or equal to its children, or reaches a leaf.

Page 38: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Performance

Page 39: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Bottom-Up Heap Construction

Suppose we are given a bunch of randomly ordered entries, and want to make a heap out of them. We could add() them one by one in O(n log n) time, but there's a faster way. We define one more heap operation.

Page 40: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

void heapify()1. Make a complete tree out of the entries by just throwing

them, in any order, into an array.2. Work backward from the last internal node (non-leaf node)

to the root node, in reverse order in the array or the level-order traversal: When visiting a node, percolate its entry down the heap as in remove().

Time complexity?

Page 41: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

Heapsort

Binary heaps give us yet another O(n log n) sorting algorithm. We use a max heap; i.e., one where the highest-priority item is the one with the largest key, and, thus, this item will be at the root of the heap. A max heap is easily implemented by defining the appropriate comparator.

Time complexity?

Page 42: Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:

In-place Sorting

We can use the array to both store the heap H and represent the list L. As items are removed, the heap shrinks toward the beginning of thearray, making room for the elements removed.