-
Notifications
You must be signed in to change notification settings - Fork 83
Algorithm: Graph Representation
Jeff Levesque edited this page Feb 22, 2015
·
17 revisions
An Adjacency list describes a graph representation, using unordered lists. Specifically, each vertex within the graph representation, consists of a set of neighbors (list).
Consider the following arbitrary graphical representation of vertices, and corresponding (directed) edges.
b ---> c
^ ^
/ \
/ \
a <----------> d
The following is the adjacency list representation of the given graph:
a, b, c, d = range(4)
N = [
[b, d], # a
[c], # b
[a, c] # d
]The following is the adjacency list representation of the given graph:
a, b, c, d = range(4)
N = [
{b, d}, # a
{c}, # b
{a, c} # d
]Note: since sets (and dicts) implement a hashing mechanism, accessing a specific index within a set (or dict) is on average constant time O(1), assuming the hashing function used is sufficient.
Note: since lists do not implement a hashing function, for pure iteration (without index lookups), lists are significantly faster.