EEE2108: Programming for Engineers Chapter 8. Hashing

Slides:



Advertisements
Similar presentations
CS Data Structures Chapter 8 Hashing.
Advertisements

Hash Tables CIS 606 Spring 2010.
Review of Chapter 8 張啟中.
Part II Chapter 8 Hashing Introduction Consider we may perform insertion, searching and deletion on a dictionary (symbol table). Array Linked list Tree.
Hashing. CENG 3512 Motivation The primary goal is to locate the desired record in a single access of disk. – Sequential search: O(N) – B+ trees: O(log.
CSCE 3400 Data Structures & Algorithm Analysis
Skip List & Hashing CSE, POSTECH.
NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 8 Hashing 1.
Data Structures Using C++ 2E
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys. Operations.  get(theKey)  put(theKey, theElement)  remove(theKey) 5/2/20151.
© 2004 Goodrich, Tamassia Hash Tables1  
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by:  Search the hash table in.
FALL 2004CENG 3511 Hashing Reference: Chapters: 11,12.
Hash Tables1 Part E Hash Tables  
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
CS Data Structures Chapter 8 Hashing (Concentrating on Static Hashing)
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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 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.
Comp 335 File Structures Hashing.
© 2004 Goodrich, Tamassia Hash Tables1  
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-1 Chapter 8 Hashing Introduction to Data Structure CHAPTER 8 HASHING 8.1 Symbol Table Abstract Data.
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.
Hashing Static Hashing Dynamic Hashing. – 2 – Sungkyunkwan University, Hyoung-Kee Choi © Symbol table ADT  We define the symbol table as a set of name-attribute.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Chapter 9 Hashing Dr. Youssef Harrath
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? –
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Data Structures Chapter 8: Hashing 8-1. Performance Comparison of Arrays and Trees Is it possible to perform these operations in O(1) ? ArrayTree Sorted.
Hashing (part 2) CSE 2011 Winter March 2018.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Data Structures Using C++ 2E
Hashing Jeff Chastine.
CSCI 210 Data Structures and Algorithms
Hashing CSE 2011 Winter July 2018.
CS 332: Algorithms Hash Tables David Luebke /19/2018.
Hashing Alexandra Stefan.
Week 8 - Wednesday CS221.
Hash Tables (Chapter 13) Part 2.
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Review Graph Directed Graph Undirected Graph Sub-Graph
Advanced Associative Structures
Hash Table.
Chapter 28 Hashing.
Chapter 10 Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Dictionaries and Their Implementations
Resolving collisions: Open addressing
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.
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by: Search the hash table in some.
Hashing Alexandra Stefan.
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Indexing and Hashing B.Ramamurthy Chapter 11 2/5/2019 B.Ramamurthy.
Dictionaries Collection of pairs. Operations. (key, element)
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by: Search the hash table in some.
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by: Search the hash table in some.
CS 5243: Algorithms Hash Tables.
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by: Search the hash table in some.
Collision Handling Collisions occur when different elements are mapped to the same cell.
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
What we learn with pleasure we never forget. Alfred Mercier
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Presentation transcript:

EEE2108: Programming for Engineers Chapter 8. Hashing

8.1 Introduction Consider the ADT dictionary Can be found in many applications: The index for a DB Symbol table in compiler When a dictionary with n entries is represented as a binary search tree, search, insert, and delete take O(n) time - may be performed in (logn) using a balanced binary tree If we use a hashing technique, we can perform the operations in O(1) expected time Static hashing and dynamic hashing

8.2 Static Hashing 8.2.1 Hash Tables The dictionary pairs are stored in a hash table The hash table is partitioned into b buckets, ht[0], ..., ht[b-1] Each bucket is capable of holding s dictionary pairs : A bucket consists of s slots Each slot holds one dictionary pair h(k) is the hash value (home address of k) k is the key h is a hash function which maps keys into buckets in the range 0 through b-1

8.2.2 Hash Functions A hash function maps a key into a bucket in the hash table The desired properties of such a function are that Easy to compute (Hardware implementation) Minimize the number of collisions Even distribution A random key has an equal chance of hashing into any of the buckets : A uniform hash function Mod operation: k % #buckets Folding Key k is partitioned into several parts. These partitions are then added together to obtain the hash address for k Shift folding: Different parts are added to get h(k)

Example: k = 12320324111220. Using shift folding More examples: A hash table with 13 buckets

8.2.3 Overflow Handling: Linear probing Two popular ways to handle overflows Open addressing, chaining Open addressing methods Linear probing (= linear open addressing), quadratic probing, rehashing, random probing Example A 13-bucket table with one slot per bucket The last identifier, function, hashes to the same bucket as if: collision Using a circular rotation, the next available bucket is ht [0]

Table Search with linear probing : Compute h(k) and Examine the hash table buckets in the order ht[h(k)], ht[(h(k)+1) % b], …, ht[(h(k)+b-1) % b] until one of the following happens: The bucket ht[(h(k)+j) % b] has a pair whose key is k; In this case, the desired pair has been found ht[h(k) + j] is empty; k is not in the table Return to the starting position ht[h(k)]; the table is full and k is not in the table

[Program 8.3] Linear probing element* search(int k) { /* search the linear probing hash table ht (each bucket has exactly one slot) for k, if a pair with key k is found, return a pointer to this pair; otherwise, return NULL */ int homeBucket, currentBucket; homeBucket = h(k); for (currentBucket = homeBucket; ht[currentBucket] && ht[currentBucket]->key != k;) { currentBucket = (currentBucket + 1) % b; /* treat the table as circular */ if (currentBucket == homeBuket) return NULL; /* back to start point */ } if (ht[currentBucket]->key == k) return ht[currentBucket]; return NULL;

8.2.4 Theoretical Evaluation of Overflow Technique The hashing techniques shows a very good performance The worst-case performance is vey bad An insertion or a search in a hash table with n keys may take O(n) time Let ht[0:b-1]: hash table with b buckets, each bucket has one slot h is a uniform has function with range [0, b-1] If n keys k1, k2, ..., kn are entered into the hash table, there are b*n distinct hash sequences h(k1), h(k2), ..., h(kn)

Determine Sn: the expected number of key comparisons needed to locate a randomly chosen ki, 1≤ i ≤n Un: the expected number of key comparisons when a search is made for a key not in the hash table Theorem: Let α = n/b be the loading density of a hash table using a uniform hashing function h. Then, for linear open addressing