Adjacency list vs adjacency matrix
Two ways to store the same graph in memory: a grid of every possible pair, or a list of neighbours per node. Seven steps compare what each costs in space and time.
In 30 seconds
To store a graph, a program must record which nodes are joined. An adjacency matrix is a grid with a cell for every pair of nodes: 1 for an edge, 0 for none.
An adjacency list gives each node a list of just its neighbours.
The matrix answers “are these two joined?” in one step, but takes V × V cells however few edges exist. The list takes space only for real edges and hands over a node’s neighbours directly. Most real graphs are sparse, so the list is the usual choice.
Adjacency list vs adjacency matrix, step by step
Costs are for plain lists. Storing each node’s neighbours in a hash set makes the edge check O(1) on average. A directed graph stores each edge once, so its matrix is not symmetric.
- Step 1: A graph to store. A graph is a set of nodes and the edges that join them. This one has five nodes, A to E, and five edges. A picture is fine for people, but a program needs the same information as data: for any two nodes, are they joined? There are two classic ways to write that down.
- Step 2: The adjacency matrix. An adjacency matrix is a grid with one row and one column per node. The cell in row C, column D holds 1 if C and D are joined and 0 if not. The grid always has V × V cells, 25 here, whether the graph has many edges or none. Each edge appears twice, so the grid mirrors itself across the diagonal.
- Step 3: The adjacency list. An adjacency list keeps, for each node, just the list of its neighbours. C is joined to A, B and D, so its list holds those three; E has a single neighbour. There are no entries for missing edges at all. Five lists hold ten entries in total, two for each edge, so the size grows with V + E.
- Step 4: Is there an edge?. Ask both structures whether C and D are joined. The matrix goes straight to row C, column D and reads a 1: one step, whatever the size of the graph. The list has to walk through C’s neighbours until it meets D or runs out, here three checks. That cost grows with the degree, the number of neighbours a node has.
- Step 5: Who are the neighbours?. Now ask for all the neighbours of C, which is what most graph algorithms do at every node. The list simply hands over its three entries. The matrix has to read the whole of row C, zeros included: V cells, even if the node has one neighbour. In a graph with a million nodes that is a million cells per node.
- Step 6: Memory: V² against V + E. Scale up to a sparse graph of 1,000 nodes and 3,000 edges, where each node has six neighbours on average. The matrix needs a million cells, and 99.4% of them are zeros. The list needs 1,000 list heads plus 6,000 entries: 7,000 slots, about 140 times fewer. The squares are drawn to scale by area.
- Step 7: Sparse or dense?. The choice follows how full the graph is. In a sparse graph, each node touches only a few others and the list saves most of the memory. In a dense graph, where most pairs are joined, the matrix wastes little and keeps its instant edge check. Road maps, social networks and the web are all sparse, so the list is the usual default.
Try it yourself
Learn this first
Learn next
- Dijkstra’s shortest pathGrow the cheapest frontier until you reach the goal. Intermediate
- Directed acyclic graphs (DAGs)One-way arrows and no loops: the shape of anything that has dependencies. Beginner
- Topological sortLining up tasks so that everything a task depends on comes before it. Intermediate
Spotted a mistake on this page?