Instructor: Dr. Michael Geiger Spring 2017 Lecture 33: Hash tables

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,
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.
CSE 250: Data Structures Week 12 March 31 – April 4, 2008.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
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.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
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.
Hash Table March COP 3502, UCF.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
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.
1 Hash table. 2 A basic problem We have to store some records and perform the following:  add new record  delete record  search a record by key Find.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
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 as a Dictionary Implementation Chapter 19.
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
CSE 373 Data Structures and Algorithms Lecture 17: Hashing II.
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? –
Instructor: Lilian de Greef Quarter: Summer 2017
Hashing (part 2) CSE 2011 Winter March 2018.
Hashing.
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
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
Dictionaries and Their Implementations
Introduction to Algorithms 6.046J/18.401J
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 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.
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
Introduction to Algorithms
Hash Tables Computer Science and Engineering
A Hash Table with Chaining
Advanced Implementation of Tables
Searching: Hash Tables
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Adapted from instructor resource slides
Ch Hash Tables Array or linked list Binary search trees
Heaps and priority queues
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture-Hashing.
CSE 373: Data Structures and Algorithms
Presentation transcript:

Instructor: Dr. Michael Geiger Spring 2017 Lecture 33: Hash tables EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017 Lecture 33: Hash tables

Data Structures: Lecture 33 Lecture outline Announcements/reminders Program 5 due 4/26 Today’s lecture Hash tables 7/26/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 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) 7/26/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 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 7/26/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 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 7/26/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 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 7/26/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 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 7/26/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Hash Functions Strategies for improved performance 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 7/26/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 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 7/26/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 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 7/26/2019 Data Structures: Lecture 33

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 7/26/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Final notes Next time: TBD—either Inheritance, or Balanced binary search trees Reminders: Program 5 due 4/26 7/26/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Acknowledgements These slides are adapted from slides provided with the course textbook: Larry Nyhoff, ADTs, Data Structures, and Problem Solving with C++, 2nd edition, 2005, Pearson/Prentice Hall. ISBN: 0-13-140909-3 7/26/2019 Data Structures: Lecture 33