Skip to content

Adjacency List

For this implementation

  • We use a vector Adj[], of length |V|=n.
  • Each cell Adj[u], uV.
    • Is labeled in a way that reflects the given graph
    • Contains a pointer to a linked list, where each element of the list represents a vertex vV adjacent to u, such that (u,v)E
      • Those elements are labeled too.

Furthermore

  • For vertex-weighted graphs, Adj[u] also contains the weight w(u)
  • For edge-weighted graphs, each element of the linked list has additional information for the weight w(u)

Example: this graph right here, would be implemented like this

AdjacencyList

Space

CaseSpace S(n)
Sparse GraphS(n)=O(|V|+|E|)=O(n+m)
Dense GraphS(n)=O(n2)

Time

Verifying adjacency in the case of a long linked-list, takes more time.

Conclusions

Because the adjacency-list representation provides a compact way to represent sparse graphs, it is usually the method of choice.

ProsCons
Best for sparse graphsWorst for dense graphs
Space complexity is not fixed to Θ(n2)Can worsen with dense graphs and large n
Flexible structure for both undirected and directed graphsVerifying adjacency when the lists are long, takes more time