Nepedia
Back to Discover
Advanced Reader
💻

Hash table

technology Maturity 7-9 Next article →

A hash table is a fundamental data structure used in computer science to implement an associative array. An associative array is a type of abstract data structure that maps unique keys to specific values. You might know these structures by other names, such as a dictionary or a map.

Hash table 3 1 1 0 1 0 0 SP
Hash table 3 1 1 0 1 0 0 SP

Hash tables are vital because they allow for very efficient data management. They are used widely in software for database indexing, caches, and sets. Many modern programming languages provide these structures built-in. For example, Python uses dictionaries, while Java uses the HashMap. C++ provides the unordered_map, and Go uses maps. These built-in tools handle the complex math of hashing for the programmer.

The mechanism of a hash table relies on a mathematical process called hashing. To store data, the table takes a key and passes it through a hash function. This function computes an index, which is also called a hash code. This index points to a specific location in an array of buckets or slots.

Hash table 5 0 1 1 1 1 0 SP
Hash table 5 0 1 1 1 1 0 SP

The key and its associated value are then stored at that index. When a user wants to look up a value, the computer hashes the key again. This immediately indicates exactly where the corresponding value is located. In a well-designed table, the time it takes to find an item is independent of how many items are stored. This makes searching extremely fast compared to other methods.

However, most hash functions are imperfect. This leads to a situation called a hash collision. A collision occurs when the hash function generates the same index for two different keys. Because of this, every hash table must have a strategy to resolve these conflicts. One common method is called separate chaining.

Hash table 5 0 1 1 1 1 1 LL
Hash table 5 0 1 1 1 1 1 LL

In separate chaining, each slot in the bucket array holds a pointer to a linked list. If multiple items land in the same slot, they are chained together in that list. Another method is open addressing.

Hash table 5 0 1 1 1 1 0 LL
Hash table 5 0 1 1 1 1 0 LL

In open addressing, each slot holds only one item. If a collision occurs, the system follows a probing sequence to find the next available empty slot.

Efficiency in a hash table is often measured by the load factor. The load factor is the ratio of the number of stored elements to the total number of available slots. If the load factor is too high, the performance of the table begins to deteriorate. Software often manages this by resizing the table, a process known as rehashing. For separate chaining, the best performance usually happens when the load factor is between 1 and 3. For open addressing, the load factor can never exceed 1. In open addressing, performance drops sharply as the load factor approaches 1. Therefore, developers usually resize the table when the load factor reaches 0.6 to 0.75.

There are several ways to create a hash function. One common method is hashing by division. This involves taking the key and finding the remainder after dividing by the table size. Another method is hashing by multiplication. This uses a non-integer real-valued constant. The mathematician Donald Knuth suggested using the golden ratio for this method. When keys are strings rather than numbers, different rules apply. A simple string hash might involve shifting bits and using the XOR operation. Another approach is the polynomial rolling hash function. A perfect hash function is one where every key maps to a unique index. These can be created if all keys are known before the table is built.

The history of hashing involves many independent discoveries. In January 1953, Hans Peter Luhn wrote an internal IBM memorandum describing hashing with chaining. Shortly after, A. D. Linh proposed the first example of open addressing. At the same time, researchers at IBM like Gene Amdahl and Arthur Samuel implemented hashing for the IBM 701 assembler. Amdahl is also credited with open addressing using linear probing, an idea also held by Andrey Ershov. The term "open addressing" was later coined by W. Wesley Peterson. The word "hashing" itself was first published by Robert Morris. These early developments laid the groundwork for modern computing.

Using hash tables involves a specific space-time tradeoff. This means that developers must choose between using more memory or more processing time. If a computer had infinite memory, it could use the entire key as an index for instant access. If infinite time were available, the computer could ignore keys and use a linear search instead. In the real world, hash tables are often more efficient than search trees. They provide a balanced way to handle large amounts of data quickly. This makes them a cornerstone of efficient computer programming and data organization.

773 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (4)
File:Hash table 3 1 1 0 1 0 0 SP.svg
Hash table 3 1 1 0 1 0 0 SP.svg
File:Hash table 5 0 1 1 1 1 1 LL.svg
Hash table 5 0 1 1 1 1 1 LL.svg
File:Hash table 5 0 1 1 1 1 0 LL.svg
Hash table 5 0 1 1 1 1 0 LL.svg
File:Hash table 5 0 1 1 1 1 0 SP.svg
Hash table 5 0 1 1 1 1 0 SP.svg
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.