Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2007CS 225 Sets and Maps Chapter 7. Fall 2007CS 225 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.

Similar presentations


Presentation on theme: "Fall 2007CS 225 Sets and Maps Chapter 7. Fall 2007CS 225 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn."— Presentation transcript:

1 Fall 2007CS 225 Sets and Maps Chapter 7

2 Fall 2007CS 225 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about hash coding and its use to facilitate efficient search and retrieval To study two forms of hash tables—open addressing and chaining—and to understand their relative benefits and performance tradeoffs

3 Fall 2007CS 225 Chapter Objectives To learn how to implement both hash table forms To be introduced to the implementation of Maps and Sets To see how two earlier applications can be more easily implemented using Map objects for data storage

4 Fall 2007CS 225 The Set Abstraction A set is a collection that contains no duplicate elements Operations on sets include: –Testing for membership –Adding elements –Removing elements –Union –Intersection –Difference –Subset

5 Fall 2007CS 225 Sets and the Set Interface The part of the Collection hierarchy that relates to sets includes three interfaces, two abstract classes, and two actual classes

6 Fall 2007CS 225 Methods of the Set Interface Has required methods for testing set membership, testing for an empty set, determining set size, and creating an iterator over the set Two optional methods for adding an element and removing an element Constructors enforce the no duplicate members criterion Add method does not allow duplicate items to be inserted

7 Fall 2007CS 225 The Set Interface (Set ) cardinalityint size() set intersection, optionalboolean retainAll(Collection ) set difference, optionalboolean removeAll(Collection ) optionalboolean remove( Object) Iterator iterator() boolean isEmpty() subset testboolean containsAll(Collection ) set membershipboolean contains( E) set union, optionalboolean addAll( Collection ) false if o already present, optionalboolean add( E o) BehaviorMethod

8 Fall 2007CS 225 Maps and the Map Interface The Map is related to the Set Mathematically, a Map is a set of ordered pairs whose elements are known as the key and the value Keys are required to be unique, but values are not necessarily unique You can think of each key as a “mapping” to a particular value A map can be used to enable efficient storage and retrieval of information in a table

9 Fall 2007CS 225 The Map Interface (Map ) int size() returns value associated with key or null V remove( Object key) returns previous value or null if not present V put( K key, V value) boolean isEmpty() returns object with given key or null if not present V get( Object key) BehaviorMethod

10 Fall 2007CS 225 Hash Tables The goal behind the hash table is to be able to access an entry based on its key value, not its location We want to be able to access an element directly through its key value, rather than having to determine its location first by searching for the key value in the array Using a hash table enables us to retrieve an item in constant time (on average) with a linear (worst case) behavior

11 Fall 2007CS 225 Hash Codes and Index Calculation The basis of hashing is to transform the item’s key value into an integer value which will then be transformed into a table index

12 Fall 2007CS 225 Example Getting letter frequencies to generate Huffman codes For text-only documents, use the ascii code of a character as its key value table size of 128 isn't too big If you need the unicode value, the table would get too large. (65,536) Not all characters will occur Use smaller table and use mod operation to compute the index from the unicode value

13 Fall 2007CS 225 Methods for Generating Hash Codes In most applications, the keys will consist of strings of letters or digits rather than a single character The number of possible key values is much larger than the table size Generating good hash codes is somewhat of an experimental process Besides a random distribution of its values, the hash function should be relatively simple and efficient to compute

14 Fall 2007CS 225 Hash Codes for Strings For strings, simply summing the int values of all characters will return the same hash code for sign and sing The Java API algorithm accounts for position of the characters as well

15 Fall 2007CS 225 String hashCode Method The String.hashCode() returns the integer calculated by the formula: s 0 x 31 (n-1) + s 1 x 31 (n-2) + … + s n-1 where s i is the ith character of the string, and n is the length of the string “Cat” will have a hash code of:‘C’ x 31 2 + ‘a’ x 31 + ‘t’ 31 is a prime number that generates relatively few collisons

16 Fall 2007CS 225 Handling Collisions A collision happens when two distinct entries in the hash table have the same hash value Consider two ways to handle collisions –Open addressing each element of the hash table has a single entry –Chaining each element of the hash table points to a list (possibly empty) of entries

17 Fall 2007CS 225 Open Addressing Linear probing can be used to access an item in a hash table –If that element contains an item with a different key, increment the index by one –Keep incrementing until you find the key or a null entry

18 Fall 2007CS 225 Algorithm for Open Addressing Compute index using hashCode % table.length if table[index] is null add item at position index else if table[index] equals item being added item is already in the table else while table[index] not null increment index add item at new value of index

19 Fall 2007CS 225 Search Termination As you increment the table index, your table should wrap around as in a circular array –This leads to the possibility of an infinite loop How do you know when to stop searching if the table is full and you have not found the correct value? –Stop when the index value for the next probe is the same as the hash code value for the object –Ensure that the table is never full by increasing its size after an insertion if its occupancy rate exceeds a specified threshold

20 Fall 2007CS 225 Name hashCode () hashCode% 5 hashCode()% 11 "Tom " 8427443 "Dick " 212986945 "Harr y" 69496488310 "Sam " 8278945 "Pete"248403837

21 Fall 2007CS 225 Traversing a hash table You can visit all elements in a hash table by getting each element of the array in turn The sequence of values that results is arbitrary For previous example size = 5 "Dick", "Sam", "Pete", "Harry", "Tom" size = 11 "Tom", "Dick", "Sam", "Pete", "Harry"

22 Fall 2007CS 225 Deleting from a Hash Table When an item is deleted, you cannot just set its table entry to null –Any element with the same hash code that was inserted into the table later will not be found –Store a dummy value instead –Deleted items waste storage space and reduce search efficiency

23 Fall 2007CS 225 Hash Table Considerations Using a prime number for the size of the table tends to reduce collisions Load factor is ratio of filled elements to total elements load factor = actual elements / size A larger load factor will result in increased collisions Don't allow the table get too full Specify a maximum load factor If table gets too full, rehash Allocate a new table with twice the capacits Get each element from the old table, compute its index in new table and insert Don't insert deleted items

24 Fall 2007CS 225 Clustering Example Consider a hash table of size 11 Add elements with hash codes 5, 6, 5, 6, 7 in that order 5(1) 6(1) 5(2) 6(2) 7(1)

25 Fall 2007CS 225 Reducing Collisions Using Quadratic Probing Linear probing tends to form clusters of keys in the table, causing longer search chains Quadratic probing can reduce the effect of clustering –Increments form a quadratic series –Disadvantage is that the next index calculation is time consuming as it involves multiplication, addition, and modulo division –Not all table elements are examined when looking for an insertion index

26 Fall 2007CS 225 Clustering Example Consider a hash table of size 11 Add elements with hash codes 5, 6, 5, 6, 7 in that order using quadratic probing There is less overlap in the search chains 5 -> 6 -> 9 -> 14 6 -> 7 -> 10 -> 15 5(1) 6(1) 6(2) 7(1) 5(2)

27 Fall 2007CS 225 Hashing with Chaining Chaining is an alternative to open addressing Each table element references a linked list that contains all of the items that hash to the same table index –The linked list is often called a bucket –The approach sometimes called bucket hashing Only items that have the same value for their hash codes will be examined when looking for an object

28 Fall 2007CS 225 Chaining Illustrated

29 Fall 2007CS 225 Performance of Hash Tables Load factor is the number of filled cells divided by the table size Load factor has the greatest effect on hash table performance The lower the load factor, the better the performance as there is a lesser chance of collision when a table is sparsely populated

30 Fall 2007CS 225 Performance of Hash Tables

31 Fall 2007CS 225 Implementing a Hash Table Use interface KWHashMap and implement the interface using both open hashing and chaining. MethodBehavior V get( K key)returns value associated with key or null boolean isEmpty() V put( K key, V value) Inserts or replaces object associated with key V remove( K key) Removes mapping for key; returns value or null int size()

32 Fall 2007CS 225 Entry class Create an inner class to store key-value pairs Data fields private Object key private Object value Constructor public Entry( Object key, Object value) Methods public Object getKey() public Object getValue(); public Object setValue();

33 Fall 2007CS 225 Open Hash Table Data Fields private Entry table[] private static final int START_CAPACITY private double LOAD_THRESHOLD private int numKeys private int numDeletes private static final Entry DELETED Private Methods private int find( Object key) private void rehash()

34 Fall 2007CS 225 find Algorithm for Open Hash Table 1.set index to key.hashCode() % table.length 2.if index < 0 add table.length 3.while table[index] not empty and != key 4. increment index 5. if index >= table.length 6. index = 0 7.return index

35 Fall 2007CS 225 get Algorithm for Open Hash Table 1.find table element for key 2.if table element contains key 3. return value at this element 4.else 5. return null

36 Fall 2007CS 225 put Algorithm for Open Hash Table 1.find table element for key 2.if table element is empty 3. insert new item 4. increment numKeys 5. rehash if needed 6. return null 7.else 8. save old value of element 9. replace value with new value 10. return old value

37 Fall 2007CS 225 Removing from Open Hash Table 1.find table element for key 2.if element is empty 3. return null 4.else 5. save value of element 6. replace element with DELETED 7. increment numDeletes 8. decrement numKeys 9. return saved value

38 Fall 2007CS 225 Rehashing 1.Alocate a new table with twice as many elements 2.set numKeys to 0 3.set numDeletes to 0 4.add each undeleted element of original table to new table

39 Fall 2007CS 225 Chained Hash Table Data Fields private LinkedList table[] private static final int CAPACITY private static final int LOAD_THRESHOLD Rehash when numKeys=3*CAPACITY

40 Fall 2007CS 225 get Algorithm for Chained Hash Table 1.set index to key.hashCode % table.length 2.if index < 0 3. add table.length 4.if table[index] is empty 5. return null 6.for each element in list at table[index] 7. if element key = search key 8. return element's value 9.return null (key not found)

41 Fall 2007CS 225 put Algorithm for Chained Hash Table 1.set index to key.hashCode % table.length 2.if index < 0 add table.length 3.if table[index] is empty 4. create a new linked list at table[index] 5.Search list at table[index] for key 6.if successful 7. replace value and return old value 8.else 9. insert new key-value pair into list 10. increment numKeys 11. rehash if needed 12. return null

42 Fall 2007CS 225 Removing from Chained Hash Table 1.set index to key.hashCode % table.length 2.if index < 0 add table.length 3.if table[index] is empty 4. return null 5.Search list at table[index] for key 6.if successful 7. remove value and decrement numKeys 8. if list at table[index] is empty 9. table[index] -> null 10. return value associated with key 11.return null

43 Fall 2007CS 225 Implementation Considerations for Maps and Sets Class Object implements methods hashCode and equals, so every class can access these methods unless it overrides them –Object.equals compares two objects based on their addresses, not their contents –Object.hashCode calculates an object’s hash code based on its address, not its contents Java recommends that if you override the equals method, then you should also override the hashCode method

44 Fall 2007CS 225 Implementing HashSetOpen HashSetOpen is similar to a hash table Methods have slightly different signatures HashSetOpen is an adapter class with an internal HashTableOpen Map MethodSet Method Object get( Object key)boolean contains( Object key) Object put(Object key, Object value)boolean add( Object key) Object remove( Object key)boolean remove( Object key)

45 Fall 2007CS 225 Implementing the Java Map and Set Interfaces The Java API uses a hash table to implement both the Map and Set interfaces The task of implementing the two interfaces is simplified by the inclusion of abstract classes AbstractMap and AbstractSet in the Collection hierarchy

46 Fall 2007CS 225 Nested Interface Map.Entry One requirement on the key-value pairs for a Map object is that they implement the interface Map.Entry, which is an inner interface of interface Map –An implementer of the Map interface must contain an inner class that provides code for the methods Object getKey() Object getValue() Object setValue( Object value)

47 Fall 2007CS 225 Additional Applications of Maps Can implement the phone directory using a map PhoneDirectory Interface Map Interface addOrChangeEntryput lookUpEntryget removeEntryremove loadDatanone savenone

48 Fall 2007CS 225 Additional Applications of Maps Huffman Coding Problem –Use a map for creating an array of elements and replacing each input character by its bit string code in the output file –Frequency table The key will be the input character The value is the character code string

49 Fall 2007CS 225 Chapter Review The Set interface describes an abstract data type that supports the same operations as a mathematical set The Map interface describes an abstract data type that enables a user to access information corresponding to a specified key A hash table uses hashing to transform an item’s key into a table index so that insertions, retrievals, and deletions can be performed in expected O(1) time

50 Fall 2007CS 225 Chapter Review A collision occurs when two keys map to the same table index In open addressing, linear probing is often used to resolve collisions The best way to avoid collisions is to keep the table load factor relatively low by rehashing when the load factor reaches a value such as 0.75

51 Fall 2007CS 225 Chapter Review In open addressing, you can’t remove an element from the table when you delete it, but you must mark it as deleted A set view of a hash table can be obtained through method entrySet Two Java API implementations of the Map (Set) interface are HashMap (HashSet) and TreeMap (TreeSet)


Download ppt "Fall 2007CS 225 Sets and Maps Chapter 7. Fall 2007CS 225 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn."

Similar presentations


Ads by Google