Skip to content
Part IA Michaelmas, Lent Term

Relations as Matrices

Boolean Matrices

A boolean matrix is a matrix whose entries are 00 or 11, with operations:

  • And (∧\land): for multiplication-like operations
  • Or (∨\lor): for addition-like operations

The Boolean Semiring

The boolean semiring B={0,1}\mathbb{B} = \{0, 1\} has:

  • Addition: 0∨0=00 \lor 0 = 0, 0∨1=10 \lor 1 = 1, 1∨0=11 \lor 0 = 1, 1∨1=11 \lor 1 = 1 (OR)
  • Multiplication: 0∧0=00 \land 0 = 0, 0∧1=00 \land 1 = 0, 1∧0=01 \land 0 = 0, 1∧1=11 \land 1 = 1 (AND)

Matrix-Relation Correspondence

From Relation to Matrix

For finite sets A=[m]A = [m] and B=[n]B = [n], a relation R:A↛BR : A \nrightarrow B corresponds to an m×nm \times n boolean matrix mat(R)\text{mat}(R):

mat(R)ij={1if iRj0otherwise\text{mat}(R)_{ij} = \begin{cases} 1 & \text{if } iRj \\ 0 & \text{otherwise} \end{cases}

From Matrix to Relation

Given an m×nm \times n boolean matrix MM, define rel(M):[m]↛[n]\text{rel}(M) : [m] \nrightarrow [n] by:

i rel(M) j  ⟺  Mij=1i\,\text{rel}(M)\,j \iff M_{ij} = 1

Mutual Inverses

rel(mat(R))=Randmat(rel(M))=M\text{rel}(\text{mat}(R)) = R \quad \text{and} \quad \text{mat}(\text{rel}(M)) = M

This is a bijection between relations [m]↛[n][m] \nrightarrow [n] and m×nm \times n boolean matrices.

Matrix Operations for Relations

Composition as Matrix Multiplication

For R:[m]↛[n]R : [m] \nrightarrow [n] and S:[n]↛[p]S : [n] \nrightarrow [p]:

mat(S∘R)=mat(S)⊗mat(R)\text{mat}(S \circ R) = \text{mat}(S) \otimes \text{mat}(R)

where ⊗\otimes is boolean matrix multiplication:

(M⊗N)ik=⋁j=1n(Mij∧Njk)(M \otimes N)_{ik} = \bigvee_{j=1}^{n} (M_{ij} \land N_{jk})

Union as Matrix Addition

mat(R∪S)=mat(R)⊕mat(S)\text{mat}(R \cup S) = \text{mat}(R) \oplus \text{mat}(S)

where ⊕\oplus is boolean matrix addition (pointwise OR):

(M⊕N)ij=Mij∨Nij(M \oplus N)_{ij} = M_{ij} \lor N_{ij}

Example

Let R:{1,2}↛{a,b}R : \{1, 2\} \nrightarrow \{a, b\} with R={(1,a),(2,b)}R = \{(1, a), (2, b)\}.

mat(R)=(1001)\text{mat}(R) = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}

Let S:{a,b}↛{X,Y}S : \{a, b\} \nrightarrow \{X, Y\} with S={(a,X),(b,Y)}S = \{(a, X), (b, Y)\}.

mat(S)=(1001)\text{mat}(S) = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}

Then S∘R={(1,X),(2,Y)}S \circ R = \{(1, X), (2, Y)\} and:

mat(S)⊗mat(R)=(1001)⊗(1001)=(1001)\text{mat}(S) \otimes \text{mat}(R) = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} \otimes \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}

Reachability via Matrix Powers

The Adjacency Matrix

For a relation R:[n]↛[n]R : [n] \nrightarrow [n], the matrix mat(R)\text{mat}(R) is the adjacency matrix of the corresponding directed graph.

Path Matrices

The matrix mat(R)∘k=mat(R)⊗⋯⊗mat(R)\text{mat}(R)^{\circ k} = \text{mat}(R) \otimes \cdots \otimes \text{mat}(R) (k times) encodes paths of length exactly kk.

Computing Reachability

Define:

M0=In(identity matrix)M_0 = I_n \quad \text{(identity matrix)} Mk+1=In⊕(M⊗Mk)M_{k+1} = I_n \oplus (M \otimes M_k)

Then Mn=mat(R∘∗)M_n = \text{mat}(R^{\circ *}), the adjacency matrix of the path relation.

Why This Works

  • A simple path in an nn-vertex graph has length <n< n
  • MkM_k accumulates all paths of length ≤k\leq k
  • By step nn, all reachable vertices are discovered

Example

For R={(1,2),(2,3),(3,1)}R = \{(1, 2), (2, 3), (3, 1)\} (a 3-cycle):

mat(R)=(010001100)\text{mat}(R) = \begin{pmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 0 & 0 \end{pmatrix}

Computing M3M_3 gives mat(R∘∗)=mat({1,2,3}×{1,2,3})\text{mat}(R^{\circ *}) = \text{mat}(\{1, 2, 3\} \times \{1, 2, 3\}) (everything reachable).


Semirings for Different Graph Problems

The power of matrix multiplication is that changing the underlying semiring changes what problem we solve.

The General Formula

Matrix multiplication over a semiring (S,⊕,⊗,0,1)(S, \oplus, \otimes, 0, 1):

(M⊗N)ik=⨁j=1n(Mij⊗Njk)(M \otimes N)_{ik} = \bigoplus_{j=1}^{n} (M_{ij} \otimes N_{jk})

This formula works for ANY semiring!

Comparison of Semirings

SemiringAddition ⊕\oplusMultiply ⊗\otimesZeroOneComputes
Boolean∨\lor (OR)∧\land (AND)0011Reachability
Min-Plusmin⁡\min++∞\infty00Shortest path
Max-Plusmax⁡\max++−∞-\infty00Longest path
Standard++×\times0011Path counting

Semiring comparison for graph algorithms

The Boolean Semiring: Reachability

For adjacency matrix AA:

Aijk=1  ⟺  exists path of length k from i to jA^k_{ij} = 1 \iff \text{exists path of length } k \text{ from } i \text{ to } j

Aij∗=1  ⟺  exists ANY path from i to jA^*_{ij} = 1 \iff \text{exists ANY path from } i \text{ to } j

The Min-Plus Semiring: Shortest Path

For weighted adjacency matrix WW (where WijW_{ij} = weight of edge i→ji \to j, ∞\infty if no edge):

Wij(k)=min⁡j1,…,jk−1(Wij1+Wj1j2+⋯+Wjk−1j)W_{ij}^{(k)} = \min_{j_1, \ldots, j_{k-1}}(W_{ij_1} + W_{j_1 j_2} + \cdots + W_{j_{k-1}j})

This is the minimum cost of any path of exactly kk edges.

Wij∗=shortest path cost from i to jW^*_{ij} = \text{shortest path cost from } i \text{ to } j

Example: Shortest Path

Consider weighted graph:

    1 --3--> 2
    |        |
    4        1
    |        |
    v        v
    3 --2--> 4

Adjacency matrix (min-plus):

W=(∞34∞∞∞∞1∞∞∞2∞∞∞∞)W = \begin{pmatrix} \infty & 3 & 4 & \infty \\ \infty & \infty & \infty & 1 \\ \infty & \infty & \infty & 2 \\ \infty & \infty & \infty & \infty \end{pmatrix}

W2W^2 computes shortest paths of exactly 2 edges:

W1,42=min⁡(W1,2+W2,4,W1,3+W3,4)=min⁡(3+1,4+2)=4W^2_{1,4} = \min(W_{1,2} + W_{2,4}, W_{1,3} + W_{3,4}) = \min(3+1, 4+2) = 4

The path 1→2→41 \to 2 \to 4 costs 4, which is cheaper than 1→3→41 \to 3 \to 4 (cost 6).

Identity Elements in Min-Plus

  • Additive identity (⊕\oplus-identity): ∞\infty (since min⁡(x,∞)=x\min(x, \infty) = x)
  • Multiplicative identity (⊗\otimes-identity): 00 (since x+0=xx + 0 = x)

Annnihilation Property

∞+x=∞\infty + x = \infty

Even if a path exists (xx finite), if you must traverse a non-existent edge (∞\infty), the total is impossible.

Warning: Negative Weights

The min-plus semiring works correctly only when:

  • All edge weights are non-negative, OR
  • The graph has no negative-weight cycles

If negative cycles exist, shortest paths may not be well-defined.


Summary

  • Relations on finite sets correspond to boolean matrices
  • Composition corresponds to boolean matrix multiplication
  • Union corresponds to boolean matrix addition
  • Matrix powers compute reachability in directed graphs
  • Changing the semiring changes the problem: Boolean = reachability, Min-Plus = shortest path
  • Min-Plus semiring: ⊕=min⁡\oplus = \min, ⊗=+\otimes = +, identities are ∞\infty and 00