liveBook · Manning

Adjacency matrix

As explained in Graph Neural Networks in Action

An adjacency matrix is a matrix representation of a graph in which entries encode whether or how strongly pairs of nodes are connected.

An adjacency matrix puts a graph’s connections into a table. Each row and column corresponds to a node; the cell where they meet records whether those nodes share an edge. It gives you a quick way to inspect relationships that would be cumbersome to describe one edge at a time. A zero means no connection, while a nonzero value means an edge is present. section 1.2.1 section 8.3

Read a small graph

For nodes A through E, the A row contains 1 under B and E and 0 under A, C, and D. So A connects to B and E, but not to itself. The B–A and A–B cells match: this graph is undirected, and its matrix is symmetric across the diagonal. section 1.2.1

Table 1.1 The adjacency matrix for the simple graph in figure 1.1
A
B
C
D
E
A
0
1
0
0
1
B
1
0
1
1
1
C
0
1
0
1
0
D
0
1
1
0
1
E
1
1
0
1
0

Use the five-node table to trace a row’s neighbors, then compare cells on opposite sides of the diagonal. section 1.2.1

The cell values also tell you what kind of connection is represented:

  • In an unweighted graph, entries are 1 or 0. A weighted graph can use a larger value to make one edge more important than another. section 1.2.1
  • A zero on the diagonal means that node has no edge to itself. An undirected graph has matching values across the diagonal. section 1.2.1

The matrix is one representation, not the only way to prepare graph data. An adjacency list records each node’s neighbors; an edge list records connected node pairs. In a data pipeline, choose a representation that fits the raw data, processing steps, and model. For example, relationships in a passenger table can become graph edges or an adjacency matrix, making family and social connections available for graph-based learning. section 8.3 section 1.2.5

The same matrix can be a learning target. A graph autoencoder compresses graph data into a latent representation, then learns to reconstruct edges—equivalently, to reconstruct the adjacency matrix. In a PyTorch Geometric implementation, a GNN encoder receives node features and an edge index; an inner-product decoder scores edges from the resulting node representations. A molecule-generation training loop goes further: it compares a predicted adjacency matrix with one built from the molecule’s edge index and edge attributes. section 5.2.2 section 5.4.4

Node ordering matters to the table’s layout, but a graph-learning method should not depend on an arbitrary ordering: consistently reordering its rows and columns should leave the result unaffected. A conventional two-dimensional adjacency matrix also describes pairwise edges; a hyperedge joining multiple nodes calls for a higher-dimensional incidence tensor. For evolving graphs, adjacency matrices from different times can instead be combined along the diagonal to include temporal information. section 1.2.4 section 1.2.2 section 6.6

How Graph Neural Networks in Action covers it

How deep it goes
Defines matrix entries and works through symmetry, weights, and self-connections; extends to model workflows without a formal derivation.
Examples
Uses a five-node matrix, passenger relationships, and predicted versus real molecular adjacency matrices.
What it assumes
Introduces nodes, edges, and matrix entries through a small graph before using them in graph-learning workflows.
What you can do after
Evaluate/understand graph connections from matrix entries and apply the matrix as a reconstruction target in a graph autoencoder.
How it connects
Connects adjacency lists, edge lists, permutation invariance, graph autoencoders, hypergraphs, and spatiotemporal GNNs.

Also explained in

Compare how 8 books explain adjacency matrix →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Neural Networks in Action. The section links point to the parts of the book it draws on.