Comparison of Graph Representations

Learn how adjacency matrices and adjacency lists compare.

After studying both adjacency matrices and adjacency lists, let’s see how they compare for common operations that we’ll perform in graph algorithms.

In most applications of graph algorithms, the set of nodes VV is constant. No nodes are added or removed over time. We can therefore focus on operations that work on the edge set EE.

Memory usage

The adjacency matrix stores at least one bit for every ordered pair of nodes (u,v)(u, v). Since there are exactly (V2V)(|V|^2 - |V|) such pairs, the memory footprint is O(V2)\mathcal{O}(|V|^2).

The adjacency list representation needs to store one list for each node in the graph, which takes at least O(V)\mathcal{O}(|V|) space. Additionally, every edge of the graph needs to be stored in one of the lists, taking up further O(E)\mathcal{O}(|E|) space. The total memory requirements are thus O(V+E)\mathcal{O}(|V| + |E|).

In most cases, the adjacency list representation is more memory efficient, as it stores only the present edges, while the adjacency matrix also contains entries for all absent edges. The two representations are comparable only in dense graphs. Note that in a dense graph, the number of edges is close to the maximum number of edges (V2V|V|^2-|V|), so we have E=O(V2)|E| = \mathcal{O}(|V|^2).

Operations on single edges

Let’s assume that we are given two nodes u,vu, v, and we want to perform an operation on the edge (u,v)(u,v), for example:

  • Test whether the edge exists.
  • Add or remove the edge.
  • Modify the edge’s weight.

Get hands-on with 1400+ tech skills courses.