201 DSA Chapter 7 Hash Search New

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 124

Search + Hash

Tran Ngoc Bao Duy

Searching and Hash structure Searching algorithms


Sequential Search
Interval Search

Hash table
Basic concepts
Data Structures and Algorithms Direct-address tables
Hash tables
Hash functions
Open addressing

Tran Ngoc Bao Duy


Faculty of Computer Science and Engineering
Ho Chi Minh University of Technology, VNU-HCM

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

Tran Ngoc Bao Duy

Searching algorithms
Sequential Search
Interval Search

SEARCHING Hash table


Basic concepts
Direct-address tables

ALGORITHMS
Hash tables
Hash functions
Open addressing

Search + Hash.3
Search + Hash
Searching algorithms
Tran Ngoc Bao Duy

Definition Searching algorithms

Searching Algorithms are designed to check for an ele- Sequential Search


Interval Search

ment or retrieve an element from any data structure where Hash table

it is stored. Based on the type of search operation, these Basic concepts


Direct-address tables

algorithms are generally classified into two categories: Hash tables


Hash functions
Open addressing
1 Sequential Search: In this, the list or array is traversed
sequentially and every element is checked.
2 Interval Search: These algorithms are specifically de-
signed for searching in sorted data-structures.

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

3 If x doesn’t match with any of elements, return -1.

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

3 If x doesn’t match with any of elements, return -1.

The time complexity of the above algorithm is O(n).

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

lower half, otherwise narrow it to the upper half. Hash functions


Open addressing

2 Repeatedly check until the value is found or the interval


is empty.

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

lower half, otherwise narrow it to the upper half. Hash functions


Open addressing

2 Repeatedly check until the value is found or the interval


is empty.

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

with fixed size m. Open addressing

1 Find the block k such that the first element of block


k is less than key and the first element of block k + 1
is greater than or equals to key.
2 Perform the linear search on the block k.

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

with fixed size m. Open addressing

1 Find the block k such that the first element of block


k is less than key and the first element of block k + 1
is greater than or equals to key.
2Perform the linear search on the block k.

Time complexity: n.

Search + Hash.7
Search + Hash
Jump Seach
Tran Ngoc Bao Duy

Consider the following array:


Searching algorithms
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]. Sequential Search
Interval Search
Length of the array is 16, block size is 4.
Hash table
1 Jump from index 0 to index 4. Basic concepts
Direct-address tables

2 Jump from index 4 to index 8. Hash tables


Hash functions
Open addressing
3 Jump from index 8 to index 12.
4 Since the element at index 12 is greater than 55 we
will jump back a step to come to index 8.
5 Do linear search from index 8 to get the element 55.

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

formula. Hash table


Basic concepts
Direct-address tables

(x − arr[lo]) × (hi − lo) Hash tables

pos = lo + Hash functions

arr[hi] − arr[lo] Open addressing

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

position of the left sub-array. Otherwise calculate the


same in the right sub-array.

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

position of the left sub-array. Otherwise calculate the


same in the right sub-array.
4 Repeat until a match is found or the sub-array
reduces to zero.

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

(18 − 10) × (14 − 0) Hash table


pos = 0 + =3
47 − 10 Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing

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

(18 − 10) × (14 − 0) Hash table


pos = 0 + =3
47 − 10 Basic concepts
Direct-address tables
Hash tables

2 arr[3] = 16 > 18 = x ⇒ Search from index = 4 to Hash functions


Open addressing

the end index = 14.

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

(18 − 10) × (14 − 0) Hash table


pos = 0 + =3
47 − 10 Basic concepts
Direct-address tables
Hash tables

2 arr[3] = 16 > 18 = x ⇒ Search from index = 4 to Hash functions


Open addressing

the end index = 14.


3 Calculate the probe position:

(18 − 18) × (14 − 4)


pos = 4 + =4
47 − 18

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

(18 − 10) × (14 − 0) Hash table


pos = 0 + =3
47 − 10 Basic concepts
Direct-address tables
Hash tables

2 arr[3] = 16 > 18 = x ⇒ Search from index = 4 to Hash functions


Open addressing

the end index = 14.


3 Calculate the probe position:

(18 − 18) × (14 − 4)


pos = 4 + =4
47 − 18
4 arr[4] = 18 = x ⇒ End.

Search + Hash.10
Search + Hash

Tran Ngoc Bao Duy

Searching algorithms
Sequential Search
Interval Search

Hash table

HASH TABLE Basic concepts


Direct-address tables
Hash tables
Hash functions
Open addressing

Search + Hash.11
Search + Hash
Basic concepts
Tran Ngoc Bao Duy

Searching algorithms
Sequential Search

• Sequential search: O(n) Interval Search

Hash table
• Binary search: O(log2 n) Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing

→ Requiring several key comparisons before


the target is found.

Search + Hash.12
Search + Hash
Basic concepts
Tran Ngoc Bao Duy

Search complexity:
Searching algorithms
Size Binary Sequential Sequential Sequential Search

(Average) (Worst Case) Interval 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

1,000 10 500 1,000


10,000 14 5,000 10,000
100,000 17 50,000 100,000
1,000,000 20 500,000 1,000,000

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

(Source: Introduction to algorithms - 3rd edition)

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

Each of these operations takes only O(1) time.

Search + Hash.18
Search + Hash
Downside of direct addressing
Tran Ngoc Bao Duy

• If the universe U is large, storing a table Searching algorithms


Sequential Search
Interval Search

T of size |U | may be impractical, or even Hash table

impossible. Basic concepts


Direct-address tables
Hash tables
Hash functions
Open addressing

• The set K of keys actually stored may be


so small relative to U that most of the
space allocated for T would be wasted.

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

much less storage than the universe U of all possible keys.

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

• With direct addressing, an element with key k is stored Direct-address tables


Hash tables

in slot k. Hash functions


Open addressing

• With hashing, this element is stored in slot h(k). Hash


function h computes the slot from the key k that
maps the universe keys U into the slots of a hash table
T [0 . . . m − 1]:

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

• With direct addressing, an element with key k is stored Direct-address tables


Hash tables

in slot k. Hash functions


Open addressing

• With hashing, this element is stored in slot h(k). Hash


function h computes the slot from the key k that
maps the universe keys U into the slots of a hash table
T [0 . . . m − 1]:

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

(Source: Introduction to algorithms - 3rd edition)

Search + Hash.20
Search + Hash
Collision
Tran Ngoc Bao Duy

There is one hitch: two keys may hash to the Searching algorithms

same slot. We call this situation a collision. Sequential Search


Interval Search

Fortunately, we have effective techniques for Hash table


Basic concepts

resolving the conflict created by collisions. Direct-address tables


Hash tables
Hash functions
Open addressing

Search + Hash.21
Search + Hash
Collision
Tran Ngoc Bao Duy

There is one hitch: two keys may hash to the Searching algorithms

same slot. We call this situation a collision. Sequential Search


Interval Search

Fortunately, we have effective techniques for Hash table


Basic concepts

resolving the conflict created by collisions. Direct-address tables


Hash tables
Hash functions
Open addressing

• Ideal solution: avoid collisions altogether, try to


achieve this goal by choosing a suitable hash func-
tion h.

Search + Hash.21
Search + Hash
Collision
Tran Ngoc Bao Duy

There is one hitch: two keys may hash to the Searching algorithms

same slot. We call this situation a collision. Sequential Search


Interval Search

Fortunately, we have effective techniques for Hash table


Basic concepts

resolving the conflict created by collisions. Direct-address tables


Hash tables
Hash functions
Open addressing

• Ideal solution: avoid collisions altogether, try to


achieve this goal by choosing a suitable hash func-
tion h.
• Simplest solution: collision resolution by chaining.

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

same slot into the same linked list. Interval Search

Hash table
Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing

(Source: Introduction to algorithms - 3rd edition)

Search + Hash.22
Search + Hash
Collision resolution by chaining
Tran Ngoc Bao Duy

Definition Searching algorithms

In chaining, we place all the elements that hash to the Sequential Search
Interval Search

same slot into the same linked list. 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

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

The worst-case behavior of hashing with chaining is terrible:


all n keys hash to the same slot, creating a list of length n.

Search + Hash.22
Search + Hash

Tran Ngoc Bao Duy

Searching algorithms
Sequential Search
Interval Search

Hash table

HASH FUNCTIONS Basic concepts


Direct-address tables
Hash tables
Hash functions
Open addressing

Search + Hash.23
Search + Hash
Interpreting keys as natural numbers
Tran Ngoc Bao Duy

• Most hash functions assume that the universe of Searching algorithms

keys is the set N = {0, 1, 2, . . .}. Sequential Search


Interval Search

• 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

• Most hash functions assume that the universe of Searching algorithms

keys is the set N = {0, 1, 2, . . .}. Sequential Search


Interval Search

• 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

Example Open addressing

Interpret the key in string pt as natural number.

Search + Hash.24
Search + Hash
Interpreting keys as natural numbers
Tran Ngoc Bao Duy

• Most hash functions assume that the universe of Searching algorithms

keys is the set N = {0, 1, 2, . . .}. Sequential Search


Interval Search

• 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

Example Open addressing

Interpret the key in string pt as natural number.


Since p = 112, t = 116, express it as a radix-128 integet. It
becomes (112 × 128) + 116 = 14452

Search + Hash.24
Search + Hash
A good hash function
Tran Ngoc Bao Duy

A good hash function satisfies (approximately) the as- Searching algorithms


Sequential Search
sumption of simple uniform hashing (SUHA): each key Interval Search

is equally likely to hash to any of the m slots, indepen- Hash table


Basic concepts
dently of where any other key has hashed to. Direct-address tables
Hash tables
Hash functions
Open addressing

Search + Hash.25
Search + Hash
A good hash function
Tran Ngoc Bao Duy

A good hash function satisfies (approximately) the as- Searching algorithms


Sequential Search
sumption of simple uniform hashing (SUHA): each key Interval Search

is equally likely to hash to any of the m slots, indepen- Hash table


Basic concepts
dently of where any other key has hashed to. Direct-address tables
Hash tables

• Division method Hash functions


Open addressing

• 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

divided by m. That is, the hash function is Interval Search

Hash table
Basic concepts

h(k) = k mod m Direct-address tables


Hash tables
Hash functions
Open addressing

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

divided by m. That is, the hash function is Interval Search

Hash table
Basic concepts

h(k) = k mod m Direct-address tables


Hash tables
Hash functions
Open addressing

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

divided by m. That is, the hash function is Interval Search

Hash table
Basic concepts

h(k) = k mod m Direct-address tables


Hash tables
Hash functions
Open addressing

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

erates in two steps: Interval 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

In short, the hash function is

h(k) = bm (kA mod 1)c


where kA mod 1 means kA − bkAc.

Search + Hash.27
Search + Hash
Digit extraction
Tran Ngoc Bao Duy

Searching algorithms

Address = selected digits f rom Key Sequential Search


Interval Search

Hash table
Basic concepts

Example: Direct-address tables


Hash tables

379452→394 Hash functions


Open addressing

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

Address = middle digits of Key 2


Basic concepts
Direct-address tables
Hash tables
Hash functions
Open addressing
Example:
9452 * 9452 = 89340304→3403

Search + Hash.29
Search + Hash
Mid-square
Tran Ngoc Bao Duy

• Disadvantage: the size of the Key 2 is too Searching algorithms


Sequential Search

large. Interval Search

Hash table

• Variations: use only a portion of the key. Basic concepts


Direct-address tables
Hash tables

Example: Hash functions


Open addressing

379452: 379 * 379 = 143641→364 121267:


121 * 121 = 014641→464 045128: 045 *
045 = 002025→202

Search + Hash.30
Search + Hash
Folding
Tran Ngoc Bao Duy

The key is divided into parts whose size matches


the address size.
Searching algorithms
Sequential Search

Example: Interval Search

Hash table

Key = 123k456k789 Basic concepts


Direct-address tables
Hash tables

fold shift Hash functions


Open addressing

123 + 456 + 789 = 1368


→ 368

Search + Hash.31
Search + Hash
Folding
Tran Ngoc Bao Duy

The key is divided into parts whose size matches


the address size.
Searching algorithms
Sequential Search

Example: Interval Search

Hash table

Key = 123k456k789 Basic concepts


Direct-address tables
Hash tables

fold shift Hash functions


Open addressing

123 + 456 + 789 = 1368


→ 368

fold boundary
321 + 456 + 987 = 1764
→ 764
Search + Hash.31
Search + Hash
Rotation
Tran Ngoc Bao Duy

• Hashing keys that are identical except for


Searching algorithms
the last character may create synonyms. Sequential Search
Interval Search

• The key is rotated before hashing. Hash table


Basic concepts

original key rotated key Direct-address tables


Hash tables
Hash functions
600101 160010 Open addressing

600102 260010
600103 360010
600104 460010
600105 560010

Search + Hash.32
Search + Hash
Rotation
Tran Ngoc Bao Duy

• Used in combination with fold shift.


original key rotated key Searching algorithms
Sequential Search

600101 → 62 160010 → 26 Interval Search

Hash table

600102 → 63 260010 → 36 Basic concepts


Direct-address tables

600103 → 64 360010 → 46
Hash tables
Hash functions
Open addressing

600104 → 65 460010 → 56
600105 → 66 560010 → 66

Spreading the data more evenly across the ad-


dress space.

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

For maximum efficiency, a and c should be


prime numbers.

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

c=7 Direct-address tables


Hash tables

listSize = 307 Hash functions


Open addressing

Address = ((17*121267 + 7) mod 307


= (2061539 + 7) mod 307
= 2061546 mod 307
= 41

Search + Hash.35
Search + Hash
Open addressing
Tran Ngoc Bao Duy

When a collision occurs, an unoccupied element is searched Searching algorithms


Sequential Search
for placing the new element in (probing). Interval Search

Hash table
Basic concepts
Hash function: Direct-address tables

h : U × {0, 1, 2, ..., m − 1} → {0, 1, 2, ..., m − 1} Hash tables


Hash functions
Open addressing

For every key k, the probe sequence:


< h(k, 0), h(k, 1), . . . , h(k, m − 1) >

Search + Hash.36
Search + Hash
Open addressing
Tran Ngoc Bao Duy

When a collision occurs, an unoccupied element is searched Searching algorithms


Sequential Search
for placing the new element in (probing). Interval Search

Hash table
Basic concepts
Hash function: Direct-address tables

h : U × {0, 1, 2, ..., m − 1} → {0, 1, 2, ..., m − 1} Hash tables


Hash functions
Open addressing

For every key k, the probe sequence:


< h(k, 0), h(k, 1), . . . , h(k, m − 1) >
be a permutation of < 0, 1, . . . , m − 1 >

Search + Hash.36
Search + Hash
Open Addressing
Tran Ngoc Bao Duy

1 Algorithm hashInsert(ref T <array>, val k <key>)


2 Inserts key k into table T.
Searching algorithms
3 i=0 Sequential Search

4 while i < m do Interval Search

Hash table
5 j = h(k, i) Basic concepts

6 if T[j] = nil then Direct-address tables


Hash tables

7 T[j] = k Hash functions


Open addressing

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

1 Algorithm hashSearch(val T <array>, val k <key>)


2 Searches for key k in table T.

3 i=0 Searching algorithms


Sequential Search
4 while i < m do Interval Search

5 j = h(k, i) Hash table


Basic concepts
6 if T[j] = k then Direct-address tables

7 return j Hash tables


Hash functions

8 else if T[j] = nil then Open addressing

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

Given an ordinary hash function h0 : U → {0, 1, 2, ..., m−1},


which we refer to as an auxiliary hash function, the method Searching algorithms
Sequential Search
of linear probing uses the hash function Interval Search

Hash table

h(k, i) = h0 (k) + ci
 Basic concepts
mod m Direct-address tables
Hash tables

where c is positive auxiliary constant. Hash functions


Open addressing

Search + Hash.39
Search + Hash
Linear probing
Tran Ngoc Bao Duy

Given an ordinary hash function h0 : U → {0, 1, 2, ..., m−1},


which we refer to as an auxiliary hash function, the method Searching algorithms
Sequential Search
of linear probing uses the hash function Interval Search

Hash table

h(k, i) = h0 (k) + ci
 Basic concepts
mod m Direct-address tables
Hash tables

where c is positive auxiliary constant. Hash functions


Open addressing

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

h(10, 0) = (h0 (10) + 0) mod 11 = 10. Hash table


Basic concepts
Direct-address tables
Hash tables
0 1 2 3 4 5 6 7 8 9 10 Hash functions
Open addressing

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

h(22, 0) = (h0 (22) + 0) mod 11 = 0. Hash table


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 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

h(31, 0) = (h0 (31) + 0) mod 11 = 9. Hash table


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 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

h(4, 0) = (h0 (4) + 0) mod 11 = 4. Hash table


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 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

h(15, 0) = (h0 (15) + 0) mod 11 = 4. Collission. Hash table


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 10 10 10 10 31 10

Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy

Searching algorithms
Insert 15. Sequential Search

h(15, 0) = (h0 (15) + 0) mod 11 = 4. Collission. Interval Search

h(15, 1) = (h0 (15) + 1) mod 11 = 5. Hash table


Basic concepts
Direct-address tables
Hash tables
Hash functions
0 1 2 3 4 5 6 7 8 9 10 Open addressing

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

h(28, 0) = (h0 (28) + 0) mod 11 = 6. Hash table


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.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy

Searching algorithms
Insert 17. Sequential Search
Interval Search

h(17, 0) = (h0 (17) + 0) mod 11 = 6. Collission. Hash table


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.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy

Searching algorithms
Insert 17. Sequential Search

h(17, 0) = (h0 (17) + 0) mod 11 = 6. Collission. Interval Search

h(17, 1) = (h0 (17) + 1) mod 11 = 7. Hash table


Basic concepts
Direct-address tables
Hash tables
Hash functions
0 1 2 3 4 5 6 7 8 9 10 Open addressing

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

h(88, 0) = (h0 (88) + 0) mod 11 = 0. Collission. Hash table


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 17 10 31 10

Search + Hash.40
Search + Hash
Linear Probing
Tran Ngoc Bao Duy

Searching algorithms
Insert 88. Sequential Search

h(88, 0) = (h0 (88) + 0) mod 11 = 0. Collission. Interval Search

h(88, 1) = (h0 (88) + 1) mod 11 = 1. Hash table


Basic concepts
Direct-address tables
Hash tables
Hash functions
0 1 2 3 4 5 6 7 8 9 10 Open addressing

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

h(59, 0) = (h0 (59) + 0) mod 11 = 4. Collission. Hash table


Basic concepts
Direct-address tables
Hash tables
0 1 2 3 4 5 6 7 8 9 10 Hash functions
Open addressing

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

h(59, 0) = (h0 (59) + 0) mod 11 = 4. Collission. Interval Search

h(59, 1) = (h0 (59) + 1) mod 11 = 5. Collission. Hash table


Basic concepts
Direct-address tables
Hash tables
Hash functions
0 1 2 3 4 5 6 7 8 9 10 Open addressing

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


Interval Search

h(59, 1) = (h0 (59) + 1) mod 11 = 5. Collission. Hash table

h(59, 2) = (h0 (59) + 2) mod 11 = 6. Collission. Basic concepts


Direct-address tables
Hash tables
Hash functions
Open addressing
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.
Searching algorithms
h(59, 0) = (h0 (59) + 0) mod 11 = 4. Collission. Sequential Search

h(59, 1) = (h0 (59) + 1)


Interval Search
mod 11 = 5. Collission.
Hash table
h(59, 2) = (h0 (59) + 2) mod 11 = 6. Collission. Basic concepts

h(59, 3) = (h0 (59) + 3)


Direct-address tables
mod 11 = 7. Collission. Hash tables
Hash functions
Open addressing

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

h(59, 2) = (h0 (59) + 2) mod 11 = 6. Collission. Hash table


Basic concepts
h(59, 3) = (h0 (59) + 3) mod 11 = 7. Collission. Direct-address tables

h(59, 4) = (h0 (59) + 4)


Hash tables
mod 11 = 8. Hash functions
Open addressing

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

• Suffers from a problem as primary clustering. Hash functions


Open addressing

Search + Hash.41
Search + Hash
Quadratic probing
Tran Ngoc Bao Duy

Quadratic probing uses a hash function of the form


Searching algorithms
h(k, i) = h0 (k) + c1 i + c2 i2

mod m Sequential Search
Interval Search

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

Quadratic probing uses a hash function of the form


Searching algorithms
h(k, i) = h0 (k) + c1 i + c2 i2

mod m Sequential Search
Interval Search

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

h(10, 0) = (h0 (10) + 0) mod 11 = 10. Hash table


Basic concepts
Direct-address tables
Hash tables
0 1 2 3 4 5 6 7 8 9 10 Hash functions
Open addressing

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

h(22, 0) = (h0 (22) + 0) mod 11 = 0. Hash table


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 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

h(31, 0) = (h0 (31) + 0) mod 11 = 9. Hash table


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 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

h(4, 0) = (h0 (4) + 0) mod 11 = 4. Hash table


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 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

h(15, 0) = (h0 (15) + 0) mod 11 = 4. Collission. Hash table


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 10 10 10 10 31 10

Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy

Searching algorithms
Insert 15. Sequential Search

h(15, 0) = (h0 (15) + 0) mod 11 = 4. Collission. Interval Search

h(15, 1) = (h0 (15) + 1 + 3 × 12 ) mod 11 = 8. Hash table


Basic concepts
Direct-address tables
Hash tables
Hash functions
0 1 2 3 4 5 6 7 8 9 10 Open addressing

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

h(28, 0) = (h0 (28) + 0) mod 11 = 6. Hash table


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 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

h(17, 0) = (h0 (17) + 0) mod 11 = 6. Collission. Hash table


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 10 28 10 15 31 10

Search + Hash.43
Search + Hash
Quadratic Probing
Tran Ngoc Bao Duy

Searching algorithms
Insert 17. Sequential Search

h(17, 0) = (h0 (17) + 0) mod 11 = 6. Collission. Interval Search

h(17, 1) = (h0 (17) + 1 + 3 × 12 ) mod 11 = 10. Collission. Hash table


Basic concepts
Direct-address tables
Hash tables
Hash functions
0 1 2 3 4 5 6 7 8 9 10 Open addressing

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


Interval Search

h(17, 1) = (h0 (17) + 1 + 3 × 12 ) mod 11 = 10. Collission. Hash table

h(17, 2) = (h0 (17) + 2 + 3 × 22 ) mod 11 = 9. Collission. Basic concepts


Direct-address tables
Hash tables
Hash functions
Open addressing
0 1 2 3 4 5 6 7 8 9 10

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

h(17, 1) = (h0 (17) + 1 + 3 × 12 ) mod 11 = 10. Collission.


Interval Search

Hash table
h(17, 2) = (h0 (17) + 2 + 3 × 22 ) mod 11 = 9. Collission. Basic concepts

h(17, 2) = (h0 (17) + 3 + 3 × 32 ) mod 11 = 3.


Direct-address tables
Hash tables
Hash functions
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

Searching algorithms
Insert 88. Sequential Search
Interval Search

h(88, 0) = (h0 (88) + 0) mod 11 = 0. Collission. Hash table


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 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

h(88, 0) = (h0 (88) + 0) mod 11 = 0. Collission. Interval Search

h(88, 1) = (h0 (88)+1+3×12 ) mod 11 = 4. Collission. Hash table


Basic concepts
Direct-address tables
Hash tables
Hash functions
0 1 2 3 4 5 6 7 8 9 10 Open addressing

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


Interval Search

h(88, 1) = (h0 (88) + 1 + 3 × 12 ) mod 11 = 4. Collission. Hash table

h(88, 2) = (h0 (88)+2+3×22 ) mod 11 = 3. Collission. Basic concepts


Direct-address tables
Hash tables
Hash functions
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.
Searching algorithms
h(88, 0) = (h0 (88) + 0) mod 11 = 0. Collission. Sequential Search

h(88, 1) = (h0 (88) + 1 + 3 × 12 ) mod 11 = 4. Collission.


Interval Search

Hash table
h(88, 2) = (h0 (88) + 2 + 3 × 22 ) mod 11 = 3. Collission. Basic concepts

h(88, 3) = (h0 (88) + 3 + 3 × 32 ) mod 11 = 8. Collission.


Direct-address tables
Hash tables
Hash functions
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
Interval Search

h(88, 3) = (h0 (88) + 3 + 3 × 32 ) mod 11 = 8. Collission. 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

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

h(88, 3) = (h0 (88) + 3 + 3 × 32 ) mod 11 = 8. Collission.


Interval 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

• Suffers from a problem as secondary clustering. Hash functions


Open addressing

Search + Hash.44
Search + Hash
Double hashing
Tran Ngoc Bao Duy

Double hashing offers one of the best methods available for


open addressing because the permutations produced have
many of the characteristics of randomly chosen permuta- Searching algorithms
Sequential Search

tions. Double hashing uses a hash function of the form Interval Search

Hash table
Basic concepts

h(k, i) = (h1 (k) + ih2 (k)) mod m Direct-address tables


Hash tables
Hash functions
where h1 and h2 are auxiliary hash functions. Open addressing

Search + Hash.45
Search + Hash
Double hashing
Tran Ngoc Bao Duy

Double hashing offers one of the best methods available for


open addressing because the permutations produced have
many of the characteristics of randomly chosen permuta- Searching algorithms
Sequential Search

tions. Double hashing uses a hash function of the form Interval Search

Hash table
Basic concepts

h(k, i) = (h1 (k) + ih2 (k)) mod m Direct-address tables


Hash tables
Hash functions
where h1 and h2 are auxiliary hash functions. Open addressing

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

h(10, 0) = (h1 (10) + 0 × h2 (10)) mod 11 = 10. Hash table


Basic concepts
Direct-address tables
Hash tables
0 1 2 3 4 5 6 7 8 9 10 Hash functions
Open addressing

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

h(22, 0) = (h1 (22) + 0 × h2 (2)) mod 11 = 0. Hash table


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 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

h(31, 0) = (h1 (31) + 0 × h2 (31)) mod 11 = 9. Hash table


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 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

h(4, 0) = (h1 (4) + 0 × h2 (4)) mod 11 = 4. Hash table


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 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

h(15, 0) = (h1 (15) + 0 × h2 (15)) mod 11 = 4. Collission. Hash table


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 10 10 10 10 31 10

Search + Hash.46
Search + Hash
Double hashing
Tran Ngoc Bao Duy

Insert 15. Searching algorithms


Sequential Search
h(15, 0) = (h1 (15) + 0 × h2 (15)) mod 11 = 4. Collission. Interval Search

h(15, 1) = (h1 (10) + 1 × h2 (10)) mod 11 = 10. Hash table


Basic concepts
Collission. Direct-address tables
Hash tables
Hash functions
Open addressing
0 1 2 3 4 5 6 7 8 9 10

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

h(28, 0) = (h1 (28) + 0 × h2 (28)) mod 11 = 6. Hash table


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

Searching algorithms
Insert 17. Sequential Search

h(17, 0) = (h1 (17) + 0 × h2 (17)) mod 11 = 6. Interval 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

Insert 17. Searching algorithms

h(17, 0) = (h1 (17) + 0 × h2 (17)) mod 11 = 6. Sequential Search


Interval Search

Collission. Hash table

h(17, 1) = (h1 (17) + 1 × h2 (17)) mod 11 = 2. Basic concepts


Direct-address tables
Hash tables
Hash functions
Open addressing
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

Searching algorithms
Insert 88. Sequential Search

h(88, 0) = (h1 (88) + 0 × h2 (88)) mod 11 = 0. Interval 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

Insert 88. Searching algorithms


h(88, 0) = (h1 (88) + 0 × h2 (88)) mod 11 = 0. Sequential Search
Interval Search
Collission. Hash table
h(88, 1) = (h1 (88) + 1 × h2 (88)) mod 11 = 9. Basic concepts
Direct-address tables
Collission. Hash tables
Hash functions
Open addressing

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

h(88, 1) = (h1 (88) + 1 × h2 (88)) mod 11 = 9. Hash table


Basic concepts
Collission. Direct-address tables
Hash tables
h(88, 2) = (h1 (88) + 2 × h2 (88)) mod 11 = 7. Hash functions
Open addressing

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

h(59, 0) = (h1 (59) + 0 × h2 (59)) mod 11 = 4. Interval 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

Insert 59. Searching algorithms

h(59, 0) = (h1 (59) + 0 × h2 (59)) mod 11 = 4. Sequential Search


Interval Search

Collission. Hash table

h(59, 1) = (h1 (59) + 1 × h2 (59)) mod 11 = 3. Basic concepts


Direct-address tables
Hash tables
Hash functions
Open addressing
0 1 2 3 4 5 6 7 8 9 10

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

• Hashing data to buckets that can hold mul- Hash table


Basic concepts
tiple pieces of data. Direct-address tables
Hash tables

• Each bucket has an address and collisions Hash functions


Open addressing

are postponed until the bucket is full.

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

Tran Ngoc Bao Duy

Searching algorithms
Sequential Search
Interval Search

Hash table

THANK YOU. Basic concepts


Direct-address tables
Hash tables
Hash functions
Open addressing

Search + Hash.49

You might also like