Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked.

Slides:



Advertisements
Similar presentations
Part II Chapter 8 Hashing Introduction Consider we may perform insertion, searching and deletion on a dictionary (symbol table). Array Linked list Tree.
Advertisements

CSCE 3400 Data Structures & Algorithm Analysis
Hashing as a Dictionary Implementation
Hashing Part One Reaching for the Perfect Search Most of this material stolen from "File Structures" by Folk, Zoellick and Riccardi.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing Techniques.
Hashing CS 3358 Data Structures.
CS 261 – Data Structures Hash Tables Part 1. Open Address Hashing.
Maps, Dictionaries, Hashtables
1 Hashing (Walls & Mirrors - end of Chapter 12). 2 I hate quotations. Tell me what you know. – Ralph Waldo Emerson.
Lecture 10 Sept 29 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
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.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Hashing General idea: Get a large array
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
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.
COSC 2007 Data Structures II
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Hash Table March COP 3502, UCF.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
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:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
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.
1 Hash table. 2 A basic problem We have to store some records and perform the following:  add new record  delete record  search a record by key Find.
1 HashTable. 2 Dictionary A collection of data that is accessed by “key” values –The keys may be ordered or unordered –Multiple key values may/may-not.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Comp 335 File Structures Hashing.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Storage and Retrieval Structures by Ron Peterson.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
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.
Hashing as a Dictionary Implementation Chapter 19.
Hashing – Part I CS 367 – Introduction to Data Structures.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
Lecture 12COMPSCI.220.FS.T Symbol Table and Hashing A ( symbol) table is a set of table entries, ( K,V) Each entry contains: –a unique key, K,
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.
Ihab Mohammed and Safaa Alwajidi. Introduction Hash tables are dictionary structure that store objects with keys and provide very fast access. Hash table.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
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.
Midterm Midterm is Wednesday next week ! The quiz contains 5 problems = 50 min + 0 min more –Master Theorem/ Examples –Quicksort/ Mergesort –Binary Heaps.
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
CSCI 210 Data Structures and Algorithms
Hashing Alexandra Stefan.
Hashing Alexandra Stefan.
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Advanced Associative Structures
Hash Table.
Dictionaries and Their Implementations
CSCE 3110 Data Structures & Algorithm Analysis
CS202 - Fundamental Structures of Computer Science II
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Presentation transcript:

Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked list? (O(N)) – (balanced) binary search tree (e.g., AVL tree)? (O(log 2 N)) – Unsorted array (O(N)) – Sorted array, binary search (O(log 2 N)) Can we do better than O(log n) (e.g., O(1))? – Yes, we can use “hash” functions

Hashing Basic Idea Use hash function to map keys into positions in a hash table Ideally If element e has key k and h is hash function, then e is stored in position h(k) of table To search for e, compute h(k) to locate position. If no element, the table does not contain e.

Hashing (cont’d) The general idea behind hashing is to directly map each data item into a position in a table (e.g., an array) using some function h – Complex data items must have a “key” in order to perform the hashing. Then, position = h(key) Data Name: Mike Height:.. Address: … h(“Mike”) =1 – With perfect hashing, every key is mapped to a different position. What is the implication of this in terms of T, the size of the array? 0 1Mike 2 T-1

Some Hash Function Example Division Folding

Hash Function: Division Hash Functions: Division If the keys are integers then key%T is generally a good hash function

Example The hash function was h(key) = key%10. Inserting entries 20, 25, 34, 76, 59, and

Hash Division What happens if more than one key hash to the same position? In general, to avoid situations like this, table size T should be a prime number. Collision methods, will be discussed later

Hash Function: Folding A key is divided into several parts, and use a simple operation to combine them in a certain way.

Hash Function: Folding If the keys are strings the hash function is some function of the characters in the strings. One possibility is to simply add the ASCII values of the characters: h(str) = (sum(str(i)))%T Another possibility is to convert the string into some number in some arbitrary base b (b also might be a prime number): Example h(ABC)=(65b 0 +66b 1 +67b 2 )%T

Folding Example int h(String x, int T) { int i, sum; for (sum=0, i=0; i<x.length(); i++) sum+= (int)x.charAt(i); return (sum%T); } – sums the ASCII values of the letters in the string ASCII value for “ A ” =65; sum will be in range for 10 upper-case letters; good when T around 100, for example – order of chars in string has no effect

Collision What happens if more than one key hash to the same position? The problem is called a “collision” – Solution #1: Open Addressing – Solution #2: “separate chaining” collision resolution approach, all data items that hash to the same position are kept in a linked list. So, to find the item that we are looking for, we have to search the linked list Hash functions should try to achieve uniform coverage of the hash table, while minimizing collisions

Handling Collision “Open addressing” resolves collisions by trying alternative slots in the hash table, until an empty cell is found. In general we try cells in the following order H 1 (key) = (h(key) + f(i))%T f(0) = 0

Example: Handling Collision-Open Addressing T =10, after 0, 1, 4, 19, 16, 25, 36, 59, 64 and 81 have been inserted using open addressing. The hash function h(key) = key %10 and f(i) =i ** 381 ** ** 919

Handling Collision: Chaining With “separate chaining” all data items that hash to the same location are kept in a linked list in that location. So, each entry in the hash table can be a linked list of arbitrary length. To find a piece of data we use the hash function to find the correct list, and then we search the list.

Chaining Example T =10, after 0, 1, 4, 19, 16, 25, 36, 59, 64 and 81 have been inserted > > > >19

Store colliding elements in the same position in the table When a bucket is full, the open addressing can be used.