← Course home

Graphs · Lesson 34 of 52

Graph representations

Choose adjacency lists or matrices based on graph density and required operations.

Full lessonAbout 10 min

You will learn to compare adjacency-list and matrix space and choose representation for graph operations.

Recommended first: Arrays and Python sequence costs.

On this page 6 sections

The idea

Vertices A, B, C, D and three edges store three neighbor pairs.

A graph can be stored as an adjacency list or as a matrix. The library Graph is a dictionary from each vertex to a list of neighbor and weight pairs. Add A to B, A to C, and B to D. The graph has 4 vertices and 3 outgoing pairs. A matrix for the same vertices has 16 cells, one for every ordered pair, almost all of them empty. The list stores only the edges you added. A sparse graph is one where the edge count is much smaller than the square of the vertex count. Breadth-first search on that shape should use a queue with constant-time ends. Python's collections.deque is the usual choice. The library's own breadth-first search uses its linked queue for the same reason. Scanning a matrix row to find neighbors costs a full vertex scan even when a vertex has one edge.

A worked trace

StepStateWhat changed
Edgeedges = 1Add A to B. The list now stores 1 neighbor pairs.
Edgeedges = 2Add A to C. The list now stores 2 neighbor pairs.
Edgeedges = 3Add B to D. The list now stores 3 neighbor pairs.

Why it is correct

The list answers 'who are my neighbors' by reading one list. The matrix answers the same question by scanning a row. Both can answer 'is there an edge from u to v', and the matrix does it in constant time while the list scans u's neighbors. Space follows the same split. The list is proportional to vertices plus edges. The matrix is proportional to vertices squared, whether or not the edges exist. Choose the representation for the operation you repeat, not for the picture you draw.

A common mistake

A matrix of 4 vertices stores 16 cells, including the empty pairs.

What it costs

Building the list is proportional to the number of edges. A traversal that walks each list once is proportional to vertices plus edges. The same traversal on a matrix is proportional to vertices squared. Extra memory for the search is still the queue or the stack, proportional to the vertices. Use a matrix only when the graph is dense or you need constant-time edge tests in the inner loop. A list traversal is Θ(V + E) time, and the queue uses Θ(V) extra memory.

Run it

python
from bitsandbytes.graphs.adjacency_list import Graphgraph = Graph()graph.add_edge('A', 'B', 1)graph.add_edge('A', 'C', 1)graph.add_edge('B', 'D', 1)print(sum(len(graph.neighbors(vertex)) for vertex in graph.vertices()))# prints: 3 

Practice and next step

Open Graph traversals next. Graph traversals walk those neighbor lists with a queue or a stack.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.graphs.adjacency_list

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Count the pairs an adjacency list stores

A directed graph with vertices A, B, C, D and three edges stores three neighbor pairs. A matrix would store 16 cells.

edges
1

State

  1. 0Acomparing
  2. 1Bcomparing

Add A to B. The list now stores 1 neighbor pairs.

Practice feedback is loading. The lesson explanation remains available above.