Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a hash table take up more memory than other data-structures?

I've been doing some reading about hash tables, dictionaries, etc. All literature and videos that I have watched imply to hash-tables as having the space/time trade-off property.

I am struggling to understand why a hash table takes up more space than, say, an array or a list with the same number of total elements (values)? Does it have something to do with actually storing the hashed keys?

As far as I understand and in basic terms, a hash table takes a key identifier (say some string), passes it through some hashing function, which spits out an index to an array or some other data-structure. Apart from the obvious memory usage to store your objects (values) in the array or table, why does a hash table use up more space? I feel like I am missing something obvious...

like image 205
rex Avatar asked Nov 02 '22 03:11

rex


1 Answers

Like you say, it's all about the trade-off between lookup time and space. The larger the number of spaces (buckets) the underlying data structure has, the greater the number of locations the hash function has where it can potentially store each item, and so the chance of a collision (and therefore worse than constant-time performance) is reduced. However, having more buckets obviously means more space is required. The ratio of number of items to number of buckets is known as the load factor, and is explained in more detail in this question: What is the significance of load factor in HashMap?

In the case of a minimal perfect hash function, you can achieve O(1) performance storing n items in n buckets (a load factor of 1).

like image 83
mtripp100 Avatar answered Nov 15 '22 13:11

mtripp100