Intermediate

Graphs

AicademyAicademy
·A-Level Computer Science·AQA 7517·5 min
4.2.4 Graphs: directed, undirected, weighted·adjacency matrix·adjacency list

What Is a Graph?

A graph is a data structure consisting of a set of vertices (nodes) connected by edges (arcs). Graphs model relationships between entities: road networks, social networks, circuit diagrams, dependency trees, and more.

Key terminology:

TermMeaning
Vertex (node)An entity in the graph
Edge (arc)A connection between two vertices
Directed graph (digraph)Edges have a direction (one-way)
Undirected graphEdges have no direction (two-way)
Weighted graphEdges carry a numeric value (cost, distance, time)
Unweighted graphAll edges treated as equal

Example — undirected weighted graph (road network):

     A ---4--- B
     |         |
     2         3
     |         |
     C ---1--- D
      \       /
        --5--

Vertices: A, B, C, D. Edge A–B has weight 4; edge C–D has weight 1.

Directed vs Undirected Graphs

Undirected graph — edges are bidirectional; if A connects to B then B connects to A.

Directed graph (digraph) — edges are one-way; A → B does not imply B → A.

Undirected:         Directed:
A --- B             A --→ B
|   /               ↑   /
C                   C

Applications:

  • Undirected: friendship networks (mutual), road maps (two-way streets)
  • Directed: Twitter follows (one-way), web page links, task dependencies

(Extra context — not required by AQA 4.2.4) A graph with no cycles is a directed acyclic graph (DAG), used in build systems, spreadsheet dependencies, and scheduling.

Adjacency Matrix

An adjacency matrix represents a graph as a 2D array where matrix[i][j] indicates whether there is an edge from vertex to vertex .

  • Unweighted: 1 if an edge exists, 0 if not
  • Weighted: the edge weight if an edge exists, 0 (or ) if not

Example — the road network from slide 1:

Vertices: A=1, B=2, C=3, D=4

     A  B  C  D
A  [ 0  4  2  0 ]
B  [ 4  0  0  3 ]
C  [ 2  0  0  1 ]
D  [ 0  3  1  0 ]  (weight 5 edge C–D already shown as C→D=1, but let's use the simpler example)

Note: for undirected graphs the matrix is symmetric (value at [i][j] equals value at [j][i]).

Space complexity: — always allocates a array regardless of edge count.

Adjacency List

An adjacency list stores a list of neighbours for each vertex. Each vertex maps to the list of vertices it connects to (and optionally the edge weights).

Example — same graph:

A: [(B, 4), (C, 2)]
B: [(A, 4), (D, 3)]
C: [(A, 2), (D, 1)]
D: [(B, 3), (C, 1)]

Space complexity: — uses space proportional to the number of vertices plus the number of edges.

Comparison:

Adjacency matrixAdjacency list
Space
Check if edge exists
List all neighbours
Best forDense graphs (many edges)Sparse graphs (few edges)

A dense graph has close to edges; a sparse graph has far fewer. Most real-world networks (road maps, social networks) are sparse — adjacency lists are usually more efficient.

Studying this for an exam?

Generate a personalised learning path for this subject. Free to get started.

Create a learning path

Choosing a Representation

The choice of representation depends on the graph's characteristics and the operations required:

Use an adjacency matrix when:

  • The graph is dense (many edges)
  • Frequent edge-existence checks are needed
  • Memory is not a constraint

Use an adjacency list when:

  • The graph is sparse (few edges, as in most real applications)
  • Memory efficiency matters
  • Operations mainly iterate over neighbours (as in BFS and DFS)

Worked example — representing a social network of 1 million users:

  • Matrix would require cells — impractical
  • Each user has on average 200 friends → total edges ≈
  • Adjacency list uses space proportional to — manageable

Common Exam Mistakes

1. Forgetting that undirected adjacency matrices are symmetric

In an undirected graph, if matrix[A][B] = 4 then matrix[B][A] must also be 4. Missing the symmetric entry loses marks in matrix construction questions.

2. Confusing vertices and edges in Big-O analysis

refers to vertices squared; refers to vertices plus edges. Substituting one for the other produces wrong complexity claims.

3. Using an adjacency matrix for a sparse graph

A social network or the Internet is sparse. Claiming a matrix is the better choice is incorrect without justifying that the graph is dense.

4. Stating that a tree is not a graph

A tree is a special case of a graph: connected, undirected, and with no cycles. All the graph representations apply to trees.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set — personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Stacks and Queues

Next

Binary Search Trees

Related lessons

7 Slides

Lesson

Graph Traversal: BFS and DFS

A-Level Computer Science · AQA 7517

7 days ago

7 Slides

Lesson

Dijkstra's Shortest Path Algorithm

A-Level Computer Science · AQA 7517

7 days ago

6 Slides

Lesson

Arrays and Abstract Data Types

A-Level Computer Science · AQA 7517

10 hours ago