Horje
Hashing in Data Structure

Hashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. It involves mapping data to a specific index in a hash table using a hash function that enables fast retrieval of information based on its key. This method is commonly used in databases, caching systems, and various programming applications to optimize search and retrieval operations. The great thing about hashing is, we can achieve all three operations (search, insert and delete) in O(1) time on average.

Data Structures – Hashing

What is Hashing in Data Structure?

Hashing is a technique used in data structures to store and retrieve data efficiently. It involves using a hash function to map data items to a fixed-size array which is called a hash table. Below are basic terminologies in hashing.

  1. Hash Function: You provide your data items into the hash function.
  2. Hash Code: The hash function crunches the data and give a unique hash code. This hash code is typically integer value that can be used an index.
  3. Hash Table: The hash code then points you to a specific location within the hash table.

Hash Table in Data Structure

A hash table is also referred as a hash map (key value pairs) or a hash set (only keys). It uses a hash function to map keys to a fixed-size array, called a hash table. This allows in faster search, insertion, and deletion operations.

Hash Function

The hash function is a function that takes a key and returns an index into the hash table. The goal of a hash function is to distribute keys evenly across the hash table, minimizing collisions (when two keys map to the same index).

Common hash functions include:

  • Division Method: Key % Hash Table Size
  • Multiplication Method: (Key * Constant) % Hash Table Size
  • Universal Hashing: A family of hash functions designed to minimize collisions

What is a Hash Collision?

A hash collision occurs when two different keys map to the same index in a hash table. This can happen even with a good hash function, especially if the hash table is full or the keys are similar.

Causes of Hash Collisions:

  • Poor Hash Function: A hash function that does not distribute keys evenly across the hash table can lead to more collisions.
  • High Load Factor: A high load factor (ratio of keys to hash table size) increases the probability of collisions.
  • Similar Keys: Keys that are similar in value or structure are more likely to collide.

Collision Resolution Techniques

There are two types of collision resolution techniques:

  1. Open Addressing:
    • Linear Probing: Search for an empty slot sequentially
    • Quadratic Probing: Search for an empty slot using a quadratic function
  2. Closed Addressing:
    • Chaining: Store colliding keys in a linked list or binary search tree at each index
    • Cuckoo Hashing: Use multiple hash functions to distribute keys

Applications of Hashing

Hash tables are used wherever we have a combinations of search, insert and/or delete operations.

  • Dictionaries : To implement a dictionary so that we can quickly search a word
  • Databases: Hashing is used in database indexing. There are two popular ways to implement indexing, search trees (B or B+ Tree) and hashing.
  • Cryptography : When we create a password on a website, they typically store it after applying a hash function rather than plain text
  • 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.
  • Symbol Tables: Mapping identifiers to their values in programming languages
  • Network Routing: Determining the best path for data packets
  • 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.

Please refer Applications of Hashing for more detailed list.

Basics of Hashing in Data Structure

Easy Problem on Hashing

Medium Problem on Hashing

Hard Problem on Hashing

Quick Links :

Recommended:




Reffered: https://www.geeksforgeeks.org


DSA

Related
Transform Strings by Frequency Exchange and Swapping Transform Strings by Frequency Exchange and Swapping
Shortest path between two cities without running out of fuel Shortest path between two cities without running out of fuel
Types of Heap Data Structure Types of Heap Data Structure
Smallest subarray with positive sum for all indices Smallest subarray with positive sum for all indices
String | Palindrome String | Palindrome

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14