0% found this document useful (0 votes)
30 views

Hashing

Uploaded by

ahmedthomas909
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Hashing

Uploaded by

ahmedthomas909
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

2024

Hashing Techniques
SUPERVISOR: Eng/ Noor El-Deen Magdy

BY: Ahmed Mamdouh

Faculty of Engineering,
𝟐𝒏𝒅 Year Computer and Systems Department
Intro to Hashing:
 Hashing is a data processing technique that maps data elements
(keys) to a specific location in memory, known as a hash table, using a
hash function. This technique provides efficient access to data with
average time complexity of O(1) for insertion, deletion, and search
operations.
 Key Components:
1-Hash Function: Maps keys to indices in the hash table.
Example: h(k)=k%m, where m is the table size.
2-Hash Table: An array-like data structure to store key-value pairs.
3-Collision Handling: When multiple keys hash to the same index,
collisions occur. Collision resolution is necessary for efficient
operation.
 Collision Resolution Techniques:
1-Separate Chaining:
o Each hash table index stores a linked list of key-value pairs.
o Example: Index 2 may hold a linked list if keys 12 and 22 collide.
2-Open Addressing:
 Probes for the next available slot in the hash table. Types include:
o Linear Probing: Check the next slot sequentially until an empty
slot is found. ex: h(k,i)=(h(k)+i)%m.
o Quadratic Probing: Check slots using quadratic increments.
Example: h(k,i)=(h(k)+i^2)%m.
o Double Hashing: Use a secondary hash function to find slots.
Example: h(k,i)=(h1(k)+i×h2(k))%

3-Perfect Hashing:
Design a collision-free hash function for a fixed dataset.
*Example with Implementation:
 Linked List:
#include <iostream>
#include <list>
using namespace std;

class HashTable {
int size;
list<int>* table;
public:
HashTable(int size) {
this->size = size;
table = new list<int>[size];
}

int hashFunction(int key) {


return key % size;
}

void insert(int key) {


int index = hashFunction(key);
table[index].push_back(key);
}

void display() {
for (int i = 0; i < size; i++) {
cout << i << ": ";
for (int key : table[i]) {
cout << key << " -> ";
}
cout << "null" << endl;
}
}
};

*Use cases & life applications:


1-Hashing provides constant time search, insert and delete operations
on average. This is why hashing is one of the most used data structure,
example problems are, distinct elements, counting frequencies of
items, finding duplicates, etc.
2-Database indexing: Hashing is used to index and retrieve data
efficiently in databases and other data storage systems.
3-Dictionaries : To implement a dictionary so that we can quickly
search a word
4-Password storage: Hashing is used to store passwords securely by
applying a hash function to the password and storing the hashed
result, rather than the plain text password.
5-Network Routing: Determining the best path for data packets
6-Bloom Filters : Bloom filter is a space optimized and probabilistic
version of hashing and has huge applications like spam filtering,
recommendations.
7-Cryptography: Hashing is used in cryptography to generate digital
signatures, message authentication codes (MACs), and key derivation
functions.
8-Load balancing: Hashing is used in load-balancing algorithms, such
as consistent hashing, to distribute requests to servers in a network.
9-Blockchain: Hashing is used in blockchain technology, such as the
proof-of-work algorithm, to secure the integrity and consensus of the
blockchain.
10-Image processing: Hashing is used in image processing applications,
such as perceptual hashing, to detect and prevent image duplicates
and modifications.
11-File comparison: Hashing is used in file comparison algorithms,
such as the MD5 and SHA-1 hash functions, to compare and verify the
integrity of files.
12-Fraud detection: Hashing is used in fraud detection and
cybersecurity applications, such as intrusion detection and antivirus
software, to detect and prevent malicious activities.
13-Caching: Storing frequently accessed data for faster retrieval. For
example browser caches, we can use URL as keys and find the local
storage of the URL.
14-Symbol Tables: Mapping identifiers to their values in programming
languages
15-Associative Arrays: Associative arrays are nothing but hash tables
only. Commonly SQL library functions allow you retrieve data as
associative arrays so that the retrieved data in RAM can be quickly
searched for a key.

You might also like