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.

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

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Hashing.
An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
Hashing General idea Hash function Separate Chaining Open Addressing
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.
© 2004 Goodrich, Tamassia Hash Tables1  
Log Files. O(n) Data Structure Exercises 16.1.
Hashing Techniques.
© 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.
CS2420: Lecture 33 Vladimir Kulyukin Computer Science Department Utah State University.
1 Hashing: Collision Resolution Schemes Collision Resolution Techniques Introduction to Separate Chaining Collision Resolution using Separate Chaining.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Aree Teeraparbseree, Ph.D
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.
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.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Hashing. Hashing as a Data Structure Performs operations in O(c) –Insert –Delete –Find Is not suitable for –FindMin –FindMax –Sort or output as sorted.
Hashing The Magic Container. Interface Main methods: –Void Put(Object) –Object Get(Object) … returns null if not i –… Remove(Object) Goal: methods are.
1. 2 Problem RT&T is a large phone company, and they want to provide enhanced caller ID capability: –given a phone number, return the caller’s name –phone.
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).
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
Hash Table March COP 3502, UCF.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
1.  We’ll discuss the hash table ADT which supports only a subset of the operations allowed by binary search trees.  The implementation of hash tables.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
Dictionaries and Hash Tables1 Hash Tables  
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Storage and Retrieval Structures by Ron Peterson.
© 2004 Goodrich, Tamassia Hash Tables1  
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.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
LECTURE 35: COLLISIONS CSC 212 – Data Structures.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
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.
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.
Data Structure & Algorithm Lecture 8 – Hashing JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
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.
CMSC 341 Hashing Readings: Chapter 5. Announcements Midterm II on Nov 7 Review out Oct 29 HW 5 due Thursday CMSC 341 Hashing 2.
Hashing & Hash Tables. Sets/Dictionaries Set - Our best efforts to date:
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.
CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
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.
Hashing Jeff Chastine.
Hashing Problem: store and retrieving an item using its key (for example, ID number, name) Linked List takes O(N) time Binary Search Tree take O(logN)
Hash Tables (Chapter 13) Part 2.
Hash In-Class Quiz.
Collision Resolution Neil Tang 02/18/2010
Resolving collisions: Open addressing
Double hashing Removal (open addressing) Chaining
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
A Hash Table with Chaining
Collision Resolution Neil Tang 02/21/2008
Ch. 13 Hash Tables  .
Presentation transcript:

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 should be placed Mapping is called the hash function What is a Hash Table?

Hash Table Operations size() isEmpty() find(k) – find element with key k insertItem(k, e) – insert element e with key k removeElement(k) – remove element with key k elements() – return Iterator of elements keys() – return Iterator of keys

Array Implementation element is name key is id number map id number to array index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] bob 1234 jane 1344 sally 1354

Why a Hash Table? Random access to items –Don’t have to perform search to find item Complexity of find/remove?

Hash Function The hash function has to map the key to a value –Bad option: add up ASCII values of characters in key and % by number of table elements –Better option: for(i=0; i < key.length(); i++) hashVal= 37*hashVal + key[i] hashVal %= tableSize

Collisions It is still possible that two keys will map to the same value Separate chaining –each array slot is a pointer to a linked list of elements –how would find/insert/remove work? Linear probing –walk down the list until you find an empty slot

Collisions Quadratic probing –A[i+f(j) mod N] for j=0, 1, 2, … where f(j) = j 2 Double Hashing –probe at hash 2 (x), 2hash 2 (x), etc

Rehashing As table gets full, insertions may slow because of more collisions Rehash by creating table twice as large and reinserting all elements into new table –requires rehashing all keys (because of new table size)