\documentclass{article}

\usepackage{graphicx}
\usepackage{parskip}
\usepackage{amsmath}

\title{BSP Tree}
\author{Gerard Martin Teixidor}

\begin{document}
\maketitle

\section*{Overview}
A binary space partitioning (BSP) tree, is a data structure which subdivides space into convex subspaces by using hyperplanes~\cite{original}. This data structure can be seen as a generalization of a \textit{k}-d tree which, unlike a \textit{k}-d tree, the orientation and position of the spiting planes are arbitrary.

In a BSP tree, each inner node represents an hyperplane that splits the space into two half-spaces. The right child represents the positive half-space, while the left child represents negative half-space. The leaf nodes, unlike the inner nodes, nodes does contain any information about the hyperplane and represent a convex subspace.

\begin{figure}[h]
\includegraphics[width=0.9\linewidth]{bsp_tree.png}
\caption{Example of a BSP tree.}
\end{figure}

Because of the freedom of choosing the position and orientation of the hyperplanes, a common approach is to use the planes defined by the input data. Given a set of polygons as the input data, when all the hyperplanes are polygons from the input data, it is called an auto-partition BSP tree.

In the computer graphics field, BSP trees are used in multiple applications. The most common applications are:

\begin{description}
\item[Constructive solid geometry] Auto-partition BSP trees allow to represent volumetric polygonal closed objects by defining if a space space region is inside or outside the object. This representation also allows to check very efficiently if a point belongs inside or outside the object. Figure~\ref{object} shows an example of a 2D BSP tree representing an object.

\item[Boolean operations] Another interesting property of an object represented by a BSP tree is the ability to perform very efficiently boolean operation between objects. Given two objects, $A$ and $B$, being able to perform the boolean operations $\{\cup, \cap, \setminus, \ominus\}$. As a simple overview, the main idea behind this operations is to split one of the two BSP tree by the planes of the other BSP tree, and merging them with the corresponding boolean operation.

\item[Visibility priority] When rendering the polygons on the screen, an auto-partition BSP tree can be used to determine the drawing priority of those polygons~\cite{siggraph}.
\end{description}

\begin{figure}
\includegraphics[width=\linewidth]{bsp_tree_object.png}
\caption{Auto-partition BSP tree which represents a closed object.}
\label{object}
\end{figure}

\subsection*{Visibility priority}
In computer graphics, the visibility problem of a 3D scene consists on defining which parts of the geometry are going to be visible on screen. The painter's algorithm allows to solve this problem by painting those polygons from back to front by overdrawing those portions of the screen which are occluded by the polygons painted on top.

In static scenes, an auto-partition BSP tree can be used to solve this problem since it stores implicitly the polygon painting order from any view point.

\subsubsection*{Generation}
Given a set of polygons as the input data, to generate an auto-partition BSP the following algorithm is performed:
\begin{enumerate}
\item A polygon is chosen from the list.
\item For each other polygon in the list:
\begin{enumerate}
\item If the polygon is completely in front of the hyperplane, it is added to the right child.
\item If the polygon is completely behind the hyperplane, it is added to the left child
\item If the polygon intersects with the hyperplane, the polygon is split and each new fragment is added to the corresponding child.
\item If the polygon lies in the plane, is added tot the list of polygons of that node.
\end{enumerate}
\item This algorithm is applied recursively to each child until there is only one polygon on each node.
\end{enumerate}

It is important to note that the selection of the polygon which specifies the hyperplane (step~1) is going to impact the number of fragments generated down the tree. There exist different heuristics which try to minimize the number of splits.

\subsubsection*{Image generation}
Once the BSP tree has been generated, the tree can be traversed in linear time in order to determine the visibility priorities of each polygon.

From the root node:
\begin{enumerate}
\item If the view location is in front of the hyperplane
\begin{itemize}
\item Paint the left child (behind) polygons recursively.
\item Paint the current node polygons.
\item Paint the right child (int front) polygons recursively.
\end{itemize}
\item If the view location is behind the the hyperplane
\begin{itemize}
\item Paint the right child (int front) polygons recursively.
\item Paint the current node polygons.
\item Paint the left child (behind) polygons recursively.
\end{itemize}
\end{enumerate}

To check if the view is in front or behind the plane, is just a matter com computing the singed distance of a point with respect of the hyperplane.

\section*{Personal opinion}
Nowadays the BSP trees are not that relevant in the computer graphics field and seams to be taken over by other more advanced data structures. This is obvious in the case of the visibility problem. Nowadays the default solution to this problem is the use of the Z-Buffer. This alternative is not a data structure by itself, but instead is a hardware accelerated GPU buffer. This GPU buffer allows to perform the priority sorting directly on the GPU, and at pixel level, with a minimum performance penalty.

Also, in the case of solid modeling, the rendering of those objects does not require them to be represented by BSP trees. Like with the visibility problem, this rendering can be perform with  object primitives and the help of another hardware buffer called stencil buffer.

\section*{Experiments}
One of the main problem with BSP trees is the size of the tree. In the case of a BSP tree constructed from a set of polygons, this size of the tree can be much larger than the number of polygons $n$ due to the hyperplanes splitting those polygons. While the number of fragments in an 2D auto-partition BSP tree is bounded $O(n^2 \log n)$, in higher dimensions the known bound is much wider. In the case of 3D BSP trees they can be as many as $O(n^2)$ fragments.

Even thought there can be as many as a quadratic number of fragments, the reality is that, due to the principle of locality, BSP trees contain much fever fragments. The principle of locality states that polygons are smaller compared to the whole space.

The implemented experiment tries to show this reality by constructing multiple BSP trees and comparing the number of polygons with respect to the number of fragments. The BSP trees generated are auto-partitions BSP trees from real models and from a set of random polygons.

As we can see in the results (Table~\ref{results}) we can see that in reality, the number of fragments is far from the upper bound. Also this experiment shows the principle of locality as we can see that, when using random polygons, the number of fragments increase with respect to the models. This is due to the models being composed by small polygons, while the size of the random polygons is variable.

\begin{table}
\centering
\begin{tabular}{|c|c|}
\hline
\textbf{Polygons} & $\boldsymbol{Fragments/Polygons^2}$ \\
\hline\hline
Monkey & 0.004\% \\
Sphere & 0.028\% \\
Teapod & 0.017\% \\
Random & 1.633\% \\
\hline
\end{tabular}
\caption{Table showing the number of fragments generated by the BSP tree.}
\label{results}
\end{table}

\section*{Conclusions}
Even being forgotten nowadays, BSP trees are an interesting very data structure which still are being used. It seams that one of the causes of its decrease in popularity is that many applications now uses more advanced an efficient techniques which allow to parallelize the computation (e.g. Z-Buffer for the visibility problem).

\bibliographystyle{unsrt}
\bibliography{report.bib}

\end{document}


\chapter{Neuron Update Optimisation}

\section{Initial Method}
Originally, each update iteration of the Hopfield network involved scanning through all neurons and computing the $h$ value. As this involves scanning through all the neurons in every step, it is clearly an $O(n)$ operation.

\section{Optimisation}
First we shuffle the list of neurons\footnote{Although this is an extra $O(n)$ operation, it has a negligible footprint to computing the $h$ value}. We then select the first neuron. If it is updatable then we are done. If not then continue with the second neuron, and so on. In the worst case, we will perform as bad as the initial method described above. In general, however, we will always beat or match the above algorithm as we avoid computing the $h$ value for every node.

At the beginning of the convergence process, the patterns tend to undergo a lot of transformations, and have a large number of updatable neurons. Depending on the size of the basin of attraction in which the pattern will land, and on the journey the pattern has to go trough until it reaches the basin, the number of updatable neurons during the journey varies greatly.

As a pattern approaches convergence, the number of updatable neurons decreases (as for a fixed point, there are no updatable neurons) so we slightly reach the old performance. In any case our algorithm is still at least as good as the initial method.


% !TEX root = main.tex

\chapter{Cauchy's Theorem}
\section{The Estimation Lemma}
The Estimation Lemma is an extremely important result that will be used to prove a number of important Theorems in subsequent sections.  For this reason, it has been promoted to the rank of `Theorem.'

\begin{theorem}[The Estimation Lemma]
Let $f$ be a function which is continuous along a smooth path $\Gamma$ given by the function $\gamma : [a,b] \to \C$, then
\[
\abs{ \int_{\Gamma} f } \leq \int_a^b \abs{ f(\gamma(t))} \abs{ \gamma' (t) }\ dt,
\]
 and if there exists a real number $M>0$ with $\abs{f(z)} \leq M$ for all $z \in \Gamma$,
\[
\abs{\int_{\Gamma} f} \leq ML,
\]
where $L$ is the length of $\Gamma$.
\end{theorem}
\begin{exercise}
Use the following steps to prove this Theorem.
\begin{enumerate}
\item[(i)]  Define a new function $g:[a,b] \to \C$ via $\displaystyle g(t)=f(\gamma(t))\gamma' (t)$.  Explain why the result is trivial if $\displaystyle \int_a^b g(t)\ dt =0$.

\item[(ii)] Assuming $\displaystyle \int_a^b g(t)\ dt \neq 0$, define $\lambda$ via
\[
\lambda = \frac{\abs{\int_a^b g(t)\ dt}}{\int_a^b g(t)\ dt}.
\]
Show that $\abs{\lambda}=1$ and that
\[
\abs{\int_a^b g(t)\ dt } = \int_a^b \Re \brac{ \lambda g(t) }\ dt.
\]
\item[(iii)] Show that $\Re \brac{ \lambda g(t) } \leq \abs{g(t)}$ for all $t \in [a,b]$.
\item[(iv)] Deduce the result from parts (ii) and (iii).
\end{enumerate}
\end{exercise}

\begin{proof}
By definition of the integral of $f$ along $\Gamma$, we have
\[
\abs{ \int_{\Gamma} f } = \abs{ \int_a^b f \left( \gamma(t) \right) \gamma' (t)\ dt },
\]
and thus we need to show that
\[
\abs{ \int_a^b f \left( \gamma (t) \right) \gamma'(t)\ dt } \leq \int_a^b \abs{ f \left( \gamma (t) \right) }\ \abs{ \gamma'(t) }\ dt.
\]
Let $g(t)=f(\gamma(t))\gamma'(t)$, so that we need to show
\[
\abs{\int_a^b g(t)\ dt } \leq \int_a^b \abs{g(t)}\ dt.
\]
Note that if $\int_a^b g(t) = 0$, then the inequality holds trivially.  If not, let
\[
\lambda = \frac{\abs{\int_a^b g(t)\ dt}}{\int_a^b g(t)\ dt},
\]
and note that $\abs{\lambda} = 1$ and
\[
\lambda \int_a^b g(t)\ dt = \abs{ \int_a^b g(t)\ dt }.
\]
It follows that
\begin{align*}
\abs{\int_a^b g(t)\ dt } &= \int_a^b \lambda g(t)\ dt  \\
& = \int_a^b \Re \left( \lambda g(t) \right)\ dt + i \int_a^b \Im \left( \lambda g(t) \right)\ dt.
\end{align*}
Since the modulus is always real, we must have
\[
\int_a^b \Im \left( \lambda g(t) \right)\ dt =0
\]
and so
\[
\abs{\int_a^b g(t)\ dt } = \int_a^b \Re \left( \lambda g(t) \right)\ dt.
\]
Now, since $\Re (z) \leq \abs{z}$ for all $z \in \C$, we have
\[
\Re \left( \lambda g(t) \right) \leq \abs{\lambda g(t) } = \abs{\lambda} \abs{g(t)} = \abs{g(t)}.
\]
Together with Monotonicity of the real integral (That is to say, if $\phi_1,\phi_2:[a,b] \to \R $ with $\phi_1(t) \leq \phi_2 (t)$ for all $t$, we have $\int_a^b \phi_1(t)\ dt \leq \int_a^b \phi_2 (t)\ dt$ (Theorem~\ref{t:realint})), we have
\begin{align*}
\abs{ \int_a^b g(t)\ dt } =&  \int_a^b \Re \left( \lambda g(t) \right)\ dt \\
& \leq \int_a^b \abs{g(t)}\ dt.
\end{align*}
In other words, we have shown that

\[
\abs{\int_{\Gamma} f } = \abs{ \int_a^b f \left( \gamma (t) \right) \gamma'(t)\ dt } \leq \int_a^b \abs{ f \left( \gamma (t) \right) }\ \abs{ \gamma'(t) }\ dt.
\]
  For the second part, if there is some $M>0$ with $\abs{f(z)} \leq M$ for all $z \in \Gamma$, then monotonicity and linearity of the real integral gives us
  \begin{align*}
  \abs{\int_{\Gamma} f } & \leq \int_a^b M \abs{ \gamma ' (t) } \ dt \\
  & = M \int_a^b \abs{\gamma ' (t)}\ dt \\
  & = M L,
  \end{align*}
where the final equality follows from the definition of the length of a smooth path.
\end{proof}
It follows easily from the definition of the integral of $f$ along a contour $\mathcal{C}=\Gamma_1 + \ldots + \Gamma_n$ that
\[
\abs{\contint f} \leq M L
\]
where $\abs{f(z)} \leq M$ for all $z \in \mathcal{C}$ and $L$ is the length of $\mathcal{C}$.
\begin{note}
If we can show that
\[
\abs{ \contint f } \leq K,
\]
then $K$ is called an \emph{upper estimate} for $\displaystyle \contint f$
\end{note}
\begin{example}
\label{e:estimation}
The function $f$ is defined by
\[
f(z) = \frac{1}{1+z^2}
\]
and $\Gamma$ is described by \[ \gamma:[0,\pi] \to \C, \quad \gamma(t) = r \cos (t) +i r \sin (t), \]
where $r>1$.  We shall find an upper estimate for $\int_{\Gamma} f$ and show that
\[
\int_{\Gamma} f \to 0 \text{ as } r \to \infty.
\]
\end{example}
\begin{solution}
The domain of $f$ is $\C \backslash \set{i,-i}$, as $f$ is defined and continuous everywhere except where $1+z^2=0$.  As $\Gamma$ consists of part of the circle with centre $0$ and radius $r>1$, $\Gamma$ does not contain $i$ or $-i$.

Now, for any $z \in \Gamma$, the Backwards Triangle Inequality gives
\[
\abs{1+z^2} \geq \abs{\abs{1}-\abs{z^2}} = r^2-1
\]
since $\abs{z}=r>1$ for all $z \in \Gamma$.  Thus
\[
\abs{f(z)} = \abs{\frac{1}{1+z^2}} \leq \frac{1}{r^2-1}
\]
for all $z \in \Gamma$.

Setting $M = \frac{1}{r^2-1}$, we have $\abs{f(z)} \leq M$ for all $z \in \Gamma$, and since $\Gamma$ is a semicircle, the length $L$ of $\Gamma$ is equal to $\pi r$.  Thus the estimation Lemma gives the upper estimate
\[
\abs{ \int_{\Gamma} f } \leq ML = \frac{\pi r}{r^2-1}
\]
for this integral.

If we rewrite the above inequality as
\[
\abs{ \int_{\Gamma} f } \leq \frac{\pi}{r-\frac{1}{r}},
\]
we see that
\[
\abs{\int_{\Gamma} f } \to 0 \text{ as } r \to \infty
\]
and hence
\[
 \int_{\Gamma} f \to 0 \text{ as } r \to \infty.
\]

\end{solution}
\section{Cauchy's Theorem for a Triangle}
\begin{definition}
A region $\mathcal{R}$ is called \emph{simply connected} if given any closed contour $\mathcal{C}$ in $\mathcal{R}$, all points enclosed by $\mathcal{C}$ also belong to $\mathcal{R}$.
\end{definition}
It is surprisingly difficult to write down the precise definition of a point being enclosed by a contour $\mathcal{C}$, thus we shall treat this notion informally and avoid complicated cases.  Essentially, a simply connected region cannot have any `holes.'

\begin{center}
\begin{tabular}{cc}
\altgraphics[scale=0.6]{ch4_upperhalf_full}{ch4_upperhalf} & \altgraphics[scale=0.6]{ch4_cless0_full}{ch4_cless0}
\end{tabular}
\vspace{1cm}
\begin{tabular}{cc}
\altgraphics[scale=0.4]{ch4_notsc1_full}{ch4_notsc1} & \altgraphics[scale=0.4]{ch4_notsc2_full}{ch4_notsc2}
\end{tabular}
\end{center}

%\hspace{-2cm}
%\altgraphics[scale=0.6]{sc_full}
%\end{center}
%\end{full}
%\begin{student}
%\begin{center}
%\includegraphics[scale=1]{upperhalf}
%\includegraphics[scale=1]{hplus}
%\vspace*{1cm}
%\includegraphics[scale=1]{sc1}
%\hspace{1cm}
%\includegraphics[scale=1]{sc2}
%\end{center}
%\vspace*{2cm}
%\end{student}
In the proof of Cauchy's Theorem we will need the following fact about the integral of a continuous function $f$ along a smooth path $\Gamma$: if $\tilde{\Gamma}$ denotes the reverse of $\Gamma$ then
\[
\int_{\tilde{\Gamma}} f = - \int_{\Gamma} f.
\]
We shall also fix the following notation: $T(z_1,z_2,z_3)$ denotes the triangle with vertices $z_1,z_2,z_3$, and hence with edges given by the line segments $[z_1,z_2],[z_2,z_3],[z_3,z_1]$.  The boundary of $T$ is denoted by $\partial T$, which defines a closed contour
\[
\partial T = [z_1,z_2] + [z_2,z_3] + [z_3,z_1].
\]
\begin{theorem}[Cauchy's Theorem for a Triangle]
\label{t:cauchyt} Let $f$ be a function that is holomorphic on a simply connected region $\mathcal{R}$ and let $T(z_1,z_2,z_3)$ be a triangle in $\mathcal{R}$ with boundary $\partial T$.  Then
\[
\int_{\partial T} f = 0.
\]
\end{theorem}
\begin{center}
\includegraphics[scale=0.4]{ch4_cauchyt_full}
\end{center}
Note that if we knew that $f$ were to have an antiderivative $F$ on $\mathcal{R}$, then Theorem~\ref{t:closed} would show that
\[
\int_{\partial T} f =0.
\]
Later we will see that in fact $f$ does have an antiderivative on $\mathcal{R}$, but the proof will rely Cauchy's Theorem for a triangle - thus we need to prove Theorem~\ref{t:cauchyt} without this assumption.

Before proving Theorem~\ref{t:cauchyt}, we need the following lemma:
\begin{lemma}
\label{l:cauchyt}
Let $f$ be a function that is holomorphic on a simply connected region $\mathcal{R}$ and let $T(z_1,z_2,z_3)$ be a triangle in $\mathcal{R}$.  Then there exists a nested sequence of triangles $T \supseteq T_1 \supseteq T_2 \supseteq \ldots \supseteq T_n \supseteq \ldots$ with the property that
\[
\frac{1}{4^n} \abs{ \int_{\partial T} f } \leq \abs{ \int_{\partial T_n} f } \text{ and } \ell ( \partial T_n) = \frac{1}{2^n} \ell ( \partial T)
\]
for all $n$. Moreover, there exists a point $z_0 \in \mathcal{R}$ with $z_0 \in T_n$ for all $n$.
\end{lemma}
{\bf Proof }
 Using the midpoints of the sides of $T$, construct four similar triangles $T^{(1)},T^{(2)},T^{(3)}$ and $T^{(4)}$ as shown.

\begin{blankbox}
%\vspace*{2cm}
\begin{center}
\altgraphics[scale=0.6]{ch4_cauchyt2_full}{ch4_cauchyt2}
\end{center}
%\vspace*{2cm}
Then for $j=1,2,3,4$ we have $\ell(\partial T^{(j)}) = \frac{1}{2} \ell ( \partial T)$ by construction.

Note that with all contours taken anticlockwise,
\[
\int_{\partial T^{(1)}} f + \int_{\partial T^{(2)} } f + \int_{\partial T^{(3)}} f + \int_{\partial T^{(4)}} f = \int_{\partial T} f,
\]

as the integrals along the `interior' edges of the $T^{(j)}$ cancel in pairs (this uses $\int_{\tilde{\Gamma}} f = - \int_{\Gamma} f$, where $\tilde{\Gamma}$ is the reverse of $\Gamma$ ):
\begin{center}
\altgraphics[scale=0.6]{ch4_cauchyt3_full}{ch4_cauchyt3}
\end{center}
\end{blankbox}
Moreover, by the triangle inequality
\[
\abs{\int_{\partial T} f} \leq \abs{ \int_{\partial T^{(1)}} f} + \abs{\int_{\partial T^{(2)} } f }+ \abs{\int_{\partial T^{(3)}} f }+ \abs{\int_{\partial T^{(4)}} f }.
\]
%\vspace*{5cm}
\begin{blankbox}
At least one of these four integrals has modulus greater than or equal to the other three; call the corresponding triangle $T_1$.  Then
\[
\abs{\int_{\partial T} f} \leq \abs{ \int_{\partial T^{(1)}} f} + \abs{\int_{\partial T^{(2)} } f }+ \abs{\int_{\partial T^{(3)}} f }+ \abs{\int_{\partial T^{(4)}} f } \leq 4 \abs{ \int_{\partial T_1} f},
\]
and so
\[
\frac{1}{4} \abs{ \int_{\partial T } f } \leq \abs{\int_{\partial T_1} f}.
\]
\end{blankbox}
Now, use the midpoints of the edges of $T_1$ to get four more triangles, one of which is a triangle $T_2$ satisfying
\[
\frac{1}{4} \abs{ \int_{\partial T_1} f } \leq \abs{ \int_{\partial T_2} f } \quad\text{and}\quad \ell ( \partial T_2) = \frac{1}{2} \ell ( \partial T_1) = \frac{1}{2^2} \ell(\partial T),
\]

\begin{center}
\altgraphics[scale=0.6]{ch4_cauchyt4_full}{ch4_cauchyt4}
\end{center}

\begin{blankbox}
Thus
\[
\frac{1}{4^2} \abs{ \int_{\partial T} f } \leq \abs{ \int_{\partial T_2} f }.
\]

Continuing in this way we get $T\supseteq T_1 \supseteq T_2 \supseteq \ldots \supseteq T_n \supseteq \ldots$ with
\[
\frac{1}{4^n} \abs{ \int_{\partial T} f} \leq \abs{ \int_{\partial T_n} f }\quad\text{and}\quad \ell(\partial T_n) = \frac{1}{2^n} \ell ( \partial T)
\]
for each $n$.
\end{blankbox}
\begin{center}
\includegraphics[scale=0.6]{ch4_cauchyt5}
\end{center}

As the sequence is nested, there exists a point $z_0$ with $z_0 \in T_n$ for all $n$, and since $\mathcal{R}$ is simply connected, $z_0 \in \mathcal{R}$.
\qed

We are now in a position to prove Cauchy's Theorem for a Triangle.
\begin{exercise}
Try to prove Cauchy's Theorem for a Triangle, using the following steps.
\begin{enumerate}
\item[(i)] Let $\epsilon >0$ be given.  Use the fact that $f$ is differentiable at $z_0$ to show that there exists $\delta >0$ such that
\[
z \in \mathcal{R},\  \abs{z-z_0} < \delta \Rightarrow \abs{f(z)-f(z_0)-(z-z_0)f'(z_0)} \leq \epsilon \abs{z-z_0}.
\]
\item[(ii)] With $\epsilon$ and $\delta$ as chosen in the previous step, show that there exists $n \in \mathbb{N}$ such that
\[
z \in \partial T_n \Rightarrow \abs{f(z)-f(z_0)-(z-z_0)f'(z_0)} < \epsilon \ell( \partial T_n).
\]
As a hint, first explain why we can choose $n$ with $\ell ( \partial T_n) < \delta$.
\item[(iii)] Show that for any constants $\alpha,\beta \in \C$ we have
\[
\int_{\partial T_n} (\alpha + \beta (z-z_0) )\ dz = 0,
\]
and deduce that
\[
\int_{\partial T_n} f = \int_{\partial T_n} (f(z)-f(z_0)-f'(z_0)(z-z_0))\ dz.
\]
\item[(iv)] Use step (iii) and the Estimation Lemma to show that
\[
\abs{\int_{\partial T_n} f } \leq \epsilon \cdot \frac{1}{4^n} \cdot \brac{\ell(\partial T) }^2.
\]
\item[(v)] Complete the proof.
\end{enumerate}
\end{exercise}
\begin{proof}[of Cauchy's Theorem for a Triangle (Theorem~\ref{t:cauchyt})]
We are going to show that given any $\epsilon>0$ we have
\[
\abs{ \int_{\partial T} f } \leq \epsilon [\ell (\partial T)]^2
\]
where $\ell ( \partial T )$ is the length of the boundary of the triangle $T$.

Let $\set{ T_n}$ be the nested sequence of triangles constructed in Lemma~\ref{l:cauchyt} and $z_0 \in \mathcal{R}$ such that $z_0 \in T_n$ for all $n$.

\emph{Step 1:}    Let $\epsilon>0$ be given.  We will show that there exists $\delta>0$ such that
\[
\abs{z-z_0}<\delta \Longrightarrow \abs{f(z)-f(z_0)-(z-z_0)f'(z_0)} \leq \epsilon \abs{z-z_0}.
\]

  Since $f$ is holomorphic on $\mathcal{R}$ it is differentiable at $z_0$ with derivative $f'(z_0)$, or in other words
  \[
  \lim_{h \to 0} \frac{f(z_0+h)-f(z_0)}{h} = f'(z_0).
  \]

  This means that there is some $\delta>0$ such that
  \begin{align*}
  0 < \abs{h} < \delta &\Longrightarrow \abs{ \frac{f(z_0+h)-f(z_0)}{h} - f'(z_0) } < \epsilon \\
  \shortintertext{ or in other words, writing $z=z_0+h$,}
  0 < \abs{z-z_0} < \delta &\Longrightarrow \abs{ \frac{f(z)-f(z_0)}{z-z_0} - f'(z_0) } < \epsilon \\
  & \Longrightarrow \abs{ \frac{f(z)-f(z_0)-(z-z_0)f'(z_0)}{z-z_0} } < \epsilon.
  \end{align*}
Thus
\[
0 < \abs{z-z_0} < \delta \Longrightarrow \abs{f(z)-f(z_0)-(z-z_0)f'(z_0)} < \epsilon \abs{z-z_0}.
\]
If we allow $z=z_0$, this becomes
\[
\abs{z-z_0} < \delta \Longrightarrow \abs{f(z)-f(z_0)-(z-z_0)f'(z_0)} \leq \epsilon \abs{z-z_0}
\]
(since when $z=z_0$, both sides are zero).

\emph{Step 2: } With $\epsilon>0$ as before, and $\delta>0$ as chosen in the previous step, we choose $n$ large enough so that $\ell ( \partial T_n ) < \delta$, which can always be done as $\ell ( \partial T_n ) = \frac{1}{2^n} \ell ( \partial T)$ (note that $n$ depends on $\delta$, which in turn depends on $\epsilon$).

We will show that for $z \in \partial T_n$ we have
\[
\abs{f(z)-f(z_0)-(z-z_0)f'(z_0)}<\epsilon \ell ( \partial T_n).
\]

If $z \in \partial T_n$, then the distance from $z$ to $z_0$ can be no more than the length of the longest edge of $T_n$, which is less than $\ell ( \partial T_n)$.
%\vspace*{5cm}
\begin{center}
\includegraphics[scale=0.6]{ch4_cauchyt6_full}
\end{center}
%\vspace*{7cm}
Thus
\[
z \in \partial T_n \Longrightarrow \abs{z-z_0} \underbrace{< \ell (\partial T_n) }_{\text{geometry}} \underbrace{< \delta}_{\text{choice of }n},
\]
and hence $T_n \subset D(z_0,\delta)$.

Thus for any $n$ large enough so that $\ell ( \partial T_n) < \delta$, we have
\begin{align*}
z \in \partial T_n \Longrightarrow \abs{f(z)-f(z_0)-(z-z_0)f'(z_0)} &\leq \epsilon \abs{z-z_0} \\
& < \epsilon \ell ( \partial T_n).
\end{align*}
\begin{center}
\includegraphics[scale=1]{ch4_cauchyt7}
\end{center}

\emph{Step 3}: We use the Estimation Lemma to find an upper estimate for $\int_{\partial T_n} f$, where $n$ was chosen in the previous step.

Note that
\[
\int_{\partial T_n} (\alpha+\beta(z-z_0)) dz =0 \text{ for }\alpha,\beta \in \C,
\]
because $z \mapsto \alpha + \beta (z-z_0)$ has an antiderivative $z \mapsto \alpha z + \frac{\beta(z-z_0)^2}{2}$.

This means that
\[
\int_{\partial T_n} {\Big(} \underbrace{f(z_0)}_{\alpha}+\underbrace{f'(z_0)}_{\beta} (z-z_0) {\Big)} dz =0,
\]
and so
\[
\int_{\partial T_n} f = \int_{\partial T_n} {\Big(} f(z)-\underbrace{f(z_0)-f'(z_0)(z-z_0)}_{\text{integral }0\text{ along } \partial T_n} {\Big)} dz
\]
We shall use the Estimation Lemma to find an upper estimate for this integral.  Indeed, we have
\begin{align*}
\abs{\int_{\partial T_n} f} & = \abs{\int_{\partial T_n} \left[ f(z)-f(z_0)-f'(z_0)(z-z_0) \right]\ dz} &&\\
& \leq \epsilon \ell ( \partial T_n ) \ell ( \partial T_n) &&\text{ By step 3} \\
& = \epsilon \left( \frac{1}{2^n} \ell ( \partial T ) \right) \left( \frac{1}{2^n} \ell ( \partial T) \right) && \text{ as } \ell (\partial T_n) = \frac{1}{2^n} \ell ( \partial T) \\
& = \epsilon \frac{1}{4^n} \left[ \ell ( \partial T) \right]^2.
\end{align*}

\emph{Step 4:} Completing the proof.

By Step 3, given any $\epsilon>0$, there is $n$ such that
\[
\abs{ \int_{\partial T_n} f } \leq \epsilon \frac{1}{4^n} \left[ \ell ( \partial T) \right]^2.
\]

In Lemma~\ref{l:cauchyt}, we showed that
\[
\frac{1}{4^n} \abs{ \int_{\partial T} f } \leq \abs{ \int_{\partial T_n} f},
\]
and so combining these facts, we have
\[
\abs{ \int_{\partial T} f } \leq \epsilon \left[ \ell ( \partial T) \right]^2
\]
for all $\epsilon > 0$.  Because $\epsilon>0$ was arbitrary and $\ell ( \partial T)$ is fixed, we get
\[
\abs{ \int_{\partial T} f } = 0, \text{ hence } \int_{\partial T} f =0.
\]

\end{proof}

\begin{example}
Let $f:\C \to \C$ be defined by $f(z)=\sin \left( \exp \left( \cos \left( z^3-4z^2+i \right) \right) \right)$, which is holomorphic on $\C$.  There is no obvious antiderivative for $f$, but nonetheless, Cauchy's Theorem shows us that for any triangle $T$ we have
\[
\int_{\partial T} f = 0.
\]
\end{example}
\begin{example}
Let $f(z) = \dfrac{1}{z}$, which is holomorphic on $\C \backslash \set{0}$.  We shall evaluate $\int_{\partial T} f$ for different triangles $T$: let $T_1$ be a triangle enclosing the origin, and $T_2$ a triangle lying to the left of the imaginary axis as shown.
\begin{center}
\begin{tabular}{cc}
\includegraphics[scale=1]{ch4_cauchytexample1} & \includegraphics[scale=1]{ch4_cauchytexample2}
\end{tabular}
\end{center}
\end{example}
\begin{solution}
Since $\C \backslash \set{0}$ is not simply connected, Cauchy's Theorem for a triangle tells us nothing about
\[
\int_{\partial T_1} \frac{1}{z}\ dz.
\]
However, $\C \backslash \set{0}$ has simply connected subregions, for example, the left-half plane
\[
\mathcal{L} = \set{ z \in \C : \Re (z) < 0 }.
\]

The triangle $T_2$ is contained in $\mathcal{L}$, so Cauchy's Theorem for a triangle gives
\[
\int_{\partial T_2} \frac{1}{z}\ dz =0.
\]
In fact, if $T$ is any triangle in $\C \backslash \set{0}$ that does not enclose the origin, we can find a simply connected subregion of $\C \backslash \set{0}$ that contains $T$, and conclude that
\[
\int_{\partial T} \frac{1}{z}\ dz =0.
\]
\end{solution}
%\newpage
%\begin{absolutelynopagebreak}
\section{Cauchy's Theorem for Starlit Regions}

\begin{definition}
A region $\mathcal{R}$ is called \emph{starlit} if there is a point $z_{\ast} \in \mathcal{R}$ such that for any $z \in \mathcal{R}$ the line segment $[z_{\ast},z]$ lies inside $\mathcal{R}$.  The point $z_{\ast}$ is called a \emph{star centre} for $\mathcal{R}$.
\end{definition}

The name starlit comes from the idea that the `rays of light' from the `star' $z_{\ast}$ fall on every point of $\mathcal{R}$.
%\begin{student}
\begin{center}
\altgraphics[scale=1.5]{ch4_starlit1_full}{ch4_starlit1} \\
\blank{The region $\mathcal{R}_1$ on the left is starlit, as any point $z \in \mathcal{R}_1$ can be connected to $z_*$ using the line segment $[z_*,z]$.  The region $\mathcal{R}_2$ on the right is not, since no matter which point we choose as $z_*$, the line segment joining $z_*$ to the point opposite it leaves $\mathcal{R}_2$.}
\end{center}
\begin{tabular}{c m{0.5\textwidth} }
\begin{minipage}{0.5\textwidth}
\centering
\altgraphics[scale=1.5]{ch4_starlit2_full}{ch4_starlit2}
\end{minipage} & \blank{The region $\C \backslash \set{0}$ is not starlit; indeed if $z_*$ were a star centre, then the line segment $[z_*,-z_*]$ would be contained in $\C \backslash \set{0}$, which is not the case for any choice of $z_*$.}
\end{tabular}
\bigskip

\begin{theorem}[The Existence of Antiderivatives on Starlit Regions]
\label{t:starlit}
Let $f$ be holomorphic on a starlit region $\mathcal{R}$ with star centre $z_{\ast} \in \mathcal{R}$.  Then the function $F:\mathcal{R} \to \C$ defined by
\[
F(z) = \int_{[z_{\ast},z]} f
\]
is an antiderivative for $f$ on $\mathcal{R}$.
\end{theorem}

\leftimage{
\includegraphics[scale=0.75]{ch4_starlitantid}
}
{
Since $\mathcal{R}$ is starlit, $[z_*,z]$ is contained in $\mathcal{R}$ and so $\int_{[z_*,z]} f$ is well-defined for all $ z \in \mathcal{R}$.
}

\begin{exercise}
Use the following steps to prove this Theorem.
\begin{enumerate}
\item[(i)] Use the definition of $F$, together with Cauchy's Theorem for a Triangle, to show that for $h \in \C$,
\[
F(z_0+h)-F(z_0) = \int_{[z_0,z_0+h]} f.
\]
\item[(ii)] Show that
\[
\frac{F(z_0+h)-F(z_0)}{h} - f(z_0) = \frac{1}{h} \int_{[z_0,z_0+h]} (f(z)-f(z_0)\ dz.
\]
\item[(iii)] Let $\epsilon >0$ be given.  Use the Estimation Lemma to show that there exist $\delta >0$ such that
\[
\abs{h} < \delta \Rightarrow \abs{\int_{[z_0,z_0+h]} \brac{f(z)-f(z_0)}\ dz} \leq \epsilon \abs{h}.
\]
(Hint: you will need to use the fact that differentiability implies continuity).
\item[(v)] Combine steps (ii) and (iii) to complete the proof.
\end{enumerate}
\end{exercise}

\begin{proof}
We need to show that for any $z_0 \in \mathcal{R}$, $F'(z_0)$ exists and is equal to $f(z_0)$, or in other words
\[
\lim_{h \to 0} \frac{F(z_0+h)-F(z_0)}{h} = f(z_0).
\]
  Write
\[
\frac{F(z_0+h)-F(z_0)}{h} = \frac{1}{h} \int_{[z_{\ast},z_0+h]} f - \frac{1}{h} \int_{[z_{\ast},z_0]} f.
\]
Since $\mathcal{R}$ is open, for sufficiently small $h$, the triangle $T(z_{\ast},z_0+h,z_0)$ is contained in $\mathcal{R}$.
\begin{center}
\includegraphics[scale=1]{ch4_starlittriangle}
\end{center}

  Cauchy's Theorem for a Triangle gives
\[
\int_{[z_*,z_0+h]} f + \int_{[z_0+h,z_0]} f + \int_{[z_0,z_*]} f = 0,
\]
hence
\[
\int_{[z_*,z_0+h]}f - \int_{[z_*,z_0]}f = - \int_{[z_0+h,z_0]} f = \int_{[z_0,z_0+h]} f.
\]
This gives
\[
\frac{F(z_0+h)-F(z_0)}{h} = \frac{1}{h} \int_{[z_0,z_0+h]} f.
\]
We now use the fact that
\[
\int_{[z_0,z_0+h]} 1 dz = h,
\]
which implies that
\[
f(z_0) = f(z_0) \cdot \frac{1}{h} \cdot h = f(z_0) \frac{1}{h} \int_{[z_0,z_0+h]} 1 dz = \frac{1}{h} \int_{[z_0,z_0+h]} f(z_0)\ dz
\]
(since $f(z_0)$ is a constant).  Combining this with the previous step, we see that
%\newpage
%\vspace*{5cm}
%\newpage
\begin{align*}
 \frac{F(z_0+h)-F(z_0)}{h}-f(z_0)  &= \frac{1}{h} \left[ \int_{[z_0,z_0+h]} f(z)\ dz - \int_{[z_0,z_0+h]} f(z_0)\ dz \right] \\
& = \frac{1}{h} \int_{[z_0,z_0+h]} (f(z)-f(z_0))\ dz
\end{align*}
(since both integrals are along the same path, we can combine them).
%\begin{absolutelynopagebreak}
Hence
\[
\abs{ \frac{F(z_0+h)-F(z_0)}{h}-f(z_0) } = \frac{1}{\abs{h}} \abs{ \int_{[z_0,z_0+h]} (f(z)-f(z_0)) dz }
\]
Now we shall use the Estimation Lemma to obtain an upper estimate for this integral.  It is easy to see that $\ell([z_0,z_0+h]) = \abs{h}$, thus we must find an upper bound for $\abs{f(z)-f(z_0)}$ when $z \in [z_0,z_0+h]$.
%\end{absolutelynopagebreak}

We use the fact that $f$ holomorphic on $\mathcal{R}$ implies $f$ continuous on $\mathcal{R}$, and in particular, continuous at $z_0$.  Hence given any $\epsilon >0$ there is some $\delta >0$ such that
\[
0< \abs{z-z_0} < \delta \Longrightarrow \abs{f(z)-f(z_0)} < \epsilon.
\]
Thus if $\abs{h} < \delta$ then
\begin{align*}
z \in [z_0,z_0+h] & \Longrightarrow \abs{z-z_0} < \abs{h} < \delta \\
& \Longrightarrow \abs{f(z)-f(z_0)} < \epsilon.
\end{align*}
%\newpage
%\vspace*{25cm}
In other words, once $\abs{h}<\delta$, $\epsilon$ is an upper bound for $\abs{f(z)-f(z_0)}$ for $z \in [z_0,z_0+h]$.  Thus the Estimation Lemma gives the upper estimate
\[
\abs{ \int_{[z_0,z_0+h]} \left( f(z)-f(z_0) \right)\ dz } \leq \epsilon \abs{h}
\]
whenever $\abs{h} < \delta$.

Thus given any $\epsilon>0$ there is $\delta>0$ such that
\begin{align*}
0 < \abs{h} < \delta \Longrightarrow
\abs{ \frac{F(z_0+h)-F(z_0)}{h}-f(z_0) } & = \frac{1}{\abs{h}} \abs{ \int_{[z_0,z_0+h]} (f(z)-f(z_0) dz } \\
& \leq \frac{1}{\abs{h}} \epsilon \abs{h} = \epsilon.
\end{align*}
But this is equivalent to saying
\[
\lim_{h \to 0} \frac{F(z_0+h)-F(z_0)}{h} = f(z_0),
\]
or in other words, $F'(z_0)=f(z_0)$, as required.
%\vspace*{8cm}

\end{proof}
\begin{theorem}[Cauchy's Theorem for Starlit Regions]
\label{t:cauchyst}
Let $f$ be a function that is holomorphic in a starlit region $\mathcal{R}$ and let $\mathcal{C}$ be a closed contour in $\mathcal{R}$.  Then
\[
\int_{\mathcal{C}} f = 0.
\]
\end{theorem}
\begin{blankbox}
{\bf Proof}
By Theorem~\ref{t:starlit}, $f$ has an antiderivative in $\mathcal{R}$.  Thus by Theorem~\ref{t:closed}
\[
\int_{\mathcal{C}} f = 0.
\]
\qed
\end{blankbox}

In fact, Cauchy's Theorem extends to more general regions, as we shall see later.

\section{Application: The Complex Logarithm and Power Functions}

In this section we will define the Complex Logarithm and Power functions.

Consider the Real function $f:(0,+\infty) \to \R$ defined by $f(x) = \dfrac{1}{x}$. Since $f$ is continuous on $(0,+\infty)$, the Fundamental Theorem of Calculus ensures that $f$ has an antiderivative on $(0,+\infty)$.  One way of defining the \emph{natural logarithm} function, is to define it as an antiderivative of $\dfrac{1}{x}$ on $(0,+\infty)$:
\begin{equation}
\label{e:reallog}
\log (x) = \int_1^x \frac{1}{t}\ dt \quad \text{ for all } x \in (0,+\infty).
\end{equation}
\begin{note}
\begin{enumerate}
\item[(i)] There are of course other ways of defining $\log (x)$ for $x \in (0,+\infty)$, most commonly as the inverse of the exponential function $\exp:\mathbb{R} \to (0,+\infty)$.  This definition is equivalent.
\item[(ii)]  The Fundamental Theorem of calculus actually tells us that for \emph{any} choice of $a \in (0,+\infty)$, the function $F: (0,+\infty) \to \mathbb{R}$ defined by
\[
F(x) = \int_a^x \frac{1}{t}\ dt
\]
is an antiderivative for $x \mapsto \frac{1}{x}$.  However, we must take $a=1$ to ensure that $F$ is indeed the inverse of $x \mapsto e^x$: since $e^0=1$ we want $\log (1)=0$.
\end{enumerate}
\end{note}
We shall generalise~\eqref{e:reallog} to define the Complex Logarithm function, using Theorem~\ref{t:starlit} (the existence of antiderivatives on starlit regions).  The function
\[
f(z) = \frac{1}{z}\quad (z \in \C \backslash \set{0})
\]
is holomorphic on $\C \backslash \set{0}$. However, this set is neither simply connected nor starlit.  Therefore, we will restrict our attention to the subset
\[
\C_{\pi} = \set{ z \in \C: z \neq 0 \text{ and } \Arg(z) \neq \pi },
\]
which is both starlit and simply connected.
\begin{center}
\includegraphics[scale=1]{ch4_cpi2}
\end{center}
 The point $1$ on the real axis is a star centre for $\C_{\pi}$ - in particular, this allows us to apply Theorem~\ref{t:starlit} to conclude that
\[
z \mapsto \int_{[1,z]} \frac{1}{\zeta}\ d \zeta
\]
is an antiderivative for $z \mapsto \frac{1}{z}$ on $\C_{\pi}$.  We use the variable $\zeta$ (zeta) as the variable of integration because $z$ has been used already.

\begin{definition}
The \emph{Complex Logarithm} function $\Log : \C_{\pi} \to \C$ is defined by
\[
\Log (z) = \int_{[1,z]} \frac{1}{\zeta}\ d \zeta\quad \text{ for } z \in \C_{\pi}.
\]
It is holomorphic on $\C_{\pi}$.
\end{definition}
In other words, to find the value of $\Log (z)$ we need to evaluate the integral
\[
\int_{[1,z]} \frac{1}{\zeta}\ d \zeta.
\]
We can do this directly by choosing a parametrisation of the path $[1,z]$, however, it is much easier to obtain an alternative definition using the following example.
\begin{example}
We shall show that for all $z \in \C_{\pi}$ we have
\[
\Log(z) = \log ( \abs{z} ) + i \Arg (z)
\]
where $\log:(0,+\infty) \to \R$ denotes the real (natural) logarithm and $\Arg$ is the principal value of the argument.
\end{example}
\begin{solution}
Since the function $f$ defined by $f(z)=\frac{1}{z}$ has an antiderivative on $\C_{\pi}$, the Contour Independence Theorem (Theorem~\ref{t:contint}) tells us that
$\displaystyle \int_{\mathcal{C}} f = \int_{[1,z]} f$ for any contour $\mathcal{C}$ in $\C_{\pi}$ that starts at $1$ and ends at $z$.
\begin{center}
\altgraphics[scale=1]{ch4_logpath}{ch4_cpi2}
%{logpath_full}{cpi3}
\end{center}
We shall use the contour $\mathcal{C}=\Gamma_1+\Gamma_2$, where $\Gamma_1=[1,\abs{z}]$ is the straight line segment along the real axis from $1$ to $\abs{z}$, and $\Gamma_2$ is the arc of the circle with centre $0$ and radius $\abs{z}$ from the point $\abs{z}$ on the positive real axis to $z$.

Parametrise $\Gamma_1$ with $\gamma_1:[1,\abs{z}] \to \C$, $\gamma_1(t)=t$, and $\Gamma_2$ with $\gamma_2:[0,\Arg (z)] \to \C,$
\[
\gamma_2(t) = \polar{\abs{z}}{t}
\]
Note that $\Gamma_2$ is a path from $\gamma_2 (0) = \abs{z}$ to $\gamma_2 ( \Arg (z)) = z$, hence is traversed
\begin{itemize}
\item anticlockwise if $\Arg (z) \geq 0$, and
\item clockwise if $\Arg(z)<0$,
\end{itemize}
which ensures that we stay within $\C_{\pi}$.

We have $\gamma_1'(t)=1$ and $\gamma_2'(t) =  i \gamma_2 (t)$, and so
\begin{align*}
\Log (z) & = \int_{\Gamma_1} \frac{1}{\zeta}\ d\zeta + \int_{\Gamma_2} \frac{1}{\zeta}\ d\zeta \\
& =
\int_{1}^{\abs{z}} \frac{1}{t}\ dt + \int_0^{\Arg (z)} \frac{1}{\gamma_2(t)} \gamma_2'(t)\ dt \\
& = \left[ \log \abs{t} \right]_1^{\abs{z}} + \int_0^{\Arg (z)} i\ dt \\
& = \log \abs{z} + i \Arg (z).
\end{align*}
\end{solution}
%\vspace*{20cm}
This yields the following alternative definition of the Complex Logarithm function:
\begin{equation}
\Log (z) = \log ( \abs{z} )+i \Arg (z) \quad (z \in \C_{\pi}).
\end{equation}
\begin{note}
This definition of $\Log (z)$ uses the principal value of the argument - that is to say, our definition depends on us taking $\arg (z) \in (-\pi, \pi]$.  For this reason it is sometimes called the \emph{principal branch} of the (complex) Logarithm function.  Other `branches' (i.e. other definitions) of $\Log (z)$ are possible by taking different values of the argument.
\end{note}
\begin{example}
Compute the following principal logarithms: $\Log (7)$, $\Log (-2i)$ and $\Log (1+i)$.
\end{example}
\begin{solution}
\begin{align*}
\Log (7) & = \log \abs{7} + i \Arg (7) = \log(7) \\
\Log (-2i) & = \log \abs{-2i} + i \Arg (-2i) \\
& = \log(2) -i \frac{\pi}{2} \\
\Log (1+i ) & = \log \abs{1+i} + i \Arg (1+i) \\
& = \log ( \sqrt{2} ) + i \frac{\pi}{4} \\
& = \frac{1}{2} \log (2) + i \frac{\pi}{4.}
\end{align*}
\end{solution}

So far, $\Log (z)$ is defined (and is holomorphic) on $\C_{\pi}$. We can extend the domain of $\Log$ to $\C \backslash\set{0}$ by defining it on the negative real axis, where points have principal argument $\pi$, as follows:
\[
\Log (z) = \log \left( \abs{z} \right) + i \pi, \mbox{ for $z \neq 0$ on the negative real axis.}
\]
\begin{question}
Is $\Log(z)$ continuous on $\C \backslash \set{0}$?
\end{question}
\begin{answer}
%\begin{center}
%\altgraphics[scale=0.4]{log_discontinuous_full}{cless0}
%\end{center}
For $t$ on the negative real axis,
\[
\rlim{z \to t}{ \Im (z) > 0 } \Arg (z) = \pi, \text{ while} \rlim{z \to t}{\Im(z)<0} \Arg (z) = -\pi.
\]
Thus $z \mapsto \Arg (z)$ is discontinuous on the negative real axis.  Since $\Arg (z)$ is the imaginary part of $\Log(z)$, it follows that $\Log$ is also discontinuous on the negative real axis.
\end{answer}

\begin{question}
Is $\Log$ the inverse of $\exp$?
\end{question}
\begin{answer}
Not exactly.  For any $z \in \C \backslash \set{0}$, we have
\begin{align*}
\exp ( \Log (z)) & = \exp \left( \log ( \abs{z} ) + i \Arg (z) \right) \\
& = e^{\log \abs{z}} \left( \cos ( \Arg (z))+i \sin ( \Arg (z) ) \right) \\
& = \underbrace{\abs{z}\left( \cos ( \Arg (z))+i \sin ( \Arg (z) ) \right)}_{\text{Polar form of }z} \\
& = z.
\end{align*}
However, $\exp:\C \to \C$ is not injective (e.g. $\exp(0)=\exp(i2\pi)=1$), thus we cannot have $\Log ( \exp(z)) = z$ for all $z$.  We do have
\begin{align*}
\Log ( \exp (x+iy ) ) & = \log \abs{\exp(x+iy)} + i \Arg (\exp(x+iy)) &&\\
& = \log(e^x) + i (y-2k\pi) && \text{ for some }k \in \mathbb{Z} \\
\end{align*}
Hence $\Log (\exp(z))=z$ if and only if $-\pi<\Im(z) \leq \pi$.
%\newpage
%\vspace*{10cm}
\end{answer}

We have seen already how to take roots of complex numbers. We will now extend this definition to \emph{complex} powers of complex numbers, allowing us to compute expressions such as
\[
2^{i}, i^{i}, (1+3i)^{1-i}
\]
and so on.
\begin{blankbox}
 The following observation about powers of real numbers may be useful: suppose we want to find the value of $x^a$ for some $a,x \in \mathbb{R}$ with $x>0$.  We know that
\[
\log (x^a) = a \log (x),
\]
and since $e^{\log(y)}=y$ for all $y \in (0,+\infty)$, we have
\[
x^a = e^{  \log \left( x^a \right) } = e^{  a \log (x) }.
\]
Thus we may take $e^{ a \log (x) }$ to be the definition of $x^a$.
\end{blankbox}
\begin{definition}
\label{d:ppower}
For $\alpha \in \C$, the \emph{Principal $\alpha^{th}$ Power Function} is defined by
\[
z^{\alpha} = \exp \left( \alpha \Log (z) \right)
\]
for all $z \in \C \backslash \set{0}$.
\end{definition}
Because $\Log$ is holomorphic on $\C_{\pi}$ and $\exp$ is holomorphic on $\C$, the Principal Power function is holomorphic on $\C_{\pi}$.  `Principal' refers to the fact that we have used the principal branch of the Logarithm function to define the power function - taking different branches of the Logarithm function will give different power functions.

\begin{example}
 Let us verify that $i^2=-1$ agrees with Definition~\ref{d:ppower}.
\end{example}
\begin{solution}
Indeed, we have
\begin{align*}
i^2 = \exp \left( 2 \Log (i) \right) & = \exp \left( 2 \left[ \log \abs{i}+i \Arg(i) \right] \right) \\
& = \exp \left( 2 \left[ \log(1)+i \frac{\pi}{2} \right] \right) \\
& = \exp(i\pi) = -1.
\end{align*}
\end{solution}
%\vspace*{7cm}

\begin{example}
Calculate $2^i$ and $(1+i)^{-i}$.
\end{example}
\begin{solution}
This time, we have
\begin{align*}
2^i = \exp(i \Log(2) ) & = \exp(i \log (2) ) \\
& = \cos(\log(2))+i \sin(\log(2))
\end{align*}
and
\begin{align*}
(1+i)^{-i} & = \exp \left( -i \Log (1+i) \right) \\
& = \exp \brac{-i\brac{\tfrac{1}{2}\log(2)+i\frac{\pi}{2}}} \\
& = \exp \brac{\tfrac{\pi}{4}-i\tfrac{1}{2}\log(2)} \\
& = \polar{e^{\pi/4}}{\tfrac{1}{2}\log(2)}.
\end{align*}
\end{solution}


\chapter{Probabilistic Analysis and Randomized Algorithms}

\section{The hiring problem}

\begin{enumerate}

\item[5.1{-}1]{Show that the assumption that we are always able to determine
which candidate is best, in line 4 of procedure \textsc{Hire-Assistant}, implies
that we know a total order on the ranks of the candidates.}

\begin{framed}
Let $A$ be the set of candidates in random order and $R$ the binary relation
``is better than or equal''. $R$ is a total order if
\begin{enumerate}
  \item $R$ is \textbf{reflexive}. That is, $a\;R\;a \; \Forall a \in A$;
  \item $R$ is \textbf{antisymmetric}. That is, $a\;R\;b$ and $b\;R\;a$ imply $a = b$;
  \item $R$ is \textbf{transitive}. That is, $a\;R\;b$ and $b\;R\;c$ imply $a\;R\;c$;
  \item $R$ is a \textbf{total relation}. That is, $a\;R\;b$ or $b\;R\;a \; \Forall a, b \in A$.
\end{enumerate}

The above properties are necessary because
  \begin{enumerate}
    \item if two different candidates have the same qualification, it is
      necessary so that they can be compared;
    \item if both $a$ is ``better than or equal'' than $b$ and $b$ is ``better
      than or equal'' than $a$ and they qualifications are not equal, we would
      not be able to choose one of them and still be hiring ``the best candidate
      we have seen so far'';
    \item if we hire $b$ because he is ``better than or equal'' than $a$ and
      then we hire $c$ because he is ``better than or equal'' than $b$ and $c$
      is not ``better than or equal'' than $a$, we are not hiring ``the best
      candidate we have seen so far'';
    \item if $R$ is not a total relation, we would not be able to compare any
      two candidates.
\end{enumerate}
\end{framed}

\item[5.1{-}2]{($\star$) Describe an implementation of the procedure \textsc{Random}(a, b)
that only makes calls to \textsc{Random}(0, 1). What is the expected running
time of your procedure, as a function of a and b?}

\begin{framed}
The pseudocode is stated below.\\
\begin{algorithm}[H]
\SetAlgoNoEnd\DontPrintSemicolon
\BlankLine
\SetKwFunction{algo}{RandomInterval}
\SetKwProg{myalg}{}{}{}
\myalg{\algo{a, b}}{%
\nl $flips = \ceil{\lg (b - a)}$\;
\nl $count = \infty$\;
\nl \While{$count > b$}{%
\nl   $count = 0$\;
\nl   \For{$i = 1$ \KwTo $flips$}{%
\nl     $count = count + (2^{i - 1} \cdot \texttt{Random}(0, 1))$\; } }
\nl \Return{$count + a$}\; }
\end{algorithm}

The expected running time is
\[
  \underbrace{2^{\ceil{\lg(b - a)}}/(b - a)}_\text{while loop} \cdot \underbrace{\ceil{\lg(b - a)}}_\text{for loop} < 2 \cdot \ceil{\lg(b - a)},
\]
where the last inequality is valid since $1 \le 2^{\ceil{\lg(b - a)}}/(b - a) < 2$.
\end{framed}

\item[5.1{-}3]{($\star$) Suppose that you want to output 0 with probability
$1/2$ and 1 with probability $1/2$.  At your disposal is a procedure
\textsc{Biased-Random}, that outputs either 0 or 1. It outputs 1 with some
probability $p$ and 0 with probability $1 - p$, where $0 < p < 1$, but you do
not know what $p$ is. Give an algorithm that uses \textsc{Biased-Random} as
a subroutine, and returns an unbiased answer, returning 0 with probability $1/2$
and 1 with probability $1/2$. What is the expected running time of your
algorithm as a function of $p$?}

\begin{framed}
The pseudocode is stated below.\\
\begin{algorithm}[H]
\SetAlgoNoEnd\DontPrintSemicolon
\BlankLine
\SetKwFunction{algo}{Random}
\SetKwProg{myalg}{}{}{}
\myalg{\algo{}}{%
\nl \While{$1$}{%
\nl   $r_1 = \texttt{Random}(0, 1)$\;
\nl   $r_2 = \texttt{Random}(0, 1)$\;
\nl   \If{$r_1 \neq r_2$}{%
\nl     \Return{$r_1$}\; } } }
\end{algorithm}

The expected running time is
\[
  \frac{1}{\underbrace{(1 - p) p}_\text{$(r_1, r_2) = (0, 1)$} + \underbrace{p (1 - p)}_\text{$(r_1, r_2) = (1, 0)$}} \cdot 1 = \frac{1}{2p (1- p)}.
\]
\end{framed}

\end{enumerate}

\newpage

\section{Indicator random variables}

\begin{enumerate}

\item[5.2{-}1]{In \textsc{Hire-Assistant}, assuming that the candidates are
presented in a random order, what is the probability that you hire exactly
one time? What is the probability that you hire exactly $n$ times?}

\begin{framed}
Since the initial dummy candidate is the least qualified,
\textsc{Hire-Assistant} will always hire the first candidate. It hires exacly
one time when the best candidate is the first to be interviewed. Thus, the
probability is $1/n$. To hire exactly $n$ times, the candidates has to be in
increasing order of quality. Since there are $n!$ possible orderings (each one
with equal probability of happening), the probability is $1/n!$.
\end{framed}

\item[5.2{-}2]{In \textsc{Hire-Assistant}, assuming that the candidates are
presented in a random order, what is the probability that you hire exactly
twice?}

\begin{framed}
The first candidate is always hired, thus the best qualified candidate cannot be
the first to be interviewed. Also, among all the candidates that are better
qualified than the first candidate, the best candidate must be interviewed first.
Otherwise, a third candidate will be hired between them. Now assume that the
first candidate to be interviewed is the $i$th best qualified, for $i = 2,
\dots, n$. This occurs with a probability of $1/n$. To hire exactly twice, the
best candidate must be the first to be interviewed among the $i - 1$ candidates
that are better qualified than candidate $i$. This occurs with a probability
of $1/(i - 1)$. Thus, the probability of hiring exactly twice is
\[
  \sum_{i = 2}^{n} \frac{1}{n} \frac{1}{i - 1} = \frac{1}{n} \sum_{i = 1}^{n - 1} \frac{1}{i} = \frac{1}{n} (\lg(n - 1) + O(1)).
\]
\end{framed}

\item[5.2{-}3]{Use indicator random variables to compute the expected value of
the sum of $n$ dice.}

\begin{framed}
Let $X_i$ be an indicator random variable of a dice coming up the number $i$. We
have $\text{Pr}\{X_i\} = 1/6$. Let $X$ be a random variable denoting the
result of throwing a dice. Then
\[
  \text{E}[X] = \sum_{i = 1}^{6} i \cdot \text{Pr}\{X_i\}
              = \sum_{i = 1}^{6} i \cdot \frac{1}{6}
              = \frac{1}{6} \sum_{i = 1}^{6} i
              = \frac{1}{6} \frac{6 \cdot 7}{2} = 3.5.
\]
By linearity of expectations, the expected value of the sum of $n$ dice is the
sum of the expected value of each dice. Thus,
\[
  \sum_{i = 1}^{n} \text{E}[X] = \sum_{i = 1}^{n} 3.5 = 3.5 \cdot n.
\]
\end{framed}

\item[5.2{-}4]{Use indicator random variables to solve the following problem,
which is known as the \textbf{\emph{hat-check problem}}. Each of $n$ customers
gives a hat to a hat-check person at a restaurant. The hat-check person
gives the hats back to the customers in a random order. What is the expected
number of customers who get back their own hat?}

\begin{framed}
Let $X_i$ be an indicator random variable of customer $i$ getting back his own
hat. We have
\[
\text{Pr}\{X_i\} = \text{E}[X_i] = 1/n.
\]
Let $X$ be a random variable denoting the number of customers who get back their
own hat. Then
\[
  X = X_1 + X_2 + \cdots + X_n,
\]
which implies
\begin{equation*}
\begin{aligned}
  \text{E}[X] &= \text{E}\left[\sum_{i = 1}^{n} X_i\right]\\
              &= \sum_{i = 1}^{n} \text{E}[X_i]\\
              &= \sum_{i = 1}^{n} \frac{1}{n}\\
              &= 1.
\end{aligned}
\end{equation*}
\end{framed}

\item[5.2{-}5]{Let $A[1, \dots, n]$ be an array of $n$ distinct numbers. If
$i < j$ and $A[i] > A[j]$, then the pair $(i, j)$ is called an
$\textbf{\emph{inversion}}$ of $A$. (See Problem 2-4 for more on inversions.)
Suppose that the elements of $A$ form a uniform random permutation of
$\langle 1, 2, \dots, n \rangle$. Use indicator random variables to compute
the expected number of inversions.}

\begin{framed}
Let $X_{ij}$ be an indicator random variable for the event that the pair
$(i, j)$ is inverted. Since $A$ forms a uniform random permutation, we have
\[
\text{Pr}\{X_{ij}\} = \text{Pr}\{\overline{X_{ij}}\} = 1/2,
\]
which implies
\[
  \text{E}[X_{ij}] = 1/2.
\]
Let $X$ be a random variable denoting the number of
inversions of $A$. Since there are $\binom{n}{2}$ possible pairs on $A$, each
with probability $1/2$ of being inverted, we have
\[
  \text{E}[X] = \binom{n}{2} \frac{1}{2} = \frac{n!}{2! \cdot (n - 2)!} \frac{1}{2} = \frac{n (n - 1)}{4}.
\]
\end{framed}

\end{enumerate}

\newpage

\section{Randomized algorithms}

\begin{enumerate}

\item[5.3{-}1]{Professor Marceau objects to the loop invariant used in the proof
of Lemma 5.5. He questions whether it is true prior to the first iteration. He
reasons that we could just as easily declare that an empty subarray contains
no 0-permutations. Therefore, the probability that an empty subarray
contains a 0-permutation should be 0, thus invalidating the loop invariant
prior to the first iteration. Rewrite the procedure \textsc{Randomize-In-Place}
so that its associated loop invariant applies to a nonempty subarray prior
to the first iteration, and modify the proof of Lemma 5.5 for your procedure.}

\begin{framed}
Just select a random element in the array and swap it with the first element.

\begin{algorithm}[H]
\SetAlgoNoEnd\DontPrintSemicolon
\BlankLine
  \SetKwFunction{algo}{Randomize-In-Place}
\SetKwProg{myalg}{}{}{}
\myalg{\algo{A}}{%
\nl $n = A.length$\;
\nl swap $A[1]$ with $A[\texttt{Random}(1, n)]$\;
\nl \For{$i = 2$ \KwTo $n - 1$}{%
\nl   swap $A[i]$ with $A[\texttt{Random}(i, n)]$\; } }
\end{algorithm}

The only difference in the proof of Lemma 5.5 is the initialization of the loop
invariant:
\begin{itemize}
  \item \textbf{Initialization.} Consider the situation just before the first
    loop iteration, so that $i = 2$. The loop invariant says that for each
    possible 1-permutation, the subarray $A[1, \dots, 1]$ contains this
    1-permutation with probability $(n - i + 1)/n! = (n - 1)!/n! = 1/n$. The
    subarray $A[1, \dots, 1]$ has a single element and this element was
    randomly choosed among the $n$ elements of the array. Thus, $A[1, \dots, 1]$
    contains this $1$-permutation with probability $1/n$, and the loop invariant
    holds prior to the first iteration.
\end{itemize}
\end{framed}

\item[5.3{-}2]{Professor Kelp decides to write a procedure that produces at
random any permutation besides the identity permutation. He proposes the
following procedure:

\begin{algorithm}[H]
\SetAlgoNoEnd\DontPrintSemicolon
\BlankLine
\SetKwFunction{algo}{Permute-Without-Identity}
\SetKwProg{myalg}{}{}{}
\myalg{\algo{A}}{%
\nl $n = A.length$\;
\nl \For{$i = 1$ \KwTo $n - 1$}{%
\nl   swap $A[i]$ with $A[\texttt{Random}(i + 1, n)]$\; } }
\end{algorithm}

Does this code do what Professor Kelp intends?
}

\begin{framed}
No. This code enforces that every position $i$ of the resulting array receives
an element that is different from the $i$th element of the original array.
However, this requirement discards much more permutations than just the identity
permutation. For instance, consider the array $A = [1, 2, 3]$ and a permutation
of it $A' = [1, 3, 2]$. In this case, the permutation $A'$ is not identical
to the original array $A$. However, Professor Kelp's code is not able to produce
this permutation.
\end{framed}

\item[5.3{-}3]{Suppose that instead of swapping element $A[i]$ with a random
element from the subarray $A[i, \dots, n]$, we swapped it with a random element
from anywhere in the array:

\begin{algorithm}[H]
\SetAlgoNoEnd\DontPrintSemicolon
\BlankLine
\SetKwFunction{algo}{Permute-With-All}
\SetKwProg{myalg}{}{}{}
\myalg{\algo{A}}{%
\nl $n = A.length$\;
\nl \For{$i = 1$ \KwTo $n$}{%
\nl   swap $A[i]$ with $A[\texttt{Random}(1, n)]$\; } }
\end{algorithm}

Does this code produce a uniform random permutation? Why or why not?
}

\begin{framed}
No. As a counterexample, consider the input array $A = [1, 2, 3]$. Since each
call to \textsc{Random} can produce one of three values, the number of possible
outcomes after all the \textsc{Random} calls can be seen as the number of
strings over the set $\{1, 2, 3\}$, which is $3^3 = 27$. However, since an array
of size $3$ has $3! = 6$ distinct permutations, and 27 is not divisible by 6, it
is not possible that each of the 6 permutations of $A$ has the same probability
of happening among the 27 possible outcomes of \textsc{Permute-With-All}.
\end{framed}

\newpage

\item[5.3{-}4]{Professor Armstrong suggests the following procedure for
generating a uniform random permutation:

\begin{algorithm}[H]
\SetAlgoNoEnd\DontPrintSemicolon
\BlankLine
\SetKwFunction{algo}{Permute-By-Cyclic}
\SetKwProg{myalg}{}{}{}
\myalg{\algo{A}}{%
\nl $n = A.length$\;
\nl let $B[1 \dots n]$ be a new array\;
\nl \emph{offset} $= \texttt{Random}(1, n)$\;
\nl \For{$i = 1$ \KwTo $n$}{%
\nl   $\text{\emph{dest}} = i + \text{\emph{offset}}$\;
\nl   \If{\emph{dest} $> n$}{%
\nl     \emph{dest} $=$ \emph{dest} $- n$\; }
\nl   $B[\text{\emph{dest}}] = A[i]$\; } }
\end{algorithm}

Show that each element $A[i]$ has a $1/n$ probability of winding up in any
particular position in $B$. Then show that Professor Armstrong is mistaken by
showing that the resulting permutation is not uniformly random.
}

\begin{framed}
What Professor Armstrong's code does is a circular shift of all the elements to
the right by $i$ positions. Since each of the $n$ possible shifts has the same
probability of happening, each element has indeed a probability of $1/n$ of
winding up in any particular position of the final array $B$. However, since
this code has only $n$ possible outcomes and $A$ has $n!$ permutations, it can
not produce a uniform random distribution over $A$. More precisely, the
Professor Armstrong's code is not able to produce any permutation of $A$ that
is not a circular shift of $A$.
\end{framed}

\item[5.3{-}5]{($\star$) Prove that in the array $P$ in procedure
\textsc{Permute-By-Sorting}, the probability that all elements are unique is at
least $1 - 1/n$.}

\begin{framed}
Let $X_i$ be an indicador random variable for the event that the $i$th priority
is not unique. Since the subarray $P[1, \dots, i - 1]$ has at most $i - 1$
distinct numbers, we have $\text{Pr}\{X_i\} = \text{E}[X_i] \le (i - 1)/n^3$.
Let $X$ be a random variable for the event that at least one priority is not
unique. Then
\[
  X = (X_1 \cup X_2 \cup \cdots X_n) = X_1 + X_2 + \cdots + X_n,
\]
which implies
\begin{equation*}
\begin{aligned}
  \text{E}[X] &=   \text{E}\left[\sum_{i = 1}^{n} X_i\right]\\
              &=   \sum_{i = 1}^{n} \text{E}[X_i]\\
              &\le \sum_{i = 1}^{n} \frac{i - 1}{n^3}\\
              &=   \frac{1}{n^3} \sum_{i = 0}^{n - 1} i\\
              &=   \frac{1}{n^3} \frac{(n - 1) \cdot n}{2}\\
              &=   \frac{n - 1}{2 n^2}\\
              &\le \frac{1}{n}.
\end{aligned}
\end{equation*}

Thus, the probability that all elements are unique is
\[
  \text{E}[\overline{X}] = 1 - \text{E}[X] \ge 1 - \frac{1}{n}.
\]
\end{framed}

\newpage

\item[5.3{-}6]{Explain how to implement the algorithm
\textsc{Permute-By-Sorting} to handle the case in which two or more priorities
are identical. That is, your algorithm should produce a uniform random
permutation, even if two or more priorities are identical.}

\begin{framed}
The pseudocode is stated below.

\begin{algorithm}[H]
\SetAlgoNoEnd\DontPrintSemicolon
\BlankLine
\SetKwFunction{algo}{Permute-By-Sorting-Unique}
\SetKwProg{myalg}{}{}{}
\myalg{\algo{A}}{%
\nl $n = A.length$\;
\nl let $P[1 \dots n]$ be a new array\;
\nl \Repeat{\text{unique}}{%
\nl   \For{$i = 1$ \KwTo $n$}{%
\nl     $P[i] = \texttt{Random}(1, n^3)$\; }
\nl   let $Q$ be a copy of $P$\;
\nl   sort $Q$\;
\nl   $\text{\emph{unique}} = \text{True}$\;
\nl   \For{$i = 2$ \KwTo $n$}{%
\nl     \If{$Q[i] == Q[i - 1]$} {%
\nl       $\text{\emph{unique}} = \text{False}$\;
\nl       break\; } } }
\nl sort $A$, using $P$ as sort keys\; }
\end{algorithm}

Before sorting $A$ using $P$ as sort keys, the above algorithm verifies if $P$
has unique priorities. If the priorities are not unique, $P$ is generated again
until it has unique priorities. Since the probability that a random $P$ is
unique is at least $1 - 1/n$, the expected number of iterations of the repeat
loop of lines 3-12 is less than $2$.
\end{framed}

\newpage

\item[5.3{-}7]{Suppose we want to create a \textbf{\emph{random sample}} of the
set $\{1, 2, 3, \dots, n\}$, that is, an $m$-element subset $S$, where
$0 \le m \le n$, such that each $m$-subset is equally likely to be created. One
way would be to set $A[i] = i$ for $i = 1, 2, 3, \dots, n$, call
\textsc{Randomized-In-Place}($A$), and then take just the first $m$ array
elements. This method would make $n$ calls to the \textsc{Random} procedure. If
$n$ is mucn larger than $m$, we can create a random sample with fewer calls to
\textsc{Random}. Show that the following recursive procedure returns a random
$m$-subset $S$ of $\{1, 2, 3, \dots, n\}$, in which each $m$-subset is equally
likely, while making only $m$ calls to \textsc{Random}:

\begin{algorithm}[H]
\SetAlgoNoEnd\DontPrintSemicolon
\BlankLine
\SetKwFunction{algo}{Random-Sample}
\SetKwProg{myalg}{}{}{}
\myalg{\algo{m, n}}{%
\nl \If{$m == 0$}{%
\nl   \Return{$\emptyset$}\; }
\nl  \Else{%
\nl    $S = \texttt{Random-Sample}(m - 1, n - 1)$\;
\nl    $i = \texttt{Random}(1, n)$\;
\nl    \If{$i \in S$}{%
\nl      $S = S \cup \{n\}$\; }
\nl    \Else{%
\nl      $S = S \cup \{i\}$\; }
\nl    \Return{S}\; } }
\end{algorithm}
}

\begin{framed}
The recursion has $m + 1$ levels. Let $R_k$, for $k = 0, 1, \dots, m$, denote
the recursion at depth $k$, in which an $k$-subset is returned ($R_0$ returns
the empty set; $R_m$ returns the final $m$-subset). After $R_k$, $S$ will
consist of $k$ elements from the set $\{1, 2, \dots, n - (m - k)\}$. There are
$\binom{n - (m - k)}{k}$ ways to choose $k$ elements from an
$(n - (m - k))$-set. Thus, to $S$ be a random sample, we wish to show that, in
each recursion level $k$, this particular $k$-subset is selected with
probability $1/\binom{n - (m - k)}{k}$.

For the base case of the recursion, which occurs when $k = 0$, there are
$\binom{n - m}{0} = 1$ distincts $0$-subsets and the algorithm returns the empty
set with probability $1 = 1/\binom{n - m}{0}$. Now assume $R_{k - 1}$ returns an
random $(k - 1)$-sample. There are two ways to add the $k$th element to $S$ on
$R_k$:
\begin{itemize}
\item The element $n - (m - k)$ is added. This occurs when line 5 either
selects the element $n - (m - k)$ or an element $e$ such that $e \in R_{k - 1}$.
This probability is
\[
  \underbrace{\frac{1}{n - (m - k)}}_\text{$(n - (m - k))$ is selected} +
  \underbrace{\frac{k - 1}{n - (m - k)}}_\text{$e \in R_{k - 1}$ is selected} = \frac{k}{n - (m - k)}.
\]
Thus, $R_k$ produces a particular $k$-sample with the element $n - (m - k)$ with
probability
\begin{equation*}
\begin{aligned}
  \frac{k}{n - (m - k)} \cdot \frac{1}{\binom{n - (m - k) - 1}{k - 1}}
  &= \frac{k}{n - (m - k)} \cdot \left(\frac{(n - (m - k) - 1)!}{(k - 1)! \cdot (n - (m - k) - 1 - (k - 1))}\right)^{-1}\\
  &= \left(\frac{(n - (m - k))!}{k! \cdot (n - (m - k) - k )}\right)^{-1}\\
  &= \frac{1}{\binom{n - (m - k)}{k}}.
\end{aligned}
\end{equation*}

\item An element $j < n - (m - k)$ is added. The probability of line 5 selecting
such element is
\[
  \frac{n - (m - k) - k}{n - (m - k)} = \frac{n - m}{n - (m - k)}.
\]
Thus, $R_k$ produces a particular $k$-sample with the element $j$ with
probability
\begin{equation*}
\begin{aligned}
  \frac{n - m}{n - (m - k)} \cdot \frac{1}{\binom{n - (m - k) - 1}{k}}
  &= \frac{n - m}{n - (m - k)} \cdot \left(\frac{(n - (m - k) - 1)!}{k! \cdot (n - (m - k) - 1 - k)}\right)^{-1}\\
  &= \left(\frac{(n - (m - k))!}{k! \cdot (n - (m - k) - k)}\right)^{-1}\\
  &= \frac{1}{\binom{n - (m - k)}{k}}.
\end{aligned}
\end{equation*}
\end{itemize}

Since each recursion level $R_k$ such that $k > 0$ makes exactly one
call to \textsc{Random}, there are $m$ such calls. Also, among the
$\binom{n}{m}$ ways of choosing $m$ elements from an $n$-set,
\textsc{Random-Sample} returns each of them with probability
\[
  \frac{1}{\binom{n - (m - m)}{m}} = \frac{1}{\binom{n}{m}}.
\]
\end{framed}

\end{enumerate}

\newpage

\section*{Problems}
\addcontentsline{toc}{section}{\protect\numberline{}Problems}%

\begin{enumerate}
\item[5{-}1]{\textbf{\emph{Probabilistic counting}}\\
With a $b$-bit counter, we can ordinarily only count up to $2^b - 1$. With R.
Morri's \textbf{\emph{probabilistic counting}}, we can count up to a much larger
value at the expense of some loss of precision.

We let a counter value of $i$ represent a count of $n_i$ for
$i = 0, 1, \dots, 2^b - 1$, where the $n_i$ form an increasing sequence of
nonnegative values. We assume that the initial value of the counter is 0,
representing a count of $n_0 = 0$. The \textsc{Increment} operation works on
a counter containing the value $i$ in a probabilistic manner. If $i = 2^b - 1$,
then the operation reports an overflow error. Otherwise, the \textsc{Increment}
operation increases the counter by 1 with probability $1/(n_{i + 1} - n_i)$.

If we select $n_i = i$ for all $i \ge 0$, then the counter is an ordinary one.
More interesting situations arise if we select, say, $n_i = 2^{i - 1}$ for
$i > 0$ or $n_i = F_i$ (the $i$th Fibonacci number {--} see Section 3.2).

For this problem, assume $n_{2^b - 1}$ is large enough that the probability of
an overflow error is negligible.

\begin{enumerate}
\item[a.] Show that the expected value represented by the counter after $n$
\textsc{Increment} operations have been performed is exactly $n$.
\item[b.] The analysis of the variance of the count represented by the counter
depends on the sequence of the $n_i$. Let us consider a simple case:
$n_i = 100i$ for all $i \ge 0$. Estimate the variance in the value represented
by the register after $n$ \textsc{Increment} operations have been performed.
\end{enumerate}
}

\begin{framed}
\begin{enumerate}
\item
Let $X_i$ denote a random variable for the expected \emph{increment} of the
count represented by a counter of value $i$ after \emph{one} \textsc{Increment}
operation. We have
\[
  \text{E}[X_i] = 0 \cdot \left(1 - \frac{1}{n_{i + 1} - n_{i}}\right) + (n_{i + 1} - n_{i}) \cdot \frac{1}{n_{i + 1} - n_{i}} = 1,
\]
which shows that, independently from the current state of the \emph{counter},
the expected \emph{increment} of the \emph{count} after each \textsc{Increment}
operation is always 1. Thus, after $n$ \textsc{Increment} operations, the
expected \emph{count} is:
\[
  \sum_{i = 1}^{n} \text{E}[X_0] = \sum_{i = 1}^{n} 1 = n,
\]

\item We have
\begin{equation*}
\begin{aligned}
  \text{Var}[X_i] &= \text{E}[{X_i}^2] - \text{E}^2[X_i]\\
                  &= \left( 0^2 \cdot \left(1 - \frac{1}{100}\right) + 100^2 \cdot \frac{1}{100} \right) - 1\\
                  &= 99,
\end{aligned}
\end{equation*}
which shows that the estimated variance after each \textsc{Increment} operation
does not depend on the current state of the \emph{counter}. Thus, after $n$
\textsc{Increment} operations, the
estimated variance is
\[
  \sum_{i = 1}^{n} \text{Var}[X_0] = \sum_{i = 1}^{n} 99 = 99n.
\]
\end{enumerate}
\end{framed}

\newpage

\item[5{-}2]{\textbf{\emph{Searching an unsorted array}}\\
This problem examines three algorithms for searching for a value $x$ in an
unsorted array $A$ consisting of $n$ elements.

Consider the following randomized strategy: pick a random index $i$ into $A$. If
$A[i] = x$, then we terminate; otherwise, we continue the search by picking
a new random index into $A$. We continue picking random indices into $A$ until
we find an index $j$ such that $A[j] = x$ or until we have checked every element
of $A$. Note that we pick from the whole set of indices each time, so that we
may examine a given element more than once.

\begin{enumerate}
\item[\textbf{a.}] Write pseudocode for a procedure \textsc{Random-Search} to
implement the strategy above. Be sure that your algorithm terminates when all
indices into $A$ have been picked.
\item[\textbf{b.}] Suppose that there is exactly one index $i$ such that
$A[i] = x$. What is the expected number of indices into $A$ that we must pick
before we find $x$ and \textsc{Random-Search} terminates?
\item[\textbf{c.}] Generalizing your solution to part (b), suppose that there
are $k \ge 1$ indices $i$ such that $A[i] = x$. What is the expected number of
indices into $A$ that we must pick before we find $x$ and \textsc{Random-Search}
terminates? Your answer should be a function of $n$ and $k$.
\item[\textbf{d.}] Suppose that there are no indices $i$ such that $A[i] = x$.
What is the expected number of indices into $A$ that we must pick before we
have checked all elements of $A$ and \textsc{Random-Search} terminates?
\end{enumerate}

Now consider a deterministic linear serach algorithm, which we refer to as
\textsc{Deterministic-Search}. Specifically, the algorithm searches $A$ for $x$
in order, considering $A[1], A[2], A[3], \dots, A[n]$ until either it finds
$A[i] = x$ or it reaches the end of the array. Assume that all possible
permutations of the input array are equally likely.

\begin{enumerate}
\item[\textbf{e.}] Suppose that there is exactly one index $i$ such that
$A[i] = x$. What is the average-case running time of
\textsc{Deterministic-Search}?  What is the worst-case running time of
\textsc{Deterministic-Search}?
\item[\textbf{f.}] Generalizing your solution to part (e), suppose that there
are $k \ge 1$ indices $i$ such that $A[i] = x$. What is the average-case running
time of \textsc{Deterministic-Search}? What is the worst-case running time of
\textsc{Deterministic-Search}? Your answer should be a function of $n$ and $k$.
\item[\textbf{g.}] Suppose that there are no indices $i$ such that $A[i] = x$.
What is the average-case running time of \textsc{Deterministic-Search}? What
is the worst-case running time of \textsc{Deterministic-Search}?
\end{enumerate}

Finally, consider a randomized algorithm \textsc{Scramble-Search} that works by
first randomly permuting the input array and then running the deterministic
linear search given above on the resulting permuted array.

\begin{enumerate}
\item[\textbf{h.}] Letting $k$ be the number of indices $i$ such that
$A[i] = x$, give the worst-case and expected running times of
\textsc{Scramble-Search} for the cases in which $k = 0$ and $k = 1$. Generalize
your solution to handle the case in which $k \ge 1$.
\item[\textbf{i.}] Which of the three searching algorithms would you use?
Explain your answer.
\end{enumerate}
}

\begin{framed}
\begin{enumerate}
\item The pseudocode is stated below.

\begin{algorithm}[H]
\SetAlgoNoEnd\DontPrintSemicolon
\BlankLine
\SetKwFunction{algo}{Random-Search}
\SetKwProg{myalg}{}{}{}
\myalg{\algo{A, x}}{%
\nl $I = \emptyset$\;
\nl $n = A.length$\;
\nl $\text{\emph{index}} = -1$\;
\nl \While{$|I| < n$}{%
\nl   $i = \texttt{Random}(1, n)$\;
\nl   $I = I \cup \{i\}$\;
\nl   \If{$A[i] == x$}{%
\nl     $\text{\emph{index}} = i$\;
\nl     break\; } }
\nl \Return{index}\; }
\end{algorithm}

\item This can be viewed as a sequence of Bernoulli trials, each with
a probability $p = 1/n$ of success. Let $X$ be a random variable for the number
of trials needed to pick $i$ such that $A[i] = x$. From Equation (C.32), we have
\[
  \text{E}[X] = \frac{1}{p} = n.
\]

\item This can also be viewed as a sequence of Bernoulli trials, but with
a probability $p = k/n$ of success. Thus, we have
\[
  \text{E}[X] = \frac{1}{p} = \frac{n}{k}.
\]

\item Let $I$ be the set of indexes that was already checked. Let $X_i$ be
a random variable for the number of trials needed to pick an index $i$, for
$i = 1, 2, \dots, n$, such that $i \notin I$ and $|I| = i - 1$. This can be
viewed as a sequence of Bernoulli trials. Thus, we have
\[
  p = \frac{n - |I|}{n} = \frac{n - i + 1}{n},
\]
and
\[
  \text{E}[X_i] = \frac{1}{p} = \frac{n}{n - i + 1}.
\]

Now let $X$ be a random variable for the number of trials to pick all elements
of $A$. We have
\begin{equation*}
\begin{aligned}
  \text{E}[X] &= \text{E}\left[\sum_{i = 1}^{n} X_i \right]
              = \sum_{i = 1}^{n} \text{E}[X_i]\\
              &= \sum_{i = 1}^{n} \frac{n}{n - i + 1}\\
              &= n \sum_{i = 1}^{n} \frac{1}{n - i + 1}\\
              &= n \sum_{i = 0}^{n - 1} \frac{1}{n - i}\\
              &= n \sum_{i = 1}^{n} \frac{1}{i} & \text{($n$th harmonic number)}\\
              &= n (\ln n + O(1)).
\end{aligned}
\end{equation*}

\item Lets first consider the average case. Among the $n - 1$ elements that is
not $x$, $(n - 1)/2$ of them are expected to be before the element $x$ on the
array. Thus, the expected running time of the algorithm is
\[
  \frac{n - 1}{2} + 1 = \frac{n + 1}{2}.
\]

The worst-case occur when the number of elements before $x$ is $n - 1$. In this
case, the algorithm will make $n$ checks.

\item Let $I$ be the set of indexes such that $i \in I \rightarrow A[i] = x$.
For each element $e$ such that $e \neq x$, there are $k + 1$ possibilities to
position $e$ with respect to $I$ (before all elements of $I$, after one
element of $I$, but before the remaining $k - 1$ elements of $I$, and so on).
Each of these positions is equally likely.  Therefore, among the $n - k$
elements that is not $x$, $(n - k) \cdot 1/(k + 1) = (n - k)/(k + 1)$ are
expected to be before all the elements of $I$. Thus, the expected running time
of the algorithm is
\[
  \frac{n - k}{k + 1} + 1 = \frac{(n - k) + (k + 1)}{k + 1} = \frac{n + 1}{k + 1}.
\]

The worst-case occurs when the number of elements before the first $x$ is
$n - k$. In this case, the algorithm will make $n - k + 1$ checks.

\item In every case, the algorithm will check all elements of $A$. Thus, there
will be $n$ checks.

\item Suppose the algorithm uses \textsc{Randomize-In-Place} to randomize the
input array. Independently from the value of $k$, the algorithm will take
$n$ on this operation. Thus, lets focus on the number of checks for each
case. When $k = 0$, the algorithm will make exactly $n$ checks in every case.
Thus, it the expected running time is $n + n = 2n$. When $k = 1$, the
behaviour of the algorithm is similar to the one of item (e). Thus, the expected
running time is $n + (n + 1)/2 = (3n + 1)/2$. As for the worst-case, note that
this notation refers to the distribution of inputs.  Since for every input the
expected running time is the same, the worst-case (over the inputs) is
$n + (n + 1)/2 = (3n + 1)/2$. Similarly, for a given $k$ and from item (f),
both the expected running time and the worst-case is $n + (n + 1)/(k + 1)$.

\item \textsc{Deterministic-Search} is better in all cases.
\end{enumerate}
\end{framed}

\end{enumerate}


\section{Background Gauge Fields}
Let $P$ be the fermion parity operator.
There is a fermion parity symmetry
\begin{align}
    P \gamma_i P = -\gamma_i
\end{align}

Consider a $\mathbb{Z}_2^f$ background gauge field with $s=\pm 1$.
Let there be some centered Majoranas $\gamma_{i,\alpha}$
where $i$ labels site location
and $\alpha$ labels flavour.

In one dimensions the Hamiltonian is
\begin{align}
    H &= \frac{i}{4}\sum_{i,j}
    M_{ij}^{\alpha\beta} \gamma_i^\alpha \gamma_j^\beta
\end{align}

You can introduce a $\mathbb{Z}_2$ gauge field so than whenever you hop from $i$
to $j$,
there is a $\mathbb{Z}_2$ string from $i$ to $j$.
\begin{align}
    H[s] &=
    \frac{1}{4}\sum_{i,j}M_{i,j}^{\alpha\beta}
    \gamma_i^d \gamma_i^\beta
    \prod_{i=0}^{j - 1} S_{k,k + 1}
\end{align}
This is the $\mathbb{Z}_2$ analogue of say $U(1)$ electron symmetry
where you would take an $e^{i\in_1^2 A\, dl}$.

\begin{question}
    Why do we have multiple fermion flavours?
\end{question}
For example we could have two fermions on each site,
and furthermore if these had spin,
these would also have spin.

Back to the spinless particle superconductor,
\begin{align}
    H[s] &=
    \underbrace{
        \frac{1}{2}\sum_{i}(1? + t) \gamma_{2i}\gamma_{2i + 1} s_{i,i+1}
    }_{\text{intersite}}
    - \underbrace{\mu \gamma_{2i - 1}\gamma_{2i}}_{\text{intersite}}
\end{align}
You can write
$s_{i,i+1}=e^{i\pi \sigma_{i,i+1}}$
where $\sigma\in\left\{ 0, 1 \right\}$.
There are $\mathbb{Z}_2^+$ gauge transforms for
$\lambda_i\in\left\{ 0, 1 \right\}$
and we get
\begin{align}
    \sigma_{i, i + 1} &\to
    \left(
        \sigma_{i, i+1} + \lambda_{i + 1} - \lambda_i
    \right)\mod 2\\
    \gamma_{2i + 1} &\to
    (-1)^{\lambda_i}
    \gamma_{2i + 1}\\
    \gamma_i &\to
    (-1)^{\lambda_i} \gamma_{2i}
\end{align}
I want to emphasize the effect of changing boundary conditions from periodic to
anti-periodic results is equivalent to inserting $\pi$ net flux through the
ring.

\begin{example}
    For periodic boundary conditions $c_{i + N} = c_i$
    where $c$ is complex fermion.
    Singular gauge transform.
    Define
    \begin{align}
        \tilde{c}_i &=
        c_i \prod_{k=1}^{i} s_{k,k+1}
    \end{align}
    where
    \begin{align}
        \prod_{i=1}^{N} s_{i,i+1} = -1
    \end{align}
    then
    \begin{align}
        \tilde{c}_{i+N} &=
        c_{i + N} \prod_{k=1}^{N} s_{k, k+1}\\
        &=
        \underbrace{c_i \prod_{k = 1}^{i} S_{k, k + 1}}_{\tilde{c}_i}
        \underbrace{\prod_{k=i + 1} s_{k, k + 1}}_{-1}\\
        &=
        - \tilde{c}_i
    \end{align}
    But for periodic boundary conditions and $s=1$,
    we have anti-periodic boundary conditions with $\pi$ flux through ring.
\end{example}

\section{Topological invariant}
non-trivial phase.
Suppose you have a ring and a gap in between through close together to allow the
zero modes to interact.
The Hamiltonian is
\begin{align}
    H_{bdy} &= \tilde{t} i \gamma_{L}\gamma_{R}
\end{align}
Depending on the sign of $\tilde{t}$,
the ground state has either even or odd fermion parity.

Changing the sign of $\tilde{t}$ is equivalent to
inserting $\mathbb{Z}_2^f$ flux through the ring,
which is equivalent to changing boundary conditions.
It is also equivalent to changing fermion parity of ground state.

Trivial phase:
No Majorana Zero Mods,
so changing boundary conditions
doesn't change ground state parity.

Let us define the topological invariant
\begin{align}
    \mathcal{I} &=
    P_P P_A
\end{align}
where $P_P$ is the fermion parity of the ground state with periodic boundary
conditions and $P_A$ is the parity with anti-periodic boundary conditions.

The claim is that
\begin{align}
    \mathcal{I} &=
    \begin{cases}
        -1 & \mathrm{topological phase}\\
        1 & \mathrm{trivial phase}\\
    \end{cases}
\end{align}
In spinless $p$-wave superconductors,
\begin{align}
    P_A = + 1, \qquad P_P = -1
\end{align}

\subsection{Relation to free bond invariant}
For free fermions,
the topological invariant is
\begin{align}
    \mathcal{I} &=
    P\left( H_P \right) P\left( H_A \right)
\end{align}
where
\begin{align}
    P(H) = \frac{i}{4} \sum_{i} \gamma_i \gamma_j A_{i,j}
\end{align}
where $P(H)$ is given by the Pfaffian
\begin{align}
    P(H) &= \sgn(\textrm{Pf}(A))
\end{align}
Go into $k$-space the Hamiltonian becomes
\begin{align}
    H &=
    \frac{1}{2} \sum_{k}
    \Psi_k^\dagger H_k \Psi_k
\end{align}
where
\begin{align}
    \Psi_k &=
    \begin{pmatrix}
        \Psi_k\\
        \Psi_{-k}^\dagger
    \end{pmatrix}
\end{align}
For $k=0$,
$H$
only
involves $\Psi_i\Psi^\dagger$.
\begin{align}
    H &=
    \frac{1}{2} \sum_{k = 0, \pi}
    \Psi_k^\dagger
    H_k\Psi_{k}
    + \frac{1}{2}\cdots
\end{align}

At the end of the day,
the fermion parity for odd length ring
$k=2\pi n/N$ periodic boundary conditions
$k=0$, with $k=\pi$ so
\begin{align}
    k &= \frac{2\pi}{N}\left( n + \frac{1}{2} \right)
\end{align}
with antiperiodic boundary conditions.

Three is no $k=0$,
but there is $k=\pi$.
\begin{align}
    I = S_0 S_\pi = \nu.
\end{align}

For even length ring,
$k=0,\pi$ allowed by periodic boundary conditions,
but neigher is allowed for anti-periodic boundary conditions.
Then we have
\begin{align}
    I = S S_{\pi} = \nu
\end{align}

\subsection{TQFT}
When you have fermions and wnat TQFT,
you want spin TQFt.
For every $d$-dimensional manifold,
we have a vector space.
We have 2 possible spin structure, $+$ periodic and $-1$ anti-periodic.
In both cases we know we have a unique ground state.
\begin{align}
    V(S^1, +)
    \simeq
    V(S^1,-)
    \simeq\mathbb{C}
\end{align}
Let's try to figure out the path integral on $T^2$
with our choice of spin structure $\eta$.
I can think of the two directions of the torus as time and spcae.
So I have $x,t$,
and the spin structure $(x,t)$
can have boundary conditions $(++)$, $(+-)$, $(-+)$ or $(--)$.

When you have a system of verimions,
you can write it like a propagator
\begin{align}
    Z &= \bra{j} e^{iHt} \ket{i}
\end{align}
which is the coherent sate path integral.
In the coherent state path integral for fermions,
when you do the derivation,
the fermions have naturally have anti-periodic boundary conditions.
It's counterintiutive,
because you'd think periodic boundary conditions are what you should have,
but it turns out anti-periodic is natural.
The path integral for the TQFT is just the trace for the identity operator.
We have anit-periodic boundary conditions for the boundary operator.
This is equal to
\begin{align}
    Z(T^2, +-) &= \Tr_{V(S^1,+)}\mathbf{1} = 1\\
    Z(T^2, -+) &= \Tr_{V(S^1,-)}\mathbf{1} = 1
\end{align}
Remember when discussing TQFt,
if you're gluing the time boundary,
and you're twisting the boundary condition,
that is inserting flux,
which is the fermion parity operator.
\begin{align}
    Z(T^2, ++) &= \Tr_{V(S^1,+)}P = -1\\
    Z(T^2, --) &= \Tr_{V(S^1,-)}P = +1
\end{align}
You can choose the convention the $-1$ case if periodic and the $+1$ case is
anti-periodic.

Now you can ask what is the path integral in a closed genus $g$ surface
$\Sigma_g$.

We have a genus $g$ surface and a spin structure $\eta$.
We should get a $\pm 1$ because there are only two phases.
There are canonical invariants associated with spin surfaces
and it's callled the Arf invariant of the spin structure.
\begin{align}
    Z\left( \Sigma_g, \eta \right) &= (-1)^{\mathrm{Arf}(\theta)}
\end{align}
Arf invariants are associated with quadratic forms.
Definition of Arf.
Suppose $V$ is a vector space over $\mathbb{Z}_2$.
Let $\phi: V\times V\to \mathbb{Z}_2$ be a map
that is a non-degenerate symmetric bilinear form.
Then suppose you have a quadratic form $q: V\to \mathbb{Z}_2$
that satisfies by definition
\begin{align}
    q(x + y) &= q(x) + q(y) + \phi(x, y)
\end{align}
and $q\in Q(V, \phi)$ is an element in the space of all quadratic forms.
Then the Arf invariant is defined as $\mathrm{Arf}:Q\to\mathbb{Z}_2$
by
\begin{align}
    (-1)^{\textrm{Arf}(q)} &=
    \frac{1}{\sqrt{|V|}}
    \sum_{\lambda\in V} (-1)^{q(x)}
    = \pm 1
\end{align}
There is a theorem which I will not prove.
\begin{theorem}
    Spin structures on genus $g$ surfaces in 1-1 correspondence with elements of
    \begin{align}
        Q\left(
        H_1\left( \Sigma_g, \mathbb{Z}_2 \right),
        \underbrace{\phi}_{\text{intersection form}}
        \right)
    \end{align}
\end{theorem}
The intersection form takes 2 loops and tells you whether they intersect.
Given a spin structure,
you have a quadratic form like this,
and then you can evaluate the Arf invariant.
I can't give you an explanation of this,
that would be far beyond what we want.

\begin{question}
    What is a genus $g$ surface?
\end{question}
You should have stopped me earlier!
Surfaces are classified by their genus,
which is basically the number of holes they have.
Sphere has $g=0$,
Torus has $g=1$,
and if you have 2 holes then $t=2$.
The Euler characteristic of a genus-$g$ surface is
\begin{align}
    \chi(\Sigma_g) = 2 - 2g
\end{align}

\begin{question}
    What is $Q(V,\phi)$?
\end{question}
It is the space of all quadratic forms on $V$,
functions which satisfy the definition with $\phi$ as the form.

\begin{question}
    What is $H_1$?
\end{question}
$H_1$ is the space of formal linear combinations of loops.
OK too complicated.
Let's just say it's the space of loops modulo the space of contractible loops.
I'm not going to give you a rigorous definition.
I'll just define by example.

If I give you a surface, (genus 2)
there are 4 independent loops you can draw,
which are called $x_1,y_1,x_2,y_2$.
Then you can take formal linear combinations of them like
\begin{align}
    C = \alpha x_1 + \beta x_2 + \gamma y_1 + \delta y_2
\end{align}
where $\alpha,\beta,\gamma,\delta\in \mathbb{Z}_2$
and then this linear combination
$C\in H_1(\Sigma_g, \mathbb{Z}_2$.

Then you can have forms $a,b\in H^1(\Sigma_g, \mathbb{Z}_2)$
which map from $H_1\to \mathbb{Z}_2$.

So $H^1$ are linear combinations of loops (cohomology),
but $H_1$ are the actual loops themselves (homology).

Given a loop $X$,
you can define
\begin{align}
    \int_X a = \pm 1
\end{align}

I should have just said $x$ intersects $y$.

You can go to the torus,
first enumerate all possible functions from the loops on the torus to
$\mathbb{Z}_2$,
there are 16 of them,
then find all possible functions like this quadratic form,
there are 4 of them,
and then take the corespondence with periodioc and anti-periodic.

\begin{question}
    What is the Arf invariant invariant under?
\end{question}
I don't know.
It's invariant under all mapping class diffeommorphisms on the surface and the
Arf invariant will still be invariant.

To fully specify the TQFT,
I need to give you more information.
I need to specify the path integral on manifold with boundary.
for example,
I can tell you what the path integral on a disk $D^2$ is
(like a hemisphere with axis in the time direction).
\begin{align}
    Z(D^2) \in V(S^1, -)
\end{align}
On $S^1$,
two spin structures, $\pm 1$.
Only $-$ can be the boundayr of the disk.
In path, $-1$ is called the bounding spin structure
and $+$ is called the non-bounding spin structure.

The way ot think about spin structure is this.
You QFT,
a fermion is described by a fermion,
and you want a framing of the worldline of hte fmrion,
which goes on a loop,
and you want a frame for the world line.
Thereare two frames.
There's one that goes like this,
pointing orthogonal to the line.
Or you could consider one like this, all pointing in a direction.
The first is no-bounding,
the second one is bounding.

As you go along this second line,
and you unfold it,
as you go along, it kind of twists $2\pi$.
And a $2\pi$ rotation gives you a $-1$ for fermions.

Whereas in the first case, there is no twisting.
That's why the anti-periodic boundary condition gives something that can be a
boundary.

This comes up in a lot of sitautions with topological phases and fermions.
If you are confused why anit-periodic is showing up when it is,
this is why I think it's useful.
I haven't given you a deep understanding of what framing is rigorously,
but it's whether it twists or not relative to its worldline and whether the
boundary should be periodic or not.

In QFT fermions are defined in $SO(1,d)$.
If space-time is 2D,
you should have a 2D frame field,
and the two components are the frame field and the tangent ot the curve.
Relativisitc QFT somehow winds up giving the correct language for TQFT aspects
of things.
And somehow the condensed matter stuff
ends up fitting well into TQFT.

Relativistic fermions are spinors of $SO(3)$,
but if they're relativistic Minkowski space,
it's $SO(1,d)$.

Microsopically,
the fermions don't need this structure,
but when doing TQFT with fermions,
you have spin structures,
and a way to think about spin structures is to think of relativistic
fermions.

Boundary needs
\begin{align}
    Z\left( M^{d+1}, \eta \right) &=
    V\left( \partial M^{d + 1}, \eta_{\partial M^{d+1}} \right)
\end{align}

I will post the paper for reference.

The reason I'm telling you this,
is I want to show you wnat structure is giving you this TQFT,
then I'm going ot guess what kind of structure classifies all kinds of
topolgoical atter.
One of the goals is how do we classify all phases of topological matter.
One of thoese asnswers is in terms of TQFT.
We're going to see how to think about phases fro mthe persepective of TQFT,
and know what to expect how to classify phases.


\section{The general solution of a linear system}

\begin{outcome}
  \begin{enumerate}
  \item Use linear transformations to determine the particular
    solution and general solution to a system of equations.
  \item Find the kernel of a linear transformation.
  \end{enumerate}
\end{outcome}

Recall the definition of a linear transformation discussed above.
$T$ is a \textbf{linear transformation} if whenever $\vect{x}, \vect{y}$ are
vectors and $k,p$ are scalars,
\begin{equation*}
T(k\vect{x}+p\vect{y}) =k T (\vect{x}) +p T(\vect{y})
\end{equation*}
Thus linear transformations distribute across addition and pass scalars to
the outside.

It turns out that we can use linear transformations to solve
systems of linear equations. Indeed given a system of linear equations of the
form $A\vect{x}=\vect{b}$, one may rephrase this as $T(\vect{x})=\vect{b}$ where $T$ is the linear
transformation $T_A$ induced by the coefficient matrix $A$. With this in mind consider the following definition.

\begin{definition}{Particular solution of a system of equations}{particular-solutions}
Suppose a system of linear equations can be written in the form
\begin{equation*}
T(\vect{x})=\vect{b}
\end{equation*}
If $T(\vect{x}_{p})=\vect{b}$,
then $\vect{x}_{p}$ is called a \textbf{particular solution}\index{particular solution} of
the linear system.
\end{definition}

Recall that a system is called homogeneous if every equation in the system is equal to $0$.
Suppose we represent a homogeneous system of equations by $T(\vect{x})=0$. It turns out
that the $\vect{x}$ for which $T (\vect{x}) = 0$ are part of a special set called the \textbf{null space}
of $T$. We may also refer to the null space as the \textbf{kernel} of $T$, and we write $ker(T)$.

Consider the following definition.

\begin{definition}{Null space or kernel of a linear transformation}{null-space-of-linear-transformation}
Let $T$ be a linear transformation. Define
\begin{equation*}
\ker (T) = \set{\vect{x}:T (\vect{x})= \vect{0} }
\end{equation*}
The kernel\index{kernel}, $\ker (T) $ consists of the set of all vectors $\vect{x}$ for which
$T (\vect{x}) = \vect{0}$. This is also called the
\textbf{null space}\index{null space} of $T$.
\end{definition}

We may also refer to the kernel of $T$ as the
\textbf{solution space}\index{solution space} of the equation $T (\vect{x}) = \vect{0}$.

Consider the following example.

\begin{example}{The kernel of the derivative}{kernel-derivative}
Let $\frac{d}{dx}$ denote the linear transformation defined on $f$, the functions
which are defined on $\R$ and have a continuous derivative. Find
$\ker \paren{\frac{d}{dx}}$.
\end{example}

\begin{solution} The example asks for functions $f$ which the property that $\frac{df}{dx}
=0. $ As you may know from calculus, these functions are the constant functions.
Thus $\ker \paren{\frac{d}{dx}}$ is the set of constant functions.
\end{solution}

Definition~\ref{def:null-space-of-linear-transformation} states that $\ker (T) $ is the set of
solutions to the equation,
\begin{equation*}
T(\vect{x}) = \vect{0}
\end{equation*}
Since we can write $T(\vect{x})$ as $A\vect{x}$, you have been solving such
equations for quite some time.

We have spent a lot of time finding solutions to systems of equations in general, as well as
homogeneous systems. Suppose we look at a system given by $A\vect{x}=\vect{b}$, and consider the
related homogeneous system. By this, we mean that we replace $\vect{b}$ by $\vect{0}$ and look at $A\vect{x}=\vect{0}$.
It turns out that there is a very important relationship between the solutions of the original
system and the solutions of the associated homogeneous system. In the following
theorem, we use linear transformations to denote a system of equations. Remember that
$T(\vect{x}) = A\vect{x}$.

\begin{theorem}{Particular solution and general solution}{particular-and-general-solution}
Suppose $\vect{x}_{p}$ is a solution to the linear system given by
\begin{equation*}
T(\vect{x}) = \vect{b}.
\end{equation*}
Then if $\vect{y}$ is any other solution to $T(\vect{x})=\vect{b}$,
\index{general solution!solution space} there exists $\vect{x}_0 \in \ker
(T) $ such that
\begin{equation*}
\vect{y} = \vect{x}_{p}+ \vect{x}_0.
\end{equation*}
Hence, every solution to the linear system can be written as a sum of a particular solution, $\vect{x}_p$,
 and a solution $\vect{x}_0$ to the associated
homogeneous system given by $T(\vect{x})=\vect{0}$.
\end{theorem}

\begin{proof}
Consider $\vect{y} - \vect{x}_{p}= \vect{y} + (
-1) \vect{x}_{p}$. Then $T(\vect{y} - \vect{x}_{p}) =T(\vect{y})
-T(\vect{x}_{p})$. Since $\vect{y}$ and $\vect{x}_{p}$ are both solutions to the system, it follows that $T(\vect{y})= \vect{b} $
and $T(\vect{x}_p) = \vect{b}$.

Hence, $T(\vect{y})-T(\vect{x}_{p})
=\vect{b} - \vect{b} = \vect{0}$.  Let $\vect{x}_0 = \vect{y} - \vect{x}_{p}$.
Then, $T(\vect{x}_0)= \vect{0} $ so $\vect{x}_0$ is a solution to the associated homogeneous system and so is in $\ker (T)$.
\end{proof}

Sometimes people remember the above theorem in the following form. The
solutions to the system $T(\vect{x})=\vect{b}$ are given by
$\vect{x}_{p}+\ker (T) $ where $\vect{x}_{p}$ is a particular
solution to $T(\vect{x})=\vect{b}$.

For now, we have been speaking about the kernel or null space of a linear transformation $T$. However,
we know that every linear transformation $T$ is determined by some matrix $A$. Therefore,
we can also speak about the null space of a matrix. Consider the following example.

\begin{example}{The null space of a matrix}{matrix-null-space}
Let
\begin{equation*}
A=\begin{mymatrix}{rrrr}
1 & 2 & 3 & 0 \\
2 & 1 & 1 & 2 \\
4 & 5 & 7 & 2
\end{mymatrix}
\end{equation*}
Find $\nullspace(A)$. Equivalently, find the solutions to the
system of equations $A\vect{x}=\vect{0}$.
\end{example}

\begin{solution}  We are asked to find $\set{\vect{x} : A\vect{x} = \vect{0}}$. In other
words we want to solve the system, $A\vect{x}=\vect{0}$. Let $\vect{x} =
\begin{mymatrix}{r}
x \\
y \\
z \\
w
\end{mymatrix}$. Then this amounts to solving
\begin{equation*}
\begin{mymatrix}{rrrr}
1 & 2 & 3 & 0 \\
2 & 1 & 1 & 2 \\
4 & 5 & 7 & 2
\end{mymatrix} \begin{mymatrix}{c}
x \\
y \\
z \\
w
\end{mymatrix} =\begin{mymatrix}{c}
0 \\
0 \\
0
\end{mymatrix}
\end{equation*}
This is the linear system
\begin{equation*}
\begin{array}{c}
x+2y+3z=0 \\
2x+y+z+2w=0 \\
4x+5y+7z+2w=0
\end{array}
\end{equation*}
To solve, set up the augmented matrix and row reduce to find the {\rref}.
\begin{equation*}
\begin{mymatrix}{rrrr|r}
1 & 2 & 3 & 0 & 0 \\
2 & 1 & 1 & 2 & 0 \\
4 & 5 & 7 & 2 & 0
\end{mymatrix}
\roweq\ldots\roweq
\begin{mymatrix}{rrrr|r}
1 & 0 & -
\vspace{0.05in}\frac{1}{3} & \vspace{0.05in}\frac{4}{3} &  0 \\
0 & 1 & \vspace{0.05in}\frac{5}{3} & -\vspace{0.05in}\frac{2}{3} & 0 \\
0 & 0 & 0 & 0 & 0
\end{mymatrix}
\end{equation*}
This yields $x=\vspace{0.05in}\frac{1}{3}z-\vspace{0.05in}\frac{4}{3}w$ and
$y=\vspace{0.05in}\frac{2}{3}w-\vspace{0.05in}\frac{5}{3}z$.
Since $\nullspace(A) $ consists of the solutions to this system, it consists vectors of the form,
\begin{equation*}
\begin{mymatrix}{c}
\vspace{0.05in}\frac{1}{3}z-\vspace{0.05in}\frac{4}{3}w \\
\vspace{0.05in}\frac{2}{3}w-\vspace{0.05in}\frac{5}{3}z \\
z \\
w
\end{mymatrix} =z \begin{mymatrix}{r}
\vspace{0.05in}\frac{1}{3} \\
-\vspace{0.05in}\frac{5}{3} \\
1 \\
0
\end{mymatrix} +w \begin{mymatrix}{r}
-\vspace{0.05in}\frac{4}{3} \\
\vspace{0.05in}\frac{2}{3} \\
0 \\
1
\end{mymatrix}
\end{equation*}
\end{solution}

Consider the following example.

\begin{example}{A general solution}{general-solution}
The \textbf{general solution}\index{general solution} of a system of linear equations
is the set of
all possible solutions. Find the general solution to the linear system,
\begin{equation*}
\begin{mymatrix}{rrrr}
1 & 2 & 3 & 0 \\
2 & 1 & 1 & 2 \\
4 & 5 & 7 & 2
\end{mymatrix} \begin{mymatrix}{r}
x \\
y \\
z \\
w
\end{mymatrix} =\begin{mymatrix}{r}
9 \\
7 \\
25
\end{mymatrix}
\end{equation*}
given that $\begin{mymatrix}{r}
x \\
y \\
z \\
w
\end{mymatrix}=\begin{mymatrix}{r}
1 \\
1 \\
2 \\
1
\end{mymatrix}$ is one solution.
\end{example}

\begin{solution} Note the matrix of this system is the same as the matrix in Example~\ref{exa:matrix-null-space}. Therefore, from Theorem~\ref{thm:particular-and-general-solution}, you will obtain all
solutions to the above linear system by adding a particular solution $\vect{x}_p$ to the solutions of the associated homogeneous
system, $\vect{x}$. One particular solution is given above by
\begin{equation}
\vect{x}_p
=
\begin{mymatrix}{r}
x \\
y \\
z \\
w
\end{mymatrix}=\begin{mymatrix}{r}
1 \\
1 \\
2 \\
1
\end{mymatrix}
\end{equation}

Using this particular solution along with the solutions found in Example~\ref{exa:matrix-null-space}, we
obtain the following solutions,
\begin{equation*}
z\begin{mymatrix}{r}
\vspace{0.05in}\frac{1}{3} \\
-\vspace{0.05in}\frac{5}{3} \\
1 \\
0
\end{mymatrix} +w\begin{mymatrix}{r}
-\vspace{0.05in}\frac{4}{3} \\
\vspace{0.05in}\frac{2}{3} \\
0 \\
1
\end{mymatrix} +\begin{mymatrix}{r}
1 \\
1 \\
2 \\
1
\end{mymatrix}
\end{equation*}

Hence, any solution to the above linear system is of this form.
\end{solution}