Hashing as a Dictionary Implementation

Slides:



Advertisements
Similar presentations
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Advertisements

Hashing as a Dictionary Implementation
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.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 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.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
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.
Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Chapter 5: Hashing Collision Resolution: Separate Chaining Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Hashing as a Dictionary Implementation Chapter 19.
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.
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.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
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.
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
Sets and Maps Chapter 9.
Sections 10.5 – 10.6 Hashing.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
TCSS 342, Winter 2006 Lecture Notes
Hashing CSE 2011 Winter July 2018.
Data Abstraction & Problem Solving with C++
Slides by Steve Armstrong LeTourneau University Longview, TX
Hash Tables (Chapter 13) Part 2.
An Introduction to Sorting
A Bag Implementation that Links Data
Efficiency add remove find unsorted array O(1) O(n) sorted array
Chapter 28 Hashing.
CSE 2331/5331 Topic 8: Hash Tables CSE 2331/5331.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 10 Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Dictionaries and Their Implementations
Collision Resolution Neil Tang 02/18/2010
Double hashing Removal (open addressing) Chaining
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
CSE 373, Copyright S. Tanimoto, 2002 Hashing -
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
Hash Tables Computer Science and Engineering
Advanced Implementation of Tables
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Sets and Maps Chapter 9.
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by: Search the hash table in some.
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by: Search the hash table in some.
Collision Resolution Neil Tang 02/21/2008
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by: Search the hash table in some.
Ch Hash Tables Array or linked list Binary search trees
Hashing in java.util
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Ch. 13 Hash Tables  .
Podcast Ch21f Title: HashSet Class
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations that Use Arrays
A List Implementation that Links Data
Presentation transcript:

Hashing as a Dictionary Implementation Chapter 22 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents The Efficiency of Hashing The Load Factor The Cost of Open Addressing The Cost of Separate Chaining Rehashing Comparing Schemes for Collision Resolution Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents A Dictionary Implementation That Uses Hashing Entries in the Hash Table Data Fields and Constructors The Methods getValue, remove, and add Iterators Java Class Library: The Class HashMap Jave Class Library: The Class HashSet Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Describe relative efficiencies of various collision resolution techniques Describe hash table’s load factor Describe rehashing and why necessary Use hashing to implement ADT dictionary Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Efficiency of Hashing Observations Successful retrieval or removal has same efficiency as successful search Unsuccessful retrieval or removal has same efficiency as unsuccessful search A successful addition has same efficiency as unsuccessful search An unsuccessful addition has same efficiency as successful search Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Efficiency of Hashing Load factor Minimum load factor = 0 When dictionary is empty It is never negative Maximum load factor Depends on type of collision resolution used Cannot exceed 1   Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Open Addressing   Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 22-1 The average number of comparisons required by a search of the hash table for given values of the load factor λ when using linear probing Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Open Addressing   Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 22-2 The average number of comparisons required by a search of the hash table for given values of the load factor λ when using either quadratic probing or double hashing Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Separate Chaining     Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 22-3 The average number of comparisons required by a search of the hash table for given values of the load factor λ when using separate chaining Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Rehashing When load factor gets too high Resize array to a prime number at least twice former size Must rehash to different locations c % n, based on new size, n, of array Note – this is more work than simply increasing the size of an array Not a task to be done often Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Comparing Schemes Figure 22-4 The average number of comparisons required by a search of the hash table versus the load factor λ for four collision resolution techniques when the search is (a) successful; (b) unsuccessful Copyright ©2012 by Pearson Education, Inc. All rights reserved

Dictionary Implementation That Uses Hashing We implement linear probing Other open addressing strategies involve few changes Note source code of class HashedDirectory, Listing 22-1 Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved

Dictionary Implementation That Uses Hashing Figure 22-5 A hash table and one of its entry objects Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 22-6 A hash table containing dictionary entries, removed entries, and null values Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Class Library: The Class HashMap Standard package java.util contains the class HashMap<K, V> Table is a collection of buckets Constructors public HashMap() public HashMap(int initialSize) public HashMap(int initialSize, float maxLoadFactor) public HashMap(Map<? extends K,? extends V> table) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Class Library: The Class HashMap   Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Class Library: The Class HashSet Implements the interface java.util.Set of Chapter 1 Uses an instance of the class HashMap to contain entries in a set Constructors public HashSet() public HashSet(int initialCapacity) public HashSet(int initialCapacity, float loadFactor) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved End Chapter 22 Copyright ©2012 by Pearson Education, Inc. All rights reserved