HashMaps. Overview What are HashMaps? Implementing DictionaryADT with HashMaps HashMaps 2/16.

Slides:



Advertisements
Similar presentations
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Advertisements

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Hashing.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
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.
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.
Using arrays – Example 2: names as keys How do we map strings to integers? One way is to convert each letter to a number, either by mapping them to 0-25.
Hashing Techniques.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Maps, Dictionaries, Hashtables
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.
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.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hash Tables1 Part E Hash Tables  
Sets and Maps (and Hashing)
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Aree Teeraparbseree, Ph.D
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Hashing The Magic Container. Interface Main methods: –Void Put(Object) –Object Get(Object) … returns null if not i –… Remove(Object) Goal: methods are.
1. 2 Problem RT&T is a large phone company, and they want to provide enhanced caller ID capability: –given a phone number, return the caller’s name –phone.
Hashing 1. Def. Hash Table an array in which items are inserted according to a key value (i.e. the key value is used to determine the index of the item).
COSC 2007 Data Structures II
Hashing CS 105. Hashing Slide 2 Hashing - Introduction In a dictionary, if it can be arranged such that the key is also the index to the array that stores.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
1 HASHING Course teacher: Moona Kanwal. 2 Hashing Mathematical concept –To define any number as set of numbers in given interval –To cut down part of.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
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.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
David Luebke 1 11/26/2015 Hash Tables. David Luebke 2 11/26/2015 Hash Tables ● Motivation: Dictionaries ■ Set of key/value pairs ■ We care about search,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Hash Tables CSIT 402 Data Structures II. Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions.
Author: Takdir, S.ST. © Sekolah Tinggi Ilmu Statistik.
Hashing Chapter 7 Section 3. What is hashing? Hashing is using a 1-D array to implement a dictionary o This implementation is called a "hash table" Items.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashing CS 110: Data Structures and Algorithms First Semester,
H ASH TABLES. H ASHING Key indexed arrays had perfect search performance O(1) But required a dense range of index values Otherwise memory is wasted Hashing.
Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Data Structure & Algorithm Lecture 8 – Hashing JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
CISC220 Fall 2009 James Atlas Dec 04: Hashing and Maps K+W Chapter 9.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
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.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
Building Java Programs Generics, hashing reading: 18.1.
DS.H.1 Hashing Chapter 5 Overview The General Idea Hash Functions Separate Chaining Open Addressing Rehashing Extendible Hashing Application Example: Geometric.
Appendix I Hashing.
Sets and Maps Chapter 9.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
TCSS 342, Winter 2006 Lecture Notes
Slides by Steve Armstrong LeTourneau University Longview, TX
Chapter 28 Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Hash Tables Computer Science and Engineering
Hash Tables Computer Science and Engineering
Hash Tables Computer Science and Engineering
Sets and Maps Chapter 9.
Presentation transcript:

HashMaps

Overview What are HashMaps? Implementing DictionaryADT with HashMaps HashMaps 2/16

Hashing What if: We could store everything in our dictionary in an array? We could somehow take any key and turn it into an index into the array in constant time? What would the cost of insert, delete, and find be then? HashMaps p. 3/16

Designing a hashtable There are three main design questions: What hash function to use (the function that turns a key into an array index) What size to make the array What to do if two items index into the same spot (a “collision”) HashMaps p. 4/16

Step 1: what if your key isn’t an integer? Suppose the keys are individual letters, stored as Strings. What happens if the hash function returns A number from 0-25? The ASCII code (or Unicode) of the letter? HashMaps p. 5/16

Choosing a hash function for Strings What if the hash function for a String returns The value of the hash function for the first letter of the String? The sum of the values of the hash functions for all the letters? Java’s solution: For a String of length n: s[0] *31 n-1 + s[1]*31 n-2 + … + s[n-1]*31 0 HashMaps p. 6/16

Step 2: get the right integer Once your key has been converted into an integer, then what? Is it in the right range to index into the array? Easy solution: index = hashCode % arraySize; Harder question: what if two keys hash to the same array index (a collision)? HashMaps p. 7/16

Resolving collisions 1: chaining What is chaining? Suppose we insert the following names into a table of size 26 using chaining (with the hash function h(A)=0, h(B)=1, etc.) Ann, Andrew, Bob, Doug, Elizabeth, Betty, Barbara, Hal, Bill, Mary, Tim, Walter, Xena What does the table look like after the inserts? HashMaps p. 8/16

Resolving collisions: chaining A chained hashtable has an array size of 512. What is the maximum number of entries that can be placed in the table? A. 256 B. 511 C. 512 D. 164 E. There is no maximum. HashMaps p. 9/16

Resolving collisions: linear probing What is linear probing? Suppose we insert the following names into a table of size 26 using linear probing (with the hash function h(A)=0, h(B)=1, etc.) Ann, Andrew, Bob, Doug, Elizabeth, Betty, Barbara, Hal, Bill, Mary, Tim, Walter, Xena What does the hashtable look like after the inserts? HashMaps p. 10/16

Resolving collisions: Quadratic probing What is quadratic probing? Suppose we insert the following names into a table of size 26 using quadratic probing (with the hash function h(A)=0, h(B)=1, etc.) Ann, Andrew, Bob, Doug, Elizabeth, Betty, Barbara, Hal, Bill, Mary, Tim, Walter, Xena What does the hashtable look like after the inserts? HashMaps p. 11/16

Resolving collisions: Double Hashing What is double hashing? What are its pros and cons? HashMaps p. 12/16

Deleting elements from a hashtable How does this work if collisions are handled using chaining? How does this work if collisions are handled using linear probing or another strategy that stores data in the table? HashMaps p. 13/16

Load factor Suppose you place m items in a hashtable with an array size of s. What is the correct formula for the load factor? A. s+m B. s-m C. m-s D. m*s E. m/s HashMaps p. 14/16

Java’s HashMap class What does Java mean by a map? What is Java’s default value for the load factor for the class HashMap? What are the HashMap operations for insert, delete, and find? HashMaps p. 15/16

Coming attractions Next time, we’ll look at a new data structure that allows us to model the Internet, airplane routes, and a variety of other data connected in ways that may be neither linear nor hierarchical: graphs. Homework: read chapter 13 Graphs (or the equivalent in the earlier edition). p. 16/16