What is a graph and how is it represented in a computer?
Understand graphs, vertices and edges, directed, undirected and weighted graphs, and how a graph is represented as an adjacency matrix or an adjacency list.
A focused answer to AQA A-Level Computer Science 4.2.4, covering graphs, vertices and edges, directed, undirected and weighted graphs, and the adjacency matrix and adjacency list representations with their trade-offs.
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
AQA wants you to define a graph in terms of vertices and edges, distinguish directed, undirected and weighted graphs, and represent a graph as either an adjacency matrix or an adjacency list, with the trade-offs between them.
Graphs, vertices and edges
- Undirected graph: edges have no direction, so a connection works both ways (like a two-way road or a mutual friendship).
- Directed graph (digraph): each edge has a direction, shown by an arrow (like a one-way street or a web hyperlink that points from one page to another).
- Weighted graph: each edge has a weight, such as distance, time or cost, used in shortest-path problems.
A graph is the most general of the structures in this module: a tree is just a special graph (connected, with no cycles), and a list is a graph where each vertex links to the next. Recognising graphs in real problems (social networks, transport maps, dependencies between tasks) is part of what the specification expects.
Adjacency matrix
Adjacency list
The rule of thumb is: use a matrix for a small or dense graph where fast edge look-up matters, and use a list for a large or sparse graph where memory matters. Most real-world graphs (road networks, the web) are sparse, so adjacency lists dominate in practice, while small fixed graphs in exam questions are often shown as matrices because they are easy to lay out.
To make the sparse-versus-dense distinction concrete: a graph is dense when the number of edges is close to the maximum possible (roughly for vertices), and sparse when it has far fewer. A road network is sparse because each junction connects to only a handful of others, never to thousands; a fully connected social clique would be dense. Since the adjacency matrix always uses cells regardless of edges, it is only memory-competitive when the graph is dense enough to fill most of those cells, which is rarely the case in practice.
The two representations also differ in which operations they make cheap, and this drives the choice as much as memory does. A matrix answers "is there an edge between and ?" in constant time by a single array look-up, which suits algorithms that test specific connections repeatedly. An adjacency list answers "what are the neighbours of ?" quickly by iterating one short list, which suits traversal algorithms such as breadth-first and depth-first search and Dijkstra's algorithm, where you repeatedly need a vertex's neighbours. Matching the representation to the dominant operation is the kind of reasoning higher-mark questions reward.
Exam-style practice questions
Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AQA 20194 marksAn undirected graph has vertices P, Q, R with edges P to Q and Q to R. Show how this graph would be stored as an adjacency matrix and explain why each undirected edge produces two entries.Show worked answer →
Label rows and columns P, Q, R. Edge P to Q sets [P][Q] and [Q][P] to 1; edge Q to R sets [Q][R] and [R][Q] to 1; all other entries are 0. The matrix is symmetric:
P row: 0 1 0; Q row: 1 0 1; R row: 0 1 0.
Each undirected edge produces two entries because an undirected connection works both ways, so the edge from P to Q is also an edge from Q to P; the matrix records the connection from each end. (A directed graph would set only one of the two entries.)
Markers reward the correct symmetric matrix and the explanation that an undirected edge is bidirectional so both cells are set.
AQA 20224 marksCompare the adjacency matrix and adjacency list representations of a graph, stating the memory used by each and recommending which to use for a large sparse graph.Show worked answer →
An adjacency matrix is a 2D array of size for vertices, so it always uses memory regardless of how many edges exist, but it can check whether two given vertices are connected in constant time. An adjacency list stores, for each vertex, a list of its neighbours, using memory proportional to the number of edges, , and lets you iterate over a vertex's neighbours quickly, but checking a specific connection may be slower.
For a large sparse graph (many vertices, few edges) the adjacency list is recommended because the matrix would waste huge amounts of memory storing mostly zeros, whereas the list stores only the edges that exist.
Markers reward the versus edge-proportional memory comparison and recommending the adjacency list for a sparse graph with a reason.
Related dot points
- Understand trees as a connected, undirected graph with no cycles, the terms root, child, parent, leaf and subtree, and the structure and use of a binary search tree.
A focused answer to AQA A-Level Computer Science 4.2.5, covering trees as connected acyclic graphs, the terminology root, child, parent and leaf, binary trees, and the structure and use of a binary search tree.
- Understand depth-first and breadth-first graph traversal, the data structures they use, and the pre-order, in-order and post-order tree traversal algorithms.
A focused answer to AQA A-Level Computer Science 4.3.1, covering depth-first and breadth-first graph traversal and the data structures they use, plus the pre-order, in-order and post-order tree traversal algorithms.
- Understand Dijkstra's shortest path algorithm, how it finds the lowest-cost path from a start vertex, the role of a priority queue, and its applications.
A focused answer to AQA A-Level Computer Science 4.3.4, covering Dijkstra's shortest path algorithm, how it finds the lowest-cost path from a start vertex in a weighted graph, the role of a priority queue, and its applications.
- Understand arrays (one, two and three dimensional), records and fields, and the difference between static and dynamic data structures.
A focused answer to AQA A-Level Computer Science 4.2.1, covering one, two and three dimensional arrays, records and fields, indexing, and the difference between static and dynamic data structures.
- Understand a hash table, the role of a hashing algorithm, how a key maps to an index, the meaning of a collision, and collision-resolution methods such as rehashing and chaining.
A focused answer to AQA A-Level Computer Science 4.2.6, covering hash tables, hashing algorithms, mapping a key to an index, collisions, load factor, and collision-resolution methods including rehashing and chaining.
Sources & how we know this
- AQA A-level Computer Science (7517) specification — AQA (2015)