The same graph can be represented as an adjacency matrix or as adjacency lists. The representation affects both the storage requirement and the cost of the operations an algorithm performs.
Worked example
A matrix gives a direct entry for every ordered pair of vertices, making an edge-existence lookup straightforward. It uses space proportional to V squared, including entries for missing edges. An adjacency-list representation stores neighbors for each vertex and uses space proportional to V plus E. Scanning actual neighbors is efficient on a sparse graph.
What to check
Consider how the caller uses the graph. Frequent edge-existence queries can favor a representation with fast membership checks, while traversal of sparse data often favors adjacency lists. Parallel edges, self-loops, and directed edges need explicit conventions. An undirected adjacency list commonly stores each edge twice, which changes a constant factor while preserving O(V + E) space. Complexity claims should name the representation they assume.
Continue with the book
Explore the full treatment, worked examples, and exercises in Algorithms Made Simple.
Companion article for Algorithms Made Simple, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
