Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.

Slides:



Advertisements
Similar presentations
Hash Tables.
Advertisements

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Skip List & Hashing CSE, POSTECH.
Data Structures Using C++ 2E
Hashing as a Dictionary Implementation
Appendix I Hashing. Chapter Scope Hashing, conceptually Using hashes to solve problems Hash implementations Java Foundations, 3rd Edition, Lewis/DePasquale/Chase21.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Lecture Objectives To learn about hash coding and its use to facilitate efficient search and retrieval To study two forms of hash tables—open addressing.
Hashing21 Hashing II: The leftovers. hashing22 Hash functions Choice of hash function can be important factor in reducing the likelihood of collisions.
Sets and Maps ITEC200 – Week Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.
Fall 2007CS 225 Sets and Maps Chapter 9. Fall 2007CS 225 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Sets and Maps (and Hashing)
Hash Tables1 Part E Hash Tables  
Hashing General idea: Get a large array
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.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Dictionaries 4/17/2017 3:23 PM Hash Tables  
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
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:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Hash Tables1   © 2010 Goodrich, Tamassia.
Comp 335 File Structures Hashing.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
1 5. Abstract Data Structures & Algorithms 5.2 Static Data Structures.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Hashing Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing as a Dictionary Implementation Chapter 19.
CSC 427: Data Structures and Algorithm Analysis
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 Space vs. time  space/time tradeoffs  hashing  hash table, hash function  linear probing.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Lecture Objectives  To learn about hash coding and its use to facilitate efficient search and retrieval  To study two forms of hash tables—open addressing.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Appendix I Hashing.
Sets and Maps Chapter 9.
Sections 10.5 – 10.6 Hashing.
Slides by Steve Armstrong LeTourneau University Longview, TX
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Efficiency add remove find unsorted array O(1) O(n) sorted array
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
Sets and Maps Chapter 9.
Dictionaries 4/5/2019 1:49 AM Hash Tables  
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Sets and Maps Chapter 7 CS 225.
Presentation transcript:

Sets and Maps Chapter 9

Chapter 9: Sets and Maps2 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

Chapter 9: Sets and Maps3 Chapter Objectives (continued) 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

Chapter 9: Sets and Maps4 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

Chapter 9: Sets and Maps5 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

Chapter 9: Sets and Maps6 The Set Interface and Methods 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

Chapter 9: Sets and Maps7 The Set Interface and Methods (continued)

Chapter 9: Sets and Maps8 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

Chapter 9: Sets and Maps9 Maps and the Map Interface (continued)

Chapter 9: Sets and Maps10 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) linear (worst case)

Chapter 9: Sets and Maps11 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

Chapter 9: Sets and Maps12 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

Chapter 9: Sets and Maps13 Java HashCode Method 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 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 ‘a’ x 31 + ‘t’ 31 is a prime number that generates relatively few collisons

Chapter 9: Sets and Maps14 Open Addressing Consider two ways to organize hash tables Open addressing Chaining 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

Chapter 9: Sets and Maps15 Open Addressing (continued)

Chapter 9: Sets and Maps16 Table Wraparound and Search Termination As you increment the table index, your table should wrap around as in a circular array Leads to the potential 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

Chapter 9: Sets and Maps17 Hash Table Considerations You cannot traverse a hash table in a meaningful way as the sequence of stored values is arbitrary When an item is deleted, you cannot just set its table entry to null Store a dummy value instead Deleted items waste storage space and reduce search efficiency Use a prime number for the size of the table so as to reduce collisions A fuller table will result in increased collisions

Chapter 9: Sets and Maps18 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

Chapter 9: Sets and Maps19 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

Chapter 9: Sets and Maps20 Chaining (continued)

Chapter 9: Sets and Maps21 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

Chapter 9: Sets and Maps22 Performance of Hash Tables (continued)

Chapter 9: Sets and Maps23 Implementing a Hash Table

Chapter 9: Sets and Maps24 Implementing a Hash Table (continued)

Chapter 9: Sets and Maps25 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

Chapter 9: Sets and Maps26 Implementing HashSetOpen

Chapter 9: Sets and Maps27 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

Chapter 9: Sets and Maps28 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 in the table below

Chapter 9: Sets and Maps29 Additional Applications of Maps Can implement the phone directory using a map

Chapter 9: Sets and Maps30 Additional Applications of Maps (continued) 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

Chapter 9: Sets and Maps31 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 A collision occurs when two keys map to the same table index In open addressing, linear probing is often used to resolve collisions

Chapter 9: Sets and Maps32 Chapter Review (continued) 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 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)