\documentclass[10pt]{article}

\usepackage{systeme}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
%\usepackage{palatino}
\usepackage{setdeck}

\usepackage{xcolor}
\usepackage{tcolorbox}

%\usepackage[textheight=8.5in, margin=1.6in]{geometry}
\usepackage[total={6in,8in}]{geometry}

\usepackage{tikz}
\usetikzlibrary{cd}
\tikzset{
      labr/.style={anchor=north, rotate=90, inner sep=.7mm}
    }
\tikzset{
      labl/.style={anchor=south, rotate=90, inner sep=.7mm}
    }

%\usepackage{pgfplots}
\tikzstyle{ball} = [circle,shading=ball, ball color=black,
    minimum size=1mm,inner sep=1.3pt]
%\usetikzlibrary{arrows.meta}
%\tikzset{>={Latex[length=2mm,width=1.5mm]}}

%\usepackage{graphicx}

\usepackage{needspace}  % <-- to prevent bad pagebreak over environment titles
% Use:  \Needspace{3\baselineskip} before opening an environment

\usepackage[shortlabels]{enumitem}
\setlist[enumerate]{leftmargin=*,label=(\alph*)}

%\usepackage[colorlinks=true,citecolor=black,linkcolor=black,urlcolor=blue]{hyperref}

\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\sn}{\mathfrak{S}}
\newcommand{\PP}{\mathbb{P}}
\newcommand{\ve}{\varepsilon}
\newcommand{\ips}[2]{\langle{#1}, {#2}\rangle}
\DeclareMathOperator{\tr}{tr}
\DeclareMathOperator{\Span}{\mathrm{Span}}
\DeclareMathOperator{\rk}{\mathrm{rank}}
\DeclareMathOperator{\rspace}{\mathrm{rowspace}}
\DeclareMathOperator{\cspace}{\mathrm{colspace}}
\DeclareMathOperator{\trace}{\mathrm{tr}}
\DeclareMathOperator{\im}{\mathrm{im}}
\DeclareMathOperator{\Sym}{\mathrm{Sym}}
\DeclareMathOperator{\Hom}{\mathrm{Hom}}
\DeclareMathOperator{\diag}{\mathrm{diag}}
\DeclareMathOperator{\glb}{\mathrm{glb}}
\DeclareMathOperator{\lub}{\mathrm{lub}}

\parskip = 0.05in
\parindent = 0.0in

%\pagestyle{empty}

\allowdisplaybreaks

\begin{document}
\centerline{Math 201 lecture for Monday, Week 11}
\bigskip

\centerline{\textbf{Walks on graphs}}  
\bigskip
 
We have devoted a lot of energy to the problem of diagonalizing a matrix.  
One major motivation for diagonalization is that it makes taking
powers of a matrix easier.  Explicitly, suppose that~$A\in M_{n\times n}(F)$ is
diagonalizable.  So there exists a matrix~$P$ such that
\[
  P^{-1}AP=D=\diag(\lambda_1,\dots,\lambda_n).
\]
It is easy to take powers of a diagonal
matrix:~$D^{\ell}=\diag(\lambda_1^{\ell},\dots,\lambda_n^{\ell})$.  Here is the
important trick:
\begin{align*}
  D^{\ell}
  &=(P^{-1}AP)^{\ell}\\[5pt]
  &= (P^{-1}AP)(P^{-1}AP)(P^{-1}AP)\cdots(P^{-1}AP)(P^{-1}AP)\\[5pt]
  &= P^{-1}A(PP^{-1})A(PP^{-1})A(PP^{-1})\cdots(PP^{-1})AP\\[5pt]
  &=P^{-1}A^{\ell}P.
\end{align*}
Therefore,
\[
  A^{\ell}=PD^{\ell}P^{-1}=P\diag(\lambda_1^{\ell},\dots,\lambda_n^{\ell})P^{-1}.
\]
In general, there will be many fewer arithmetic operations required on the
right-hand side of this equation than on the left-hand side.

This lecture will consider one application of this idea.
\medskip

\textbf{Walks in graph.} A graph (or network) consists of vertices connected by
edges.  Here is an example with~$4$ vertices connected by~$5$ edges:
\begin{center}
  \begin{tikzpicture}[scale=0.8]
    \node[ball,label={left:$v_4$}] (4) at (-1,0) {};
    \node[ball,label={right:$v_2$}] (2) at (1,0) {};
    \node[ball,label={above:$v_3$}] (3) at (0,1.732) {};
    \node[ball,label={below:$v_1$}] (1) at (0,-1.732) {};
    \draw (1)--(2)--(3)--(4)--(1);
    \draw (2)--(4);
    \node at (0,-3) {The diamond graph.};
  \end{tikzpicture}
\end{center}
  A \emph{walk of length~$\ell$} in a graph is a sequence of
  vertices~$u_0u_1\dots u_{\ell}$ where~$u_{i-1}$ is connected to~$u_i$ for
  $i=1,\dots,\ell$.  So the length is the number of edges traversed.  In our example, the following are walks from~$v_1$
  to~$v_4$:
  \[
    v_1v_4\quad\text{and}\quad v_1v_2v_3v_4.
  \]
  The first has length~$1$ and the second has length~$3$.  We are interested in
  counting the number of closed walks between vertices.
  \medskip

  \textbf{Definition.} Let~$G$ be a graph with vertices~$v_1,\dots,v_n$.
  The \emph{adjacency matrix} of~$G$ is the~$n\times n$ matrix~$A=A(G)$
  defined by
  \[
    A_{ij} = 
    \begin{cases}
      1&\text{if there is an edge connecting~$v_i$ and~$v_j$}\\
      0&\text{otherwise.}
    \end{cases}
  \]
  \medskip
   
  For example, the adjacency matrix of the diamond graph is
  \begin{center}
    \begin{tikzpicture}
      \node at (0,0) {$
	\left(\begin{array}{cccc}
	    0&1&0&1\\
	    1&0&1&1\\
	    0&1&0&1\\
	    1&1&1&0
	\end{array} \right).$
	};
	\node at (-0.85,1.3) {$v_1$};
	\node at (-0.3,1.3) {$v_2$};
	\node at (0.2,1.3) {$v_3$};
	\node at (0.8,1.3) {$v_4$};
	\node at (-1.7,0.75) {$v_1$};
	\node at (-1.7,0.25) {$v_2$};
	\node at (-1.7,-0.25) {$v_3$};
	\node at (-1.7,-0.75) {$v_4$};
	\node at (-2.5,0) {$A=$};
	\begin{scope}[xshift=-2.2in,yshift=0.0in,scale=0.4]
	    \node[ball,label={left:$v_4$}] (4) at (-1,0) {};
	    \node[ball,label={right:$v_2$}] (2) at (1,0) {};
	    \node[ball,label={above:$v_3$}] (3) at (0,1.732) {};
	    \node[ball,label={below:$v_1$}] (1) at (0,-1.732) {};
	    \draw (1)--(2)--(3)--(4)--(1);
	    \draw (2)--(4);
	\end{scope}
    \end{tikzpicture}
  \end{center}
  \medskip

  \textbf{Theorem.} Let~$A$ be the adjacency matrix for a graph~$G$ with
  vertices~$v_1,\dots,v_n$, and let~$\ell\in\Z\geq0$.  Then then number of walks
  of length~$\ell$ from~$v_i$ to~$v_j$ is~$(A^{\ell})_{ij}$.
  \begin{proof}
    Homework.
  \end{proof}

  \textbf{Example.} Consider the diamond graph and its adjacency matrix~$A$,
  displayed above.  Then 
  \begin{center}
    \begin{tikzpicture}
      
      \node at (0,0) {$\displaystyle
    A^{0}=I_4,\quad
    A=
    \left(\begin{array}{rrrr}
	0 & 1 & 0 & 1 \\
	1 & 0 & 1 & 1 \\
	0 & 1 & 0 & 1 \\
	1 & 1 & 1 & 0
    \end{array}\right),\quad
 A^2=
 \left(\begin{array}{rrrr}
     2 & 1 & 2 & 1 \\
     1 & 3 & 1 & 2 \\
     2 & 1 & 2 & 1 \\
     1 & 2 & 1 & 3
 \end{array}\right),\quad
 A^3= 
 \left(\begin{array}{rrrr}
     2 & 5 & 2 & 5 \\
     5 & 4 & 5 & 5 \\
     2 & 5 & 2 & 5 \\
     5 & 5 & 5 & 4
 \end{array}\right).$};
 \fill[color=blue!40,opacity=0.3] (1.28,0.05) rectangle (1.57,0.45);
 \fill[color=blue!40,opacity=0.3] (4.85,0.05) rectangle (5.3,0.45);
    \end{tikzpicture}
  \end{center}
  The highlighted entries in the matrix say there is~$1$ path of
  length~$2$ from~$v_2$ to~$v_3$ and there are~$4$ paths of length~$3$
  from~$v_2$ to itself.  Can you find them?  (The answer appears at the end of
    this lecture.)
\medskip

So to count the number of walks, we need to compute powers of the adjacency
matrix.  Here is some good news:
\medskip

\textbf{Theorem.} If~$A$ is an~$n\times n$ symmetric matrix ($A=A^t$) over the
real numbers, then it is diagonalizable over~$\R$.  

\begin{proof}
  We may prove this later in the semester.  (To look it up online,
    search for the ``spectral theorem'', which is usually stated for the more
    general class of Hermitian matrices.  Over the real numbers, the Hermitian
    matrices are exactly the symmetric matrices.)
\end{proof}


This means that we can find a matrix~$P$ such that~$P^{-1}AP=D$, where~$D$ is
the diagonal matrix of the eigenvalues.  Then~$A^{\ell}=PD^{\ell}P^{-1}$.  So we
can find a nice closed form for the number of walks of length~$\ell$ between any
two vertices as a linear expression in the~$\ell$-th powers of the eigenvalues
of~$A$. If the eigenvalues are~$\lambda_1,\dots,\lambda_n$, the
equation~$A^{\ell}=PD^{\ell}P^{-1}$ immediately implies that for each pair of
vertices~$v_i$ and~$v_j$ there exist
real numbers~$c_1,\ldots,c_{n}$, independent of~$\ell$, such that the number of closed walks
of length~$\ell$ from~$v_i$ to~$v_j$ is
\[
  c_1\lambda_1^{\ell}+\cdots+c_n\lambda_n^{\ell}.  
\]
The special case of closed walks is particularly nice.
\medskip

\textbf{Definition.} 
A walk is \emph{closed} if it begins and ends at the same vertex.
\medskip

\textbf{Definition.} Let~$A$ be any~$n\times n$ matrix.  Then the
\emph{trace} of~$A$ is the sum of its diagonal entries:
\[
  \trace(A)=\sum_{i=1}^nA_{ii}.
\]
\medskip

\textbf{Proposition.} Let~$A$ be the adjacency matrix of a graph~$G$.  Then the
number of closed walks in~$G$ of length~$\ell$ is~$\trace(A^{\ell})$.

\begin{proof}
  For each~$i=1,\dots,n$, the number of closed walks from~$v_i$
  to~$v_i$ is~$(A^{\ell})_{ii}$.  Summing over~$i$ gives the total number of
  closed walks.
\end{proof}

\textbf{Proposition.} Let~$A$ be any~$n\times n$ matrix.  Then the trace
of~$A$ is the sum of its eigenvalues, each counted according to its (algebraic)
multiplicity.

\begin{proof}
  Homework.
\end{proof}

\textbf{Corollary.} Let~$A$ be the adjacency matrix of a graph~$G$ with~$n$
vertices, and let~$\lambda_1,\dots,\lambda_n\in\R$ be its list of (not
necessarily distinct) eigenvalues.  Then the number of closed walks in~$G$ of
length~$\ell$ is~$\sum_{i=1}^n\lambda_i^{\ell}$. 

\begin{proof}
  The number of closed walks of length~$\ell$ is~$\tr(A^{\ell})$,
    which is the sum of the eigenvalues of~$A^{\ell}$.  By homework (an easy
    induction argument), if~$\lambda$ is an eigenvalue of~$A$,
    then~$\lambda^{\ell}$ is an eigenvalue of~$A^{\ell}$ with unchanged eigenspace.
    It follows that the eigenvalues for~$A^{\ell}$
    are~$\lambda_1^{\ell},\dots,\lambda_n^{\ell}$.  
\end{proof}

\textbf{Example.} Let~$A$ be the adjacency matrix of the diamond
graph~$G$.  The characteristic polynomial of~$A$ is
\[
  \det(A-xI_4)=x^4 - 5x^2 - 4x=x(x+1)(x^2-x-4).
\]
Using the quadratic equation, we find the eigenvalues for~$A$:
\[
  0,-1,\frac{1+\sqrt{17}}{2},\frac{1-\sqrt{17}}{2}.
\]
Therefore, the number of closed walks in~$G$ of length~$\ell$ is
\[
  w(\ell)=(0)^{\ell}+(-1)^{\ell}+\left(\frac{1+\sqrt{17}}{2}\right)^{\ell}+\left(\frac{1-\sqrt{17}}{2}\right)^{\ell},
\]
where
\[
  (0)^{\ell}=
  \begin{cases}
    1&\text{if~$\ell=0$}\\
    0&\text{if~$\ell>0$}.
  \end{cases}
\]
The following table gives the number of closed walks for~$\ell=0,1,\dots,6$:
\[
  \begin{array}{c|ccccccc}
    \ell&0&1&2&3&4&5&6\\\hline
    w(\ell)&4&0&10&12&50&100&298
\end{array}
\]

\textbf{Exercise.}  The complete graph, $K_n$, has vertices~$1,\ldots,n$ and an
edge between every pair of vertices.  How many closed walks are there in $K_n$
of lenght~$\ell$?
\medskip

\textbf{Questions.}
\begin{enumerate}
  \item How would you generalize today's ideas to the case of a directed graph
    (in which the edges have directions)?
  \item How would you generalize today's ideas to the case in which the edges
    have weights?  (A special case would be to let the weight of edge $(u,v)$ be
    the probability that the edge is traversed given that the starting point
    is~$u$.  Another possibility is to think of the weight as a cost for
    traveling across the edge.)
\end{enumerate}
\bigskip


\textbf{Answer to example on page 2}: $v_2v_4v_3$ has length~$2$ and the following have length~$3$: $v_2v_3v_4v_2$, $v_2v_4v_3v_2$, $v_2v_1v_4v_2$, and $v_2v_4v_1v_2$.

\end{document}  
