I have a class that represents undirected edges in a graph. Every edge has two members vertex1
and开发者_C百科 vertex2
representing the vertices it connects. The problem is, that an edge can be specified two directions. My idea was now to define the hash of an edge as the sum of the hashes of its vertices. This way, the direction plays no role anymore, the hash would be the same. Are there any pitfalls with that?
I have had to solve a similar problem and found that using the sum of hashes as a hash results in too many collisions. The distribution of the sum of hashes is just not spread out enough.
I found that using the product of hashes resulted in much less collisions. This of course depends on the nature of the hashes for the individual vertices.
Set up a test bed and test a few symmetric hash functions and then choose the best based on collisions.
You could 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)
精彩评论