3
$\begingroup$

I am currently studying hash tables in an introductory course to computer science. I was taught that hash table is a data structure that associates a key to an index (a hash table) and then to the value associated to the key.

I don't understand why the hash function doesn't directly associate the key to the value

$\endgroup$
  • $\begingroup$ Think what could be the benefit when the set of possible indexes is much smaller than the set of possible keys. Or in a separate situation, where the set of possible keys is actually astronomically big. $\endgroup$ – Apiwat Chantawibul Jan 24 '17 at 23:42
  • 1
    $\begingroup$ The hash function is chosen by the hash table implementor without knowing what key-value pairs will actually be inserted by hash table users. $\endgroup$ – pyon Jan 25 '17 at 0:03
  • 1
    $\begingroup$ @pyon Not necessarily. Look up "perfect hashing" (also "minimal perfect hashing"). $\endgroup$ – Pseudonym Jan 25 '17 at 0:30
  • $\begingroup$ @Pseudonym: Whoa, thanks for the pointer! TIL something new. :-) $\endgroup$ – pyon Jan 25 '17 at 0:54
3
$\begingroup$

You cannot associate the key to a value directly, since you don't know in advance the set of keys. Hash tables implement an associative array, which is indexed by arbitrary objects. A naive implementation would have a huge array, indexed by all possible values of all possible types of objects. But this is clearly not realistic. Hash tables instead use a hash function to map a key to a numeric index, which is then used to address a conventional array.

If you knew the set of keys in advance, you could implement associative arrays as arrays indexed by the keys (like your suggestion). But it is not clear how to implement this. Suppose, for example, that the keys are all 3-regular graphs on 10 vertices. Given a 3-regular graph on 10 vertices, how do you associate it to a position in your array? It's not clear how to do this efficiently. Using a hash function, however, this becomes very easy.

$\endgroup$
2
$\begingroup$

The main use for a hash table is to structure data in such a way that when given a known (int, string, etc) one is able to easily find that specific object in memory.


An example would be a hash table which holds integers. Lets say the hash function of our hash table is (n modulo 10) or (n % 10).

Our dummy data will be: 1, 10, 15, 24, 51.

Passing each element through our hash function produces this hash table in memory:

[0] 1,10

[1] 51

[2]

[3]

[4] 24

[5] 15

[6]

[7]

[8]

[9]


Now lets say we want to find element: (15)

Passing 15 through our hash function leaves us with 5, when we look at memory address 5 we see our element 15!

You can think of how helpful hash tables can be when working with large amounts of data (think of a bank and how many customers they have, an employee cant afford to wait 10 minutes for the system to find 1 person).

These are some of the basic features of a hash table, but proper implementation of hash tables can drastically improve your programs speed.

$\endgroup$
  • $\begingroup$ I still don't understand why it woul be problematic not to have a direct key-value relation $\endgroup$ – Sylvester Stallone Jan 25 '17 at 7:49
  • $\begingroup$ @SylvesterStallone Because was able to define a structure up front without knowing all the key values. What would you use for a structure to just have a direct direct key-value relation? $\endgroup$ – paparazzo Jan 25 '17 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.