🔒
Pro Content
Subscribe to access all 69 OCR H446 A Level lessons.
£7.99/month
or £59/year
Subscribe now →
🔒 Pro · Component 1 · 1.4.2 Data Structures
1.4.2c Graphs
OCR H446 · A Level Computer Science · ~13 min read
Notes
Video
Slides
Worksheet
Quiz

What is a Graph?

A graph is a non-linear data structure consisting of vertices (nodes) connected by edges (arcs). Unlike trees, graphs may have cycles and multiple paths between nodes, with no single root.

Graphs model relationships in many real-world domains: road networks, social networks, the internet, dependency chains, and more.

Key Terminology

TermDefinition
Vertex (node)A fundamental unit/entity in the graph
Edge (arc)A connection between two vertices
Weighted edgeAn edge with a numeric value (e.g. distance, cost)
DegreeNumber of edges connected to a vertex
PathA sequence of vertices connected by edges
CycleA path that starts and ends at the same vertex
Connected graphThere is a path between every pair of vertices

Directed vs Undirected Graphs

FeatureUndirectedDirected (Digraph)
EdgesBidirectional — travel in either directionOne-way — indicated by arrows
ExampleFacebook friendships (mutual)Twitter follows (one-way)
Adjacency matrixSymmetric (a[i][j] = a[j][i])Not necessarily symmetric
In-degree / Out-degreeOnly degreeIn-degree: edges arriving; out-degree: edges leaving

Weighted vs Unweighted Graphs

In a weighted graph, each edge carries a numeric weight (cost, distance, time). In an unweighted graph, edges simply indicate connection with no associated value. Algorithms such as Dijkstra's shortest path require weighted graphs.

Representing Graphs in Memory

1. Adjacency Matrix

A 2D array where matrix[i][j] = weight if there is an edge from vertex i to vertex j, or 0 (or ∞) otherwise. For unweighted graphs, use 1 and 0.

-- Graph: vertices A,B,C,D (index 0,1,2,3) -- Edges: A-B(4), A-C(2), B-D(3), C-D(5) -- Undirected weighted adjacency matrix: A B C D A [ 0, 4, 2, 0 ] B [ 4, 0, 0, 3 ] C [ 2, 0, 0, 5 ] D [ 0, 3, 5, 0 ]

Pros: O(1) edge lookup; simple; works for dense graphs.
Cons: O(V²) space — wasteful for sparse graphs where most cells are 0.

2. Adjacency List

Each vertex stores a list of its neighbours (and weights). More space-efficient for sparse graphs.

-- Same graph as above, adjacency list: A: [(B,4), (C,2)] B: [(A,4), (D,3)] C: [(A,2), (D,5)] D: [(B,3), (C,5)]

Pros: O(V + E) space — much better for sparse graphs; easy to iterate over neighbours.
Cons: O(V) edge lookup in worst case; more complex to implement.

Choosing a Representation

CriterionUse Adjacency MatrixUse Adjacency List
Graph densityDense (many edges, E ≈ V²)Sparse (few edges, E << V²)
Edge lookup speedO(1)O(V) worst case
SpaceO(V²)O(V + E)
Iterate neighboursO(V) per vertexO(degree) per vertex

Graph Traversal Algorithms

Graph traversal visits every vertex exactly once. Two main approaches:

Breadth-First Search (BFS)

  • Uses a queue (FIFO)
  • Visits all neighbours at the current level before going deeper
  • Finds the shortest path (fewest edges) in an unweighted graph
  • Applications: finding shortest route, web crawler, social network distance

Depth-First Search (DFS)

  • Uses a stack (or recursion)
  • Goes as deep as possible before backtracking
  • Applications: cycle detection, topological sort, maze solving

Special Graph Types

TypeDescriptionExample
TreeConnected, acyclic graphFile system directory
DAG (Directed Acyclic Graph)Directed, no cyclesTask dependency/build system
Complete graphEvery vertex connected to every otherRound-robin tournament
Bipartite graphVertices split into 2 groups; edges only between groupsJob-applicant matching

Real-world Graph Applications

  • Road/transport networks: vertices = towns, weighted edges = distances. Dijkstra's finds shortest route.
  • Internet: routers (vertices), connections (edges). BFS/DFS used for network discovery.
  • Social networks: users (vertices), friendships/follows (edges). Degree of separation queries.
  • Dependency graphs: software packages — must install dependencies first (DAG).
  • AI state spaces: game states (vertices), moves (edges). A* and BFS search the graph.
Exam tip: For OCR H446, know how to draw adjacency matrices and lists from a diagram, and back again. The adjacency matrix for an undirected graph is always symmetric. You must also know BFS uses a queue and DFS uses a stack.
Exam tip: Space complexity — adjacency matrix O(V²), adjacency list O(V+E). For a graph with 100 vertices and only 150 edges, the list uses roughly 250 entries; the matrix uses 10,000. Adjacency lists are almost always preferable for real-world (sparse) graphs.
⚠ Common Mistakes
  • Forgetting that an undirected adjacency matrix is symmetric — both matrix[i][j] and matrix[j][i] must be set.
  • Confusing a graph with a tree — a tree is a special case of a graph (connected, acyclic). Not all graphs are trees.
  • Saying BFS uses a stack — it uses a queue. DFS uses a stack (or recursion).
✓ Notes completed!
Video coming soon
Click to advance · Arrow keys also work
Click slide or press arrow keys to navigate

Worksheet — 1.4.2c Graphs

8 questions · 20 marks · instantly marked

Q1Define the terms 'vertex' and 'edge' in the context of a graph data structure.[2 marks]
✓ Mark scheme
Vertex (node): a fundamental unit or entity in the graph — a point that can be connected to others [1]. Edge (arc): a connection or link between two vertices — can be directed (one-way) or undirected (bidirectional) [1].
Q2State the difference between a directed graph (digraph) and an undirected graph. Give one real-world example of each.[4 marks]
✓ Mark scheme
Undirected: edges have no direction — travel is possible in both directions between connected vertices [1]. Example: friendship network (Facebook) — if A is friends with B, B is also friends with A [1]. Directed: edges have a direction — travel is only possible in the indicated direction [1]. Example: Twitter follows — A can follow B without B following A [1]. Also accept: road network with one-way streets, web page links, email sending.
Q3Draw the adjacency matrix for the following undirected, unweighted graph: vertices A, B, C, D. Edges: A-B, A-C, B-D, C-D.[3 marks]
✓ Mark scheme
4×4 matrix [1]:
  A B C D
A [0 1 1 0]
B [1 0 0 1]
C [1 0 0 1]
D [0 1 1 0]
Symmetric (because undirected) [1]. Diagonal all 0 (no self-loops) [1].
Q4For the same graph (A–D with edges A-B, A-C, B-D, C-D), write the adjacency list representation.[2 marks]
✓ Mark scheme
A: [B, C] [1]
B: [A, D]
C: [A, D]
D: [B, C] [1]
All four correct lists for full marks. Accept any order within each list.
Q5Compare the space complexity of an adjacency matrix and an adjacency list for a graph with V vertices and E edges. When should each be used?[4 marks]
✓ Mark scheme
Adjacency matrix: O(V²) space — stores a cell for every possible edge, even if no edge exists [1]. Adjacency list: O(V + E) space — only stores existing edges [1]. Matrix preferred for dense graphs (E ≈ V²) where edge lookup speed O(1) is needed [1]. List preferred for sparse graphs (E << V²) where memory efficiency is important, or when iterating over neighbours [1].
Q6Explain what a weighted graph is and give one example of how edge weights are used in a real-world algorithm.[2 marks]
✓ Mark scheme
A weighted graph assigns a numeric value (weight) to each edge, representing cost, distance, time, or capacity [1]. Example: in a road network, edge weights represent distances between towns; Dijkstra's algorithm uses these weights to find the shortest path between two locations [1]. Also accept: routing protocols using link costs; flight networks with ticket prices.
Q7State which data structure is used by (a) Breadth-First Search and (b) Depth-First Search. Explain why each is appropriate.[4 marks]
✓ Mark scheme
(a) BFS uses a queue (FIFO) [1] — because it processes nodes level by level, first exploring all neighbours of the current vertex before moving deeper; the FIFO order ensures closer nodes are visited before further ones [1]. (b) DFS uses a stack (or recursion which implicitly uses the call stack) (LIFO) [1] — because it goes as deep as possible before backtracking; LIFO means the most recently discovered unvisited branch is explored next [1].
Q8What is a Directed Acyclic Graph (DAG)? Describe one computing use case where a DAG is an appropriate model.[3 marks]
✓ Mark scheme
DAG: a directed graph with no cycles — all edges point in one direction and you cannot return to a vertex by following edges [1]. Use case (any one): software package dependency — each package is a vertex; an edge from A to B means A depends on B; cycles would cause deadlock (package A needs B which needs A), so DAGs model valid dependency resolution [2]. Also accept: university course prerequisites; task scheduling (topological sort); spreadsheet formula dependencies.
Topic Quiz
1 of 15
You scored
out of 15
🎯

Mini Test — 1.4.2c Graphs

  • 10 questions · 10 marks · 10 minutes
  • 5 MCQ + 5 short answer
Card 1 of 15
Click to reveal
🎉
Complete!
TermDefinition
← 1.4.2b Stacks & Queues 1.4.2 Data Structures Next: 1.4.2d Trees →