What does this mean that the operation is "approaching O (1)" and not "O (1)"?

Consider, for example, the documentation for the .NET Framework 4.5 Dictionary<TKey, TValue> class:

In the notes for the .ContainsKey method .ContainsKey they indicate that

This method approaches operation O (1).

And in the notes for the .Count property .Count they state that

Getting the value of this property is an O (1) operation.

Please note that I do not necessarily request the details of C# , .NET , Dictionary or what the Big O designation is in general. I just found this distinction of “approaches” intriguing.

Is there any difference? If so, how significant can it be? Should I pay attention to this?

+7
optimization c # big-o
source share
4 answers

If the hash function used by the underlying objects in the hash code is “good,” this means that collisions will be very rare. The odds are good that in this hash bucket there will be only one element, maybe two, and almost never again. If you could say for sure that there will never be more c elements in the bucket (where c is a constant), then the operation will be O (c) (which is O (1)). But this confidence cannot be achieved. It’s possible that you just have n different elements that, unfortunately, all collide, and they all fall into the same bucket, in which case ContainsKey is O (n). It is also possible that the hash function is not “good” and often leads to hash collisions, which can lead to the fact that the actual containing check is worse than just O (1).

+9
source share

This is because the Dictionary is an implementation of hash tables - this means that the search for keys is performed using a hash function that tells you which bucket among the many buckets contained in the data structure contains the value you are looking for. As a rule, for a good hash function, assuming a sufficiently large set of codes, each bucket contains only one element - and in this case the complexity is really O (1) - unfortunately, this is not always true - the hash function may have collisions, and in In this case, the bucket may contain more than one record, and therefore, the algorithm must iterate over the bucket until it finds the record you are looking for - therefore, it is no longer O (1) for these (hopefully) rare cases.

+4
source share

O (1) is a constant running time, approaching O (1) close to a constant, but not quite, but for most purposes this is a slight increase. You should not pay attention to it.

0
source share

Something is either there or not O (1). I think they are trying to say that the runtime is approximately O (1) per operation for a large number of operations.

0
source share

All Articles