Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.

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

Preliminaries Advantages –Hash tables can insert(), remove(), and find() with complexity close to O(1). –Relatively easy to program Disadvantages –There.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.
Hash Tables.
Asst. Prof. Dr. İlker Kocabaş Hash Tables. 2 Overview Information Retrieval Binary Search Trees Hashing. Applications. Example. Hash Functions. Hash Tables.
An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
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.
Data Structures Using C++ 2E
Hashing: Collision Resolution Schemes
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hash Tables and Associative Containers CS-212 Dick Steflik.
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.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Aree Teeraparbseree, Ph.D
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
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.
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 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).
COSC 2007 Data Structures II
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Hash Table March COP 3502, UCF.
Hashtables David Kauchak cs302 Spring Administrative Talk today at lunch Midterm must take it by Friday at 6pm No assignment over the break.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
Hashing CS 105. Hashing Slide 2 Hashing - Introduction In a dictionary, if it can be arranged such that the key is also the index to the array that stores.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
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.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
Hashing Hashing is another method for sorting and searching data.
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
Robbie CSCI2100A Data Structures Tutorial
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
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
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 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.
Hashing CS 110: Data Structures and Algorithms First Semester,
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 Data Structures CSCI 132, Spring 2014 Lecture 34 Analyzing Hash Tables.
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
Hashtables David Kauchak cs302 Spring Administrative Midterm must take it by Friday at 6pm No assignment over the break.
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Tables, hashtables, relations and dictionaries Dr. Andrew Wallace PhD BEng(hons) EurIng
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Searching Tables Table: sequence of (key,information) pairs
A Hash Table with Chaining
Tree traversal preorder, postorder: applies to any kind of tree
Presentation transcript:

Hash Tables: Collision Resolution

2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining

3 Hashing key hash function TABLESIZE - 1 : : hash table pos

4 “Kruse” hash table Example: 5 Kruse hash function

5 Hashing Each item has a unique key. Use a large array called a Hash Table. Use a Hash Function.

6 Operations Initialize –all locations in Hash Table are empty. Insert Search Delete

7 Hash Function Maps keys to positions in the Hash Table. Be easy to calculate. Use all of the key. Spread the keys uniformly.

8 Example: Hash Function #3 “collisions” value = (s[i] + 3*value) % 7; Hash KeyValue Aho 0 Kruse5 Standish1 Horowitz5 Langsam5 Sedgewick2 Knuth1

9 Collision When two keys are mapped to the same position. Very likely.

10 Birthdays Number of PeopleProbability

11 Collision Resolution Linear Probing. Chaining

12 Linear Probing Linear search in the array from the position where collision occurred.

13 Insert Apply hash function to get a position. Try to insert key at this position. Deal with collision.

14 Aho Hash Function hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 0 Example: Insert with Linear Probing Aho

15 Kruse hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Insert with Linear Probing Aho Kruse Hash Function

16 Standish hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 1 Example: Insert with Linear Probing Aho Kruse Standish Hash Function

17 Horowitz hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Insert with Linear Probing Aho Kruse Standish Horowitz Hash Function

18 Langsam hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Insert with Linear Probing Aho Kruse Standish Horowitz Hash Function Langsam

19 module linearProbe(item) { position = hash(key of item) count = 0 loop { if (count == hashTableSize) then { output “Table is full” exit loop } if (hashTable[position] is empty) then { hashTable[position] = item exit loop } position = (position + 1) % hashTableSize count++ }

20 Search Apply hash function to get a position. Look in that position. Deal with collision.

21 Kruse Langsam hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Search with Linear Probing Aho Standish Horowitz Hash Function Langsam found.

22 Kruse Knuth hash table 1 Example: Search with Linear Probing Aho Standish Horowitz Hash Function Langsam not found. Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth

23 module search(target) { count = 0 position = hash(key of target) loop { if (count == hashTableSize) then { output “Target is not in Hash Table” return -1. } else if (hashTable[position] is empty) then { output “Item is not in Hash Table” return -1. } else if (hashTable[position].key == target) then { return position. } position = (position + 1) % hashTableSize count++ }

24 Delete Use the search function to find the item If found check that items after that also don’t hash to the item’s position If items after do hash to that position, move them back in the hash table and delete the item.

25 Linear Probing: Problems Speed. Tendency for clustering to occur as the table becomes half full. Deletion of records is very difficult.

26 Chaining Uses a Linked List at each position in the Hash Table.

27 hash table : :

Aho, Kruse, Standish, Horowiz, Langsam, Sedgwick, Knuth Example: Chaining 0, 5, 1, 5, 5, 2, KruseHorowitz Knuth 1 Standish Aho Sedgewick Langsam 0 0 0

29 Hashtable with Chaining At each position in the array you have a list: List hashTable[MAXTABLE]; :

30 Insert with Chaining Apply hash function to get a position in the array. Insert key into the Linked List at this position in the array.

31 module InsertChaining(item) { posHash = hash(key of item) insert (hashTable[posHash], item); } Knuth 1 Standish Aho Sedgewick :

32 Search with Chaining Apply hash function to get a position in the array. Search the Linked List at this position in the array.

33 /* module returns NULL if not found, or the address of the * node if found */ module SearchChaining(item) { posHash = hash(key of item) Node* found; found = searchList (hashTable[posHash], item); return found; } Knuth 1 Standish Aho Sedgewick :

34 Delete with Chaining Apply hash function to get a position in the array. Delete the node in the Linked List at this position in the array.

35 /* module uses the Linked list delete function to delete an item *inside that list, it does nothing if that item isn’t there. */ module DeleteChaining(item) { posHash = hash(key of item) deleteList (hashTable[posHash], item); } Knuth 1 Standish Aho Sedgewick :

36 Disadvantages of Chaining Uses more space. More complex to implement. –A linked list at every element in the array

37 Advantages of Chaining Insertions and Deletions are easy and quick. Allows more records to be stored. Naturally resizable, allows a varying number of records to be stored.