📄 Paper 1 · 4.2 Data Structures
⭐ Pro
4.2.4 Graphs & Adjacency Representation
AQA 7517 · A-Level Computer Science · ~18 min read

What is a Graph?

A graph is a non-linear data structure consisting of vertices (nodes) connected by edges (arcs). Graphs can model real-world networks such as road maps, social networks, and the internet.

Types of Graphs

PropertyDescriptionExample
Undirected graphEdges have no direction; connection is bidirectionalFriendship network
Directed graph (digraph)Edges have a direction (shown with arrows)Web page links
Weighted graphEdges have a numerical weight/costRoad distances
Unweighted graphEdges have no weight (or all weight = 1)Social connections

Graph Terminology

  • Vertex (node) — a point in the graph
  • Edge (arc) — a connection between two vertices
  • Adjacent — two vertices directly connected by an edge
  • Degree — the number of edges connected to a vertex
  • Path — a sequence of vertices connected by edges
  • Cycle — a path that starts and ends at the same vertex

Adjacency Matrix

An adjacency matrix is a 2D array where matrix[i][j] = 1 (or weight) if there is an edge from vertex i to vertex j, and 0 otherwise.

// For graph with vertices A, B, C, D (undirected, unweighted)
// Edges: A-B, A-C, B-D
     A  B  C  D
A  [ 0, 1, 1, 0 ]
B  [ 1, 0, 0, 1 ]
C  [ 1, 0, 0, 0 ]
D  [ 0, 1, 0, 0 ]
// Note: undirected graph → matrix is symmetric

Advantages: O(1) lookup for edge existence; simple to implement.
Disadvantages: O(V²) space even for sparse graphs; wasteful when few edges exist.

Adjacency List

An adjacency list represents the graph as a list/dictionary where each vertex maps to a list of its neighbours.

// Same graph as above:
adjacency_list = {
  "A": ["B", "C"],
  "B": ["A", "D"],
  "C": ["A"],
  "D": ["B"]
}

Advantages: Space-efficient for sparse graphs — O(V + E).
Disadvantages: Slower edge lookup O(degree(v)) compared to O(1) for matrix.

Comparison: Matrix vs List

FeatureAdjacency MatrixAdjacency List
SpaceO(V²)O(V + E)
Edge lookupO(1)O(degree(v))
Best forDense graphsSparse graphs
Adding edgeO(1)O(1)
Finding all neighboursO(V)O(degree(v))

Applications of Graphs

  • Shortest path — Dijkstra's algorithm (4.3.6) uses a weighted graph
  • Network topology — modelling internet/LAN connections
  • Social networks — friend recommendations
  • State machines — FSMs modelled as directed graphs
Exam tip: Be able to draw and interpret both adjacency matrices and adjacency lists for given graphs (directed, undirected, weighted). Know the space and time trade-offs. A symmetric adjacency matrix indicates an undirected graph.
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.2.4 Graphs

8 questions · instantly marked · AQA 7517 standard

Q1Define the terms 'vertex' and 'edge' in the context of a graph.[2]
✅ Mark scheme
Mark scheme
Vertex (node) — a point/item in the graph [1]; edge (arc) — a connection/link between two vertices [1].
Q2Distinguish between a directed and undirected graph. Give a real-world example of each.[4]
✅ Mark scheme
Mark scheme
Undirected: edges have no direction; connection is bidirectional [1]; e.g. friendship network / road (two-way) [1]; Directed (digraph): edges have a direction (one-way arrows) [1]; e.g. web page links / one-way streets [1].
Q3A graph has vertices A, B, C, D with edges A–B, B–C, C–D, A–D (all undirected). Draw the adjacency matrix.[3]
✅ Mark scheme
Mark scheme
Correct 4×4 matrix with 1s at: [A][B]=[B][A]=1; [B][C]=[C][B]=1; [C][D]=[D][C]=1; [A][D]=[D][A]=1; all other cells 0 [2]; matrix is symmetric (undirected) [1].
Q4Using the same graph as Q3, write the adjacency list representation.[2]
✅ Mark scheme
Mark scheme
A: [B, D]; B: [A, C]; C: [B, D]; D: [C, A] — all four vertices listed with correct neighbours [2].
Q5State two advantages of an adjacency list over an adjacency matrix.[2]
✅ Mark scheme
Mark scheme
Any two: uses less memory O(V+E) rather than O(V²) — better for sparse graphs [1]; faster to find all neighbours of a vertex — only connected nodes listed [1]; more space-efficient when V is large and E is small [1].
Q6State one advantage of an adjacency matrix over an adjacency list.[1]
✅ Mark scheme
Mark scheme
O(1) lookup to determine whether an edge between two specific vertices exists [1].
Q7How can you tell from an adjacency matrix whether a graph is undirected?[1]
✅ Mark scheme
Mark scheme
The matrix is symmetric — matrix[i][j] = matrix[j][i] for all i and j [1].
Q8Give two real-world applications of graphs in computing.[2]
✅ Mark scheme
Mark scheme
Any two: shortest path in road/sat-nav systems [1]; modelling network topology [1]; social network friend recommendations [1]; state machines/FSMs [1]; web page linking [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 12
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — Graphs

10 questions · 10 minutes

← 4.2.3 Stacks
14 of 70 · AQA 7517
4.2.5 Trees →