Defining the hash of an object as the sum of the hashes of its members

I have a class that represents undirected edges in a graph. Each edge has two members vertex1and vertex2, representing the vertices that it connects. The problem is that the rib can be pointed in two directions. My idea was to define the edge hash as the sum of the hashes of its vertices. So the direction no longer matters; the hash will be the same. Are there any pitfalls with this?

+3
source share
1 answer

I had to solve a similar problem and it turned out that using the hash sum as a hash leads to too many collisions. The distribution of the amount of hashes is simply not widespread.

I found that using hashes resulted in significantly fewer collisions. This, of course, depends on the nature of the hashes for the individual vertices.

Set up a test bed and check out a few symmetrical hash functions, and then pick the best based on the collision.

You can try

h(x,y) = x+y
h(x,y) = x*y
h(x,y)  = x * y + (x ^ y)
h(x,y) = x *y + x + y

where x ^ y = min (x, y)

+3
source

Source: https://habr.com/ru/post/1749553/


All Articles