How do we represent a graph in a program, and when is an adjacency matrix better than an adjacency list?
Describe graphs and their terminology, represent them as adjacency matrices and adjacency lists, and compare the two representations
A focused answer to the H2 Computing outcome on representing graphs. Vertices and edges, directed and weighted graphs, adjacency matrix versus adjacency list, and the space and lookup trade-offs between them.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
SEAB wants you to describe graphs and their terminology, represent them as adjacency matrices and adjacency lists, and compare the two. The central idea is that the same graph can be stored two ways, and the right choice depends on whether the graph is dense or sparse and which operations - fast edge checks or compact storage - matter most.
The answer
Graph terminology
A graph is a set of vertices (nodes) connected by edges. Key terms:
- Directed graph: edges have a direction (a one-way link); undirected: edges go both ways.
- Weighted graph: each edge carries a value (distance, cost).
- Degree of a vertex: the number of edges touching it.
- Path: a sequence of edges from one vertex to another; a cycle returns to its start.
Graphs model networks of all kinds - roads, social links, dependencies.
Adjacency matrix
An adjacency matrix is a grid where cell is 1 (or the weight) if an edge runs from to , else 0. For an undirected graph the matrix is symmetric:
1 2 3
1 0 1 1
2 1 0 0
3 1 0 0
Checking whether an edge exists is a single cell lookup: . But it always uses space, even when most cells are 0.
Adjacency list
An adjacency list stores, for each vertex, a list of its neighbours:
1: [2, 3]
2: [1]
3: [1]
It uses space - only the edges that exist. Checking a specific edge means scanning a vertex's list, which is , but iterating a vertex's neighbours (as traversals do) is efficient.
Comparing the two
| Aspect | Adjacency matrix | Adjacency list |
|---|---|---|
| Space | ||
| Edge exists? | ||
| Iterate neighbours | ||
| Best for | dense graphs, fast edge checks | sparse graphs, traversal |
A dense graph (many edges, near ) suits a matrix; a sparse graph (few edges) suits a list, which most real-world graphs are.
Examples in context
Example 1. A road network. A map with thousands of intersections but only a handful of roads from each is sparse, so navigation software stores it as an adjacency list. A matrix would waste enormous space on the vast majority of intersection pairs that have no direct road between them.
Example 2. A small dense network. A fully connected tournament where every team plays every other is dense ( near ), so an adjacency matrix is appropriate: it costs little extra over the list and gives instant checks of whether two specific teams have a fixture.
Try this
Q1. State the space complexity of an adjacency matrix and of an adjacency list. [2 marks]
- Cue. Matrix ; list .
Q2. Which representation answers "is there an edge from u to v?" in ? [1 mark]
- Cue. The adjacency matrix - a single cell lookup
matrix[u][v].
Q3. Why is an adjacency list preferred for a sparse graph? [2 marks]
- Cue. It stores only the edges that exist (), so it avoids the wasted space a matrix spends on the many absent edges.
Exam-style practice questions
Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
Original6 marksAn undirected graph has vertices 1, 2, 3, 4 and edges {1-2, 1-3, 2-3, 3-4}. (a) Write its adjacency matrix. (b) Write its adjacency list. (c) State the degree of vertex 3.Show worked answer →
(a) The adjacency matrix has a 1 where an edge exists, 0 otherwise. Undirected edges make it symmetric:
1 2 3 4
1 0 1 1 0
2 1 0 1 0
3 1 1 0 1
4 0 0 1 0
(b) The adjacency list stores each vertex's neighbours:
1: [2, 3]
2: [1, 3]
3: [1, 2, 4]
4: [3]
(c) The degree of vertex 3 is the number of edges touching it: it connects to 1, 2 and 4, so its degree is 3.
Markers reward the symmetric matrix with 1s in the right cells, the neighbour lists, and degree 3 for vertex 3.
Original5 marks(a) State the space complexity of an adjacency matrix and of an adjacency list for a graph with vertices and edges. (b) For a sparse graph (few edges), which representation is more memory-efficient and why? (c) Which representation answers 'is there an edge between u and v?' faster, and what is its complexity?Show worked answer →
(a) An adjacency matrix uses space - a full grid regardless of how many edges exist. An adjacency list uses space - one entry per vertex plus one per edge.
(b) For a sparse graph (where is much smaller than ), the adjacency list is far more memory-efficient, because it stores only the edges that exist rather than a cell for every possible pair. A matrix wastes space on the many absent edges.
(c) The adjacency matrix answers "is there an edge between u and v?" in - a single cell lookup matrix[u][v]. An adjacency list takes , scanning u's neighbour list.
Markers reward versus , the list winning on sparse graphs (only real edges stored), and the matrix giving edge lookup.
Related dot points
- Implement and trace breadth-first and depth-first traversal of a graph, and contrast the two strategies and their applications
A focused answer to the H2 Computing outcome on graph traversal. Breadth-first search with a queue and depth-first search with a stack or recursion, tracing each on a graph, and choosing between them for shortest paths and exploration.
- Describe a binary search tree, perform insertion, search and in-order traversal, and explain how balance affects complexity
A focused answer to the H2 Computing outcome on binary search trees. The ordering property, inserting and searching nodes, in-order traversal giving sorted output, and why an unbalanced tree degrades to O(n).
- Describe a linked list as nodes joined by pointers, implement insertion and deletion, and contrast it with an array
A focused answer to the H2 Computing outcome on linked lists. Nodes and pointers, traversal, inserting and deleting nodes by relinking, and the trade-offs against arrays for access and modification.
- Describe a hash table and hash function, explain collision resolution by chaining and open addressing, and analyse the effect of load factor
A focused answer to the H2 Computing outcome on hash tables. Hash functions mapping keys to buckets, average O(1) lookup, collisions resolved by chaining or open addressing, and how the load factor affects performance.
- Describe arrays and records as data structures, explain constant-time array indexing and contrast static with dynamic arrays
A focused answer to the H2 Computing outcome on arrays and records. Contiguous storage and constant-time indexing, one and two dimensional arrays, records as fields of mixed type, and static versus dynamic arrays.