\documentclass{article}

\usepackage[UKenglish]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[all]{xy}

\usepackage{url}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}

\usepackage{enumerate}
\usepackage{url}
\usepackage{algorithmic}
\usepackage{algorithm}

\include{macros}

\usepackage{natbib}
\setlength{\bibsep}{1.75pt}
\usepackage{array}

\title{Cyclic factorization of complete graphs into spanning trees with an Euler trail}
\author{Alex Horn}
\date{}

\begin{document}
\maketitle

\begin{abstract}
A simple graph algorithm is presented which decomposes an $n$-complete graph of even order into edge-disjoint, isomorphic copies of a spanning tree whose edges form an Euler trail of length $n-1$. While more general methods of such spanning tree factorizations have been known, we emphasize here its simple algorithmic treatment with an efficient data structure.
\end{abstract}

\section{Introduction}

This document is written mainly for fun and it concerns a graph decomposition algorithm which computes the cyclic spanning tree factorization of complete graphs of even order. This algorithm could find application in ad hoc network designs in which link failures need to be restored quickly by creating a new isomorphic yet link-disjoint network structure.

We consider only simple and finite graphs. Let $G$ be such a graph. A \define{decomposition} of $G$ is a set of pairwise edge-disjoint subgraphs $H_1, H_2, \ldots H_k$ such that every edge of $G$ belongs to exactly one subgraph $H_i$ for $1 \leq i \leq k$. If every such subgraph is isomorphic to a graph $H$, then $G$ is said to have an \define{$H$-decomposition}. An $H$-decomposition into $H_1, H_2, \ldots H_k$ is called \define{cyclic} if there exists an ordering $(x_1, x_2, \ldots, x_n)$ of the vertices of $G$ together with graph isomorphisms $\phi_i : H_1 \to H_i$, for all $1 < i \leq k$, such that $\phi_i(x_j) = x_{(i+j) \bmod n}$ for all $1 \leq j \leq n$. If $H$ has the same order as $G$ and none of the vertices in $H$ are isolated, then $H$ is a connected factor and the decomposition is called an \define{$H$-factorization}. In particular, if $H$ is a spanning tree, we say that graph $G$ has a \define{spanning tree factorization}. Figure~\ref{fig:cyclic-decomposition} (p.~\pageref{fig:cyclic-decomposition}) shows cyclic decompositions of the $6$-complete graph and $8$-complete graph which can be verified by ``rotating'' the subgraphs twice. Note that only the centre and most right decompositions are spanning tree factorizations.

\begin{figure}[t]
\center{
\begin{tabular}{m{3.2cm} m{3.2cm} m{3.2cm}}
  \[\xymatrix@=0.2cm{
            & \bullet\ar@{-}[dd] && \bullet\ar@{-}[ll]\ar@{-}[ddll] &          \\
    \bullet &                    &&                                 & \bullet  \\
            & \bullet\ar@{-}[rr] && \bullet\ar@{-}[uu]              &
  }\]
&
  \[\xymatrix@=0.2cm{
                         & \bullet\ar@{-}[ddrr] && \bullet\ar@{-}[ll] &          \\
    \bullet\ar@{-}[urrr] &                      &&                    & \bullet  \\
                         & \bullet\ar@{-}[urrr] && \bullet\ar@{-}[ll] &
  }\]
&
  \[\xymatrix@=0.2cm{
                         & \bullet\ar@{-}[dddrr] && \bullet\ar@{-}[ll] &                      \\
    \bullet\ar@{-}[urrr] &                       &&                    & \bullet\ar@{-}[llll] \\
    \bullet              &                       &&                    & \bullet\ar@{-}[llll] \\
                         & \bullet\ar@{-}[urrr]  && \bullet\ar@{-}[ll] &
  }\]
\end{tabular}
}
\caption{Cyclic decomposition of $K_6$ into a subgraph whose edges form an Euler trail (left). Similarly, $K_6$ and $K_8$ have also a cyclic spanning tree factorization whose edges form an Euler trail (centre, right).}
\label{fig:cyclic-decomposition}
\end{figure}

Many real-life applications such as network designs and combinatorial problems come in the disguise of complete graph decompositions. Graph theory studies their underlying properties. For example, when is the decomposition of a complete graph into cycles of length $m$ possible? Since the existence of such a decomposition requires the degree of every vertex to be even, the complete graph must be of odd degree. Otherwise, a $1$-factor can be removed from the $2n$-complete graph. Then, an $m$-cycle decomposition exists if and only if the length of the cycle, $m$, divides the number of edges in the graph~\cite{A01},~\cite{S02a}.

In contrast, no necessary and sufficient condition is known for a tree to admit a factorization of complete graphs~\cite{K11}. Rather, most factorizations are subject to graph labeling techniques (see below). In general, a label is a function from the set of vertices into the set of natural numbers. Informally, these labels are chosen such that every edge appears in exactly one factor. In particular, Rosa-type labelings (see~\cite{E09} for a recent survey) are being used for cyclic decompositions of complete graphs into spanning trees~\cite{E97},~\cite{F02},~\cite{F04}.

In the sequel, we automate the labeling of the vertices in an Euler trail such that it admits a cyclic decomposition into spanning trees of a complete graph. In fact, cyclic spanning tree factorizations can be completely characterized in terms of their symmetrical structure~\cite{E97},~\cite{F04}. Before we can make this characterization precise, we must agree on the following definitions.

\begin{definition}\label{def:labeling}
Define a \define{labeling} of a graph $G$ with $m$ edges to be an injective function $\lambda : V(G) \to L$ where $L = \{0, 1, \ldots , 2m\}$. The \define{length} of an edge $(x,y) \in E(G)$ is defined by $\ell(x,y) \deq min\{|\lambda(x)−\lambda(y)|, 2m+1−|\lambda(x)−\lambda(y)|\}$. If $\{ \ell(x,y) \alt (x,y) \in E(G) \} = \{1, 2, \ldots, m\}$, then $\lambda$ is called a \define{$\rho$-labeling}; if the image of $\lambda$ is moreover a subset of $\{0, 1, \ldots, m\}$, then $\lambda$ is a \define{graceful labeling}. A graph which admits a graceful labeling is said to be \define{graceful}.
\end{definition}

\begin{definition}\label{def:symmetric-labeling}
A connected graph $G$ is \define{symmetric} if it has a bridge $(x,y)$ and there exists an automorphism $\phi$ such that $\phi(x) = y$ and $\phi(y) = x$. The isomorphic connected components of $G−(x,y)$ are called \define{banks} and denoted by $H$ and $H'$ respectively. A labeling of a symmetric graph $G$ with $2n−1$ edges and banks $H$ and $H'$ is \define{$\rho$-symmetric graceful} if $H$ has a $\rho$-labeling and $\lambda(\phi(x)) = \lambda(x) + n$ for each vertex $x \in V(H)$. A labeling of a symmetric graph with $2n − 1$ edges is said to be \define{symmetric graceful} if it is $\rho$-symmetric graceful and the bank H is moreover graceful.
\end{definition}

\begin{theorem}[\cite{E97},\cite{F04}]\label{theorem:cyclic-decomposition}
Let $G$ be a symmetric graph with $2n − 1$ edges. Then, $K_{2n}$ has a cyclic $G$-decomposition if and only if G is $\rho$-symmetric graceful.
\end{theorem}

In the next section, we present a polynomial time graph algorithm which constructs the symmetric graceful spanning tree of $K_{2n}$ with the additional constraint that this spanning tree must have an Euler trail. Clearly, the length of this Euler trail is bound to be odd ($2n-1$). Moreover, the spanning tree which contains the Euler trail is unique up to isomorphism.

%\begin{proposition}\label{proposition:unique-spanning-tree}
%Let $S$ be a spanning tree of $K_n$ for even $n$. If $S$ has an Euler trail, then $S$ is unique up to isomorphism.
%\begin{proof}
%We prove the uniqueness of $S$ by induction on $n$. Clearly, the $2$-complete graph has a unique spanning tree. For the induction step, suppose $K_{n+2}$ has two spanning trees $S$ and $S'$ with an Euler trail. By Euler's Theorem, $S$ must have exactly two vertices of odd degree. Denote both these vertices by $a$ and $b$. Then, $a$ and $b$ must have degree one; for otherwise, the degree of every vertex in $S$ would be at least two forcing the existence of a cycle. An identical argument applies to $S'$. Thus, there exists an isomorphism between the odd degree vertices in $S$ and $S'$. By induction hypothesis, the subtrees induced by deleting both these vertices from $S$ and $S'$ are isomorphic.
%\end{proof}
%\end{proposition}

\section{Algorithm}

Given the $2n$-complete graph, we aim at finding the spanning tree $S$ with an Euler trail such that $K_{2n}$ has a cyclic $S$-decomposition. This spanning tree factorization is computed by Algorithm~\ref{algorithm:spanning-tree} and Algorithm~\ref{algorithm:spanning-tree-decomposition} (see p.~\pageref{algorithm:spanning-tree-decomposition}). More precisely, Algorithm~\ref{algorithm:spanning-tree} computes the symmetric labeling of the Euler trail such that a cyclic spanning tree factorization exists by theorem~\eqref{theorem:cyclic-decomposition}. Then, Algorithm~\ref{algorithm:spanning-tree-decomposition} creates the cyclic permutations of this Euler trail.

\begin{algorithm}
\caption{Finds the spanning tree $S$ with an Euler trail such that $K_{2n}$ has a cyclic $S$-decomposition. The Euler trail in $S$ is formed by the edges incident to the vertex labeled $k$ and vertex labeled $trails[k]$ where $0 \leq k < 2n$. The trail can be traversed by starting at the vertex labeled $0$ and stops when $trail[k]=k$ for some  $0 \leq k < 2n$.}
\label{algorithm:spanning-tree}
\begin{algorithmic}
\REQUIRE $N$ to be even and $N \geq 2$
\REQUIRE $trail[k] = k$ for all $0 \leq k < N$

\STATE $i \leftarrow 0$
\STATE $j \leftarrow N / 2 - 1$
\STATE $x \leftarrow N - 1$
\STATE $y \leftarrow N / 2$
\LOOP
\STATE $trail[i] \leftarrow j$
\STATE $trail[x] \leftarrow y$
\STATE $i \leftarrow i + 1$
\STATE $y \leftarrow y + 1$
\IF{$i = j$}
\STATE exit loop
\ENDIF
\STATE $trail[j] \leftarrow i$
\STATE $trail[y] \leftarrow x$
\STATE $j \leftarrow j - 1$
\STATE $x \leftarrow x - 1$
\IF{$i = j$}
\STATE exit loop
\ENDIF
\ENDLOOP
\IF{$N > 2$}
\STATE $trail[i] \leftarrow y$
\ENDIF
\end{algorithmic}
\end{algorithm}

The following runtime complexity result is obvious:

\begin{proposition}\label{proposition:spanning-tree-analysis}
Algorithm~\ref{algorithm:spanning-tree} is $O(n)$ given the $2n$-complete graph.
\begin{proof}
Given $K_{2n}$, the loop iterates $n$ times plus a final constant operation.
\end{proof}
\end{proposition}

\begin{algorithm}
\caption{Computes the cyclic permutations of the symmetric spanning tree of $K_{2n}$ (see Algorithm~\ref{algorithm:spanning-tree}). For each $0 \leq s < n$, the tree traversal starts at the vertex labeled $trail[s][s]$ and stops when $trail[s][t]=t$ for some $0 \leq t < 2n$.}
\label{algorithm:spanning-tree-decomposition}
\begin{algorithmic}
\REQUIRE $N$ to be even and $N \geq 2$
\REQUIRE $trail[s][k] = k$ for all $0 \leq s < \onehalf N$ and $0 \leq k < N$

\STATE Store result of Algorithm~\ref{algorithm:spanning-tree} into $trails[0]$
\FOR{$i = 1$ to $\onehalf N - 1$}
\FOR{$j = 0$ to $N - 1$}
\STATE $v \leftarrow (trails[i - 1][j] + 1) \bmod N$
\STATE $trails[i][(j + 1) \bmod N] \leftarrow v$
\ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}

\begin{proposition}
Algorithm~\ref{algorithm:spanning-tree-decomposition} is $O(n^2)$ given the $2n$-complete graph.
\begin{proof}
By proposition~\eqref{proposition:spanning-tree-analysis}, Algorithm~\ref{algorithm:spanning-tree} is $O(n)$. Afterwards, the innermost loop executes $2n$ times. The outermost loop executes this loop $n - 1$ times counting a total of $2(n^2 - n)$ iterations. Therefore, Algorithm~\ref{algorithm:spanning-tree-decomposition} is $O(n^2)$.
\end{proof}
\end{proposition}

\begin{figure}[b!]
\centering{
\begin{displaymath}
  \xymatrix{
    *+[o][F]{0}\ar@/^2.2pc/@{-}[rrrr]  &
    *+[o][F]{1}\ar@/^1pc/@{-}[rr]      &
    *+[o][F]{2}\ar@/^2.5pc/@{-}[rrrrr] &
    *+[o][F]{3}\ar@/_0.5pc/@{-}[l]     &
    *+[o][F]{4}\ar@/_1.5pc/@{-}[lll]   &
    *+[o][F]{5}                        &
    *+[o][F]{6}\ar@/^1.5pc/@{-}[rrr]   &
    *+[o][F]{7}\ar@/^0.5pc/@{-}[r]     &
    *+[o][F]{8}\ar@/_1pc/@{-}[ll]      &
    *+[o][F]{9}\ar@/_2.2pc/@{-}[llll]
  }
\end{displaymath}

\begin{tabular}{|c||c|c|c|c|c|c|c|c|c|c|}
\hline
$v$        & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\ \hline
$trail[v]$ & 4 & 3 & 7 & 2 & 1 & 5 & 9 & 8 & 6 & 5 \\ \hline
\end{tabular}
}
\caption{The diagrammatic and key-indexed array representation of the spanning tree with an Euler trail for the $10$-complete graph. This Euler trail is formed by joining the vertices labeled as $0,4,1,3,2,7,8,6,9,5$. More generally, notice that $trail[v]$ is the label of the vertex adjacent to the vertex labeled $v$. If $trail[v] = v$, then the end of the trail has been reached. According to definition~\eqref{def:symmetric-labeling}, the spanning tree is symmetric because the subgraphs formed by the edges of the trail $0,4,1,3,2$ and $7,8,6,9,5$ are two isomorphic banks joined by the bridge between the vertices labeled $2$ and $7$. Since the first bank is graceful, we conclude that the entire spanning tree is symmetric graceful.}
\label{fig:spanning-tree}
\end{figure}

Both algorithms feature an efficient key-indexed array data structure which represents vertex labels and supports the traversal of the spanning trees.

\begin{example}
Figure~\ref{fig:spanning-tree} (p.~\pageref{fig:spanning-tree}) illustrates the spanning tree labeling produced by Algorithm~\ref{algorithm:spanning-tree} (p.~\pageref{algorithm:spanning-tree}) of the $10$-complete graph. This labeling is symmetric graceful by definition~\eqref{def:symmetric-labeling}. Therefore, by theorem~\eqref{theorem:cyclic-decomposition}, the algorithm produced a cyclic spanning tree factorization of the $10$-complete graph. Algorithm~\ref{algorithm:spanning-tree-decomposition}, in turn, creates the cyclic permutations of the trail to obtain the remaining four Euler trails of equal length which partition the edge set accordingly.
\end{example}

\section{Conclusion}

In summary, by relying on more recent graph theoretical results based on $\rho$-labelings~\cite{E97},~\cite{F04}, a simple polynomial time graph algorithm was presented that constructs a spanning tree factorization of the $2n$-complete graph such that the spanning tree has an Euler trail.

\begin{figure}[t]
\centering{
\begin{tabular}{|c||c|c|c|c|c|c|c|c|c|c|}
\hline
$v$           & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\ \hline
$trail[1][v]$ & 6 & 5 & 4 & 8 & 3 & 2 & 6 & 0 & 9 & 7 \\ \hline
$trail[2][v]$ & 8 & 7 & 6 & 5 & 9 & 4 & 3 & 7 & 1 & 0 \\ \hline
$trail[3][v]$ & 1 & 9 & 8 & 7 & 6 & 0 & 5 & 4 & 8 & 2 \\ \hline
$trail[4][v]$ & 3 & 2 & 0 & 9 & 8 & 7 & 1 & 6 & 5 & 9 \\ \hline
\end{tabular}
}
\label{fig:spanning-tree-decomposition}
\caption{Given the trail from Figure~\ref{fig:spanning-tree} (p.~\pageref{fig:spanning-tree}), Algorithm~\ref{algorithm:spanning-tree-decomposition} (p.~\pageref{algorithm:spanning-tree-decomposition}) computes the remaining four trails of length nine by applying cyclic permutations.}
\end{figure}

\bibliographystyle{plain}
\bibliography{doc}

\end{document}


\documentclass[11pt]{article}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage{graphicx}
\usepackage{amssymb, amsmath}
\usepackage{epstopdf}
\usepackage{color}
\usepackage[table]{xcolor}
\usepackage{multirow}

\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}

\title{Efficient Implementation of Automatic Differentiation}
\author{Michael Betancourt}
\date{}

\begin{document}

\maketitle

Automatic differentiation (autodiff) is a tool for automating the chain rule when
computing derivatives of complex, composite functions.  As one moves to higher
derivatives the construction of autodiff becomes significantly more difficult and
obfuscates computationally efficient implementations.

In this note I will take advantage of the \textit{dual number} perspective of autodiff
to simplify the construction and implementation of various higher-order derivations.
After discussing the basics of dual numbers and how they can be used to derive
automatic differentiation, I'll review first, second, and third-order implementations.

\section*{Dual Numbers}

Dual numbers are an extension of the real numbers, $\mathbb{R}$, given by the
addition of a new element, $\mathbf{a}$,%
%
\footnote{In this note bold face will refer to the dual units while the regular
typeface will correspond to elements of $\mathbb{R}$.  I will also reserve
roman letters for real numbers and greek letters for dual numbers.}
%
which is nilpotent, $\mathbf{a}^{2} = 0$. I will refer to this new element as a \textit{dual unit}.

This extended space of dual numbers forms a two-dimensional associative algebra
over the reals; in other words, the addition of the dual unit generates a two-dimensional
space, $\mathbb{D}$, with elements
%
\begin{equation*}
\xi = x + \mathbf{a} \, \delta x; \, \xi \in \mathbb{D}, \, x, \delta x \in \mathbb{R}
\end{equation*}
%
equipped with addition and multiplication operations,
%
\begin{align*}
\xi_{1} + \xi_{2} &= \left( x_{1} + \mathbf{a} \, \delta x_{1} \right) + \left( x_{2} + \mathbf{a} \,  \delta x_{2} \right)
\\
&= \left( x_{1} + x_{2} \right) + \mathbf{a} \left( \delta x_{1} + \delta x_{2} \right)
\\
\xi_{1} \cdot \xi_{2}
&= \left( x_{1} + \mathbf{a} \, \delta x_{1} \right) \cdot \left( x_{2} + \mathbf{a} \, \delta x_{2} \right)
\\
&= x_{1} x_{2} + \left( x_{1} \delta x_{2} + \delta x_{1} x_{2} \right) \mathbf{a} + \delta x_{1} \delta x_{2} \, \mathbf{a}^{2}
\\
&= x_{1} x_{2} + \left( x_{1} \delta x_{2} + \delta x_{1} x_{2} \right) \mathbf{a}.
\end{align*}

Note that, because the dual unit is nilpotent, we cannot define an inverse to multiplication.
Mathematically this means that the dual numbers form only a local ring; compare this to
the complex numbers on which we can define division and whose associative algebra is a field.

Dual numbers have many uses in mathematics and physics, but here we will focus on their
relationship to functions.  Any smooth function $f : \mathbb{R} \rightarrow \mathbb{R}$
induces a function on the dual numbers, $f : \mathbb{D} \rightarrow \mathbb{D}$,
via a Taylor series around any purely real point, $\xi_{0} = x_{0} + \mathbf{a} \, 0$,
%
\begin{align*}
f \! \left( \xi \right)
&=
\sum_{n = 0}^{\infty} \frac{ \left( \xi - \xi_{0} \right)^{n} }{n!}
\frac{ \partial^{n} f }{ \partial \xi^{n} } \! \left( \xi = \xi_{0} \right)
\\
&=
\sum_{n = 0}^{\infty} \frac{ \left( \left( x - x_{0} \right) + \mathbf{a} \, \delta x \right)^{n} }{n!}
\frac{ \partial^{n} f }{ \partial x^{n} } \! \left( x_{0} \right)
\\
&=
\sum_{n = 0}^{\infty} \frac{ \left( x - x_{0} \right)^{n} }{n!}
\frac{ \partial^{n} f }{ \partial x^{n} } \! \left( x_{0} \right)
+ \mathbf{a} \, \delta x \sum_{n = 0}^{\infty} \frac{ \left( x - x_{0} \right)^{n - 1} }{\left( n - 1 \right)!}
\frac{ \partial^{n} f }{ \partial x^{n} } \! \left( x_{0} \right)
\\
&=
f \! \left( x \right)
+ \mathbf{a} \, \delta x \frac{ \partial f }{ \partial x } \! \left( x \right).
\end{align*}
%
Extending a function to dual numbers requires not just the value of the function
but also its derivatives!  The second component of dual numbers formalizes
the intuitive concept of an ``infinitesimal'' in that any product of more than one
will vanish by construction.

The generalization to multivariate functions is straightforward: any function
$f : \mathbb{R}^{n} \rightarrow \mathbb{R}^{m}$ is first decomposed into $m$
functions $f_{i} : \mathbb{R}^{n} \rightarrow \mathbb{R}$ which generalize
to dual numbers as
%
\begin{equation} \label{dualFunction}
f_{i} \! \left( \xi_{j} \right) = f_{i} \! \left( x_{j} \right)
+ \mathbf{a} \sum_{j = 1}^{n} \delta x_{j} \frac{ \partial f_{i} \! \left( x_{j} \right) }{ \partial x_{j} }.
\end{equation}
%
Note that in the general case the extension does not require the full Jacobian,
$ \partial f_{i} \! \left( x_{j} \right) \! / \partial x_{j}$, but rather its inner product with
$\delta x_{j}$.

The power of extending functions to dual numbers is the implicit incorporation of
the chain rule. A composition of the functions $f : \mathbb{R}^{n} \rightarrow \mathbb{R}^{m}$
and $g : \mathbb{R}^{m} \rightarrow \mathbb{R}^{p}$, for example, becomes
%
\begin{align*}
g_{i} \! \left( f_{j} \! \left( \xi_{k} \right) \right)
&=
g_{i} \! \left( f_{j} \! \left( x_{k} + \mathbf{a} \, \delta x_{k} \right) \right)
\\
&=
g_{i} \! \left( f_{j} \! \left( x_{k} \right)
+ \mathbf{a} \, \sum_{k} \delta x_{k} \frac{ \partial f_{j} }{ \partial x_{k} } \! \left( x_{k} \right) \right)
\\
&=
g_{i} \! \left( f_{j} \! \left( x_{k} \right) \right)
+ \mathbf{a} \, \sum_{k} \delta x_{k}
\frac{ \partial g_{i} }{ \partial f_{j} }  \! \left( f_{j} \! \left( x_{k} \right) \right)
\frac{ \partial f_{j} }{ \partial x_{k} } \! \left( x_{k} \right):
\end{align*}
%
the second component is just $\sum_{k} \delta x_{k} \partial g_{i} / \partial x_{k} $
as would have been computed by the chain rule.  The real utility of dual numbers is
that they factor the chain rule into the purely differential operations (computing partial
derivatives) and the purely algebraic operations (addition and multiplication), which
allows for a straightforward algorithmic implementation.

\section*{First-Order Automatic Differentiation}

Automatic differentiation implements the propagation of dual numbers through a
composite function and, consequently, the chain rule.  We begin by transforming
a composite function into an \textit{expression graph}, where each node is given
by a dual number intermediate to the calculation and edges denote the
dependencies of the functions (Figure \ref{fig:exprGraph}).

\begin{figure}
\setlength{\unitlength}{0.1in}
\centering
\begin{picture}(50, 30)
%
%\put(0, 0) { \framebox(50, 30){} }
%\put(25, 0) { \framebox(25, 30){} }
%\put(25, 0) { \framebox(6.25, 30){} }
%\put(25, 0) { \framebox(12.5, 30){} }
%\put(25, 0) { \framebox(18.75, 30){} }
%
%\put(25, 0) { \framebox(3.125, 30){} }
%\put(25, 0) { \framebox(9.375, 30){} }
%\put(25, 0) { \framebox(15.625, 30){} }
%
\put(12.5, 15) { \makebox(0, 0)
{$z \! \left( y_{1} \! \left( x_{1}, x_{2} \right), y_{2} \! \left( x_{2}, x_{3} \right) \right)$} }
%
\put(21.875, 15) { \vector(1, 0){6.25} }
%
\put(31.25, 7.5) { \circle{4} }
\put(31.25, 7.5) { \makebox(0, 0) {$ x_{1} $} }
%
\put(37.5, 7.5) { \circle{4} }
\put(37.5, 7.5) { \makebox(0, 0) { $ x_{2} $ } }
%
\put(43.75, 7.5) { \circle{4} }
\put(43.75, 7.5) { \makebox(0, 0) { $ x_{3} $ } }
%
\put(31.25, 9.5) { \vector(3, 4){2.75} }
\put(37.5, 9.5) { \vector(-3, 4){2.75} }
\put(37.5, 9.5) { \vector(3, 4){2.75} }
\put(43.75, 9.5) { \vector(-3, 4){2.75} }
%
\put(35, 15) {\circle{4} } % Tweaked to the right
\put(34.375, 15) { \makebox(0, 0) { $y_{1}$ } }
%
\put(41.25, 15) {\circle{4} } % Tweaked to the right
\put(40.625, 15) { \makebox(0, 0) { $y_{2}$ } }
%
\put(34.375, 17) { \vector(3, 4){2.75} }
\put(40.625, 17) { \vector(-3, 4){2.75} }
%
\put(38, 22.5) {\circle{4} } % Tweaked to the right
\put(37.5, 22.5) { \makebox(0, 0) { $ z $ } }
%
\end{picture}
\caption{
Composite functions are isomorphic to directed acyclic graphs known as
an \textit{expression graph} or \textit{expression tree}.  Here we the function
$z \! \left( y_{1} \! \left( x_{1}, x_{2} \right), y_{2} \! \left( x_{2}, x_{3} \right) \right)$
generates a three level graph.
}
\label{fig:exprGraph}
\end{figure}

Once the expression graph has been constructed the evaluation of the function
reduces to propagating the values of the dual numbers along the edges.  The
directionality of the messages at each node is not constrained, but in practice we
typically consider passing all messages \textit{forward} with the evaluation and passing
all messages \textit{reverse} to the evaluation.

\subsection*{Forward Mode}

Note that the second component of a dual number-valued function is really
a directional derivative, mapping the input vector $\delta x_{j}$ to
$\sum_{k} \delta x_{k} \partial f_{j} / \partial x_{k}$.  In other words,
the Jacobian is a map $J: \mathbb{R}^{n} \rightarrow \mathbb{R}^{m}$.

In forward mode autodiff we propagate $\delta x_{j}$ defined at the inputs
forward through the expression graph using the Jacobian map at each
node (Figure \ref{fig:directions}).  Following the literature we will refer to the components of the dual
numbers as \textit{values},
%
\begin{equation*}
\mathcal{V} \! \left( \xi \right)
= \mathcal{V} \! \left( x + \mathbf{a} \, \delta x \right)
= x,
\end{equation*}
%
and \textit{tangents} or \textit{perturbations},
\begin{equation*}
\mathcal{T} \! \left( \xi \right)
= \mathcal{T} \! \left( x + \mathbf{a} \, \delta x \right)
= \delta x,
\end{equation*}
%
respectively.

\begin{figure}
\setlength{\unitlength}{0.1in}
\centering
\begin{picture}(50, 30)
%
%\put(0, 0) { \framebox(50, 30){} }
%\put(0, 0) { \framebox(12.5, 30){} }
%\put(0, 0) { \framebox(25, 30){} }
%\put(0, 0) { \framebox(37.5, 30){} }
%\put(0, 0) { \framebox(50, 10){} }
%\put(0, 0) { \framebox(50, 20){} }
%
% Forward Mode
%
\put(12.5, 5) { \makebox(0, 0) { Forward Mode } }
%
\put(13, 10) {\circle{4} } % Tweak to the right
\put(12.5, 10) { \makebox(0, 0) { $ \xi_{j} $ } }
%
\put(12, 12) { \vector(0, 1){6} }
\put(13, 12) { \vector(0, 1){6} }
%
\put(13, 20) {\circle{4} } % Tweak to the right
\put(12.5, 20) { \makebox(0, 0) { $ \xi_{i} $ } }
%
\put(6.75, 15) { \makebox(0, 0)
{ $ \mathcal{V} \! \left( \xi_{j} \right) = x_{j} \! \left( x_{i} \right)$ } }
\put(16.5, 16) { \makebox(0, 0)
{ $ \mathcal{T} \! \left( \xi_{i} \right) =  $} }
\put(20, 14) { \makebox(0, 0)
{ $ \sum_{i} \frac{ \partial x_{i} }{ \partial x_{j} } \mathcal{T} \! \left( \xi_{j} \right)  $ } }
%
% Reverse Mode
%
\put(37.5, 5) { \makebox(0, 0) { Reverse Mode } }
%
\put(38, 10) {\circle{4} } % Tweak to the right
\put(37.5, 10) { \makebox(0, 0) { $ \xi_{j} $ } }
%
\put(37, 12) { \vector(0, 1){6} }
\put(38, 18) { \vector(0, -1){6} }
%
\put(38, 20) {\circle{4} } % Tweak to the right
\put(37.5, 20) { \makebox(0, 0) { $ \xi_{i} $ } }
%
\put(31.75, 15) { \makebox(0, 0)
{ $ \mathcal{V} \! \left( x_{j} \right) = x_{j} \! \left( x_{i} \right)$ } }
\put(41.5, 14) { \makebox(0, 0)
{ $ \mathcal{A} \! \left( \xi_{j} \right) =  $} }
\put(45, 16) { \makebox(0, 0)
{ $ \sum_{i} \frac{ \partial x_{i} }{ \partial x_{j} } \mathcal{A} \! \left( \xi_{i} \right)  $ } }
%
\end{picture}
\caption{
In forward mode autodiff the Jacobian propagates directional derivatives,
$\mathcal{T} \! \left( \xi \right)$ forward in the same direction as the
values, $\mathcal{V} \! \left( \xi \right)$.  Reverse mode autodiff, on
the other hand, uses the adjoint Jacobian to propagate adjoint directional
derivatives, $\mathcal{A} \! \left( \xi \right)$, backwards against the
values, $\mathcal{V} \! \left( \xi \right)$.
}
\label{fig:directions}
\end{figure}

Messages accumulated at the output nodes yield the components of the directional
derivative, $\sum_{k} \delta x_{k} \partial f_{j} / \partial x_{k}$.

\subsection*{Reverse Mode}

In reverse mode autodiff we consider not the Jacobian but rather its \textit{adjoint},
$J^{T} : \mathbb{R}^{m} \rightarrow \mathbb{R}^{n}$, which maps the second
component of dual numbers at the outputs, denoted $\mathrm{d} f_{i}$ to differentiate
them from their use in forward mode autodiff, to the adjoint directional derivative,
$\sum_{j} \mathrm{d} f_{j} \partial f_{j} / \partial x_{k}$.  Using the adjoint Jacobian
we can propagate the $\mathrm{d} f_{i}$ at the outputs backwards through the
expression graph until the input nodes yield the components of the transposed
directional derivative (Figure \ref{fig:directions}).

When considering the action of the adjoint Jacobian the components of the dual
numbers as denoted \textit{values},
%
\begin{equation*}
\mathcal{V} \! \left( \xi \right)
= \mathcal{V} \! \left( x + \mathbf{a} \, \mathrm{d} x \right)
= x,
\end{equation*}
%
and \textit{adjoints} or \textit{sensitivities},
\begin{equation*}
\mathcal{A} \! \left( \xi \right)
= \mathcal{A} \! \left( x + \mathbf{a} \, \mathrm{d} x \right)
= \mathrm{d} x,
\end{equation*}
%
respectively.

\subsection*{Performance}

The relative performance of forward and reverse mode autodiff depends on
the sizes of $n$ and $m$ and the desired components of the Jacobian.  Note
that per evaluation forward mode will be faster than reverse mode as it requires
only one sweep compared to the two reverse mode requires (one forward sweep
to build up the expression graph and one reverse sweep to compute the adjoint
directional derivative).  Moreover the overhead for forward mode will be larger
since it does not require the full expression graph to be stored.

Consider, for example, the many-to-one case where $m = 1$.  Here forward
mode computes the scalar $\sum_{k} \delta x_{i} \partial f / \partial x_{i}$
where as the reverse mode computes the full vector gradient
$\mathrm{d} f \partial f / \partial x_{i}$.  If the directional derivative is all
that is necessary then the forward mode calculation will be quicker given
the considerations above.  On the other hand, if the full vector gradient is required
then the small overhead from reverse mode will be dwarfed by the $m$ repetitions
required to build the full gradient from directional derivatives in forward mode.

In general, computing the full Jacobian is faster with forward mode if $n \ll m$
and faster with reverse mode if $n \gg m$.

\section*{Higher-Order Automatic Differentiation}

One substantial advantage of dual numbers is that they dramatically ease the
manipulation of higher-order variants of the chain rule.  In order to go to
higher-order, however, we need to introduce nested dual numbers.

For example, let $\mathbf{a}$ and $\mathbf{b}$ be two distinct dual units.
A first-order dual number,
%
\begin{equation*}
\zeta_{i} = z_{i} + \mathbf{a} \, \delta z_{i},
\end{equation*}
%
then becomes a second-order dual number by replacing the
real-valued components with dual numbers in the direction of the second
dual unit,
%
\begin{alignat*}{3}
\zeta_{i}
&=
\xi_{i}
&&+ \mathbf{a} \, \eta_{i}
\\
&=
\left( x_{i} + \mathbf{b} \, \delta x_{i} \right)
&&+ \mathbf{a} \left( \delta y_{i} + \mathbf{b} \, \delta^{2} y_{i} \right).
\end{alignat*}
%
I will refer to $x_{i}$ as the first value, $\delta x_{i}$ as the first gradient,
$\delta y_{i}$ as the second value, and $\delta^{2} y_{i}$
as the second gradient.  Higher-order dual numbers follow recursively
by replacing the real-valued components with additional dual numbers along
distinct dual units.

Higher-order dual numbers are evaluated in the same manner of forward and
reverse sweeps as the first order dual numbers, with the propagation rules
for each component generated by the dual algebra.  Forward and reverse
propagations yield various directional derivatives as defined in Table \ref{tab:directDerivs}.

\begin{table*}[t!]
	\centering
	\renewcommand{\arraystretch}{2}
	\begin{tabular}{cccc}
	\rowcolor[gray]{0.9} \textbf{Value Type} & \textbf{Order}
	& \textbf{Formula} & \textbf{Mode}
	\\
	Scalar & First &
	$ \displaystyle \sum_{j} v_{j} \, f_{i} \! \left( x_{j} \right)$ & Forward
	\\
	\rowcolor[gray]{0.9}
	Vector & First &
	$ \displaystyle  f_{i} \! \left( x_{j} \right)$ & Reverse
	\\
	Scalar & Second &
	$ \displaystyle \sum_{jk} v_{j} \, u_{k} \frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} }$
	& Forward
	\\
	\rowcolor[gray]{0.9}
	Vector & Second &
	$ \displaystyle \sum_{j} v_{j} \frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} }$
	& Reverse
	\\
	Scalar & Third &
	$ \displaystyle \sum_{jkl} v_{j} \, u_{k} \, w_{l}
	\frac{ \partial^{3} f_{i} }{ \partial x_{j} \partial x_{k} \partial_{l}}$
	& Forward
	\\
	\rowcolor[gray]{0.9}
	Vector & Third &
	$\displaystyle \sum_{jk} v_{j} \, u_{k} \frac{ \partial^{3} f_{i} }{ \partial x_{j} \partial x_{k} \partial_{l}}$
	& Reverse
	\\
	\end{tabular}
	\caption{Automatic differentiation computes directional derivatives and
	generalizations thereof.  In general, forward mode calculations return
	scalars while reverse mode calculations return vectors.
	\label{tab:directDerivs}}
\end{table*}

Here we compute the propagation rules for second and third-order directional
derivatives to complement the first-order rules derived above before considering
how to use these rules to compute popular differential objects such as the Hessian.

\subsection*{Second-Order}

The extension of a function $f: \mathbb{R}^{n} \rightarrow \mathbb{R}^{m}$ to
a second-order dual number follows from the recursive application of the
first-order extension: given the dual number
%
\begin{alignat*}{3}
\zeta_{i}
&=
\xi_{i}
&&+ \mathbf{a} \, \eta_{i}
\\
&=
\left( x_{i} + \mathbf{b} \, \delta x_{i} \right)
&&+ \mathbf{a} \left( \delta y_{i} + \mathbf{b} \, \delta^{2} y_{i} \right)
\end{alignat*}
%
we have
%
\begin{align*}
f_{i} \! \left( \zeta_{j} \right)
=&
f_{i} \! \left( \xi_{j} \right)
+ \mathbf{a} \sum_{j} \eta_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( \xi_{j} \right)
\\
=&
f_{i} \! \left( x_{j} + \mathbf{b} \, \delta x_{j} \right)
+ \mathbf{a} \sum_{j} \left( \delta y_{j} + \mathbf{b} \, \delta^{2} y_{j} \right)
\frac{ \partial f_{i} }{ \partial x_{j} }
\! \left( x_{j} + \mathbf{b} \, \delta x_{j} \right)
\\
=&
f_{i} \! \left( x_{j} \right)
+ \mathbf{b} \sum_{j} \delta x_{j}  \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( x_{j} \right)
\\
&+
\mathbf{a} \sum_{j} \left( \delta y_{j} + \mathbf{b} \, \delta^{2} y_{j} \right)
\left( \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( x_{j} \right) +
\mathbf{b} \, \sum_{k} \delta x_{k}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( x_{j} \right) \right)
\\
=&
\quad\quad\quad
f_{i} \! \left( x_{j} \right)
\quad\quad\quad\;\;\;
+ \mathbf{b} \;\;\;
\sum_{j} \delta x_{j}  \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( x_{j} \right)
\\
&+
\mathbf{a} \left(
\sum_{j} \delta y_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( x_{j} \right)
+ \mathbf{b} \left(
\sum_{j} \delta^{2} y_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( x_{j} \right)
+ \sum_{jk} \delta x_{k} \, \delta y_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( x_{j} \right)
\right)
\right)
\end{align*}
%
These results are summarized in Table \ref{tab:secondOrder}.

\begin{table*}[t!]
	\centering
	\renewcommand{\arraystretch}{2}
	\begin{tabular}{ccc}
	\rowcolor[gray]{0.9} \textbf{Component} & \textbf{Input} & \textbf{Output} \\
	First Value &
	$x_{i}$ &
	$f_{i} \! \left( x_{j} \right)$
	\\
	\rowcolor[gray]{0.9}
	First Gradient &
	$\delta x_{i}$ &
	$\displaystyle \sum_{j} \delta x_{j}  \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( x_{j} \right)$
	\\
	Second Value &
	$\delta y_{i}$ &
	$\displaystyle \sum_{j} \delta y_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( x_{j} \right)$
	\\
	\rowcolor[gray]{0.9}
	Second Gradient &
	$\delta^{2} y_{i}$ &
	$\displaystyle \sum_{j} \delta^{2} y_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( x_{j} \right)
	+ \sum_{jk} \delta x_{k} \, \delta y_{j}
	\frac{ \partial f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( x_{j} \right)$
	\\
	\end{tabular}
	\caption{Recursively expanding an input function yields its action
	on a second-order dual number input.
	\label{tab:secondOrder}}
\end{table*}

In forward mode we compute four different values at each node -- the function
evaluation, a directional derivative along $\delta x_{i}$, a directional derivative
along $\delta y_{i}$, and the scalar-valued second-order directional derivative
$ \sum_{j} \delta^{2} y_{j} \, \partial f_{i} / \partial x_{j}
+ \sum_{jk} \delta x_{k} \, \delta y_{j} \, \partial f_{i} / \partial x_{j} \partial x_{k}$.

The generalization of reverse mode requires some care because the second-order
Jacobian, $\partial f_{i} / \partial x_{j} \partial x_{k}$, does not have a well-defined
transpose.  If we propagate the second-order values forward first, however,
then we can define the linear operator
$\delta y_{j} \, \partial f_{i} / \partial x_{j} \partial x_{k}$ which can be transposed.
Second-order reverse mode then consists of a forward sweep in which the
first and second-order values are computed, and then reverse sweep in which
the first and second-order gradients are computed.  This yields the function
evaluation, the full gradient, a directional derivative along $\delta y_{j}$, and
the vector-valued second-order directional derivative
$ \sum_{j} \mathrm{d}^{2} y_{j} \, \partial f_{i} / \partial x_{j}
+ \sum_{jk} \mathrm{d} x_{k} \, \delta y_{j} \, \partial f_{i} / \partial x_{j} \partial x_{k}$

\subsection*{Third-Order}

Continuing to third-order follows in kind.  Given a third-order dual number,
%
\begin{alignat*}{10}
\zeta_{i}
&=
&& \xi_{i} && && &&
&&+ \mathbf{a} \,
&& \eta_{i} && && &&
\\
&=
( && \sigma_{i} && + \mathbf{b} && \tau_{i} &&)
&&+ \mathbf{a} \,
( && \upsilon_{i} && + \mathbf{b} && \nu_{i} && )
\\
&=
(( && s_{i} + \mathbf{c} \, \delta s_{i} )
&& + \mathbf{b} \;
( && \delta t_{i} + \mathbf{c} \, \delta^{2} t_{i}  ) &&)
&&+ \mathbf{a} \;
(( && \delta u_{i} + \mathbf{c} \, \delta^{2} u_{i} )
&& + \mathbf{b} \;
( && \delta^{2} v_{i} + \mathbf{c} \, \delta^{3} v_{i} ) &&)
\end{alignat*}
%
we have
%
\begin{align*}
f_{i} \! \left( \zeta_{j} \right)
%
=&
f_{i} \! \left( \xi_{j} \right)
+ \mathbf{a} \sum_{j} \eta_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( \xi_{j} \right)
\\
%
=&
f_{i} \! \left( \sigma_{j} + \mathbf{b} \, \tau_{j} \right)
+ \mathbf{a} \sum_{j} \left( \upsilon_{j} + \mathbf{b} \, \nu_{j} \right)
\frac{ \partial f_{i} }{ \partial x_{j} }
\! \left( \sigma_{j} + \mathbf{b} \, \tau_{j} \right)
\\
%
=&
f_{i} \! \left( \sigma_{j} \right) + \mathbf{b}
\sum_{j} \tau_{j} \frac{ \partial f_{i} }{ \partial x_{j} }
\! \left( \sigma_{j} \right)
+ \mathbf{a} \sum_{j} \left( \upsilon_{j} + \mathbf{b} \, \nu_{j} \right)
\left( \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( \sigma_{j} \right)
+ \mathbf{b} \sum_{k} \tau_{k}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( \sigma_{j} \right) \right)
\\
%
=&
f_{i} \! \left( \sigma_{j} \right) + \mathbf{b}
\sum_{j} \tau_{j} \frac{ \partial f_{i} }{ \partial x_{j} }
\! \left( \sigma_{j} \right)
+ \mathbf{a} \sum_{j}
\upsilon_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( \sigma_{j} \right)
\\
& + \mathbf{a} \, \mathbf{b} \sum_{j}
 \nu_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( \sigma_{j} \right)
+ \mathbf{a} \, \mathbf{b} \, \sum_{jk} \upsilon_{j} \tau_{k}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( \sigma_{j} \right)
\\
%
=&
f_{i} \! \left( s_{j} + \mathbf{c} \, \delta s_{j} \right) + \mathbf{b}
\sum_{j} \left( \delta t_{j} + \mathbf{c} \, \delta^{2} t_{j} \right)
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} + \mathbf{c} \, \delta s_{j} \right)
\\
&+
\mathbf{a} \sum_{j}
\left( \delta u_{j} + \mathbf{c} \, \delta^{2} u_{j} \right)
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} + \mathbf{c} \, \delta s_{j} \right)
\\
& + \mathbf{a} \, \mathbf{b} \sum_{j}
\left( \delta^{2} v_{j} + \mathbf{c} \, \delta^{3} v_{j} \right) \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} + \mathbf{c} \, \delta s_{j} \right)
\\
&+
\mathbf{a} \, \mathbf{b} \, \sum_{jk}
\left( \delta u_{j} + \mathbf{c} \, \delta^{2} u_{j} \right)
\left( \delta t_{k} + \mathbf{c} \, \delta^{2} t_{k} \right)
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} + \mathbf{c} \, \delta s_{j} \right)
\\
%
=&
f_{i} \! \left( s_{j} \right) + \mathbf{c} \sum_{j} \delta s_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
\\
&+
\mathbf{b}
\sum_{j} \left( \delta t_{j} + \mathbf{c} \, \delta^{2} t_{j} \right)
\left( \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+  \mathbf{c} \sum_{k} \delta s_{k}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right) \right)
\\
&+
\mathbf{a} \sum_{j}
\left( \delta u_{j} + \mathbf{c} \, \delta^{2} u_{j} \right)
\left( \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{c} \sum_{k} \delta s_{k}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right) \right)
\\
& + \mathbf{a} \, \mathbf{b} \sum_{j}
\left( \delta^{2} v_{j} + \mathbf{c} \, \delta^{3} v_{j} \right)
\left( \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{c} \sum_{k} \delta s_{k}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right) \right)
\\
&+
\mathbf{a} \, \mathbf{b} \, \sum_{jk}
\left( \delta u_{j} + \mathbf{c} \, \delta^{2} u_{j} \right)
\left( \delta t_{k} + \mathbf{c} \, \delta^{2} t_{k} \right)
\\
& \quad\quad\quad\quad \times \left(
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
+ \mathbf{c} \sum_{l} \delta s_{l}
\frac{ \partial^{3} f_{i} }{ \partial x_{j} \partial x_{k} \partial_{l} } \! \left( s_{j} \right)
\right)
\\
%
\end{align*}

\begin{align*}
f_{i} \! \left( \zeta_{j} \right)
%
=&
f_{i} \! \left( s_{j} \right) + \mathbf{c} \sum_{j} \delta s_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
\\
&+
\mathbf{b} \sum_{j} \delta t_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{b} \, \mathbf{c} \sum_{j} \delta^{2} t_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{b} \, \mathbf{c} \sum_{jk} \delta s_{k} \, \delta t_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+
\mathbf{a} \sum_{j} \delta u_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{a} \, \mathbf{c} \sum_{j} \delta^{2} u_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{a} \, \mathbf{c} \sum_{jk} \delta s_{k} \, \delta u_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+
\mathbf{a} \, \mathbf{b} \sum_{j} \delta^{2} v_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{a} \, \mathbf{b} \, \mathbf{c} \sum_{j} \delta^{3} v_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{a} \, \mathbf{b} \, \mathbf{c} \sum_{jk} \delta s_{k} \, \delta^{2} v_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+
\mathbf{a} \, \mathbf{b} \, \sum_{jk}
\left( \delta u_{j} \delta t_{k}
+ \mathbf{c} \left(
\delta u_{j} \, \delta^{2} t_{k} + \delta^{2} u_{j} \, \delta t_{k}
\right) \right)
\\
& \quad\quad\quad\quad \times \left(
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
+ \mathbf{c} \sum_{l} \delta s_{l}
\frac{ \partial^{3} f_{i} }{ \partial x_{j} \partial x_{k} \partial_{l} } \! \left( s_{j} \right)
\right)
\\
%
=&
f_{i} \! \left( s_{j} \right) + \mathbf{c} \sum_{j} \delta s_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
\\
&+
\mathbf{b} \sum_{j} \delta t_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{b} \, \mathbf{c} \sum_{j} \delta^{2} t_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{b} \, \mathbf{c} \sum_{jk} \delta s_{k} \, \delta t_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+
\mathbf{a} \sum_{j} \delta u_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{a} \, \mathbf{c} \sum_{j} \delta^{2} u_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{a} \, \mathbf{c} \sum_{jk} \delta s_{k} \, \delta u_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+
\mathbf{a} \, \mathbf{b} \sum_{j} \delta^{2} v_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{a} \, \mathbf{b} \, \mathbf{c} \sum_{j} \delta^{3} v_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{a} \, \mathbf{b} \, \mathbf{c} \sum_{jk} \delta s_{k} \, \delta^{2} v_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+
\mathbf{a} \, \mathbf{b} \, \sum_{jk}
\delta u_{j} \delta t_{k} \frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+ \mathbf{a} \, \mathbf{b} \, \mathbf{c} \sum_{jk}
\left( \delta u_{j} \, \delta^{2} t_{k} + \delta^{2} u_{j} \, \delta t_{k}  \right)
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+
\mathbf{a} \, \mathbf{b} \, \mathbf{c} \sum_{jkl}
\delta u_{j} \delta t_{k} \, \delta s_{l}
\frac{ \partial^{3} f_{i} }{ \partial x_{j} \partial x_{k} \partial_{l} } \! \left( s_{j} \right)
\\
%
\end{align*}

\begin{align*}
f_{i} \! \left( \zeta_{j} \right)
%
=&
f_{i} \! \left( s_{j} \right) + \mathbf{c} \sum_{j} \delta s_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
\\
&+
\mathbf{b} \sum_{j} \delta t_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{b} \, \mathbf{c} \sum_{j} \delta^{2} t_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{b} \, \mathbf{c} \sum_{jk} \delta s_{k} \, \delta t_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+
\mathbf{a} \sum_{j} \delta u_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{a} \, \mathbf{c} \sum_{j} \delta^{2} u_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{a} \, \mathbf{c} \sum_{jk} \delta s_{k} \, \delta u_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+
\mathbf{a} \, \mathbf{b}
\left(
\sum_{j} \delta^{2} v_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
\delta u_{j} \delta t_{k}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\right)
\\
&+ \mathbf{a} \, \mathbf{b} \, \mathbf{c} \sum_{j} \delta^{3} v_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
\\
&+ \mathbf{a} \, \mathbf{b} \, \mathbf{c} \sum_{jk}
\left(
\delta s_{k} \, \delta^{2} v_{j} + \delta u_{j} \, \delta^{2} t_{k} + \delta^{2} u_{j} \, \delta t_{k}  \right)
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
&+
\mathbf{a} \, \mathbf{b} \, \mathbf{c} \sum_{jkl}
\delta u_{j} \delta t_{k} \, \delta s_{l}
\frac{ \partial^{3} f_{i} }{ \partial x_{j} \partial x_{k} \partial_{l} } \! \left( s_{j} \right)
\\
%
=& \hspace{26mm}
f_{i} \! \left( s_{j} \right)
\hspace{9mm}
+ \mathbf{c}
\hspace{3mm}
\sum_{j} \delta s_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
\\
& \hspace{8mm} +
\mathbf{b} \left( \sum_{j} \delta t_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
\hspace{1mm}
+ \mathbf{c} \left(
\sum_{j} \delta^{2} t_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \sum_{jk} \delta s_{k} \, \delta t_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right) \right) \right)
\\
&+
\mathbf{a} \Bigg(
\hspace{9mm}
\sum_{j} \delta u_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \mathbf{c} \left( \sum_{j} \delta^{2} u_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
+ \sum_{jk} \delta s_{k} \, \delta u_{j}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right) \right)
\\
&
\hspace{8mm}
+ \mathbf{b} \Bigg(
\hspace{0.5mm}
\sum_{j} \delta^{2} v_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
\delta u_{j} \delta t_{k}
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
& \hspace{34mm}
+ \mathbf{c} \Bigg(
\sum_{j} \delta^{3} v_{j}
\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
\\
& \hspace{40mm}
+ \sum_{jk}
\left(
\delta s_{k} \, \delta^{2} v_{j} + \delta u_{j} \, \delta^{2} t_{k} + \delta^{2} u_{j} \, \delta t_{k}  \right)
\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)
\\
& \hspace{40mm} +
\sum_{jkl}
\delta u_{j} \delta t_{k} \, \delta s_{l}
\frac{ \partial^{3} f_{i} }{ \partial x_{j} \partial x_{k} \partial_{l} } \! \left( s_{j} \right)
\Bigg)
\Bigg)
\Bigg)
\\
%
\end{align*}
%
These results are summarized in Table \ref{tab:thirdOrder}.

\begin{table*}[t!]
	\centering
	\renewcommand{\arraystretch}{2}
	\begin{tabular}{ccc}
	\rowcolor[gray]{0.9} \textbf{Component} & \textbf{Input} & \textbf{Output} \\
	First Value &
	$s_{i}$ &
	$f_{i} \! \left( s_{j} \right)$
	\\
	\rowcolor[gray]{0.9}
	First Gradient &
	$\delta s_{i}$ &
	$\displaystyle \sum_{j} \delta s_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right) $
	\\
	Second Value &
	$\delta t_{i}$ &
	$\displaystyle \sum_{j} \delta t_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)$
	\\
	\rowcolor[gray]{0.9}
	Second Gradient &
	$\delta^{2} t_{i}$ &
	$\displaystyle \sum_{j} \delta^{2} t_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
	+ \sum_{jk} \delta s_{k} \, \delta t_{j}
	\frac{ \partial f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)$
	\\
	Third Value &
	$\delta u_{i}$ &
	$\displaystyle \sum_{j} \delta u_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)$
	\\
	\rowcolor[gray]{0.9}
	Third Gradient &
	$\delta^{2} u_{i}$ &
	$\displaystyle \sum_{j} \delta^{2} u_{j}
	\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
	+ \sum_{jk} \delta s_{k} \, \delta u_{j}
	\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)$
	\\
	Fourth Value &
	$\delta^{2} v_{i}$ &
	$\displaystyle \sum_{j} \delta^{2} v_{j} \frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right)
	\delta u_{j} \delta t_{k}
	\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right)$
	\\
	\rowcolor[gray]{0.9}
       \multirow{3}{*}{ \vspace{-8mm} Fourth Gradient} &
	\multirow{3}{*}{ \vspace{-8mm} $\delta^{3} v_{i}$} &
	$\displaystyle \sum_{j} \delta^{3} v_{j}
	\frac{ \partial f_{i} }{ \partial x_{j} } \! \left( s_{j} \right) $
	\\
	& &
	$\displaystyle + \sum_{jk} \left(
	\delta s_{k} \, \delta^{2} v_{j} + \delta u_{j} \, \delta^{2} t_{k} + \delta^{2} u_{j} \, \delta t_{k}  \right)
	\frac{ \partial^{2} f_{i} }{ \partial x_{j} \partial x_{k} } \! \left( s_{j} \right) $
	\\
	\rowcolor[gray]{0.9}
	& &
	$ \displaystyle+ \sum_{jkl} \delta u_{j} \delta t_{k} \, \delta s_{l}
	\frac{ \partial^{3} f_{i} }{ \partial x_{j} \partial x_{k} \partial_{l} } \! \left( s_{j} \right) $
	\\
	\end{tabular}
	\caption{Recursively expanding an input function yields its action
	on a third-order dual number input.
	\label{tab:thirdOrder}}
\end{table*}

A forward sweep of third-order autodiff yields a wealth of information.  In addition
to the function evaluation we have three different first-order scalar-valued directional
derivatives, three different second-order scalar-valued directional derivatives,
and a single third-order scalar-valued directional derivative.

As above, reverse mode requires a separation of values and gradients.  A
preliminary forwards sweep first builds the expression graph and computes the
function, three first-order scalar-valued directional derivatives, and a second-order
scalar-valued directional derivative.  Given the values we can define adjoint
Jacobians and sweep backwards, generating the vector-valued gradient,
two second-order vector-valued directional derivatives, and one third-order
vector-valued directional derivative.

\subsection*{Higher-Order Techniques}

Lastly let's review some techniques for computing various differential objects that
are common in statistical and optimization applications.  Here we focus on many-
to-one functions, $f : \mathbb{R}^{n} \rightarrow 1$.

\begin{description}

	\item[Directional Derivative] \hfill \\

	\begin{description}
		\item[Form:] $\displaystyle \vec{v} \cdot \vec{g} = \sum_{i} v_{i} \frac{ \partial f }{ \partial x_{i} } $
		\item[Algorithm:] \hfill \\
		Initialize $\delta x_{i} = v_{i}$. \\
		Compute first-order forward sweep. \\
		Return first gradient of output.
		\item[Cost:] $\mathcal{O} \! \left( 1 \right)$
		\item[Adjuncts:] $ f $
	\end{description}

	\item[Gradient] \hfill \\
	\begin{description}
		\item[Form:] $\displaystyle \vec{g} = \frac{ \partial f }{ \partial x_{i} } $
		\item[Algorithm:] \hfill \\
		Initialize $\mathrm{d} f = 1$. \\
		Compute first-order reverse sweep. \\
		Return first gradient of inputs.
		\item[Cost:] $\mathcal{O} \! \left( 1 \right)$
		\item[Adjuncts:] $ f $
	\end{description}

	\item[Hessian Quadratic Form] \hfill \\
	\begin{description}
		\item[Form:]
		$\displaystyle \vec{v\,}^{T} H \, \vec{u} = \sum_{ij} v_{i} u_{j} \frac{ \partial^{2} f }{ \partial x_{i} \partial x_{j} } $
		\item[Algorithm:] \hfill \\
		Initialize $\delta x_{i} = v_{i}, \, \delta y_{i} = u_{i}, \delta^{2} y_{i} = 0$. \\
		Compute second-order forward sweep. \\
		Return second gradient of output.
		\item[Cost:] $\mathcal{O} \! \left( 1 \right)$
		\item[Adjuncts:] $ f, \vec{v} \cdot \vec{g}, \vec{u} \cdot \vec{g}$
	\end{description}

	\item[Hessian-Vector Product] \hfill \\
	\begin{description}
		\item[Form:] $\displaystyle H \vec{v} = \sum_{j} v_{j} \frac{ \partial^{2} f }{ \partial x_{i} \partial x_{j} } $
		\item[Algorithm:] \hfill \\
		Initialize $\mathrm{d} f = 1$. \\
		Compute first-order reverse sweep for first gradients. \\
		Initialize $\delta y_{i} = v_{i}$. \\
		Propagate second values in a forward sweep. \\
		Initialize $\mathrm{d}^{2} f = 0.$ \\
		Compute second-order reverse sweep. \\
		Return second gradient of inputs.
		\item[Cost:] $\mathcal{O} \! \left( 1 \right)$
		\item[Adjuncts:] $ f, \vec{v} \cdot \vec{g}, \vec{g}$
	\end{description}

	\item[Hessian] \hfill \\
	\begin{description}
		\item[Form:] $\displaystyle \frac{ \partial^{2} f }{ \partial x_{i} \partial x_{j} } $
		\item[Algorithm:] \hfill \\
		Initialize $\mathrm{d} f = 1$. \\
		Compute first-order reverse sweep for first gradients. \\
		For each $j$ compute the $j$th row of the Hessian as:
		\begin{itemize}
			\setlength{\itemsep}{0cm}
			\setlength{\parskip}{0cm}
			\item[] Initialize $\delta y_{i} = \delta^{j}_{i}$.
			\item[] Propagate second values in a forward sweep.
			\item[] Initialize $\mathrm{d}^{2} f = 0.$
			\item[] Compute second-order reverse sweep.
			\item[] Return second gradient of inputs
		\end{itemize}
		\item[Cost:] $\mathcal{O} \! \left( n \right)$
		\item[Adjuncts:] $ f, \vec{g}$
	\end{description}

	\item[Gradient of the Trace of a Matrix Hessian Product] \hfill \\
	\begin{description}
		\item[Form:]
		$\displaystyle \frac{\partial}{\partial x_{i} } \mathrm{Tr} \! \left[ M \cdot H \right]
		= \sum_{jk} M_{jk} \frac{ \partial^{3} f }{ \partial x_{i}  \partial x_{j}  \partial x_{k} } $
		\item[Algorithm:] \hfill \\
		Initialize $\mathrm{d} f = 1$. \\
		Compute first-order reverse sweep for first gradients. \\
		Initialize the trace gradient to zero.
		For each $j$ increment the trace gradient with:
		\begin{itemize}
			\setlength{\itemsep}{0cm}
			\setlength{\parskip}{0cm}
			\item[] Initialize $\delta t_{i} = \delta^{j}_{i}$.
			\item[] Propagate second values in a forward sweep.
			\item[] Initialize $\mathrm{d}^{2} f = 0$.
			\item[] Compute second-order reverse sweep.
			\item[] Initialize $\delta u_{i} = M_{ji}, \delta^{2} v_{i} = 0$.
			\item[] Propagate third and fourth values in a  forward sweep.
			\item[] Initialize $\mathrm{d}^{3} f = 0$.
			\item[] Compute third-order reverse sweep.
			\item[] Return the fourth gradient of the inputs.
		\end{itemize}
		\item[Cost:] $\mathcal{O} \! \left( n \right)$
		\item[Adjuncts:] $ f, \vec{g}, H$
	\end{description}

\end{description}

\end{document}


\clearpage
\newpage

\section{Results}
\paragraph{Statistical errors}  We used  coalescent simulations of neutral polymorphisms under spatial models of admixture to compare the statistical errors of the AQP and APLS estimates with those of the {\tt tess3} algorithm. The ground truth for the $Q$-matrix (${\bf Q}_0$)  was computed from the mathematical model for admixture proportions used to generate the data. For the $G$-matrix, the  ground truth matrix (${\bf G}_0$) was computed from the empirical genotype frequencies in the two population samples before an admixture event.  The root mean squared errors (RMSE) for the ${\bf Q}$ and ${\bf G}$ estimates decreased as the sample size and the number of loci increased (Figure 1). For all algorithms, the statistical errors were generally small when the number of loci was greater than $10$k SNPs. Those results provided evidence that the three algorithms produced equivalent estimates of the matrices ${\bf Q}_0$ and ${\bf G}_0$. The results also provided a formal check that the APLS and {\tt tess3} algorithms converged to the same estimates as those obtained after the application of  the AQP algorithm, which is guaranteed to converge mathematically.

\paragraph{The benefit of including spatial information in algorithms}    Using  neutral coalescent simulations of spatial admixture, we compared the statistical estimates obtained from a spatial algorithm (APLS) and a non-spatial algorithm (sNMF, Frichot et al. 2014).  For various levels of ancestral population differentiation, estimates obtained from the spatial algorithm were more accurate than for those obtained using non-spatial approaches (Figure 2). For the larger samples, much finer population structure was detected with the spatial method than with the non-spatial algorithm (Figure 2).

In simulations of outlier loci, we used the area under the precision-recall curve (AUC) for quantifying the performances of tests based on the estimates of ancestry matrices, {\bf Q} and {\bf G}. In addition, we computed AUCs for $F_{\rm ST}$-based neutrality tests using truly ancestral genotypes. As they represented the maximum reachable values, AUCs based on truly ancestral genotypes were always higher than those obtained for tests based on reconstructed matrices. For all values of the relative  selection intensity, AUCs were higher for spatial methods than for non-spatial methods (Figure 3, the relative selection intensity is the ratio of migration rates at neutral and adaptive loci). For high selection intensities, the performances of tests based on estimates of ancestry matrices were close to the optimal values reached  by tests based on true ancestral frequencies. These results provided evidence that including spatial information in ancestry estimation algorithms improves the detection of signatures of hard selective sweeps having occurred in unknown ancestral populations.

\paragraph{Runtime and convergence analyses} We subsampled a large SNP data set for {\it A. thaliana} ecotypes  to compare the convergence properties and runtimes of the {\tt tess3}, AQP, and APLS algorithms. In those experiments, we used $K = 6$ ancestral populations, and replicated 5 runs for each simulation. For $n = 100-600$ individuals ($L = 50$k SNPs), the APLS algorithm required more iterations (25 iterations)  than the AQP algorithm (20 iterations) to converge to its solution  (Figure 4). This was less than for {\tt tess3} (30 iterations).  For $L = 10-200$k SNPs ($n = 150$ individuals), similar results were observed. For $50$k SNPs, the runtimes were significantly lower for the APLS algorithm than for the {\tt tess3} and AQP algorithms. For $L = 50$k SNPs and $n = 600$ individuals, it took on average 0.956 min for the APLS and 100 min for the AQP algorithm to compute ancestry estimates. For {\tt tess3}, the runtime was on average 66.3 min. For $L = 100$k SNPs and $n = 150$ individuals, it took on average 0.628 min (8.97 min) for the APLS (AQP) algorithm to compute ancestry estimates. For {\tt tess3}, the runtime was on average 1.27 min.  For those values of $n$ and $L$, the APLS algorithm implementation ran about 2 to 100 times faster than the other algorithm implementations.

\paragraph{Application to European ecotypes of {\it Arabidopsis  thaliana}} We used  the APLS algorithm to survey spatial population genetic structure and perform a genome scan for adaptive alleles in European ecotypes of the plant species {\it A.  thaliana}. The cross validation criterion decreased rapidly from $K=1$ to $K=3$ clusters,  indicating  that  there were three main ancestral groups in Europe, corresponding to geographic regions in Western Europe, Eastern and Central Europe and Northern Scandinavia. For $K$ greater than four, the values of the cross validation criterion decreased in a slower way, indicating that subtle substructure resulting from complex historical isolation-by-distance  processes could also be detected (Figure 5). The spatial analysis provided an approximate range of  $\sigma = 150$km for the spatial variogram (Figure 5). Figure 6 displays the $Q$-matrix estimate  interpolated on a geographic map of Europe for $K = 6$ ancestral groups. The estimated admixture coefficients provided clear evidence for the clustering of the ecotypes in spatially homogeneous genetic groups.

\paragraph{Targets of selection in {\it A.  thaliana} genomes}  Tests based on the $F^Q_{\rm ST}$  statistic were applied to the 241k SNP data set to reveal new targets of natural selection in the {\it A. thaliana} genome. {\it A. thaliana} occurs in a broad variety of habitats, and local adaptation to the environment is acknowledged to be important in shaping its genetic diversity through space~\citep{Hancock2011, Fournier-Level2011}.
The APLS algorithm was run on the 1,095 European lines of {\it A. thaliana} with $K=6$ ancestral populations and $\sigma = 1.5$ for the range parameter. After controlling the FDR at the level $1\%$, the program produced a list of 12,701 candidate SNPs, including linked loci and representing 3\% of the total number of loci.
 The top 100 candidates included SNPs in the flowering-related genes SHORT VEGETATIVE PHASE (SVP), COP1-interacting protein 4.1 (CIP4.1) and FRIGIDA (FRI) ($p$-values $< 10^{-300}$). These genes were detected by previous scans for selection on this dataset~\citep{Horton2012}.
 We performed a gene ontology enrichment analysis using AmiGO in order to evaluate which biological functions might be involved in local adaptation in Europe.
 We found a significant over-representation of genes involved in cellular processes (fold enrichment of 1.06, $p$-value equal to 0.0215 after Bonferonni correction).


\documentclass[letterpaper,12pt,addpoints,answers]{exam}
%\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage[top=1in, bottom=1in, left=0.75in, right=0.75in]{geometry}
\usepackage{amsmath,amssymb,graphicx,xcolor,asymptote}
\usepackage[pdf]{graphviz}
\usepackage{float}

\newcommand{\university}{SANTA MONICA COLLEGE}
\newcommand{\faculty}{Department of Mathematics}
\newcommand{\class}{Math 10}
\newcommand{\examnum}{Final Exam}
\newcommand{\content}{Discrete Structures}
\newcommand{\examdate}{June 6-8, 2021}
\newcommand{\timelimit}{12AM-11:59PM}

\newcommand{\tf}{$\quad\quad T\quad F$}
\newcommand{\tft}{$\quad\quad \boxed T\quad F$}
\newcommand{\tff}{$\quad\quad T\quad \boxed F$}
\newcommand{\tfnt}{$\quad\quad \boxed T\quad F\quad$neither}
\newcommand{\tfnf}{$\quad\quad T\quad \boxed F\quad$neither}
\newcommand{\tfnn}{$\quad\quad T\quad F\quad \boxed{\text{ neither}}$}
\newcommand{\tfn}{$\quad\quad T\quad F\quad$neither}

\pagestyle{headandfoot}
\firstpageheader{}{}{}
\firstpagefooter{}{Page \thepage\ of \numpages}{}
\runningheader{\class}{\examnum}{\examdate}
\runningheadrule
\runningfooter{}{Page \thepage\ of \numpages}{}

\begin{document}

\title{\Large \textbf{\university\\ \faculty\\
        \bigskip
        \class -- \examnum }}
\author{Instructor: Kevin Arlin}
\date{\examdate}
\maketitle
\noindent \rule{\textwidth}{1pt}

Submit your work, typed, as a .pdf or .tex file on Canvas as usual.

This exam allows one sheet of handwritten notes. Handheld calculators are allowed.
\textcolor{red}{It is not open Internet.}  I take
academic honesty during COVID even more seriously than usual, and have been known to
be rather vindictive when I see solutions that were garnered online. Nobody wants that.
Don't do it. I am generous with curves for honest students.

Show your work. Partial credit will be given when earned. Explanations are
required unless explicitly not requested.

\begin{center}
    \textbf{Distribution of Points}\\
    \medskip
    \multicolumngradetable{2}[questions]
\end{center}

\clearpage

\begin{questions}

    \question For any string $s\in A^*$ where $A=\{a,b,c,d,\ldots,x,y,z\},$ say another
    string in $A^*$ \emph{avoids $s$} if $s$ does not occur as a substring.
    \begin{parts}
        \part[4] Derive a recurrence relation for the number $a_n$ of strings in $A^*$ that avoid the string `ck', including initial conditions.
        \begin{solutionorlines}[2in]
            $a_1 = 26$, and $a_2 = 26 * 25$
            Let $c_k$ be the number of strings in $A^*$ that do \textbf{not} end in \textit{'c'} and avoid the string \textit{'ck'}.
            $c_k = a_{k-1} * 25$, removing \textit{'c'}.
            $a_n = 25 * c_{n-1} * 26$, avoiding \textit{'k'} when ending with \textit{'c'} and allowing any letter otherwise.
            \begin{equation}
                a_n = 25 * (a_{n-2} * 25) * 26
            \end{equation}
        \end{solutionorlines}
        \part[3] By finding a closed form for $a_n,$ how many digits does $a_{50}$ have? You may use a programming language or WolframAlpha for this.
        \setlength\answerlinelength{5in}
        \answerline[$19151123960735656846813146744436007044769689232586820865 * 10^{49}$]
        \part[4] Derive a recurrence relation for the number $b_n$ of strings that avoid the string `ack'.
        \begin{solutionorlines}[2in]
            $b_1 = 26$, $b_2 = 26 * 26$, $b_3 = 26 * 26 * 25$
            Let $c_k$ be the number of strings in $A^*$ that do \textbf{not} end in \textit{'ac'} and avoid the string \textit{'ack'}.
            $c_k = a_{k-2} * 26 * 25$, removing \textit{'c'}.
            $b_n = 25 * c_{n-1} * 26$, avoiding \textit{'ak'} when ending with \textit{'ac'} and allowing any letter otherwise.
            $$b_n = 25 * (b_{n-3} * 26 * 25) + 26$$
        \end{solutionorlines}
        \part[3] Compute $b_5$ using inclusion-exclusion.
        \begin{solutionorlines}[2in]
            \paragraph{Excluded Strings:} \textit{ack\textbf{xx}}, \textit{\textbf{x}ack\textbf{x}}, and \textit{\textbf{xx}ack}
            $$n(excluded) = 3 * 26^2$$
            $$b_5 = 26^5 - 3 * 26^2 = 11879348$$
        \end{solutionorlines}
    \end{parts}
    \question
    \begin{parts}
        \part[4] Explain which axioms of an equivalence relation fail for the relation
        ''there is a driving route of length less than $0.25$ miles between $x$ and $y$'' on the set of all points in Santa Monica.
        \begin{solutionorlines}[3in]
            The relation isn't transitive, as the route from point $A$ to point $B$, and the route from point $B$ to point $C$ can both be less than $0.25$ miles, that does not mean that the rounte $A \to B$ is less than $0.25$ miles.
        \end{solutionorlines}
        \part[6] Give an example of an equivalence relation on the set $\mathbb Z$ of integers under which all primes are equivalent, and such that the number of equivalence classes equals the total number of letters in your first and last name combined.
        \setlength\answerlinelength{5in}
        \answerline[has the same number of factors modulo 17 as]
        \begin{solutionorlines}[1in]
            Since all primes only have themselves and $1$ as factors, they all have $2$ factors.

            Furthermore, since I added the modulo 17, there are only 17 equivalence classes, as it wraps back around to 0.
        \end{solutionorlines}
    \end{parts}

    \question[4] Give a natural example of a predicate $P$ of domain the set $H$ of all humans such that $\forall x\in H \exists y \in H (P(x,y))$ is true but $\exists y\in H\forall x\in H (P(x,y))$ is false.
    \setlength\answerlinelength{5in}
    \answerline[$P$ is whether $x$ is a biological child of $y$]
    \begin{solutionorlines}
        Since all humans $x$ have a parent (excluding the edge case of first human, as that begets the chicken-egg problem), but there is no human $y$ that is a child to ALL humans $x$, as humans typically have $2$ biological parents.
    \end{solutionorlines}

    \question[5] State and prove which natural numbers can be written in the form $6x+10y$ for some $x,y\in \mathbb N.$

    \begin{solutionorlines}[5in]
        \paragraph{Base case $16$:} $10 (1) + 6(1) = 16$
        \paragraph{Inductive Case:} Assume $a_{x,y}$ is can be written in the form $6x+10y$.
        Then $a_{x,y+1}$ can also be written as $6x+10(y + 1)$, as $a_{x,y+1} = a_{x,y} + 10$.
        \paragraph{Inductive Case:} Assume $a_{x,y}$ is can be written in the form $6x+10y$.
        Then $a_{x+1,y}$ can also be written as $6(x + 1)+10y$, as $a_{x+1,y} = a_{x,y} + 6$.

        Therefore $16,22,26,28,32,34,36,\dots$ can be written in the form $6x+10y$.

        $a_1 = 16 + 6 = 22$
        $a_2 = 16 + 10 = 26$
        $a_3 = 16 + 6 + 6 = 28$

        Since from these we can add $10$ any number of times, we get that all even numbers greater than 20 can be written in the form $6x+10y$.
    \end{solutionorlines}

    \question Consider the graph $G=K_{3,4}.$
    \begin{parts}
        \part[3] Does $G$ have an Euler circuit? (Why not, if not?)
        \begin{solutionorlines}[2in]
            No, since there is no way to go from the last bottom node you have back to the start without going through a used top node.
        \end{solutionorlines}
        \part[3] Does $G$ have a Hamilton circuit? (Why not, if not?)
        \begin{solutionorlines}[2in]
            No, since there is no euler circuit, it is impossible to get a Hamilton circuit.
        \end{solutionorlines}
        \part[4] Is $G$ planar? If so, explain how to draw it without any edges crossing (you may upload a drawing in this case.) If not, prove it is not.
        \begin{solutionorbox}[2in]
            \begin{figure}[H]
                \digraph{abc}{
                    rankdir=TB;
                    subgraph top {
                            T1;
                            T2;
                            T3;
                        }
                    subgraph bottom {
                            B1;
                            B2;
                            B3;
                            B4;
                        }
                    T1 -> B1 [shape=none];
                    T1 -> B2 [shape=none];
                    T1 -> B3 [shape=none];
                    T1 -> B4 [shape=none];
                    T2 -> B1 [shape=none];
                    T2 -> B2 [shape=none];
                    T2 -> B3 [shape=none];
                    T2 -> B4 [shape=none];
                    T3 -> B1 [shape=none];
                    T3 -> B2 [shape=none];
                    T3 -> B3 [shape=none];
                    T3 -> B4 [shape=none];
                }
            \end{figure}
        \end{solutionorbox}
        \part[6] What are all the isomorphisms $\varphi: G\to G$?
        \begin{solutionorlines}[2in]
            Since the top and bottom have no set order, there are $3! * 4! = 144$ ways to draw $G$.
        \end{solutionorlines}
    \end{parts}

    \question Let's do some landscaping.
    \begin{parts}
        \part[3] How many ways are there to choose six trees from a landscaper that sells orange, lemon, lime, and kumquat trees?
        \answerline[$4^6 = 4096$]
        \begin{solution}
            Since there are $6$ trees to choose from $4$ variants.
        \end{solution}
        \part[4] Supposing I bought two orange, two lemon, and two lime trees, how many ways are there to distribute my trees among three identical bins for transport home?
        \answerline[$3 * 2 = 6$]
        \begin{solution}
            Assuming we are distributing evenly.

            Since there are $3$ possible cominations to distribute the two orange trees, and another $2$ possible cominations to distribute the two lime trees, and only one way to distribute the lemon trees (into the remaining spots).
        \end{solution}
        \part[7] Let $n$ be the number of ways to arrange my six trees along my driveway. There
        are several possibilities for $n$ depending on how my trees are distributed among the
        four species. What are all of these possibilities?
        \begin{solutionorlines}[2in]
            $$\sum_{o=0}^6 \sum_{l=0}^{6-o} \sum_{k=0}^{6-l-o} \frac{6!}{o!l!k!(6-o-l-k)!}$$
            Counting the number of orange, lime, and kumquat (and the rest are lemon), sum all the possibilities.
        \end{solutionorlines}
    \end{parts}
\end{questions}
\end{document}


\documentclass[bigger]{beamer}

\input{header-beam} % change to header-handout for handouts

% ====================
\title[Lecture 8]{Logic I F13 Lecture 8}
\date{October 3, 2013}
% ====================

\include{header}

\section{Review of Rules}

\subsec{$\land$Intro}{

If $A$ and $B$ are both true, then $A \land B$ is true
\bigskip
\setlength{\fitchprfwidth}{1.2in}
\fitchctx{
\nline[$n.$]{A}\\
\nline[$m.$]{B}\\
\fpline[\quad]{A \land B}[\landi{n}{m}]}

}

\subsec{$\land$Elim}{

If $A \land B$ is true, then $A$ ($B$) is true
\bigskip
\begin{tabular}{@{}ll@{}}
\setlength{\fitchprfwidth}{1in}
\fitchctx{
\nline[$n.$]{A \land B}\\
\fpline[\quad]{A}[\lande{n}]}
&\setlength{\fitchprfwidth}{1in}
\fitchctx{
\nline[$n.$]{A \land B}\\
\fpline[\quad]{B}[\lande{n}]}
\end{tabular}
}

\subsec{$\lor$Intro}{

If $A$ ($B$) is true, then $A \lor B$ is true
\bigskip
\begin{tabular}{@{}ll@{}}
\setlength{\fitchprfwidth}{1in}
\fitchctx{
\nline[$n.$]{A}\\
\fpline[\quad]{A \lor B}[\lori{n}]}
&\setlength{\fitchprfwidth}{1in}
\fitchctx{
\nline[$n.$]{B}\\
\fpline[\quad]{A \lor B}[\lori{n}]}
\end{tabular}
}

\subsec{$\lor$Elim}{

Formal version of proof by cases
\bigskip
\bigskip
\setlength{\fitchprfwidth}{1.2in}
\fitchctx{
\nline[$j.$]{A \lor B}\\
\subproof{\nline[$k.$]{A}}{\dots\\ \nline[$l.$]{C}}
\subproof{\nline[$m.$]{B}}{\dots\\ \nline[$n.$]{C}}
\fpline[\quad]{C}[\lore{$j$}{$k$--$l$}{$m$--$n$}]}

}

\subsec{Example}{

\fitchprf{\pline[1.]{A \lor (B \land C)}}{
\subproof{\pline[2.]{A}}{\pline[3.]{A \lor B}[\lori{2}]}
\subproof{\pline[4.]{B \land C}}{
\pline[5.]{B}[\lande{4}]\\
\pline[6.]{A \lor B}[\lori{5}]}
\pline[7.]{A \lor B}[\lore{1}{2--3}{4--6}]
}}

\section{Indirect Proof}

\subsec{Indirect Proof}{

\begin{quote}
To prove that $A$ is true (false), you may proceed by pretending that
$A$ is false (true), and showing that that's impossible.
\end{quote}

\bit
\item ``show that's impossible'': prove something known to be false,
  e.g., proving a claim that is in conflict with the premises, or
  outright contradictory
\item ``proof by contradiction'' = ``reductio (ad absurdum)'' =
  indirect proof \eit

}

\subsec{Galileo on Falling Bodies}{

From Galileo Galilei's, \textit{Discourses and Mathematical
  Demonstrations Relating to Two New Sciences}, 1638.
\medskip

\emph{Simplicio.}  There can be no doubt but
  that one and the same body moving in a single medium has a fixed
  velocity which is determined by nature and which cannot be increased
  except by the addition of momentum or diminished except by some
  resistance which retards it.
\medskip

\emph{Salviati.}  If then we take two bodies whose natural speeds are
different, it is clear that on uniting the two, the more rapid one
will be partly retarded by the slower, and the slower will be somewhat
hastened by the swifter. Do you not agree with me in this opinion?
\medskip

\emph{Simp.}  You are unquestionably right.}
\subsec{Galileo on Falling Bodies}{

\emph{Salv.}  But if this is true, and if a large stone moves with a speed
of, say, eight while a smaller moves with a speed of four, then when
they are united, the system will move with a speed less than eight;
but the two stones when tied together make a stone larger than that
which before moved with a speed of eight.  Hence the heavier body
moves with less speed than the lighter; an effect which is contrary to
your supposition.  Thus you see how, from your assumption that the
heavier body moves more rapidly than the lighter one, I infer that the
heavier body moves more slowly.
}

\subsec{Disjunctive Syllogism}{

\setlength{\fitchprfwidth}{1.2in}

\begin{columns}
\begin{column}{3cm}
\fitchprf{
\nline{A \lor B}\\
\nline{\lnot A}}{\nline{B}}
\end{column}
\begin{column}{7cm}
\bens
\item Suppose $\sf B$
were not true.
\item By premise 1, $\sf A \lor B$ is true.
\item Case 1.  $\sf A$ is
true. By premise 2, $\sf \lnot A$ is true, so we have a contradiction.
\item Case 2. $\sf B$ is true. This contradicts the assumption that $\sf B$ is false, so again we have a contradiction.
\item So by proof by
cases, we have proved a contradiction from the assumption that $\sf
B$ is not true.
\item So $\sf B$ must be true, by indirect proof. QED.
\een
\end{column}
\end{columns}
}

\subsec{Rules for $\lnot$}{

\setlength{\fitchprfwidth}{.9in}
\fitchctx{
\subproof{\nline[$k.$]{A}}{\dots\\ \nline[$l.$]{\bot}}
\fpline[\quad]{\lnot A}[\lnoti{$k$--$l$}]}
\fitchctx{
\subproof{\nline[$k.$]{\lnot A}}{\dots\\ \nline[$l.$]{\bot}}
\fpline[\quad]{A}[\lnoti{$k$--$l$}]}

\bigskip
\fitchctx{
\nline[$k.$]{\lnot\lnot A}\\
\fpline[\quad]{A}[\lnote{k}]}

}

\subsec{Rules for $\bot$}{

\fitchctx{
\nline[$k.$]{A}\\
\nline[$l.$]{\lnot A}\\
\fpline[\quad]{\bot}[\lfalsei{k}{l}]}
\bigskip
\setlength{\fitchprfwidth}{.9in}
\fitchctx{
\nline[$k.$]{\bot}\\
\fpline[\quad]{A}[\lfalsee{k}]}

}

\subsec{Disjunctive Syllogism}{

\setlength{\fitchprfwidth}{1.2in}

\fitchprf{
\nline{A \lor B}\\
\nline{\lnot A}}{\nline{B}}
}

\subsec{De Morgan's Law}{
\setlength{\fitchprfwidth}{1.2in}

\fitchprf{
\nline{A \lor B}}{\nline{\lnot(\lnot A \land \lnot B)}}
}

\section{Proofs from Contradictory Premises}

\subsec{Proofs from Contradictory Premises}{

\begin{quote}
Any argument with TT-contradictory premises is tautologically valid.
\end{quote}

}

\subsec{Proofs from Contradictory Premises}{

\begin{columns}
\begin{column}{3cm}
\fitchprf{
\pline{Small(b)}\\
\pline{\lnot Small(b)}}{
\pline{Tet(a)}}
\end{column}
\begin{column}{7cm}
\bens
\item Suppose, by way of contradiction, that $\sf \lnot Tet(a)$.
\item We are told (premise 1) that $\sf
Small(b)$.
\item We are also told (premise 2) that $\sf \lnot
Small(b)$.
\item That's a contradiction.
\item So $Tet(a)$ by indirect proof. QED.
\een
\end{column}
\end{columns}
}

\end{document}

\section{Proof Strategies}

\subsec{Strategy for Proving Conjunctions}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{
   \pline[1.]{}
}{
 \ellipsesline \\
 \pline[2.]{} \\
 \ellipsesline \\
 \pline[3.]{} \\
 \pline[4.]{P \land Q}[\hphantom{\landi{2}{3}}]
}
  &
If we want to prove $\sf P \land Q$, we have to use $\land$Intro.
\end{tabular}

}

\subsec{Strategy for Proving Conjunctions}{

\begin{tabular}{@{}lp{13em}}
  \fitchprf{\pline[1.]{}}{
    \ellipsesline \\
    \pline[2.]{P} \\
    \ellipsesline \\
    \pline[3.]{Q} \\
    \pline[4.]{P \land Q}[\landi{2}{3}]
  }
  &
  For $\land$Intro, we need to prove $\sf P$ first, then prove $\sf Q$. For the last line,
  we'll cite the lines where we proved $\sf P$ and  $\sf Q$, and use $\land$Intro.
\end{tabular}
}

\subsec{Strategy for Using Conjunctions}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{
   \pline[1.]{P \land Q}
}{
 \pline{}[\hphantom{\lande{1}}] \\
 \pline{} \\
 \ellipsesline \\
 \pline{R}
}
  &
If we want to use a sentence of the form $\sf P \land Q$ which is one
of our premises, or a sentence we've already proved, use $\land$Elim.
\end{tabular}
}

\subsec{Strategy for Using Conjunctions}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{
   \pline[1.]{P \land Q}
}{
 \pline[2.]{P}[\lande{1}]\\
 \pline[3.]{Q}[\lande{1}]\\
 \ellipsesline \\
 \pline{R}
}
& Use $\land$Elim to ``split up'' $\sf P \land Q$, proving both $\sf
P$ and $\sf Q$.  We'll probably need them further down in the proof.
\end{tabular}
}

\subsec{Strategy for Proving Disjunctions}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{
   \pline[1.]{}
}{
 \ellipsesline \\
 \pline[2.]{} \\
 \pline[3.]{P \lor Q}[\hphantom{\lori{2}}]
}
  &
We want to prove $\sf P \lor Q$. We could do this using  $\lor$Intro.
\end{tabular}
}

\subsec{Strategy for Proving Disjunctions}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\ellipsesline \\
\pline[2.]{P} \\
\pline[3.]{P \lor Q}[\lori{2}]
}
&
For $\lor$Intro, we need to prove one of the disjuncts.
We'll try $\sf P$ first. For the last line,
we'll cite the line where we proved $\sf P$, and use $\lor$Intro.
\end{tabular}
}

\subsec{Strategy for Proving Disjunctions}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\ellipsesline \\
\pline[2.]{Q} \\
\pline[3.]{P \lor Q}[\lori{2}]
}
&
If we can't prove $\sf P$, we may have to backtrack and try to prove $\sf Q$
instead.  Again, we'll cite the line where we proved $\sf Q$, and use
$\lor$Intro.
\end{tabular}
}

\subsec{Strategy for Using Disjunctions}{

\setlength{\fitchprfwidth}{.9in}

\begin{tabular}{@{}l@{}p{13em}}
\fitchprf{
   \pline[1.]{P \lor Q}
}{
 \pline{}\\
\pline{} \\
\pline{}\\
\pline{} \\
\pline{} \\
\pline{} \\
\ellipsesline \\[.7em]
  \pline[6.]{R}[\hphantom{\lore{1}{2--3}{4--5}}]
}
  &
Suppose we want to prove R, and we have $\sf P \lor Q$ as a premise,
as the assumption of a subproof, or as something proved before.
We'll try to use it by applying $\lor$Elim.
\end{tabular}

}

\subsec{Strategy for Using Disjunctions}{

\setlength{\fitchprfwidth}{.9in}

\begin{tabular}{@{}l@{}p{13em}}
\fitchprf{
   \pline[1.]{P \lor Q} }{ \subproof{\pline[2.]{P}}{ \ellipsesline \\
\pline[3.]{R}} \subproof{\pline[4.]{Q}}{ \ellipsesline \\
\pline[5.]{R}} \pline[6.]{R}[\lore{1}{2--3}{4--5}] }
& To prove $\sf R$ using $\lor$Elim from $\sf P \lor Q$, we'll have to
look for two subproofs: one in which we prove $\sf R$ from $\sf P$,
another where we prove $\sf R$ from $\sf Q$. Once we have both, we
can justify $\sf R$ by using $\lor$Elim, citing the line with $\sf P
\lor Q$ and the two subproofs.
\end{tabular}
}

\subsec{Strategy for Proving Negations}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{
   \pline[1.]{}
}{
 \pline{}\\
\pline{} \\
\pline{}\\
\ellipsesline \\[.7em]
  \pline[4.]{\neg P}[\hphantom{\lnoti{2}}]
}
  &
If we want to prove $\neg\sf P$, we'll have to use  $\neg$Intro.
\end{tabular}

}

\subsec{Strategy for Proving Negations}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{P}}{
\ellipsesline \\
\pline[3.]{\bot}}
\pline[4.]{\neg P}[\lnoti{2--3}]
}
& For $\neg$Intro, we have to start a subproof with assumption $\sf
P$; the last line of the subproof has to be $\bot$.  We'll cite the
subproof, and use $\lnot$Intro.
\end{tabular}
}

\subsec{Strategy for Indirect Proof}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{
   \pline[1.]{}
}{
 \pline{}\\
\pline{} \\
\pline{}\\
\ellipsesline \\[.7em]
  \pline[4.]{P}[\hphantom{\lnoti{2}}]
}
& Sometimes, we want to prove a sentence $\sf P$, but our other
strategies didn't get us far. This is often the case, e.g., when we
want to prove disjunctions but don't have any premises (for example:
$\sf A \lor \neg A$).  Then we can also use $\neg$Intro.
\end{tabular}

}

\subsec{Strategy for Indirect Proof}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg P}}{
\ellipsesline \\
\pline[3.]{\bot}}
\pline[4.]{P}[\lnoti{2--3}]}
&
Here, we have to start a subproof with assumption $\sf \neg P$;
the last line of the subproof has to be $\bot$.
We'll cite the subproof, and use $\lnot$Intro.  In the subproof, we now
have an additional assumption (on line 2) to work with.
\end{tabular}

}

\subsec{How to Find a Contradiction}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg P}}{
\pline{}\\
\ellipsesline \\
\pline[4.]{\bot}}
\pline[5.]{P}[\lnoti{2--3}]
}
&
Suppose we used the indirect proof strategy, or we're in some other situation
where we're looking for a proof of $\bot$.  What's a good candidate?
\end{tabular}

}

\subsec{How to Find a Contradiction}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg P}}{
\ellipsesline \\
\pline[3.]{P}\\
\pline[4.]{\bot}[\lfalsei{2}{3}]}
\pline[5.]{P}[\lnoti{2--4}]
}
& One good candidate is $\sf P$ itself!  This may look weird but
remember that inside the subproof, we have an additional assumption
$\sf \lnot P$ to work with.
\end{tabular}

}

\subsec{How to Use a Negated Sentence}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{
   \pline[1.]{\neg P}
}{
 \pline{}\\
\pline{} \\
\pline{}\\
\ellipsesline \\[.7em]
  \pline[4.]{R}[\hphantom{\lnoti{2}}]
}
  &
Suppose $\sf\neg P$ is one of your premises, or something else you've
already proved, and the sentence you're shooting for doesn't contain
$\sf \neg P$. How are you going to make use of  $\sf\neg P$?
\end{tabular}

}

\subsec{How to Use a Negated Sentence}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{\neg P}}{
\subproof{\pline[2.]{\neg R}}{
\ellipsesline \\
\pline[3.]{P}\\
\pline[4.]{\bot}[\lfalsei{1}{3}]}
\pline[5.]{R}[\lnoti{2--4}]
}
&
The only way to make use of $\sf\neg P$ in such a situation is as part
of a contradiction. For instance, apply the indirect proof strategy,
and then look for $\sf P$ inside the subproof.
\end{tabular}

}

\subsec{Summary}{

\bits
\item Trying to prove $\sf P \land Q$? Look for a proof of $\sf P$,
  then for a proof of $\sf Q$ ($\land$Intro).
\item Split up conjunctions ($\land$Elim) whenever you can.
\item Next, use the disjunctions you have ($\lor$Elim).
\item Trying to prove $\sf\neg P$? Look for a subproof of $\bot$ from
  $\sf P$ ($\neg$Intro).
\item Trying to prove $\sf P \lor Q$?  Look for a proof of $\sf P$, if
  that doesn't work, look for a proof of $\sf Q$ ($\lor$Intro). If
  that doesn't work either, use indirect proof.  (Note: $\lor$Intro
  rarely works unless you're in a subproof.)
\item Apply the strategies at each step in the construction of your
  proof. You may not be able to prove $\sf P \lor Q$ using $\lor$Intro
  at one point, but then you might find yourself in a subproof where
  it is possible.
\eit
}

\subsec{Summary}{
\bits
\item It's good to know where you're going: So use the strategies
  based on $\land$Intro, $\lor$Elim, $\neg$Intro before those based on
  $\land$Elim and $\lor$Intro.
\item Picking a disjunct to prove for $\lor$Intro or a candidate for a
  contradiction doesn't always work. If it doesn't, you have to
  backtrack and try something else.
\item If you're looking for a proof of $\sf P$ and you're not getting
  anywhere, make sure you're on the right track: is $\sf P$ even a
  tautological consequence of the premises and assumptions? If not,
  you'll have to backtrack.
\end{itemize}

}

\section{Examples}

\setlength{\fitchprfwidth}{1.5in}

\subsec{An Example}{

\begin{tabular}{@{}l@{}p{13em}}
\fitchprf{
   \pline[1.]{(A \land B) \lor (A \land C)}
}{
 \pline{}\\
\pline{} \\
\pline{}\\
\ellipsesline \\[.7em]
  \pline{A \land (B \lor C)}[\hphantom{\lore{1}{2--5}{6--9}}]
}
  &
First, we use the disjunction on line 1, and set up the subproofs we
need for $\lor$Elim.
\end{tabular}

}
\subsec{An Example}{

\begin{tabular}{@{}l@{}p{13em}}
\fitchprf{
   \pline[1.]{(A \land B) \lor (A \land C)}
}{
\subproof{\pline[2.]{A \land B}}{
\ellipsesline \\
  \pline[6.]{A \land (B \lor C)}}\\[.5ex]
\subproof{\pline[7.]{A \land C}}{
\ellipsesline \\
  \pline[11.]{A \land (B \lor C)}}
  \pline{A \land (B \lor C)}[\lore{1}{2--6}{7--11}]
}
  &
In the first subproof, we now set up the sub goals for proving line 6
using $\land$Intro.
\end{tabular}

}
\subsec{An Example}{

\begin{tabular}{@{}l@{}p{13em}}
\fitchprf{
   \pline[1.]{(A \land B) \lor (A \land C)}
}{
\subproof{\pline[2.]{A \land B}}{
\ellipsesline \\
  \pline[4.]{A}\\
  \pline[5.]{B \lor C}\\
  \pline[6.]{A \land (B \lor C)}[\landi{4}{5}]}\\[.5ex]
\subproof{\pline[7.]{A \land C}}{
  \ellipsesline \\
  \pline[11.]{A \land (B \lor C)}}
\pline{A \land (B \lor C)}[\lore{1}{2--6}{7--11}]
}
  &
We immediately see that we get line 4 from 2 by $\land$Elim; let's
apply the strategy for proving disjunctions to line 5: look for a
proof of $\sf B$.
\end{tabular}

}
\subsec{An Example}{

\begin{tabular}{@{}l@{}p{13em}}
\fitchprf{
   \pline[1.]{(A \land B) \lor (A \land C)}
}{
\subproof{\pline[2.]{A \land B}}{
  \pline[3.]{B}\\
  \pline[4.]{A}[\lande{2}]\\
  \pline[5.]{B \lor C}[\lori{3}]\\
  \pline[6.]{A \land (B \lor C)}[\landi{4}{5}]}\\[.5ex]
\subproof{\pline[7.]{A \land C}}{
\ellipsesline \\
  \pline[11.]{A \land (B \lor C)}}
  \pline[12.]{A \land (B \lor C)}[\lore{1}{2--6}{7--11}]
}
  &
Like line 4 before, we get line 3 from 2 by $\land$Elim. That's it for
the first subproof. The second subproof is exactly the same.
\end{tabular}

}
\subsec{An Example}{

\begin{tabular}{@{}l@{}p{13em}}
\fitchprf{
   \pline[1.]{(A \land B) \lor (A \land C)}
}{
\subproof{\pline[2.]{A \land B}}{
  \pline[3.]{B}[\lande{2}]\\
  \pline[4.]{A}[\lande{2}]\\
  \pline[5.]{B \lor C}[\lori{3}]\\
  \pline[6.]{A \land (B \lor C)}[\landi{4}{5}]}\\[.5ex]
\subproof{\pline[7.]{A \land C}}{
  \pline[8.]{C}[\lande{7}]\\
  \pline[9.]{A}[\lande{7}]\\
  \pline[10.]{B \lor C}[\lori{8}]\\
  \pline[11.]{A \land (B \lor C)}[\landi{9}{10}]}
  \pline[12.]{A \land (B \lor C)}[\lore{1}{2--6}{7--11}]
}
  &
\end{tabular}

}
\subsec{Example Indirect Proof}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg (P \lor ~P)}}{
\ellipsesline\\
\pline[8.]{\bot}[\lfalsei{2}{7}]}
\pline[9.]{P \lor \neg P}[\lnoti{2--8}]
}
&
Suppose we want to prove $\sf P \lor \neg P$.  A little reflection
shows that this can only be done by indirect proof.  So we set up a subproof
for use with $\neg$Intro.
\end{tabular}

}
\subsec{Example Indirect Proof}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg (P \lor ~P)}}{
\ellipsesline\\
\pline[7.]{P \lor \neg P}[?]\\
\pline[8.]{\bot}[\lfalsei{2}{7}]}
\pline[9.]{P \lor \neg P}[\lnoti{2--8}]
}
&
Our first candidate for a contradiction is the assumption of the
subproof.  How should we get $\sf P \lor neg P$ inside that
subproof?
\end{tabular}

}
\subsec{Example Indirect Proof}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg (P \lor ~P)}}{
\subproof{\pline[3.]{P}}{
\ellipsesline\\
\pline[5.]{\bot}[?]
}
\pline[6.]{\neg P}[\lnoti{3--5}]\\
\pline[7.]{P \lor \neg P}[\lori{6}]\\
\pline[8.]{\bot}[\lfalsei{2}{7}]}
\pline[9.]{P \lor \neg P}[\lnoti{2--8}]
}
&
Inside the subproof, we \emph{can} get $\sf P \lor \neg P$ by
$\lor$Intro. So let's try to prove $\sf \neg P$; it's a negated
sentence, so we'll use $\neg$Intro.
\end{tabular}

}
\subsec{Example Indirect Proof}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg (P \lor ~P)}}{
\subproof{\pline[3.]{P}}{
\pline[4.]{P \lor \neg P}[\lori{3}]\\
\pline[5.]{\bot}[\lfalsei{2}{4}]
}
\pline[6.]{\neg P}[\lnoti{3--5}]\\
\pline[7.]{P \lor \neg P}[\lori{6}]\\
\pline[8.]{\bot}[\lfalsei{2}{7}]}
\pline[9.]{P \lor \neg P}[\lnoti{2--8}]
}
&
Here, the assumption of the subproof, $\sf P$, is not
really a good candidate for a negation. But we can use the negated
sentence on line 2 again, and it's easy to get in this situation.
\end{tabular}

}
\subsec{Another Example}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\ellipsesline\\
\pline[13.]{\neg \neg (P \lor Q) \lor \neg P}[\hphantom{\lnoti{2--12}}]
}
  &
Here, we're proving that $\sf\neg \neg (P \lor Q) \lor \neg P$ is a
tautology---that's why there are no premises.  Since neither P nor
$\sf\neg\neg( P \lor Q)$ is a tautology, we won't be able to
prove either of these, so the strategy for proving the conclusion
using $\lor$Intro won't work. We have to use indirect proof.
\end{tabular}
}
\subsec{Another Example}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg(\neg \neg (P \lor Q) \lor \neg P)}}{
\ellipsesline\\
\pline[12.]{\bot}[?]
}
\pline[13.]{\neg \neg (P \lor  Q) \lor \neg P}[\lnoti{2--12}]
}
  &
To use $\neg$Intro as the last step we need a subproof that proves
a contradiction from the negation of the goal sentence.  What's a
good candidate for sentences from which we get $\bot$ using $\bot$Intro?
\end{tabular}
}
\subsec{Another Example}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg(\neg\neg (P \lor Q) \lor \neg P)}}{
\ellipsesline\\
\pline[11.]{\neg\neg( P \lor  Q) \lor \neg P}[?] \\
\pline[12.]{\bot}[\lfalsei{2}{11}]
}
\pline[13.]{\neg \neg (P \lor Q) \lor \neg P}[\lnoti{2--12}]
}
  &
The only plausible candidate is the sentence $\sf \neg\neg (P \lor Q)
\lor \neg P$ itself, since no other negated sentence is available.  So
let's try to prove that inside the subproof.  We'll get $\bot$ by
$\bot$Intro from lines 2 and 11. So how do we get line 11?
\end{tabular}
}
\subsec{Another Example}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg(\neg\neg( P \lor Q) \lor \neg P)}}{
\ellipsesline\\
\pline[10.]{\neg\neg (P \lor  Q)}[?] \\
\pline[11.]{\neg \neg (P \lor Q) \lor \neg P}[\lori{10}]\\
\pline[12.]{\bot}[\lfalsei{2}{11}]
}
\pline[13.]{\neg \neg (P \lor Q) \lor \neg P}[\lnoti{2--12}]
}
  &
Line 11 is a disjunction, so the strategy for $\lor$Intro applies.  We
have two choices: try to prove $\sf\neg\neg (P \lor Q)$, or prove $\sf \neg P$.
Either would work, but lets try to prove $\sf \neg\neg (P \lor Q)$.
\end{tabular}
}
\subsec{Another Example}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg(\neg\neg( P \lor Q) \lor \neg P)}}{
\subproof{\pline[3.]{\neg(P \lor Q)}}{
\ellipsesline\\
\pline[9.]{\bot}[?]}
\pline[10.]{\neg\neg (P \lor  Q)}[\lnoti{3--9}]\\
\pline[11.]{\neg \neg (P \lor Q) \lor \neg P}[\lori{10}]\\
\pline[12.]{\bot}[\lfalsei{2}{11}]
}
\pline[13.]{\neg \neg (P \lor Q) \lor \neg P}[\lnoti{2--12}]
}
  &
Since $\sf\neg\neg(P \lor Q)$ starts with a $\neg$, we'll apply the
strategy for proving negations: start a subproof with $\sf\neg(P \lor
Q)$ as the assumption, and try to prove $\bot$.  We justify line 10
using $\neg$Intro.
\end{tabular}
}
\subsec{Another Example}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg(\neg\neg( P \lor Q) \lor \neg P)}}{
\subproof{\pline[3.]{\neg(P \lor Q)}}{
\ellipsesline\\
\pline[8.]{P \lor Q}[?]\\
\pline[9.]{\bot}[\lfalsei{3}{8}]}
\pline[10.]{\neg\neg (P \lor  Q)}[\lnoti{3--9}]\\
\pline[11.]{\neg \neg (P \lor Q) \lor \neg P}[\lori{10}]\\
\pline[12.]{\bot}[\lfalsei{2}{11}]
}
\pline[13.]{\neg \neg (P \lor Q) \lor \neg P}[\lnoti{2--12}]
}
  &
Now we need a candidate for a contradiction.  There are two negated
lines in the avaliable assumptions. Let's pick line 3: try to prove
$\sf P \lor Q$.
\end{tabular}
}
\subsec{Another Example}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg(\neg\neg( P \lor Q) \lor \neg P)}}{
\subproof{\pline[3.]{\neg(P \lor Q)}}{
\ellipsesline\\
\pline[7.]{P}[?]\\
\pline[8.]{P \lor Q}[\lori{7}]\\
\pline[9.]{\bot}[\lfalsei{3}{8}]}
\pline[10.]{\neg\neg (P \lor  Q)}[\lnoti{3--9}]\\
\pline[11.]{\neg \neg (P \lor Q) \lor \neg P}[\lori{10}] \\
\pline[12.]{\bot}[\lfalsei{2}{11}]
}
\pline[13.]{\neg \neg (P \lor Q) \lor \neg P}[\lnoti{2--12}]
}
  &
To get $\sf P \lor Q$, we'll use $\lor$Intro.  For this we have to
prove one of the disjuncts, say, $\sf P$.
\end{tabular}
}
\subsec{Another Example}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg(\neg\neg( P \lor Q) \lor \neg P)}}{
\subproof{\pline[3.]{\neg(P \lor Q)}}{
\subproof{\pline[4.]{\neg P}}{
\ellipsesline\\
\pline[6.]{\bot}[?]
}
\pline[7.]{P}[\lnoti{4--6}]\\
\pline[8.]{P \lor Q}[\lori{7}]\\
\pline[9.]{\bot}[\lfalsei{3}{8}]
}
\pline[10.]{\neg\neg (P \lor  Q)}[\lnoti{3--9}]\\
\pline[11.]{\neg \neg (P \lor Q) \lor \neg P}[\lori{10}]\\
\pline[12.]{\bot}[\lfalsei{2}{11}]
}
\pline[13.]{\neg \neg (P \lor Q) \lor \neg P}[\lnoti{2--12}]
}
& $\sf P$ is an atomic sentence, so no introduction rule can give us
$\sf P$.  We'll have to do indirect proof: start a subproof using $\sf
\neg P$ a the assumption, and prove another contradiction.
\end{tabular}
}
\subsec{Another Example}{

\begin{tabular}{@{}lp{13em}}
\fitchprf{\pline[1.]{}}{
\subproof{\pline[2.]{\neg(\neg\neg( P \lor Q) \lor \neg P)}}{
\subproof{\pline[3.]{\neg(P \lor Q)}}{
\subproof{\pline[4.]{\neg P}}{
\pline[5.]{\neg \neg (P \lor Q) \lor \neg P}[\lori{4}] \\
\pline[6.]{\bot}[\lfalsei{2}{5}]
}
\pline[7.]{P}[\lnoti{4--6}] \\
\pline[8.]{P \lor Q}[\lori{7}] \\
\pline[9.]{\bot}[\lfalsei{3}{8}]}
\pline[10.]{\neg\neg (P \lor  Q)}[\lnoti{3--9}]\\
\pline[11.]{\neg \neg (P \lor Q) \lor \neg P}[\lori{10}]\\
\pline[12.]{\bot}[\lfalsei{2}{11}]
}
\pline[13.]{\neg \neg (P \lor Q) \lor \neg P}[\lnoti{2--12}]
}
  &
How are we getting the contradiction now?  Note how since we have
$\sf\neg P$ as an assumption, we can immediately get $\sf\neg\neg(P
\lor Q) \lor \neg P$ by $\lor$Intro.  Everything's now in place.
\end{tabular}
}

\end{document}

\end{document}

\end{document}