0% found this document useful (0 votes)
21 views18 pages

Micro - Project DSP 2024.pdf Prasen Vishal Pratik

Uploaded by

prasen3110
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)
21 views18 pages

Micro - Project DSP 2024.pdf Prasen Vishal Pratik

Uploaded by

prasen3110
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/ 18

Micro Project

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

This is to certify that SUTHAR PRASEN R (236040316094)


MILAN
THAKOR(236040316097) VANKAR PRATIK (236040316101) , TALPDA
VISHALKUMAR (236040316096) have/has successfully completed
the Micro project on HASHMAP for the subject Data
Structure with Python (4331601) under my guidance and
supervision.

Date: 27 / 9 / 24
Place: B&B INSTITUE OF TECHNOLOGY VV NAGAR
ANAND

Signature of Subject Coordinator:


Nishi Patel
Acknowledgement

I wish to express our sincere gratitude to our external guide for


continuously guiding me at the and answering all my doubts
with patience. I would also like to thank you my Internal guide
Prof. Jay Patel for helping us through our internship by giving us
the necessary suggestions and advices along with their valuable
co- ordination in-completing this internship. I would also like to
thank my parents, friends and all the members of the family for
their precious support and encouragement which they had
provided in completion of my addition to that, I would also like
to mention the company personals who gave me the permission
to use and experience the valuable resources required to the
internship. T hus, inconclusion to the above said, I once again
thank the staff. for their valuable support in completion of the
project.

THANKS PRASEN SUTHAR


October 2024
Table of Contents

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

A hash map, or hash table, is a fundamental data structure that enables


the efficient storage and retrieval of data through key-value pairs. It is
widely used in computer science for various applications, such as
caching, indexing, and associative arrays. This report will delve into the
mechanics of hash maps, including their structure, operations,
advantages, disadvantages, and applications, along with practical
examples.

1.2 Basic Components

Array: The underlying structure is typically an array where


elements are stored.
Key-Value Pairs: Each entry in the hash map consists of a
key and a corresponding value.
Hash Function: A function that converts a key into a hash
code, which is then mapped to an index in the array.
1.2.1 Hash Function
A hash function is critical to the performance of a hash map. It transforms keys into
integers (hash codes) which dictate where the values will be stored. A good hash
function has the following characteristics:

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.

Example of a Simple Hash Function:

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):

return sum(ord(char) for char in key) % array_size

1.2.2Collision Handling

Collisions occur when multiple keys hash to the same


index. Common methods to handle collisions include:
Chaining: Each index of the array points to a linked
tlihset (or another structure) of entries that hash
to
same index.
Example:
plaintext Copy code Index 0 ->
[("key1", "value1"), ("key2", "value2")]
Index 1 -> [("key3", "value3")]

Open Addressing: When a collision occurs, the


algorithm searches for the next available slot.
Strategies include:
o Linear Probing: Checks the next slot
sequentially.
o Quadratic Probing: Checks slots based on a
quadratic function.
o Double Hashing: Uses a secondary hash
function to find the next slot.

1.3 4. Load Factor and Resizing


The load factor (λ) is defined as the number of entries (n) divided by the number of
slots (m) in the array:
Load Factor=nm\text{Load Factor} = \frac{n}{m}Load Factor=mn

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

To insert a key-value pair:

1 Compute the hash code using the hash function.


. Determine the index in the array using the hash code.
2 Store the key-value pair at that index, handling collisions if necessary (using chaining
. or probing).
3
.
Pseudocode for Insertion:

plaintext Copy code function


insert(key, value):

index = hash_function(key)
if array[index] is empty:
array[index] = (key, value)
else:
handle_collision(array[index], key, value)

2. Deletion

To delete a key-value pair:

1 Compute the hash code to find the index.


. Remove the entry at that index, adjusting for collisions if necessary.
2
.
Pseudocode for Deletion:

plaintext Copy code


function delete(key):
index = hash_function(key)
if array[index] contains key:
remove key-value pair from array[index]

3. Lookup
To retrieve a value:

1 Compute the hash code for the key.


. Access the appropriate index and search for the key within the list (if using chaining)
2 or follow the probing sequence (if using open addressing).
.
Pseudocode for Lookup:

plaintext Copy code


function lookup(key):

index = hash_function(key)
if array[index] contains key:
return value associated with key
return None

Performance Analysis
1. Time Complexity

Average Case: O(1) for insertion, deletion, and lookup.


Worst Case: O(n) when all keys collide (though this is mitigated by a good hash
function and load factor management).

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

Fast Access: Provides constant time complexity for average-case operations.


Dynamic Sizing: Automatically resizes as needed to maintain efficiency.
Flexibility: Supports a wide range of data types as keys and values.

Disadvantages of Hash Maps

Memory Overhead: Requires more memory compared to simpler data structures


like arrays.
Complexity of Implementation: Designing efficient hash functions and handling
collisions can complicate implementation.
Performance Degradation: In cases of poor hash functions leading to many
collisions, performance can degrade significantly.
Applications of Hash Maps

Database Indexing: Used in databases for quick data retrieval.


Caching Mechanisms: Frequently accessed data can be stored for rapid access.
Symbol Tables: Used in compilers and interpreters to store variable names and their
corresponding values.
Counting Frequencies: Efficiently counts occurrences of elements in a dataset (e.g.,
word frequency analysis).

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:

def __init__(self, size=10):


self.size = size
self.map = [[] for _ in range(size)]

def hash_function(self, key):


return sum(ord(char) for char in key) % self.size

def insert(self, key, value):


index = self.hash_function(key)
for pair in self.map[index]:
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

def lookup(self, key):


index = self.hash_function(key)
for pair in self.map[index]:
if pair[0] == key:
return pair[1]
return None # Key not found

def delete(self, key):


index = self.hash_function(key)
for i, pair in enumerate(self.map[index]):
if pair[0] == key:
del self.map[index][i]
return True
return False # Key not found

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

Hash maps are an essential data structure in


computer science, providing efficient storage and
retrieval of data through key-value pairs. Their fast
average-case performance and dynamic resizing
capabilities make them suitable for a wide range of
applications. However, careful attention must be paid
to the design of hash functions and collision handling
to ensure optimal performance. Understanding hash
maps is crucial for software developers and computer
scientists, as they are foundational to many
algorithms and systems.
2. Softwares for HASH MAP

Source Code

### 1. **Python: Built-in d`ict`**

- **Description**: Python's `dict `type is


a hash map that provides efficient key-
value storage.

- **Features**: Supports dynamic resizing,


handles collisions internally, and allows
for various data types as keys.

### 2. **Java: H`ashMap `Class**

J- ava* *Description**: Part of the


Collections Framework, H`ashMap `implements
the Map interface.

- **Features**: Allows null values and


keys, provides average O(1) time complexity
for basic operations, and is not
synchronized (not thread-safe).
### 3. **C++: `unordered_map`**

- **Description**: A part of the Standard


Template Library (STL), `unordered_map `
uses a hash table to store elements.

O- (1) **Features**: Offers average


complexity for search, insert, and delete
operations, supports custom hash functions,
and allows for automatic resizing.

### 4. **JavaScript: Objects and M`ap`**

- **Description**: JavaScript uses objects


as hash maps, while the M`ap ` object
provides a more robust implementation.

- **Features**: M`ap `maintains insertion


order, supports any data type for keys, and
provides O(1) average time complexity for
operations.

### 5. **Go: m`ap `Type**


- **Description**: Go has a built-in `map`
type that provides hash table
functionality.

- **Features**: Dynamic resizing, allows


any comparable type as keys, and provides
efficient performance for inserts and
lookups.

### 6. **C#: D`ictionary<TKey, TValue>`**

- **Description**: A part of the .NET


Framework, D`ictionary ` is a generic
collection that implements a hash table.

s- afet*y*,F eatures**: Provides type


allows null values for reference types, and
features automatic resizing.

### 7. **Ruby: H`ash`**

- **Description**: Ruby's H` ash `class is a


flexible and dynamic hash map
implementation.
- **Features**: Supports various key types,
maintains insertion order, and provides
efficient lookups.

### 8. **PHP: Associative Arrays**

a- sso**cDiaetsivcer iption**:
PHP uses
arrays, which function similarly to hash
maps.

- **Features**: Allows for key-value pairs,


supports multiple data types, and provides
efficient access.

### 9. **Rust: H`ashMap`**

- **Description**: Rust's standard library


includes H`ashMap,` which provides
efficient key-value storage.

- **Features**: Uses a cryptographic hash


function by default for better security,
offers concurrency support through
H`ashMap.`
SOURCE CODE :
class HashMap:
def __init__(self, size=10):
self.size = size
self.map = [[] for _ in range(size)] # Initialize a list of
empty lists for chaining

def hash_function(self, key):


"""Compute hash code for a given key."""
return sum(ord(char) for char in key) % self.size

def insert(self, key, value):


"""Insert a key-value pair into the hash map."""
index = self.hash_function(key)
# Check if the key already exists and update it
for pair in self.map[index]:

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

def lookup(self, key):


"""Retrieve a value by key from the hash map."""
index = self.hash_function(key)
for pair in self.map[index]:

if pair[0] == key:
return pair[1] # Return the value associated with
the key
return None # Key not found

def delete(self, key):


"""Remove a key-value pair from the hash map."""
index = self.hash_function(key)
for i, pair in enumerate(self.map[index]):
if pair[0] == key:
del self.map[index][i] # Remove the key-value pair
return True
return False # Key not found
References

[1] https:// https://www.w3schools.com/java/java_hashmap.asp


[2] https://
https://stackoverflow.com/questions/24486318/hashmap-
object-reference-confusion

[3] https://www.digitalocean.com/community/tutorials/java-
hashmap

You might also like