Part XXII — Graph Neural Networks & Structured Data · Chapter 1

Graphs as data

Hook

Part IV's convolution slides a fixed 3×3 window over an image, because every pixel has exactly 8 neighbors in the same fixed arrangement. A social network, a molecule, a road map — none of that holds. What replaces "neighbor" when there's no grid at all?

Intuition
01.012.020.033.041.052.0
click a node to see its neighbors

Click any node. Its neighbors light up — and notice they're not the same number of neighbors every time. Some nodes connect to three others, some to just one. There's no fixed window size that could ever slide over this.

Formalize
  1. Nodes, edges, and neighborhoods

    A graph is just a set of nodes (things) and edges (connections between them). The only structure a node has is which other nodes it touches — its neighborhood, N(v)\mathcal{N}(v) — and how many, its degree.

  2. The adjacency matrix encodes it all

    The whole graph can be written as an adjacency matrix AA, where Aij=1A_{ij}=1 if nodes ii and jj share an edge and 00 otherwise.

  3. Symmetric, zero diagonal, rows sum to degree

    AA is symmetric (an edge goes both ways) with a zero diagonal (no node is its own neighbor), and row ii's sum is exactly node ii's degree.

Play
01.012.020.033.041.052.0
degree(1) = 3 — neighbors {0, 2, 3}

Click node 1 or node 3 — both hubs with degree 3 — then click a leaf like node 4 or 5, with degree 1. Whatever comes next (message passing, attention, pooling) has to work correctly for both of these at once, with no assumption about how many neighbors show up.

Worked example

For this graph — a triangle {0,1,2}\{0,1,2\} fused to a two-node tail {4,5}\{4,5\} through hub node 33:

  1. Read off the neighborhoods by hand
    • N(0)={1,2}\mathcal{N}(0)=\{1,2\}, degree 22
    • N(1)={0,2,3}\mathcal{N}(1)=\{0,2,3\}, degree 33
    • N(2)={0,1}\mathcal{N}(2)=\{0,1\}, degree 22
    • N(3)={1,4,5}\mathcal{N}(3)=\{1,4,5\}, degree 33
    • N(4)={3}\mathcal{N}(4)=\{3\}, degree 11
    • N(5)={3}\mathcal{N}(5)=\{3\}, degree 11

    Degrees 2,3,2,3,1,12,3,2,3,1,1 — none of them equal.

  2. Build the adjacency matrix

    In node order 0,1,2,3,4,50,1,2,3,4,5, row ii has a 11 in column jj exactly where jN(i)j\in\mathcal{N}(i):

    • Row 00: (0,1,1,0,0,0)(0,1,1,0,0,0)
    • Row 11: (1,0,1,1,0,0)(1,0,1,1,0,0)
    • Row 22: (1,1,0,0,0,0)(1,1,0,0,0,0)
    • Row 33: (0,1,0,0,1,1)(0,1,0,0,1,1)
    • Row 44: (0,0,0,1,0,0)(0,0,0,1,0,0)
    • Row 55: (0,0,0,1,0,0)(0,0,0,1,0,0)

    Every row sums to that node's degree — row 33 sums to 33, matching N(3)=3|\mathcal{N}(3)|=3 exactly.

  3. Confirm what a grid convolution would get wrong

    A 3×3 kernel assumes 8 fixed neighbors, in a fixed spatial layout, for every pixel. Node 44 has one neighbor; node 11 has three, in no particular spatial order at all — there's no way to write a single fixed-size, fixed-shape kernel that covers both.

Checkpoint

Click through the nodes until you've found both leaves — the nodes with degree exactly 1. Found so far: none.

01.012.020.033.041.052.0
click a node
Click a node to check its degree
Summary
Aij={1if (i,j)edges0otherwise,deg(v)=jAvjA_{ij} = \begin{cases}1 & \text{if } (i,j)\in\text{edges}\\0 & \text{otherwise}\end{cases}, \qquad \deg(v) = \sum_j A_{vj}

Graphs generalize both the grid (an image is a graph where every node's neighborhood is the same fixed shape) and the sequence (a chain is a graph where every node has at most two neighbors, in order). The next chapters build architectures that work correctly no matter how many neighbors a node has, or how they're arranged — starting with the simplest possible rule: average them.