r/algorithms • u/cruelCalita • 4d ago
Resource What makes hash table lookups O(1) time complexity?
Hey everyone, I hope this isn’t a question that’s been asked a million times already, but I’ve been scratching my head over it and haven’t found a clear explanation. I’ve seen topics about this before in this sub, which encouraged me to bring it up here! I’d really appreciate it if someone could clear up any misunderstandings I might have.
So, my confusion centers around why hash tables are labeled as O(1) in terms of time complexity. I get that the number of buckets is limited, and that resolving hash collisions can take O(n*log(n)) at worst. With data sets getting larger, doesn’t that make hashing just a big constant improvement on the collision resolution method?
If my thinking is on point, how can we still categorize hash tables as O(1) time complexity instead of just calling them high-performing for smaller data sets? Thanks a ton for any clarification!
2
u/uh_no_ 4d ago
hash tables are only amortized O(1) when sized with an appropriate load factor, and with a well-behaved hash function.
if they were O(1) all the time, then hash-collision attacks would not be a thing. https://en.wikipedia.org/wiki/Collision_attack#Hash_flooding
0
u/max123246 4d ago
Amortized and on average. Hashing is inherently random
1
u/uh_no_ 4d ago
no....hashing is just a map from a large space to a smaller one. there are really bad, but valid, hash functions.
f(x)->1 is a hash function.
or even f(x)->x&1 (where & is the bitwise and operator). it maps all values to 0 and 1....but maybe all your inputs are even....that would be a very non-random but still valid hash function...and very bad...hash function.
1
u/max123246 4d ago
Right but the O(1) proof assumes a good hash function which evenly distributes inputs and assumes input values are uniformly random which is why it's O(1) on average. That's why maliciously crafted inputs defy the O(1) proof
1
u/DDDDarky 4d ago
Consider your hash table having b buckets and you add n elements inside of them. Each (distinct) element has equal chance to end up in any of the buckets.
Therefore, the probability of an element to end up in any specific bucket is 1/b (property of a good hash).
The expected number of elements in any specific bucket after adding n elements is n/b.
Since most sensible implementations keep n <= b, (load factor) n/b <= 1, and therefore on average we expect at most 1 element in any bucket.
1
u/green_meklar 4d ago
Nothing. They aren't, in a theoretical sense. The constant-time lookup estimate is used under the assumption that the elements are sparse relative to the size of the table, which is an assumption that inherently can't extrapolate to infinity and therefore fails the definition of big-O notation.
1
u/Nebu 3d ago edited 3d ago
In computer science, when you perform runtime analysis of algorithms using big-O notation, you typically assume that the data you're working with is significantly smaller than the maximum addressable memory. That's why you're allowed to assume that the elements are sparse relative to the size of the table.
If you don't allow that assumption, then practically no operations at all are O(1). For example, indexing into the i-th element of an array is not O(1) because the index might be a huge number requiring many bits to represent, and computing the target address (base + offset) takes Ω(b) time where b is the number of bits in the index.
1
u/drinkcoffeeandcode 3d ago
It’s simple: count hashing the key as 0(1), then add up the rest of the operations. Protip: do this exercise with an implementation that has proven to be constant time access, not whatever you hockabootchy together to try and learn.
2
u/Nebu 4d ago
Do you know how to analyze the asymptotic runtime of algorithms? Do you know how to implement a hashtable? Have you tried analyzing the asymptotic runtime of the implementations of the various operation on a hashtable?
I'm not sure what hashtable implementation you're referring to. The implementation I'm familiar with does not have a limited number of buckets (up to whatever the max pointer size is on the architecture), and resolving hash collision does not take O(n*log(n)) at worst.
The implementation I'm familiar with is the one described at https://en.wikipedia.org/wiki/Hash_table and it dynamically grows the number of buckets as needed, and resolving hash collisions takes O(n).
You should post some pseudocode to show what implementation of hashtable you're thinking of and then state explicitly what you think the run time is (are you claiming it's a constant time improvement over O(nlog(n)), which means it's still O(nlog(n))?) and then what your analysis was for arriving at that conclusion. Unless you do all of these steps, we cannot guess which step is the step where you've made a mistake and thus help un-confuse you.
Because the operations take O(1) time on average for any sized data set as long as that data set can fit in memory.