Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graph implementation adjacency list vs set

After reading about how to implement a graph it seems I have basically two options:

  • Matrix
  • Adjacency list

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.

like image 964
ilopezluna Avatar asked Nov 26 '25 11:11

ilopezluna


1 Answers

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!

like image 118
zenwraight Avatar answered Nov 29 '25 19:11

zenwraight



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!