Skip to content

Adjacency matrix

An adjacency matrix is a way to represent a graph using a square matrix.

This is especially useful for representing graphs in computers.

An adjacency matrix looks like this:

V1V2V3
V1ABC
V2DEF
V3GHI

Where:

  • The rows and columns represent the vertices of the graph, , , , etc.
  • The entries in the matrix (A, B, C, etc.) indicate the number of edges attaching a node to another node.

In an undirected graph, the adjacency matrix is symmetric along the main diagonal (from the top-left to the bottom-right).

That’s because an edge from vertex to vertex is the same as an edge from to in an undirected graph.

For the following undirected graph:

A
/ \
B---C

The adjacency matrix for this graph would be:

ABC
A011
B101
C110

For this undirected graph:

A
/ \
B===C
\
D

The adjacency matrix for this graph would be:

ABCD
A0110
B1021
C1200
D0100