Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.

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.
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
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.
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.
Previous Lecture Revision Previous Lecture Revision Hashing Searching : –The Main purpose of computer is to store & retrieve –Locating for a record is.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CSE 326 Hashing Richard Anderson (instead of Martin Tompa)
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)
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.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
Chapter 5: Hashing Collision Resolution: Separate Chaining Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.
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
CS201: Data Structures and Discrete Mathematics I Hash Table.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
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.
Hashing CS 110: Data Structures and Algorithms First Semester,
Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.
A Introduction to Computing II Lecture 11: Hashtables Fall Session 2000.
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.
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? –
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.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
BBM 204 Algorithms Lab Recitation 5 Hash functions Sequential Chaining
Searching Tables Table: sequence of (key,information) pairs
A Hash Table with Chaining
Tree traversal preorder, postorder: applies to any kind of tree
Collision Resolution Neil Tang 02/21/2008
Presentation transcript:

Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – 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

5Hashing Each item has a unique key. Uses a large array called a Hash Table. Uses a Hash Function. Hash Function Maps keys to positions in the Hash Table. Be easy to calculate. Use all of the key. Spread the keys uniformly.

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

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

8Collision When two keys are mapped to the same position. Very likely Number of PeopleProbabilityBirthdays

9 Collision Resolution Two methods are commonly used: –Linear Probing. –Chaining.

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

11 Insert with Linear Probing Apply hash function to get a position. Try to insert key at this position. Deal with collision. –Must also deal with a full table!

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

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

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

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

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

17 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++ }

18 Search with Linear Probing Apply hash function to get a position. Look in that position. Deal with collision.

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

20 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

21 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++ }

22 Delete with Linear Probing 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. Very difficult and time/resource consuming!

23 Linear Probing: Problems Speed. Tendency for clustering to occur as the table becomes half full. Deletion of records is very difficult. If implemented in arrays – table may become full fairly quickly, resizing is time and resource consuming

24 Chaining Uses a Linked List at each position in the Hash Table. –Linked list at a position contains all the items that ‘hash’ to that position. –May keep linked lists sorted or not.

25 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

27 Hashtable with Chaining At each position in the array you have a list: List hashTable[MAXTABLE]; You must initialise each list in the table :

28 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.

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

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

31 /* 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 :

32 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.

33 /* 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 :

34 Disadvantages of Chaining Uses more space. More complex to implement. –Contains a linked list at every element in the array. –Requires linear searching. –May be time consuming.

35 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.

36Revision Hash Tables –Hash Functions –Insert, Search Collision Resolution –Linear Probing, Chaining Revision: Reading Kruse 8 Standish 11 Langsam 7.4 Sedgewick 16 Preparation Next lecture: Advanced Sorting Read Chapter in Kruse et al.