Quadratic Probing in Python
Last Updated :
01 Aug, 2024
Hashing is an improvement technique over the Direct Access Table. The idea is to use a hash function that converts a given phone number or any other key to a smaller number and uses the small number as the index in a table called a hash table.
What is Quadratic Probing?
Quadratic probing is a technique used in hash tables to resolve collisions that occur when two different keys hash to the same index. It's a variation of open addressing, where an alternate location is searched within the hash table when a collision occurs. In quadratic probing, when a collision happens, instead of simply moving to the next slot linearly (as in linear probing), the algorithm searches for the next available slot by using a quadratic function.
Quadratic probing is an open-addressing scheme where we look for the i2‘th slot in the i’th iteration if the given hash value x collides in the hash table.
How Quadratic Probing works?
Let hash(x) be the slot index computed using the hash function.
- If the slot hash(x) % S is full, then we try (hash(x) + 1*1) % S.
- If (hash(x) + 1*1) % S is also full, then we try (hash(x) + 2*2) % S.
- If (hash(x) + 2*2) % S is also full, then we try (hash(x) + 3*3) % S.
- This process is repeated for all the values of i until an empty slot is found.
Approach: Simple Quadratic Probing
Use a quadratic function to find the next available slot when a collision occurs.
- Compute the initial hash value.
- If the slot is occupied, probe the next slot using a quadratic function.
- Repeat until an empty slot is found.
- Insert the key into the empty slot.
Implementation of Quadratic Probing:
Below is the implementation of Quadratic Probing in Python:
Python
# Python3 implementation of
# the Quadratic Probing
# Function to print an array
def printArray(arr, n):
# Iterating and printing the array
for i in range(n):
print(arr[i], end=" ")
# Function to implement the
# quadratic probing
def hashing(table, tsize, arr, N):
# Iterating through the array
for i in range(N):
# Computing the hash value
hv = arr[i] % tsize
# Insert in the table if there
# is no collision
if (table[hv] == -1):
table[hv] = arr[i]
else:
# If there is a collision
# iterating through all
# possible quadratic values
for j in range(tsize):
# Computing the new hash value
t = (hv + j * j) % tsize
if (table[t] == -1):
# Break the loop after
# inserting the value
# in the table
table[t] = arr[i]
break
printArray(table, N)
# Driver code
if __name__ == "__main__":
arr = [50, 700, 76,
85, 92, 73, 101]
N = 7
# Size of the hash table
L = 7
hash_table = [0] * 7
# Initializing the hash table
for i in range(L):
hash_table[i] = -1
# Function call
hashing(hash_table, L, arr, N)
Output700 50 85 73 101 92 76
Time Complexity: O(N * L), where N is the length of the array and L is the size of the hash table.
Auxiliary Space: O(1)
The above implementation of quadratic probing does not guarantee that we will always be able to use a hash table empty slot. It might happen that some entries do not get a slot even if there is a slot available. For example consider the input array {21, 10, 32, 43, 54, 65, 87, 76} and table size 11, we get the output as {10, -1, 65, 32, 54, -1, -1, -1, 43, -1, 21} which means the items 87 and 76 never get a slot. To make sure that elements get filled, we need to have a higher table size.
A hash table can be fully utilized using the below idea : Iterate over the hash table to next power of 2 of table size. For example if table size is 11, then iterate 16 times. And iterate over the hash table using the below formula
hash(x) = [hash(x) + (j + j*j)/2] % (Next power of 2 of table size)
Below is the implementation of this Approch:
Python
# Function to print an array
def print_array(arr):
for i in arr:
print(i, end=" ")
# Function to calculate the next power of 2 greater than or equal to m
def next_power_of_2(m):
m -= 1
m |= m >> 1
m |= m >> 2
m |= m >> 4
m |= m >> 8
m |= m >> 16
return m + 1
# Function to implement the quadratic probing
def hashing(table, tsize, arr):
for num in arr:
# Compute the hash value
hv = num % tsize
# Insert in the table if there is no collision
if table[hv] == -1:
table[hv] = num
else:
# If there is a collision, iterate through possible quadratic values
m = next_power_of_2(tsize)
for j in range(1, m + 1):
t = (hv + (j + j * j) // 2) % m
if t < tsize and table[t] == -1:
table[t] = num
break
print_array(table)
# Main driver code
if __name__ == "__main__":
arr = [21, 10, 32, 43, 54, 65, 87, 76]
n = len(arr)
# Size of the hash table
tsize = 11
hash_table = [-1] * tsize
# Call the hashing function
hashing(hash_table, tsize, arr)
Output10 87 -1 -1 32 -1 54 65 76 43 21
Similar Reads
Quadratic Probing in Hashing Hashing is an improvement technique over the Direct Access Table. The idea is to use a hash function that converts a given phone number or any other key to a smaller number and uses the small number as the index in a table called a hash table. Quadratic ProbingQuadratic probing is an open-addressing
14 min read
Linear Probing in Python Linear probing is a technique used in hash tables to handle collisions. When a collision occurs (i.e., when two keys hash to the same index), linear probing searches for the next available slot in the hash table by incrementing the index until an empty slot is found. What is Linear Probing?In linear
2 min read
Python - Quiz Application Project Python provides us with many libraries to create GUI(Graphical User Interface), and Tkinter is one of them. Creating GUI with Tkinter is very easy and it is the most commonly used library for GUI development. In this article, we will create a Quiz application using Tkinter. A Quiz application has a
4 min read
Role of Python in Quantum Computing Quantum computing is one of the most exciting technological frontiers today, with the potential to solve complex problems that classical computers struggle with. Python, already a powerhouse in fields like data science and AI, is emerging as a key player in quantum computing. Here's why Python is es
7 min read
Python Pyramid - Testing In the realm of web development, Python Pyramid stands out as a robust framework known for its flexibility and scalability. Testing is an integral part of any software development process, ensuring the reliability and stability of applications. In this article, we delve into the world of testing wit
3 min read