%!TEX root = da2020-10.tex

\Chapter{10}{Sinkless Orientation}

\noindent
In this chapter we will study the complexity of \emph{sinkless orientation}, a problem that was introduced in the previous chapter. This is a problem that is understood well: we will design algorithms and show that these are asymptotically optimal.

Recall that sinkless orientation is the problem of orienting the edges of the tree so that each internal node has got at least one outgoing edge. We begin by studying sinkless orientation on paths (or $(2,2)$-biregular trees), and show that we can easily argue about local neighborhoods to prove a tight lower bound result. However, when we switch to $(3,3)$-biregular trees, we will need the round elimination technique to do the same.

\section{Sinkless Orientation on Paths} \label{sec:so-paths}

We define \emph{sinkless orientation on paths} to be the following bipartite locally verifiable problem $\Pi = (\Sigma, \collA, \collP)$. The alphabet is $\Sigma = \{ \mI, \mO \}$, with the interpretation that $\mI$ indicates that the edge is oriented towards the active node (``incoming'') and $\mO$ indicates that the edge is oriented away from the active node (``outgoing''). Each active node must label at least one incident edge with $\mO$, and thus the active configurations are
\[
	\collA = \bigl\{ \, [\mO, \mI],\, [\mO, \mO] \, \bigr\}.
\]
Each passive node must have at least one incident edge labeled with $\mI$, and thus the passive configurations are
\[
	\collP = \bigl\{ \, [\mI, \mO],\,[\mI, \mI] \, \bigr\}.
\]
As previously, nodes of degree 1 are unconstrained; the edges incident to them can be labeled arbitrarily.

\subsection{Hardness of Sinkless Orientation}\label{ssec:so-hard-paths}

We begin by showing that solving sinkless orientation requires $\Omega(n)$ rounds on $(2,2)$-biregular trees.

\begin{figure}
	\centering
	\includegraphics[page=\PSOPathIntuition,scale=0.4]{figs.pdf}
	\caption{Propagation of a sinkless orientation on paths. Orienting a single edge (orange) forces the orientation of the path all the way to the other endpoint.} \label{fig:so-intuition}
\end{figure}

\begin{lemma} \label{lem:so-hard-paths}
	Solving sinkless orientation on $(2,2)$-biregular trees in the bipartite $\PN$-model requires at least $n/4-1$ rounds, even if the nodes know the value $n$.
\end{lemma}

Let us first see why the lemma is intuitively true. Consider a path, as illustrated in Figure~\ref{fig:so-intuition}. Each active node $u$ must choose some label for its incident edges, and at least one of these labels must be $\mO$. Then its passive neighbor $v$ over the edge labeled with $\mO$ must have its other incident edge labeled $\mI$. This further implies that the other active neighbor $w$ of $v$ must label its other edge with $\mO$. The original output of $u$ \emph{propagates} through the path and the outputs of other nodes far away from $u$ depend on the output of $u$.

\begin{figure}
	\centering
	\includegraphics[page=\PSOPathLBConstruction,scale=0.4]{figs.pdf}
	\caption{Sinkless orientation lower bound. Assume $T = 4$. Top: any algorithm must fix some output labeling with an outgoing edge for a fixed neighborhood $\ball_N(v,4)$. Bottom: Copying the same 4-neighborhood twice, and arranging the copies towards the same node creates an input $N'$ where the two nodes orient towards the middle. There is no legal way to label the rest of the path.} \label{fig:so-path-lb}
\end{figure}

Let us now formalize this intuition.

\begin{proof}[Proof of Lemma~\ref{lem:so-hard-paths}]
	Consider any algorithm $A$ running in $T(n) = o(n)$ rounds. Then there exists $n_0$ such that for all $n \geq n_0$, we have that $T(n) \leq (n-5)/4$. Now fix such an integer $n$ and let $T = T(n)$ denote the running time of the algorithm.

	Consider an active node $v$ in the middle of a path $N$ on $n$ nodes. Let $\ball_N(v,T)$ denote the $T$-neighborhood of $v$. Assume that $\ball_N(v,T)$ is consistently port-numbered from \emph{left} to \emph{right}, as illustrated in Figure~\ref{fig:so-path-lb}. Node $v$ must use the output label $\mO$ on one of its incident edges; without loss of generality, assume that this is port~$1$. We can now construct a counterexample $N'$ as follows. Take two copies of $\ball_N(v,T)$, denoted by $\ball_{N'}(v_1,T)$ and $\ball_{N'}(v_2,T)$. In particular, this includes the port-numbering in $\ball_N(v,T)$.
	Add one new node that is connected to the \emph{right} endpoint of both $\ball_{N'}(v_1,T)$ and $\ball_{N'}(v_2,T)$. Finally, add leaves at the other endpoints of $\ball_{N'}(v_1,T)$ and $\ball_{N'}(v_2,T)$; see Figure~\ref{fig:so-path-lb} for an illustration. We claim that the algorithm $A$ fails on $N'$.

	By definition, edges must be labeled alternatively $\mO$ and $\mI$ starting from both $v_1$ and $v_2$. Therefore we must eventually find an active node labeled $[\mI, \mI]$ or a passive node labeled $[\mO, \mO]$, an incorrect solution.

	The total number of nodes in $N'$ is $n = 2(2T+1)+3 = 4T + 5$, giving $T = (n-5)/4$. Thus solving sinkless orientation requires at least $T+1 = (n-5)/4 + 1 \geq n/4 - 1$ rounds, as required.
\end{proof}

\subsection{Solving Sinkless Orientation on Paths} \label{ssec:so-alg-path}

The proof of Lemma~\ref{lem:so-hard-paths} shows that it is impossible to solve sinkless orientation on paths in sublinear number of rounds. Now we will show a linear upper bound: it is possible to solve sinkless orientation once all nodes see an endpoint of the path.

\begin{lemma}\label{lem:so-path-ub}
	Sinkless orientation can be solved in $\lfloor n/2 \rfloor$ rounds in the bipartite $\PN$-model on $(2,2)$-biregular trees.
\end{lemma}

\begin{proof}
	The idea of the algorithm is the following: initially send messages from the leaves towards the middle of the path. Orient edges against the incoming messages, i.e., toward the closest leaf. Once the messages reach the midpoint of the path, all edges have been correctly oriented away from the midpoint.

	The algorithm works as follows. Initially all non-leaf nodes wait. The leaf nodes send a message to their neighbor and stop. If they are active, they output $\mI$ on their incident edge.
	Whenever a node receives a message for the first time, in the next round it sends a message to the other port and stops. If it is an active node, it outputs $\mO$ in the port from which it received the message, and $\mI$ in the other port. That is, it orients its incident edges towards the closer leaf. If a node receives two messages in the same round, it is the midpoint of the path; it does not send any further messages. If it is an active node, it outputs $\mO$ on both of its incident edges.

	The algorithm runs in $\lfloor n/2 \rfloor$ rounds: on paths with an even number of nodes, all nodes have received a message in round $n/2 - 1$, and thus stop in the next round. On paths with an odd number of nodes, the middle node receives two messages in round $\lfloor n/2 \rfloor$ and stops.

	It remains to show that our algorithm is correct. All leaf nodes are trivially labeled correctly. Any active non-leaf node always has an incident label $\mO$. Now consider a passive node $u$: there is an active $v$ that sends $u$ a message before stopping. This node will output $\mI$ on $\{u,v\}$, and thus $u$ is also correctly labeled.
\end{proof}

\begin{theorem}
	The complexity of sinkless orientation on paths is $\Theta(n)$.
\end{theorem}

\begin{proof}
	Follows from Lemmas \ref{lem:so-hard-paths} and \ref{lem:so-path-ub}.
\end{proof}

\section{Sinkless Orientation on Trees}

In Section~\ref{sec:so-paths} we saw that if we require that degree-$2$ nodes have at least one outgoing edge, we arrive at a problem that is hard already in the case of paths. The proof of hardness was a straightforward argument that used local neighborhoods.

However, what happens if we relax the problem slightly and allow any orientation around degree-$2$ nodes? The proof of hardness from Section~\ref{ssec:so-hard-paths} no longer works, but does the problem get easier to solve?

For concreteness, let us consider \emph{trees of maximum degree $3$}, that is, both active and passive nodes have degree at most $3$; the case of higher degrees is very similar. We define the problem so that nodes of degree $1$ and $2$ are unconstrained, but nodes of degree $3$ must have at least one outgoing edge. We can encode it as follows as a bipartite locally verifiable problem $\Pi_0 = (\Sigma_0, \collA_0, \collP_0)$:
\begin{align*}
	\Sigma_0 &= \{ \mO, \mI \}, \\
	\collA_0 &= \bigl\{ \, [\mO],\,[\mI],\,[\mO, \mO],\,[\mO,\mI],\,[\mI,\mI],\,[\mO,\mI,\mI],\,[\mO,\mO,\mI],\,[\mO,\mO,\mO] \, \bigr\}, \\
	\collP_0 &= \bigl\{ \, [\mO],\,[\mI],\,[\mO, \mO],\,[\mO,\mI],\,[\mI,\mI],\,[\mI,\mO,\mO],\,[\mI,\mI,\mO],\,[\mI,\mI,\mI] \, \bigr\}.
\end{align*}
Here we have listed all possible configurations for nodes of degrees $1$, $2$, and $3$.

\subsection{Solving Sinkless Orientation on Trees} \label{ssec:so-trees-alg}

The algorithm for solving sinkless orientation on trees uses ideas similar to the algorithm on paths: each node $u$ must determine the closest unconstrained node $v$, i.e., a node of degree $1$ or $2$, and the path from $u$ to $v$ is oriented away from $u$. This will make all nodes happy: each active node of degree $3$ has an outgoing edge, and all other nodes are unconstrained.

Let us call nodes of degree 1 and 2 \emph{special nodes}. We must be careful in how the nodes choose the special node: the algorithm would fail if two nodes want to orient the same edge in different directions.

The algorithm functions as follows. In the first round, only special nodes are sending messages, broadcasting to each port. Then the special nodes stop and, if they are active nodes, they output $\mI$ on each edge. Nodes of degree 3 \emph{wake up} in the first round in which they receive at least one message. In the next round they broadcast to each port from which they did not receive a message in the previous round. After sending this message, the nodes stop. If they are active nodes, they orient their incident edges towards the smallest port from which they received a message: output $\mO$ on that edge, and $\mI$ on the other edges.

\paragraph{Correctness.} Assume that the closest special nodes are at distance $t$ from some node $u$. Assume that $v$ is one of those nodes, and let $(v_1, v_2, \dotsc, v_{t+1})$ denote the unique path from $v = v_1$ to $u = v_{t+1}$. We claim that in each round $i$, node $v_{i}$ broadcasts to $v_{i+1}$. By assumption, $v$ is also one of the closest special nodes to all $v_i$; otherwise there would be a closer special node to $u$ as well.
In particular, there will never be a broadcast from $v_{i+1}$ to $v_i$, as then $v_{i+1}$ would have a different closer special node. Therefore each $v_i$ will broadcast to $v_{i+1}$ in round $i$. This implies that in round $t$, node $u$ will receive a broadcast from $v_t$.

All nodes that receive a broadcast become happy: Active nodes output $\mO$ on one of the edges from which they received a broadcast, making them happy. They output $\mI$ on the other edges, so each passive node is guaranteed that every edge from which it receives a broadcast has the label $\mI$.

\paragraph{Time Complexity.} It remains to bound the round by which all nodes have received a broadcast. To do this, we observe that each node is at distance $O(\log n)$ from a special node.

Consider a fragment of a $3$-regular tree centered around some node~$v$, and assume that there are no special nodes near~$v$. Then at distance $1$ from $v$ there are $3$ nodes, at distance $2$ there are $6$ nodes, at distance $3$ there are $12$ nodes, and so on. In general, if we do not have any special nodes within distance $i$, then at distance $i$ there are
$
	3\cdot2^{i-1} > 2^i
$
nodes in the tree. At distance $i = \log_2 n$, we would have more than $n$ nodes. Thus, within distance $\log_2 n$, there has to be a special node. Since each node can stop once it has received a broadcast, the running time of the algorithm is $O(\log n)$.

\subsection{Roadmap: Next Steps}

We have seen that sinkless orientation in trees can be solved in $O(\log n)$ rounds. We would like to now prove a matching lower bound and show that sinkless orientation cannot be solved in $o(\log n)$ rounds. We will apply the round elimination technique from Chapter~9 to do this. However, we will need one further refinement to the round elimination technique that will make our life a lot easier: we can \emph{ignore all non-maximal configurations}. We will explain this idea in detail in Section~\ref{sec:re-maximal}, and then we are ready to prove the hardness result in Section~\ref{sec:so-hard-trees}.

\section{Maximal Output Problems}\label{sec:re-maximal}

In Chapter~\chapterref{9} we saw how to use the round elimination technique to construct the \emph{output problem} $\Pi' = \re(\Pi)$ for any given bipartite locally verifiable problem $\Pi$. We will now make an observation that allows us to \emph{simplify} the description of output problems. We will change the definition of output problems to include this simplification.

Consider an output problem $\Pi' = (\Sigma, \collA, \collP)$. Recall that $\Sigma$ now consists of \emph{sets} of labels. Assume that there are two configurations
\begin{align*}
X &= [X_1, X_2, \dotsc, X_d], \\
Y &= [Y_1, Y_2, \dotsc, Y_d],
\end{align*}
in $\collA$. We say that $Y$ \emph{contains} $X$ if we have $X_i \subseteq Y_i$ for all $i$.

If $Y$ contains $X$, then configuration $X$ is redundant; whenever an algorithm solving $\Pi'$ would like to use the configuration $X$, it can equally well use $Y$ instead:
\begin{itemize}
	\item Active nodes are still happy if active nodes switch from $X$ to $Y$: By assumption, $Y$ is also a configuration in $\collA$.
	\item Passive nodes are still happy if active nodes switch from $X$ to $Y$: Assume that $Z = [Z_1, Z_2, \dotsc, Z_\delta]$ is a passive configuration in $\collP$. As this is a passive configuration of $\re(\Pi)$, it means that there is a choice $z_i \in Z_i$ such that $[z_1, z_2, \dotsc, z_\delta]$ is an active configuration in the original problem $\Pi$. But now if we replace each $Z_i$ with a superset $Z'_i \supseteq Z_i$, then we can still make the same choice $z_i \in Z'_i$, and hence $Z' = [Z'_1, Z'_2, \dotsc, Z'_\delta]$ also has to be a passive configuration in $\collP$. Therefore replacing a label with its superset is always fine from the perspective of passive nodes, and in particular switching from $X$ to $Y$ is fine.
\end{itemize}
Therefore we can \emph{omit} all active configurations that are contained in another active configuration and only include the \emph{maximal} configurations, i.e., configurations that are not contained in any other configuration.

We get the following mechanical process for constructing the output problem $\re(\Pi) = (\Sigma, \collA, \collP)$.
\begin{enumerate}[noitemsep]
	\item Construct the output problem $\re(\Pi) = (\Sigma, \collA, \collP)$ as described in Section~\longref{9.2.2}{ssec:output-problems}.
	\item Remove all non-maximal active configurations from $\collA$.
	\item Remove all unused elements from $\Sigma$.
	\item Remove all passive configurations containing labels not in $\Sigma$.
\end{enumerate}

The resulting problem is exactly as hard to solve as the original problem:
\begin{itemize}
	\item Since the simplified sets of configurations are subsets of the original sets of configurations, any solution to the simplified problem is a solution to the original problem, and thus the original problem is  \emph{at most as hard as} the simplified problem.
	\item By construction, any algorithm solving the original output problem can solve the simplified problem equally fast, by replacing some labels by their supersets as appropriate. Therefore the original problem is \emph{at least as hard as} the simplified problem.
\end{itemize}
We will apply this simplification when we use the round elimination technique to analyze the sinkless orientation problem.

\section{Hardness of Sinkless Orientation on Trees}\label{sec:so-hard-trees}

We will now show that sinkless orientation requires $\Omega(\log n)$ rounds on $(3,3)$-biregular trees\mydash and therefore also in trees of maximum degree $3$, as $(3,3)$-biregular trees are a special case of such trees.

Let us first write down the sinkless orientation problem as a bipartite locally verifiable problem $\Pi_0 = (\Sigma_0, \collA_0, \collP_0)$ in $(3,3)$-biregular trees; as before, we will only keep track of the configurations for nodes of degree~$3$, as leaf nodes are unconstrained:
\begin{align*}
\Sigma_0 &= \{ \mO, \mI \}, \\
\collA_0 &= \bigl\{ \, [\mO, x, y] \bigm| x, y \in \Sigma \, \bigr\}, \\
\collP_0 &= \bigl\{ \, [\mI, x, y] \bigm| x, y \in \Sigma \, \bigr\}.
\end{align*}

\subsection{First Step}

\begin{lemma}
	Let $\Pi_0$ be the sinkless orientation problem. Then the output problem is $\Pi_1 = \re(\Pi_0) = (\Sigma_1, \collA_1, \collP_1)$, where
	\begin{align*}
		\Sigma_1 &= \bigl\{ \, \{ \mI \},\, \{ \mO, \mI \} \, \bigr\}, \\
		\collA_1 &= \Bigl\{ \, \bigl[\{ \mI \},\, \{ \mO, \mI \},\, \{ \mO, \mI \} \bigr] \, \Bigr\}, \\
		\collP_1 &= \Bigl\{ \, \bigl[\{ \mO, \mI \}, x, y\bigr] \Bigm| x,y \in \Sigma_1 \, \Bigr\}.
	\end{align*}
\end{lemma}

\begin{proof}
	Let us follow the procedure from Section~\ref{sec:re-maximal}. First, we arrive at alphabet $\Sigma_1$ that contains all non-empty subsets of $\Sigma_0$:
	\[
		\Sigma_1 = \bigl\{\, \{\mO\},\,\{\mI\},\,\{\mO,\mI\} \,\bigr\}.
	\]
	The active configurations $\collA_1$ consist of all multisets $[X, Y, Z]$ such that no matter how we choose $x \in X$, $y \in Y$, and $z \in Z$, at least one element of the multiset $[x,y,z]$ is $\mI$. This happens exactly when at least one of the labels $X$, $Y$, and $Z$ is the singleton set $\{ \mI \}$. We get that
	\begin{align*}
		\collA_1 = \Bigl\{\, & [ \{ \mI \}, X, Y ] \bigm| X,Y \subseteq \{ \mO, \mI \} \, \Bigr\} \\
		= \Bigl\{\,
		&\bigl[\{ \mI \},\, \{\mI \},\, \{ \mI \}\bigr],\\
		&\bigl[\{\mI\},\, \{\mI\},\, \{ \mO \}\bigr],\\
		&\bigl[\{\mI\},\,\{\mI\},\,\{\mO,\mI\} \bigr],\\
		&\bigl[\{ \mI \},\, \{\mO \},\,\{ \mO \}\bigr],\\
		&\bigl[\{ \mI \},\, \{\mO \},\,\{ \mO, \mI \}\bigr], \\
		&\bigl[\{\mI\},\, \{ \mO, \mI \},\, \{ \mO, \mI \}\bigr] \, \Bigr\}.
	\end{align*}

	Next we remove all non-maximal configurations. We note that all other active configurations are contained in the configuration
	\[
	\bigl[\{ \mI \},\, \{ \mO, \mI \},\, \{ \mO, \mI \} \bigr].
	\]
	This becomes the only active configuration:
	\[
		\collA_1 = \Bigl\{ \, \bigl[\{ \mI \},\, \{ \mO, \mI \},\, \{ \mO, \mI \} \bigr] \, \Bigr\}.
	\]
	Since the label $\{ \mO \}$ is never used, we may remove it from the alphabet, too: we get that
	\[
		\Sigma_1 = \bigl\{\, \{\mI\}, \{\mO, \mI \} \,\bigr\}.
	\]

	The passive configurations are all multisets such that at least one label contains $\mO$. Thus the simplified passive configurations are
	\begin{align*}
		\collP_1 = \Bigl\{\,
		&\bigl[\{ \mO, \mI \},\, \{ \mO, \mI \},\, \{ \mO, \mI \}\bigr],\\
		&\bigl[\{ \mO, \mI \},\, \{ \mO, \mI \},\,\{ \mI \}\bigr],\\
		&\bigl[\{ \mO, \mI \},\,\{\mI\},\,\{\mI\}\bigr] \, \Bigr\}. \qedhere
	\end{align*}
\end{proof}

\subsection{Equivalent Formulation}\label{ssec:so-output-equiv}

Now let us simplify the notation slightly. We say that a problem $\Pi'$ is \emph{equivalent} to another problem $\Pi$ if a solution of $\Pi'$ can be converted in zero rounds to a solution of $\Pi$ and vice versa. In particular, equivalent problems are exactly as hard to solve.

\begin{lemma}
	Let $\Pi_0$ be the sinkless orientation problem. Then the output problem $\re(\Pi_0)$ is equivalent to $\Pi_1 = (\Sigma_1, \collA_1, \collP_1)$, where
	\begin{align*}
		\Sigma_1 &= \{ \mA, \mB \}, \\
		\collA_1 &= \bigl\{ \, [\mA, \mB, \mB ] \, \bigr\}, \\
		\collP_1 &= \bigl\{ \, [\mB, x, y] \bigm| x,y \in \Sigma_1 \, \bigr\}.
	\end{align*}
\end{lemma}
\begin{proof}
	Rename the labels of $\re(\Pi_0)$ as follows to arrive at $\Pi_1$:
	\begin{align*}
		\mA &= \{ \mI \},\\
		\mB &= \{ \mO, \mI \}. \qedhere
	\end{align*}
\end{proof}

\noindent In what follows, we will use $\Pi_1$ and $\re(\Pi_0)$ interchangeably, as they are equivalent.

\subsection{Fixed Points in Round Elimination} \label{ssec:re-failure}

As we will see soon, problem $\Pi_1$ is a \emph{fixed point} in round elimination: when round elimination is applied to $\Pi_1$, the output problem is again~$\Pi_1$ (or, more precisely, a problem equivalent to $\Pi_1$).

This means that if we assume that $\Pi_1$ can be solved in $T$ rounds, then by applying round elimination $T$ times we get a $0$-round algorithm for $\Pi_1$. It can be shown that $\Pi_1$ is \emph{not} $0$-round solvable. This would seem to imply that $\Pi_1$, and thus sinkless orientation, are not solvable at all, which would contradict the existence of the $O(\log n)$-time algorithm presented in Section~\ref{ssec:so-trees-alg}!

To resolve this apparent contradiction, we must take a closer look at the assumptions that we have made. The key step in round elimination happens when a node $u$ simulates the \emph{possible} outputs of its neighbors. The correctness of this step assumes that the possible $T$-neighborhoods of the neighbors are \emph{independent} given the $(T-1)$-neighborhood of $u$. When $T$ is so large in comparison with $n$ that the $T$-neighborhoods of the neighbors put together might already imply the existence of more than $n$ nodes, this assumption no longer holds\mydash see Figure~\ref{fig:full-tree} for an example.

\begin{figure}
	\centering
	\includegraphics[page=\PSOFullTree,scale=0.3]{figs.pdf}
	\caption{If we know that, e.g., $n = 35$, then orange, green, and blue extensions are no longer independent of each other: inputs (a) and (b) are possible but we cannot combine them arbitrarily to form e.g.\ input~(c).} \label{fig:full-tree}
\end{figure}

For the remainder of this chapter we consider algorithms that know the value $n$, the number of nodes in the graph. This allows us to define a \emph{standard form} for algorithms that run in $T = T(n)$ rounds, where $T(n)$ is some function of $n$: since $n$ is known, each node can calculate $T(n)$, gather everything up to distance $T(n)$, and simulate any $T(n)$-time algorithm.

In $(d,\delta)$-biregular trees, where $d > 2$, it can be shown that round elimination can be applied if the initial algorithm is assumed to have running time $T(n) = o(\log n)$: this guarantees the independence property in the simulation step. However, round elimination fails for some function $T(n) = \Theta(\log n)$; calculating this threshold is left as Exercise~\ref{ex:re-boundary}.

Any problem that can be solved in time $T(n)$ can be solved in time $T(n)$ with an algorithm in the standard form. If the problem is a fixed point and $T(n) = o(\log n)$, we can apply round elimination. We get the following lemma.

\begin{lemma}
	Assume that bipartite locally verifiable problem $\Pi$ on $(d,d)$-biregular trees, for $d>2$, is a fixed point. Then the deterministic complexity of $\Pi$ in the bipartite $\PN$-model is either $0$ rounds or $\Omega(\log n)$ rounds, even if the number of nodes $n$ is known.
\end{lemma}

\subsection{Sinkless Orientation Gives a Fixed Point}

We will now show that the output problem $\Pi_1 = \re(\Pi_0)$ of the sinkless orientation problem $\Pi_0$ is a fixed point, that is, $\re(\Pi_1)$ is a problem equivalent to $\Pi_1$ itself. Since this problem cannot be solved in 0 rounds, it requires $\Omega(\log n)$ rounds. As sinkless orientation requires, by definition, one more round than its output problem, sinkless orientation also requires $\Omega(\log n)$ rounds.

\begin{lemma}
	The output problem $\Pi_1 = (\Sigma_1, \collA_1, \collP_1)$ of sinkless orientation, given by
	\begin{align*}
		\Sigma_1 &= \{ \mA, \mB \}, \\
		\collA_1 &= \bigl\{ \, [\mA, \mB, \mB ] \, \bigr\}, \\
		\collP_1 &= \bigl\{ \, [\mB, x, y] \bigm| x,y \in \Sigma_1 \, \bigr\},
	\end{align*}
	is a fixed point.
\end{lemma}

\begin{proof}
	Let $\Pi_2 = \re(\Pi_1) = (\Sigma_2, \collA_2, \collP_2)$ denote the output problem of $\Pi_1$. Again, we have that
	\[
		\Sigma_2 = \bigl\{ \, \{ \mA \}, \{ \mB \}, \{ \mA, \mB \} \, \bigl\}.
	\]
	The active configurations $\collA_2$ are
	\[
		\collA_2 = \Bigl\{\, \bigl[ \{ \mB \}, x, y \bigr] \Bigm| x,y \subseteq \{ \mA, \mB \}\,  \Bigl\}.
	\]
	That is, one set must be the singleton $\{ \mB \}$ to satisfy $\collP_1$ for \emph{all choices}, and the remaining sets are arbitrary.

	Next we determine the maximal configurations. Again, there is a single active configuration that covers the other configurations:
	\[
		\collA_2 = \Bigl\{\,  \bigl[\{\mB \}, \{ \mA,\mB\}, \{\mA, \mB \}\bigr]\,  \Bigr\}.
	\]
	The alphabet is immediately simplified to
	\[
		\Sigma_2 = \bigl\{\, \{\mB \}, \{\mA, \mB \} \,\bigr\},
	\]
	as the label $\{\mA \}$ is never used by any active configuration.

	The passive configurations $\collP_2$ are all multisets that contain the active configuration $[\mA, \mB, \mB]$. Since $\mA$ is now only contained in $\{ \mA, \mB \}$, we get that
	\begin{align*}
		\collP_2 = \Bigl\{\,
		&\bigl[\{ \mA, \mB \},\, \{ \mA, \mB \},\, \{ \mA, \mB \}\bigr], \\
		&\bigl[\{ \mA, \mB \},\, \{ \mA, \mB \},\, \{ \mB \}\bigr], \\
		&\bigl[\{ \mA, \mB \},\, \{ \mB \},\, \{ \mB \}\bigr] \, \Bigr\}.
	\end{align*}
	Now we may do a simple renaming trick to see that $\Pi_2$ is equivalent to $\Pi_1$: rename $\{ \mB \} \to \mA$ and $\{\mA,\mB\} \to \mB$. Written this way, we have that $\Pi_2$ is equivalent to the following problem:
	\begin{align*}
		\Sigma_2 &= \{ \mA, \mB \}, \\
		\collA_2 &= \bigl\{\,  [\mA, \mB, \mB ] \,\bigr\}, \\
		\collP_2 &= \bigl\{\,  [\mB, x, y] \bigm| x,y \in \Sigma_2 \,\bigr\},
	\end{align*}
	which is exactly the same problem as $\Pi_1$.
\end{proof}

\section{Quiz}

Calculate the \emph{number of different $2$-round algorithms} in the $\PN$ model on $(3,3)$-biregular trees for bipartite locally verifiable problems with the binary alphabet $\Sigma = \{0,1\}$.

Here two algorithms $A_1$ and $A_2$ are considered to be \emph{different} if there is some port-numbered network $N$ and some edge $e$ such that the label edge $e$ in the output of $A_1$ is different from the label of the same edge $e$ in algorithm $A_2$. Note that $A_1$ and $A_2$ might solve the same problem, they just solved it differently. Please remember to take into account that in a $(3,3)$-biregular tree each node has degree $1$ or $3$.

Please give your answer in the scientific notation with two significant digits: your answer should be a number of the form  $a \cdot 10^b$, where $a$ is rounded to two decimal digits, we have $1 \le a < 10$, and $b$ is a natural number.

\section{Exercises}

\begin{ex}[0-round solvability]
	Prove that the following problems are not 0-round solvable. \begin{subex}
		\item $(d+1)$-edge coloring in $(d,d)$-biregular trees (see Section~\longref{9.1.2}{ssec:bipartite-examples}).
		\item Maximal matching in $(d,d)$-biregular trees (see Section~\longref{9.1.2}{ssec:bipartite-examples}).
		\item $\Pi_1$, the output problem of sinkless orientation, in $(3,3)$-biregular trees (see Section~\ref{ssec:so-output-equiv}).
	\end{subex}
	\hint{Show that a 0-round algorithm consists of choosing one active configuration and assigning its labels to the ports. Show that for any way of assigning the outputs to the ports, there exists a port numbering such that a the incident edges of a passive node are not labeled according to any passive configuration.}
\end{ex}

\begin{ex}[higher degrees]
	Generalize the sinkless orientation problem from $(3,3)$-biregular trees to $(10,10)$-biregular trees. Apply round elimination and show that you get again a fixed point.
\end{ex}

\begin{ex}[non-bipartite sinkless orientation]
	Define non-bipartite sinkless orientation as the following problem $\Pi = (\Sigma, \collA, \collP)$ on $(3,2)$-biregular trees:
	\begin{align*}
		\Sigma &= \{ \mO, \mI \}, \\
		\collA &= \bigl\{\,  [\mO, x, y ] \bigm| x,y \in \Sigma \,\bigr\}, \\
		\collP &= \bigl\{\,  [\mI, \mO] \, \bigr\}.
	\end{align*}
	Prove that applying round elimination to $\Pi$ leads to a period-$2$ point, that is, to a problem $\Pi'$ such that $\Pi' = \re(\re(\Pi'))$.
\end{ex}

\begin{ex}[matching lower bound]\label{ex:pm-lb}
	Let us define \emph{sloppy perfect matching} in trees as a matching such that all non-leaf nodes are matched. Encode this problem as a bipartite locally verifiable problem on $(3,3)$-biregular trees. Show that solving it requires $\Omega(\log n)$ rounds in the $\PN$-model with deterministic algorithms.
\end{ex}

\begin{ex}[matching upper bound]\label{ex:pm-ub}
	Consider the sloppy perfect matching problem from Exercise~\ref{ex:pm-lb}.
	Design an algorithm for solving it with a deterministic $\PN$-algorithm on $(3,3)$-biregular trees in $O(\log n)$ rounds.
	\hint{Decompose the graph into \emph{layers} $(V_0, V_1, \dots, V_L)$, where nodes in layer $i$ have distance $i$ to the closest leaf. Then iteratively solve the problem, starting from layer $V_L$: match all nodes in layer $V_L$, $V_{L-1}$, and so on.}
\end{ex}

\begin{ex}[sinkless and sourceless]\label{ex:sinkless-sourceless}
	Sinkless and sourceless orientation is the problem of orienting the edges of the graph so that each non-leaf node has at least one outgoing edge \emph{and} at least one incoming edge.
	Encode the sinkless and sourceless orientation problem as a binary locally verifiable labeling problem on $(5,5)$-biregular trees.
	Design an algorithm for solving sinkless and sourceless orientation on $(5,5)$-biregular trees.
\end{ex}

\begin{ex}[failure of round elimination] \label{ex:re-boundary}
	In Section~\ref{ssec:re-failure} we discussed that round elimination fails if in the simulation step the $T(n)$-neighborhoods of the neighbors are dependent from each other. This happens when there exist $T$-neighborhoods of the neighbors such that the resulting tree would have more than $n$ nodes.
	Consider $(d,d)$-biregular trees. Calculate the bound for $F(n)$ such that round elimination fails for algorithms with running time $T(n) \geq F(n)$.
\end{ex}

\begin{exs}\label{ex:sinkless-sourceless-3}
	Design an algorithm for solving sinkless and sourceless orientation on $(3,3)$-biregular trees.
\end{exs}

\section{Bibliographic Notes}

Brandt et al.\ \cite{brandt16lll} introduced the sinkless orientation problem and proved that it cannot be solved in $o(\log\log n)$ rounds with randomized algorithms, while Chang et al.\ \cite{chang16exponential} showed that it cannot be solved in $o(\log n)$ rounds with deterministic algorithms. Ghaffari and Su \cite{ghaffari17distributed} gave matching upper bounds.

Exercises \ref{ex:pm-lb} and \ref{ex:pm-ub} are inspired by \cite{balliu20binary-labeling}, and Exercises \ref{ex:sinkless-sourceless} and \ref{ex:sinkless-sourceless-3} are inspired by \cite{ghaffari19degree-splitting}.


% file: problems/sorting-lower-bound.tex

\section{Lower Bound for Comparison-based Sorting (UD Problem 6.13)}

Prove a lower bound of $O(n \lg n)$ on the time complexity of any comparison-based sorting algorithm.

\subsection{Understanding the Problem}

First of all, this problem is \emph{flawed}, if not wrong.
\marginnote{I am not surprised at all if you are good at proving something wrong to be a theorem.}
It should be $\Omega(n \lg n)$, instead of $O(n \lg n)$.

{\bf Comparison-based Sorting.}

\begin{itemize}
  \item We only consider the sorting algorithms in which
    \marginnote{Counting sort, radix sort, or bucket sort is not a comparison-based sorting algorithm.}
    comparison is the only way to obtain order information between elements; and
  \item Comparisons of elements are the critical operations we care about.
    \marginnote{In \href{https://en.wikipedia.org/wiki/Pancake\_sorting}{pancake sorting},
    we are interested in the minimum number of flips (or technically, prefix reversals).\cite{Gates:DM1979}}
\end{itemize}

{\bf Time complexity $O(n \lg n)$.} Algorithms on the inputs of size $n$.

\subsection{Solution}

\subsection{Comments}


\chapter{Non-Relativistic Quantum Mechanics}
Here an overview of some basic results from Schr{\"o}dinger and Heisenberg's quantum mechanics are given. The quantum harmonic oscillator (QHO) is solved exactly, and extended to the anharmonic case with first-order perturbation theory. Lagrangian mechanics is briefly covered, before some consideration of the important Dirac $\delta$ and Heaviside $\theta$ step functions.

A time-varying quantum state my be expressed by a vector $\ket{\psi(t)}$. In the position representation, this yields the wavefunction, $\psi(x, t) \equiv \braket{x}{\psi(t)}$.

\section{Schr{\"o}dinger's equation and probability current density}
We start from the classical energy-momentum relation for a non-relativistic particle with mass $m$,
\begin{equation}
E = \frac{p^2}{2m}. \label{eq:energyMomentum}
\end{equation}
Replace the energy with the operator $E\rightarrow\hat{E} = i \frac{\partial}{\partial t}$ and momentum with $p\rightarrow \hat{p} = -i \vec{\nabla}$. Then \eqref{eq:energyMomentum} becomes the time-dependent Schr dinger equation (TDSE),
\begin{equation}
i \frac{\partial \psi}{\partial t} = -\frac{1}{2m} \nabla^2 \psi \label{eq:schrodinger}.
\end{equation}
Multiplying this by the complex conjugate of the wavefunction, $\psi^*$,
\begin{equation}
i \psi^* \frac{\partial \psi}{\partial t} = -\frac{1}{2m} \psi^* \nabla^2 \psi \label{eq:part1}.
\end{equation}
Take the complex conjugate of \eqref{eq:schrodinger} and multiply by $\psi$,
\begin{equation}
-i \frac{\partial \psi^*}{\partial t} \psi = -\frac{1}{2m} (\nabla^2 \psi^*) \psi \label{eq:part2}.
\end{equation}
Subtracting \eqref{eq:part2} from \eqref{eq:part1},
\begin{equation}
i\left( \psi^*\frac{\partial \psi}{\partial t} + \frac{\partial \psi^*}{\partial t} \psi \right) = -\frac{1}{2m} \left[ \psi^* (\nabla^2 \psi) - \psi (\nabla^2 \psi^*) \right]
\end{equation}
and rearranging using the product rule,
\begin{equation}
\frac{\partial}{\partial t}(\psi^*\psi) + \frac{i}{2m} \vec{\nabla}\cdot \left[ \psi(\vec{\nabla}\psi^*) - (\vec{\nabla}\psi)\psi^* \right] = 0.
\end{equation}
This is nothing but a continuity equation for the probability density $\rho = \psi^*\psi = \abs{\psi}^2$ and the probability current density $\vec{j} = \frac{i}{2m}\left[ \psi(\vec{\nabla}\psi^*) - (\vec{\nabla}\psi)\psi^* \right]$.

\subsubsection{Application to a plane wave}
Consider a free plane wave whose wavefunction is given by $\psi(x, t) = \mathcal{N} e^{i(\vec{p}\cdot\vec{x} - Et)}$, where $\mathcal{N}$ is a normalisation constant. Then, applying the definitions for $\rho$ and $\vec{j}$ above,
\begin{align}
\rho &= \abs{\psi}^2 = \abs{N}^2 \\
\vec{j} &= \frac{i}{2m}\left[ \psi(\vec{\nabla}\psi^*) - (\vec{\nabla}\psi)\psi^* \right] = \frac{\abs{N}^2}{m} \vec{p}
\end{align}

\section{Heisenberg and interaction pictures}\
\subsection{Heisenberg picture}
In the above, we have used the Schr{\"o}dinger picture where operators remain constant in time, and the states can vary. In contrast, the Heisenberg picture treats the states as constants, and the operators evolve in time. To show that these are equivalent, solve the time-independent Schr{\"o}dinger equation for an energy eigenstate $\ket{\psi(t)}$.
\begin{align}
i \frac{\dd \ket{\psi(t)}}{\dd t} &= \hat{H} \ket{\psi(t)} \\
\Rightarrow\quad \int\limits_0^t \frac{\dd \ket{\psi(t^\prime)}}{\ket{\psi(t^\prime)}} &= -i \int\limits_0^t \hat{H} \, \dd t^\prime \\
\Rightarrow\quad \ket{\psi(t)} &= e^{-i\hat{H}t} \ket{\psi(0)} \quad \text{for time-independent $\hat{H}$.} \label{eq:evolution}
\end{align}
In quantum mechanics, observables connect the theory with measurements. Observable quantities are expectation values of operators. Consider some general operator, $\hat{A}$, whose expectation value is given by
\begin{align}
\expval{A} &= \expval{\hat{A}}{\psi(t)} \\
&= \expval{e^{i\hat{H}t}\hat{A}e^{-i\hat{H}t}}{\psi(0)}.
\end{align}
Therefore, it is equivalent to define time-independent states and time-dependent operators in the Heisenberg picture:
\begin{align}
\ket{\psi_H} &\equiv \ket{\psi(0)} \\
\hat{A}_H &\equiv e^{i\hat{H}t}\hat{A}e^{-i\hat{H}t}.
\end{align}
Then the expectation value is simply
\begin{equation}
\expval{A} = \expval{\hat{A}_H}{\psi_H}
\end{equation}
and
\begin{align}
\dv{\hat{A}_H}{t} &= i\hat{H}e^{i\hat{H}t}\hat{A}e^{-i\hat{H}t} - ie^{i\hat{H}t}\hat{A}\hat{H}e^{-i\hat{H}t} \nonumber \\
&= i(\hat{H}\hat{A} - \hat{A}\hat{H}) \nonumber \\
&= i [ \hat{H}, \hat{A} ]
\end{align}

\subsection{The interaction picture}
In the interaction picture, both states and operators have some time dependance. In the interaction picture, we split the Hamiltonian into a free part $\hat{H}_0$ and interaction part $\hat{H}^\prime$,
\begin{equation}
\hat{H} = \hat{H}_0 + \hat{H}^\prime.
\end{equation}
The free part is generally time-independent and easily solved. We then say that operators $\hat{A}_I$ evolve according to the free part of the Hamiltonian,
\begin{equation}
\hat{A}_I(t) = e^{i\hat{H}_0t}\hat{A}e^{-i\hat{H}_0t}.
\end{equation}
Then the operator in the interaction picture evolves according to
\begin{equation}
\dv{\hat{A}_I}{t} = i [ \hat{H}_0, \hat{A}_I(t) ].
\end{equation}
Now consider the expectation value of the operator, $\expval{A} = \expval{e^{i\hat{H}_0t}\hat{A}e^{-i\hat{H}_0t}}{\psi_I(t)}$. We see that for this to be equivalent to the Schr{\"o}dinger picture expectation value, the states must evolve according to the interaction part of the Hamiltonian $\hat{H}^\prime$,
\begin{equation}
\ket{\psi_I(t)} = e^{-i\hat{H}^\prime t}\ket{\psi(0)}
\end{equation}
or equivalently
\begin{equation}
\ket{\psi_I(t)} = e^{i\hat{H}_0 t}\ket{\psi(t)}.
\end{equation}
The interaction picture is used in the perturbative expansion of the $S$-matrix in quantum field theory (QFT). This leads to the concept of using Feynman diagrams to calculate amplitudes.

Note that the three different pictures of quantum mechanics coincide at $t=0$.

\section{Quantum harmonic oscillator}
The quantum harmonic oscillator is an exactly solvable problem that neatly introduces ladder operators.

Consider a Hamiltonian of the form
\begin{equation}
H = \frac{p^2}{2m} + \frac{1}{2} m \omega^2 x^2
\end{equation}
where it should be clear from the context which quantities are operators, so we drop the hat notation, $\hat{O} \rightarrow O$.

Introduce the ladder operator
\begin{equation}
a = \frac{m \omega x + ip}{\sqrt{2m\omega}}
\end{equation}
and bearing in mind that $x$ and $p$ are hermitian,
\begin{equation}
a^\dagger = \frac{m \omega x - ip}{\sqrt{2m\omega}}.
\end{equation}
The product $a^\dagger a$ is
\begin{align}
a^\dagger a &= \frac{(m \omega x - ip)(m \omega x + ip)}{2 m \omega} \nonumber \\
&= \frac{1}{2m\omega} \left\{ (m \omega x)^2 + im\omega [x, p] + p^2 \right\} \nonumber \\
&= \frac{H}{\omega} - \frac{1}{2}.
\end{align}
Rearranging, the Hamiltonian can be written
\begin{equation}
H = \left( a^\dagger a + \frac{1}{2} \right) \omega.
\end{equation}
Similarly,
\begin{equation}
a a^\dagger = \frac{H}{\omega} + \frac{1}{2}.
\end{equation}
We also have that
\begin{equation}
[a, a^\dagger] = 1
\end{equation}
and
\begin{align}
[a, H] &= \omega a\\
[a^\dagger, H] &= -\omega a^\dagger.
\end{align}

\subsection{Energy spectrum}

Now consider the action of the operator $a^\dagger$ on a stationary state $\ket{n}$ with some energy $E_n$. What is the energy of the state $A^\dagger \ket{n}$?
\begin{align}
H a^\dagger \ket{n} &= \left( a^\dagger H - [a^\dagger, H] \right)\ket{n} \nonumber \\
&= \left( a^\dagger H + \omega a^\dagger \right)\ket{n} \nonumber \\
&= \left( E_n + \omega \right) a^\dagger \ket{n}.
\end{align}
So $a^\dagger\ket{n}$ is also an energy eigenstate with energy $\left( E_n + \omega \right)$. Since $a^\dagger$ has raised the energy of the state $\ket{n}$ by one quantum of energy, we call it the raising operator.

Similarly, the application of $a$ to $\ket{n}$ lowers the energy by $\omega$,
\begin{equation}
H a \ket{n} = (E_n - \omega) a \ket{n}.
\end{equation}
$a$ is called the lowering operator and together $a$ and $a^\dagger$ are ladder operators that span the energy spectrum of the QHO. In QFT, these operators correspond to the creation and annihilation of particles since particles are wave excitations of the vacuum.

\subsection{Zero-point energy}
There must be some state $\ket{0}$ for which $a\ket{0}$ vanishes. Equating to zero the square length of this state vector,
\begin{align}
0 = \abs{a\ket{0}}^2 &= \expval{a^\dagger a}{0} \\
&= \expval{\frac{H}{\omega} - \frac{1}{2}}{0} \nonumber \\
&= \frac{E_0}{\omega} - \frac{1}{2}.
\end{align}
Rearranging gives the energy of the vacuum state, $\ket{0}$,
\begin{equation}
E_0 = \frac{\omega}{2}.
\end{equation}

\subsection{Action of the ladder operators}

Now we can build up the ladder of energy states by continuous action of the raising operator,
\begin{align*}
a^\dagger\ket{0} &= \alpha_0 \ket{1} \\
a^\dagger\ket{1} &= \alpha_1 \ket{2} \\
\ldots \\
a^\dagger\ket{n} &= \alpha_n \ket{n+1}
\end{align*}
where the normalisation constant can be found via
\begin{align}
\alpha_n^2 = \abs{a^\dagger\ket{n}} &= \expval{a a^\dagger}{n} \nonumber \\
&= \expval{\frac{H}{\omega} + \frac{1}{2}}{n} \nonumber \\
&= n+1
\end{align}
so $\alpha_n = \sqrt{n+1}$. Therefore the general action of the raising operator is
\begin{equation}
\boxed{
a^\dagger \ket{n} = \sqrt{n+1}\ket{n+1}
}.
\end{equation}
Similarly,
\begin{equation}
\boxed{a \ket{n} = \sqrt{n} \ket{n-1}}.
\end{equation}

A general state $\ket{n}$ can be made from repeated application of $a^\dagger$ from the ground state:
\begin{equation}
\ket{n} = \frac{1}{\sqrt{n!}} (a^\dagger)^n \ket{0}.
\end{equation}

Notice that $a^\dagger a \ket{n} = n \ket{n}$, so $a^\dagger a$ is called the number operator.

\section{The anharmonic oscillator (perturbation theory)}
We now generalise the above QHO to include an anharmonic term,
\begin{align}
H &= \frac{p^2}{2m} + \frac{1}{2}m \omega^2 x^2 + \lambda x^3 \\
&= H_0 + \lambda H^\prime
\end{align}
where the second line expresses the Hamiltonian as in the interaction picture. In this case, the interaction hamiltonian is $H^\prime = x^3$. In what follows, we assume that $\lambda$ is small such that $H^\prime$ is a perturbation to the QHO solution and we stop expansions after first order in $\lambda$.

The eigenvectors of the full Hamiltonian may be expanded as
\begin{equation}
\ket{n} = \ket{n}^0 + \lambda\ket{n}^1 + \cdots
\end{equation}
where all the $\ket{n}^i$ are assumed to be orthogonal. The state has corresponding energy
\begin{equation}
E_n = E_n^0 + E_n^1 + \ldots
\end{equation}
such that it is the solution of the TISE,
\begin{align}
H\ket{n} &= E_n\ket{n} \\
\left( H_0 + \lambda H^\prime + \ldots \right)\left( \ket{n}^0 + \lambda\ket{n}^1 + \ldots \right) &= \left( E_n^0 + \lambda E_n^1 + \ldots \right)\left( \ket{n}^0 + \lambda\ket{n}^1 + \ldots \right)
\end{align}

Equating the coefficients of $\lambda^0$ gives the trivial harmonic part of the solution,
\begin{equation}
H_0 \ket{n}^0 = E_n^0 \ket{n}^0.
\end{equation}
Equating the coefficients of $\lambda^1$ gives
\begin{equation}
H_0\ket{n}^1 + H^\prime\ket{n}^0 = E_n^0\ket{n}^1 + E_n^1\ket{n}^0. \label{eq:lambda1}
\end{equation}
Rearranging and multiplying by $^0\!\bra{n}$,
\begin{equation}
^0\!\bra{n}H_0 - E_n^0\ket{n}^1 + ^0\!\bra{n}H^\prime - E_n^1\ket{n}^0 = 0.
\end{equation}
Now use that $^0\!\bra{n}H_0 = \,^0\!\bra{n} E_n^0$ to see that the first term is equal to zero. This gives the solution for the change in energy (up to first order in $\lambda$) of the $n$th excited state,
\begin{equation}\boxed{
E_n^1 = \,^0\!\expval{H^\prime}{n}^0
}.
\end{equation}

Now we wish to find the first-order correction to the state, $\ket{n}^1$. First, use the representation of $\ket{n}^1$ in the basis of harmonic solutions, $\ket{m}^0$,
\begin{equation}
\ket{n}^1 = \sum_m \ket{m}^0 \, ^0\!\braket{m}{n}^1 \label{eq:spectrum}.
\end{equation}
The eigenstates of the harmonic problem $\ket{m}^0$ may serve as a basis since they are all orthonormal, i.e.~$^0\!\braket{m}{n}^0 = \delta_{nm}$.

Now to get the coefficients $^0\!\braket{m}{n}^1$, multiply \eqref{eq:lambda1} by $^0\!\bra{m}$,
\begin{equation}
\underbrace{^0\!\bra{m}H_0\ket{n}^1}_{E_m^0 \,^0\!\braket{m}{n}^1} + ^0\!\bra{m}H^\prime\ket{n}^0 = ^0\!\bra{m}E_n^0\ket{n}^1 + \underbrace{^0\!\bra{m}E_n^1\ket{n}^0}_0.
\end{equation}
Rearranging,
\begin{equation}
^0\!\braket{m}{n}^0 = \frac{^0\!\bra{m}H^\prime\ket{n}^0}{E_n^0 - E_m^0} \label{eq:coefficients}.
\end{equation}
Notice that this approach only works if the energy levels are non-degenrate, i.e.~$E_n^0 \neq E_m^0$ for all $n \neq m$. For the QHO this is the case, but in more general cases one must diagonalise the Hamiltonian first.

Now, substituting \eqref{eq:coefficients} into \eqref{eq:spectrum} gives the value of the first-order correction to the energy eigenstates,
\begin{equation}
\boxed{
\ket{n}^1 = \sum_m \frac{^0\!\bra{m}H^\prime\ket{n}^0}{E_n^0 - E_m^0} \ket{m}^0
}.
\end{equation}

In the particular case where $H^\prime = x^3$ it is helpful to express the perturbation in terms of the ladder operators,
\begin{equation}
x^3 = \frac{(a + a^\dagger)^3}{(2m\omega)^\frac{2}{3}}.
\end{equation}
Then the determination of $E_n^1$ and $\ket{n}^1$ is achievable via the application of the ladder operators to the QHO eigenstates.

\section{Lagrangian mechanics}
In general, a Lagrangian is a function of coordinates $q$ and their derivatives, $L = L(q, \dot{q})$. The action is the integral of $L$,
\begin{equation}
S = \int L(q, \dot{q}) \, \dd t.
\end{equation}
Classical paths are those paths with stationary action, where $\delta S = 0$. This requires,
\begin{align}
\delta S &= \int \dd t \left\{ \pdv{L}{q} \delta q + \pdv{L}{\dot{q}} \delta\dot{q} \right\} \\
&= \int \dd t \left\{ \left[ \pdv{L}{q} - \dv{t}\left( \pdv{L}{\dot{q}} \right) \right] \delta q \right\} + \left[ \pdv{L}{\dot{q}} \delta q \right]_{t_1}^{t_2} = 0.
\end{align}
The last term is a total derivative and vanishes for any $\delta q$ that decays at spatial infinity and obeys $\delta q(t_1) = \delta q(t_2)$. For all such paths, we obtain the Euler-Lagrange equations of motion,
\begin{equation}
\boxed{
\pdv{L}{q} - \dv{t} \left( \pdv{L}{(\dot{q})} \right) = 0
}.
\end{equation}

\subsection{Relation to the Hamiltonian}

Each coordinate $q$ has a conjugate momentum,
\begin{equation}
p = \pdv{L}{\dot{q}}.
\end{equation}
Then the Hamiltonian is related to the Lagrangian by
\begin{equation}
\boxed{
H = p\dot{q} - L
}
\end{equation}

For a quantum treatment, we replace the coordinates with operators. The operators and their conjugate momenta have the commutation relations
\begin{equation}
[\hat{q}, \hat{p}] = i
\end{equation}
in natural units.

\subsection{Application to the QHO}
The QHO Lagrangian is
\begin{equation}
L = \frac{1}{2}m\dot{x}^2 - \frac{1}{2}m\omega^2 x^2.
\end{equation}
Therefore the momentum, $p = \pdv{L}{\dot{x}} = m\dot{x}$. So the Hamiltonian is
\begin{align}
H &= p\dot{x} - L \\
&= \frac{p^2}{m} - \left(\frac{1}{2}m\dot{x}^2 - \frac{1}{2}m\omega^2 x^2\right)\\
&= \frac{1}{2}m\dot{x}^2 + \frac{1}{2}m\omega^2 x^2
\end{align}
which is the same as we used for the QHO above.

The Lagrangian and Hamiltonian treatments are equivalent, but often a problem is more soluble in one over the other. QFT makes use of the Lagrangian density, and this is used to express the particles and interactions of the Standard Model.

\section{Dirac $\delta$ function}
The $\delta$ function may be thought of as a peaked function of width $\Delta x$ and height $1/\Delta x$ around a value $x=x_0$ in the limit $Delta x \rightarrow 0$. It has the properties
\begin{equation}
\int\limits_{-\infty}^{+\infty} \delta(x - x_0) \, \dd{x} = 1
\end{equation}
and
\begin{equation}
\int\limits_{-\infty}^{+\infty} f(x) \, \delta(x - x_0) \, \dd{x} = f(x_0).
\end{equation}
As a result, $\delta(x) - \delta(-x)$ is an even function and $\delta{ax} = \delta{x}/\abs{a}$. The proof is as follows:
\begin{align}
\int\limits_{-\infty}^{+\infty} \delta(ax) \, \dd{x} &= \int\limits_{-\infty}^{+\infty} \delta(y) \, \frac{\dd{y}}{a} \quad \text{for $y=ax$,} \\
&= \frac{1}{\abs{a}}.
\end{align}
Now consider a function $f(x)$ with roots $a_i$ such that $f(a_i)=0$. Then $\delta(f(x))$ function is non-zero only in the vicinity $x \sim a_i$. Expanding $f(x)$ about a particular root,
\begin{equation}
f(x) = \underbrace{f(a_i)}_0 + (x-a_i)\left[ \pdv{f}{x} \right]_{x=a_i} + \ldots
\end{equation}
so, using the preceding result, we have
\begin{align}
\delta(f(x)) &= \sum_i \delta\left((x-a_i)\left[\pdv{f}{x}\right]_{x=a_i}\right)\\
&= \sum_i \frac{\delta(x-a_i)}{\left|\pdv{f}{x}\right|_{x=a_i}}.
\end{align}

The $\delta$ function is normally constructed by taking the limit of some test function. Here, various test functions are discussed.

\subsubsection{Discrete $\delta$ function}
Consider some function $f(x)$ as a set of values $x_i$ in bins of width $\Delta x$. Then the integral becomes a discrete sum,
\begin{equation}
\int\limits_{-\infty}^{+\infty} f(x) \, \delta_t(x - x_0) \, \dd{x} \rightarrow \sum_{i={-\infty}}^{+\infty} f(x_i) \, \delta(x_i-x_0) \Delta x
\end{equation}
where $\delta_t(x - x_0) = 0$ for $i \neq 0$ and $\delta_t(0) = 1/\Delta{x}$. In the limit $\Delta x \rightarrow 0$, $\delta_t$ becomes the Dirac $\delta$ function. However, it is not particularly useful to have a discrete, non-differentiable definition.

\subsubsection{Breit-Wigner lineshape}
Consider a test function of a Breit-Wigner resonance peak,
\begin{equation}
\delta_t(x) = \frac{1}{\pi} \frac{\epsilon}{x^2 + \epsilon^2}.
\end{equation}
Its integral is
\begin{equation}
\int\limits_{-\infty}{+\infty} \frac{1}{\pi} \frac{\epsilon}{x^2 + \epsilon^2} \, \dd{x} = \int\limits_{-{\pi}/{2}}^{+{\pi}/{2}} \frac{1}{\pi} \frac{\sec^2{\theta}}{1+\tan^2{\theta}} \, \dd{\theta} = 1
\end{equation}
where the substitution $x = \epsilon \tan \theta$ has been used. At $x=0$, $\delta_t(0) = 1/\pi\epsilon$ and the full width at half-maximum is $2\epsilon$. The Breit-Wigner lineshape approaches the Dirac $\delta$ function in the limit $\epsilon\rightarrow 0$.

\subsubsection{Fourier analysis}
The Fourier integral theorem states that
\begin{equation}
f(x) = \frac{1}{2\pi} \int\limits_{-\infty}^{+\infty} e^{ipx} \left( \int\limits_{-\infty}^{+\infty} e^{-p\alpha} f(\alpha) \, \dd{\alpha} \right) \, \dd{p}.
\end{equation}
That is, the reverse transform of the Fourier transform of a function returns the original function. Rearranging\footnote{Cauchy showed that the order of integration matters, but this rearrangement is justified under the \emph{theory of distributions}.},
\begin{equation}
f(x) = \frac{1}{2\pi} \int\limits_{-\infty}^{+\infty} \left( \int\limits_{-\infty}^{+\infty} e^{pix} e^{-ip\alpha} \, \dd{p} \right) f(\alpha) \, \dd{\alpha} = \int\limits_{-\infty}^{+\infty} \delta(x-\alpha) f(\alpha) \, \dd{\alpha}
\end{equation}
where we see the $\delta$ function can now be defined
\begin{equation}
\delta(x) = \frac{1}{2\pi} \int\limits_{-\infty}^{+\infty} e^{ipx} \, \dd{p}.
\end{equation}

\section{Heaviside step function}
The Heaviside step function is defined in discrete form
\begin{equation}
\theta(x) =
\begin{cases}
1 & x > 0 \\
0 & x < 0
\end{cases}
\end{equation}

An integral representation for $\theta$ can be reached through recognising that its derivative is the Dirac $\delta$ function,
\begin{equation}
\dv{\theta}{x} = \delta(x) = \frac{1}{2\pi} \int\limits_{-\infty}^{+\infty} e^{ipx} \, \dd{p}.
\end{equation}
Integrating with respect to $x$,\begin{align}
\theta(x) &= \frac{1}{2\pi} \int\limits_{-\infty}^{+\infty} \int e^{ipx} \, \dd{p} \, \dd{x} \\
&= \frac{1}{2\pi i} \int\limits_{-\infty}^{+\infty} \frac{e^{ipx}}{p} \dd{p}
\end{align}
where the constant of integration is zero. Adding a small offset to the denominator, we get the final integral form of the $\theta$ step function
\begin{align}
\theta(x) = \lim_{\epsilon\rightarrow 0^+} \frac{1}{2\pi i} \oint_C \frac{e^{ipx}}{p+i\epsilon} \, \dd{p}.
\end{align}
where $C$ is a contour along the real axis closed in the upper-half plane.

The residue theorem states that
\begin{equation}
\oint_C \frac{f(z)}{z-a} \dd{z} = 2\pi i f(a)
\end{equation}
for a contour $C$ containing $a$. Applying this to the integral representation for $\theta$ above,
\begin{equation}
\theta(x) = \lim_{\epsilon\rightarrow 0^+} \frac{2\pi i}{2\pi i} e^{ip(-i\epsilon)} = \lim_{\epsilon\rightarrow 0^+} e^{p\epsilon} = 1
\end{equation}
for $x > 0$. For $x < 0$, the contour must be closed in the lower-half plane and the integral evaluates to $0$.


\documentclass{memoir}
\usepackage{linalg}

% \begin{figure}[ht]
%     \centering
%     \incfig{riemmans-theorem}
%     \caption{Riemmans theorem}
%     \label{fig:riemmans-theorem}
% \end{figure}

\begin{document}
\chapter{Inner Products and Norms}
\label{cha:inner_products_and_norms}
\section{Inner Products}
\label{sec:inner_products}

\begin{defn}[Dot Product]
	For $x,y \in \R^{n}$, the \textbf{dot product} of $x$ and $y$, denoted $x\cdot y$, is defined by
	\begin{align*}
		x\cdot y = x_1y_1+\ldots+x_ny_n
	\end{align*}
	where $x = (x_1,\ldots,x_n)$ and $y = (y_1,\ldots,y_n)$.
\end{defn}
The dot product takes in two vectors and returns a scalar. Moreover, $x\cdot x = \|x\|^2$.
\begin{cor}[Properties of dot products]
	\begin{itemize}
		\item $x\cdot x \geq 0$ for all $x\in \R^{n}$
		\item $x\cdot x = 0$ if and only if $x = 0$
		\item for $y \in \R^{n}$ fixed, the map from $\R^{n}$ to $\R$ that sends $x \in \R^{n}$ to $x\cdot y$ is linear
		\item $x\cdot y = y\cdot x$ for all $x,y \in \R^{n}$
	\end{itemize}
\end{cor}
This is almost quite right for complex numbers, but for that we must generalize a little further.
\begin{defn}[Inner Product]
	An \textbf{inner product} on $V$ is a function that takes each ordered pair $(u,v)$ of elements in $V$ to a number $\langle u, v \rangle \in F$ and has the following properties:
	\begin{itemize}
		\item $ \langle v, v \rangle \geq 0 $ for all $v \in V$ \textbf{(positivity)}
		\item $ \langle v, v \rangle = 0$ if and only if $v = 0$ \textbf{(definiteness)}
		\item  $ \langle u+v, w \rangle = \langle u, w \rangle + \langle v, w \rangle $ for all $u,v,w \in V$ \textbf{(additivity in first slot)}
		\item $ \langle \lambda u, v \rangle = \lambda \langle u, v \rangle $ for all $\lambda \in F$ and all $u,v \in V$ \textbf{(homogeneity in first slot)}
		\item $ \langle u, v \rangle = \overline{ \langle v, u \rangle }$ for all $u,v \in V$ \textbf{(conjugate symmetry)}.
	\end{itemize}
\end{defn}
\begin{defn}[Inner Product Space]
	An \textbf{inner product space} is a vector space $V$ along with an inner product on $V$.
\end{defn}
Notation: For the rest of the chapter, $V$ denotes a inner product space over $F$.

\begin{cor}[Basic properties of an inner product]
	\begin{itemize}
		\item For each fixed $u \in V$, the function that takes $v$ to $ \langle v, u \rangle $ is a linear map from $V$ to $F$
		\item $ \langle 0, u \rangle = 0$ for every $u \in V$
		\item $ \langle u, 0 \rangle = 0$ for every $u \in V$
		\item $ \langle u, v+w \rangle = \langle u, v \rangle + \langle u, w \rangle $ for all $u,v,w \in V$
		\item $ \langle u, \lambda v \rangle  = \overline{\lambda} \langle u, v \rangle $ for all $\lambda \in F$ and $u,v \in V$
	\end{itemize}
\end{cor}
\section{Norms}
\label{sec:norms}
\begin{defn}[Norm]
	For $v \in V$, the \textbf{norm} of $v$, denoted $\|v\|$, is defined by
	\begin{align*}
		 \|v\| = \sqrt{ \langle v, v \rangle } .
	\end{align*}
\end{defn}
\begin{cor}[Basic properties of the norm]
	Suppose $v \in V$.
	\begin{itemize}
		\item $\|v\|=0$ if and only if $v = 0$.
		\item $\|\lambda v\|= \left| \lambda \right| \|v\|$ for all $\lambda \in F$.
	\end{itemize}
\end{cor}
\begin{defn}[Orthogonal]
	Two vectors $u,v \in V$ are called \textbf{orthogonal} if $ \langle u, v \rangle = 0$.
\end{defn}
\begin{cor}
	\begin{itemize}
		\item 0 is orthogonal to every vector in $V$
		\item 0 is the only vector in $V$ that is orthogonal to itself
	\end{itemize}
\end{cor}
\begin{thm}[Pythagorean Theorem]
	Suppose $u,v$ are orthogonal vectors in $V$. Then
	\begin{align*}
		\|u+v\|^2 = \|u\|^2+\|v\|^2
	\end{align*}
\end{thm}
\begin{cor}[Orthogonal Decomposition]
	Suppose $u,v \in V$, with $v\neq 0$. Set $c = \frac{ \langle u, v \rangle }{\|v\|^2}$ and $w = u - \frac{ \langle u, v \rangle }{\|v\|^2}v$. Then
	\begin{align*}
 \langle w, v \rangle =0 \text{ and } u = cv + w.
	\end{align*}
\end{cor}
\begin{thm}[Cauchy-Schwarz Inequality]
	Suppose $u,v \in V$. Then
	\begin{align*}
		 \left| \langle u, v \rangle  \right| \leq \|u\|\|v\|.
	\end{align*}
	This inequality is an equality if and only if one of $u,v$ is a scalar multiple of the other.
\end{thm}
\begin{thm}[Triangle Inequality]
	Suppose $u,v \in V$. Then
	\begin{align*}
 \|u+v\| \leq \|u\|+ \|v\|.
	\end{align*}
	This inequality is an equality if and only if one of $u,v$ is a nonnegative multiple of the other.
\end{thm}
\begin{thm}[Parallelogram Equality]
	Suppose $u,v \in V$. Then
	\begin{align*}
 \|u+v\|^2 + \|u-v\|^2 = 2\left( \|u\|^2+ \|v\|^2 \right) .
	\end{align*}
\end{thm}
\section{Orthonormal Bases}
\label{sec:orthonormal_bases}
\begin{defn}[Orthonormal]
	A list of vectors is called \textbf{orthonormal} if each vector in the list has norm 1 and is orthogonal to all other vectors in the list.
\end{defn}
\begin{cor}
	Every orthonormal list of vectors in $V$ with length \textrm{dim}$V$ is an orthonormal basis of $V$.
\end{cor}
\begin{lemma}
	Suppose $e_1,\ldots,e_n$ is an orthonormal basis of $V$ and $v \in V$. Then
	\begin{align*}
		v = \langle v, e_1 \rangle e_1 + \ldots + \langle v, e_n \rangle e_n
	\end{align*}
	and
	\begin{align*}
		\|v\|^2 = \left| \langle v, e_1 \rangle  \right|^2 + \ldots + \left| \langle v, e_n \rangle  \right|^2.
	\end{align*}
\end{lemma}
\begin{thm}[Gram-Schmidt Procedure]
	Suppose $v_1,\ldots,v_m$ is a linearly independent list of vectors in $V$. Let $e_1 = \frac{v_1}{\|v_1\|}$. For $j=2,\ldots,m$, define $e_j$ inductively by
	\begin{align*}
		e_j = \frac{v_j - \langle v_j, e_1 \rangle e_1 - \ldots - \langle v_j, e_{j-1} \rangle e_{j-1}}{\|v_j - \langle v_j, e_1 \rangle e_1 - \ldots - \langle v_j, e_{j-1} \rangle e_{j-1}\|}.
	\end{align*}
Then $e_1,\ldots,e_m$ is an orthonormal list of vectors in $V$ such that
\begin{align*}
	span(v_1,\ldots,v_j) = span(e_1,\ldots,e_j)
\end{align*}
\end{thm}
\begin{cor}
	Every finite-dimensional inner product space has an orthonormal basis.
\end{cor}
\begin{cor}
	Suppose $V$ is finite-dimensional. Then every orthonormal list of vectors in $V$ can be extended to an orthonormal basis of $V$.
\end{cor}
\begin{lemma}
	Suppose $T \in \mathcal{L}(V)$. If $T$ has an upper-triangular matrix with respect to some basis of $V$, then $T$ has an upper-triangular matrix with respect to some orthonormal basis of $V$.
\end{lemma}
\begin{thm}[Schur's Theorem]
	Suppose $V$ is a finite-dimensional complex vector space and $T \in \mathcal{L}(V)$. Then $T$ has an upper-triangular matrix with respect to some orthonormal basis of $V$.
\end{thm}
\begin{thm}[Riesz Representation Theorem]
	Suppose $V$ is finite-dimensional and $\varphi $ is a linear functional on $V$. Then there is a unique vector $u\in V$ such that
	 \begin{align*}
		 \phi(v) = \langle v, u \rangle
	\end{align*}
	for every $v \in V$.
\end{thm}
To construct this vector, first we write
\begin{align*}
	\varphi(v) = \varphi\left( \langle v, e_1 \rangle e_1 + \ldots + \langle v, e_n \rangle e_n \right) \\
	= \langle v, e_1 \rangle \varphi(e_1) + \ldots + \langle v, e_n \rangle \varphi(e_n) \\
	= \langle v, \overline{\varphi(e_1)}e_1 + \ldots + \overline{\varphi(e_n)}e_n \rangle
\end{align*}
for every \(v \in V\). Thus, we can simply set
\begin{align*}
	u = \overline{\varphi(e_1)}e_1 + \ldots + \overline{\varphi(e_n)}e_n.
\end{align*}
\begin{lemma}
	Suppose there exists $ u_1,u_2 \in V$ such that
	\begin{align*}
		 \langle v, u_1 \rangle = \langle v, u_2 \rangle \text{ for all } v \in V.
	\end{align*}
	Then $u_1 = u_2$.
\end{lemma}
\end{document}


\chapter{Aerodynamics}

Aerodynamic forces are calculated in Aerodynamic Axis System, while moments are calculated in Stability Axis System. Rotation matrix from Stability Axis System to Body Axis System can be calculated using formula (\ref{eq-aero-matrix-alpha}). Rotation matrix from Aerodynamic Axis System to Body Axis System can be calculated using following formulas:
\begin{equation}
  \label{eq-aero-matrix-alpha}
  \boldsymbol T \left( \alpha \right)
  =
  \left[
    \begin{matrix}
      -1 & 0 &  0 \\
       0 & 1 &  0 \\
       0 & 0 & -1 \\
    \end{matrix}
  \right]
  \left[
    \begin{matrix}
      \cos \alpha & 0 & -\sin \alpha \\
                0 & 1 &            0 \\
      \sin \alpha & 0 &  \cos \alpha \\
    \end{matrix}
  \right]
  =
  \left[
    \begin{matrix}
      -\cos \alpha & 0 &  \sin \alpha \\
                 0 & 1 &            0 \\
      -\sin \alpha & 0 & -\cos \alpha \\
    \end{matrix}
  \right]
\end{equation}

\begin{equation}
  \label{eq-aero-matrix-beta}
  \boldsymbol T \left( \beta \right)
  =
  \left[
    \begin{matrix}
       \cos\psi & \sin\psi & 0 \\
      -\sin\psi & \cos\psi & 0 \\
              0 &        0 & 1 \\
    \end{matrix}
  \right]
\end{equation}

\begin{equation}
  \boldsymbol T \left( \alpha, \beta \right)
  =
  \boldsymbol T \left( \alpha \right) \boldsymbol T \left( \beta \right)
  =
  \left[
    \begin{matrix}
      -\cos \alpha \cos \beta & -\cos \alpha \sin \beta &  \sin \alpha \\
                  -\sin \beta &              \cos \beta &            0 \\
      -\sin \alpha \cos \beta & -\sin \alpha \sin \beta & -\cos \alpha \\
    \end{matrix}
  \right]
\end{equation}

Considering a no-wind conditions angle of attack and angle of sideslip (positive when the aircraft velocity component along the transverse axis is positive \cite{ISO-1151-1-1988}) are given as follows:
\begin{align}
  \alpha &= \arctan \left( \frac{w}{ \sqrt{ u^2 + v^2 } } \right) \\
  \beta  &= \arcsin \left( \frac{v}{V} \right)
\end{align}

\section{Tail-off Aircraft}

Tail-off aircraft aerodynamics model is intended to be used in application, e.g. fixed-wing aircrafts, where asymmetric aerodynamic effects, such as autorotation spin or roll damping, are significant.

Forces and moments are calculated for each half-wing to consider asymmetric effects. Half wing aerodynamic center velocity vector used to calculate angle of attack, angle of sideslip as well as forces and moments is given as follows:
\begin{equation}
  \label{eq-aero-v-ac}
  {\vec V}_{AC} = {\vec V}_O + {\vec \Omega} \times {\vec r}_{AC}
\end{equation}

Forces and moments generated by the half-wing are given as follows: \cite{StevensLewis1992}
\begin{align}
  {\vec F}_a &= \left[ F_{X,a}, F_{Y,a}, F_{Z,a} \right]^T \\
  {\vec M}_s &= \left[ M_{X,s}, M_{Y,s}, M_{Z,s} \right]^T
\end{align}

Where:
\begin{align}
  \label{eq-aero-fxa}
  F_{X,a} &= \frac{1}{2} \rho V^2 S C_D \\
  \label{eq-aero-fya}
  F_{Y,a} &= \frac{1}{2} \rho V^2 S C_Y \\
  \label{eq-aero-fza}
  F_{Z,a} &= \frac{1}{2} \rho V^2 S C_L \\
  M_{X,s} &= \frac{1}{2} \rho V^2 S \hat c C_l \\
  M_{Y,s} &= \frac{1}{2} \rho V^2 S \hat c C_m \\
  M_{Z,s} &= \frac{1}{2} \rho V^2 S \hat c C_n
\end{align}

Forces and moments generated by the half-wing expressed in Body Axis System are given by the following formulas:
\begin{align}
  \label{eq-aero-fb}
  {\vec F}_b &= {\boldsymbol T} \left( \alpha, \beta \right) {\vec F}_a \\
  {\vec M}_b &=
  {\boldsymbol T} \left( \alpha \right) {\vec M}_s
  +
  {\vec r}_{AC,b} \times {\vec F}_b
\end{align}

\section{Stabilizers}

Velocity vector used to calculate stabilizer angle of attack, angle of sideslip as well as forces and moments is calculated using expression (\ref{eq-aero-v-ac}).

Horizontal stabilizer angle of attack is modified due to incidence angle and downwash angle, what can be expressed as follows. \cite{Etkin1972}
\begin{equation}
  \Delta \alpha_h
  =
  i_h + \frac{ \partial \epsilon }{ \partial \alpha } \alpha
\end{equation}

Forces generated by stabilizers are calculated using formulas (\ref{eq-aero-fxa}), (\ref{eq-aero-fya}) and (\ref{eq-aero-fza}).

Formula (\ref{eq-aero-fb}) can be used to calculate stabilizer generated forces expressed in Body Axis System.

It is assumed that horizontal stabilizer generates only drag and lift, while vertical stabilizer generates only drag and side force. Moments generated by stabilizers comes only from force acting on arm, other moments are neglected.
\begin{equation}
  {\vec M}_b = {\vec r}_{AC,b} \times {\vec F}_b
\end{equation}


% -*- LaTeX -*-
% -*- coding: utf-8 -*-
%
% michael a.g. aïvázis
% orthologue
% (c) 1998-2022 all rights reserved
%

\section{Two simple implementations}
\label{sec:simple}

Turning the math of the previous section into actual code is not very hard, especially if
you don't try improve the effective convergence rate by sampling the integration region in
tricky ways. To simplify things even further, we will forgo integration with non-trivial
integrands for now. Instead, we will compute areas and volumes by letting $f$ be the unit
function over our chosen region.
%
\begin{figure}
\centering
\includegraphics[scale=.75]{figures/quadrant.pdf}
\caption{Estimating $\pi$ by computing the area of a quadrant of the unit circle
  \label{fig:pie}}
\end{figure}
%

As a warm up exercise, let's use Monte Carlo integration to compute $\pi$. We can get an
estimate for $\pi/4$ by computing the area of the upper right quadrant of the unit circle,
as shown in \figref{pie}. This will require generating points in the unit square and
counting the fraction that fall in the shaded region.

The implementation strategy is very simple. Let $N$ be the total number of points we would like
to generate. We will set up two counters, $t$ for the total number of points and $i$ for the
points that fall inside the unit circle, and iterate $N$ times building random points in the
unit square, checking whether each point falls inside the unit circle and updating our counters
appropriately, as shown in \algref{pi}.  Integration has been reduced to placing points in two
bins. Note that $\Theta(\Omega)$ in \eqref{mc} corresponds to the conditional in
\alglineref{pi:check}, and the dimensionality of the integrand is reflected in the number of
calls to the random number generator it takes to build a point. Further, our sampling box $B$
has unit area, so there is no need for any extra normalization of the result.
%
\begin{algorithm}
\caption{$\pi_{N}$: the Monte Carlo estimate of $\pi$ \label{alg:pi}}
%
\DontPrintSemicolon
\SetAlgoNoEnd
%
$i \leftarrow 0$ \;
$t \leftarrow 0$ \;
\While{$t < N$}{
   $x \leftarrow \mbox{random()}$ \;
   $y \leftarrow \mbox{random()}$ \;
   \If{$\sqrt{x^{2}+y^{2}} \leq 1$}{ \nllabel{line:pi:check}
      $i \leftarrow i + 1$
   }
   $t \leftarrow t + 1$
}
$\pi_{N} = 4 i/N \;$ %\frac{i}{t}$ \;
\end{algorithm}
%

It is straightforward to generalize this procedure to compute integrals of non-trivial
integrands over higher dimensional regions. Perhaps you can already see that computing the
integral of a non-trivial $f$ should only affect our interpretation of the counter $i$. We will
address this and other ways to generalize this algorithm in \secref{classes}.

\subsection{The two-minute python script}
\label{sec:simple:python}

The python implementation is a straightforward translation of the pseudocode of
\algref{pi}. The python standard library\supercite{python-doc} includes the package {\tt random}
with an implementation of the Mersenne Twister\supercite{mersenne-twister} random number
generator. Two minutes of typing produce:
%
\python{
  % firstnumber=9,
  linerange={9-25},
  label={lst:simple:python},
  caption={\srcfile{simple/gauss.py}: Estimating $\pi$ in python},
}{listings/simple/gauss.py}
%
Note that by deciding whether the point is inside the circle using the square of the distance
saves us a large number of calls to \function{sqrt}. This type of consideration is ubiquitous
when trying to get good performance out of Monte Carlo methods.  Also, note that we did not
have to convert our integer counters to floats before the computation of the area fraction,
since division of integers results in floating point numbers whenever necessary, a behavior
that is standard since version 3.0 of the python interpreter.

There are two obvious questions to answer: how accurate and how fast is this calculation?  To
answer these questions, we can perform some experiments where we vary the total number of
points $N$, and record the result and the time it took, as shown in \tabref{simple-python}.
%
\begin{table}
\centering
\[
\begin{array}{rd{8}cd{4}}
% headersep
  \multicolumn{1}{c}{N} &
  \multicolumn{1}{c}{\pi_{N}} &
  \multicolumn{1}{c}{\Delta} &
  \multicolumn{1}{c}{t \mbox{(sec)}} \\
  \hline \\ [-1.5ex]
% body
  10^{0} &  0          & 1                   &    .014  \\
  10^{1} &  3.6        & 1.46 \times 10^{-1} &    .014  \\
  10^{2} &  3.36       & 6.95 \times 10^{-2} &    .014  \\
  10^{3} &  3.076      & 2.09 \times 10^{-2} &    .015  \\
  10^{4} &  3.156      & 4.59 \times 10^{-3} &    .027  \\
  10^{5} &  3.14496    & 1.07 \times 10^{-3} &    .144  \\
  10^{6} &  3.144028   & 7.75 \times 10^{-4} &   1.265  \\
  10^{7} &  3.142112   & 1.65 \times 10^{-4} &  12.624  \\
  10^{8} &  3.14170136 & 3.46 \times 10^{-5} & 130.430
\end{array}
\]
\caption{Estimating $\pi$: accuracy and cost of the python implementation
  \label{tab:simple-python}}
\end{table}
%
Varying the sample size involves editing the script, making the corresponding change to the
value of $N$ and making another run. We could have exposed this variable to the end user by
attaching it to a command line argument, but there is no reason to go through all this at this
point. We will see a better way to get this kind of flexibility in \secref{pyreapp}.

As estimators of $\pi$ go, this is not a good one: it converges rather slowly and it is rather
expensive. Sample sets larger than $10^{8}$ are not practical in pure python. How much better
can we do by switching over to a lower level language, such as \cpp?

\subsection{The ten-minute \cpp\ program}
\label{sec:simple:c}

The implementation in \cpp\ is also fairly straightforward. One complication is that the random
number generator in the standard \cc\ library has rather poor properties and it is not well
suited for stochastic calculations; fortunately, \package{GSL}, the \package{GNU} Scientific
Library\supercite{gsl}, has a large number of good generators whose properties and cost are
rather well documented. Binary distributions of \package{GSL} are available for a variety of
platforms, and you can always download and compile it from its freely available source code.

Note that we are only using \cpp\ as a better \cc, taking advantage of the strict type
checking, the ability to declare variables where we need them, and the prettier comment
syntax. Later we will see a couple of other reasons to compile \cc\ programs with \cpp\
compilers. For now, here is what a simple translation of the pseudocode of \algref{pi} looks
like:
%
\Cpp{
  % firstnumber=9,
  linerange={9-35},
  label={lst:simple:cpp},
  caption={\srcfile{simple/gauss.cc}: Estimating $\pi$ in \cpp},
}{listings/simple/gauss.cc}
%
whose accuracy and cost is shown in \tabref{simple-c}. It is clear that the
\cpp\ implementation is almost an order of magnitude faster. However, the cost of making the
necessary changes to the value of $N$ is a little higher for the \cpp\ program, since the cycle
involves the additional steps of compiling and linking. Again, a flexible program would expose
this variable to the end user; we will address this issue in \secref{pyreapp}.
%
\begin{table}
\centering
\[
\begin{array}{rd{8}cd{4}}
% headers
  \multicolumn{1}{c}{N} &
  \multicolumn{1}{c}{\pi_{N}} &
  \multicolumn{1}{c}{\Delta} &
  \multicolumn{1}{c}{t \mbox{(sec)}} \\
  \hline \\ [-1.5ex]
% body
  10^{0} &  0          & 1                   &    .002  \\
  10^{1} &  3.2        & 1.86 \times 10^{-2} &    .002  \\
  10^{2} &  3.16       & 5.86 \times 10^{-3} &    .002  \\
  10^{3} &  3.2        & 1.86 \times 10^{-2} &    .002  \\
  10^{4} &  3.1456     & 1.28 \times 10^{-3} &    .004  \\
  10^{5} &  3.13528    & 2.01 \times 10^{-3} &    .026  \\
  10^{6} &  3.140756   & 2.66 \times 10^{-4} &    .230  \\
  10^{7} &  3.141948   & 1.13 \times 10^{-4} &   2.277  \\
  10^{8} &  3.1417769  & 5.86 \times 10^{-5} &  22.749  \\
  10^{9} &  3.1415631  & 9.41 \times 10^{-6} & 227.735
\end{array}
\]
\caption{Estimating $\pi$: accuracy and cost of the \cpp\ implementation
  \label{tab:simple-c}}
\end{table}
%

We have chosen the \package{RANLUX} random number generator, one the most costly but well behaved
generators in the library. Even so, the \cpp\ implementation is six to seven times
faster. Since the python generator \function{random} is implemented in \cc, the difference in
running times is mostly due to the cost of setting up the function call in the two
languages. For many computations, the extra flexibility that python has to offer is worth the
performance hit; for others it is unacceptable. However, as we will see later, a little care,
some common sense, and some careful profiling will make it possible to have the best of both
worlds.

% end of file


%s How the representative set is computed; pruning strategies and verification
%step goes here.

\section{Computing Representative Sets}
\label{sec:representative}
Representative vertex $v$ of a pattern
vertex $u$ implies that there exists an isomorphism $\phi$ for which $\phi(u) =
v$.  One way to interpret it is that the neighborhood of $u$ matches with that
of $v$.  By comparing the neighborhoods we can find vertices that are not valid
representatives of $u$ without trying to find an isomorphism exhaustively.
Therefore, to compute the representative sets we will start with a candidate
representative set denoted by \CR  and iteratively prune some of the vertices if
the neighborhoods cannot be matched.  The candidate set is a super set of the
representative set, $\CR \supseteq \RS$.  An example of candidate set, $ \CR =
\{v| v \in \vg, \matij{C}{L(u)}{L(v)} \leq \alpha \}$ i.e., the isomorphisms of
the single vertex pattern with label $L(u)$.  In this section, we will describe
different notions of neighborhood and show how they help us in computing the
representative sets of vertices in a pattern.

The problem of checking whether a vertex $v \in R(u)$ involves solving
isomorphism which is a NP complete problem. The pruning methods typically do not
prune all the invalid vertices.  So, we use an exhaustive enumeration method to
prune these invalid vertices and reduce \CR to \RS.

 \subsection{\khop Label}
 %Neighbors of a vertex in a graph denotes the set of vertices that are
 %reachable via a single edge.
 \khop label is defined as the set of vertices that are reachable via a simple
 path of length $k$.  In other words, k-hop label contains all vertices that are
 reachable in k-hops starting from $u$ and by visiting each vertex at most once.
 Note that, we use the word label even though we refer to a set of vertices.
 Formally, the \khop label of a vertex $u$ in graph $G$, $\khopl{k}{u,G} = \{v |
 v \in G, \kpath{u}{v}{k}\}$.  We simply write it as $\khopl{k}{u}$ when the
 graph is evident from the context.  For example, for pattern $P$ in
 Fig.~\ref{subfig:pattern}, the $0$-hop label of vertex $5$ is $h_0(5) = \{5\}$,
 its $1$-hop label is the multiset $h_1(5) = 2, 4, 6$ (we omit the set notation
 for convenience) and its $2$-hop label $h_2(5) = 1, 3$. The minimum cost of
 matching \khop labels $\khopl{k}{u}$ and $\khopl{k}{v}$ is

 \begin{equation} \label{eq:khop} \khopcost{k}{u}{v} =
     \text{min}\displaystyle\sum_{u' \in \khopl{k}{u}}
     \matij{C}{L(u')}{L(f(u'))} \end{equation}

 where the minimization is over all injective functions $f\!\!:\khopl{k}{u}
 \rightarrow \khopl{k}{v}$ and $\matij{C}{L(u')}{L(f(u'))}$ is the cost of
 matching the vertex labels.  In other words, it is the minimum total cost of
 matching the vertices present in the k-hop labels.  The following theorem
 places a upper bound on the minimum cost of matching the k-hop labels of
 pattern vertex and any of its representative vertices.

\begin{thm} Given any pattern vertex $u$, a representative vertex $v \in R(u)$
    and cost threshold $\alpha$, the minimum cost of matching the \khop labels,
    $\khopcost{k}{u}{v} \leq \alpha$ for all $k \geq 0$.

\begin{myproof} Consider any isomorphism $\phi$ such that $\phi(u) = v$. It is
    enough if we can show an injective function $f\!\!:\khopl{k}{u} \rightarrow
    \khopl{k}{v}$ with a cost ( as defined in equation \ref{eq:khop}) $\leq
    \alpha$. We will argue that the function $\phi$ on the restricted domain
    $\khopl{k}{u}$ is one such function $f$.  First, we know that
    $\sum\matij{C}{L(u)}{\phi(L(u)} \leq \alpha$, $u \in \vp$, since $\phi$ is
    an isomorphism. Second, let $\kpath{u}{u'}{k}$ then $\phi(u') \in
    \khopl{k}{v}$ because for every edge $(u_1, u_2)$ on a path between $u$ and
    $u'$ in $\pat$, $(\phi(u_1),\phi(u_2)) \in \eg$.  Therefore the minimum cost
    of matching the \khop labels is upper bounded by $\alpha$.  \end{myproof}
    \label{thm:khop} \end{thm}

Based on the above theorem, a vertex $v$ is not a representative vertex of $u$
if $\khopcost{k}{u}{v} > \alpha$ for any $k \geq 0$. However, in practice, it
enough to check the condition only for $k \leq |V_P|-1$ because $\khopl{k}{u}$
is the null set $\forall k \geq |V_P|$ and the condition is trivially satisfied.

Figure~\ref{fig:ncexample} shows an example for the \khop label based pruning of
the candidate representative set where the threshold $\alpha = 0.5$. Consider
vertex $2 \in \vp$ and vertex $20 \in \vg$, we have, $\khopcost{0}{2}{20} = 0$,
since the cost of matching vertex labels $\matij{C}{L(2)}{L(20)} = 0$ , as per
the label matching matrix $C$ in Fig.~\ref{subfig:match}. The \khop labels for
$k=1,2,3$ and the minimum of cost matching them are as shown in the table
\ref{tab:khop220}, and it can be verified that the minimum cost is within the
threshold $\alpha$.

Thus far, we cannot prune node $20$ from $R'(2)$.  However, $\khopl{4}{2} = 4, 5
$ and $\khopl{4}{20} = 30, 60$ and the minimum cost of matching them is $0.6 >
\alpha$.  Thus, we conclude that $20 \notin R'(2)$. This example illustrates
that \khop labels can help prune the candidate representative sets.

% Example for showing the incremental updates of the labels
\begin{figure}[!ht]
\captionsetup[subfloat]{captionskip=15pt}
  \centering
  \subfloat[Pattern $P$]{
    \label{subfig:pattern}
	\scalebox{0.9}{
    % # 3 vertices and 2 edge pattern
    \begin{pspicture}(0,0)(4,3)
    \cnodeput[linecolor=black](0,2) {n1} {A}
    \cnodeput[linecolor=black](0,1) {n2} {C}
    \cnodeput[linecolor=black](1,0) {n3} {B}
    \cnodeput[linecolor=black](2,2) {n4} {C}
    \cnodeput[linecolor=black](2,1) {n5} {A}
    \cnodeput[linecolor=black](2,0) {n6} {D}
    %% Draw the edges of the pattern
  \ncline{-}{n1}{n2}
  \ncline{-}{n2}{n3}
  \ncline{-}{n3}{n4}
  \ncline{-}{n2}{n5}
  \ncline{-}{n4}{n5}
  \ncline{-}{n5}{n6}
  \ncline{-}{n3}{n6}
    \uput{.3cm}[90](n1){ {1} }
    \uput{.3cm}[180](n2){ {2} }
    \uput{.3cm}[270](n3){ {3} }
    \uput{.3cm}[90](n4){ {4} }
    \uput{.3cm}[0](n5){ {5} }
    \uput{.3cm}[270](n6){ {6} }
    \end{pspicture}
	} }
  \subfloat[Database Graph $G$]{
    \label{subfig:database}
	\scalebox{0.9}{
    \begin{pspicture}(0,0)(2.5,2.5)
    \cnodeput[linecolor=black](0,2) {N1} {A}
    \cnodeput[linecolor=black](0,1) {N2} {C}
    \cnodeput[linecolor=black](0,0) {N3} {D}
    \cnodeput[linecolor=black](1,2) {N4} {B}
    \cnodeput[linecolor=black](2.25,1) {N5} {B}
    \cnodeput[linecolor=black](2,0) {N6} {A}
    % vertex ids
    \uput{.3cm}[90](N1){ {10} }
    \uput{.3cm}[180](N2){ {20} }
    \uput{.3cm}[270](N3){ {30} }
    \uput{.3cm}[90](N4){ {40} }
    \uput{.3cm}[0](N5){ {50} }
    \uput{.3cm}[270](N6){ {60} }
    % edges in the database
  \ncline{-}{N1}{N2}
  \ncline{-}{N2}{N3}
  \ncline{-}{N4}{N5}
  \ncline{-}{N5}{N6}
  \ncline{-}{N3}{N4}
  \ncline{-}{N2}{N5}
  \ncline{-}{N2}{N6}
    \end{pspicture}
	} }
  \newline
\captionsetup[subfloat]{captionskip=5pt}
\subfloat[Cost Matrix]{
  \label{subfig:match}
  % Table for the search space pruning
  \begin{tabular}{|c|c|c|c|c|}
    \hline
    $C$ & A &  B & C & D \\
    \hline
    A & 0 & 0.7 & 0.6 & 0.1\\
    \hline
    B & 0.7 & 0 & 0.3 & 1\\
    \hline
    C & 0.6 & 0.3 & 0 & 0.8\\
    \hline
    D & 0.1 & 1 & 0.8 & 0\\
    \hline
  \end{tabular}
  }
  \caption{Pattern \protect\subref{subfig:pattern},
  database graph \protect\subref{subfig:database}, and cost
  matrix \protect\subref{subfig:match}.
  }
  \label{fig:ncexample}
\end{figure}

\begin{table}[h]
    \centering
    \begin{tabular}{|c|c|c|c|}
        \hline
        k & $\khopl{k}{2}$ & $\khopl{k}{20}$ & $\khopcost{k}{2}{20}$\\
        \hline
        1 & 1, 3, 5 & 10, 30, 50, 60 & 0 \\
        2 & 4, 6 & 40, 50, 60 & 0.4 \\
        3 & 3, 5 & 40, 30, 50 & 0.1\\
        \hline
    \end{tabular}
    \caption{\khop label of vertices $2$ and $20$}
    \label{tab:khop220}
\end{table}

\begin{table}[h]
    \centering
    \begin{tabular}{|c|c|c|c|}
        \hline
        k & $h_k(3)$ & $h_k(50)$ & $\khopcost{k}{3}{50}$ \\
        \hline
        0 & 3 & 50 & $0$\\
        1 & 2, 4, 6 & 20, 40, 60 & $0.4$ \\
        2 & 1, 5 & 10, 20 , 30, 60 & 0\\
        3 & 2, 4, 5 & 10, 20, 30, 40 & $0.3$ \\
        4 & $1$ & $10, 40, 60$ & $0$ \\
        \hline
    \end{tabular}
    \caption{\khop labels of vertices $3$ and $50$.}
    \label{tab:khop350}
\end{table}

\subsection{Neighbor Concatenated Label} In Neighbor concatenated label (\ncl) ,
the information regarding the candidates of a neighbor that were pruned in the
previous iteration is used along with the current \khop label to prune
candidates in the current iteration. In contrast, the \khop label pruning
strategy for a vertex $u$ works independently of the result of \khop label
pruning of other vertices in the pattern. This leads us to the following
recursive formulation for \ncl.

The \ncl of a vertex in the ${k+1}^{th}$ iteration, $\nclab{k+1}{u}$, is defined
as the tuple $(\{\nclab{k}{u'} | u' \in N(u)\},\xspace \khopl{k+1}{u})$.  The
first element(A) of the tuple is the \ncl of the neighbors of the vertex $u$ in
the previous iteration and the second element(B) is exactly same as the \khop
label defined in the previous section. We say that $\nclab{k+1}{v}$ dominates
$\nclab{k+1}{u}$, denoted by $\nclab{k+1}{u} = (A, B) \preceq \nclab{k+1}{v} =
(A', B') $, i) iff $\khopcost{k+1}{B}{B'} \leq \alpha$ i.e., the minimum cost of
matching the \khop labels is within $\alpha$ ii) there exits an injective
function $g\!\!:A\rightarrow A'$ such that $a \preceq g(a)$ for all $a \in A$
i.e., there is a one to one mapping between the \ncl labels (in the previous
iteration, $k$) of neighbors of $u$ and $v$.  The base case $\nclab{0}{u}
\preceq \nclab{0}{v}$ iff $\matij{C}{L(u)}{L(v)} \leq \alpha$. For example, in
Fig~\ref{fig:ncexample} $\nclab{1}{2} \preceq \nclab{1}{20}$ because
$\khopcost{1}{2}{20} \leq \alpha$ and the \ncl labels of vertices $1, 3, 5$ are
dominated by the \ncl labels of vertices $10, 50, 30$ respectively.  The
following theorem states that the \ncl of a pattern vertex $u$ is dominated by
the \ncl of any of its representative vertex $v \in  R(u)$.

\begin{thm} Given any pattern vertex $u$, a representative vertex $v \in R(u)$
    and cost threshold $\alpha$, $\nclab{k}{u} \preceq \nclab{k}{v}$ for all $k
    \geq 0$.  \begin{myproof} Let $\phi$ be any isomorphism such that $\phi(u) =
        v$.  We prove the theorem by using induction on $k$.\\ \textbf{Base
        case:} $\nclab{0}{u} \preceq \nclab{0}{v} \iff \matij{C}{L(u)}{L(v)}
        \leq \alpha$ is true because $v \in R(u)$. \\ \textbf{Inductive
        Hypothesis:} Assume that $\nclab{k}{u} \preceq \nclab{k}{v}$ holds true
        for all $u \in \pat$ and $v \in R(u)$. \\ Now consider $\nclab{k+1}{u} =
        (A, B)$  and $ \nclab{k+1}{v} = (A', B') $, from theorem \ref{thm:khop}
        we know that $\khopcost{k+1}{B}{B'} \leq \alpha$, for all $k \geq 0$.
        Let $u'$ be a neighbor of $u$ and let $v' \in \phi(u')$ , then from the
        inductive hypothesis $\nclab{k}{u'} \preceq \nclab{k}{v'}$.  Therefore,
        the injective function $\phi$ maps the elements $a \in A$ to $\phi(a)
        \in A'$.  The theorem follows from the definition of the NL label.
    \end{myproof} \label{thm:ncl} \end{thm}

Based on the above theorem, a vertex $v$ can be pruned from \CR if $\nclab{k}{u}
\not\preceq \nclab{k}{v}$ for some $k \geq 0$. In Fig~\ref{fig:ncexample} ,
consider the vertices $3 \in \pat$ , $50 \in \db$ and let $\alpha = 0.5$. The
\ncl labels, $\nclab{0}{3} \preceq \nclab{0}{50}$ as $\matij{C}{B}{B} = 0 \leq
\alpha$.  Similarly it is also true for the pairs $(2, 20)$, $(4, 40)$ etc. It
follows that $\nclab{1}{3} \preceq \nclab{1}{50}$ as the neighbors $2, 4, 6$ can
be mapped to $20, 40, 60$ respectively and  the minimum cost of the matching the
$1$-hop label is $0.4$ which is less than the $\alpha$ threshold. But
$\nclab{2}{3} \not\preceq \nclab{2}{50}$ because the \ncl label $\nclab{1}{6}$
is not dominated by the \ncl label of $20, 40$ or  $60$ in the previous
iteration. So, there is no mapping between the neighbors of vertices $3$ and
$50$ in the current iteration. Hence, the vertex $50$ can be pruned from the
candidate representative set of vertex $3$.  Note that using the \khop label in
the same example will not prune the vertex $50$ because the minimum cost of
matching the \khop labels is within $\alpha$ as shown in table
\ref{tab:khop350}. Therefore, \ncl label is more efficient compared to \khop
label as it subsumes the latter label.

\subsection{Candidate set verification} \label{sec:verification} The pruning
methods based on the \khop and the \ncl labels start with a \CR and prune some
of the candidate vertices based on the conditions described in theorems
\ref{thm:khop} and \ref{thm:ncl}.  The verification step reduces \CR to \RS by
retaining only those vertices for which there exists an isomorphism $\phi$ in
which $\phi(u) = v$.  Informally, it does this by checking if the pattern $P$ can be
embedded at $v$ such that total cost of label mismatch is at most $\alpha$.

Let $w_p = u_0,\ldots, u_m$ be a walk in the pattern that covers each edge of
the pattern at least once starting from vertex $u$ i.e. $u_0 = u$. Finding a
path that covers each edge at least once is a special case of the Chinese
postman problem \cite{chinesepostman} where the distance between pair of
vertices is one. The following three conditions are satisfied iff the vertex $v$
represents the vertex $u$. i) there exists a walk $w_d = v_0,\ldots, v_m$,
$\forall (v_i, v_{i+1}) \in w_d$, $v_i \in R(u_i)$ and $v_{i+1} \in R(u_{i+1})$.
ii) $v_i = v_j$ implies that $u_i = u_j$. iii) $\sum\matij{C}{L(u_i)}{L(v_i)}
\leq \alpha$ where $u_i \in \vp$. Unlike the \ncl label,
these conditions are necessary and sufficient and can be verified by following
the definition of isomorphism. Using an example we will
show how these conditions can be used to check definitely whether $v \in R(u)$.

Consider checking whether the vertex $30$ is a valid representative of the vertex
$1$ in the pattern in the figure~\ref{subfig:ex_sub} and let $\alpha = 0.5$ . The
walk $ w_p = 1, 2, 4, 3, 1$ covers each edge of the pattern at least once. A
walk $w_d$, in the database in the figure~\ref{subfig:ex_db},
that satisfies the above three conditions should start by mapping the
vertex $1$ to $30$. The total cost of matching the labels till now is $0.2$ and
the budget available for matching other vertices in $P$ is $0.5 - 0.2 = 0.3$.
We will cover the walk $w_p$ one edge at a time. For any $(u_i, u_{i+1}) \in
w_p$, if $u_i$ and $u_{i+1}$ are mapped, then we verify that there is an edge
between the database vertices $v_i$ and $v_{i+1}$ to which the pattern vertices
are mapped.  If however, $u_{i+1}$ is not mapped then we map it to some vertex
$v_{i+1} \in R'(u_{i+1})$ and subtract the cost of this mapping from the
remaining $\alpha$ cost.  For example, in the first step $(1,2)$ we can map $2$ to $20$.
The remaining cost is then $0.3 - 0.2 = 0.1$. In the next step $(2,3)$, the
vertex $3$ cannot be mapped to any vertex in the database without violating the
third condition.  In such a case, we back track one step and choose a different
mapping say $10$ for the vertex $2$ which is the last vertex that was mapped.
Proceeding this way, we can arrive at the mapping corresponding to $\phi_{1}$ as
in table~\ref{subfig:ex_occur}.  This isomorphism not only guarantees that $30
\in R(1)$, it also implies that the verification check between the pairs $(2,
10), (3, 60)$ and $(4, 40)$ can be avoided because of the approximate isomorphism
$\phi_1$ that was found.  The above procedure can extended to enumerate the
complete set of isomorphisms.

\subsection{Label costs and dominance checking} \label{sec:labelcheck}
Candidate representative vertices are pruned by checking for dominance
relation between the \ncl labels of pattern vertex and that of candidate
vertex in the database. Comparing the \ncl labels requires i) computing the
cost of matching the \khop labels ii) matching the neighbors of pattern vertex
with neighbors of the candidate vertex. First problem can be formulated
as a minimum cost maximum flow in a network and the second as maximum matching
in a bipartite graph.

\medskip{\textit{Computing \khop label cost}:} To compute the minimum matching
cost between the \khop labels $\khopl{k}{u}$ and $\khopl{k}{v}$, we compute the
maximum flow with minimum cost in a flow network $F$ defined as follows.  Each
edge in $F$ is associated with a maximum capacity and the cost for sending a
unit flow across it.  The network contains a vertex for each label $l_u =
L(u')$ where $v' \in h_k(u)$ and a vertex for each label $l_v = L(v')$ where $v'
\in h_k(v)$. There is a directed between between source vertex ($s$) and each
$l_u$ with a zero cost and a capacity equal to the multiplicity of the $l_u$
i.e., the number of vertices in $h_k(u)$ that have the label $l_u$. Similarly
there is a directed edge between $l_v$ and the sink node ($t$). In addition,
there is a directed edge from $l_u$ to $l_v$ with a cost
equal to $\matij{C}{l_u}{l_v}$ and a capacity equal to the
multiplicity of $l_u$. The cost between the \khop labels is equal to
the minimum cost for maximum flow if the maximum flow is equal to
$|\khopl{k}{u}|$ and $\infty$ otherwise.  \\ Figure~\ref{fig:Hflow} shows the
flow network required to compute the minimum cost of matching the \khop labels
$\khopl{2}{2} = 4,6 $ and $\khopl{2}{20} = 40, 50, 60$ as shown in Table
\ref{tab:khop220}. The labels of vertices in the \khop labels are $C,D$ and $B,
B, A$ respectively. The capacity of the edge betweenn $B$ and $t$ is two because
both the vertices $40$ and $50$ have the same label $B$.
There is an edge from $s$ to each of $C, D$ with zero cost
and maximum capacity of one.  Similarly, there is an edge from each of $A, B$ to
the sink vertex $t$ with zero cost and maximum capacity of one and two
respectively. There is an edge from $C, D$ to each of $A, B$ with cost equal to
the corresponding entry in the cost matrix $C$. The maximum flow in the network
is two and the minimum cost of sending two units of flow $0.4$ is achieved by
pushing a unit flow along the paths $s, C, B, t$ and $a, D, A, t$.  Therefore,
the cost of matching the labels $\khopl{2}{2}$ and $\khopl{2}{20}$ is $0.4$. It
implies that the vertex $4$ with label $C$ can be matched to either $40$ or $50$
and the vertex $6$ to $60$.

\medskip{\textit{Dominance check}: } Consider the
\ncl labels $\nclab{k+1}{u} = (A, B)$ and  $\nclab{k+1}{v} = (A', B')$, the cost matching
$B$ and $B'$ can be computed using the above the network formulation.
Finding an injective function $f\!\!:A \rightarrow A'$ such that $a \preceq
f(a)$ , is equivalent to find a matching of size $|N(u)|$ in the bipartite graph
with edges $(a, a')$, for all $a \in A$ and $a \preceq a'$.
The \ncl label $\nclab{k}{v}$ therefore dominates $\nclab{k}{u}$ if the
cost between the \khop labels is within $\alpha$ and the size of maximum
bipartite matching is $|N(u)|$.

\medskip{\textit{Optimization}: } The candidate pattern may contain groups of
symmetric vertices that are indistinguishable with respect to the \khop label.
In such a scenario, the candidate representative sets of all these vertices are
exactly the same. Utilizing the symmetry, we can apply the pruning label strategy only on
one vertex per symmetry group and replicate the results for all other vertices
in the group. For example, the vertices $10$ and $40$ in
figure~\ref{subfig:ex_sub} are symmetric and the representative sets $R(10)$
and $R(40)$ are exactly the same.  In abstract algebra terms such  groups are
called orbits of the graph and can be computed using nauty algorithm
\cite{nauty}.
Even
though computing the orbits is expensive, we can avoid $ (|g|-1) \times |\CR|$
\ncl label cost computations where $g$ is the size of an orbit. Note that
we find the orbits only for the pattern which is usually very small compared
to the database graph.
%Note that the payoff is zero if all the vertex orbits are of size $1$.

\subsection{Precomputing database \khop labels} The \khop label of the database
vertices is independent of the candidate pattern.
Also, the flow network to compute the cost of matching the \khop labels requires
only the aggregate information about the number of vertices of a given label.
Hence, we can precompute the \khop label of the database vertices and store them
in the memory. The following theorem proves that computing \khop label is
expensive.

\begin{thm} k-reachable (KR) : Given a graph $G$, $k$ and $u \in \vg$. Compute
    $\khopl{k}{u}$.  KR cannot be solved in polynomial time unless $P =
    NP$.

\begin{myproof} We prove this by reducing Hamiltonian path (HP) to KR.
    Hamiltonian Path : Given a graph $G$, is there a simple path of length
    $|\vg|-1$ i.e. is there a path that visits each and every vertex exactly
    once. The problem of finding a Hamiltonian path is proven to be an
    NP-Complete problem \cite{npcomplete}.\\ Assume that algorithm $X(k)$ can
    compute KR in polynomial time. Let $|\vg| = n$ and $u$ be the starting
    vertex in HP if it exists.  Given an instance of HP, we first get a vertex
    $v$, $\kpath{u}{v}{n-1}$ using $X(n-1)$. The vertex $v$ is removed from the
    graph and we find a vertex $v'$ such that $\kpath{u}{v'}{n-2}$ and $(v', v)
    \in \eg$. We repeat this process $n-1$ times. If at any stage $X(j) = \{\}$
    then we restart from a different starting vertex. The vertices selected in
    each iteration lie on a path of length $n-1$ if it exists. If there is
    polynomial time algorithm for KR then HP could be solved in polynomial time
    by reducing it to KR. Therefore, $KR$ is atleast as hard as $HP$.
    So, KR cannot be solved in polynomial time unless
    P = NP.
\end{myproof}
\end{thm}

To compute \khop label of a vertex $u$, we check for each vertex $v$ whether $v
\in \kpath{u}{v}{k}$ by enumerating all possible $k$ length paths until a path
is found.  This procedure is exponential, we therefore fix a maximum value
$k_{max}$ and use the \khop label based pruning only for values of $k \leq
k_{max}$.  It only takes a couple of minutes to compute the \khop label for $k
\leq 6$ for all the vertices in the database graph. This is significantly less
than the overall run time of the algorithm. Once $\khopl{k}{u}$ is computed we
store in memory only the tuples $(m, l)$ where $m$ is the number of vertices $u'
\in \khopl{k}{u}$, $L(u') = l$. The total amount of main memory required to store
the precomputed \khop labels is O($|\vg| \times |\Sigma| \times k_max$).

\begin{figure}[!h]
    \centering
\scalebox{0.6}[0.6]{
  \psset{unit=0.85in}
  \newcommand\arc[4]{\ncline{#1}{#2}{#3}\ncput{\colorbox{gray!40}{#4}}}
      \begin{pspicture}(0,1)(5,3)
        \cnodeput[doubleline=true](1,2){src}{s}
        \cnodeput(2,1){n1}{C}
        \cnodeput(2,3){n2}{D}
        \cnodeput[doubleline=true](5,2){sink}{t}
        \cnodeput(4,1){n4}{B}
        \cnodeput(4,3){n5}{A}
        \arc{->}{src}{n1}{$1,0$}
        \arc{->}{src}{n2}{$1,0$}
        %\arc{->}{n1}{n4}{$1$}
        \ncline{->}{n1}{n4}\ncput[npos=0.5]{\colorbox{gray!40}{$1,0.1$}}
        \ncline{->}{n1}{n5}\ncput[npos=0.3]{\colorbox{gray!40}{$1,1$}}
        \ncline{->}{n2}{n4}\ncput[npos=0.3]{\colorbox{gray!40}{$1,0.6$}}
        \ncline{->}{n2}{n5}\ncput[npos=0.5]{\colorbox{gray!40}{$1,0.3$}}
        \arc{->}{n4}{sink}{$2,0$}
        \arc{->}{n5}{sink}{$1,0$}
        %\arc{->}{n6}{sink}{$1$}
      \end{pspicture}
    }
    \caption{Flow network for \khopl{2}{2} and \khopl{2}{20}}
	\label{fig:Hflow}
\end{figure}