Skip to main content
SingaporeComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

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 V×VV \times V grid where cell (u,v)(u, v) is 1 (or the weight) if an edge runs from uu to vv, 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: O(1)O(1). But it always uses O(V2)O(V^2) 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 O(V+E)O(V + E) space - only the edges that exist. Checking a specific edge means scanning a vertex's list, which is O(degree)O(\text{degree}), but iterating a vertex's neighbours (as traversals do) is efficient.

Comparing the two

Aspect Adjacency matrix Adjacency list
Space O(V2)O(V^2) O(V+E)O(V + E)
Edge exists? O(1)O(1) O(degree)O(\text{degree})
Iterate neighbours O(V)O(V) O(degree)O(\text{degree})
Best for dense graphs, fast edge checks sparse graphs, traversal

A dense graph (many edges, EE near V2V^2) 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 (EE near V2V^2), so an adjacency matrix is appropriate: it costs little extra over the list and gives instant O(1)O(1) 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 O(V2)O(V^2); list O(V+E)O(V + E).

Q2. Which representation answers "is there an edge from u to v?" in O(1)O(1)? [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 (O(V+E)O(V + E)), so it avoids the wasted O(V2)O(V^2) 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 VV vertices and EE 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 O(V2)O(V^2) space - a full VtimesVV \\times V grid regardless of how many edges exist. An adjacency list uses O(V+E)O(V + E) space - one entry per vertex plus one per edge.

(b) For a sparse graph (where EE is much smaller than V2V^2), 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 O(1)O(1) - a single cell lookup matrix[u][v]. An adjacency list takes O(textdegree)O(\\text{degree}), scanning u's neighbour list.

Markers reward O(V2)O(V^2) versus O(V+E)O(V + E), the list winning on sparse graphs (only real edges stored), and the matrix giving O(1)O(1) edge lookup.

Related dot points