8.2Graph with Adjacency Lists
Matrix or lists?
In this part you kept the same graph in two structures and wrote the same operations twice. The outputs were always the same; what changed was how long the operations took and how much memory they used. This lesson puts the two structures side by side and shows when to choose which. The short answer: lists when there are few edges, a matrix when there are many or when "are these two vertices adjacent?" is asked a lot.
- The memory and time differences of the two structures in one table
- What sparse and dense graphs are
- Which structure to choose for a problem
1One graph, two structures
An adjacency matrix sets aside a cell for every pair of vertices: n × n cells whether the edge exists or not. An adjacency list keeps, for each vertex, only the edges that exist, in a linked list. The two definitions differ in a single line:
// Matrix (8.1)struct Graph { int n; bool directed; int adj[MAX][MAX]; // adj[u][v] == 1: the edge exists}; // Lists (8.2)struct Graph { int n; bool directed; struct Node* adj[MAX]; // adj[u]: the neighbours of u};Ask both structures the same question: who are the neighbours of 4? In the matrix all six cells of row 4 are checked; three are 1 and three are 0. In the lists the three nodes of the list of 4 are walked, and all of them are neighbours.
2Operation by operation
In the table n is the number of vertices, e the number of edges and d the degree of the vertex in question. The list column is for lists kept sorted, as in this part.
| Operation | Matrix | Lists |
|---|---|---|
| Memory | O(n²) | O(n + e) |
| Are two vertices adjacent? | O(1) | O(d) |
| Walking the neighbours | O(n) | O(d) |
| Degree | O(n) | O(d) |
| In-degree (directed) | O(n) | O(n + e) |
| Adding an edge | O(1) | O(d) |
| Removing an edge | O(1) | O(d) |
| BFS, DFS | O(n²) | O(n + e) |
The matrix wins at operations on a single edge: does it exist, add it, remove it; each is one cell. Lists win at everything that rests on walking the neighbours: the degree, BFS, DFS and every graph algorithm built on them. Most graph algorithms walk neighbours, so lists are what is used most often in practice.
3Sparse and dense graphs
An undirected graph can have at most n(n − 1)/2 edges. If the number of edges is close to this limit the graph is called dense; if it is far below, sparse. Most real graphs are sparse: on a road map only a few roads leave each junction, and in a social network a person knows a few hundred of millions of people.
| Graph | Matrix (n²) | Lists (n + 2e) |
|---|---|---|
| This part's sample: n = 6, e = 7 | 36 | 20 |
| Road map: n = 10,000, e = 20,000 | 100,000,000 | 50,000 |
| Complete graph: n = 100, e = 4950 | 10,000 | 10,000 |
In a sparse graph almost every cell of the matrix is 0, and BFS checks every one of those zeros. In a dense graph the two structures end up at the same order; then the constant-time edge query and the simplicity of the matrix come first. Memory works the same way: a list node carries a number and an address, while a matrix cell is only a number. For a small dense graph the matrix can even take less room than the lists.
4Which one to choose?
- If the graph is sparse (edges a few times the vertices), or traversals like BFS and DFS will be run: lists.
- If the graph is dense, the number of vertices is small (a few hundred), or "is there an edge between
uandv?" is asked a lot: a matrix. - In a weighted graph the matrix cell holds the weight instead of 1, and the list node holds the weight next to the neighbour; the choice follows the same rule.
In Python the list structure is usually written with a dictionary or a list of lists instead of a linked list: adj[u] is a Python list. The idea is the same; every vertex keeps only its own neighbours, and the times do not change.
5Try it
You will run BFS on an undirected graph with 50,000 vertices and 120,000 edges. Which structure fits better?
The answer is A. The graph is sparse: a matrix holds 2.5 billion cells and BFS checks all of them. Lists finish in n + 2e, about 290,000 steps.
Show the answer
The answer is A. The graph is sparse: a matrix holds 2.5 billion cells and BFS checks all of them. Lists finish in n + 2e, about 290,000 steps.
Which operation is faster with a matrix than with lists?
The answer is B. In a matrix adj[u][v] is a single cell: O(1). With lists the list of u is walked: O(d).
Show the answer
The answer is B. In a matrix adj[u][v] is a single cell: O(1). With lists the list of u is walked: O(d).
In a graph with 8 vertices almost every pair of vertices has an edge, and the program keeps asking "are u and v adjacent?". Which do you choose?
The answer is A. The graph is small and dense; the matrix takes little room and answers the question in O(1).
Show the answer
The answer is A. The graph is small and dense; the matrix takes little room and answers the question in O(1).
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.