Hash Tables

BLUF

A hash table is a data structure that provides constant-time average-case access to elements based on their key. A hash table uses a hash function to map the key of each element to an index in an array.

A hash table in C++ is a data structure that implements an associative array, a structure that can map keys to values. In C++, hash tables are typically implemented using the Standard Template Library (STL) containers such as unordered_map or unordered_set.

Key Characteristics of Hash Tables:

  1. Efficient Data Access: Hash tables provide efficient data access. Ideally, the time complexity for insert, delete, and search operations is O(1) on average.

  2. Hash Function: A hash function is used to map keys to specific locations (buckets or slots) in the hash table. A good hash function aims to distribute keys uniformly across the table to minimize collisions (where two keys hash to the same location).

  3. Handling Collisions: Collisions are handled using techniques like chaining (where each bucket contains a list of all elements that hash to that bucket) or open addressing (where a collision results in the algorithm probing the table to find an empty slot).

  4. Dynamic Resizing: To maintain efficient operations, hash tables may dynamically resize. This involves rehashing all existing keys to new bucket locations when the load factor (ratio of the number of elements to the number of buckets) reaches a certain threshold.

Containers for Hash Tables:

  1. unordered_map:

    • A hash table that stores key-value pairs.

    • Keys are unique.

    • Provides fast access to values based on keys.

    Example:

    #include <unordered_map> std::unordered_map<std::string, int> ageMap; ageMap["Alice"] = 30; ageMap["Bob"] = 25;
  2. unordered_set:

    • A hash table that stores individual elements (keys only).

    • All elements are unique.

    • Useful for operations like checking the presence of an element.

    Example:

    #include <unordered_set> std::unordered_set<int> numbers; numbers.insert(1); numbers.insert(2);

Performance Considerations:

  • While average-case time complexity is O(1), worst-case time complexity is O(n), where n is the number of elements in the table. This happens when the hash function does not distribute elements well, leading to many collisions.

  • The choice of a hash function and the load factor threshold for resizing are crucial for performance.

Prime numbers are often chosen as the size of hash tables in computer science due to their properties which help in reducing the likelihood of collisions. This is particularly relevant in the context of hash functions, a key component of hash tables.

  1. Reducing Collisions: The main goal of a hash function is to distribute keys uniformly across the hash table. When the size of the hash table is a prime number, it tends to ensure a more uniform distribution of hash values. This is because a prime number is not a multiple of any number other than 1 and itself, which helps in evenly spreading out the data.

  2. Uniform Distribution: For hash functions that use modulo arithmetic (i.e., hash(key) % table_size), if the table size is a prime number, it reduces the likelihood that different keys will produce hash values that are multiples of the table size. This is because the only factors of a prime number are 1 and itself, which minimizes the chances of a regular pattern that could lead to collisions.

  3. Minimizing Clustering: Prime numbers help in minimizing clustering in hash tables. Clustering refers to the scenario where a hash table has many occupied consecutive slots. This can happen due to the way some hash functions work, especially with non-prime table sizes. A prime number size tends to spread out the keys more evenly, reducing the likelihood of clustering.

  4. Generalization: While not all hash functions will benefit equally from a prime number sized table, using a prime number is a good general rule that works well with many hash functions. It's a simple and effective way to improve the performance of a hash table without needing specific knowledge about the characteristics of the data or the hash function.

2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark