1 / 42

Maps

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  T heir definitions. • Social security numbers  Peopleʼs names • IP addresses  H ost names.

rletty
Download Presentation

Maps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 Key Value 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.

  11. 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?

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

  13. Hash Table • Take the hash code modulo the array size to get an index into an array. • Store the key and the value in a linked list of entries at the array index. Internal data structure? Collision: Two different keys may end up with the same hash code, or two different hash codes result in the same array index.

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

  15. Hashing Performance

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

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

  18. Time Complexity

  19. 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). Priorities in Highest Priority out • Applications • Event simulations • Packet delivery • Printer queue

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

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

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

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

  24. 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: • Using reference-based tree data structure • Using an array

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

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

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

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

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

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

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

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

  33. Performance

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

  35. void heapify() Make a complete tree out of the entries by just throwing them, in any order, into an array. 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?

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

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

More Related