Hash Function in Data Structure: Types and Functions [With Examples] (2024)

Hash Function in Data Structures: An Overview

The hash function in Data Structures is a function that takes a key and returns an index into the hash table. Have you ever heard of hashing but aren't sure how it works or why it's important? Hashing is the process of mapping a variable-length input data set into a finite-sized output data set. It increases your efficiency in retrieving the desired result from a bunch of data sets and even storing it.

In this DSA tutorial, we will endeavor to unravel key hashing concepts, shedding light on its vital role and why it has emerged as a go-to technique for efficiently managing extensive datasets within the realm of the Dsa Course Online

What is Hashing in Data Structures?

In this technique, we give an input called a key to the hash function. The function uses this key and generates the unique index corresponding to that value in the hash table. After that, it returns the value stored at that index which is known as the hash value.

Data can be hashed into a shorter, fixed-length value for quicker access using a key or set of characters. This is how key-value pairs are stored in hash tables. The representation of the hash function looks like this:

 hash=hashfunc(key)

How does Hashing in Data Structures Work?

Hash Function in Data Structure: Types and Functions [With Examples] (1)

  • Hash key: It is the data you want to be hashed in the hash table. The hashing algorithm translates the key to the hash value. This identifier can be a string or an integer. There are some types of hash keys:
    1. Public key - It is an open key used solely for data encryption.
    2. Private key - It is known as a symmetric key used for both purposes, encryption and decryption.
    3. SSH public key - SSH is a set of both public and private keys.
  • Hash Function: It performs the mathematical operation of accepting the key value as input and producing the hash code or hash value as the output. Some of the characteristics of an ideal hash function are as follows:
    • It must produce the same hash value for the same hash key to be deterministic.
    • Every input has a unique hash code. This feature is known as the hash property.
    • It must be collision-friendly.
    • A little bit of change leads to a drastic change in the output.
    • The calculation must be quick
  • Hash Table: It is a type of data structure that stores data in an array format. The table maps keys to values using a hash function.

Read More - Data Structure Interview Questions for Experienced

Use cases of Hashing In DSA

  • Password Storage: Hash functions are commonly used to securely store passwords. Instead of storing the actual passwords, the system stores their hash values. When a user enters a password, it is hashed and compared with the stored hash value for authentication.
  • Data Integrity: Hashing is used to ensure data integrity by generating hash values for files or messages. By comparing the hash values before and after transmission or storage, it's possible to detect if any changes or tampering occurred.
  • Data Retrieval: Hashing is used in data structures like hash tables, which provide efficient data retrieval based on key-value pairs. The hash value serves as an index to store and retrieve data quickly.
  • Digital Signatures: Hash functions are an integral part of digital signatures. They are used to generate a unique hash value for a message, which is then encrypted with the signer's private key. This allows for verification of the authenticity and integrity of the message using the signer's public key.

Example of Hashing

  • Python
  • Java
  • C++
import hashlibdef sha256(input): hash_object = hashlib.sha256(input.encode('utf-8')) hex_digest = hash_object.hexdigest() return hex_digestdef main(): message = "Hello, world!" hash_value = sha256(message) print("Message: " + message) print("SHA-256 Hash: " + hash_value)if __name__ == "__main__": main() 
import java.nio.charset.StandardCharsets;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class Main { public static void main(String[] args) { String message = "Hello, world!"; String hashValue = sha256(message); System.out.println("Message: " + message); System.out.println("SHA-256 Hash: " + hashValue); } public static String sha256(String input) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8)); StringBuilder hexDigest = new StringBuilder(); for (byte b : hash) { String hex = String.format("%02x", b); hexDigest.append(hex); } return hexDigest.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }} 
#include <iostream>#include <iomanip>#include <sstream>#include <openssl/sha.h>std::string sha256(const std::string &input) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, input.c_str(), input.size()); SHA256_Final(hash, &sha256); std::stringstream ss; for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { ss << std::hex << std::setw(2) << std::setfill('0') << static_cast(hash[i]); } return ss.str();}int main() { std::string message = "Hello, world!"; std::string hashValue = sha256(message); std::cout << "Message: " << message << std::endl; std::cout << "SHA-256 Hash: " << hashValue << std::endl; return 0;} 

Output

Message: Hello, world!SHA-256 Hash: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3 

Master data structures and algorithms today! Enroll in our DSA Course now.

Hash Function in Data Structure: Types and Functions [With Examples] (2)

Training NameCourse Fee
Data Structures and Algorithms Course Register For Free Demo
Data Structures with Java Certification Training Register For Free Demo

Types of Hash Functions

The primary types of hash functions are:

  1. Division Method.
  2. Mid Square Method.
  3. Folding Method.
  4. Multiplication Method.

  1. Division Method

The easiest and quickest way to create a hash value is through division. The k-value is divided by M in this hash function, and the result is used.

Formula:

h(K) = k mod M

(where k = key value and M = the size of the hash table)

Advantages:

  • This method is effective for all values of M.
  • The division strategy only requires one operation, thus it is quite quick.

Disadvantages:

  • Since the hash table maps consecutive keys to successive hash values, this could result in poor performance.
  • There are times when exercising extra caution while selecting M's value is necessary.

Example of Division Method

k = 1987M = 13h(1987) = 1987 mod 13h(1987) = 4 

  1. Mid Square Method

The following steps are required to calculate this hash method:

  • k*k, or square the value of k
  • Using the middle r digits, calculate the hash value.

Formula:

h(K) = h(k x k)

(where k = key value)

Advantages:

  • This technique works well because most or all of the digits in the key value affect the result. All of the necessary digits participate in a process that results in the middle digits of the squared result.
  • The result is not dominated by the top or bottom digits of the initial key value.

Disadvantages:

  • The size of the key is one of the limitations of this system; if the key is large, its square will contain twice as many digits.
  • Probability of collisions occurring repeatedly.

Example of Mid Square Method

k = 60Therefore,k = k x kk = 60 x 60k = 3600Thus, h(60) = 60 

  1. Folding Method

The process involves two steps:

  • except for the last component, which may have fewer digits than the other parts, the key-value k should be divided into a predetermined number of pieces, such as k1, k2, k3,..., kn, each having the same amount of digits.
  • Add each element individually. The hash value is calculated without taking into account the final carry, if any.

Formula:

k = k1, k2, k3, k4, ….., kn

s = k1+ k2 + k3 + k4 +….+ kn

h(K)= s

(Where, s = addition of the parts of key k)

Advantages:

  • Creates a simple hash value by precisely splitting the key value into equal-sized segments.
  • Without regard to distribution in a hash table.

Disadvantages:

  • When there are too many collisions, efficiency can occasionally suffer.

Example of Folding Method

k = 12345k1 = 67; k2 = 89; k3 = 12Therefore,s = k1 + k2 + k3s = 67 + 89 + 12s = 168 

  1. Multiplication Method

  • Determine a constant value. A, where (0, A, 1)
  • Add A to the key value and multiply.
  • Consider kA's fractional portion.
  • Multiply the outcome of the preceding step by M, the hash table's size.

Formula:

h(K) = floor (M (kA mod 1))

(Where, M = size of the hash table, k = key value and A = constant value)

Advantages:

  • Any number between 0 and 1 can be applied to it, however, some values seem to yield better outcomes than others.

Disadvantages:

  • The multiplication method is often appropriate when the table size is a power of two since multiplication hashing makes it possible to quickly compute the index by key.

Example of Multiplication Method

k = 5678A = 0.6829M = 200Now, calculating the new value of h(5678):h(5678) = floor[200(5678 x 0.6829 mod 1)]h(5678) = floor[200(3881.5702 mod 1)]h(5678) = floor[200(0.5702)]h(5678) = floor[114.04]h(5678) = 114So, with the updated values, h(5678) is 114. 

What is a Hash Collision?

Collision in a hash table is a term used to denote the phenomena when the hashing algorithm produces the same hash value for two or more keys using a hash function. However, it's crucial to note that collisions are not an issue; rather, they constitute a key component of hashing algorithms. Because various hashing methods used in data structures convert each input into a fixed-length code regardless of its length, collisions happen. The hashing algorithms will eventually yield repeating hashes since there are an infinite number of inputs and a finite number of outputs.

Collision Resolution Techniques in Data Structures

  1. Open hashing/separate chaining/closed addressing
  2. Open addressing/closed hashing

  1. Open hashing/separate chaining/closed addressing

A typical collision handling technique called "separate chaining" links components with the same hash using linked lists. It is also known as closed addressing and employs arrays of linked lists to successfully prevent hash collisions.

Advantages:

  • Implementation is simple and easy
  • We can add more keys to the table because the hash table has a lot of empty places.
  • Less sensitive than average to changing load factors
  • Typically utilized when there is uncertainty on the number and frequency of keys to be used in the hash table.

Disadvantages:

  • Space is wasted
  • The length of the chain lengthens the search period.
  • Comparatively worse cache performance to closed hashing.

  1. Closed hashing (Open addressing)

Instead of using linked lists, open addressing stores each entry in the array itself. The hash value is not used to locate objects. To insert, it first verifies the array beginning from the hashed index and then searches for an empty slot using probing sequences. The probe sequence, with changing gaps between subsequent probes, is the process of progressing through entries. There are three methods for dealing with collisions in closed hashing:

  1. Linear Probing

Linear probing includes inspecting the hash table sequentially from the very beginning. If the site requested is already occupied, a different one is searched. The distance between probes in linear probing is typically fixed (often set to a value of 1).

Formula

index = key % hashTableSize

Sequence
index = ( hash(n) % T) (hash(n) + 1) % T (hash(n) + 2) % T (hash(n) + 3) % T … and so on.

  1. Quadratic Probing

The distance between subsequent probes or entry slots is the only difference between linear and quadratic probing. You must begin traversing until you find an available hashed index slot for an entry record if the slot is already taken. By adding each succeeding value of any arbitrary polynomial in the original hashed index, the distance between slots is determined.

Formula

index = index % hashTableSize

Sequence
index = ( hash(n) % T) (hash(n) + 1 x 1) % T (hash(n) + 2 x 2) % T (hash(n) + 3 x 3) % T … and so on

  1. Double-Hashing

The time between probes is determined by yet another hash function. Double hashing is an optimized technique for decreasing clustering. The increments for the probing sequence are computed using an extra hash function.

Formula

(first hash(key) + i * secondHash(key)) % size of the table

Sequence
index = hash(x) % S (hash(x) + 1*hash2(x)) % S (hash(x) + 2*hash2(x)) % S (hash(x) + 3*hash2(x)) % S … and so on

Importance of Hashing

  1. Easy retrieval of required information from large data sets in an efficient manner.
  2. The hash code produced by the hash function serves as the unique identifier in the data set thus maintaining data integrity.
  3. The data is stored in a structured manner as there is an index for every record in the hash table. This ensures efficient storage and retrieval.

Limitations of Hashing

  1. Many a time there leads to a situation of collision where two or more inputs have the same hash value.
  2. The performance of the hashing algorithm depends upon the quality of the hash function. Sometimes, a not well-thought-of hash function may lead to collisions thus reducing the efficiency of the algorithm.
Summary

For effective organization, data structures include hashing, which entails turning data into fixed-length values. Separate chaining and open addressing are the two primary hashing methods. Data is transformed into distinct fixed-length codes by hash methods like SHA-256. Hashing is useful for password storage, data integrity, and digital signatures and speeds up data retrieval while requiring less storage. Division, mid square, folding, and multiplication procedures are only a few of the several hash function kinds.

FAQs

Q1. What are the Types of Hash Functions?

The primary types of hash functions are:

  1. Division Method.
  2. Mid Square Method.
  3. Folding Method.
  4. Multiplication Method

Q2. What is a Collision in Hashing?

Collision in a hash table is a term used to denote the phenomena when the hashing algorithm produces the same hash value for two or more keys using a hash function.

Q3. What are the Collision Resolution Techniques in Data Structures?

  1. Open hashing/separate chaining/closed addressing
  2. Open addressing/closed hashing
Hash Function in Data Structure: Types and Functions [With Examples] (2024)

FAQs

What is a hash function with an example? ›

A hash function converts strings of different length into fixed-length strings known as hash values or digests. You can use hashing to scramble passwords into strings of authorized characters for example. The output values cannot be inverted to produce the original input.

What are the types of hashing in data structure? ›

There are some types of hash keys: Public key - It is an open key used solely for data encryption. Private key - It is known as a symmetric key used for both purposes, encryption and decryption. SSH public key - SSH is a set of both public and private keys.

What are the different types of hashes? ›

Hashing algorithms are just as abundant as encryption algorithms, but there are a few that are used more often than others. Some common hashing algorithms include MD5, SHA-1, SHA-2, NTLM, and LANMAN.

What are two common hash functions? ›

Explanation: Two common hash functions are B. SHA-256 and AES. The SHA-256 (Secure Hash Algorithm 256-bit) is widely used for securing data through its hash capabilities, ensuring data integrity by producing a fixed-size, unique hash value.

What is a real life example of a hash function? ›

Password verification

To authenticate a user, the password presented by the user is hashed and compared with the stored hash. A password reset method is required when password hashing is performed; original passwords cannot be recalculated from the stored hash value.

What are the two hash functions? ›

Perfect Hashing

It guarantees that no two keys will hash to the same value. Types: Minimal Perfect Hashing: Ensures that the range of the hash function is equal to the number of keys. Non-minimal Perfect Hashing: The range may be larger than the number of keys.

What are the 3 hashing techniques in data structure? ›

The most popular algorithms include the following: MD5: A widely used hashing algorithm that produces a 128-bit hash value. SHA-1: A popular hashing algorithm that produces a 160-bit hash value. SHA-256: A more secure hashing algorithm that produces a 256-bit hash value.

What is the best type of hashing? ›

What's the Most Secure Hashing Algorithm? SHA-256. SHA-256 (secure hash algorithm) is an algorithm that takes an input of any length and uses it to create a 256-bit fixed-length hash value.

What is the formula for hashing? ›

With modular hashing, the hash function is simply h(k) = k mod m for some m (usually, the number of buckets). The value k is an integer hash code generated from the key. If m is a power of two (i.e., m=2p), then h(k) is just the p lowest-order bits of k.

How many different types of hash are there? ›

How many hash varieties are there? The world of hash is vast, with numerous varieties, including but not limited to Bubble Hash, Lebanese Hash, Pakistani Hash, and Nepalese Hash. Each type of hashish hash is defined by unique production methods, offering various flavors, textures, and effects.

What are the hash types in SQL? ›

Hashing and Related Functions
  • BASE64_TO_HEX.
  • HEX_TO_BASE64.
  • IRI_TO_RESOURCE_HASH.
  • MD5.
  • SHA1.
  • SHA256.
  • SHA384. SHA512.

Can two hashes be the same? ›

That fact that two files have the same hash is possible, but extremely unlikely. This is what is known as a hash collision.

What are the different types of hashing functions? ›

  • Cyclic redundancy checks.
  • Checksums.
  • Universal hash function families.
  • Non-cryptographic hash functions.
  • Keyed cryptographic hash functions.
  • Unkeyed cryptographic hash functions.
  • See also.
  • References.

What is hash function example? ›

Say we are given keys in the range 0 to 999, and have a hash table of size 10. In this case, a possible hash function might simply divide the key value by 100. Thus, all keys in the range 0 to 99 would hash to slot 0, keys 100 to 199 would hash to slot 1, and so on.

What are the different types of hash tables in data structure? ›

The schemes of hashing used in integer universe assumption include hashing by division, hashing by multiplication, universal hashing, dynamic perfect hashing, and static perfect hashing. However, hashing by division is the commonly used scheme.

What is a hash function for dummies? ›

Hash function.

This function takes the input data and applies a series of mathematical operations to it, resulting in a fixed-length string of characters. The hash function ensures that even a small change in the input data produces a significantly different hash value.

Where are hash functions used today? ›

Hash functions are used in cryptography and have variable levels of complexity and difficulty. Hash functions are used for cryptocurrency, password security, and message security.

Which of the following are examples of hashing functions? ›

MD5, Triple DES, and SHA-1 are all examples of cryptographic hash functions. These are therefore suitable for cryptography. Cryptographic hash functions are important for cyber security and it helps in verifying the authenticity of any given data.

What is an example application where one-way hash functions are used? ›

Once the receiver has the encrypted value, the computer uses the same hash function to generate the hash value. It then compares this with the message. If the two are the same, the message was sent with no errors. The most familiar application of one-way hashing is through cryptocurrencies like Bitcoin.

Top Articles
Paul Merriman Ultimate Buy and Hold Portfolio Review & ETFs (2024)
Luxor West Bank Guide: What to Do, See, Eat & Sleep | Finding Beyond
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Umn Biology
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 5519

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.