Sets and Maps ITEC200 – Week 9. www.ics.mq.edu.au/ppdp 2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.

Slides:



Advertisements
Similar presentations
ITEC200 Week04 Lists and the Collection Interface.
Advertisements

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.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys. Operations.  get(theKey)  put(theKey, theElement)  remove(theKey) 5/2/20151.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Hashing21 Hashing II: The leftovers. hashing22 Hash functions Choice of hash function can be important factor in reducing the likelihood of collisions.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Hashing Techniques.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
ITEC200 Week06 Queues. 2 Learning Objectives – Week06 Queues (Ch6) Students can Manage data using the queue Abstract Data Type.
ITEC200 Week05 Stacks. 2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface.
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.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 48 Hashing.
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 Tables1 Part E Hash Tables  
Sets and Maps (and Hashing)
Hash Tables1 Part E Hash Tables  
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.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
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:
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.
Appendix E-A Hashing Modified. Chapter Scope Concept of hashing Hashing functions Collision handling – Open addressing – Buckets – Chaining Deletions.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
1 5. Abstract Data Structures & Algorithms 5.2 Static Data Structures.
Hashing Hashing is another method for sorting and searching data.
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
Hashing as a Dictionary Implementation Chapter 19.
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.
Hashing 8 April Example Consider a situation where we want to make a list of records for students currently doing the BSU CS degree, with each.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
CS261 Data Structures Hash Tables Open Address Hashing.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
CISC220 Fall 2009 James Atlas Dec 04: Hashing and Maps K+W Chapter 9.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
Appendix I Hashing.
Sets and Maps Chapter 9.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Data Abstraction & Problem Solving with C++
School of Computer Science and Engineering
Slides by Steve Armstrong LeTourneau University Longview, TX
Hash Table.
Chapter 28 Hashing.
Chapter 10 Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Hashing Alexandra Stefan.
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
Hash Tables Computer Science and Engineering
Advanced Implementation of Tables
Hash Tables Computer Science and Engineering
Sets and Maps Chapter 9.
Dictionaries 4/5/2019 1:49 AM Hash Tables  
Collision Resolution.
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Sets and Maps Chapter 7 CS 225.
Presentation transcript:

Sets and Maps ITEC200 – Week 9

2 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 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

4 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

5 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

6 Maps 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

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

8 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

9 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 Java has a ready-made hash function for us to use.

10 How organise hash table Consider two ways to organize hash tables –Open addressing –Chaining

11 Open Addressing (continued) 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

12 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

13 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

14 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

15 Chaining (continued)

16 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

17 Performance of Hash Tables (continued)

18 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

19 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

20 Additional Applications of Maps Can implement the phone directory using a map

21 Where to from here… Work through Chapter 9 of the Koffman & Wolfgang Text Conceptual Questions and Practical Exercises Submit all preliminary work Be prompt for your online class

22 Acknowledgements These slides were based upon the Objects, Abstraction, Data Structures and Design using Java Version 5.0 Chapter 9 PowerPoint presentation by Elliot B. Koffman and Paul A. T. Wolfgang