I have a class that represents undirected edges in a graph. Every edge has two members vertex1
and 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?
views:
56answers:
1
+3
A:
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)
Il-Bhima
2010-06-11 10:20:19
Would it make sense to also include x^y as a possible hash combiner?
Vatine
2010-06-11 10:44:37
If x^y = min(x,y) then yes :-)
Il-Bhima
2010-06-11 10:56:31