Download presentation

Presentation is loading. Please wait.

Published byColin Simpson Modified over 4 years ago

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: Words Their definitions. Social security numbers People ʼ s names IP addresses Host names

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

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

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

5
Put a key into the map Need to check if the key exists. We can use a linear scan.

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

7
Remove a key from the map

8
Check if it contains a given key

9
Key iterator

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

15
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?

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

17
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?

18
Java HashCode Every 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.

19
Hashing Performance

20
Hash Maps The 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().

21
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

23
Time Complexity

24
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

25
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

26
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

27
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.

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

29
A binary heap is a complete binary tree whose entries satisfy the “Heap-order property”: No child has a key that is smaller than its parent's key. Implementation: 1)Using reference-based tree data structure 2)Using an array

30
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.

31
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.

32
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.

33
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.

34
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.

35
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.

36
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.

37
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.

38
Performance

39
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.

40
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?

41
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?

42
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 the array, making room for the elements removed.

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google