cphb/chapter23.tex

851 lines
18 KiB
TeX
Raw Normal View History

2016-12-28 23:54:51 +01:00
\chapter{Matrices}
2017-01-14 16:35:27 +01:00
\index{matrix}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
A \key{matrix} is a mathematical concept
that corresponds to a two-dimensional array
in programming. For example,
2016-12-28 23:54:51 +01:00
\[
A =
\begin{bmatrix}
6 & 13 & 7 & 4 \\
7 & 0 & 8 & 2 \\
9 & 5 & 4 & 18 \\
\end{bmatrix}
\]
2017-01-14 16:35:27 +01:00
is a matrix of size $3 \times 4$, i.e.,
it has 3 rows and 4 columns.
The notation $[i,j]$ refers to
the element in row $i$ and column $j$
in a matrix.
For example, in the above matrix,
$A[2,3]=8$ and $A[3,1]=9$.
\index{vector}
A special case of a matrix is a \key{vector}
that is a one-dimensional matrix of size $n \times 1$.
For example,
2016-12-28 23:54:51 +01:00
\[
V =
\begin{bmatrix}
4 \\
7 \\
5 \\
\end{bmatrix}
\]
2017-01-14 16:35:27 +01:00
is a vector that contains three elements.
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
\index{transpose}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
The \key{transpose} $A^T$ of a matrix $A$
is obtained when the rows and columns in $A$
are swapped, i.e., $A^T[i,j]=A[j,i]$:
2016-12-28 23:54:51 +01:00
\[
A^T =
\begin{bmatrix}
6 & 7 & 9 \\
13 & 0 & 5 \\
7 & 8 & 4 \\
4 & 2 & 18 \\
\end{bmatrix}
\]
2017-01-14 16:35:27 +01:00
\index{square matrix}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
A matrix is a \key{square matrix} if it
has the same number of rows and columns.
For example, the following matrix is a
square matrix:
2016-12-28 23:54:51 +01:00
\[
S =
\begin{bmatrix}
3 & 12 & 4 \\
5 & 9 & 15 \\
0 & 2 & 4 \\
\end{bmatrix}
\]
2017-01-14 16:35:27 +01:00
\section{Operations}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
The sum $A+B$ of matrices $A$ and $B$
is defined if the matrices are of the same size.
The result is a matrix where each element
is the sum of the corresponding elements
2017-02-10 23:21:04 +01:00
in $A$ and $B$.
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
For example,
2016-12-28 23:54:51 +01:00
\[
\begin{bmatrix}
6 & 1 & 4 \\
3 & 9 & 2 \\
\end{bmatrix}
+
\begin{bmatrix}
4 & 9 & 3 \\
8 & 1 & 3 \\
\end{bmatrix}
=
\begin{bmatrix}
6+4 & 1+9 & 4+3 \\
3+8 & 9+1 & 2+3 \\
\end{bmatrix}
=
\begin{bmatrix}
10 & 10 & 7 \\
11 & 10 & 5 \\
\end{bmatrix}.
\]
2017-01-14 16:35:27 +01:00
Multiplying a matrix $A$ by a value $x$ means
2017-02-10 23:21:04 +01:00
that each element of $A$ is multiplied by $x$.
2017-01-14 16:35:27 +01:00
For example,
2016-12-28 23:54:51 +01:00
\[
2 \cdot \begin{bmatrix}
6 & 1 & 4 \\
3 & 9 & 2 \\
\end{bmatrix}
=
\begin{bmatrix}
2 \cdot 6 & 2\cdot1 & 2\cdot4 \\
2\cdot3 & 2\cdot9 & 2\cdot2 \\
\end{bmatrix}
=
\begin{bmatrix}
12 & 2 & 8 \\
6 & 18 & 4 \\
\end{bmatrix}.
\]
2017-01-14 16:35:27 +01:00
\subsubsection{Matrix multiplication}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
\index{matrix multiplication}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
The product $AB$ of matrices $A$ and $B$
is defined if $A$ is of size $a \times n$
and $B$ is of size $n \times b$, i.e.,
the width of $A$ equals the height of $B$.
The result is a matrix of size $a \times b$
whose elements are calculated using the formula
2016-12-28 23:54:51 +01:00
\[
AB[i,j] = \sum_{k=1}^n A[i,k] \cdot B[k,j].
\]
2017-01-14 16:35:27 +01:00
The idea is that each element in $AB$
is a sum of products of elements in $A$ and $B$
according to the following picture:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.5]
\draw (0,0) grid (4,3);
\draw (5,0) grid (10,3);
\draw (5,4) grid (10,8);
\node at (2,-1) {$A$};
\node at (7.5,-1) {$AB$};
\node at (11,6) {$B$};
\draw[thick,->,red,line width=2pt] (0,1.5) -- (4,1.5);
\draw[thick,->,red,line width=2pt] (6.5,8) -- (6.5,4);
\draw[thick,red,line width=2pt] (6.5,1.5) circle (0.4);
\end{tikzpicture}
\end{center}
2017-01-14 16:35:27 +01:00
For example,
2016-12-28 23:54:51 +01:00
\[
\begin{bmatrix}
1 & 4 \\
3 & 9 \\
8 & 6 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
1 & 6 \\
2 & 9 \\
\end{bmatrix}
=
\begin{bmatrix}
1 \cdot 1 + 4 \cdot 2 & 1 \cdot 6 + 4 \cdot 9 \\
3 \cdot 1 + 9 \cdot 2 & 3 \cdot 6 + 9 \cdot 9 \\
8 \cdot 1 + 6 \cdot 2 & 8 \cdot 6 + 6 \cdot 9 \\
\end{bmatrix}
=
\begin{bmatrix}
9 & 42 \\
21 & 99 \\
20 & 102 \\
\end{bmatrix}.
\]
2017-02-10 23:21:04 +01:00
Matrix multiplication is associative,
so $A(BC)=(AB)C$ holds,
but it is not commutative,
2017-02-18 15:33:08 +01:00
so $AB = BA$ does not usually hold.
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
\index{identity matrix}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
An \key{identity matrix} is a square matrix
2017-02-10 23:21:04 +01:00
where each element on the diagonal is 1
2017-01-14 16:35:27 +01:00
and all other elements are 0.
2017-02-10 23:21:04 +01:00
For example, the following matrix
is the $3 \times 3$ identity matrix:
2016-12-28 23:54:51 +01:00
\[
I = \begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
\end{bmatrix}
\]
\begin{samepage}
2017-01-14 16:35:27 +01:00
Multiplying a matrix by an identity matrix
2017-02-10 23:21:04 +01:00
does not change it. For example,
2016-12-28 23:54:51 +01:00
\[
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
1 & 4 \\
3 & 9 \\
8 & 6 \\
\end{bmatrix}
=
\begin{bmatrix}
1 & 4 \\
3 & 9 \\
8 & 6 \\
2017-02-10 23:21:04 +01:00
\end{bmatrix} \hspace{10px} \textrm{and} \hspace{10px}
2016-12-28 23:54:51 +01:00
\begin{bmatrix}
1 & 4 \\
3 & 9 \\
8 & 6 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
1 & 0 \\
0 & 1 \\
\end{bmatrix}
=
\begin{bmatrix}
1 & 4 \\
3 & 9 \\
8 & 6 \\
\end{bmatrix}.
\]
\end{samepage}
2017-01-14 16:35:27 +01:00
Using a straightforward algorithm,
we can calculate the product of
two $n \times n$ matrices
in $O(n^3)$ time.
There are also more efficient algorithms
2017-02-21 00:17:36 +01:00
for matrix multiplication\footnote{The first such
algorithm, with time complexity $O(n^{2.80735})$,
was published in 1969 \cite{str69}, and
the best current algorithm
works in $O(n^{2.37286})$ time \cite{gal14}.},
but they are mostly of theoretical interest
and such special algorithms are not needed
2017-01-14 16:35:27 +01:00
in competitive programming.
2016-12-28 23:54:51 +01:00
2017-02-21 00:17:36 +01:00
2017-01-14 16:35:27 +01:00
\subsubsection{Matrix power}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
\index{matrix power}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
The power $A^k$ of a matrix $A$ is defined
if $A$ is a square matrix.
The definition is based on matrix multiplication:
\[ A^k = \underbrace{A \cdot A \cdot A \cdots A}_{\textrm{$k$ times}} \]
For example,
2016-12-28 23:54:51 +01:00
\[
\begin{bmatrix}
2 & 5 \\
1 & 4 \\
\end{bmatrix}^3 =
\begin{bmatrix}
2 & 5 \\
1 & 4 \\
\end{bmatrix} \cdot
\begin{bmatrix}
2 & 5 \\
1 & 4 \\
\end{bmatrix} \cdot
\begin{bmatrix}
2 & 5 \\
1 & 4 \\
\end{bmatrix} =
\begin{bmatrix}
48 & 165 \\
33 & 114 \\
\end{bmatrix}.
\]
2017-01-14 16:35:27 +01:00
In addition, $A^0$ is an identity matrix. For example,
2016-12-28 23:54:51 +01:00
\[
\begin{bmatrix}
2 & 5 \\
1 & 4 \\
\end{bmatrix}^0 =
\begin{bmatrix}
1 & 0 \\
0 & 1 \\
\end{bmatrix}.
\]
2017-01-14 16:35:27 +01:00
The matrix $A^k$ can be efficiently calculated
in $O(n^3 \log k)$ time using the
algorithm in Chapter 21.2. For example,
2016-12-28 23:54:51 +01:00
\[
\begin{bmatrix}
2 & 5 \\
1 & 4 \\
\end{bmatrix}^8 =
\begin{bmatrix}
2 & 5 \\
1 & 4 \\
\end{bmatrix}^4 \cdot
\begin{bmatrix}
2 & 5 \\
1 & 4 \\
\end{bmatrix}^4.
\]
2017-01-14 16:35:27 +01:00
\subsubsection{Determinant}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
\index{determinant}
2016-12-28 23:54:51 +01:00
2017-01-14 16:35:27 +01:00
The \key{determinant} $\det(A)$ of a matrix $A$
is defined if $A$ is a square matrix.
If $A$ is of size $1 \times 1$,
then $\det(A)=A[1,1]$.
The determinant of a larger matrix is
calculated recursively using the formula \index{cofactor}
2016-12-28 23:54:51 +01:00
\[\det(A)=\sum_{j=1}^n A[1,j] C[1,j],\]
2017-01-14 16:35:27 +01:00
where $C[i,j]$ is the \key{cofactor} of $A$
at $[i,j]$.
The cofactor is calculated using the formula
2016-12-28 23:54:51 +01:00
\[C[i,j] = (-1)^{i+j} \det(M[i,j]),\]
2017-02-10 23:21:04 +01:00
where $M[i,j]$ is obtained by removing
row $i$ and column $j$ from $A$.
2017-02-10 23:24:04 +01:00
Due to the coefficient $(-1)^{i+j}$ in the cofactor,
2017-01-14 16:35:27 +01:00
every other determinant is positive
and negative.
For example,
2016-12-28 23:54:51 +01:00
\[
\det(
\begin{bmatrix}
3 & 4 \\
1 & 6 \\
\end{bmatrix}
) = 3 \cdot 6 - 4 \cdot 1 = 14
\]
2017-01-14 16:35:27 +01:00
and
2016-12-28 23:54:51 +01:00
\[
\det(
\begin{bmatrix}
2 & 4 & 3 \\
5 & 1 & 6 \\
7 & 2 & 4 \\
\end{bmatrix}
) =
2 \cdot
\det(
\begin{bmatrix}
1 & 6 \\
2 & 4 \\
\end{bmatrix}
)
-4 \cdot
\det(
\begin{bmatrix}
5 & 6 \\
7 & 4 \\
\end{bmatrix}
)
+3 \cdot
\det(
\begin{bmatrix}
5 & 1 \\
7 & 2 \\
\end{bmatrix}
) = 81.
\]
2017-01-14 16:35:27 +01:00
\index{inverse matrix}
2016-12-28 23:54:51 +01:00
2017-02-10 23:21:04 +01:00
The determinant of $A$ tells us
whether there is an \key{inverse matrix}
$A^{-1}$ such that $A \cdot A^{-1} = I$,
2017-01-14 16:35:27 +01:00
where $I$ is an identity matrix.
It turns out that $A^{-1}$ exists
exactly when $\det(A) \neq 0$,
and it can be calculated using the formula
2016-12-28 23:54:51 +01:00
\[A^{-1}[i,j] = \frac{C[j,i]}{det(A)}.\]
2017-01-14 16:35:27 +01:00
For example,
2016-12-28 23:54:51 +01:00
\[
\underbrace{
\begin{bmatrix}
2 & 4 & 3\\
5 & 1 & 6\\
7 & 2 & 4\\
\end{bmatrix}
}_{A}
\cdot
\underbrace{
\frac{1}{81}
\begin{bmatrix}
-8 & -10 & 21 \\
22 & -13 & 3 \\
3 & 24 & -18 \\
\end{bmatrix}
}_{A^{-1}}
=
\underbrace{
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
\end{bmatrix}
}_{I}.
\]
2017-01-14 16:48:57 +01:00
\section{Linear recurrences}
\index{linear recurrence}
A \key{linear recurrence}
can be represented as a function $f(n)$
2017-02-10 23:21:04 +01:00
such that the initial values are
$f(0),f(1),\ldots,f(k-1)$
and the larger values
are calculated recursively using the formula
2016-12-28 23:54:51 +01:00
\[f(n) = c_1 f(n-1) + c_2 f(n-2) + \ldots + c_k f (n-k),\]
2017-02-10 23:24:04 +01:00
where $c_1,c_2,\ldots,c_k$ are constant coefficients.
2016-12-28 23:54:51 +01:00
2017-01-14 16:48:57 +01:00
We can use dynamic programming to calculate
2017-02-10 23:21:04 +01:00
any value of $f(n)$ in $O(kn)$ time by calculating
all values of $f(0),f(1),\ldots,f(n)$ one after another.
2017-01-14 16:48:57 +01:00
However, if $k$ is small, it is possible to calculate
$f(n)$ much more efficiently in $O(k^3 \log n)$
time using matrix operations.
2016-12-28 23:54:51 +01:00
2017-01-14 16:48:57 +01:00
\subsubsection{Fibonacci numbers}
2016-12-28 23:54:51 +01:00
2017-01-14 16:48:57 +01:00
\index{Fibonacci number}
2016-12-28 23:54:51 +01:00
2017-01-14 16:48:57 +01:00
A simple example of a linear recurrence is the
2017-02-10 23:21:04 +01:00
following function that defines the Fibonacci numbers:
2016-12-28 23:54:51 +01:00
\[
\begin{array}{lcl}
f(0) & = & 0 \\
f(1) & = & 1 \\
f(n) & = & f(n-1)+f(n-2) \\
\end{array}
\]
2017-01-14 16:48:57 +01:00
In this case, $k=2$ and $c_1=c_2=1$.
2016-12-28 23:54:51 +01:00
\begin{samepage}
2017-02-10 23:21:04 +01:00
The idea is to represent the
Fibonacci formula as a
square matrix $X$ of size $2 \times 2$,
2017-01-14 16:48:57 +01:00
for which the following holds:
2016-12-28 23:54:51 +01:00
\[ X \cdot
\begin{bmatrix}
f(i) \\
f(i+1) \\
\end{bmatrix}
=
\begin{bmatrix}
f(i+1) \\
f(i+2) \\
2017-01-14 16:48:57 +01:00
\end{bmatrix}
2016-12-28 23:54:51 +01:00
\]
2017-01-14 16:48:57 +01:00
Thus, values $f(i)$ and $f(i+1)$ are given as
''input'' for $X$,
2017-02-10 23:21:04 +01:00
and $X$ calculates values $f(i+1)$ and $f(i+2)$
2017-01-14 16:48:57 +01:00
from them.
It turns out that such a matrix is
2016-12-28 23:54:51 +01:00
\[ X =
\begin{bmatrix}
0 & 1 \\
1 & 1 \\
\end{bmatrix}.
\]
\end{samepage}
\noindent
2017-01-14 16:48:57 +01:00
For example,
2016-12-28 23:54:51 +01:00
\[
\begin{bmatrix}
0 & 1 \\
1 & 1 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
f(5) \\
f(6) \\
\end{bmatrix}
=
\begin{bmatrix}
0 & 1 \\
1 & 1 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
5 \\
8 \\
\end{bmatrix}
=
\begin{bmatrix}
8 \\
13 \\
\end{bmatrix}
=
\begin{bmatrix}
f(6) \\
f(7) \\
\end{bmatrix}.
\]
2017-01-14 16:48:57 +01:00
Thus, we can calculate $f(n)$ using the formula
2016-12-28 23:54:51 +01:00
\[
\begin{bmatrix}
f(n) \\
f(n+1) \\
\end{bmatrix}
=
X^n \cdot
\begin{bmatrix}
f(0) \\
f(1) \\
\end{bmatrix}
=
\begin{bmatrix}
0 & 1 \\
1 & 1 \\
\end{bmatrix}^n
\cdot
\begin{bmatrix}
0 \\
1 \\
\end{bmatrix}.
\]
2017-02-18 15:33:08 +01:00
The value of $X^n$ can be calculated in
2017-01-14 16:48:57 +01:00
$O(k^3 \log n)$ time,
2017-02-10 23:21:04 +01:00
so the value of $f(n)$ can also be calculated
2017-01-14 16:48:57 +01:00
in $O(k^3 \log n)$ time.
2016-12-28 23:54:51 +01:00
2017-01-14 16:48:57 +01:00
\subsubsection{General case}
2016-12-28 23:54:51 +01:00
2017-02-10 23:21:04 +01:00
Let us now consider the general case where
2017-01-14 16:48:57 +01:00
$f(n)$ is any linear recurrence.
Again, our goal is to construct a matrix $X$
for which
2016-12-28 23:54:51 +01:00
\[ X \cdot
\begin{bmatrix}
f(i) \\
f(i+1) \\
\vdots \\
f(i+k-1) \\
\end{bmatrix}
=
\begin{bmatrix}
f(i+1) \\
f(i+2) \\
\vdots \\
f(i+k) \\
\end{bmatrix}.
\]
2017-01-14 16:48:57 +01:00
Such a matrix is
2016-12-28 23:54:51 +01:00
\[
X =
\begin{bmatrix}
0 & 1 & 0 & 0 & \cdots & 0 \\
0 & 0 & 1 & 0 & \cdots & 0 \\
0 & 0 & 0 & 1 & \cdots & 0 \\
\vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\
0 & 0 & 0 & 0 & \cdots & 1 \\
c_k & c_{k-1} & c_{k-2} & c_{k-3} & \cdots & c_1 \\
\end{bmatrix}.
\]
2017-01-14 16:48:57 +01:00
In the first $k-1$ rows, each element is 0
except that one element is 1.
These rows replace $f(i)$ with $f(i+1)$,
2017-02-18 15:33:08 +01:00
$f(i+1)$ with $f(i+2)$, and so on.
2017-02-10 23:24:04 +01:00
The last row contains the coefficients of the recurrence
2017-02-10 23:21:04 +01:00
to calculate the new value $f(i+k)$.
2016-12-28 23:54:51 +01:00
\begin{samepage}
2017-01-14 16:48:57 +01:00
Now, $f(n)$ can be calculated in
$O(k^3 \log n)$ time using the formula
2016-12-28 23:54:51 +01:00
\[
\begin{bmatrix}
f(n) \\
f(n+1) \\
\vdots \\
f(n+k-1) \\
\end{bmatrix}
=
X^n \cdot
\begin{bmatrix}
f(0) \\
f(1) \\
\vdots \\
f(k-1) \\
\end{bmatrix}.
\]
\end{samepage}
2017-01-14 17:04:47 +01:00
\section{Graphs and matrices}
2016-12-28 23:54:51 +01:00
2017-01-14 17:04:47 +01:00
\subsubsection{Counting paths}
2016-12-28 23:54:51 +01:00
2017-01-14 17:04:47 +01:00
The powers of an adjacency matrix of a graph
have an interesting property.
When $V$ is an adjacency matrix of an unweighted graph,
the matrix $V^n$ contains the numbers of paths of
$n$ edges between the nodes in the graph.
2016-12-28 23:54:51 +01:00
2017-01-14 17:04:47 +01:00
For example, for the graph
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.9]
\node[draw, circle] (1) at (1,3) {$1$};
\node[draw, circle] (2) at (1,1) {$4$};
\node[draw, circle] (3) at (3,3) {$2$};
\node[draw, circle] (4) at (5,3) {$3$};
\node[draw, circle] (5) at (3,1) {$5$};
\node[draw, circle] (6) at (5,1) {$6$};
\path[draw,thick,->,>=latex] (1) -- (2);
\path[draw,thick,->,>=latex] (2) -- (3);
\path[draw,thick,->,>=latex] (3) -- (1);
\path[draw,thick,->,>=latex] (4) -- (3);
\path[draw,thick,->,>=latex] (3) -- (5);
\path[draw,thick,->,>=latex] (3) -- (6);
\path[draw,thick,->,>=latex] (6) -- (4);
\path[draw,thick,->,>=latex] (6) -- (5);
\end{tikzpicture}
\end{center}
2017-01-14 17:04:47 +01:00
the adjacency matrix is
2016-12-28 23:54:51 +01:00
\[
V= \begin{bmatrix}
0 & 0 & 0 & 1 & 0 & 0 \\
1 & 0 & 0 & 0 & 1 & 1 \\
0 & 1 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 1 & 0 \\
\end{bmatrix}.
\]
2017-01-14 17:04:47 +01:00
Now, for example, the matrix
2016-12-28 23:54:51 +01:00
\[
V^4= \begin{bmatrix}
0 & 0 & 1 & 1 & 1 & 0 \\
2 & 0 & 0 & 0 & 2 & 2 \\
0 & 2 & 0 & 0 & 0 & 0 \\
0 & 2 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 1 & 1 & 0 \\
\end{bmatrix}
\]
2017-01-14 17:04:47 +01:00
contains the numbers of paths of 4 edges
between the nodes.
For example, $V^4[2,5]=2$,
because there are two paths of 4 edges
from node 2 to node 5:
2016-12-28 23:54:51 +01:00
$2 \rightarrow 1 \rightarrow 4 \rightarrow 2 \rightarrow 5$
2017-01-14 17:04:47 +01:00
and
2016-12-28 23:54:51 +01:00
$2 \rightarrow 6 \rightarrow 3 \rightarrow 2 \rightarrow 5$.
2017-01-14 17:04:47 +01:00
\subsubsection{Shortest paths}
2016-12-28 23:54:51 +01:00
2017-01-14 17:04:47 +01:00
Using a similar idea in a weighted graph,
we can calculate for each pair of nodes the shortest
path between them that contains exactly $n$ edges.
To calculate this, we have to define matrix multiplication
2017-02-10 23:21:04 +01:00
in a new way, so that we do not calculate the numbers
of paths but minimize the lengths of paths.
2016-12-28 23:54:51 +01:00
\begin{samepage}
2017-01-14 17:04:47 +01:00
As an example, consider the following graph:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.9]
\node[draw, circle] (1) at (1,3) {$1$};
\node[draw, circle] (2) at (1,1) {$4$};
\node[draw, circle] (3) at (3,3) {$2$};
\node[draw, circle] (4) at (5,3) {$3$};
\node[draw, circle] (5) at (3,1) {$5$};
\node[draw, circle] (6) at (5,1) {$6$};
\path[draw,thick,->,>=latex] (1) -- node[font=\small,label=left:4] {} (2);
\path[draw,thick,->,>=latex] (2) -- node[font=\small,label=left:1] {} (3);
\path[draw,thick,->,>=latex] (3) -- node[font=\small,label=north:2] {} (1);
\path[draw,thick,->,>=latex] (4) -- node[font=\small,label=north:4] {} (3);
\path[draw,thick,->,>=latex] (3) -- node[font=\small,label=left:1] {} (5);
\path[draw,thick,->,>=latex] (3) -- node[font=\small,label=left:2] {} (6);
\path[draw,thick,->,>=latex] (6) -- node[font=\small,label=right:3] {} (4);
\path[draw,thick,->,>=latex] (6) -- node[font=\small,label=below:2] {} (5);
\end{tikzpicture}
\end{center}
\end{samepage}
2017-02-10 23:21:04 +01:00
Let us construct an adjacency matrix where
$\infty$ means that an edge does not exist,
2017-01-14 17:04:47 +01:00
and other values correspond to edge weights.
The matrix is
2016-12-28 23:54:51 +01:00
\[
V= \begin{bmatrix}
\infty & \infty & \infty & 4 & \infty & \infty \\
2 & \infty & \infty & \infty & 1 & 2 \\
\infty & 4 & \infty & \infty & \infty & \infty \\
\infty & 1 & \infty & \infty & \infty & \infty \\
\infty & \infty & \infty & \infty & \infty & \infty \\
\infty & \infty & 3 & \infty & 2 & \infty \\
\end{bmatrix}.
\]
2017-01-14 17:04:47 +01:00
Instead of the formula
2016-12-28 23:54:51 +01:00
\[
AB[i,j] = \sum_{k=1}^n A[i,k] \cdot B[k,j]
\]
2017-01-14 17:04:47 +01:00
we now use the formula
2016-12-28 23:54:51 +01:00
\[
2017-02-10 23:21:04 +01:00
AB[i,j] = \min_{k=1}^n A[i,k] + B[k,j]
2016-12-28 23:54:51 +01:00
\]
2017-01-14 17:04:47 +01:00
for matrix multiplication, so we calculate
a minimum instead of a sum,
and a sum of elements instead of a product.
After this modification,
2017-02-10 23:21:04 +01:00
matrix powers correspond to
shortest paths in the graph.
2016-12-28 23:54:51 +01:00
2017-02-10 23:21:04 +01:00
For example, as
2016-12-28 23:54:51 +01:00
\[
V^4= \begin{bmatrix}
\infty & \infty & 10 & 11 & 9 & \infty \\
9 & \infty & \infty & \infty & 8 & 9 \\
\infty & 11 & \infty & \infty & \infty & \infty \\
\infty & 8 & \infty & \infty & \infty & \infty \\
\infty & \infty & \infty & \infty & \infty & \infty \\
\infty & \infty & 12 & 13 & 11 & \infty \\
2017-02-10 23:21:04 +01:00
\end{bmatrix},
2016-12-28 23:54:51 +01:00
\]
2017-02-10 23:21:04 +01:00
we can conclude that the shortest path of 4 edges
2017-01-14 17:04:47 +01:00
from node 2 to node 5 has length 8.
This path is
$2 \rightarrow 1 \rightarrow 4 \rightarrow 2 \rightarrow 5$.
2016-12-28 23:54:51 +01:00
2017-01-14 17:04:47 +01:00
\subsubsection{Kirchhoff's theorem}
2016-12-28 23:54:51 +01:00
2017-01-14 17:04:47 +01:00
\index{Kirchhoff's theorem}
\index{spanning tree}
2016-12-28 23:54:51 +01:00
2017-02-25 20:59:02 +01:00
\key{Kirchhoff's theorem}\footnote{G. R. Kirchhoff (1824--1887) was
a German physicist.} provides a way
2017-01-14 17:04:47 +01:00
to calculate the number of spanning trees
2017-02-10 23:21:04 +01:00
of a graph as a determinant of a special matrix.
2017-01-14 17:04:47 +01:00
For example, the graph
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.9]
\node[draw, circle] (1) at (1,3) {$1$};
\node[draw, circle] (2) at (3,3) {$2$};
\node[draw, circle] (3) at (1,1) {$3$};
\node[draw, circle] (4) at (3,1) {$4$};
\path[draw,thick,-] (1) -- (2);
\path[draw,thick,-] (1) -- (3);
\path[draw,thick,-] (3) -- (4);
\path[draw,thick,-] (1) -- (4);
\end{tikzpicture}
\end{center}
2017-01-14 17:04:47 +01:00
has three spanning trees:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.9]
\node[draw, circle] (1a) at (1,3) {$1$};
\node[draw, circle] (2a) at (3,3) {$2$};
\node[draw, circle] (3a) at (1,1) {$3$};
\node[draw, circle] (4a) at (3,1) {$4$};
\path[draw,thick,-] (1a) -- (2a);
%\path[draw,thick,-] (1a) -- (3a);
\path[draw,thick,-] (3a) -- (4a);
\path[draw,thick,-] (1a) -- (4a);
\node[draw, circle] (1b) at (1+4,3) {$1$};
\node[draw, circle] (2b) at (3+4,3) {$2$};
\node[draw, circle] (3b) at (1+4,1) {$3$};
\node[draw, circle] (4b) at (3+4,1) {$4$};
\path[draw,thick,-] (1b) -- (2b);
\path[draw,thick,-] (1b) -- (3b);
%\path[draw,thick,-] (3b) -- (4b);
\path[draw,thick,-] (1b) -- (4b);
\node[draw, circle] (1c) at (1+8,3) {$1$};
\node[draw, circle] (2c) at (3+8,3) {$2$};
\node[draw, circle] (3c) at (1+8,1) {$3$};
\node[draw, circle] (4c) at (3+8,1) {$4$};
\path[draw,thick,-] (1c) -- (2c);
\path[draw,thick,-] (1c) -- (3c);
\path[draw,thick,-] (3c) -- (4c);
%\path[draw,thick,-] (1c) -- (4c);
\end{tikzpicture}
\end{center}
2017-01-14 17:04:47 +01:00
\index{Laplacean matrix}
2017-02-10 23:21:04 +01:00
To calculate the number of spanning trees,
we construct a \key{Laplacean matrix} $L$,
2017-01-14 17:04:47 +01:00
where $L[i,i]$ is the degree of node $i$
and $L[i,j]=-1$ if there is an edge between
nodes $i$ and $j$, and otherwise $L[i,j]=0$.
2017-02-10 23:21:04 +01:00
The Laplacean matrix for the above graph is as follows:
2016-12-28 23:54:51 +01:00
\[
L= \begin{bmatrix}
3 & -1 & -1 & -1 \\
-1 & 1 & 0 & 0 \\
-1 & 0 & 2 & -1 \\
-1 & 0 & -1 & 2 \\
2017-01-14 17:04:47 +01:00
\end{bmatrix}
2016-12-28 23:54:51 +01:00
\]
2017-02-10 23:21:04 +01:00
The number of spanning trees equals
2017-01-14 17:04:47 +01:00
the determinant of a matrix that is obtained
when we remove any row and any column from $L$.
For example, if we remove the first row
and column, the result is
2016-12-28 23:54:51 +01:00
\[ \det(
\begin{bmatrix}
1 & 0 & 0 \\
0 & 2 & -1 \\
0 & -1 & 2 \\
\end{bmatrix}
) =3.\]
2017-01-14 17:04:47 +01:00
The determinant is always the same,
regardless of which row and column we remove from $L$.
2016-12-28 23:54:51 +01:00
2017-01-14 17:04:47 +01:00
Note that a special case of Kirchhoff's theorem
is Cayley's formula in Chapter 22.5,
because in a complete graph of $n$ nodes
2016-12-28 23:54:51 +01:00
\[ \det(
\begin{bmatrix}
n-1 & -1 & \cdots & -1 \\
-1 & n-1 & \cdots & -1 \\
\vdots & \vdots & \ddots & \vdots \\
-1 & -1 & \cdots & n-1 \\
\end{bmatrix}
) =n^{n-2}.\]