NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 8 Hashing 1.

Slides:



Advertisements
Similar presentations
CS Data Structures Chapter 8 Hashing.
Advertisements

Hash Tables.
HASH TABLE. HASH TABLE a group of people could be arranged in a database like this: Hashing is the transformation of a string of characters into a.
Review of Chapter 8 張啟中.
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.
Part II Chapter 8 Hashing Introduction Consider we may perform insertion, searching and deletion on a dictionary (symbol table). Array Linked list Tree.
CSCE 3400 Data Structures & Algorithm Analysis
Skip List & Hashing CSE, POSTECH.
Data Structures Using C++ 2E
Hashing as a Dictionary Implementation
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Log Files. O(n) Data Structure Exercises 16.1.
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.
11.Hash Tables Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Directed-address tables Direct addressing is a simple technique that works well.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
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.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms.
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.
1 HASHING Course teacher: Moona Kanwal. 2 Hashing Mathematical concept –To define any number as set of numbers in given interval –To cut down part of.
NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 9 ADVANCED SEARCH TREES 1.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
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.
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
March 23 & 28, Csci 2111: Data and File Structures Week 10, Lectures 1 & 2 Hashing.
March 23 & 28, Hashing. 2 What is Hashing? A Hash function is a function h(K) which transforms a key K into an address. Hashing is like indexing.
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,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Hashing 8 April Example Consider a situation where we want to make a list of records for students currently doing the BSU CS degree, with each.
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.
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.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
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.
1 CSCD 326 Data Structures I Hashing. 2 Hashing Background Goal: provide a constant time complexity method of searching for stored data The best traditional.
Data Structures Using C++
Chapter 9 Hashing Dr. Youssef Harrath
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.
Hashing 1 Lec# 12 Presented by Halla Abdel Hameed.
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.
Data Structures Using C++ 2E
Hashing CSE 2011 Winter July 2018.
School of Computer Science and Engineering
Slides by Steve Armstrong LeTourneau University Longview, TX
Hash Tables (Chapter 13) Part 2.
EEE2108: Programming for Engineers Chapter 8. Hashing
Data Structures Using C++ 2E
Review Graph Directed Graph Undirected Graph Sub-Graph
Advanced Associative Structures
Hash Table.
Hash Table.
CS202 - Fundamental Structures of Computer Science II
Data Structures – Week #7
What we learn with pleasure we never forget. Alfred Mercier
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 8 Hashing 1

NCUE CSIE Wireless Communications and Networking Laboratory Hashing Definition: In hashing the dictionary pairs are stored in a table, ht, called the hash table. The hash table is partitioned into b buckets, ht[0],…,ht[b-1]. The address or location of a pair is determined by a hash function, h, which maps keys into buckets. Thus, for any key k, h(k) is an integer in the range 0 through b-1. 2

NCUE CSIE Wireless Communications and Networking Laboratory Hash Table index b-1 Bucket x H(x) 3

NCUE CSIE Wireless Communications and Networking Laboratory Terminologies 4 Identifier density: The identifier density of a hash table is the ratio n/T, where n is the number of identifiers in the table. The loading density or loading factor of a hash table is (T: distinct possible value of identifiers. s: number of slots per bucket. b: bucket number) utilizationCollision

NCUE CSIE Wireless Communications and Networking Laboratory 5 Terminologies Collision: A collision occurs when the home bucket for the new pair is not empty at the time of insertion. Overflow: Since many keys typically have the same home bucket, it is possible that the home bucket for a new dictionary pair is full at the time we wish to insert this pair into the dictionary. collisionoverflow not necessarily necessarily

NCUE CSIE Wireless Communications and Networking Laboratory Hashing Function Hashing Function Design : (1) Easy to Compute (2) Minimize the Number of Collisions (3) Uniform Hash function 6

NCUE CSIE Wireless Communications and Networking Laboratory Hashing Function Hashing Function :  Mid-Square  Division (Modulus)  Folding  Digit Analysis 7

NCUE CSIE Wireless Communications and Networking Laboratory Mid-square The mid-square hash function determines the home bucket for a key by squaring the key and then using an appropriate number of bits from the middle of the square to obtain the bucket address. ex) We assume the key = 8125 , and hashing table has 1000 buckets. 8 so address is “156” or “015”

NCUE CSIE Wireless Communications and Networking Laboratory Divisions The home bucket is obtained by using the modulo (%) operator. The key x is divided by some number M, and the remainder is used as the home bucket for x. f(x) = x mod M ex) 9 prime

NCUE CSIE Wireless Communications and Networking Laboratory Folding In this method the key k is partitioned into several parts, all but possibly the last being of the same length. These partitons are then added together to obtain the hash address for k. There are two ways of carrying out this additon. (1) Shift (2) Boundary ex1) We assume the key = , and hashing table has 1000 buckets. 123|203|241|112|20 (1) =699 (2) =897 10

NCUE CSIE Wireless Communications and Networking Laboratory Folding 11 ex2) (1) Shift (2) Boundary

NCUE CSIE Wireless Communications and Networking Laboratory Digit Analysis All the keys in the table are known in advance. Each key is interpreted as a number using some radix r. The same radix is used for all the keys in the table. Using this radix, the digits of each key are examined. ex) 12 phone number address

NCUE CSIE Wireless Communications and Networking Laboratory Overflow Handling  Linear Open Addressing (Linear Probing)  Quadratic Probing  Rehashing  Chaining 13

NCUE CSIE Wireless Communications and Networking Laboratory Linear Probing When the overflow occurs, we search the hash table buckets in the order (H(x)+1, H(x)+2…), until the hash table is full or reaching the first unfilled bucket. ex) Insert Insert

NCUE CSIE Wireless Communications and Networking Laboratory Linear Probing Advantages Advantages: Simple 、 Easy to Implement 。Disadvantages: When the clustering occurs, the search time will increase rapidly 。 15

NCUE CSIE Wireless Communications and Networking Laboratory Quadratic Probing H(x), overflow 16 When the overflow occurs, we search the hash table buckets by using ex) Key k, hash function H 1 st search : H(k) 2 nd search : (H(k)+1 2 )%b 3 th search : (H(k)-1 2 )%b 4 th search : (H(k)+2 2 )%b 5 th search : (H(k)-2 2 )%b N th search : (H(k)±((B-1)/2) 2 )%b

NCUE CSIE Wireless Communications and Networking Laboratory Rehashing The rehashing method is to use a series of hash functions h 1,h 2,…,h m. Buckets h i (k), 1 ≦ i ≦ m are examined in the order. 17

NCUE CSIE Wireless Communications and Networking Laboratory Chaining Many of the comparisons can be saved if we maintain lists of keys, one list per bucket, each list containing all the synonyms for that bucket Insert Insert 25 25

NCUE CSIE Wireless Communications and Networking Laboratory Assume that a hash function has the following characteristics: keys 257 and 567 hash to 3 keys 987 and 313 hash to 6 keys 734, 189 and 575 hash to 5 keys 122 and 391 hash to 8 Assume that insertions are done in order 257, 987, 122, 575, 189, 734, 567, 313, 391 (1)Indicate the position of the data if open probe addressingis used to resolve collision (2) Indicate the position of the data if chining with separate lists is used to resolve collision Hash Table 19 Question:

NCUE CSIE Wireless Communications and Networking Laboratory If H(x) = x mod 7 and separate chaining resolves collisions, What does the hash table look like after the following insertions occur: 8, 10, 24, 15, 32, 17? Assume that each table item contains only a search key. 20 Question:

NCUE CSIE Wireless Communications and Networking Laboratory Suppose the hashing function f(x) = x mod 11 is used to hash a list of input value (in the given order) into a hash table implemented by the array bucked[0],bucket[1],…bucket[10]. The inputs are 10,100,32,45,126,3,24,200,and 53. Each bucket can hold only one number. Overflow is resolved by quadratic probing, which examines buckets f(x), (f(x)+ ) mod 11,and (f(x)- ) mod 11, i=1 to 5.Show the final contents in bucket[0] to bucket[10]. 21 Question:

NCUE CSIE Wireless Communications and Networking Laboratory Ans:

NCUE CSIE Wireless Communications and Networking Laboratory For each hash table below, show the result of inserting the following sequence of key values, in the given order, into an initially empty hash table of that type: 26,17,20,9,34,32,15,21. In both cases, assume a hash table size of 11 and a hash function h(x) = x mod 11. (1)Static hash table that uses chaining (2)Hash table that uses linear probing 23 Question:

NCUE CSIE Wireless Communications and Networking Laboratory (1) (2) → →26→15 5→ →20→9 10→32→

NCUE CSIE Wireless Communications and Networking Laboratory 25 Reference  Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed 〝 Fundamentals of Data Structures in C 〞, W. H. Freeman & Co Ltd,  Ellis Horowitz, Sartaj Sahni, and Dinesh Mehta 〝 Fundamentals of Data Structures in C++ 〞 Silicon Pr, 2006  Richard F.Gilberg, Behrouz A. Forouzan, 〝 Data Structures: A Pseudocode Approach with C 〞, S Baker & Taylor Books, 2004  Fred Buckley, and Marty Lewinter 〝 A Friendly Introduction to Graph Theory 〞 Prentice Hall, 2002  〝資料結構 - 使用 C 語言〞蘇維雅譯,松崗, 2004  〝資料結構 - 使用 C 語言〞 蔡明志編著,全華, 2004  〝資料結構 ( 含精選試題 ) 〞洪逸編著,鼎茂, 2005