This post has been edited by **noctolater**: 17 April 2011 - 11:06 AM

# Help Selecting A Hash Function

Page 1 of 1## 7 Replies - 475 Views - Last Post: 17 April 2011 - 02:06 PM

### #1

# Help Selecting A Hash Function

Posted 17 April 2011 - 11:05 AM

I am using a HashMap to store the outgoing edges of a graph in each vertex. My key is a number from 0 to n-1, where n is specified at graph creation and represents the destination vertex. I have been looking around trying to find a good list of hash functions, but it seems that most places just say to develop your own based on your needs. I was wondering, however, if anyone might know a good place to start? I would imagine that the edges will be randomly distributed over the range from 0 to n-1, but the user can always input more edges at a later date. Any ideas as to general hash functions that I can specialize for this purpose? Thanks for your time.

##
**Replies To:** Help Selecting A Hash Function

### #2

## Re: Help Selecting A Hash Function

Posted 17 April 2011 - 11:08 AM

What happens if you have two vertexes with the same key? Is the second dropped?

If you ignore dupes, then a simple mod n hash would be sufficient.

If you ignore dupes, then a simple mod n hash would be sufficient.

### #3

## Re: Help Selecting A Hash Function

Posted 17 April 2011 - 11:48 AM

Yes, there are no repeated keys. However, the problem with a simple mod n is that I end up with an array the same size as the number of edges, which essentially gives me an adjacency matrix, which is too high memory cost. Or, by mod n, did you mean some number n much smaller than the number of keys? Sorry that I did not mention unique keys previously, and sorry if I misunderstood your response.

This post has been edited by **noctolater**: 17 April 2011 - 11:49 AM

### #4

## Re: Help Selecting A Hash Function

Posted 17 April 2011 - 12:29 PM

Are you designing your own HashMap class? If not, the HashMap class already handles hashing, so I'm not sure why you need to develop your own hashing function. What I would suggest is designing your own Edge class to store the destinationVertex and weight. Then store a HashMap<Integer, List<Edge>>.

Hope this helps some.

Hope this helps some.

### #5

## Re: Help Selecting A Hash Function

Posted 17 April 2011 - 01:34 PM

Thanks for the reply! I am already doing that with the edge But, from everywhere I read about HashMap, you should override the equals and getHash methods when you use it. Was this information wrong?

### #6

## Re: Help Selecting A Hash Function

Posted 17 April 2011 - 01:42 PM

You should almost always override the equals() method when creating a class, unless you want Objects from that class to be compared using the == operator solely. The values are not hashed, only the keys are. Really, no need to worry about custom hashcode if you are using an Integer as your key. When you may want to deal with overriding hashcode is if you have custom Objects as the keys. In this instance, hashcode() should return the same value only for two Objects that are equal according to the equals() method.

### #7

## Re: Help Selecting A Hash Function

Posted 17 April 2011 - 01:54 PM

Thank you so much for your responses. I will just leave it as it is then. I just finished the assignment super quick and was wondering about optimizations that could be done

### #8

## Re: Help Selecting A Hash Function

Posted 17 April 2011 - 02:06 PM

Glad I could help!

Page 1 of 1