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

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Addition Facts
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
© 2004 Goodrich, Tamassia Hash Tables
1 Hash Tables Saurav Karmakar. 2 Motivation What are the dictionary operations? What are the dictionary operations? (1) Insert (1) Insert (2) Delete (2)
CSCI 2720 Hashing   Spring 2005.
Hash Table.
Briana B. Morrison Adapted from William Collins
Preliminaries Advantages –Hash tables can insert(), remove(), and find() with complexity close to O(1). –Relatively easy to program Disadvantages –There.
Hash Tables CS 310 – Professor Roch Weiss Chapter 20 All figures marked with a chapter and section number are copyrighted © 2006 by Pearson Addison-Wesley.
1 Tables & File © Dave Bockus. 2 Binary Search Recursive int Bsearch(TableType T, KeyType key, int lt, int rt) { int mid; mid = (lt+rt)/2; if (lt>rt)
1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Hash Tables.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Hashing.
Backup Slides. An Example of Hash Function Implementation struct MyStruct { string str; string item; };
Addition 1’s to 20.
25 seconds left…...
Week 1.
An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
© 2004 Goodrich, Tamassia Hash Tables1  
Log Files. O(n) Data Structure Exercises 16.1.
Implementation of Linear Probing (continued) Helping method for locating index: private int findIndex(long key) // return -1 if the item with key 'key'
Maps, Dictionaries, Hashtables
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.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hashing Text Read Weiss, §5.1 – 5.5 Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
Hash Tables1 Part E Hash Tables  
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
Hashing General idea: Get a large array
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.
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.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
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).
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
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.
© 2004 Goodrich, Tamassia Hash Tables1  
CS201: Data Structures and Discrete Mathematics I Hash Table.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
LECTURE 35: COLLISIONS CSC 212 – Data Structures.
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.
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.
CS261 Data Structures Hash Tables Open Address Hashing.
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 ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate chaining.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Fundamental Structures of Computer Science II
Hashing CSE 2011 Winter July 2018.
Hash Tables (Chapter 13) Part 2.
Data Structures and Algorithms
Chapter 21 Hashing: Implementing Dictionaries and Sets
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.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Collision Handling Collisions occur when different elements are mapped to the same cell.
CSE 373: Data Structures and Algorithms
Presentation transcript:

1 Designing Hash Tables Sections 5.3, 5.4, 5.5

2 Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table –E.g. Index = hash(key) % table_size; 2.Resolve conflicts: –Need to handle case where multiple keys mapped to the same index. –Two representative solutions Chaining with separate lists Probing open addressing

3 Separate Chaining Each table entry stores a list of items Multiple keys mapped to the same entry maintained by the list Example –Hash(k) = k mod 10 –(10 is not a prime, just for illustration)

4 Separate Chaining Implementation Type Declaration for Separate Chaining Hash Table

5 HashedObj Needs to provide –Hash function Provided for string and int (the two non-member functions) –Equality operators ( operator== or operator!= )

6 An example class for HashedObj

7 Chaining

8 Chaining (contd.)

9

10 Analysis of Chaining Consider an array of size M with N records –Worst case insert without uniqueness check = O(1) Find location to insert and push_back/front –Worst case remove/find/unique insert = O(N) –Expected case unique insert/find/remove 1 + O(N/M) –Let us resize the table is N/M exceeds some constant –Expected time = 1 + O( ) = O(1)

11 Hash Tables Without Chaining Try to avoid buckets with separate lists How use Probing Hash Tables –If collision occurs, try another cell in the hash table. –More formally, try cells h 0 (x), h 1 (x), h 2 (x), h 3 (x)… in succession until a free cell is found. h i (x) = hash(x) + f(i) And f(0) = 0

12 f(i)=i Insert (assume no duplicated keys) 1.Index = hash(key) % table_size; 2.If table[index] is empty, put information (key and others) in entry table[index]. 3.If table[index] is not empty then Index ++; index = index % table_size; goto 2. Search (key) 1.Index = hash(key) % table_size; 2.If (table[index] is empty) return –1 (not found). 3.Else if (table[index].key == key) return index; 4.Index ++; index = index % table_size; goto 2. Linear Probing

13 Example Insert 89, 18, 49, 58, 69 (hash(k) = k mod 10)

14 Linear probing Delete –Can be tricky, must maintain the consistency of the hash table. –What is the simplest deletion strategy you can think of??

15 Quadratic Probing f(i) = i 2

16 Probing strategy hash table

17 Double Hashing f(i) = i*hash 2 (x) E.g. hash 2 (x) = 7 – (x % 7) What if hash 2 (x) = 0 for some x?

18 Analysis of Hash Table Without Chaining Expected case analysis of insertion into a table of size M containing n records – i=1 i probability of trying i buckets –= 1 (M-n)/M + 2(n/M)(M-n)/M + 3(n/M) 2 (M-n)/M +... Let = n/M –Time = 1*(1- ) + 2 (1- ) (1- ) +... –= –= = 1/(1- ) Assume < 1 –Keep bounded by some constant < 1

19 Rehashing Hash Table may get full –No more insertions possible Hash table may get too full –Insertions, deletions, search take longer time Solution: Rehash –Build another table that is twice as big and has a new hash function –Move all elements from smaller table to bigger table Cost of Rehashing = O(N) –But happens only when table is close to full –Close to full = table is X percent full, where X is a tunable parameter

20 Rehashing Example After Rehashing Original Hash Table After Inserting 23

21 Rehashing Implementation

22 Rehashing implementation