What are Hash Functions and How to choose a good Hash Function? - GeeksforGeeks (2024)

Prerequisite: Hashing | Set 1 (Introduction)

What is a Hash Function?

A function that converts a given big phone number to a small practical integer value. The mapped integer value is used as an index in the hash table. In simple terms, a hash function maps a big number or string to a small integer that can be used as the index in the hash table.

What is meant by Good Hash Function?

A good hash function should have the following properties:

  1. Efficiently computable.
  2. Should uniformly distribute the keys (Each table position equally likely for each key)

For example: For phone numbers, a bad hash function is to take the first three digits. A better function is considered the last three digits. Please note that this may not be the best hash function. There may be better ways.

Rules for choosing good hash function:

1. The hash function should be simple to compute.

2. Number of collisions should be less while placing the record in the hash table.Ideally no collision should occur. Such a function is called perfect hash function.

3. Hash function should produce such keys which will get distributed uniformly over an array.

4. The hash function should depend on every bit of the key. Thus the hash function that simply extracts the portion of a key is not suitable.

In practice, we can often employ heuristic techniques to create a hash function that performs well. Qualitative information about the distribution of the keys may be useful in this design process. In general, a hash function should depend on every single bit of the key, so that two keys that differ in only one bit or one group of bits (regardless of whether the group is at the beginning, end, or middle of the key or present throughout the key) hash into different values. Thus, a hash function that simply extracts a portion of a key is not suitable. Similarly, if two keys are simply digited or character permutations of each other (such as 139 and 319), they should also hash into different values.

The two heuristic methods are hashing by division and hashing by multiplication which are as follows:

  1. The mod method:
    • In this method for creating hash functions, we map a key into one of the slots of table by taking the remainder of key divided by table_size. That is, the hash function is
h(key) = key mod table_size i.e. key % table_size
  • Since it requires only a single division operation, hashing by division is quite fast.
  • When using the division method, we usually avoid certain values of table_size like table_size should not be a power of a number suppose r, since if table_size = r^p, then h(key) is just the p lowest-order bits of key. Unless we know that all low-order p-bit patterns are equally likely, we are better off designing the hash function to depend on all the bits of the key.
  • It has been found that the best results with the division method are achieved when the table size is prime. However, even if table_size is prime, an additional restriction is called for. If r is the number of possible character codes on an computer, and if table_size is a prime such that r % table_size equal 1, then hash function h(key) = key % table_size is simply the sum of the binary representation of the characters in the key mod table_size.
  • Suppose r = 256 and table_size = 17, in which r % table_size i.e. 256 % 17 = 1.
  • So for key = 37599, its hash is
37599 % 17 = 12
  • But for key = 573, its hash function is also
573 % 17 = 12
  • Hence it can be seen that by this hash function, many keys can have the same hash. This is called Collision.
  • A prime not too close to an exact power of 2 is often good choice for table_size.
  1. The multiplication method:
    • In multiplication method, we multiply the key k by a constant real number c in the range 0 < c < 1 and extract the fractional part of k * c.
    • Then we multiply this value by table_size m and take the floor of the result. It can be represented as
h(k) = floor (m * (k * c mod 1)) orh(k) = floor (m * frac (k * c))
  • where the function floor(x), available in standard library math.h, yields the integer part of the real number x, and frac(x) yields the fractional part. [frac(x) = x – floor(x)]
  • An advantage of the multiplication method is that the value of m is not critical, we typically choose it to be a power of 2 (m = 2p for some integer p), since we can then easily implement the function on most computers
  • Suppose that the word size of the machine is w bits and that key fits into a single word.
  • We restrict c to be a fraction of the form s / (2w), where s is an integer in the range 0 < s < 2w.
  • Referring to figure, we first multiply key by the w-bit integer s = c * 2w. The result is a 2w-bit value
r1 * 2w + r0where r1 = high-order word of the product r0 = lower order word of the product
  • Although this method works with any value of the constant c, it works better with some values than the others.
c ~ (sqrt (5) – 1) / 2 = 0.618033988 . . .
  • is likely to work reasonably well.
  • Suppose k = 123456, p = 14,
  • m = 2^14 = 16384, and w = 32.
  • Adapting Knuth’s suggestion, c to be fraction of the form s / 2^32.
  • Then key * s = 327706022297664 = (76300 * 2^32) + 17612864,
  • So r1 = 76300 and r0 = 176122864.
  • The 14 most significant bits of r0 yield the value h(key) = 67.


R

ranadeepika2409

What are Hash Functions and How to choose a good Hash Function? - GeeksforGeeks (1)

Improve

Next Article

Hash Functions and Types of Hash functions

Please Login to comment...

What are Hash Functions and How to choose a good Hash Function? - GeeksforGeeks (2024)

FAQs

What are hash functions and how to choose a good hash function? ›

Key Properties of Hash Functions
  1. Deterministic: A hash function must consistently produce the same output for the same input.
  2. Fixed Output Size: The output of a hash function should have a fixed size, regardless of the size of the input.
  3. Efficiency: The hash function should be able to process input quickly.
May 20, 2024

What is meant by hash function? ›

A hash function is a mathematical function or algorithm that simply takes a variable number of characters (called a ”message”) and converts it into a string with a fixed number of characters (called a hash value or simply, a hash).

What are the requirements of a hash function? ›

Hash Function Requirements
  • H can be applied to a block of data of any size.
  • H produces a fixed length output.
  • H(x) is relatively easy to compute.
  • For any given code h, it is computationally infeasible to find x such that H(x) = h.
  • For any given block x, it is computationally infeasible to find y  x with H(y) = H(x)

What are two common hash functions choose? ›

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 are the four basic properties to be a good hash function? ›

Key Characteristics of a Hash Function
  • Deterministic - The same input will give the same output.
  • Collision Resistant - It is unlikely that different inputs will map to the same output.
  • Fixed Size Output - The output will always be the same size no matter the input.

What makes good hash? ›

Visual characteristics of good-quality types of hash

Good-quality, pressed dry-sift ranges in appearance from light blondish-brown, soft, crumbly (almost “sandy”) in texture (for lightly-pressed hash), to dark-brown, shiny and hard (for heavily-pressed types).

Why are hash functions useful? ›

Hash functions are commonly used data structures in computing systems for tasks such as checking the integrity of messages and authenticating information. While they are considered cryptographically "weak" because they can be solved in polynomial time, they are not easily decipherable.

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.

Why is it called a hash function? ›

The term "hash" comes by way of analogy with its non-technical meaning, to "chop and mix". Indeed, typical hash functions, like the mod operation, "chop" the input domain into many sub-domains that get "mixed" into the output range to improve the uniformity of the key distribution.

What are two properties that a good hash function should have? ›

1. The hash function should be simple to compute. 2. Number of collisions should be less while placing the record in the hash table.

What are five key properties of a hashing function? ›

Certain properties of cryptographic hash functions impact the security of password storage.
  • Non-reversibility, or one-way function. A good hash should make it very hard to reconstruct the original password from the output or hash.
  • Diffusion, or avalanche effect. ...
  • Determinism. ...
  • Collision resistance. ...
  • Non-predictable.
Dec 10, 2015

What are two major desired roles of a hash function? ›

There are two major purposes of hashing functions:
  • to disperse data points uniformly into n bits.
  • to securely identify the input data.
Aug 29, 2008

How do you choose the best hash function? ›

Choosing a good hashing function, h(k), is essential for hash-table based searching. h should distribute the elements of our collection as uniformly as possible to the "slots" of the hash table. The key criterion is that there should be a minimum number of collisions.

What function can serve as a good hash function? ›

A good hash function to use with integer key values is the mid-square method. The mid-square method squares the key value, and then takes out the middle r bits of the result, giving a value in the range 0 to 2r−1. This works well because most or all bits of the key value contribute to the result.

What is the most commonly used hash function? ›

The MD5 algorithm, defined in RFC 1321, is probably the most well-known and widely used hash function. It is the fastest of all the . NET hashing algorithms, but it uses a smaller 128-bit hash value, making it the most vulnerable to attack over the long term.

What is a good hash code function? ›

A good hash function to use with integer key values is the mid-square method. The mid-square method squares the key value, and then takes out the middle r bits of the result, giving a value in the range 0 to 2r−1. This works well because most or all bits of the key value contribute to the result.

Which hash functions are recommended now? ›

SHA-256 it's a NIST's (National Institute of Standards and Technology) recommended and officially approved standard algorithm.

What is hash explained? ›

A hash is a mathematical function that converts an input of arbitrary length into an encrypted output of a fixed length. Thus, regardless of the original amount of data or file size involved, its unique hash will always be the same size.

What is a perfect hash function in data structure? ›

In computer science, a perfect hash function h for a set S is a hash function that maps distinct elements in S to a set of m integers, with no collisions. In mathematical terms, it is an injective function.

Top Articles
I've Been Catfished! What Do I Do? Expert Advice & Tips
Catfish guide — The Cyber Helpline
Ohio Houses With Land for Sale - 1,591 Properties
Ron Martin Realty Cam
Blorg Body Pillow
The UPS Store | Ship & Print Here > 400 West Broadway
Pangphip Application
Jefferey Dahmer Autopsy Photos
Es.cvs.com/Otchs/Devoted
Google Jobs Denver
Poe Pohx Profile
How Much Is 10000 Nickels
Otis Department Of Corrections
Volstate Portal
Overzicht reviews voor 2Cheap.nl
Wmlink/Sspr
Swimgs Yung Wong Travels Sophie Koch Hits 3 Tabs Winnie The Pooh Halloween Bob The Builder Christmas Springs Cow Dog Pig Hollywood Studios Beach House Flying Fun Hot Air Balloons, Riding Lessons And Bikes Pack Both Up Away The Alpha Baa Baa Twinkle
A Fashion Lover's Guide To Copenhagen
Uvalde Topic
Palace Pizza Joplin
Tracking Your Shipments with Maher Terminal
Tcgplayer Store
Craigslist Farm And Garden Cincinnati Ohio
Define Percosivism
Parent Resources - Padua Franciscan High School
Our History
Scout Shop Massapequa
Beverage Lyons Funeral Home Obituaries
zom 100 mangadex - WebNovel
Dewalt vs Milwaukee: Comparing Top Power Tool Brands - EXTOL
Inter Miami Vs Fc Dallas Total Sportek
Rainfall Map Oklahoma
Meggen Nut
Willys Pickup For Sale Craigslist
Mia Malkova Bio, Net Worth, Age & More - Magzica
Pickle Juiced 1234
Restored Republic December 9 2022
How to play Yahoo Fantasy Football | Yahoo Help - SLN24152
M Life Insider
Improving curriculum alignment and achieving learning goals by making the curriculum visible | Semantic Scholar
How to Get a Better Signal on Your iPhone or Android Smartphone
Clima De 10 Días Para 60120
10 Rarest and Most Valuable Milk Glass Pieces: Value Guide
Craigslist Com Panama City Fl
Deepwoken: How To Unlock All Fighting Styles Guide - Item Level Gaming
Watch Chainsaw Man English Sub/Dub online Free on HiAnime.to
4k Movie, Streaming, Blu-Ray Disc, and Home Theater Product Reviews & News
Image Mate Orange County
Spongebob Meme Pic
Mike De Beer Twitter
How To Connect To Rutgers Wifi
Gainswave Review Forum
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 6325

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.