Hashing Jeff Chastine.

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

Hash Tables.
Hashing.
© 2004 Goodrich, Tamassia Hash Tables1  
CS 253: Algorithms Chapter 11 Hashing Credit: Dr. George Bebis.
1.1 Data Structure and Algorithm Lecture 9 Hashing Topics Reference: Introduction to Algorithm by Cormen Chapter 12: Hash Tables.
11.Hash Tables Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Directed-address tables Direct addressing is a simple technique that works well.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
CSE 326 Hashing Richard Anderson (instead of Martin Tompa)
Lecture 10: Search Structures and Hashing
Hashtables David Kauchak cs302 Spring Administrative Talk today at lunch Midterm must take it by Friday at 6pm No assignment over the break.
Spring 2015 Lecture 6: Hash Tables
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Implementing Dictionaries Many applications require a dynamic set that supports dictionary-type operations such as Insert, Delete, and Search. E.g., a.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
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?
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
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.
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,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Midterm Midterm is Wednesday next week ! The quiz contains 5 problems = 50 min + 0 min more –Master Theorem/ Examples –Quicksort/ Mergesort –Binary Heaps.
Hashtables David Kauchak cs302 Spring Administrative Midterm must take it by Friday at 6pm No assignment over the break.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
1 Hash Tables Chapter Motivation Many applications require only: –Insert –Search –Delete Examples –Symbol tables –Memory management mechanisms.
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?
Hashing (part 2) CSE 2011 Winter March 2018.
Hash table CSC317 We have elements with key and satellite data
Hashing CSE 2011 Winter July 2018.
CS 332: Algorithms Hash Tables David Luebke /19/2018.
Hashing - resolving collisions
Hashing Alexandra Stefan.
Hash Tables (Chapter 13) Part 2.
EEE2108: Programming for Engineers Chapter 8. Hashing
Hashing Alexandra Stefan.
© 2013 Goodrich, Tamassia, Goldwasser
Hashing Course: Data Structures Lecturer: Uri Zwick March 2008
Hash Table.
Hash Table.
Richard Anderson (instead of Martin Tompa)
Hashing and Hash Tables
Hash In-Class Quiz.
Dictionaries and Their Implementations
Collision Resolution Neil Tang 02/18/2010
Introduction to Algorithms 6.046J/18.401J
Resolving collisions: Open addressing
Data Structures and Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia 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
Chapter 11: Hash Tables.
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
EE 312 Software Design and Implementation I
CS 5243: Algorithms Hash Tables.
Chapter 11: Hash Tables.
Hashing Course: Data Structures Lecturer: Uri Zwick March 2008
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
EE 312 Software Design and Implementation I
Lecture-Hashing.
Presentation transcript:

Hashing Jeff Chastine

Hashing Many applications require INSERT, SEARCH and DELETE functions Hashing on average time can do all of these in O (1) Based on keys Falls under two general categories: Direct-Address Tables Hash Tables Jeff Chastine

Direct-Addressing Good for when universe U of keys is small U = {0, 1, …, m – 1 | m is not large} All elements have unique keys Table T [0..m -1] | each slot corresponds to a key All operations take only O (1) Jeff Chastine

Direct Implementation key satellite data 1 U (universe of keys) 2 2 3 9 6 3 7 4 4 1 2 5 K (actual keys) 5 3 6 5 8 7 8 8 9 Jeff Chastine

Direct-Addressing Operations DIRECT-ADDRESS-SEARCH (T, k) return T[k] DIRECT-ADDRESS-INSERT (T, x) T[key[x]] ← x DIRECT-ADDRESS-DELETE (T, x) T[key[x]] ← NIL Jeff Chastine

Hash Tables What are potential problems with direct addressing? |U| may be impractical Set of actual keys may be small Example SSNs Here, hash tables require much less storage Only catch: O (1) is average time instead of worst-case ! Jeff Chastine

How it works With direct-addressing, something with key k goes into slot k With hashing it goes into h (k) | h is a hash function Hash functions try to “randomize” Hash function maps U to T [0..m – 1] h :U → {0, 1, …, m – 1} Instead of |U| values,need only m values Jeff Chastine

Hash Implementation T U (universe of keys) K (actual keys) k1 k4 k5 k2 U (universe of keys) h (k1) h (k4) k1 h (k2) = h (k5) K (actual keys) k4 k5 k2 k3 h (k3) m - 1 Jeff Chastine

Collisions Have two keys hash to the same slot Because |U| > m, pigeon hole principle Therefore, collisions must exist We often talk of the load factor (α = n/m) Pick a good hash function Near random, yet deterministic Can chain collisions together This is where the worst-case comes from Can use open addressing Jeff Chastine

Chaining T U (universe of keys) K (actual keys) k1 k7 k4 k7 k1 k5 k2 Jeff Chastine

Hash Functions What makes a good hash function? Equally likely to hash to any of the m slots If keys are random numbers [0 … 1} then take floor of km Convert strings to ASCII to hash? Most usually involve mod Jeff Chastine

Hash Functions Division method: Multiplication method: h (k ) = k mod m Multiplication method: Let 0 < A < 1 h (k ) = floor(m (k A mod 1) ) // Fractional part Jeff Chastine

Open Addressing Systematically examine or probe slots until item is found No lists and no elements stored outside the table; thus α <= 1 Instead of following pointers, we compute the sequence Instead of fixed order – is based off of key Jeff Chastine

Kinds of Open Addressing Linear Probing h (k, i ) = (h’ (k ) + i ) mod m Quadratic Probing h (k, i ) = (h’ (k ) +c1i + c2i 2) mod m Double Hashing h (k, i ) = (h1(k ) + i h2(k )) mod m Jeff Chastine

Jeff Chastine