The Adjacency Matrix: How Graphs Talk to Neural Networks
The humble 0-and-1 grid that powers drug discovery, social networks, and modern AI
Every time a graph neural network learns to predict a molecule's toxicity, rank your social feed, or help a self-driving car see the road — it starts with the same humble data structure: a grid of zeros and ones called the adjacency matrix.
It's one of those ideas that feels almost too simple to matter. But once you understand it, you start seeing it everywhere.
What even is a graph?
Before the matrix, let's be clear about what a graph is — because the word is overloaded.
In this context, a graph is not a bar chart. It's a mathematical structure made of two things:
- Nodes (also called vertices): the things — people, atoms, cities, words
- Edges: the connections between them — friendships, bonds, roads, co-occurrences
A social network is a graph. A molecule is a graph. A knowledge base is a graph. A street map is a graph. The internet is a graph.
The challenge: how do you feed a graph into a neural network? Neural nets love tensors — neat rectangular arrays of numbers. Graphs are irregular, relational, and have no natural "order." You can't just flatten them into a spreadsheet.
Enter the adjacency matrix.
The adjacency matrix, explained simply
Take a graph with n nodes. The adjacency matrix A is an n × n grid where:
A[i][j] = 1if there is an edge between node i and node jA[i][j] = 0if there is no edge
That's it. A complete snapshot of who is connected to whom, encoded as numbers.
For an undirected graph (edges go both ways), the matrix is always symmetric: A[i][j] = A[j][i]. If A is connected to B, then B is connected to A.
Explore it yourself
The interactive widget below lets you see exactly how a graph maps to its adjacency matrix — in real time. Try hovering over a cell in the matrix, and watch the corresponding edge light up in the graph. Click nodes or cells to toggle edges.
Adjacency Matrix Explorer
Click nodes or matrix cells to toggle edges
A basic undirected graph with 4 nodes
Graph
Badge = degree (number of connections)
Adjacency Matrix A
| A | B | C | D | |
|---|---|---|---|---|
| A | · | 1 | 1 | 0 |
| B | 1 | · | 0 | 1 |
| C | 1 | 0 | · | 1 |
| D | 0 | 1 | 1 | · |
💡 Hover over a matrix cell to highlight the corresponding edge in the graph — and vice versa
A few things to notice as you explore:
- The diagonal is always zero (a node doesn't connect to itself, usually)
- The row sum of any row equals that node's degree — the number of edges it has
- Switching to "Directed" breaks the symmetry —
A[i][j]andA[j][i]can now differ
Why does this matter for GNNs?
Graph Neural Networks (GNNs) — and Geometric Deep Learning more broadly — work by passing messages between nodes. Each node collects information from its neighbors and updates its own representation.
The adjacency matrix is what defines those neighborhoods. Concretely, if you have a matrix of node features X (each row is a node's feature vector), then:
A · X
...gives you, for each node, the sum of its neighbors' features. That's the core of message passing — just a matrix multiplication.
This means the adjacency matrix is not just a description of the graph. It's an operator — it transforms feature representations by aggregating neighborhood information.
In practice, GNNs use a normalized version: D⁻¹A or D⁻½AD⁻½, where D is the degree matrix. This prevents nodes with many connections from dominating the aggregation.
Three graphs, three matrices
1. A friendship network
Four people: Alice, Bob, Carol, Dave. Alice knows Bob and Carol. Bob knows Dave. Carol knows Dave.
A B C D
A [ 0 1 1 0 ]
B [ 1 0 0 1 ]
C [ 1 0 0 1 ]
D [ 0 1 1 0 ]
Notice: it's symmetric. Alice–Bob appears as A[0][1] = 1 and A[1][0] = 1.
2. A molecule (atoms as nodes, bonds as edges)
In drug discovery, atoms are nodes and chemical bonds are edges. A GNN reads this matrix and learns which molecular configurations lead to bioactivity — without anyone hand-coding chemistry rules.
3. A citation network
Papers as nodes, citations as directed edges. A[i][j] = 1 means paper i cites paper j. This matrix is not symmetric — citing is not the same as being cited.
The honest limitations
The adjacency matrix is elegant, but it has real costs at scale.
Memory: An n × n matrix for a graph with 1 million nodes needs 1 trillion entries. That's not feasible. Real systems use sparse representations — only storing the positions of the 1s, not the 0s.
No node features: The matrix only encodes structure. In practice, nodes also carry feature vectors (age, atom type, word embedding). These are stored separately in a feature matrix X and combined with A during message passing.
Fixed size: The matrix assumes a fixed set of nodes. Graphs that grow over time need more dynamic approaches.
Most GNN libraries (PyTorch Geometric, DGL) store graphs as edge index lists — two arrays of source and destination node indices — rather than dense matrices. It's the same information, just memory-efficient.
From matrix to learning
The full GNN pipeline looks like this:
- Input: node feature matrix X, adjacency matrix A
- Message passing: each layer computes
H = σ(ÂXW), where  is the normalized adjacency, W is a learned weight matrix, and σ is an activation - Repeat: stack multiple layers so nodes gather information from 2-hop, 3-hop neighbors
- Output: updated node representations used for classification, regression, or link prediction
The adjacency matrix never changes during training. It's fixed structural information. What changes is W — the network learns how to weigh and combine neighborhood information to make good predictions.
The takeaway
The adjacency matrix is the translation layer between the irregular world of graphs and the rectangular world of neural networks. It's simple enough to fit on a whiteboard and powerful enough to underpin billion-parameter models.
Next time you hear about a GNN predicting protein interactions or a recommendation engine surfacing the right video — somewhere underneath, there's a grid of zeros and ones doing the quiet work of defining who is connected to whom.
That grid is the adjacency matrix. And now you know exactly how it works.