Graphs Using Adjacency Matrix and List
Graphs Using Adjacency Matrix and List
Representing Graphs
Algorithms and Data Structures: We see two basic ways to represent graphs:
using adjacency matrices and by means of adjacency lists.
L ECTURE N OTES
c Carnegie Mellon University 2018
Lecture 23: Representing Graphs 2
1 Undirected Graphs
We start with undirected graphs which consist of a set V of vertices (also
called nodes) and a set E of edges, each connecting two different vertices.
The following is a simple example of an undirected graph with 5 vertices
(A, B, C, D, E) and 6 edges (AB, BC, CD, AE, BE, CE):
2 Implicit Graphs
There are many, many different ways to represent graphs. In some ap-
plications they are never explicitly constructed but remain implicit in the
way the problem was solved. The game of Lights Out is one example of
a situation that implicitly describes an undirected graph. Lights Out is an
electronic game consisting of a grid of lights, usually 5 by 5. The lights are
initially pressed in some pattern of on and off, and the objective of the game
is to turn all the lights off. The player interacts with the game by touching
a light, which toggles its state and the state of all its cardinally adjacent
neighbors (up, down, left, right).
We can think of lights out as an implicit graph with 225 vertices, one for ev-
ery possible configuration of the 5x5 lights out board, and an edge between
Lecture 23: Representing Graphs 3
two vertices if we can transition from one board to another with a single
button press. If we transition from one board to another by pressing a but-
ton, we can return to the first board by pressing the same button. Therefore
the graph is undirected.
decide that it does not make sense to add an edge into a graph when that
edge is already there, hence the second precondition. A neighbor list is just
a linked list of nodes.
With this minimal interface, we can create a graph for what will be our
Lecture 23: Representing Graphs 5
4 Adjacency Matrices
One simple way is to represent the graph as a two-dimensional array that
describes its edge relation as follows.
There is a checkmark in the cell at row v and column v 0 exactly when there
is an edge between nodes v and v 0 . This representation of a graph is called
Lecture 23: Representing Graphs 6
an adjacency matrix, because it is a matrix that stores which nodes are neigh-
bors.
We can check if there is an edge from B (= 1) to D (= 3) by looking for
a checkmark in row 1, column 3. In an undirected graph, the top-right
half of this two-dimensional array will be a mirror image of the bottom-
left, because the edge relation is symmetric. Because we disallowed edges
between a node and itself, there are no checkmarks on the main diagonal
of this matrix.
The adjacency matrix representation requires a lot of space: for a graph
with v vertices we must allocate space in O(v 2 ). However, the benefit of the
adjacency matrix representation is that adding an edge (graph_addedge)
and checking for the existence of an edge (graph_hasedge) are both O(1)
operations.
Are the space requirements for adjacency matrices (requires space in
O(v 2 )) worse than the space requirements for storing all the edges in a
linked list (requires space in O(e))? That depends on the relationship be-
tween v, the number of vertices, and e the number of edges. A graph with
v vertices has between 0 and v2 = v(v−1)
2 edges. If most of the edges ex-
ist, so that the number of edges is proportional to v 2 , we say the graph
is dense. For a dense graph, O(e) = O(v 2 ), and so adjacency matrices are
a good representation strategy for dense graphs, because in big-O terms
they don’t take up more space than storing all the edges in a linked list,
and operations are much faster.
5 Adjacency Lists
If a graph is not dense, then we say the graph is sparse. The other classic
representation of a graphs, adjacency lists, can be a good representation of
sparse graphs.
In an adjacency list representation, we have a one-dimensional array
that looks much like a hash table. Each vertex has a spot in the array, and
each spot in the array contains a linked list of all the other vertices con-
nected to that vertex. Our running example would look like this as an
adjacency list:
Lecture 23: Representing Graphs 7
The following table summarizes and compares the asymptotic cost as-
sociated with the adjacency matrix and adjacency list implementations of a
graph, under the assumptions used in this chapter.
25 }
26 return false;
27 }
Because we assume an edge must not already exist when we add it to
the graph, we can add an edge in constant time:
29 void graph_addedge(graph *G, vertex v, vertex w) {
30 REQUIRES(is_graph(G) && is_vertex(G, v) && is_vertex(G, w));
31 REQUIRES(v != w && !graph_hasedge(G, v, w));
32
33 adjlist *L;
34
45 ENSURES(is_graph(G));
46 ENSURES(graph_hasedge(G, v, w));
47 }
Finding the neighbors of a vertex is just a matter of returning its adja-
cency list.
49 vert_list *graph_get_neighbors(graph *G, vertex v) {
50 REQUIRES(is_graph(G) && is_vertex(G, v));
51 return G->adj[v];
52 }
It is tempting to implement the operation graph_free_neighbors so
that it frees every node in its input neighbor list. But this would destroy
our representation since graph_get_neighbor returned an alias into the
adjacency list representation of the graph. Instead, we shall define this
function so that it does nothing
54 void graph_free_neighbors(vert_list *L) {
55 (void)L;
56 }
Lecture 23: Representing Graphs 10
Here, (void)L serves the purpose of fooling the compiler into believing
that this function is using the variable L. Without it, our standard compila-
tion flags would cause it to report an error.
Summarizing, our analysis tells us that graph_print has cost O(max(v, e))
in the adjacency list representation and O(v 2 ) with the adjacency matrix
representation. For a dense graph — where e ∈ O(v 2 ) — these two expres-
sions are equivalent. For a sparse graph, the former can be significantly
cheaper.
Exercises
Exercise 1. Define the representation functions is_graph and is_vertex (and
any other you may need) used in the contracts of the adjacency matrix implemen-
tation in Section 6 of the graph interface of Section 3.