Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How expensive is comparing two unordered sets for equality?

Given two std::sets, one can simply iterate through both sets simultaneously and compare the elements, resulting in linear complexity. This doesn't work for std::unordered_sets, because the elements may be stored in any order. So how expensive is a == b for std::unordered_set?

like image 499
fredoverflow Avatar asked Apr 12 '12 06:04

fredoverflow


People also ask

Can we compare two Unordered_map?

The comparison between unordered_map objects is not affected by the arbitrary order in which they store their elements. Two unordered_maps are equal if they have the same number of elements and the elements in one container are a permutation of the elements in the other container. Otherwise, they are unequal.

Which is better set or unordered set?

For a small number of elements, lookups in a set might be faster than lookups in an unordered_set . Even though many operations are faster in the average case for unordered_set , they are often guaranteed to have better worst case complexities for set (for example insert ).

Does unordered set contain duplicates?

Unordered sets do not allow duplicates and are initialized using comma-delimited values enclosed in curly braces.


2 Answers

The very worst case is O(n²).

But unordered sets are in fact ordered by hash. So it is possible to compare the hashes (if this fails the sets cannot be equal) and then verify that same hashes (linear) have real same values (O(n²) for different values with the same hash) behind.

In the best case this is O(n).

Normally the complexity tends to O(n) if the hash function is "good" (different objects -> always different hash) and to O(n²) if the hash function is "bad" (everything always has the same hash value)

like image 194
Emilio Garavaglia Avatar answered Oct 15 '22 09:10

Emilio Garavaglia


Complexity of operator== and operator!=:

Linear complexity in the average case. N2 in the worst case, where N is the size of the container.

More details in the standard §23.2.5, point 11:

For unordered_set and unordered_map, the complexity of operator== (i.e., the number of calls to the == operator of the value_type, to the predicate returned by key_equal(), and to the hasher returned by hash_function()) is proportional to N in the average case and to N2 in the worst case, where N is a.size().

like image 35
Anonymous Avatar answered Oct 15 '22 07:10

Anonymous