Hash table in C++

13,890

Solution 1

Is the insertion/deletion/lookup time of a C++ map O(log n)?

Yes.

Is it possible to implement an O(1) hash table?

Definitely. The standard library also provides one as std::unordered_map.

Solution 2

C++ has a unordered_map type. The STL also contains a hash_map type, although this is not in the C++ standard library.

Now, for a bit of algorithmic theory. It is possible to implement an O(1) hash table under perfect conditions, and technically, hash tables are O(1) insertion and lookup. The perfect conditions in this case are that the hash function must be perfect (i.e. collision free), and you have infinite storage.

In practise, let's take a dumb hash table. For any input key, it returns 1. In this case, when there is a collision (i.e. on the second and subsequent insertions), it will have to chain further to find some free space. It can either go to the next storage location, or use a linked list for this.

In any case, in the best case, yes, hash tables are O(1) (until you have exhausted all of your hash values, of course, since it is impractical to have a hash function with an infinite amount of output). In the worst case (e.g. with my completely dumb hash function), hash tables are O(n), since you will have to traverse over the storage in order to find your actual value from the given hash, since the initial value is not the correct value.

Solution 3

The implementation of std::map is a tree. This is not directly specified in the standard, but as some good books are saying: "It is difficult to imagine that it can be anything else". This means that the insertion/deletion/lookup time for map is O(log n).

Classic hash tables have lookup time O(n/num_slots). Once the expected number of items in the table is comparable with the number of slots, you will have saturated O(1).

Share:
13,890
Paul S.
Author by

Paul S.

Updated on June 04, 2022

Comments

  • Paul S.
    Paul S. about 2 years

    Is the insertion/deletion/lookup time of a C++ std::map O(log n)? Is it possible to implement an O(1) hash table?

  • Paul S.
    Paul S. over 11 years
    I see. Then what is the advantage of std:map over std:unordered_map?
  • Cheers and hth. - Alf
    Cheers and hth. - Alf over 11 years
    the former is an ordered collection
  • Praetorian
    Praetorian over 11 years
    hash_map is a non-standard extension shipped by most compilers. The standard hash map is called unordered_map
  • Kos
    Kos over 11 years
    std::map uses a tree and needs its keys to be comparable, while std::unordered_map requires its keys to be hashable. Also I wouldn't assume that std::unordered_map is always faster, esp. for small data (but don't take my word for it and check if you feel that's important).
  • Praetorian
    Praetorian over 11 years
    @Kos unordered_map also requires its keys to be comparable; it's just a different default comparator (equal_to) as compared to map (less)
  • im so confused
    im so confused over 11 years
    I have a question. Is O(0) == O(1)? Is O(0) even defined?
  • Kos
    Kos over 11 years
    Thanks for the clarification! (C# has this sorted out nice, distinguishing between "comparable" (with some order) and "equatable".)
  • Kirill Kobelev
    Kirill Kobelev over 11 years
    I do not think that O(0) is a valid notation. O(1) means "constant time".
  • im so confused
    im so confused over 11 years
    yeah, just an idle thought that came up while reading your answer. thanks!
  • slugonamission
    slugonamission over 11 years
    O(0) would probably imply that the algorithm had fetched the results for you before your even asked it what you wanted ;)
  • im so confused
    im so confused over 11 years
    @slugonamission hahaha the beginnings of Minority Report?