After reading about how to implement a graph it seems I have basically two options:
In order to decide which implementation to use this post can be useful.
When an adjacency list is used to implement a graph the cost to know if there is an edge between two nodes may take linear time (for those nodes connected to all nodes).
That make me wonder: Why not to use a HashSet instead of a linked list in order to keep the neighbors of a node?
This will give us constant time to know if there is an edge between two nodes.
I'm sure must be a disadvantage using a Set instead of Linked list but I can't see it.
I think "list" is just a generic name. I've used a Set and it works perfectly well.
There is no specific reason to use a list instead of a set, here go through this link - Graph using set
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With