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.

Structure of an adjacency matrix

An adjacency matrix looks like this:

V1V2V3
V1ABC
V2DEF
V3GHI

Where:

Symmetry

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 V1 to vertex V2 is the same as an edge from V2 to V1 in an undirected graph.

Example 1

For the following undirected graph:

    A
   / \
  B---C

The adjacency matrix for this graph would be:

ABC
A011
B101
C110

Example 2

For this undirected graph:

    A
   / \
  B===C
   \
    D

The adjacency matrix for this graph would be:

ABCD
A0110
B1021
C1200
D0100