If each node maps to a set of the nodes it has edges to, instead of a list, we would gain the ability to gain constant time lookup of edges, instead of having to traverse the whole list. The only disadvantage I can think of is slightly more memory overhead and time to enumerate the edges of a node, but not asymptotically significantly so.
I think "list" is just a generic label, not to be taken literally. I've used a Set
and it works perfectly well.
If I recall correctly, my textbook also used a Set
.
来源:https://stackoverflow.com/questions/32588468/why-are-graphs-represented-using-an-adjacency-list-instead-of-an-adjacency-set