What are hash functions? Write various popular hash function names. What is collision resolution? Explain with an example.


Q.) What are hash functions? Write various popular hash function names. What is collision resolution? Explain with an example.

Subject: Data Structures

Hash Functions:

Hash functions are mathematical functions that map a large set of input values to a smaller set of output values, known as hash values or message digests. These functions are essential for a wide range of applications, including data integrity verification, data retrieval, cryptography, and distributed database management.

Characteristics of Hash Functions:

  1. Deterministic: For a given input, a hash function always produces the same output.

  2. One-Way: It is computationally infeasible to derive the input from the hash value.

  3. Collision-Resistant: It is computationally difficult to find two distinct inputs that produce the same hash value.

Popular Hash Function Names:

  1. MD5 (Message Digest 5): A widely used hash function that produces a 128-bit hash value.

  2. SHA-1 (Secure Hash Algorithm 1): A more secure hash function than MD5, also producing a 160-bit hash value.

  3. SHA-256: A stronger variant of SHA-1, producing a 256-bit hash value.

  4. SHA-512: An even stronger variant of SHA-256, producing a 512-bit hash value.

  5. BLAKE2: A family of hash functions known for its high speed and security.

  6. Keccak: A hash function designed by the designers of BLAKE2, also known as SHA-3.

Collision Resolution:

Collisions occur when two distinct inputs produce the same hash value. This can happen even with well-designed hash functions due to the limited size of the output space. Collision resolution techniques are used to handle collisions and minimize their impact.

Popular Collision Resolution Techniques:

  1. Chaining: Each hash value is stored with a linked list of input values that produced that hash value.

  2. Open Addressing: Each hash value is stored in an array, and collisions are resolved by searching for an empty slot in the array.

  3. Cuckoo Hashing: A hashing technique that uses multiple hash functions and tables to reduce collisions.

Example of Collision Resolution Using Chaining:

Consider a hash table of size 10 and a hash function that maps input values to integers in the range 0 to 9. Let's insert the following values into the hash table:

  1. 100 -> Hash Value: 0
  2. 200 -> Hash Value: 0
  3. 300 -> Hash Value: 0

Since all three values have the same hash value, a collision occurs. Using chaining, we can store these values in a linked list at the index 0 of the hash table:

Hash Table:

Index 0: [100, 200, 300]

When searching for a value in the hash table, the hash function is used to determine the index of the linked list containing the value. Then, we can traverse the linked list to find the specific value.

In conclusion, hash functions are essential for various applications requiring data verification, retrieval, and security. Popular hash functions like MD5, SHA-1, and SHA-256 are widely used. Collision resolution techniques, such as chaining and open addressing, are employed to handle collisions and maintain the efficiency of hash tables.