1 Hashing. 2 Introduction - Hashing zUse an array to store key values. zBut instead of storing them in a position relative to other key values (i.e. in.

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

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.
Hash Tables.
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.
CSCE 3400 Data Structures & Algorithm Analysis
Extendible Hashing For Use as a File Structure 1.
Searching: Self Organizing Structures and Hashing
Hashing Part Two Better Collision Resolution Small parts of this material stolen from "File Organization and Access" by Austing and Cassel.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
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.
Log Files. O(n) Data Structure Exercises 16.1.
Hashing Techniques.
1 Hashing (Walls & Mirrors - end of Chapter 12). 2 I hate quotations. Tell me what you know. – Ralph Waldo Emerson.
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.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
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.
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
External Sorting and Searching
IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms.
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.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 4 Search Algorithms.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Search  We’ve got all the students here at this university and we want to find information about one of the students.  How do we do it?  Linked List?
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.
Searching Given distinct keys k 1, k 2, …, k n and a collection of n records of the form »(k 1,I 1 ), (k 2,I 2 ), …, (k n, I n ) Search Problem - For key.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
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,
LECTURE 35: COLLISIONS CSC 212 – Data Structures.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
Hashing Basis Ideas A data structure that allows insertion, deletion and search in O(1) in average. A data structure that allows insertion, deletion and.
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.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
CS261 Data Structures Hash Tables Open Address Hashing.
Introduction to Algorithms 6.046J/18.401J LECTURE7 Hashing I Direct-access tables Resolving collisions by chaining Choosing hash functions Open addressing.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
1 Chapter 9 Searching And Table. 2 OBJECTIVE Introduces: Basic searching concept Type of searching Hash function Collision problems.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate chaining.
Hashing. Search Given: Distinct keys k 1, k 2, …, k n and collection T of n records of the form (k 1, I 1 ), (k 2, I 2 ), …, (k n, I n ) where I j is.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Fundamental Structures of Computer Science II
Hashing CSE 2011 Winter July 2018.
Hashing - resolving collisions
Design and Analysis of Algorithms
Resolving collisions: Open addressing
Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
A Hash Table with Chaining
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
CSE 373: Data Structures and Algorithms
Presentation transcript:

1 Hashing

2 Introduction - Hashing zUse an array to store key values. zBut instead of storing them in a position relative to other key values (i.e. in ascending order), store them based on a mathematical calculation. zThis mathematical calculation is called a “hash function”.

3 Discussion zAdvantage: yFastest search times - O(1) in the best case yInserts, deletes also fast zDisadvantages: yKey values not available in order. yEliminates the option of range queries. yWhat if two key values hash to the same place?

4 Basic Idea zA hash algorithm has two parts: yThe hash function yThe collision resolution strategy zSimple example: yA hash function for integers might be H(x) = x mod n where “n” is the number of elements in the array, or “buckets” yTo resolve collisions, simply search linearly for the next open bucket. (linear probing)

5 Example zFirst, hash 17: Keys to hash: 17, 23, 89, 103, 44 H(x) = x mod

6 Example z17 hashes to bucket 7...no problem. zNow hash 23: Keys to hash: 23, 89, 103, 44 H(x) = x mod

7 Example z23 hashes to bucket 3...no problem. zNow hash 89: Keys to hash: 89, 103, 44 H(x) = x mod

8 Example z89 hashes to bucket 9...no problem. zNow hash 103: Keys to hash: 103, 44 H(x) = x mod

9 Example z103 hashes to bucket 3; since 23 is already there, place 103 in bucket 4. zNow hash 44: Keys to hash: 44 H(x) = x mod

10 Example z44 hashes to bucket 4, which is full, so put 44 in bucket 5. Keys to hash: done. H(x) = x mod

11 Example zNote that bucket 4 contains a key value which does not hash to 4, but there is one that does. zThis makes delete tricky... Keys to hash: done. H(x) = x mod

12 Searching a Hash Table zHash the key value to search; zIf it is in that bucket, stop and report success. zIf not, search linearly until yIt is found (success) yAn empty bucket is encountered.

13 Deleting from a Hash Table zTo delete, first hash the key value to delete to find its bucket. zIf it is there, skip the next step zSearch linearly in the table until either yyou find it (success - continue) yyou reach an open bucket (failure - stop) zRemove the key value from its bucket

14 Deleting II zBut you might have previously hashed keys that collided with this one, so... zFor every key value from the current bucket to the next open bucket: yRehash the key value and move it if necessary.

15 Delete Example zDelete 17: Keys to delete: 17, 23, H(x) = x mod 10

16 Delete Example z17 hashes to bucket 7. It is there, so remove it; 8 is empty, so we’re done. zDelete 23: Keys to delete: 23, H(x) = x mod 10

17 Delete Example z23 hashes to bucket 3; it is there, so remove it. zNow rehash from bucket 4 to 5 (6 is empty) Keys to delete: H(x) = x mod 10

18 Delete Example zWhat do I do with 103? Keys to delete: H(x) = x mod 10

19 Delete Example zMove it to bucket 3. zWhat about 44? Keys to delete: H(x) = x mod 10

20 Delete Example zMove it to bucket 4. zDone with delete of 23. zDelete 103: Keys to delete: H(x) = x mod 10

21 Delete Example z103 hashes to bucket 3; it is there, so remove it. zWhat about rehashes? Keys to delete: done H(x) = x mod 10

22 Delete Example zI must rehash from 4 to 4 as bucket 5 is the next open bucket. z44 hashes to bucket 4, so no moves are necessary. Keys to delete: done H(x) = x mod 10

Picking a Good Hash Function zSuppose I wish to hash the values to the right. zSuppose further my table size is 16. zI could try H(X) = X mod

Picking a Good Hash Function II zThis doesn’t work out so well. zWhat else can I do? zTry looking at the bits: 24 Number Mod

Picking a Good Hash Function III 25 NumberMod

Picking a Good Hash Function IV 26 Num ber Mod bit*8 + 8bit*4 + 16bit*2 + 32bit*

Collision Resolution zLinear probing often produces a “cluster” of values. This slows the process of finding/inserting a value. zWhat else can we do? yLinear probing with a factor > 1. yQuadratic probing. yDouble Hashing. yClosed addressing. 27

Linear Probing with i>1 zSuppose we use a hash function such as yH(x) = (x+k*i) mod n y“k” is the multiplying constant. yStart with i=0 and proceed to n-1 until an open bucket is found. zThis will move values away from a cluster more quickly. 28

Linear Probing Problem zIn our previous example, n=16. zWith k=6, we have zH(x) = (x+6*i) mod 16 zWhat is the probe sequence for x=3? z3, 9, 15, 5, 11, 1, 7, 13, 3 zOOPS!!! All values are not available! 29

Problem Solution z“n” and “k” must be relatively prime. zThus, try it with k=3: zUsing H(x) = (x+3*i) mod 16 zWhat is the probe sequence for x=3? z3, 6, 9, 12, 15, 2, 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3 zThe absolute best is to choose “n” prime. 30

Quadratic Probing zUses something like H(x) = (x+i 2 ) mod n zAdvantage: zMoves values farther more quickly. zDisadvantage: zNot guaranteed to probe every bucket z(But half is possible if n is prime). 31

Double Hashing zUses something like yH 1 (x) = [x+i*H 2 (x)] mod n yH 2 (x) = R – (x mod R) yHere, it is very important to choose n and R prime and that R < n. 32

Closed Addressing zIdea: avoid collisions entirely by allowing more than one key value per bucket. zHow to allow multiple values per bucket? yLinked List yBinary Search Tree yAVL Tree yAnother Hash table 33