Chapter 25: Graphs and adjacency matrices
Motivation
In the preceding chapter, we defined graphs and built them up using structures. This time,
we show a particular representation of graphs called adjacency matrices, which allows us
to manipulate graphs using MATLAB’s powerful matrix libraries.
Adjacency Matrices
The adjacency matrix is an alternative way to represent a graph. Let’s start with a graph
whose nodes are already labeled with consecutive integer labels. In the adjacency matrix
of the graph, the matrix element a(
i
,
j
) = 1 if and only if there is an edge {
i
,
j
} in the graph,
and a(
i
,
j
) = 0 otherwise. Let’s look at an example:
The edges of this graph are [{1, 2} {1, 3} {2, 3} {3, 4}]. When we convert to matrix
form, we get:
A = [ 0 1 1 0
1 0 1 0
1 1 0 1
0 0 1 0 ]
For an undirected graph, if {
i
,
j
} exists, then both a(
i
,
j
) and a(
j
,
i
) must equal 1. So an
adjacency matrix for an undirected graph is symmetric. The adjacency matrix
representation is very simple (although not necessarily very efficient).
Path Counting
The adjacency matrix representation is very useful for analyzing paths in the graph.
Suppose for example, we wanted to count all the paths in the graph that have length
exactly 2, that is, they traverse two edges. Let
A
be the adjacency matrix of the graph,

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*