Professional Documents
Culture Documents
201 DSA Chapter 7 Hash Search New
201 DSA Chapter 7 Hash Search New
201 DSA Chapter 7 Hash Search New
Hash table
Basic concepts
Data Structures and Algorithms Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.1
Search + Hash
Overview
Tran Ngoc Bao Duy
1 Searching algorithms
Searching algorithms
Sequential Search Sequential Search
Interval Search
Interval Search
Hash table
Basic concepts
Direct-address tables
Hash tables
2 Hash table Hash functions
Open addressing
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.2
Search + Hash
Searching algorithms
Sequential Search
Interval Search
ALGORITHMS
Hash tables
Hash functions
Open addressing
Search + Hash.3
Search + Hash
Searching algorithms
Tran Ngoc Bao Duy
ment or retrieve an element from any data structure where Hash table
Search + Hash.4
Search + Hash
Linear Search
Tran Ngoc Bao Duy
Searching algorithms
Approach Sequential Search
Interval Search
1 Start from the leftmost element of list and one by one Hash table
compare x with each element of list. Basic concepts
Direct-address tables
Hash tables
Hash functions
2 If x matches with an element, return the index. Open addressing
Search + Hash.5
Search + Hash
Linear Search
Tran Ngoc Bao Duy
Searching algorithms
Approach Sequential Search
Interval Search
1 Start from the leftmost element of list and one by one Hash table
compare x with each element of list. Basic concepts
Direct-address tables
Hash tables
Hash functions
2 If x matches with an element, return the index. Open addressing
Search + Hash.5
Search + Hash
Binary Search
Tran Ngoc Bao Duy
Approach
Search a sorted array by repeatedly dividing the search in-
terval in half. Begin with an interval covering the whole Searching algorithms
Sequential Search
array. Interval Search
Hash table
1 If the value of the search key is less than the item in Basic concepts
the middle of the interval, narrow the interval to the Direct-address tables
Hash tables
Search + Hash.6
Search + Hash
Binary Search
Tran Ngoc Bao Duy
Approach
Search a sorted array by repeatedly dividing the search in-
terval in half. Begin with an interval covering the whole Searching algorithms
Sequential Search
array. Interval Search
Hash table
1 If the value of the search key is less than the item in Basic concepts
the middle of the interval, narrow the interval to the Direct-address tables
Hash tables
Implemetation:
• Recursive
• Iterative
Time complexity: O(log n)
Search + Hash.6
Search + Hash
Jump Search
Tran Ngoc Bao Duy
Approach
Jump Search is a searching algorithm for sorted arrays. The Searching algorithms
basic idea is to check fewer elements (than linear search) by Sequential Search
Interval Search
jumping ahead by fixed steps or skipping some elements in Hash table
place of searching all elements. Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.7
Search + Hash
Jump Search
Tran Ngoc Bao Duy
Approach
Jump Search is a searching algorithm for sorted arrays. The Searching algorithms
basic idea is to check fewer elements (than linear search) by Sequential Search
Interval Search
jumping ahead by fixed steps or skipping some elements in Hash table
place of searching all elements. Basic concepts
Direct-address tables
Hash tables
Suppose that an array arr of size n divided to some blocks Hash functions
Search + Hash.7
Search + Hash
Jump Search
Tran Ngoc Bao Duy
Approach
Jump Search is a searching algorithm for sorted arrays. The Searching algorithms
basic idea is to check fewer elements (than linear search) by Sequential Search
Interval Search
jumping ahead by fixed steps or skipping some elements in Hash table
place of searching all elements. Basic concepts
Direct-address tables
Hash tables
Suppose that an array arr of size n divided to some blocks Hash functions
Search + Hash.7
Search + Hash
Jump Seach
Tran Ngoc Bao Duy
Search + Hash.8
Search + Hash
Interpolation Seach
Tran Ngoc Bao Duy
Approach
Interpolation Search is an improvement over Binary Search
for instances, where the values in a sorted array are uniformly
distributed. Searching algorithms
Sequential Search
1 Calculate the value of pos using the probe position Interval Search
where
• arr: array where elements need to be searched.
• x: element to be searched.
• lo: starting index in arr.
• hi: ending index in arr.
Search + Hash.9
Search + Hash
Interpolation Seach
Tran Ngoc Bao Duy
Approach
Interpolation Search is an improvement over Binary Search
for instances, where the values in a sorted array are uniformly
distributed. Searching algorithms
Sequential Search
1 Calculate the value of pos using the probe position Interval Search
Hash table
formula. Basic concepts
Direct-address tables
2 If it is a match, return the index of the item, and exit. Hash tables
Hash functions
Open addressing
Time complexity:
• If elements are uniformly distributed, then O(log log n)).
• In worst case, it can take up to O(n).
Search + Hash.9
Search + Hash
Interpolation Seach
Tran Ngoc Bao Duy
Approach
Interpolation Search is an improvement over Binary Search
for instances, where the values in a sorted array are uniformly
distributed. Searching algorithms
Sequential Search
1 Calculate the value of pos using the probe position Interval Search
Hash table
formula. Basic concepts
Direct-address tables
2 If it is a match, return the index of the item, and exit. Hash tables
Hash functions
3 If the item is less than arr[pos], calculate the probe Open addressing
Time complexity:
• If elements are uniformly distributed, then O(log log n)).
• In worst case, it can take up to O(n).
Search + Hash.9
Search + Hash
Interpolation Seach
Tran Ngoc Bao Duy
Approach
Interpolation Search is an improvement over Binary Search
for instances, where the values in a sorted array are uniformly
distributed. Searching algorithms
Sequential Search
1 Calculate the value of pos using the probe position Interval Search
Hash table
formula. Basic concepts
Direct-address tables
2 If it is a match, return the index of the item, and exit. Hash tables
Hash functions
3 If the item is less than arr[pos], calculate the probe Open addressing
Time complexity:
• If elements are uniformly distributed, then O(log log n)).
• In worst case, it can take up to O(n).
Search + Hash.9
Search + Hash
Interpolation Search
Tran Ngoc Bao Duy
Search the element 18 in array [10, 12, 13, 16, 18, 19, 20,
21, 22, 23, 24, 33, 35, 42, 47].
1 Calculate the probe position: Searching algorithms
Sequential Search
Interval Search
Search + Hash.10
Search + Hash
Interpolation Search
Tran Ngoc Bao Duy
Search the element 18 in array [10, 12, 13, 16, 18, 19, 20,
21, 22, 23, 24, 33, 35, 42, 47].
1 Calculate the probe position: Searching algorithms
Sequential Search
Interval Search
Search + Hash.10
Search + Hash
Interpolation Search
Tran Ngoc Bao Duy
Search the element 18 in array [10, 12, 13, 16, 18, 19, 20,
21, 22, 23, 24, 33, 35, 42, 47].
1 Calculate the probe position: Searching algorithms
Sequential Search
Interval Search
Search + Hash.10
Search + Hash
Interpolation Search
Tran Ngoc Bao Duy
Search the element 18 in array [10, 12, 13, 16, 18, 19, 20,
21, 22, 23, 24, 33, 35, 42, 47].
1 Calculate the probe position: Searching algorithms
Sequential Search
Interval Search
Search + Hash.10
Search + Hash
Searching algorithms
Sequential Search
Interval Search
Hash table
Search + Hash.11
Search + Hash
Basic concepts
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Hash table
• Binary search: O(log2 n) Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.12
Search + Hash
Basic concepts
Tran Ngoc Bao Duy
Search complexity:
Searching algorithms
Size Binary Sequential Sequential Sequential Search
Hash table
16 4 8 16 Basic concepts
Direct-address tables
50 6 25 50 Hash tables
Hash functions
256 8 128 256 Open addressing
Search + Hash.13
Search + Hash
Basic concepts
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
Is there a search algorithm whose complexity is O(1)? Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.14
Search + Hash
Basic concepts
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
Is there a search algorithm whose complexity is O(1)? Basic concepts
Direct-address tables
YES
Hash tables
Hash functions
Open addressing
Search + Hash.14
Search + Hash
Basic concepts
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.15
Search + Hash
Basic concepts
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.16
Search + Hash
Direct-address tables
Tran Ngoc Bao Duy
Definition
Direct-address table is a structure using an array denoted
by T [0 . . . m − 1], in which each position, or slot, corre-
Searching algorithms
sponds to a key in the universe U . Sequential Search
Interval Search
Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.17
Search + Hash
Direct-address tables
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.18
Search + Hash
Direct-address tables
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.18
Search + Hash
Downside of direct addressing
Tran Ngoc Bao Duy
Search + Hash.19
Search + Hash
Hash tables
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Definition
Hash table
Hash table is a dynamic set that supports only the dic- Basic concepts
Direct-address tables
tionary operations INSERT, SEARCH, and DELETE with the Hash tables
Hash functions
average time to search for an element O(1) but requires Open addressing
Search + Hash.20
Search + Hash
Hash tables
Tran Ngoc Bao Duy
Definition
Hash table is a dynamic set that supports only the dic-
tionary operations INSERT, SEARCH, and DELETE with the Searching algorithms
average time to search for an element O(1) but requires Sequential Search
Interval Search
much less storage than the universe U of all possible keys. Hash table
Basic concepts
h : U → {0, 1, . . . , m − 1}
where m |U |.
Search + Hash.20
Search + Hash
Hash tables
Tran Ngoc Bao Duy
Definition
Hash table is a dynamic set that supports only the dic-
tionary operations INSERT, SEARCH, and DELETE with the Searching algorithms
average time to search for an element O(1) but requires Sequential Search
Interval Search
much less storage than the universe U of all possible keys. Hash table
Basic concepts
h : U → {0, 1, . . . , m − 1}
where m |U |.
Search + Hash.20
Search + Hash
Hash tables
Tran Ngoc Bao Duy
Definition
Hash table is a dynamic set that supports only the dic-
tionary operations INSERT, SEARCH, and DELETE with the
Searching algorithms
average time to search for an element O(1) but requires Sequential Search
much less storage than the universe U of all possible keys. Interval Search
Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.20
Search + Hash
Collision
Tran Ngoc Bao Duy
There is one hitch: two keys may hash to the Searching algorithms
Search + Hash.21
Search + Hash
Collision
Tran Ngoc Bao Duy
There is one hitch: two keys may hash to the Searching algorithms
Search + Hash.21
Search + Hash
Collision
Tran Ngoc Bao Duy
There is one hitch: two keys may hash to the Searching algorithms
Search + Hash.21
Search + Hash
Collision resolution by chaining
Tran Ngoc Bao Duy
Definition
In chaining, we place all the elements that hash to the Searching algorithms
Sequential Search
Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.22
Search + Hash
Collision resolution by chaining
Tran Ngoc Bao Duy
In chaining, we place all the elements that hash to the Sequential Search
Interval Search
Search + Hash.22
Search + Hash
Collision resolution by chaining
Tran Ngoc Bao Duy
Definition
In chaining, we place all the elements that hash to the Searching algorithms
Sequential Search
same slot into the same linked list. Interval Search
Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.22
Search + Hash
Searching algorithms
Sequential Search
Interval Search
Hash table
Search + Hash.23
Search + Hash
Interpreting keys as natural numbers
Tran Ngoc Bao Duy
• If the keys are not natural numbers, we find a way Hash table
Basic concepts
to interpret them as natural numbers. Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.24
Search + Hash
Interpreting keys as natural numbers
Tran Ngoc Bao Duy
• If the keys are not natural numbers, we find a way Hash table
Basic concepts
to interpret them as natural numbers. Direct-address tables
Hash tables
Hash functions
Search + Hash.24
Search + Hash
Interpreting keys as natural numbers
Tran Ngoc Bao Duy
• If the keys are not natural numbers, we find a way Hash table
Basic concepts
to interpret them as natural numbers. Direct-address tables
Hash tables
Hash functions
Search + Hash.24
Search + Hash
A good hash function
Tran Ngoc Bao Duy
Search + Hash.25
Search + Hash
A good hash function
Tran Ngoc Bao Duy
• Multiplication method
• Universal hashing
• Other methods
Search + Hash.25
Search + Hash
Division method
Tran Ngoc Bao Duy
Definition
In the division method for creating hash functions, we map
Searching algorithms
a key k into one of m slots by taking the remainder of k Sequential Search
Hash table
Basic concepts
Search + Hash.26
Search + Hash
Division method
Tran Ngoc Bao Duy
Definition
In the division method for creating hash functions, we map
Searching algorithms
a key k into one of m slots by taking the remainder of k Sequential Search
Hash table
Basic concepts
Example
If the hash table has size m = 12 and the key is k = 100,
then h(k) = 4
Search + Hash.26
Search + Hash
Division method
Tran Ngoc Bao Duy
Definition
In the division method for creating hash functions, we map
Searching algorithms
a key k into one of m slots by taking the remainder of k Sequential Search
Hash table
Basic concepts
Example
If the hash table has size m = 12 and the key is k = 100,
then h(k) = 4
Value of m:
• m should not be a power of 2.
• m should be a prime not too close to an exact power of 2.
Search + Hash.26
Search + Hash
Multiplication method
Tran Ngoc Bao Duy
Definition
Searching algorithms
The multiplication method for creating hash functions op- Sequential Search
Hash table
1 Multiply the key k by a constant A in the range (0, 1) Basic concepts
Direct-address tables
and extract the fractional part of kA. Hash tables
Hash functions
2 Multiply this value by m and take the floor of the result. Open addressing
Search + Hash.27
Search + Hash
Digit extraction
Tran Ngoc Bao Duy
Searching algorithms
Hash table
Basic concepts
121267→112
378845→388
160252→102
045128→051
Search + Hash.28
Search + Hash
Mid-square
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
Search + Hash.29
Search + Hash
Mid-square
Tran Ngoc Bao Duy
Hash table
Search + Hash.30
Search + Hash
Folding
Tran Ngoc Bao Duy
Hash table
Search + Hash.31
Search + Hash
Folding
Tran Ngoc Bao Duy
Hash table
fold boundary
321 + 456 + 987 = 1764
→ 764
Search + Hash.31
Search + Hash
Rotation
Tran Ngoc Bao Duy
600102 260010
600103 360010
600104 460010
600105 560010
Search + Hash.32
Search + Hash
Rotation
Tran Ngoc Bao Duy
Hash table
600103 → 64 360010 → 46
Hash tables
Hash functions
Open addressing
600104 → 65 460010 → 56
600105 → 66 560010 → 66
Search + Hash.33
Search + Hash
Pseudo-random
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.34
Search + Hash
Pseudo-random
Tran Ngoc Bao Duy
Example:
Searching algorithms
Key = 121267 Sequential Search
Interval Search
a = 17 Hash table
Basic concepts
Search + Hash.35
Search + Hash
Open addressing
Tran Ngoc Bao Duy
Hash table
Basic concepts
Hash function: Direct-address tables
Search + Hash.36
Search + Hash
Open addressing
Tran Ngoc Bao Duy
Hash table
Basic concepts
Hash function: Direct-address tables
Search + Hash.36
Search + Hash
Open Addressing
Tran Ngoc Bao Duy
Hash table
5 j = h(k, i) Basic concepts
8 return j
9 else
10 i=i+1
11 end
12 end
13 return error: “hash table overflow”
14 End hashInsert
Search + Hash.37
Search + Hash
Open Addressing
Tran Ngoc Bao Duy
9 return nil
10 else
11 i=i+1
12 end
13 end
14 return nil
15 End hashSearch
Search + Hash.38
Search + Hash
Linear probing
Tran Ngoc Bao Duy
Hash table
h(k, i) = h0 (k) + ci
Basic concepts
mod m Direct-address tables
Hash tables
Search + Hash.39
Search + Hash
Linear probing
Tran Ngoc Bao Duy
Hash table
h(k, i) = h0 (k) + ci
Basic concepts
mod m Direct-address tables
Hash tables
Example
h0 (k) = k mod 11
h(k, i) = (h0 (k) + i) mod m
Insert 10, 22, 31, 4, 15, 28, 17, 88, 59 into hash table.
Search + Hash.39
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 10. Sequential Search
Interval Search
10 10 10 10 10 10 10 10 10 10 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 22. Sequential Search
Interval Search
22 10 10 10 10 10 10 10 10 10 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 31. Sequential Search
Interval Search
22 10 10 10 10 10 10 10 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 4. Sequential Search
Interval Search
22 10 10 10 4 10 10 10 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 15. Sequential Search
Interval Search
22 10 10 10 4 10 10 10 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 15. Sequential Search
22 10 10 10 4 15 10 10 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 28. Sequential Search
Interval Search
22 10 10 10 4 15 28 10 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 17. Sequential Search
Interval Search
22 10 10 10 4 15 28 10 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 17. Sequential Search
22 10 10 10 4 15 28 17 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 88. Sequential Search
Interval Search
22 10 10 10 4 15 28 17 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 88. Sequential Search
22 88 10 10 4 15 28 17 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 59. Sequential Search
Interval Search
22 88 10 10 4 15 28 17 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 59. Sequential Search
22 88 10 10 4 15 28 17 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
22 88 10 10 4 15 28 17 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Insert 59.
Searching algorithms
h(59, 0) = (h0 (59) + 0) mod 11 = 4. Collission. Sequential Search
0 1 2 3 4 5 6 7 8 9 10
22 88 10 10 4 15 28 17 10 31 10
Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy
Insert 59.
h(59, 0) = (h0 (59) + 0) mod 11 = 4. Collission. Searching algorithms
Sequential Search
h(59, 1) = (h0 (59) + 1) mod 11 = 5. Collission. Interval Search
0 1 2 3 4 5 6 7 8 9 10
22 88 10 10 4 15 28 17 59 31 10
Search + Hash.40
Search + Hash
Linear probing: Evaluation
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
• Easy to implement. Basic concepts
Direct-address tables
Hash tables
Search + Hash.41
Search + Hash
Quadratic probing
Tran Ngoc Bao Duy
Hash table
where h0 is an auxiliary hash function, c1 and c2 are positive Basic concepts
Direct-address tables
auxiliary constants, and i = 0, 1, . . . , m − 1. Hash tables
Hash functions
Open addressing
Search + Hash.42
Search + Hash
Quadratic probing
Tran Ngoc Bao Duy
Hash table
where h0 is an auxiliary hash function, c1 and c2 are positive Basic concepts
Direct-address tables
auxiliary constants, and i = 0, 1, . . . , m − 1. Hash tables
Hash functions
Open addressing
Example
h0 (k) = k mod 11
c1 = 1, c2 = 3 ⇒ h(k, i) = (h0 (k) + i + 3i2 ) mod 11
Insert 10, 22, 31, 4, 15, 28, 17, 88, 59 into hash table.
Search + Hash.42
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 10. Sequential Search
Interval Search
10 10 10 10 10 10 10 10 10 10 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 22. Sequential Search
Interval Search
22 10 10 10 10 10 10 10 10 10 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 31. Sequential Search
Interval Search
22 10 10 10 10 10 10 10 10 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 4. Sequential Search
Interval Search
22 10 10 10 4 10 10 10 10 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 15. Sequential Search
Interval Search
22 10 10 10 4 10 10 10 10 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 15. Sequential Search
22 10 10 10 4 10 10 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 28. Sequential Search
Interval Search
22 10 10 10 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 17. Sequential Search
Interval Search
22 10 10 10 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 17. Sequential Search
22 10 10 10 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
22 10 10 10 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Insert 17.
Searching algorithms
h(17, 0) = (h0 (17) + 0) mod 11 = 6. Collission. Sequential Search
Hash table
h(17, 2) = (h0 (17) + 2 + 3 × 22 ) mod 11 = 9. Collission. Basic concepts
0 1 2 3 4 5 6 7 8 9 10
22 10 10 17 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 88. Sequential Search
Interval Search
22 10 10 17 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 88. Sequential Search
22 10 10 17 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
22 10 10 17 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Insert 88.
Searching algorithms
h(88, 0) = (h0 (88) + 0) mod 11 = 0. Collission. Sequential Search
Hash table
h(88, 2) = (h0 (88) + 2 + 3 × 22 ) mod 11 = 3. Collission. Basic concepts
0 1 2 3 4 5 6 7 8 9 10
22 10 10 17 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Insert 88.
h(88, 0) = (h0 (88) + 0) mod 11 = 0. Collission.
h(88, 1) = (h0 (88) + 1 + 3 × 12 ) mod 11 = 4. Collission. Searching algorithms
. Open addressing
0 1 2 3 4 5 6 7 8 9 10
22 10 10 17 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Insert 88.
h(88, 0) = (h0 (88) + 0) mod 11 = 0. Collission.
h(88, 1) = (h0 (88) + 1 + 3 × 12 ) mod 11 = 4. Collission.
Searching algorithms
h(88, 2) = (h0 (88) + 2 + 3 × 22 ) mod 11 = 3. Collission. Sequential Search
Hash table
h(88, 4) = (h0 (88) + 4 + 3 × 42 ) mod 11 = 8. Collission. Basic concepts
Direct-address tables
. Hash tables
. Hash functions
Open addressing
.
h(88, 8) = (h0 (88) + 8 + 3 × 82 ) mod 11 = 2.
0 1 2 3 4 5 6 7 8 9 10
22 10 88 17 4 10 28 10 15 31 10
Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy
Searching algorithms
Insert 59.
.. Sequential Search
. Interval Search
Hash table
h(59, 2) = (h0 (59) + 2 + 3 × 22 ) mod 11 = 7. Basic concepts
Direct-address tables
Hash tables
Hash functions
0 1 2 3 4 5 6 7 8 9 10 Open addressing
22 10 88 17 4 10 28 59 15 31 10
Search + Hash.43
Search + Hash
Quadratic probing: Evaluation
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
• Easy to implement. Basic concepts
Direct-address tables
Hash tables
Search + Hash.44
Search + Hash
Double hashing
Tran Ngoc Bao Duy
tions. Double hashing uses a hash function of the form Interval Search
Hash table
Basic concepts
Search + Hash.45
Search + Hash
Double hashing
Tran Ngoc Bao Duy
tions. Double hashing uses a hash function of the form Interval Search
Hash table
Basic concepts
Example
h1 (k) = k mod 11
h2 (k) = 1 + (k mod 10)
h(k, i) = (h1 (k) + ih2 (k)) mod 11
Insert 10, 22, 31, 4, 15, 28, 17, 88, 59 into hash table.
Search + Hash.45
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Searching algorithms
Insert 10. Sequential Search
Interval Search
10 10 10 10 10 10 10 10 10 10 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Searching algorithms
Insert 22. Sequential Search
Interval Search
22 10 10 10 10 10 10 10 10 10 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Searching algorithms
Insert 31. Sequential Search
Interval Search
22 10 10 10 10 10 10 10 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Searching algorithms
Insert 4. Sequential Search
Interval Search
22 10 10 10 4 10 10 10 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Searching algorithms
Insert 15. Sequential Search
Interval Search
22 10 10 10 4 10 10 10 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
22 10 10 10 4 10 10 10 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Insert 15.
Searching algorithms
h(15, 0) = (h1 (15) + 0 × h2 (15)) mod 11 = 4. Collission. Sequential Search
Interval Search
h(15, 1) = (h1 (10) + 1 × h2 (10)) mod 11 = 10.
Hash table
Collission. Basic concepts
Direct-address tables
h(15, 2) = (h1 (10) + 2 × h2 (10)) mod 11 = 5. Hash tables
Hash functions
Open addressing
0 1 2 3 4 5 6 7 8 9 10
22 10 10 10 4 15 10 10 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Searching algorithms
Insert 28. Sequential Search
Interval Search
22 10 10 10 4 15 28 10 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Searching algorithms
Insert 17. Sequential Search
Hash table
Collission. Basic concepts
Direct-address tables
Hash tables
0 1 2 3 4 5 6 7 8 9 10 Hash functions
Open addressing
22 10 10 10 4 15 28 10 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
22 10 17 10 4 15 28 10 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Searching algorithms
Insert 88. Sequential Search
Hash table
Collission. Basic concepts
Direct-address tables
Hash tables
0 1 2 3 4 5 6 7 8 9 10 Hash functions
Open addressing
22 10 17 10 4 15 28 10 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
0 1 2 3 4 5 6 7 8 9 10
22 10 17 10 4 15 28 10 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Insert 88.
h(88, 0) = (h1 (88) + 0 × h2 (88)) mod 11 = 0. Searching algorithms
Sequential Search
Collission. Interval Search
0 1 2 3 4 5 6 7 8 9 10
22 10 17 10 4 15 28 88 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
Searching algorithms
Insert 59. Sequential Search
Hash table
Collission. Basic concepts
Direct-address tables
Hash tables
0 1 2 3 4 5 6 7 8 9 10 Hash functions
Open addressing
22 10 17 10 4 15 28 88 10 31 10
Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy
22 10 17 59 4 15 28 88 10 31 10
Search + Hash.46
Search + Hash
Bucket hashing
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Search + Hash.47
Search + Hash
Bucket hashing
Tran Ngoc Bao Duy
Searching algorithms
Sequential Search
Interval Search
Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Search + Hash.48
Search + Hash
Searching algorithms
Sequential Search
Interval Search
Hash table
Search + Hash.49