23AID204 ADSAA-HashTables
23AID204 ADSAA-HashTables
• 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
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.
• 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.
• 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:
Probes required: 4.
• Task 2: Search for "Eve" (Key 50):
• 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
- 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).
These were inserted using the hash function h(k) = k mod m and
quadratic probing (h(k,i) = (h(k) + i^2) mod m).
• 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))
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.
• - 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.