0% found this document useful (0 votes)
18 views

CS 04

The document discusses different types of hash functions and compression maps for hash tables. It covers linear probing and double hashing techniques for handling collisions in open addressing. It also provides analysis of expected probe length for successful and unsuccessful searches in hash tables under different load factors.

Uploaded by

rohitsingh884882
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CS 04

The document discusses different types of hash functions and compression maps for hash tables. It covers linear probing and double hashing techniques for handling collisions in open addressing. It also provides analysis of expected probe length for successful and unsuccessful searches in hash tables under different load factors.

Uploaded by

rohitsingh884882
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Compression Maps

• h(k) = k mod m k: key, m: #slots

• h(k) = ⌊m(k A mod 1)⌋ 0<A<1

• h(k) = | ak + b | mod n

22
Compression Maps
• h(k) = k mod m
b
• Option 1: m = 2
h(k) gives the b least significant bits of k
All keys with the same ending go to the same place.

• Option 2: m is prime
• Ensures uniform distribution
• Avoid using prime numbers to close to a power of 2.
23
Compression Maps
• h(k) = ⌊m(k A mod 1)⌋ 0<A<1

p
• Value of m is not critical. Can be chosen as 2

• The choice of A depends on the characteristics of data.

5−1
A= corresponds to Fibonacci Hashing
2

24
Compression Maps
• h(k) = | ak + b | mod n

• a should not be a multiple of n


a and n should be co-prime

25
Universal Hashing
• Create a set of hash functions H, from which h can be randomly
selected.

• For a randomly chosen hash function h ∈ H,


1
P {h(k) = h(l)} ≤
m

26
Handling Collision
• A general class of techniques for handling collisions is Open Addressing

• All elements are stored in the hash table n ≤ m

• Inserting and searching in a hash table requires systematically probing


the available slots.

27
Open Addressing
• Probing can involve multiple attempts to find an available slot

• The hash function h is modified to include the probe number i as the


second parameter.
h : U × {0,1,…, m − 1} ⟶ {0,1,…, m − 1}

• The probe sequence for a given key k is a permutation of


⟨0,1,2,…, m − 1⟩

28
Linear Probing
• If there is a collision, the probe advances to the next slot

• Uses less memory than chaining

• Slower than chaining

29
Linear Probing . . . . . . . . . .
• h(k) = k mod 10 0 1 2 3 4 5 6 7 8 9

Insert 89 . . . . . . . . . 89
0 1 2 3 4 5 6 7 8 9
Insert 18 . . . . . . . . 18 89
0 1 2 3 4 5 6 7 8 9
Insert 49 49 . . . . . . . 18 89
0 1 2 3 4 5 6 7 8 9
Insert 58 49 58 . . . . . . 18 89
0 1 2 3 4 5 6 7 8 9
30
Linear Probing
• How to perform deletion?

. . . . . . . . . . . .
0 1 2 3 4 5 6 7 8 9 10 11

31
Double Hashing
• Use two hash functions h1(k) and h2(k)

• h1(k) is the position in the table where we first check for key k

• h2(k) is the offset to be used for moving to the next location.

• DoubleHashing_Insert(k):
if (table is full) error; probe = h1(k); offset = h2(k)
while (table[probe] is occupied)
probe = (probe + offset) mod m
table[probe] = k
32
Double Hashing Insert 89 . . . . . . . . . 89
h1(k) = k mod 10 0 1 2 3 4 5 6 7 8 9
h2(k) = 7 − (k mod 7) Insert 18 . . . . . . . . 18 89
0 1 2 3 4 5 6 7 8 9
Insert 49 . . . . . . 49 . 18 89
0 1 2 3 4 5 6 7 8 9
Insert 58 . . . 58 . . 49 . 18 89
0 1 2 3 4 5 6 7 8 9

Insert 69 69 . . 58 . . 49 . 18 89
0 1 2 3 4 5 6 7 8 9
33
Double Hashing Example
h1(k) = k mod 13 . . . . . 18 . . . . . . .

h2(k) = 8 − (k mod 8) 0 1 2 3 4 5 6 7 8 9 10 11 12
. . 41 . . 18 . . . . . . .
0 1 2 3 4 5 6 7 8 9 10 11 12
Insert
18 . . 41 . . 18 . . . 22 . . .
41 0 1 2 3 4 5 6 7 8 9 10 11 12
22
44 . 41 . . 48 . . . 22 . . .
44
59 0 1 2 3 4 5 6 7 8 9 10 11 12
32 44 . 41 . . 48 . 59 . 22 . . .
31
73 0 1 2 3 4 5 6 7 8 9 10 11 12
44 . 41 73 . 48 32 59 31 22 . . .
34
Analysis of Double Hashing
• The load factor λ is less than 1
• We assume that every probe looks at a random location in the table

• 1 − λ fraction of the table is empty


• Expected number of probes required to find an empty location
1
(unsuccessful search) is
1−λ

35
• Average number of probes for a successful search = average number of
probes required to insert all elements

• To insert an element we need to find an empty location

Inserting Avg # of probes Total # of probes


First m/2 <=2 m
Next m/4 <=4 m
Next m/8 <=8 m

36
m m m m
• No of probes required to insert 2 + 4 + 8 + … + 2i elements

1
= no of probes required to leave i fraction of the table empty = m×i
2

• No of probes required to leave 1 − λ fraction of the table empty

(1 − λ)
1
= m log = − m log(1 − λ)

• Average number of probes required to insert n elements is

(n) (λ)
m 1
− log(1 − λ) = − log(1 − λ)
37
Expected number of probes
• Load factor λ < 1 for probing

• Analysis of probing uses uniform hashing assumption — any permutation is


equally likely.

unsuccessful Successful
Chaining
O(1 + λ) O(1 + λ)

(1 − λ) (λ 1 − λ)
Probing
1 1 1
O O ln
38
Trees

1
Definitions A
• A, B, C, H — internal nodes
• D, E, F, G, I — leaf nodes B C
• The root has level 0
• The depth of a node is its level in D E F G H
the tree

• The height of the tree is the


I
maximum level of any node

• The degree of a node is the number


of children
2
Example Tree MD/CEO

Director- Director- Director- Director


Operations Marketing Accounts & Human
Finance Resources

GM- GM- GM- GM- GM- GM- GM-


Manufacturing Quality Sales CRM Accounts Finance HR

Chief Plant Chief Quality Chief Chief


Manager Manager Manager — Manager —
Sales CRM

Manager, Manger, Managers, Manager CRM,


Workers Workers Sales Executives CRM Executives
3
Example Tree

4
Example Tree

5
Binary Tree
• An ordered tree is one in which the children of each node are ordered.
• Binary tree is an ordered tree with all nodes having at most 2 children
A

B C

D F H

6
Recursive Definition of Binary Tree
• A binary tree is either a
• leaf or
A
• An internal node (the
root) and one/two
B C
binary trees (left subtree
and/or right subtree).
D F H

You might also like