What is the complexity of this method in the worse case scenario. Is it still O(1) even with the worse case scenario?
To answer your question, you really have to define what you mean by worst-case. The implementation of a hash table is very dependent on two factors: the (probability) distribution of data you intend to store in the hash table, and what hash function you define. Knowing these two factors you can do an average case analysis. Without these two pieces of information, you can assume the worst, for example:
- you will always get the same value to store say the number '1'
- your hash function is f(x) = x
In this case, if you are using linked-lists for buckets, your worst case is indeed O(n).
If, however, you have more information, such that your data follows a uniform distribution and you use a sensible hash function, you can show in the average case most operations should be O(1). In the previous sentence, the descriptor "average" is precisely defined by averaging over the probability distribution your data follows.
I'm not clear how detailed an analysis you require, but typically with open-addressing (doesn't matter which) the analysis of average runtime is as follows.
Let a = n / N
be the load factor of the hash-table, where n
is the number of elements stored and N
the number of buckets. Assuming no clustering problems (due to data or hash function) and that all probes are equally likely (proof that this is true for quadratic probing is separate), then you can assert
P(probe hits occupied bucket) = a
P(probe hits unoccupied bucket) = 1-a
P(probe hits unoccupied bucket in 2 steps) = a (1-a)
P(probe hits unoccupied bucket in k steps) = a^{k-1} (1-a)
therefore the average runtime of probing is
E(number of steps in probe) = \sum {for k = 0 to m} k a^{k-1}(1-a)
<=\sum {for k = 0 to infty} k a^{k-1}(1-a)
= (1-a) / (1-a)^2
= 1 / (1 - a)
where the infinite sum is given by arithmetico-geometric series, and m
is some upperbound on the number of steps in the probing (dependent on the probing technique, usually requires a separate proof that the probing technique terminates in a finite # of steps m
.)
If we maintain a reasonable load factor a
, say a = n / N = .5
, then
E(number of steps in probe) <= 1 / (1-.5) = 2
thus E(number of steps in probe)
is O(1).
The crux of the entire analysis rests in being able to recognize that the infinite sum has a nice closed form that converges.
To recap, the proof I gave above holds in the general case, but requires the following conditions on your probing technique that you are using (that may need to be proved separately):
- the probing technique yields a uniform probability based on load factor of hitting an unoccupied/occupied bucket (i.e.
P(probe hits occuppied bucket) = a
)
- the probing technique terminates in a finite number of steps
Depending on how detailed your analysis must be, you may have to prove these two properties of quadratic probing to complete the proof. (Note that quadratic probing is guaranteed to terminate only if the load factor is a < .5 )