Hash Tables Chapter 12.7 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time Nyhoff, ADTs, Data Structures and.

Slides:



Advertisements
Similar presentations
Searching: Binary Trees and Hash Tables
Advertisements

Hash Tables.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Hashing Techniques.
Dictionaries and Their Implementations
1 Hash Tables Gordon College CS Hash Tables Recall order of magnitude of searches –Linear search O(n) –Binary search O(log 2 n) –Balanced binary.
© 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.
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of 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.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Hashing Dr. Yingwu Zhu.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Comp 335 File Structures Hashing.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Hashing by Rafael Jaffarove CS157b. Motivation  Fast data access  Search  Insertion  Deletion  Ideal seek time is O(1)
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Sections 10.5 – 10.6 Hashing.
CE 221 Data Structures and Algorithms
Hashing (part 2) CSE 2011 Winter March 2018.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing.
CSCI 210 Data Structures and Algorithms
Hashing CSE 2011 Winter July 2018.
Data Abstraction & Problem Solving with C++
Slides by Steve Armstrong LeTourneau University Longview, TX
Hashing Alexandra Stefan.
Hashing Alexandra Stefan.
Hash functions Open addressing
Hash Table.
Chapter 28 Hashing.
Hash In-Class Quiz.
Hash Tables.
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
Chapter 21 Hashing: Implementing Dictionaries and Sets
Dictionaries and Their Implementations
Resolving collisions: Open addressing
Data Structures and Algorithms
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Hash Tables and Associative Containers
Hashing Alexandra Stefan.
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
Hash Tables Computer Science and Engineering
A Hash Table with Chaining
Advanced Implementation of Tables
Searching: Hash Tables
Advanced Implementation of Tables
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
EE 312 Software Design and Implementation I
Adapted from instructor resource slides
Ch Hash Tables Array or linked list Binary search trees
Ch. 13 Hash Tables  .
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EE 312 Software Design and Implementation I
Instructor: Dr. Michael Geiger Spring 2017 Lecture 33: Hash tables
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
CSE 373: Data Structures and Algorithms
Presentation transcript:

Hash Tables Chapter 12.7 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Hash Tables Recall order of magnitude of searches Linear search O(n) Binary search O(log2n) Balanced binary tree search O(log2n) Unbalanced binary tree can degrade to O(n) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Hash Tables In some situations faster search is needed Solution is to use a hash function Value of key field given to hash function Location in a hash table is calculated Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Hash Functions Simple function could be to mod the value of the key by some arbitrary integer int h(int i) { return i % someInt; } Note the max number of locations in the table will be same as someInt Note that we have traded speed for wasted space Table must be considerably larger than number of items anticipated Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Hash Functions Observe the problem with same value returned by h(i) for different values of i Called collisions A simple solution is linear probing Linear search begins at collision location Continues until empty slot found for insertion Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Hash Functions When retrieving a value linear probe until found If empty slot encountered then value is not in table If deletions permitted Slot can be marked so it will not be empty and cause an invalid linear probe Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Hash Functions Strategies for improved performance Hash table capacity Increase table capacity (less collisions) Use different collision resolution technique Devise different hash function Hash table capacity Size of table must be 1.5 to 2 times the size of the number of items to be stored Otherwise probability of collisions is too high Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Collision Strategies Linear probing can result in primary clustering Consider quadratic probing Probe sequence from location i is i + 1, i – 1, i + 4, i – 4, i + 9, i – 9, … Secondary clusters can still form Double hashing Use a second hash function to determine probe sequence Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Collision Strategies Chaining Table is a list or vector of head nodes to linked lists When item hashes to location, it is added to that linked list Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Improving the Hash Function Ideal hash function Simple to evaluate Scatters items uniformly throughout table Modulo arithmetic not so good for strings Possible to manipulate numeric (ASCII) value of first and last characters of a name Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3