Hash Functions. Kuan-Yu Chen ( 陳冠宇 ) TR-212, NTUST

Similar documents
File Organization. Kuan-Yu Chen ( 陳冠宇 ) TR-212, NTUST

TABLES AND HASHING. Chapter 13

Red-Black, Splay and Huffman Trees

Stacks & Queues. Kuan-Yu Chen ( 陳冠宇 ) TR-212, NTUST

Multi-way Search Trees

Data Structures and Algorithms(10)

Queues. Kuan-Yu Chen ( 陳冠宇 ) TR-212, NTUST

Tirgul 7. Hash Tables. In a hash table, we allocate an array of size m, which is much smaller than U (the set of keys).

Question Bank Subject: Advanced Data Structures Class: SE Computer

Algorithms with numbers (2) CISC4080, Computer Algorithms CIS, Fordham Univ.! Instructor: X. Zhang Spring 2017

Algorithms with numbers (2) CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Support for Dictionary

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5.

CSE 214 Computer Science II Searching

HashTable CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Fall 2018

1. Attempt any three of the following: 15

Hash Tables. Hashing Probing Separate Chaining Hash Function

Worst-case running time for RANDOMIZED-SELECT

Acknowledgement HashTable CISC4080, Computer Algorithms CIS, Fordham Univ.

Module 7 Highlights. Mastered Reviewed. Sections ,

Slide 1 / 180. Radicals and Rational Exponents

b) Describe a procedure visitors should follow to find a free parking space, when the space they are assigned is occupied.

Module 2: Classical Algorithm Design Techniques

Data Structures And Algorithms

III Data Structures. Dynamic sets

Comp 335 File Structures. Hashing

Lecture 4: Advanced Data Structures

Math 302 Introduction to Proofs via Number Theory. Robert Jewett (with small modifications by B. Ćurgus)

CSCD 326 Data Structures I Hashing

Introduction to Modular Arithmetic

1.2 Round-off Errors and Computer Arithmetic

Adapted By Manik Hosen

Hashing 1. Searching Lists

Data and File Structures Chapter 11. Hashing

R16 SET - 1 '' ''' '' ''' Code No: R

of characters from an alphabet, then, the hash function could be:

Consider the actions taken on positive integers when considering decimal values shown in Table 1 where the division discards the remainder.

Lecture 7: Efficient Collections via Hashing

Hashing for searching

Hash Table and Hashing

9/24/ Hash functions

Section A Arithmetic ( 5) Exercise A

Any Integer Can Be Written as a Fraction

CSI33 Data Structures

Math 96--Radicals #1-- Simplify; Combine--page 1

Hashing 1. Searching Lists

Hash Tables. Gunnar Gotshalks. Maps 1

Middle School Math 3 and 8th GLEs

AdvanceDataStructures-Unit-1(Dictionaries)

Table of Contents. Chapter 1: Introduction to Data Structures... 1

CS 310 Hash Tables, Page 1. Hash Tables. CS 310 Hash Tables, Page 2

Hashing. Hashing Procedures

Programming Iterative Loops. for while

Lecture 4. Hashing Methods

1. To reduce the probability of having any collisions to < 0.5 when hashing n keys, the table should have at least this number of elements.

Introduction to Algorithms

General Idea. Key could be an integer, a string, etc e.g. a name or Id that is a part of a large employee structure

Special Products on Factoring

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques

Hashing. Introduction to Data Structures Kyuseok Shim SoEECS, SNU.

CS1800 Discrete Structures Fall 2017 October 25, CS1800 Discrete Structures Midterm Version B

Math 10- Chapter 2 Review

Hash Tables. Hash functions Open addressing. November 24, 2017 Hassan Khosravi / Geoffrey Tien 1

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Symbol Table. Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management

PROBLEM SOLVING TECHNIQUES SECTION - A. 1. Answer any ten of the following

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

UNIT 5. Sorting and Hashing

Two states of the queue, either empty or containing some elements, can be judged by the following tests:

Hashing. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Lab Manual. Program Design and File Structures (P): IT-219

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Learning Log Title: CHAPTER 3: ARITHMETIC PROPERTIES. Date: Lesson: Chapter 3: Arithmetic Properties

Data Structures and Algorithms. Chapter 7. Hashing

Hash table basics mod 83 ate. ate. hashcode()

Hashing. Dr. Ronaldo Menezes Hugo Serrano. Ronaldo Menezes, Florida Tech

Questions. 6. Suppose we were to define a hash code on strings s by:

Radicals and Fractional Exponents

Outline. Computer Science 331. Desirable Properties of Hash Functions. What is a Hash Function? Hash Functions. Mike Jacobson.

Data Structures and Algorithms. Roberto Sebastiani

Assertions & Verification & Example Loop Invariants Example Exam Questions

Hash Tables. Hash functions Open addressing. March 07, 2018 Cinda Heeren / Geoffrey Tien 1

Understand how to deal with collisions

Algebra II Radical Equations

CSE 373 Sample Midterm #2 (closed book, closed notes, calculators o.k.)

Assertions & Verification Example Exam Questions

Random-Number Generation

A.4 Rationalizing the Denominator

PROGRAMMING IN C AND C++:

Topic HashTable and Table ADT

Algebra 1. Standard 11 Operations of Expressions. Categories Combining Expressions Multiply Expressions Multiple Operations Function Knowledge

Summer Packet 7 th into 8 th grade. Name. Integer Operations = 2. (-7)(6)(-4) = = = = 6.

HASH TABLES.

CS1020 Data Structures and Algorithms I Lecture Note #15. Hashing. For efficient look-up in a table

Midterm Exam 2B Answer key

Review of Operations on the Set of Real Numbers

Statistical Computing

11.3 Function Prototypes

Mathematics. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Transcription:

Hash Functions Kuan-Yu Chen ( 陳冠宇 ) 2018/12/12 @ TR-212, NTUST

Review A binary heap is a complete binary tree in which every node satisfies the heap property Min Heap Max Heap A binomial heap HH is a set of binomial trees Every binomial tree in HH satisfies the minimum heap property A Fibonacci heap, which is more flexible then binomial heap, is a collection of trees 2

Hashing. Hash table is a data structure in which keys are mapped to array positions by a hash function An element with key kk is stored at index h(kk) It means a hash function h is used to calculate the index at which the element with key kk will be stored 3

Hashing.. When two or more keys map to the same memory location, a collision is said to occur kk 2 and kk 6 kk 5 and kk 7 4

Hashing The main goal of using a hash function is to reduce the range of array indices that have to be handled Instead of having UU values, we just need KK values, thereby reducing the amount of storage space required Given key values range from 00000 to 99999 We need an array of size 100,000, but only 100 elements will be used 5

Hash Functions. A hash function is a mathematical formula which, when applied to a key, produces an integer which can be used as an index for the key in the hash table It produces a unique set of integers within some suitable range in order to reduce the number of collisions In practice, there is no hash function that eliminates collisions completely 6

Hash Functions.. Properties of a good hash function Low cost The cost of executing a hash function must be small Determinism A hash procedure must be deterministic Uniformity A good hash function must map the keys as evenly as possible over its output range This means that the probability of generating every hash value in the output range should roughly be the same The property of uniformity also minimizes the number of collisions 7

Representative Hash Functions. Division Method It is the most simple method of hashing an integer xx h xx = xx mod MM Since it requires only a single division operation, the method works very fast Extra care should be taken to select a suitable value for MM Suppose MM is an even number then h(xx) is even if xx is even and h(xx) is odd if xx is odd If all possible keys are equal-probable, then this is not a problem If even keys are more likely than odd keys, then the division method will not spread the hashed values uniformly It is usually to choose MM to be a prime number 8

Example Calculate the hash values of keys 1234 and 5462 by referring to MM = 97 h 1234 = 1234%97 = 70 h 5642 = 5642%97 = 16 9

Representative Hash Functions.. Multiplication Method The method is defined by h xx = mm xxxx mod 1 Choose a constant AA such that 0 < AA < 1 Multiply the key xx by AA Extract the fractional part of xxaa Multiply the fractional part of xxxx by the size of hash table mm The greatest advantage of this method is that it works practically with any value of AA Knuth has suggested that the best choice of A is 5 1 2 = 0.618033 10

Example Given a hash table of size 1000, map the key 12345 to an appropriate location in the hash table AA = 0.618033 mm = 1000 h 12345 = mm 12345 AA mod 1 = 1000 12345 0.618033 mod 1 = 1000 7629.617385 mod 1 = 1000 0.617385 = 617.385 = 617 11

Representative Hash Functions... Mid-Square Method The algorithm works well because most or all digits of the key value contribute to the result In general, the function is defined by: where ss is obtained by selecting rr digits from xx 2 Square the value of the key h xx = ss Extract the middle rr digits of the result 12

Example Calculate the hash value for keys 1234 and 5642 using the mid-square method The hash table has 100 memory locations Note that the hash table has 100 memory locations whose indices vary from 0 to 99 This means that only two digits are needed to map the key to a location in the hash table, so rr = 2 xx = 1234, xx 2 = 1522756, h 1234 = 27 xx = 5642, xx 2 = 31832164, h 5642 = 21 13

Representative Hash Functions. Folding Method The folding method works in the following two steps Divide the key value into a number of parts Add the individual parts The hash value is produced by ignoring the last carry, if any Note that the number of digits in each part of the key will vary depending upon the size of the hash table 14

Example Given a hash table of 100 locations, calculate the hash value using folding method for keys 5678, 321, and 34567 Since there are 100 memory locations to address, we will break the key into parts where each part (except the last) will contain two digits 15

Questions? kychen@mail.ntust.edu.tw 16