0% found this document useful (0 votes)
18 views41 pages

23AID204 ADSAA-HashTables

Uploaded by

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

23AID204 ADSAA-HashTables

Uploaded by

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

23AID204 AdvancedData

Structures and Algorithm


Analysis
Hash Tables
Dr. Nimmy K
Assistant Professor
Department of Computer Science and Engineering
Hash Tables
• Many applications require a dynamic set that supports only the
dictionary operations
• INSERT
• SEARCH
• DELETE
• A hash table is an effective data structure for implementing
dictionaries.
• Although searching for an element in a hash table can take as long as
searching for an element in a linked list—‚ time in the worst case
Hash Tables
• Under reasonable assumptions, the average time to search for an
element in a hash table is
Hash tables
• Eelement is stored in slot that is, we use a hash function h to compute
the slot from the key k
• h maps the universe of keys into the slots of a hash table
• Using a hash function h to map keys to hash-table slots. Because keys
and map to the same slot, they collide
Collision resolution by chaining
• Collision: two keys may hash to the same slot
Collision resolution by chaining
• The worst-case running time for insertion is
Hash functions
• The division method: we map a key k into one of m slots by taking the
remainder of k divided by m.

• The multiplication method: operates in two steps


1. First, we multiply the key k by a constant A in the range 0 < A < 1 and extract
the fractional part of kA
2. we multiply this value by m and take the floor of the result.
Compute the hash value:
• K=12345
• A=0.357840
• M=100
Example
• Given the keys [23, 45, 12, 56, 78] and a hash table size m=10,
compute the hash values using the division method.
• Given the keys [23, 45, 12, 56, 78] and a hash table size m=10,
compute the hash values using the division method.
• Solution: For each key k, calculate h(k)=kmod10

• h(23)=23mod 10=3
• h(45)=45mod 10=5
• h(12)=12mod 10=2
• h(56)=56mod 10=6
• h(78)=78mod 10=8
• For the keys [12345, 67890], calculate the hash values using the
multiplication method with m=32 and A=0.6180339887.
• For the keys [12345, 67890], calculate the hash values using the
multiplication method with m=32 and A=0.6180339887.
• Hash Values:
h(12345)=20,h(67890)=10
Universal hashing
• Selecting a hash function at random from a family of hash functions
with a certain mathematical property
• A randomized algorithm H for constructing hash functions h : U → {1, .
. . , M } is universal if for all in U , we have
Open Addressing
• All elements occupy the hash table itself.
• Each table entry contains either an element of the dynamic set or NIL.
• Searching: systematically examine table slots until either we find the
desired element or we have ascertained that the element is not in the
table.
• No lists and no elements are stored outside the table, unlike chaining.
Open-Addressing: Insert operation
• The HASH-INSERT procedure
takes as input a hash table T
and a key k.
• It either returns the slot
number where it stores key k
or flags an error because the
hash table is already full.
Open-Addressing: Search Operation
• The procedure HASH-SEARCH
takes as input a hash table T and
a key k, returning j if it finds that
slot j contains key k, or NIL if key
k is not present in table T .
• We will examine three
commonly used techniques to
compute the probe sequences
required for open addressing:
linear probing, quadratic
probing, and double hashing.
Open Addressing: Linear Probing
• Given an ordinary hash function which we refer to as an auxiliary
hash function, the method of linear probing uses the hash function
for
• Given key , we first probe i.e., the slot given by the auxiliary hash
function.
• We next probe slot and so on up to slot
• Then we wrap around to slots until we finally probe slot
Open Addressing: Linear Probing
• Linear probing is easy to implement, but it suffers from a problem
known as primary clustering
• Long runs of occupied slots build up, increasing the average search
time.
Hash Table Index Calculation

Problem 1: Hash Table Index Calculation

Question:
Given a hash table of size m=11 and a hash function h’(k)=k mod m,
insert the following keys: 22,33,44,55,6622,33,44,55,66. Use linear
probing to resolve collisions.

Task 1: Show the final state of the hash table.


Task 2: How many probes were required to insert all the keys?
Solution
• Compute h(k)=kmod 11 for each key:

• h(22)=22mod 11=0
• h(33)=33mod 11=0 (Collision, probe to next empty slot)
• h(44)=44mod 11=0(Collision, probe to next empty slot)
• h(55)=55mod 11=0(Collision, probe to next empty slot)
• h(66)=66mod 11=0(Collision, probe to next empty slot)
Solution…
•Insert keys using linear probing:
•22 at index 0.
•33 at index 1.
•44 at index 2.
•55 at index 3.
•66 at index 4.

•Number of probes for each key:

• 22: 1 probe (no collision).


• 33: 2 probes (1 collision).
• 44: 3 probes (2 collisions).
• 55: 4 probes (3 collisions).
• 66: 5 probes (4 collisions).

•Total probes: 1+2+3+4+5=151+2+3+4+5=15.


Question
• A hash table of size m=7 contains the following input data:
"Alice","Bob","Charlie","David".

• Keys: 15,22,37,29.
• These were inserted using the hash function h’(k)=k mod m and linear probing for
collision resolution.

• Task 1: Determine if "David" (Key 29) can be found in the hash table and the number
of probes required.

• Task 2: Determine the number of probes required to search for the input "Eve" (Key
50).
• Hash table after insertion:

• h(15)=15mod 7=1: "Alice" at index 11.


• h(22)=22mod 7=1: "Bob" at index 22 (Collision, probe next).
• h(37)=37mod 7=2: "Charlie" at index 33 (Collision, probe next).
• h(29)=29mod 7=1: "David" at index 44 (Collision, probe thrice).
Task 1: Search for "David" (Key 29):

Compute h(29)=29mod 7=1.


Probe index 1 (15,"Alice").
Probe index 2 (22,"Bob").
Probe index 3 (37,"Charlie").
Probe index 4 (29,"David").

Probes required: 4.
• Task 2: Search for "Eve" (Key 50):

• Compute h(50)=50mod 7=1


• Probe indices 1,2,3,4 (all checked).
• Index 5: Empty slot (not found).

• Probes required: 5.
Open Addressing: Quadratic Probing
• Quadratic probing uses a hash function of the form
• where is an auxiliary hash function and are positive auxiliary
constants and =0,1,…m-1
Searching in a Hash Table with Correct
Quadratic Probing

• Given a hash table of size m = 11 and a hash function h(k) = k mod


m, the following input data needs to be stored:
- Input Data: "Alice", "Bob", "Charlie", "David", "Eve"
- Keys: 22, 33, 44, 55, 66 (corresponding to the input data).

Use quadratic probing (h(k,i) = (h(k) + i^2) mod m) to resolve


collisions.
- Task 1: Show the final state of the hash table.
- Task 2: How many probes were required to insert all the input
data?
. Compute h(k) = k mod 11 for each key:
1

- h(22) = 22 mod 11 = 0
- h(33) = 33 mod 11 = 0 (Collision, use quadratic probing).
- h(44) = 44 mod 11 = 0 (Collision, use quadratic probing).
- h(55) = 55 mod 11 = 0 (Collision, use quadratic probing).
- h(66) = 66 mod 11 = 0 (Collision, use quadratic probing).

2. Insert input data using quadratic probing:


- "Alice" (Key 22) at index 0.
- "Bob" (Key 33):
- Probe sequence: (0 + 1^2) mod 11 = 1. Insert at index 1.
- "Charlie" (Key 44):
- Probe sequence: (0 + 1^2) mod 11 = 1 (occupied), (0 + 2^2) mod 11 = 4. Insert at index 4.
- "David" (Key 55):
- Probe sequence: (0 + 1^2) mod 11 = 1 (occupied), (0 + 2^2) mod 11 = 4 (occupied), (0 + 3^2) mod 11 =
9. Insert at index 9.
- "Eve" (Key 66):
- Probe sequence: (0 + 1^2) mod 11 = 1, (0 + 2^2) mod 11 = 4, (0 + 3^2) mod 11 = 9, (0 + 4^2) mod 11 = 5.
Insert at index 5.
• 3. Final hash table:
Index: 0 1 2 3 4 5 6 7 8 9 10
Key: 22 33 -- -- 44 66 -- -- -- 55 --
Data: "Alice" "Bob" -- -- "Charlie" "Eve" -- -- -- "David" --

4. Number of probes for each key:


- 22: 1 probe (no collision).
- 33: 2 probes (i = 0, 1).
- 44: 3 probes (i = 0, 1, 2).
- 55: 4 probes (i = 0, 1, 2, 3).
- 66: 5 probes (i = 0, 1, 2, 3, 4).

Total probes: 1 + 2 + 3 + 4 + 5 = 15.


Searching in a Hash Table with Quadratic Probing

• A hash table of size m = 7 contains the following input data: "Alice",


"Bob", "Charlie", "David".
- Keys: 15, 22, 37, 29.

These were inserted using the hash function h(k) = k mod m and
quadratic probing (h(k,i) = (h(k) + i^2) mod m).

- Task 1: Determine if "David" (Key 29) can be found in the hash


table and the number of probes required.
- Task 2: Determine the number of probes required to search for
the input "Eve" (Key 50).
1. Hash table after insertion:
- h(15) = 15 mod 7 = 1: "Alice" at index 1.
- h(22) = 22 mod 7 = 1:
- Probe sequence: (1 + 1^2) mod 7 = 2. Insert "Bob" at index 2.
- h(37) = 37 mod 7 = 2:
- Probe sequence: (2 + 1^2) mod 7 = 3. Insert "Charlie" at index 3.
- h(29) = 29 mod 7 = 1:
- Probe sequence: (1 + 1^2) mod 7 = 2 (occupied), (1 + 2^2) mod 7 = 5. Insert "David"
at index 5.

Final hash table:


Index: 0 1 2 3 4 5 6
Key: -- 15 22 37 -- 29 --
Data: -- "Alice" "Bob" "Charlie" -- "David" --
• Task 1: Search for "David" (Key 29):
- Compute h(29) = 29 mod 7 = 1.
- Probe sequence: (1 + 0^2) mod 7 = 1, (1 + 1^2) mod 7 = 2, (1 + 2^2) mod 7 = 5.
- Found "David" at index 5.
Probes required: 3.

3. Task 2: Search for "Eve" (Key 50):


- Compute h(50) = 50 mod 7 = 1.
- Probe sequence: (1 + 0^2) mod 7 = 1, (1 + 1^2) mod 7 = 2, (1 + 2^2) mod 7 = 5,
(1 + 3^2) mod 7 = 3.
- Probe all slots up to m, no match found.
Probes required: 4.
Open Addressing: Double Hashing
• Double hashing offers one of the best methods available for open
addressing because the permutations produced have many of the
characteristics of randomly chosen permutations.
• Double hashing uses a hash function of the form:
• where and are auxiliary hash functions
Hash Table Index Calculation with Double Hashing

• Given a hash table of size m = 11, two hash functions are defined as:
- Primary Hash Function: h1(k) = k mod m
- Secondary Hash Function: h2(k) = 1 + (k mod (m - 1))

The following input data needs to be stored:


- Input Data: "Alice", "Bob", "Charlie", "David", "Eve"
- Keys: 22, 33, 44, 55, 66 (corresponding to the input data).

Use double hashing to resolve collisions:


- Task 1: Show the final state of the hash table.
- Task 2: How many probes were required to insert all the input data?
• 1. Compute h1(k) = k mod 11 and h2(k) = 1 + (k mod 10) for each key:
- Key 22: h1(22) = 22 mod 11 = 0, h2(22) = 1 + (22 mod 10) = 3
- Key 33: h1(33) = 33 mod 11 = 0, h2(33) = 1 + (33 mod 10) = 4
- Key 44: h1(44) = 44 mod 11 = 0, h2(44) = 1 + (44 mod 10) = 5
- Key 55: h1(55) = 55 mod 11 = 0, h2(55) = 1 + (55 mod 10) = 6
- Key 66: h1(66) = 66 mod 11 = 0, h2(66) = 1 + (66 mod 10) = 7

2. Insert input data using double hashing:


- "Alice" (Key 22): Insert at index 0 (no collision).
- "Bob" (Key 33): Collision at index 0. Probe sequence: (0 + 1 * 4) mod 11 =
4. Insert at index 4.
- "Charlie" (Key 44): Collision at index 0. Probe sequence: (0 + 1 * 5) mod 11
= 5. Insert at index 5.
- "David" (Key 55): Collision at index 0. Probe sequence: (0 + 1 * 6) mod 11
= 6. Insert at index 6.
- "Eve" (Key 66): Collision at index 0. Probe sequence: (0 + 1 * 7) mod 11 =
7. Insert at index 7.
• Final hash table:
Index: 0 1 2 3 4 5 6 7 8 9 10
Key: 22 -- -- -- 33 44 55 66 -- -- --
Data: "Alice" -- -- -- "Bob" "Charlie" "David" "Eve" -- -- --

Number of probes for each key:


- 22: 1 probe (no collision).
- 33: 2 probes (i = 0, 1).
- 44: 2 probes (i = 0, 1).
- 55: 2 probes (i = 0, 1).
- 66: 2 probes (i = 0, 1).

Total probes: 1 + 2 + 2 + 2 + 2 = 9.
Searching in a Hash Table with Double Hashing

• A hash table of size m = 7 contains the following input data: "Alice", "Bob", "Charlie",
"David".
• - Keys: 15, 22, 37, 29.

• Two hash functions are used:


• - h1(k) = k mod 7
• - h2(k) = 1 + (k mod 6)

• - Task 1: Determine if "David" (Key 29) can be found in the hash table and the number of
probes required.
• - Task 2: Determine the number of probes required to search for the input "Eve" (Key 50).
• 1. Hash table after insertion:
- h1(15) = 15 mod 7 = 1, h2(15) = 1 + (15 mod 6) = 4: "Alice" at index 1.
- h1(22) = 22 mod 7 = 1, h2(22) = 1 + (22 mod 6) = 5:
- Probe sequence: (1 + 0 * 5) mod 7 = 1 (occupied), (1 + 1 * 5) mod 7 = 6.
Insert "Bob" at index 6.
- h1(37) = 37 mod 7 = 2, h2(37) = 1 + (37 mod 6) = 2:
- Probe sequence: (2 + 0 * 2) mod 7 = 2. Insert "Charlie" at index 2.
- h1(29) = 29 mod 7 = 1, h2(29) = 1 + (29 mod 6) = 6:
- Probe sequence: (1 + 0 * 6) mod 7 = 1 (occupied), (1 + 1 * 6) mod 7 = 0.
Insert "David" at index 0.

Final hash table:


Index: 0 1 2 3 4 5 6
Key: 29 15 37 -- -- -- 22
Data: "David" "Alice" "Charlie" -- -- -- "Bob"
Task 1: Search for "David" (Key 29):
- Compute h1(29) = 1, h2(29) = 6.
- Probe sequence: (1 + 0 * 6) mod 7 = 1 (occupied), (1 + 1 * 6) mod
7 = 0.
- Found "David" at index 0.
Probes required: 2.

Task 2: Search for "Eve" (Key 50):


- Compute h1(50) = 50 mod 7 = 1, h2(50) = 1 + (50 mod 6) = 3.
- Probe sequence: (1 + 0 * 3) mod 7 = 1 (occupied), (1 + 1 * 3) mod
7 = 4 (empty).
- Not found after 2 probes.
Probes required: 2.

You might also like