AQA 7517, section 4.2.4.1
A graph is a data structure for representing relationships between things. It is made of vertices (also called nodes) joined by edges (also called arcs).
Typical uses
Graphs model anything with connections:
- road or rail maps, where towns are vertices and roads are edges;
- computer networks, where devices are vertices and cables or links are edges;
- social networks, where people are vertices and friendships are edges;
- web pages, where pages are vertices and links are edges;
- scheduling and planning problems.
Terms
| Term | Meaning |
|---|---|
| Graph | A set of vertices and the edges that connect them |
| Vertex / node | One item in the graph |
| Edge / arc | A connection between two vertices |
| Undirected graph | Edges have no direction, so a link works both ways |
| Directed graph (digraph) | Each edge has a direction, shown by an arrow, so a link works one way |
| Weighted graph | Each edge has a value (weight), such as a distance, time or cost |
Here the vertices are A, B, C and D. The edge A to B has weight 4, and so on. If this were a map, the weights could be the distances between towns.
Representing a graph
A program needs a way to store a graph. There are two common methods.
Adjacency matrix
An adjacency matrix is a 2-D array with a row and a column for every vertex. The entry shows whether there is an edge between the two vertices. In a weighted graph, the entry is the weight, and a special value (such as 0 or infinity) means no edge.
For the graph above, with the order A, B, C, D:
| A | B | C | D | |
|---|---|---|---|---|
| A | 0 | 4 | 2 | 6 |
| B | 4 | 0 | 0 | 5 |
| C | 2 | 0 | 0 | 3 |
| D | 6 | 5 | 3 | 0 |
For an undirected graph the matrix is symmetrical, because each edge appears twice. For a directed graph, row X, column Y records an edge from X to Y, so it is not necessarily symmetrical.
matrix = [
[0, 4, 2, 6],
[4, 0, 0, 5],
[2, 0, 0, 3],
[6, 5, 3, 0],
]
print(matrix[0][1]) # 4: the weight of the edge A to B
Adjacency list
An adjacency list stores, for each vertex, a list of the vertices it is connected to (with the weights, if needed).
graph = {
"A": [("B", 4), ("C", 2), ("D", 6)],
"B": [("A", 4), ("D", 5)],
"C": [("A", 2), ("D", 3)],
"D": [("A", 6), ("B", 5), ("C", 3)],
}
print(graph["A"]) # [('B', 4), ('C', 2), ('D', 6)]
Comparing the two
| Adjacency matrix | Adjacency list | |
|---|---|---|
| Memory | Always entries, even with few edges | Only stores edges that exist |
| Best for | Dense graphs (lots of edges) | Sparse graphs (few edges) |
| Is there an edge between X and Y? | Fast: look at one cell | Slower: search X's list |
| List all neighbours of X | Slower: scan the whole row | Fast: the list is ready |
| Adding a vertex | Needs a new row and column | Just add a new entry |
For most real-world graphs, such as roads or friendships, there are far fewer edges than possible, so a list wastes less memory.
Worked example
Task: write the adjacency list for this directed graph: A to B, A to C, B to C, C to A.
The list shows only outgoing edges for each vertex:
graph = {
"A": ["B", "C"],
"B": ["C"],
"C": ["A"],
}
For the matrix (order A, B, C), a 1 means an edge from the row vertex to the column vertex:
| A | B | C | |
|---|---|---|---|
| A | 0 | 1 | 1 |
| B | 0 | 0 | 1 |
| C | 1 | 0 | 0 |
It's not symmetrical, which shows the graph is directed.
Exam tip: when comparing the two representations, tie your answer to the type of graph: "an adjacency list uses less memory for a graph with few edges, because it stores only the connections that exist".
Check yourself
What is the difference between a directed and an undirected graph?
In an undirected graph the edges have no direction and work both ways. In a directed graph each edge has a direction and can only be followed one way.
What is a weighted graph?
A graph where each edge has a value, such as a distance, cost or time.
Why is the adjacency matrix of an undirected graph symmetrical?
Each edge works in both directions, so it appears twice: from X to Y and from Y to X.
When is an adjacency list better than an adjacency matrix?
For sparse graphs with few edges, because it stores only the edges that exist and so uses less memory.