Lecture 14: directed graphs and paths [00JA]
- January 22, 2024
- Jon Sterling, with contributions from Marcelo Fiore
These lecture notes were prepared by Jon Sterling using Marcelo Fiore’s lectures as source material. Any mistakes were introduced by Jon Sterling. Generative AI was not used to write these lecture notes.
Adding matrices together [00JS]
- January 20, 2024
- Jon Sterling
Definition Pointwise addition of matrices [00JO]
- January 20, 2024
- Jon Sterling
Let M,N:\mathrm {Mat}_{S}{\mathopen {}\left (m,n\right )\mathclose {}} be two matrices over the same semiring with the same dimensions. We will write M\oplus N:\mathrm {Mat}_{S}{\mathopen {}\left (m,n\right )\mathclose {}} for the (pointwise) addition of the two matrices:
{\mathopen {}\left (M\oplus N\right )\mathclose {}}_{i,j} = M_{i,j} \oplus N_{i,j}Definition The zero matrix [00JP]
- January 20, 2024
- Jon Sterling
A zero matrix is one that whose cells all contain 0. In particular, we have:
\begin {aligned} \mathbf {0} &:\mathrm {Mat}_{S}{\mathopen {}\left (m,n\right )\mathclose {}}\\ \mathbf {0}_{i,j} &= 0 \end {aligned}Lemma Associativity, commutativity, and unit laws of matrix addition [00JQ]
- January 20, 2024
- Jon Sterling
Matrix addition forms an associative and commutative operation on \mathrm {Mat}_{S}{\mathopen {}\left (m,n\right )\mathclose {}} with zero matrices \mathbf {0} as neutral element.
Proof.
- January 20, 2024
- Jon Sterling
This follows immediately from the associativity, commutativity, and unit laws of the additive submonoid {\mathopen {}\left (+,0\right )\mathclose {}} of the semiring S.
Lemma Matrix addition and union of relations [00JR]
- January 20, 2024
- Jon Sterling
The pointwise sum M\oplus N of M,N:\mathrm {Mat}_{\mathbb {B}}{\mathopen {}\left (m,n\right )\mathclose {}} corresponds to the union of the relations associated to M and N:
\operatorname {\underline {rel}}_{\mathbb {B}}{\mathopen {}\left (M\oplus N\right )\mathclose {}} = \operatorname {\underline {rel}}_{\mathbb {B}}M \cup \operatorname {\underline {rel}}_{\mathbb {B}}NMoreover, the zero matrix corresponds to the empty relation: \operatorname {\underline {rel}}_{\mathbb {B}}\mathbf {0} = \varnothing .
Proof.
- January 20, 2024
- Jon Sterling
This follows because addition and zero in the boolean semiring are given by disjunction and falsehood respectively.
Directed graphs and paths [00JT]
- January 20, 2024
- Jon Sterling, with contributions from Marcelo Fiore
Definition Directed graph [00JC]
- January 18, 2024
- Jon Sterling, with contributions from Marcelo Fiore
A directed graph {\mathopen {}\left (A,R\right )\mathclose {}} consists of a set A and a binary relation R\colon A\mathbin {\nrightarrow } A from A to itself; such a relation from a set to itself is called an endo-relation.
Corollary Monoid structure of endo-relations [00JD]
- January 18, 2024
- Jon Sterling, with contributions from Marcelo Fiore
For every set A, the structure {\mathopen {}\left (\mathrm {Rel}{\mathopen {}\left (A,A\right )\mathclose {}},\mathsf {id}_{A},\circ \right )\mathclose {}} given by endo-relations is a monoid.
Proof.
- January 18, 2024
- Jon Sterling
We have seen in Lemma [00IP] that relational composition is associative and has identity relations \mathsf {id}_{A} as neutral elements.
Definition Iteration of endo-relations [00JE]
- January 18, 2024
- Jon Sterling, with contributions from Marcelo Fiore
For R\colon A\mathbin {\nrightarrow } A and n:\mathbb {N}, we define the n-fold iteration of R to be the following endo-relation R^{\circ n}\colon A\mathbin {\nrightarrow } A:
R^{\circ n} = \underbrace {R\circ \cdots \circ R}_{n\text { times}}More precisely, we define R^{\circ n} by recursion on n:
\begin {aligned} R^{\circ 0} &= \mathsf {id}_{A}\\ R^{\circ {\mathopen {}\left (n+1\right )\mathclose {}}} &= R\circ R^{\circ n} \end {aligned}Definition Path in a directed graph [00JF]
- January 18, 2024
- Jon Sterling, with contributions from Marcelo Fiore
Let {\mathopen {}\left (A,R\right )\mathclose {}} be a directed graph. For s,t:A, a path of length n: \mathbb {N} in R with source s and target t is a tuple {\mathopen {}\left (a_0,\ldots ,a_n\right )\mathclose {}}:A^{n+1} such that a_0=s, a_n=t, and a_i\mathrel {R}a_{i+1} for all 0\leq i <n.
We will later see in Theorem [00JZ] that the path relation is the reflexive-transitive closure.
Definition The path relation for an endo-relation [00JH]
- January 18, 2024
- Jon Sterling, with contributions from Marcelo Fiore
For R\colon A\mathbin {\nrightarrow } A, we define R^{\circ *}\colon A\mathbin {\nrightarrow } A to be the smallest relation closed under all finite paths in R.
R^{\circ *} = \bigcup _{n:\mathbb {N}} R^{\circ n}Observation Path relation of finite endo-relations [00JU]
- January 21, 2024
- Jon Sterling
In the case of an endo-relation R:\mathrm {Rel}{\mathopen {}\left ({\mathopen {}\left [n\right ]\mathclose {}}\right )\mathclose {}}, the path relation R^{\circ *} can be computed using a finite union rather over m \leq n rather than an infinite union over all of \mathbb {N}; in particular, we have R^{\circ *} = \bigcup _{k \leq n} R^{\circ k}.
Proof.
- January 21, 2024
- Jon Sterling
There are only n distinct elements that could be related, so there cannot be a chain of distinct elements with length o>n, and any finite path in R from s to t can be replaced by one that has no repetitions.
Definition Adjacency matrix of a finite directed graph [00JN]
- January 20, 2024
- Jon Sterling, with contributions from Marcelo Fiore
We have seen in Lecture 13 (Definition [00IY]) how to turn any relation R\colon {\mathopen {}\left [m\right ]\mathclose {}}\mathbin {\nrightarrow }{\mathopen {}\left [n\right ]\mathclose {}} into a matrix \operatorname {\underline {mat}}_{\mathbb {B}}R:\mathrm {Mat}_{\mathbb {B}}{\mathopen {}\left (m,n\right )\mathclose {}} over the boolean semiring. When R\colon {\mathopen {}\left [n\right ]\mathclose {}}\mathbin {\nrightarrow }{\mathopen {}\left [n\right ]\mathclose {}} is the edge relation in a graph with vertices in the finite set {\mathopen {}\left [n\right ]\mathclose {}}, we refer to \operatorname {\underline {mat}}_{\mathbb {B}}R:\mathrm {Mat}_{\mathbb {B}}{\mathopen {}\left (n,n\right )\mathclose {}} as the adjacency matrix of R.
Algorithm The adjacency matrix of the path relation [00JV]
- January 21, 2024
- Jon Sterling, with contributions from Marcelo Fiore
Let M = \operatorname {\underline {mat}}_{\mathbb {B}}R be the adjacency matrix of a finite directed graph {\mathopen {}\left ({\mathopen {}\left [n\right ]\mathclose {}},R\right )\mathclose {}}. We will show that the adjacency matrix M^* of the path relation R^{\circ *} can be computed recursively using the additive and multiplicative structure of matrices. In particular, we can define M^* = M_n where M_k is computed recursively as below:
\begin {aligned} M_0 &= I^n\\ M_{k+1} &= I^n \oplus {\mathopen {}\left (M\otimes M_k\right )\mathclose {}} \end {aligned}Thus we have a recursive algorithm that can decide whether there is a path between two elements in a finite directed graph: indeed, there is a path from s to t in R if and only if M^*_{s,t} =\mathsf {true}.
Proof.
- January 21, 2024
- Jon Sterling
We first unravel the first few M_k’s to get a better understanding.
\begin {aligned} M_1 &= I^n \oplus {\mathopen {}\left (M\otimes M_0\right )\mathclose {}} \\ &= I^n\oplus {\mathopen {}\left (M\otimes I^n\right )\mathclose {}} \\ &= I^n \oplus M \\ M_2 &= I^n \oplus {\mathopen {}\left (M\otimes M_1\right )\mathclose {}} \\ &= I^n \oplus {\mathopen {}\left (M\otimes {\mathopen {}\left (I^n \oplus M\right )\mathclose {}}\right )\mathclose {}} \\ &= I^n \oplus {\mathopen {}\left (M\otimes I^n\right )\mathclose {}} \oplus {\mathopen {}\left (M\otimes M\right )\mathclose {}} \\ &= I^n \oplus M \oplus M^2 \end {aligned}By induction, we can extend the pattern above to a closed form for each M_k. In particular, we deduce that M_k = \bigoplus _{l\leq k} M^l by recalling from Lemma [00J3] that the identity matrix is the neutral element for matrix multiplication. Therefore, we have M_n =\bigoplus _{k \leq n}M^k.
Our goal is to show that M_n = \operatorname {\underline {mat}}_{\mathbb {B}} R^{\circ *}; we have already seen in Lemma [00JR] and Lemma [00J2] that matrix addition corresponds to union of relations and matrix multiplication corresponds to relational composition.
By Observation [00JU] (Path relation of finite endo-relations), we have R^{\circ *} = \bigcup _{k\leq n} R^{\circ k}. Therefore:
\operatorname {\underline {mat}}_{\mathbb {B}}R^{\circ *} = \operatorname {\underline {mat}}_{\mathbb {B}}\bigcup _{k\leq n} R^{\circ k}By Lemma [00JR] (Matrix addition and union of relations), we have:
\operatorname {\underline {mat}}_{\mathbb {B}}\bigcup _{k\leq n} R^{\circ k} = \bigoplus _{k\leq n} \operatorname {\underline {mat}}_{\mathbb {B}}R^{\circ k} By Lemma [00J2] (Matrix product is relational composition), we have: \bigoplus _{k\leq n} \operatorname {\underline {mat}}_{\mathbb {B}}R^{\circ k} = \bigoplus _{k\leq n} {\mathopen {}\left (\operatorname {\underline {mat}}_{\mathbb {B}}R\right )\mathclose {}}^k = \bigoplus _{k\leq n} M^k = M_nPreorders and relations [00JW]
- January 21, 2024
- Jon Sterling, with contributions from Marcelo Fiore
Definition Preorder [00JX]
- January 21, 2024
- Jon Sterling, with contributions from Marcelo Fiore
A preorder {\mathopen {}\left (P,\sqsubseteq \right )\mathclose {}} consists of a set P and a relation {\sqsubseteq } \colon P\mathbin {\nrightarrow } P satisfying the following two axioms:
- Reflexivity. \forall x: P\mathpunct {.} x\sqsubseteq x
- Transitivity. \forall x,y,z: P\mathpunct {.} {\mathopen {}\left (x\sqsubseteq y\land y \sqsubseteq z\right )\mathclose {}}\implies x\sqsubseteq z
In other words, a preorder is a directed graph such that the edge relation is both reflexive and transitive.
Example Examples of preorders [00JY]
- January 21, 2024
- Jon Sterling, with contributions from Marcelo Fiore
Preorders are everywhere in both general mathematics and computer science.
- The real numbers equipped with their inequality relation form a preorder \mathbb {R},\leq . The converse relation {\mathopen {}\left (\mathbb {R},\geq \right )\mathclose {}} of course also forms a preorder. This example can be specialised to different classes of numbers, such as integers or naturals.
- Subsets with their inclusion and containment relations form preorders {\mathopen {}\left (\mathcal {P}(A),\subseteq \right )\mathclose {}} and {\mathopen {}\left (\mathcal {P}(A),\supseteq \right )\mathclose {}}. This example can be specialised to other examples that have additional structure, such as the preorder \mathcal {P}_f(A) of finite subsets of a given set A, or the preorder \mathcal {O}(X) of open sets of a topological space X.
- A slightly more nontrivial example is furnished by the preorder {\mathopen {}\left (\mathbb {Z},{\mid }\right )\mathclose {}} of the integers and the divisibility relation, where m\mid n if and only if \exists k:\mathbb {Z}\mathpunct {.} m k = n: indeed, we certainly have n divides n, and if l divides m and m divides n, then l divides m.
The first example above happen to be total orders (meaning that the underlying directed graph is connected): so {\mathopen {}\left (\mathcal {P}{\mathopen {}\left (A\right )\mathclose {}},\subseteq \right )\mathclose {}} and {\mathopen {}\left (\mathbb {Z},{\mid }\right )\mathclose {}} are example of non-total preorders.
All the examples we have seen so far are, furthermore, partial orders: a preorder is a partial order when we have a \sqsubseteq b \land b \sqsubseteq a if and only if a=b. In fact, there are arguably very few useful preorders that are not partial orders, and it is always possible to replace a preorder by an equivalent partial order; we do not pursue this here.
Theorem Reflexive-transitive closure and path relation [00JZ]
- January 21, 2024
- Jon Sterling, with contributions from Marcelo Fiore
The path relation R^{\circ *} of an endo-relation R\colon A\mathbin {\nrightarrow } A is, by definition, a preorder. We will show that R^{\circ *} enjoys a universal property as the smallest preorder containing R as a subrelation; in other words, it is the reflexive-transitive closure.
To be more precise, let \mathcal {F}_R be the set of preorders on A that contain R as a subrelation:
\mathcal {F}_R = {\mathopen {}\left \{Q\colon A\mathbin {\nrightarrow } A\,\middle \vert \,R\subseteq Q \land \text {Q is a preorder}\right \}\mathclose {}}Then we have R^{\circ *}=\bigcap \mathcal {F}_R, and so the path relation is the smallest preorder containing R as a subrelation. (Indeed, recall that the intersection of an intersection-closed class of relations is the smallest relation in that class.)
Proof.
- January 21, 2024
- Jon Sterling
To show that R^{\circ *}=\bigcap \mathcal {F}_R, it suffices to check that \bigcap \mathcal {F}_R\subseteq R^{\circ *} and R^{\circ *}\subseteq \bigcap \mathcal {F}_R.
We must first show that \bigcap \mathcal {F}_R\subseteq R^{\circ *}. Because \bigcap \mathcal {F}_R is the smallest preorder containing R, it suffices to show that R^{\circ *} is a preorder containing R, i.e. that R^{\circ *}\in \mathcal {F}_R. This holds almost by definition:
- We first need to show that R\subseteq R^{\circ *}, i.e. that when a\mathrel {R}b we have a\mathrel {R}^{\circ *}b. This is evident, as b\mathrel {R}b is a path of length 1 in R and R^{\circ *} contains all finite paths in R.
- Next, we need to show that R^{\circ *} is a preorder. But this holds because R^{\circ *} contains the empty path from an element to itself, and because a path of length m from a to b can be concatenated with a path of length n from b to c to obtain a path (of length m+n) from a to c.
Our second goal is to prove R^{\circ *}\subseteq \bigcap \mathcal {F}_R; by the definition of intersections, this holds if and only if \forall Q\in \mathcal {F}_R\mathpunct {.} R^{\circ *}\subseteq Q.
Fixing such a preorder Q over A containing R as a subrelation, we must prove that R^{\circ *}\subseteq Q. Recalling that R^{\circ *} is the union \bigcup _{n :\mathbb {N}}R^{\circ n}, we see by the definition of unions that that R^{\circ *}\subseteq Q if and only if for each n:\mathbb {N}, we have R^{\circ n} \subseteq Q. This we prove by induction on n:\mathbb {N}.
- In the base case, we must show that R^{\circ 0} = \mathsf {id}_{A} is a subrelation of Q. This is equivalent to Q being reflexive, which we deduce from our assumption that Q is a preorder.
- In the inductive step, we may assume R^{\circ n}\subseteq Q and we must prove that R^{\circ {\mathopen {}\left (n+1\right )\mathclose {}}}=R\circ R^{\circ n} is a subrelation of Q. For any a,b:A, we have a\mathrel {{\mathopen {}\left (R\circ R^{\circ n}\right )\mathclose {}}} b if and only if there exists some c:A such that a\mathrel {R}c and c\mathrel {R^{\circ n}} b. Because we have assumed that R\subseteq Q, we have a\mathrel {Q}c; by our inductive hypothesis, we have c\mathrel {Q}b. Because Q is a preorder and therefore transitive, we therefore have a\mathrel {Q}b.
Therefore, R^{\circ *} is indeed the intersection of \mathcal {F}_R.