Micro - Project DSP 2024.pdf Prasen Vishal Pratik
Micro - Project DSP 2024.pdf Prasen Vishal Pratik
On
Hash Map
By
Suthar Prasen R 236040316094 , Milankumar
Thakor
236040316097,
, Vankar Pratik 23604016101 , Talpada
vishalkumar 236040316096
Enrollment No:
236040316094 ,
236040316097,
23604016101
236040316096
A Micro Project in Data Structure with Python (4331601)
Submitted to
Information Technology Department
B & B Institute of Technology, Vallabh Vidyanagar
Certificate
Date: 27 / 9 / 24
Place: B&B INSTITUE OF TECHNOLOGY VV NAGAR
ANAND
1. Introduction
1.1 Basic Components
1.2 ... Hash Function
1.2.1 ... Collisions
1.2.2 ... Collision Handling
1.3 ... Load Factor and Resizing
2. Softwares for [Hash map]
2.1 Anaconda
2.2 Jupyter Notebook
2.3 Python
2.4 Python libraries
2.4.1 Pandas
2.4.2 Matplotlib
......
3. Flowchart, Algorithm & Pseudo Code
3.1 Flowchart
3.2 Algorithm
3.3 Pseudo Code
Source Code (your python code)
References
1. Introduction
1.1 INTRODUCTION
Deterministic: The same key always produces the same hash code.
Uniform Distribution: It distributes keys evenly across the available indices,
minimizing collisions.
Efficient: It computes the hash code quickly.
For a string key, a basic hash function might sum the ASCII values of the characters
and take the modulus with the array size:
python
Copy code
def simple_hash(key, array_size):
1.2.2Collision Handling
When the load factor exceeds a predefined threshold (commonly 0.7), the hash map is
resized (usually doubled) to maintain performance. Resizing involves rehashing all
existing entries to new indices based on the new array size.
Operations
1. Insertion
index = hash_function(key)
if array[index] is empty:
array[index] = (key, value)
else:
handle_collision(array[index], key, value)
2. Deletion
3. Lookup
To retrieve a value:
index = hash_function(key)
if array[index] contains key:
return value associated with key
return None
Performance Analysis
1. Time Complexity
2. Space Complexity
The space complexity is O(m + n), where m is the size of the array and n is the
number of entries. Additional space is required for handling collisions (e.g., linked
lists in chaining).
Advantages of Hash Maps
Practical Example
Python Implementation
Here’s a simple implementation of a hash map in Python using chaining for collision
handling:
python
Copy code
class HashMap:
Usage
python
Copy code
hash_map = HashMap()
hash_map.insert("apple", 1)
hash_map.insert("banana", 2)
print(hash_map.lookup("apple"))
# Output: 1
hash_map.delete("banana") print(hash_map.lookup("banana")) # Output:
None
Source Code
a- sso**cDiaetsivcer iption**:
PHP uses
arrays, which function similarly to hash
maps.
if pair[0] == key:
pair[1] = value # Update value if key already exists
return
self.map[index].append([key, value]) # Append new
key-value pair
if pair[0] == key:
return pair[1] # Return the value associated with
the key
return None # Key not found
[3] https://www.digitalocean.com/community/tutorials/java-
hashmap