1 Hash Tables Saurav Karmakar. 2 Motivation What are the dictionary operations? What are the dictionary operations? (1) Insert (1) Insert (2) Delete (2)

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

1 A B C
Variations of the Turing Machine
Chapter 11. Hash Tables.
1 11. Hash Tables Heejin Park College of Information and Communications Hanyang University.
Angstrom Care 培苗社 Quadratic Equation II
AP STUDY SESSION 2.
1
& dding ubtracting ractions.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
STATISTICS HYPOTHESES TEST (I)
David Burdett May 11, 2004 Package Binding for WS CDL.
Create an Application Title 1Y - Youth Chapter 5.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
CALENDAR.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
The 5S numbers game..
Media-Monitoring Final Report April - May 2010 News.
Break Time Remaining 10:00.
Factoring Quadratics — ax² + bx + c Topic
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
Chapter 10: Applications of Arrays and the class vector
Digital Lessons on Factoring
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.
Hash Table.
Briana B. Morrison Adapted from William Collins
Analysis of Algorithms CS 477/677
Hash Tables.
Hash Tables CIS 606 Spring 2010.
Hashing.
Bellwork Do the following problem on a ½ sheet of paper and turn in.
Hash Tables Dr. Li Jiang School of Computer Science,
1 The Royal Doulton Company The Royal Doulton Company is an English company producing tableware and collectables, dating to Operating originally.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Chapter 1: Expressions, Equations, & Inequalities
1..
Overview Hash Table Hash Function Hash table ADT operations
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Artificial Intelligence
Before Between After.
Slide R - 1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Prentice Hall Active Learning Lecture Slides For use with Classroom Response.
Subtraction: Adding UP
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
Essential Cell Biology
Converting a Fraction to %
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CSE20 Lecture 15 Karnaugh Maps Professor CK Cheng CSE Dept. UC San Diego 1.
Clock will move after 1 minute
famous photographer Ara Guler famous photographer ARA GULER.
PSSA Preparation.
Copyright © 2013 Pearson Education, Inc. All rights reserved Chapter 11 Simple Linear Regression.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
1 Dr. Scott Schaefer Least Squares Curves, Rational Representations, Splines and Continuity.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Hashing and Hash Tables
Presentation transcript:

1 Hash Tables Saurav Karmakar

2 Motivation What are the dictionary operations? What are the dictionary operations? (1) Insert (1) Insert (2) Delete (2) Delete (3) Search (most of the time, we will be focusing on search) (3) Search (most of the time, we will be focusing on search)

3 Objective Searching takes Θ(n) time in the worst case (when the data is unorganized). Searching takes Θ(n) time in the worst case (when the data is unorganized). Even using binary search it takes Θ(log n) time when the data are sorted. Even using binary search it takes Θ(log n) time when the data are sorted. Our Objective? Our Objective? O(1) time on average using hashing, under a reasonable assumption. O(1) time on average using hashing, under a reasonable assumption.

4 Definitions A hash table is a generalization of an array (direct addressing is allowed), so lets first talk about direct-address table. A hash table is a generalization of an array (direct addressing is allowed), so lets first talk about direct-address table. Universe of keys U={0,1,2,…,m-1}, no two elements have the same key. Universe of keys U={0,1,2,…,m-1}, no two elements have the same key. To represent a dynamic set, we use an array, or direct address table T[0..m-1], in which each position (slot) corresponds to the key in the universe. To represent a dynamic set, we use an array, or direct address table T[0..m-1], in which each position (slot) corresponds to the key in the universe.

5Definitions To represent a dynamic set, we use an array, or direct address table T[0..m-1], in which each position (slot) corresponds to a key in the universe. To represent a dynamic set, we use an array, or direct address table T[0..m-1], in which each position (slot) corresponds to a key in the universe. U (universe of keys) K (actual keys) T / / / / / / key satellite data

6 With a direct address table T[0..m-1], how do we search an element x with key k? With a direct address table T[0..m-1], how do we search an element x with key k? Direct-Address-Search(T,k): return T[k] Direct-Address-Search(T,k): return T[k] U (universe of keys) K (actual keys) T / / / / / / key satellite data

7 With a direct address table T[0..m-1], how do we search/insert/delete an element x with key k? With a direct address table T[0..m-1], how do we search/insert/delete an element x with key k? Direct-Address-Search(T,k): return T[k] Direct-Address-Search(T,k): return T[k] Direct-Address-Insert(T,x): T[key[x]] x Direct-Address-Insert(T,x): T[key[x]] x Direct-Address-Delete(T,x): T[key[x]] Nil Direct-Address-Delete(T,x): T[key[x]] Nil U (universe of keys) K (actual keys) T / / / / / / key satellite data

8 With a direct address table T[0..m-1], how do we search/insert/delete an element x with key k? With a direct address table T[0..m-1], how do we search/insert/delete an element x with key k? Direct-Address-Search(T,k): return T[k] Direct-Address-Search(T,k): return T[k] Direct-Address-Insert(T,x): T[key[x]] x O(1) time! Direct-Address-Insert(T,x): T[key[x]] x O(1) time! Direct-Address-Delete(T,x): T[key[x]] Nil Direct-Address-Delete(T,x): T[key[x]] Nil U (universe of keys) K (actual keys) T / / / / / / key satellite data

9 With a direct address table T[0..m-1], how do we search/insert/delete an element x with key k? With a direct address table T[0..m-1], how do we search/insert/delete an element x with key k? Direct-Address-Search(T,k): return T[k] Direct-Address-Search(T,k): return T[k] Direct-Address-Insert(T,x): T[key[x]] x Problem? Direct-Address-Insert(T,x): T[key[x]] x Problem? Direct-Address-Delete(T,x): T[key[x]] Nil Direct-Address-Delete(T,x): T[key[x]] Nil U (universe of keys) K (actual keys) T / / / / / / key satellite data

10 With direct addressing, an element with key k is inserted in slot h(k). h is called a hash function. With direct addressing, an element with key k is inserted in slot h(k). h is called a hash function. h maps the universe U of keys into the slots of a hash table T[0..m-1]. h maps the universe U of keys into the slots of a hash table T[0..m-1]. h : U {0,1,…,m-1} h : U {0,1,…,m-1} U (universe of keys) K (actual keys) T / / / / / / Hash Table

11 Basic Idea Use hash function to map keys into positions in a hash table Use hash function to map keys into positions in a hash tableIdeally If element e has key k and h is hash function, then e is stored in position h(k) of table 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, dictionary does not contain e. To search for e, compute h(k) to locate position. If no element, dictionary does not contain e.

12 With direct addressing, an element with key k is inserted in slot h(k). h is called a hash function. With direct addressing, an element with key k is inserted in slot h(k). h is called a hash function. h maps the universe U of keys into the slots of a hash table T[0..m-1]. h maps the universe U of keys into the slots of a hash table T[0..m-1]. h : U {0,1,…,m-1} h : U {0,1,…,m-1} U (universe of keys) K (actual keys) T / / / / / Hash Table: Collision Problem / / X Collision! If h(5)=h(8)

13 Two keys hash to the same slot --- collision. Two keys hash to the same slot --- collision. While collision is hard to avoid, if we design the hash function carefully we can at least decrease the chance for collision (and in some cases may avoid collision). While collision is hard to avoid, if we design the hash function carefully we can at least decrease the chance for collision (and in some cases may avoid collision). U (universe of keys) K (actual keys) T / / / / / Collision / / X Collision! If h(5)=h(8)

14 Two keys hash to the same slot --- collision. Two keys hash to the same slot --- collision. While collision is hard to avoid, if we design the hash function carefully we can at least decrease the chance for collision (and in some cases may avoid collision). While collision is hard to avoid, if we design the hash function carefully we can at least decrease the chance for collision (and in some cases may avoid collision). U (universe of keys) K (actual keys) T / / / / / Collision Resolution by Chaining / / If h(5)=h(8) 58

15 Chained-Hash-Insert(T,x): insert x at the head of list T[h(key[x])] Chained-Hash-Search(T,k): search for an element with key k in list T[h(k)] U (universe of keys) K (actual keys) T / / / / / Collision Resolution by Chaining / / If h(5)=h(8) 58

16 Chained-Hash-Insert(T,x): insert x at the head of list T[h(key[x])] Chained-Hash-Search(T,k): search for an element with key k in list T[h(k)] Chained-Hash-Delete(T,x): delete x from the list T[h(key[x])] Time? Time? U (universe of keys) K (actual keys) T / / / / / Collision Resolution by Chaining / / If h(5)=h(8) 58

17 Example: Let h(k)= k mod 11, insert 5,28,19,15,20,33,12,17,39,11 into T[0..10]. Example: Let h(k)= k mod 11, insert 5,28,19,15,20,33,12,17,39,11 into T[0..10]. Collision Resolution by Chaining T / / / / 11

18 A hash function which causes no collision is called perfect hash function. A hash function which causes no collision is called perfect hash function. A good hash function is one which satisfies simple uniform hashing --- each key is equally likely to hash to any of the m slots. (It is difficult to check this condition though.) A good hash function is one which satisfies simple uniform hashing --- each key is equally likely to hash to any of the m slots. (It is difficult to check this condition though.) Now lets see some example for hash functions. Assume that all the keys can be represented as natural numbers. Now lets see some example for hash functions. Assume that all the keys can be represented as natural numbers. Hash function

19 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = floor [m(kA mod 1)], Multiplication: h(k) = floor [m(kA mod 1)], A=(5 – 1)/2= A=(5 – 1)/2= Two step process Step 1: Step 1: –Multiply the key k by a constant 0< A < 1 and extract the fraction part of kA. Step 2: Step 2: –Multiply kA by m and take the floor of the result. Famous Examples of Hash Functions

20 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= … A=(5 – 1)/2= … Example. K = , m= Example. K = , m= h(k) = 10000( x … mod 1) h(k) = 10000( x … mod 1) Famous Examples of Hash Functions

21 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Example. K = , m= Example. K = , m= h(k) = 10000( x … mod 1) h(k) = 10000( x … mod 1) = 10000( … mod 1) = 10000( … mod 1) Famous Examples of Hash Functions

22 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= … A=(5 – 1)/2= … Example. K = , m= Example. K = , m= h(k) = 10000( x … mod 1) h(k) = 10000( x … mod 1) = 10000( … mod 1) = 10000( … mod 1) = x …) = x …) Famous Examples of Hash Functions

23 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Example. K = , m= Example. K = , m= h(k) = 10000( x mod 1) h(k) = 10000( x mod 1) = 10000( … mod 1) = 10000( … mod 1) = x …) = x …) = … = … Famous Examples of Hash Functions

24 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Example. K = , m= Example. K = , m= h(k) = 10000( x mod 1) h(k) = 10000( x mod 1) = 10000( … mod 1) = 10000( … mod 1) = x …) = x …) = … = … = 41 = 41 Famous Examples of Hash Functions

25 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Example 1. Shift folding: (SSN) Example 1. Shift folding: (SSN) = = mod 1000 = mod 1000 = 368. Famous Examples of Hash Functions

26 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Example 1. Shift folding: (SSN) Example 1. Shift folding: (SSN) = = mod 1000 = mod 1000 = 368. Example 2. Boundary folding: (SSN) Example 2. Boundary folding: (SSN) = = mod 1000 = mod 1000 = 566. Famous Examples of Hash Functions

27 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Mid-square function: key is squared and the middle part of the result is taken as the address. Mid-square function: key is squared and the middle part of the result is taken as the address. Example. k=3121, = , so h(k) = Example. k=3121, = , so h(k) = Famous Examples of Hash Functions

28 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Mid-square function: key is squared and the middle part of the result is taken as the address. Mid-square function: key is squared and the middle part of the result is taken as the address. Example. k=3121, = , so h(k) = 406. Example. k=3121, = , so h(k) = 406. You can also encode the square into binary representation and take the middle part. You can also encode the square into binary representation and take the middle part. Famous Examples of Hash Functions

29 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Mid-square function: key is squared and the middle part of the result is taken as the address. Mid-square function: key is squared and the middle part of the result is taken as the address. Extraction: Only a part of the key is used to compute the address. Extraction: Only a part of the key is used to compute the address. Example: , first 4 digits 1234, last 4 digits 6789 Example: , first 4 digits 1234, last 4 digits 6789 first 2 digits of 1234 last 2 digits of 6789 first 2 digits of 1234 last 2 digits of 6789 we have 1289 we have 1289 Famous Examples of Hash Functions

30 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Mid-square function: key is squared and the middle part of the result is taken as the address. Mid-square function: key is squared and the middle part of the result is taken as the address. Extraction: Only a part of the key is used to compute the address. Extraction: Only a part of the key is used to compute the address. Radix Transformation: k is transformed into another number base Radix Transformation: k is transformed into another number base Example: = 423 9, then 423 mod 100 = 23. Example: = 423 9, then 423 mod 100 = 23. Famous Examples of Hash Functions

31 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Mid-square function: key is squared and the middle part of the result is taken as the address. Mid-square function: key is squared and the middle part of the result is taken as the address. Extraction: Only a part of the key is used to compute the address. Extraction: Only a part of the key is used to compute the address. Radix Transformation: k is transformed into another number base Radix Transformation: k is transformed into another number base Example: = 423 9, then 423 mod 100 = 23. Example: = 423 9, then 423 mod 100 = = 323 9, then 323 mod 100 = = 323 9, then 323 mod 100 =23. Famous Examples of Hash Functions

32 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Mid-square function: key is squared and the middle part of the result is taken as the address. Mid-square function: key is squared and the middle part of the result is taken as the address. Extraction: Only a part of the key is used to compute the address. Extraction: Only a part of the key is used to compute the address. Radix Transformation: k is transformed into another number base Radix Transformation: k is transformed into another number base Example: = 423 9, then 423 mod 100 = 23. Example: = 423 9, then 423 mod 100 = = 323 9, then 323 mod 100 = = 323 9, then 323 mod 100 =23. Collision is hard to avoid in the worst case! Collision is hard to avoid in the worst case! Famous Examples of Hash Functions

33 Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Division: h(k) = k mod m, m should be a prime number, better close to a power of 2. Multiplication: h(k) = m(kA mod 1), Multiplication: h(k) = m(kA mod 1), A=(5 – 1)/2= A=(5 – 1)/2= Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Folding: The key is divided into several parts. These parts are combined or folded together and are transformed in a certain way to create the target address. Mid-square function: key is squared and the middle part of the result is taken as the address. Mid-square function: key is squared and the middle part of the result is taken as the address. Extraction: Only a part of the key is used to compute the address. Extraction: Only a part of the key is used to compute the address. Radix Transformation: k is transformed into another number base Radix Transformation: k is transformed into another number base Famous Examples of Hash Functions

34 In some applications, it is hard to dynamically allocate additional space for handling the chaining. In some applications, it is hard to dynamically allocate additional space for handling the chaining. So it is natural to come up with a different way to handle collision in which all elements are stored in the hash table itself. Then, instead of following pointers, we simply compute the sequences of slots to be examined. So it is natural to come up with a different way to handle collision in which all elements are stored in the hash table itself. Then, instead of following pointers, we simply compute the sequences of slots to be examined. Lets use insertion as an example. Lets use insertion as an example. Open Addressing

35 Lets use insertion as an example. Lets use insertion as an example. To perform insertion using open addressing, we successively examine or probe the hash table until we find an empty slot to put the element. Moreover, the sequence of positions probed depends on the key being inserted; i.e., Moreover, the sequence of positions probed depends on the key being inserted; i.e., h: U x {0,1,…,m-1} {0,1,…,m-1} h: U x {0,1,…,m-1} {0,1,…,m-1} Open Addressing

36 To perform insertion using open addressing, we successively examine or probe the hash table until we find an empty slot to put the element. To perform insertion using open addressing, we successively examine or probe the hash table until we find an empty slot to put the element. Moreover, the sequence of positions probed depends on the key being inserted; i.e., Moreover, the sequence of positions probed depends on the key being inserted; i.e., h: U x {0,1,…,m-1} {0,1,…,m-1} h: U x {0,1,…,m-1} {0,1,…,m-1} Apparently, for every key k, the probe sequence Apparently, for every key k, the probe sequence is a permutation of is a permutation of so that every position in the hash table is eventually considered as a slot for a new key as the table fills up. so that every position in the hash table is eventually considered as a slot for a new key as the table fills up. Now, for simplicity, assume k=x, and there is no deletion. Now, for simplicity, assume k=x, and there is no deletion. Open Addressing

37 Hash-Insert(T,k) 1. i 0 2. repeat j h(k,i) 3.if T[j] == Nil 4. then T[j] k 5. return j 6. else i i until i=m 8. error hash table overflow Open Addressing

38 Hash-Insert(T,k) 1. i 0 2. repeat j h(k,i) 3.if T[j] == Nil 4. then T[j] k 5. return j 6. else i i until i=m 8. error hash table overflow Open Addressing T Example. Insert keys 10,22,31,4,15,28,17,88,59 into T. h(k,i)=[h(k)+i] mod m, h(k)=k mod m.

39 Hash-Insert(T,k) 1. i 0 2. repeat j h(k,i) 3.if T[j] == Nil 4. then T[j] k 5. return j 6. else i i until i=m 8. error hash table overflow Open Addressing T Example. Insert keys 10,22,31,4,15,28,17,88,59 into T. h(k,i)=[h(k)+i] mod m, h(k)=k mod m. h(10,0)=(10+0) mod 11 = 10 10

40 Hash-Insert(T,k) 1. i 0 2. repeat j h(k,i) 3.if T[j] == Nil 4. then T[j] k 5. return j 6. else i i until i=m 8. error hash table overflow Open Addressing T Example. Insert keys 10,22,31,4,15,28,17,88,59 into T. h(k,i)=[h(k)+i] mod m, h(k)=k mod m. h(10,0)=(10+0) mod 11 = 10 h(22,0)= 0 h(31,0)=9 h(4,0)=4 h(15,0)=(4+0) mod 11 =

41 Hash-Insert(T,k) 1. i 0 2. repeat j h(k,i) 3.if T[j] == Nil 4. then T[j] k 5. return j 6. else i i until i=m 8. error hash table overflow Open Addressing T Example. Insert keys 10,22,31,4,15,28,17,88,59 into T. h(k,i)=[h(k)+i] mod m, h(k)=k mod m. h(10,0)=(10+0) mod 11 = 10 h(22,0)= 0 h(31,0)=9 h(4,0)=4 h(15,0)=(4+0) mod 11 =4 h(15,1)=(4+1) mod 11 =

42 Hash-Insert(T,k) 1. i 0 2. repeat j h(k,i) 3.if T[j] == Nil 4. then T[j] k 5. return j 6. else i i until i=m 8. error hash table overflow Open Addressing T Example. Insert keys 10,22,31,4,15,28,17,88,59 into T. h(k,i)=[h(k)+i] mod m, h(k)=k mod m

43 Hash-Search(T,k) 1. i 0 2. repeat j h(k,i) 3.if T[j] == k 4. then return j 5.i i until T[j]=Nil or i=m 7. return Nil Open Addressing T Example. Search 15 in T. h(k,i)=[h(k)+i] mod m, h(k)=k mod m i = 0 j h(15,0)=4 T[j] != 15 i = 1 j h(15,1)=5 T[j] = 15 return 5

44 How about deletion? You can simply use Hash-Search to find the key first. Then what? 1. i 0 2. repeat j h(k,i) 3.if T[j] != Nil and T[j]==k 4. then T[j] Nil? exit 5.i i until T[j]=Nil or i=m Open Addressing T Example. Delete 4,15 in T. h(k,i)=[h(k)+i] mod m, h(k)=k mod m

45 How about deletion? You can simply use Hash-Search to find the key first. Then what? 1. i 0 2. repeat j h(k,i) 3.if T[j] != Nil and T[j] == k 4. then T[j] Nil?, exit 5.i i until T[j]=Nil or i=m Open Addressing T Example. Delete 4,15 in T. h(k,i)=[h(k)+i] mod m, h(k)=k mod m Nil Delete 15: i = 0 j h(15,0)=4 T[j] = Nil exit

46 How about deletion? You can simply use Hash-Search to find the key first. 1. i 0 2. repeat j h(k,i) 3.if T[j] != Nil and T[j] == k 4. then T[j] deleted, exit 5.i i until T[j]=Nil or i=m Open Addressing T Example. Delete 4,15 in T. h(k,i)=[h(k)+i] mod m, h(k)=k mod m deleted Delete 15: i = 0 j h(15,0)=4 T[j] = deleted i = 1 j h(15,1)=5 T[j]=15

47 How about deletion? You can simply use Hash-Search to find the key first. 1. i 0 2. repeat j h(k,i) 3.if T[j] != Nil and T[j] == k 4. then T[j] deleted, exit 5.i i until T[j]=Nil or i=m Open Addressing T Example. Delete 4,15 in T. h(k,i)=[h(k)+i] mod m, h(k)=k mod m Delete 15: i = 0 j h(15,0)=4 T[j] = deleted i = 1 j h(15,1)=5 T[j]=15 15 is deleted! deleted

48 That is what we have just seen. h is an ordinary hash function; i.e., h: U {0,1,2,…,m-1} h(k,i) = [h(k) + i] mod m. Initial slot probed is exactly T[h(k)]. Linear probing

49 h is an ordinary hash function; i.e., h: U {0,1,2,…,m-1} h(k,i) = [h(k) + C 1 i + C 2 i 2 ] mod m, C 1, C 2 are two non-zero constants. Initial slot probed is also T[h(k)], but when i>0 it is intuitively better than linear probing. Quadratic probing

50 Example. Insert keys 10,22,31,4,15,28,17,88, 59 into T. h(k,i)=[h(k)+C 1 i + C 2 i 2 ] mod m, h(k)=k mod m, C 1 =1, C 2 =3. Quadratic probing T

51 Example. Insert keys 10,22,31,4,15,28,17,88,5 9 into T. h(k,i)=[h(k)+C 1 i + C 2 i 2 ] mod m, h(k)=k mod m, C 1 =1, C 2 =3. Quadratic probing T h(10,0)=10 h(22,0)=0 h(31,0)=9 h(4,0)=4 h(15,0)=4

52 Example. Insert keys 10,22,31,4,15,28,17,88,5 9 into T. h(k,i)=[h(k)+C 1 i + C 2 i 2 ] mod m, h(k)=k mod m, C 1 =1, C 2 =3. Quadratic probing T h(10,0)=10 h(22,0)=0 h(31,0)=9 h(4,0)=4 h(15,0)=4 h(15,1)=[4+1+3] mod 11 = 8 h(28,0)=6

53 Example. Insert keys 10,22,31,4,15,28,17,88,5 9 into T. h(k,i)=[h(k)+C 1 i + C 2 i 2 ] mod m, h(k)=k mod m, C 1 =1, C 2 =3. Quadratic probing T h(17,0)=6 h(17,1)=10 h(17,2)=[6+2+3x2 2 ] mod 11 = 9 h(17,3)=[6+3+3x3 2 ] mod 11 =

54 Example. Insert keys 10,22,31,4,15,28,17,88,5 9 into T. h(k,i)=[h(k)+C 1 i + C 2 i 2 ] mod m, h(k)=k mod m, C 1 =1, C 2 =3. Quadratic probing T

55 Double probing h(k,i) = [h 1 (k) + ih 2 (k)] mod m, h 1, h 2 are two auxiliary hash functions. Initial slot probed is also T[h 1 (k)].

56 Example. Insert keys 10,22,31,4,15,28,17,88,5 9 into T. h(k,i)=[h 1 (k)+ih 2 (k)] mod m, h 1 (k)=k mod m, h 2 (k)=1 + [k mod (m-1)]. Double probing T

57 Example. Insert keys 10,22,31,4,15,28,17,88,5 9 into T. h(k,i)=[h 1 (k)+ih 2 (k)] mod m, h 1 (k)=k mod m, h 2 (k)=1 + [k mod (m-1)]. Double probing T h(10,0)=10 h(22,0)=0 h(31,0)=9 h(4,0)=4 h(15,0)=4 h(15,1)=[4+1x6] mod 11 = 10 h(15,2)=[4+2x6] mod 11 = 5

58 Example. Insert keys 10,22,31,4,15,28,17,88,5 9 into T. h(k,i)=[h 1 (k)+ih 2 (k)] mod m, h 1 (k)=k mod m, h 2 (k)=1 + [k mod (m-1)]. Double probing T h(28,0)=6 h(17,0)=6 h(17,1)=[6+1x8] mod 11 = 3 … h(88,2)=[0+2x9] mod 11 = 7 … h(59,2)=[4+2x10] mod 11 =

59 Example. Insert keys 10,22,31,4,15,28,17,88,5 9 into T. h(k,i)=[h 1 (k)+ih 2 (k)] mod m, h 1 (k)=k mod m, h 2 (k)=1 + [k mod (m-1)]. Double probing T h(28,0)=6 h(17,0)=6 h(17,1)=[6+1x8] mod 11 = 3 … h(88,2)=[0+2x9] mod 11 = 7 … h(59,2)=[4+2x10] mod 11 =

60 Analysis of hashing (in general tough) In a hash table with size m, we want to store n elements with collision resolved by chaining. In a hash table with size m, we want to store n elements with collision resolved by chaining. Load Factor α = n/m. Load Factor α = n/m. Theorem: An unsuccessful search takes expected time O(1+α), under the assumption of simple uniform hashing. Theorem: An unsuccessful search takes expected time O(1+α), under the assumption of simple uniform hashing.