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

Post on 24-Dec-2015

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

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

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

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

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

Remove a key from the map

Check if it contains a given key

Key iterator

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

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?

Hash Table

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

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?

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.

Hashing Performance

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

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

Time Complexity

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

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

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

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.

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.

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

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.

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.

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.

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.

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.

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.

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.

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.

Performance

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.

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?

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?

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.

top related