简体   繁体   中英

Iteration through HashSet vs a Linked Hashset

I am thinking about ways to represent a graph in memory?

I was thinking to use a hash maps of hash maps so that it will behave similar to an adjacency matrix, but we can use Comparable edge labels instead of just integers.

In Breadth First Search and Dijkstra's algorithms, we have to iterate through adjacency lists and add nodes to the queue. This leads to my question:

Is iteration through a linked hash set more efficient than iteration through a regular HashSet in Java?

It seems like it would be because there are links between each node in the order that they were added so we do not have to iterate through empty bins if they exist (depending on the re-hashing ratio of the HashMap, this could be more or less). This would allow us to combine the random access behavior of the adjacency matrix with the search algorithm efficiency of the adjacency list.

Yes, you're right.

Java HashMap/Set has poor performance in sparse graph, because it must iterator from empty bins. When most of nodes only connected one other node. The HashMap/Set maybe take 8 iteration to get the exact result. The related analysis could be found in Codeforces: Performance of hash set iterators in different programming languages .

In order to represent graph, Java generic mechanism may let you create Object type for primitive type, like Integer. They will slow down performance when convert to primitive type and build graph. In best practice, you need to use Trove or other library. Perhaps, implement by yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM