% \documentclass[draft,11pt]{article}
%\documentclass[11pt]{article}
\chapter{Classical
  Algorithms for Maximum Flow I}
\label{cha:maxflow1}
%\input{../defs}
%\input{../layout-defs}
%\usepackage{amsmath}
%\usepackage{amssymb}
%\usepackage{amsthm}
%\usepackage{graphicx}
%\usepackage{float}
%\usepackage[ruled,vlined]{algorithm2e}
%\SetKwBlock{Repeat}{repeat}{}
%%% for this lecture
%\newcommand{\gap}{\text{gap}}
% \newtheorem{theorem}{Definition}
%\interfootnotelinepenalty=10000
% \newtheorem{lemma}{Lemma}

%\begin{document}

\sloppy
%\lecture{10 --- Wednesday, April 29th}
%{Spring 2020}{Rasmus Kyng, Scribe: Meher Chaitanya}{Classical
  %Algorithms for Maximum Flow}
\section{Maximum Flow}
% \section{Overview}
In this chapter, we will study the \emph{Maximum Flow Problem} and
    discuss some classical algorithms for finding solutions to it.
% Talk about max flow, classical algorithms for maximum
% flow. interesting open problems with things we can do with classical
% algorithms and things we can do with modern algorithms and close the
% gap between them.
%
\paragraph{Setup.} Consider a directed graph \(G=(V, E, \cc)\), where
$V$ denotes vertices, $E$ denotes edges and $\cc \in \R^{E},  \cc \geq 0$ denotes edge
capacities. In contrast to earlier chapters, the direction
of each edge will be important, and not just as a book keeping tool
(which is how we previously used it in undirected graphs).
We consider an edge $(u,v) \in E$ to be \emph{from} $u$ and \emph{to}
$v$.
Edge capacities are associated with directed edges, and we allow both
edge $(u,v)$ and $(v,u)$ to exist, and they may have different
capacities.

A \emph{flow} is any vector $\ff \in \R^E.$

We say that a flow is feasible
when $\veczero \leq \ff \leq \cc$.
The constraint $\veczero \leq \ff$ ensures that the flow respects edge
directions, while the constraint $\ff \leq \cc$ ensures that the flow
does not exceed capacity on any edge.

We still define the edge-vertex incidence matrix $\BB \in \R^{V \times E}$ of $G$ by
\[
  \BB(v,e) =
  \begin{cases}
    1 & \text{ if } e = (u,v) \\
    -1 &\text{ if } e = (v,u) \\
    0 &\text{ o.w.}
  \end{cases}
\]
As in the undirected case, a \emph{demand vector} is a vector \(\dd \in \R^{V} \).
And as in the undirected case, we can express the net flow constraint that
$\ff$ routes the demand $\dd$ by
\[
  \BB \ff = \dd
  .
\]
We will focus on the case:
  \begin{equation} \nonumber
      \BB\ff= F (-\ee_{s}+\ee_t) = F \bb_{st}.
  \end{equation}
 Flows that satisfy the above equation for some scalar \(F\) are
 called $s$-$t$ flows where \(s, t \in V\).
 The vertex \(s\) is called the source, \(t\) is called the sink and  \(\ee_s\), \(\ee_t\) are indicator vectors for source and sink nodes respectively. The vector \(\bb_{st}\) has -1 at source and 1 at sink. The maximum flow can be expressed as a linear program as follows
 \begin{equation}
   \label{eq:maxflow}
\begin{aligned}
\max_{\ff \in \R^E,F} \quad & F\\
\textrm{s.t.} \quad & \BB\ff= F \bb_{st}\\
  & 0 \leq \ff \leq \cc   \\
\end{aligned}
\end{equation}
We use $\val(\ff)$ to denote $F$ when $\BB\ff = F\bb_{st}$.

\section{Flow Decomposition}
We now look at a way of simplifying flows
\begin{definition}
  A $s$-$t$ path flow is a flow \(\ff \geq \veczero\) that can be written as
  \begin{equation}\nonumber
      \ff = \alpha \sum_{e \in p} \ee_e
  \end{equation}
  where p is a simple path from s to t.
\end{definition}
\begin{definition}
  A cycle flow is a flow \(\ff \geq \veczero \) that can be written as a cycle i.e.
  \begin{equation}\nonumber
      \ff = \alpha \sum_{e \in c} \ee_e
  \end{equation}
  where c is a simple cycle.
\end{definition}
% }
% \item GRADED? gaussian elimination for Laplacians is closed ?
% \end{enumerate}
\begin{lemma}[The path-cycle decomposition lemma]
 Any $s$-$t$ flow \(\ff\) can be decomposed into a sum of $s$-$t$ path
 flows and cycle flows such that the sum contains at most
 \(\nnz(\ff)\) terms. Note $\nnz(\ff) \leq \abs{E}$.
\end{lemma}
\begin{proof}
We perform induction on the number of edges with non-zero flow, which
we denote by $\nnz(\ff)$.
Note that by ``the support of $\ff$'', we mean the set $\setof{e \in E :
  \ff(e) \neq 0}$.
\paragraph{Base case:}  \(\ff = 0\): nothing to do.
\paragraph{Inductive step:}
Try to find a path from \(s\) to \(t\) OR a cycle in the support of \(\ff\). \\
\emph{``Path'' case.}
If there exists such a $s$-$t$ path, let \(\alpha\) be the minimum flow value along the edges of the path, i.e.
\begin{equation} \nonumber
\begin{aligned}
\alpha = \min_{(a,b) \in p} \ff(a,b)
\end{aligned}
\end{equation}
\begin{equation} \nonumber
    \ff^{'} = \alpha \sum_{e \in p} \ee_e
\end{equation}
Update the flow $\ff$ by
\begin{equation} \nonumber
    \ff  \leftarrow \ff - \ff'
\end{equation}
The value of the flow will still be non-negative after this update as we
subtracted the minimum entry along any positive edge on the path. The
number of non-zeros, $\nnz(\ff)$, went down by at least one.
Note that the updated $\ff$ must again be an $s$-$t$ flow, as it is the
difference of two $s$-$t$ flows.
% If we continue this procedure by finding paths,
% eventually the flow value $\val(\ff)$ must become zero.

\emph{``Cycle'' case.}
Suppose we find a cycle \(c\) in the support of \(\ff\).
Let \(\alpha\) be the minimum flow value along the edges of the cycle, i.e.\
\begin{equation*}
\alpha = \min_{(a,b) \in c} \ff(a,b)
\end{equation*}
\begin{equation*}
 \ff^{'} = \alpha \sum_{e \in c} \ee_e
\end{equation*}
Update the flow $\ff$ by
\begin{equation} \nonumber
    \ff  \leftarrow \ff - \ff'
  \end{equation}
  As in the path case, $\ff$ stays non-negative, and
number of non-zeros, $\nnz(\ff)$, goes down by at least one.
Note that the updated $\ff$ must again be an $s$-$t$ flow, as it is the
difference of two $s$-$t$ flows.

\emph{``No path or cycle'' case.}
Suppose we can find neither a path nor a cycle, and $\ff \neq
\veczero$.
Then there must be an edge $(u,v)$ with non-zero flow leading into a
vertex $v \neq s,t$ and with no outgoing edge from
$v$ in the support of $\ff$.
In that case, we must have $(\BB\ff)(v) \geq 0$.
But since $v \neq t$, this contradicts $\BB \ff = F \bb_{st} $.
So this case cannot occur.
\end{proof}
\begin{lemma}
In any $s$-$t$ max flow problem instance, there is an optimal flow \(\ff^*\) with a path-cycle decomposition that has only paths and no cycles.
\end{lemma}

\begin{proof} Let \(\fftil\) be an optimal flow. Let \(\ff^*\) be the
  sum of path flows in the path cycle decomposition of
  \(\fftil\). They route the same flow (as cycles contribute to no net
  flow from $s$ to $t$). Thus
 \begin{align*}
  \BB\ff^* = \BB\fftil
 \end{align*}
 and hence $\val(\ff^*) = \val(\fftil)$.
 Furthermore
\begin{equation*}
  \veczero \leq \ff^* \leq \fftil \leq \cc
\end{equation*}
The first inequality follows from  \(\ff^*\) being a sum of positive
path flow.
The second inequality holds as $\ff^*$ is upper bounded in every single entry by \(\fftil\), because we reduced
it by positive entry cycles.
The third inequality holds because
$\fftil$ is a feasible flow, so it is upper bounded by the capacities.
\end{proof}
An optimal flow solving the maximum flow problem may not be unique.
For example, consider the graph below with source s and sink t:\\
\begin{figure}[H]
 \centering
  \includegraphics[width=50mm,scale=0.1]{fig/fig1_lec10.PNG}
  \label{fig:ex1}
\end{figure}
There are two optimal paths in this example.
Maximum flow is a convex optimization problem but not a strongly
convex problem as the solutions are not unique, and this is part of
what makes it hard to solve.

\section{Cuts and Minimum Cuts}

The decomposition shown earlier provides a way to show that the
maximum flow in a graph is upper bounded by constructing graph
cuts.

Given a vertex subset $S \subseteq V$, we say that $(S, V\setminus S)$
is a \emph{cut} in $G$ and that the value of the cut is
\[
  c_G(S) = \sum_{e \in E \intersect (S \times V\setminus S) } \ww(e)
  .
\]

Note that in a directed graph, only edges crossing the cut
going \emph{from} $S$ and \emph{to} $V\setminus S$ count toward the cut.

\begin{figure}[H]
 \centering
 \includegraphics[width=50mm,scale=0.1]{fig/fig2_lec10.PNG}
 \caption{Example of a cut: No edges go from $S$ to $(S, V\setminus
   S)$, and so the value of this cut is zero.}
  \label{fig:cutnoforwardedges}
\end{figure}

\begin{definition*}
  ($s$-$t$ cuts). We define an $s$-$t$ cut to be a subset $S \subset
  V$, where $s \in S$ and $t \in V \setminus S$.

  %\begin{equation}\nonumber
  %    \ff = \alpha \sum_{e \in c} %\underline{e}_{e}
 % \end{equation}
  %where c is a simple cycle.
\end{definition*}

\paragraph{A decision problem:}
\emph{``Given an instance of the Maximum Flow  problem, is there a flow from s to t such that \(\ff \neq \veczero\)?''}

\begin{description}
\item[If YES:] We can decompose this flow into $s$-$t$ path flows and
  cycle flows.
\item[If NO:]  There is no flow path from $s$ to $t$. Let $S$ be the
  set of vertices reachable from source $s$. Then (\(S,V \setminus
  S\)) is a cut in the graph, with no edges crossing from $S$ to \( V
  \setminus S\). Figure~\ref{fig:cutnoforwardedges} gives an example.
\end{description}

\paragraph{Upper bounding the maximum possible flow value.}
How can we recognize a maximum flow? Is there a way to confirm that a
flow is of maximum value?

We can now introduce the \emph{Minimum Cut} problem.
%
\begin{align}
   \label{eq:mincutset}
\min_{S \subseteq V } \ & c_G(S)\\ \nonumber
\textrm{s.t. } & s \in V \text{ and } t \not\in V
\end{align}
The Minimum Cut problem can also be phrased as a linear program,
although we won't see that today.

We'd like to obtain a tight connection between flows and cuts in the
graph.
As a first step, we won't get that, but we can at least observe that the value of any $s$-$t$ cut
provides an upper bound to the maximum possible $s$-$t$ flow value.

\begin{theorem}[Max Flow $\leq$ Min Cut] The maximum $s$-$t$ flow
  value in a directed graph G \mbox{(Program~\eqref{eq:maxflow})} is upper bounded by the minimum value of
  any $s$-$t$ cut (Program~\eqref{eq:mincutset}).
  I.e.\ if $S$ is an $s$-$t$ cut, and $\ff$ a feasible $s$-$t$ flow
  then
  \[
    \val(\ff) \leq c_G(S)
  \]
  And in particular this holds for any minimum cut $S^*$ and maximum
  flow $\ff^*$, i.e.\
  $\val(\ff^*) \leq c_G(S^*)$.
\end{theorem}
\begin{proof}

Consider any feasible flow  \(0 \leq \ff \leq \cc \) and a cut \(S,T = V\setminus S\).
% such that \(S \cup T = V \).  % This is always true for a cut..
Consider a path-cycle decomposition of
\(\ff\), where each $s$-$t$ path must cross the cut going forward from
S to T at least once. We pick a cut $S,T$ with source on one side and
sink on the other side as shown in the Figure (\ref{fig:ex5}). Every
time the path flow passes through the cut, it has to use one of the
edges that connect $S$ and $T$. Total amount of flow crossing the cut
is bounded above by total amount of capacity of the cut, otherwise the
capacities would be violated, thus
$ \val(\ff) \leq c_{G}(S,T) = \sum_{e\in E \cap S \times T}{\cc(e)}$.
\begin{figure}[H]
 \centering
  \includegraphics[width=100mm,scale=0.5]{fig/fig5_lec10.PNG}
  \caption{$s$-$t$ Cut}\label{fig:ex5}
\end{figure}
Note that the edges from $T$ to $S$ do not count towards the cut. The above equation holds for all flows with all $s$-$t$ cuts. This implies that
    $\textbf{max flow} \leq \textbf{min cut}$.
\end{proof}
This theorem is an instance of a general pattern, known as \emph{weak
duality}. Weak duality is a relationship between two optimization programs, a maximization
problem and a minimization problem, where any solution to the former has
its value upper bounded by the value of any solution to the latter.

%\newpage

\section{Algorithms for Max flow}
\paragraph{How can we find a good flow?}\ \newline

\begin{algorithm}[H]
\caption{A first attempt -- bad idea?}
\SetAlgoLined

 \(\ff \leftarrow \veczero\)\;
 \Repeat{
  Find an $s$-$t$ path flow \(\fftil\) that is feasible with respect to \(\cc - \ff\).\\
  \(\ff  \leftarrow \ff + \fftil\)}
\end{algorithm}

\paragraph{Does Algorithm 1 work?}
Consider the graph below with directed edges with capacities 1 at
every edge.
If we make a single path update as shown by the orange lines in
Figure~\ref{fig:ex6}, then afterwards, using the remaining capacity,
there's no path flow we can route, as shown in Figure~\ref{fig:ex6}.
But the max flow is 2, as shown by the flow on orange edges in
Figure~\ref{fig:ex8}.
So, the above algorithm does not always find a maximum flow: It can
get stuck at a point where it cannot make progress despite not having
reached the maximum value.
\begin{figure}[H]
  \centering
  \includegraphics[width=80mm,scale=0.5]{fig/fig6_lec10.PNG}
  \caption{Sending a unit $s$-$t$ path flow through the graph.}
    \label{fig:ex6}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[width=50mm,scale=0.5]{fig/fig7_lec10.PNG}
  \caption{Remaining edge capacities after sending a path flow through
    the graph as depicted in Figure~\ref{fig:ex6}.}
  \label{fig:ex7}
\end{figure}
\begin{figure}[H]
 \centering
 \includegraphics[width=50mm,scale=0.5]{fig/fig8_lec10.PNG}
 \caption{The flow depicted routes two units from $s$ to $t$.}
  \label{fig:ex8}
\end{figure}

\paragraph{A better approach.} It turns out we can fix the problem
with the previous algorithm using a simple fix. This idea is known as
\emph{residual graphs}.

\begin{algorithm}[H]
\SetAlgoLined
 \(\ff \leftarrow \veczero\)\;
 \Repeat{
 Find an $s$-$t$ path flow \(\fftil\) that is feasible with respect to \( - \ff\leq \fftil \leq \cc - \ff\).\\
  \(\ff  \leftarrow \ff + \fftil\)}
 \caption{Better Idea (Residual Graph)}
\end{algorithm}

The \(-\ff(e)\) can be treated as an edge going in the other direction with
capacity \(\ff(e)\).
By convention, an edge in $G$ with $\ff(e) = \cc(e)$ is called
\emph{saturated}, and we do not include the edge in $G_f$.
The graph defined above with such
capacities is called \textbf{the residual graph of \(\ff\)},
\(G_{\ff}\).  \(G_{\ff}\) is only defined for feasible \(\ff\), since
otherwise the constraint $\fftil \leq \cc - \ff$ gives trouble.

Suppose we start with a graph having a single edge with capacity 2 and we introduce a flow of 1 unit.
\begin{figure}[H]
 \centering
  \includegraphics[width=25mm,scale=0.6]{fig/fig9_lec10.PNG}
  \label{fig:ex9}
\end{figure}
The residual graph \(G_{\ff}\) has an edge with capacity 1 going
forward and $-1$ capacity going forward, but we can treat the latter as $+1$
capacity going backwards. So it is an edge that allows you to undo the choice made to send flow along that direction.
\begin{figure}[H]
 \centering
  \includegraphics[width=40mm,scale=0.5]{fig/fig10_lec10.PNG}
  \label{fig:ex10}
\end{figure}
Let us consider the same example with its residual graph. The original graph is shown in Figure \ref{fig:ex11}.
\begin{figure}[H]
 \centering
  \includegraphics[width=40mm,scale=0.5]{fig/fig11_lec10.PNG}
  \caption{Original graph $G$ and an $s$-$t$ path flow in $G$ shown in
    orange.}
  \label{fig:ex11}
\end{figure}
The residual graph for the same is shown in \autoref{fig:ex12}.
\begin{figure}[H]
 \centering
  \includegraphics[width=40mm,scale=0.5]{fig/fig12_lec10.PNG}
  \caption{The residual graph w.r.t.\ the flow from
    Figure~\ref{fig:ex11}, and new $s$-$t$ path flow which is feasible
  in the residual graph.}\label{fig:ex12}
\end{figure}
Adding both flows together, we get the paths as shown in Figure \ref{fig:ex13} with value 2, which is the optimum.
\begin{figure}[H]
 \centering
  \includegraphics[width=90mm,scale=0.5]{fig/fig13_lec10.PNG}
  \caption{Maximum flow in the graph.}\label{fig:ex13}
\end{figure}
Let's prove some important properties of residual graphs.
\begin{lemma}
    \label{lem:gtogres}
Suppose   \(\ff\), \(\ffhat\) are feasible in $G$. Then this implies that \(\ffhat - \ff\)  (where negative entries count as flow in opposite direction) is feasible in \(G_{\ff}\).
\end{lemma}
\begin{proof}
\(\veczero \leq \ff \leq \cc\) and  \(\veczero \leq \fftil \leq \cc\), this implies \(-\ff \leq \fftil - \ff \leq \cc - \ff \). Hence, proved.
\end{proof}
\begin{lemma}
  \label{lem:grestog}
Suppose that \(\ff\) is feasible in \(G\) and \(\ffhat\) is feasible in \(G_{\ff}\). Then, \(\ff + \ffhat\) is feasible in G.
\end{lemma}
\begin{proof}
\(\veczero \leq \ff \leq \cc\) and  \(\ff \leq \fftil \leq \cc - \ff \), this implies \(\veczero \leq \fftil + \ff \leq \cc\).
\end{proof}

\begin{lemma}
A feasible \(\ff\) is optimal if and only if \(t\) is not reachable from \(s\) in \(G_{\ff}\).
\end{lemma}
\begin{proof}
 Let \(\ff\) be optimal, and suppose $t$ is reachable from $s$ in \(G_{\ff}\)
 then, we can find a $s$-$t$ path flow \(\fftil\) that is feasible in \(G_{\ff}\),
 and \(\val(\ff + \fftil) > \val(\ff)\). \(\ff +
 \fftil\) is feasible in $G$ by Lemma~\ref{lem:grestog}. This is a
 contradiction, as we assumed \(\ff\) was supposed to be optimal.

 Suppose $t$ is not reachable from $s$ in \(G_{\ff}\), and \(\ff\) is
 feasible, but not optimal. Let \(\ff^*\) be optimal, then by
 Lemma~\ref{lem:gtogres}, the flow \(\ff^*-\ff\) is feasible in \(G_{\ff}\) and \(\val(\ff^* - \ff) > 0\). So there exists a $s$-$t$ path flow from $s$ to $t$ in \(G_{\ff}\) (as we can do a path decomposition of \(\ff^* - \ff\)). But, this is a contradiction as $t$ is not reachable from $s$ in \(G_{\ff}\).
\end{proof}

\begin{theorem}[Max Flow $=$ Min Cut theorem] \label{thm:maxFlowEqualsMinCut} The maximum flow in a directed graph G equals the minimum cut.
\end{theorem}
\begin{proof}
%\subsubsection{Strong Duality}
Consider the set $S = \setof{ \text{vertices reachable from } s \text{ in
  } G_{\ff}}$.
Note that \(\ff\) saturates the
edge capacities in cut \(S,V\setminus S\) in G:
Consider any edge from $S$ to $T:=V \setminus S$ in $G$. Since this edge does not
exist in the residual graph, we must have $\ff(e) = \cc(e)$.
% Suppose for a contradiction that say that there exists an edge from set $S$ to set $T$ in G as shown in Figure~\ref{fig:ex13} below. Since $S$ and $T$ are disconnected in \(G_{\ff}\), it means that the edges in G from $S$ to $T$ should have used up all their capacity.
\begin{figure}[H]
 \centering
 \includegraphics[width=60mm,scale=0.5]{fig/fig14_lec10.PNG}
 \caption{The cut between vertices reachable from $S$ and everything
   else in $G_f$ must have all outgoing edges saturated by $\ff$.}
  \label{fig:ex14}
\end{figure}
%
This means that
\begin{equation} \nonumber
   \val(\ff) \geq c_G(S, V \setminus S).
\end{equation}
Since we already know the opposite inequality by weak duality, we have
shown that
\begin{equation} \nonumber
   \val(\ff) = c_G(S, V \setminus S)
\end{equation}
which proves the \textbf{Max Flow} = \textbf{Min Cut} theorem; also called strong duality.
\end{proof}

\begin{remark}
  Note that this proof also gives an algorithm to compute an $s,t$-min
  cut given an $s,t$ maximum flow $\ff^*$: Take one side of the cut to be the
  set $S$ of nodes reachable from $s$ in the residual graph w.r.t. $\ff^*$.
\end{remark}

\paragraph{Ford-Fulkerson Algorithm.}\ \\
\begin{algorithm}[H]
\SetAlgoLined
 \Repeat{
 Add update by arbitrary $s$-$t$ path flow in \(G_{\ff}\) (augment the flow \(\ff\) by the path flow)
}
 \caption{Ford-Fulkerson Algorithm}
\end{algorithm}

\subsubsection{Convergence properties and analysis of runtime of Ford-Fulkerson algorithm}
\begin{itemize}
    \item \textbf{Does this algorithm terminate?}\\
    The algorithm terminates if the capacities are integers. However for irrational capacities the algorithm may not terminate.
    \item \textbf{Does it converge towards the max flow value?}\\
    No, it does not converge to max flow value if the updates are poor and the capacities are irrational.
\end{itemize}

\begin{lemma}
Consider Ford-Fulkerson algorithm with integer capacities. The algorithm terminates in \(\val(\ff^*)\) augmentations i.e.\ $O(m \val(\ff^*))$ time.
\end{lemma}
\begin{proof}
Each iteration increases the flow by at least one unit as the
capacities in \(G_{\ff}\) are integral and each iteration can be
computed in  $O(m)$ time.
\end{proof}

\paragraph{Can we do better than this?}
Suppose we pick the maximum bottleneck capacity (minimum capacity
along path) augmenting path. This gives an algorithm that is better in
some regimes.

How to pick the maximum bottleneck capacity augmenting path? We are
going to perform a binary search on the capacities in \(G_{\ff}\), to
find a path with maximum bottleneck capacity.
Each time our binary search has picked a threshold capacity, we then
try to find an $s$-$t$ path flow in \(G_{\ff}\) using only  edges with
capacity above that threshold. If we find a path, the binary search
tries a higher capacity next. If we don't find a path, the binary search
tries a lower capacity next.

Using this approach, the time to find a single path is $O(m \log(n))$ where $m$ is number of edges in the graph.
This path must carry at least a \( \frac{1}{m}\) fraction of the total
amount of flow left in \(G_{\ff}\). For instance, if \(\hat{F}\) is
the amount of flow left in \(G_{\ff}\), then the path must carry
\(\frac{\hat{F}}{m}\) flow (from the path decomposition lemma, there
are at most $m$ paths, and the one carrying the most flow must carry
at least the average amount).

So if the flow is integral, the algorithms completes when
\begin{equation}\nonumber
    \left( 1 - \frac{1}{m}\right)^T \val(\ff^*) < 1
\end{equation}
where $T$ is the number of augmentations.

This means
\begin{equation}\nonumber
    T = m \log{F}
\end{equation}
\begin{align*}
\text{Total time} &= O(m \log{n} T) \\
                  &= O(m^2 \log{n} \log{F})\\
                  &\leq O(m^2 \log{n} \log{mU}) \text { as }  F \leq m U \text{ where U is the maximum capacity}\\
\end{align*}

\paragraph{Current state-of-the-art approaches for Max Flow.} For the
interested reader, we'll briefly mention the current state-of-the-art
for Maximum Flow algorithms, which is a  very active area of research.
% TODO(Tim): Would be nice to provide references here..
\begin{itemize}
   \item In undirected graphs, there are algorithms known which
     compute a $(1+\epsilon)$-approximate solution in time
     $\tilde{O}(m/\epsilon)$ \cite{S17}.
    \item Strongly polynomial time algorithm:
     \begin{itemize}
       \item has to work with real valued capacities, then the best time $O(mn)$ by Orlin.
    \end{itemize}
    \item Restricted to unit capacities:
    \begin{itemize}
        \item Old algorithm that runs in \(O(m^{1.5})\).
        \item The time for this was improved in 2014 to
          \(O(m^{\frac{10}{7}} )\) by Madry.
        \item Run time improved again in 2020 to \(\tilde{O}(m^{\frac{4}{3}})\) by Liu and Sidford.
    \end{itemize}
  \item General integer capacities: For a long time, the
    state-of-the-art was a result by Goldberg and Rao achieving a
    runtime of \(O(m \min(\sqrt{m},n^{2/3}) \log(n) \log(U) )\) where $U$ is the
    maximum capacity.
    In dense graphs, the best running time is now $\tilde{O}(m +
    n^{3/2})$ \cite{BLLSSSW21}, and in sparse graphs, a recently
    accounted result makes a small improvement over Goldberg-Rao,
    achieving a running time of  \(O(m^{3/2 - 1/328} \log(n) \log(U) )\) where $U$ is the
    maximum capacity \cite{GLP21}.
\end{itemize}
%\end{document}

% % Consider the flow for the graph shown in Figure (\ref{fig:ex3}). Once we have chosen the flow along the green paths, there is no other way to update the flow without violating the capacity constraints. The value that this flow routes is 2. However, this is not the optimal flow. As can be seen from Figure (\ref{fig:ex4}), the value of optimal flow is 3.\\

% \begin{figure}[h!]
%  \centering
%   \includegraphics[width=70mm,scale=0.1]{fig/fig3_lec10.PNG}
%   \caption{Flow on Diamond graph}
%   \label{fig:ex3}
% \end{figure}

% \begin{figure}[h!]
%  \centering
%   \includegraphics[width=70mm,scale=0.1]{fig/fig4_lec10.PNG}
%   \caption{Optimal flow on Diamond graph }
%   \label{fig:ex4}
% \end{figure}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "agao21_script"
%%% TeX-engine: luatex
%%% End:


\subsection{The hyperelliptic curve}


\documentclass[conf]{new-aiaa}
\usepackage[utf8]{inputenc}

\usepackage{float}
\usepackage{caption}
\usepackage{multicol}
\usepackage{verbatim}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{longtable,tabularx}
\usepackage{fontspec}
\usepackage{minted}

\setmainfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}

\renewcommand\refname{}

\usepackage{hyperref}
\hypersetup{
    colorlinks=false,
    linkcolor=black,
    filecolor=black,
    urlcolor=cyan,
}

\urlstyle{same}

\graphicspath{{./media/}}

\title{Exploring Invariant Manifolds and Halo Orbits}

\author{Joseph D. Carpinelli
    \footnote{
        Graduate Assistant,
        Department of Aerospace Engineering,
        University of Maryland}}
\affil{University of Maryland, College Park, Maryland, 20740}

\begin{document}

\maketitle
\nocite{*}

\begin{abstract}
Low-cost trajectory design has been a subject of research for decades.
Recently, researchers at California Polytechnic State University
studied low-cost trajectory design through the use of invariant
manifolds about Lagrange points in our solar system -- a
transfer from Earth to Jupiter, with an epoch similar to
that of Voyager 1, was investigated
\cite{rund2018interplanetary}.
This project aims to summarize,
and at times replicate, the core concepts behind this manifold-based
trajectory design.
A general, analytical
implementation for a Halo orbit solver is described, and implemented.
A general, numerical implementation for a Halo orbit solver is
described, and the preliminary code implementation is provided
as a reference.
This project was completed as part of the University of
Maryland's ENAE601 (Graduate Astrodynamics) course.
\end{abstract}

\begin{multicols}{2}

\tableofcontents

\section{Motivation}
Halo orbits are stable and periodic
within the Circular Restricted Three Body
Problem, and can therefore provide satellites the benefit
of reduced orbit maintenance fuel costs \cite{williams2017targeting}.
For these reasons, Halo orbits have have been
highly researched in recent
years, and will be utilized in upcoming NASA Artemis missions
\cite{nasa2020artemis}. Separately, low cost trajectories
are needed to design more efficient space exploration missions.
One such method for low cost trajectory design utilizes
invariant manifolds about Lagrange points in our solar system --
this method has been shown, in one example, to
decrease the required transfer $\Delta v$ from
Earth to Jupiter by approximately
$4 \frac{\text{km}}{\text{s}}$ when compared with the $\Delta v$ of a transfer
designed through patched conics \cite{rund2018interplanetary}.

Halo solutions are not easy to find -- they are often quite
difficult to solve for numerically.
This topic's complexity, coupled
with it's benefits to future low-cost interplanetary missions,
motivates additional research.

\section{Introduction}
Periodic, stable orbits are desirable for human spaceflight missions;
eclipse avoidance, positioning for spacecraft communication and space
science experiments, and data collection can be
improved through robust orbit stability and periodicity or
quasi-periodicity \cite{zimovan2020near}. In the Circular Restricted
Three-body Problem, several families of orbits are known which
orbit Lagrange points. One such
family of theoretically periodic orbits are
known as Halo orbits; they are periodic
within the Circular Restricted Three-body Problem, and
quasi-periodic in higher fidelity ephemeris models
\cite{zimovan2020near} \cite{rund2018interplanetary}. This
periodicity makes Halo orbits attractive candidates for target
orbits in future space missions. Unfortunately, finding
Halo orbits is not trivial. An analytical solution, as
described by Rund, can provide an initial estimate
for a particular Halo orbit \cite{rund2018interplanetary}. However,
numerically propagating this analytical solution will result in
a non-periodic orbit. A numerical algorithm has been developed to
iterate from this aperiodic initial orbit to a nearby, periodic orbit
\cite{rund2018interplanetary} \cite{howell1984three}.

Halo orbits' connection with Lagrange points results in an additional,
exploitable characteristic: invariant manifolds. Collections of
trajectories approach, and depart periodic orbits about Lagrange points.
Groups of trajectories which approach the Halo orbit are known
as stable invariant manifolds, while groups of trajectories which
depart the Halo orbit are known as unstable invariant manifolds
\cite{rund2018interplanetary}. These trajectories can be utilized by a
spacecraft for low-cost travel along the manifold.

This project implements the analytical Halo solution in Julia, a scientific
computing language. Several analytical Halo orbits are presented, and
rough estimates for their numerical solutions are also shown. Note that
neither of these estimates are truly periodic when used as inputs to
a Circular Restricted Three-body propagator. The summarized numerical
Halo orbit solver must be applied to these initial conditions to find
close-by, truly periodic orbits \cite{rund2018interplanetary}
\cite{howell1984three}.

\subsection{Outline}
First, the Circular Restricted Three-body Problem (CR3BP) and
Lagrange points will be reviewed, and the added context of
invariant manifolds will be presented. Next, Halo orbits will be
introduced, and analytical and numerical methods for solving
Halo orbits will be summarized \cite{rund2018interplanetary}
\cite{howell1984three}. Finally, invariant manifolds about
Halo orbits will be summarized, and low-cost manifold-based
interplanetary transfer designs will be summarized, as presented
by Rund et al \cite{rund2018interplanetary}.

\section{CR3BP Overview}
The Circular Restricted Three-body Problem is an orbital mechanics
simplification. Within this domain, a spacecraft of infinitesimal
mass is accelerated by two celestial bodies.

\subsection{Nondimensionalization}
Initial conditions are
often provided, and propagated, in non-dimensional form. This
nondimensionalization is completed by dividing all position
units by the distance between the two celestial bodies, and dividing
all time units by a user-specified duration \cite{vallado2001fundamentals}.
These nondimensionalizations are shown in equations $(1)$ and $(2)$ below,
where subscript $N,s$ denotes the \textit{nondimensional} state of the spacecraft with
respect to the center of mass of the two celestial bodies (COM), and subscript
$D,s$ denotes the \textit{dimensional} state of the spacecraft with respect to COM.
The single nondimensional mass parameter is labeled $\mu$, while $\mu_1$ and $\mu_2$ are the
dimensioned mass parameters for the first and second celestial bodies respectively.

\begin{equation}
    \overrightarrow{r}_{N,s} = \overrightarrow{r}_{D,s} \times \frac{1}{DU}
\end{equation}
\begin{equation}
    \overrightarrow{v}_{N,s} = \overrightarrow{v}_{D,s} \times \frac{DT}{DU}
\end{equation}
\begin{equation}
    \mu = \frac{\text{min}(\mu_1, \mu_2)}{\mu_1 + \mu_2}
\end{equation}

\subsection{Equations of Motion}
The nondimensional equations of motion for a spacecraft within the Circular Restricted
Three-body Problem are shown in equations $(4)$, $(5)$, and $(6)$ below
\cite{vallado2001fundamentals}.

\begin{equation}
\ddot{x} = 2\dot{y} + x - \frac{(1-\mu)(x + \mu)}{r_1^3} -
    \frac{\mu (x - 1 + \mu)}{r_2^3}
\end{equation}
\begin{equation}
\ddot{y} = -2\dot{x} + y - \frac{(1 - \mu)y}{r_1^3} - \frac{\mu y}{r_2^3}
\end{equation}
\begin{equation}
\ddot{z} = -\frac{(1-\mu)z}{r_1^3} - \frac{\mu z}{r_2^3}
\end{equation}

\begin{equation*}
    r_1 = \sqrt{(x+\mu)^2 + y^2 + z^2}
\end{equation*}
\begin{equation*}
    r_2 = \sqrt{(x+\mu-1)^2 + y^2 + z^2}
\end{equation*}

\section{Lagrange Points}
Lagrange points are equilibrium points within the Circular Restricted Three-body
Problem. There are $5$ equilibrium positions for each CR3BP system
\cite{vallado2001fundamentals}. Their general equations are shown below
\cite{rund2018interplanetary}. All L1, L2, and L3 Lagrange points are of the form
$\left( x^*, 0, 0 \right)$, and the L4 and L5 Lagrange points are of the form
$\left( x^*, y^*, 0 \right)$. The Lagrange points for the Earth-Moon CR3BP system
are shown in Figure 1, which replicates Figure 2.2 in Rund et al's \cite{rund2018interplanetary}.

\begin{equation}
    L_1: x - \frac{(1-\mu)}{(x+\mu)^2} + \frac{\mu}{(x + \mu - 1)^2} = 0
\end{equation}
\begin{equation}
    L_2: x - \frac{(1 - \mu)}{(x + \mu)^2} - \frac{\mu}{(x + \mu - 1)^2} = 0
\end{equation}
\begin{equation}
    L_3: x + \frac{(1-\mu)}{(x+\mu)^2} + \frac{\mu}{(x + \mu + 1)^2} = 0
\end{equation}
\begin{equation}
    L_4: x = \frac{1}{2} - \mu,\; y = \frac{\sqrt{3}}{2}
\end{equation}
\begin{equation}
    L_5: x = \frac{1}{2} - \mu,\; y = \frac{-\sqrt{3}}{2}
\end{equation}

\begin{figure}[H]
    \includegraphics[width=8cm]{fig1}
    \centering
    \captionof{figure}{Earth-Moon Lagrange Points}
\end{figure}

In general, equilibrium positions for nonlinear systems can be stable or unstable.
Lagrange points are no exception -- perturbing a spacecraft at a stable Lagrange point
will result in the spacecraft returning to that equilibrium position, while perturbing
a spacecraft at an unstable Lagrange point will result in the spacecraft continuing to
diverge from the equilibrium position. Collections of trajectories that
\textit{diverge from}, or \textit{converge to} Lagrange points are known as
\textit{manifolds} \cite{rund2018interplanetary}. Figure 2 shows one such example of a
divergant conditions at an Earth-Moon Lagrange point \cite{rund2018interplanetary}.

\begin{figure}[H]
    \includegraphics[width=8cm]{fig2}
    \centering
    \captionof{figure}{Divergant Trajectory near Sun-Earth L2}
\end{figure}

\subsection{Manifolds about Lagrange Points}

As previously mentioned, there exist collections of trajectories near Lagrange points
which approach, and depart each Lagrange point. These collections of trajectories are
known as manifolds \cite{rund2018interplanetary}. To compute one such trajectory, the
eigenvectors of the acobian of the state vector at the Lagrange point can be used to
find perturbations which would place the spacecraft within a manifold
\cite{rund2018interplanetary}. This use of eigenectors
is a familiar concept in linear analysis. For a linear system, the eigenvectors
of the zero-input dynamics specify how each state excites each dynamical mode.

The Jacobian of the
state vector at a Lagrange point will produce two oscillatory pairs of eigenvalues,
and one pair of real eigenvalues \cite{rund2018interplanetary}.
The span of the eigenvector corresponding to the
\textit{larger} real eigenvalue
represents the perturbations which would move a spacecraft from a Lagrange point to
an unstable manifold. The span of the eigenvector corresponding to the \textit{smaller} real
eigenalue represents the perturbations which would move a spacecraft from the Lagrange point
to a stable manifold \cite{rund2018interplanetary}.
Expressions for the Jacobian of the state vector, and
perturbing initial conditions for the stable and unstable manifolds near a Lagrange
point are shown below \cite{rund2018interplanetary}.

\begin{equation}
    J = \begin{bmatrix}
        0 & 0 & 0 & 1 & 0 & 0 \\
        0 & 0 & 0 & 0 & 1 & 0 \\
        0 & 0 & 0 & 0 & 0 & 1 \\
        \frac{\partial \ddot{x}}{\partial x} &
        \frac{\partial \ddot{x}}{\partial y} &
        \frac{\partial \ddot{x}}{\partial z} & 0 & 2 & 0 \\
        \frac{\partial \ddot{y}}{\partial x} &
        \frac{\partial \ddot{y}}{\partial y} &
        \frac{\partial \ddot{y}}{\partial z} & -2 & 0 & 0 \\
        \frac{\partial \ddot{z}}{\partial x} &
        \frac{\partial \ddot{z}}{\partial y} &
        \frac{\partial \ddot{z}}{\partial z} & 0 & 0 & 0 \\
    \end{bmatrix}
\end{equation}
\begin{equation}
    X_{\text{STABLE}} = X \pm \epsilon V_{\text{STABLE}}
\end{equation}
\begin{equation}
    X_{\text{UNSTABLE}} = X \pm \epsilon V_{\text{UNSTABLE}}
\end{equation}

\subsection{Orbits about Lagrange Points}
Several families of orbits are known which orbit Lagrange points -- these orbits
are known as Libration orbits \cite{rund2018interplanetary}. Common Libration
orbits include Lyapunov orbits, Halo orbits, and Lissajous orbits; each are briefly
summarized by Rund, and their descriptions are paraphrased here \cite{rund2018interplanetary}.
Lyapunov orbits are two-dimensional, Halo orbits are periodic and three-dimensional,
and Lissajous orbits are "quasi-periodic" and three-dimensional
\cite{rund2018interplanetary}.

\section{Halo Orbit Solutions}
Solving for a Halo orbit is difficult, as the computations are incredibly numerically
sensitive. An iterative, numerical algorithm has been developed to find a three-dimensional,
periodic orbit near a provided initial guess \cite{rund2018interplanetary} \cite{howell1984three}.
As Rund shows, an analytical solution can be used as the initial guess \cite{rund2018interplanetary}.

\subsection{Analytical Halo Solution}
Rund outlines an analytical solution for a Halo orbit, given a Z-amplitude and
non-dimensional mass parameter $\mu$, in great detail in section 3.2 \cite{rund2018interplanetary}.
The algorithm first sets parameter values corresponding to the selected Lagrange point;
only \textit{L1} and \textit{L2} analytical Halo orbits can be found with this algorithm.
Then, the algorithm plugs these parameters into a general equation of motion. The distance
from the CR3BP center of mass to the selected Lagrange point must be added to all $x$ values,
as the algorithm calculates all positions with respect to the selected Lagrange point
\cite{rund2018interplanetary}.
Rund's explanation is excellant and complete, so it will not be explained in more detail here.

I implemented this analytical Halo solution with Julia code, as shown in Appendix A.
The analytical solutions can also show the approximate shape of a Halo orbit. The analytical
algorithm requires an input for Z amplitude of the Halo orbit;
Rund uses a dimensional Z amplitude as an input,
and my implementation takes a non-dimensional Z amplitude \cite{rund2018interplanetary}.
Four examples of analytical solutions for families of Halo orbits are shown in Figure 3,
Figure 4, Figure 5, and Figure 6. They were computed using my implementation of
the analytical algorithm \cite{rund2018interplanetary}.

\end{multicols}

\begin{multicols}{2}
\begin{figure}[H]
    \includegraphics[width=8cm]{fig3}
    \centering
    \captionof{figure}{Northern Halo Orbits about Sun-Jupiter L1}
\end{figure}

\begin{figure}[H]
    \includegraphics[width=8cm]{fig4}
    \centering
    \captionof{figure}{Southern Halo Orbits about Sun-Jupiter L1}
\end{figure}

\begin{figure}[H]
    \includegraphics[width=8cm]{fig5}
    \centering
    \captionof{figure}{Northern Halo Orbits about Earth-Moon L2}
\end{figure}

\begin{figure}[H]
    \includegraphics[width=8cm]{fig6}
    \centering
    \captionof{figure}{Southern Halo Orbits about Earth-Moon L2}
\end{figure}

\end{multicols}

\newpage
\begin{multicols}{2}

\subsection{Numerical Halo Solution}

With the $t=0$ step of the analytical Halo solution in-hand, we can use the numerical
Halo solution to iteratively find a close estimate to a periodic Halo orbit
\cite{rund2018interplanetary}. This algorithm is presented in literature,
including by Howell and Rund \cite{howell1984three} \cite{rund2018interplanetary}.
This algorithm is also presented in Lecture 16 of the University of Maryland's
Graduate Astrodynamics course, ENAE$601$. For this reason, this section does not
go into the algorithm in complete detail. Instead, it provides a broad overview.
Rund provides a more complete and detailed explanation \cite{rund2018interplanetary}.

The numerical algorithm involves iterative propagation. Source code in Julia is available
in Appendix B. The analytical algorithm should have produced a state vector of the following form:

\begin{equation*}
\overrightarrow{x} = \begin{bmatrix} x_0^* & 0 & z_0^* & 0 & \dot{y}_0^* & 0 \end{bmatrix}^T
\end{equation*}

We will need to iterate on this initial guess to find a numerically periodic Halo orbit.
The form of our subsequent guesses will stay the same, but our $x_0$ and $\dot{y}_0$ values
will change. We could instead choose to change our $z_0$ and $\dot{y}_0$ values, but in
this description the former option will be used \cite{rund2018interplanetary}
\cite{howell1984three}.

To find a new initial state guess, we can use linear analysis techniques to find
state delta values which will move the solutions in the desired direction. We can
use the state transition matrix $\Phi$ for this purpose. In the context of the
Circular Restricted Three-body Problem, the state transition matrix
$\Phi$ relates the initial state vector to the state vector at each future time $t$
\cite{rund2018interplanetary}. The state transition matrix dynamics are described by
$\dot{\Phi} = F \Phi$, where $F$ is a $6\times 6$ block matrix with zeros at the top left,
the identity matrix at the top right, the Hessian (matrix of second partial derivatives)
of the potential energy of the spacecraft at the bottom left, and a constant matrix at
the bottom right \cite{rund2018interplanetary}.

\begin{equation}
    F = \begin{bmatrix} 0 & I_3 \\ U_{XX} & 2\Omega \end{bmatrix}
\end{equation}
\begin{equation}
    \Omega = \begin{bmatrix} 0 & 1 & 0 \\ -1 & 0 & 0 \\ 0 & 0 & 0  \end{bmatrix}
\end{equation}
\begin{equation}
    U_{XX} = \begin{bmatrix}
        \frac{\partial^2 U}{\partial x \partial x} &
        \frac{\partial^2 U}{\partial x \partial y} &
        \frac{\partial^2 U}{\partial x \partial z} & \\
        \frac{\partial^2 U}{\partial y \partial x} &
        \frac{\partial^2 U}{\partial y \partial y} &
        \frac{\partial^2 U}{\partial y \partial z} & \\
        \frac{\partial^2 U}{\partial z \partial x} &
        \frac{\partial^2 U}{\partial z \partial y} &
        \frac{\partial^2 U}{\partial z \partial z} & \\
    \end{bmatrix}
\end{equation}

With the equations above defined, we have all we need to complete the numerical algorithm.
The initial state transition matrix is set to the identity matrix, and each row is appended
to the state vector. As a result, the total state vector for this numerical integration
contains $42$ variables. We numerically integrate the initial state vector until
the solution crosses the $x-z$ axis (that is, when $y = 0$). We then add the following
state deltas to our initial condition, and repeat the procedure until the values $\dot{x}$ and
$\dot{z}$ are within some tolerance near zero \cite{rund2018interplanetary}. After this
procedure finishes, we should be left with an initial condition vector for a periodic Halo orbit.
This orbit may still diverge after several periods of numerical integration, depending on
how small our numerical tolerance was set \cite{rund2018interplanetary}.

\begin{equation}
    \delta \dot{x} = -\dot{x},\; \delta \dot{z} = -\dot{z}
\end{equation}
\begin{equation}
    \begin{bmatrix} \delta x_0 \\ \delta \dot{y}_0 \end{bmatrix} =
    \left( \begin{bmatrix} \Phi_{4 1} & \Phi_{4 5} \\ \Phi_{6 1} & \Phi_{6 5} \end{bmatrix} -
    \frac{1}{\dot{y}} \begin{bmatrix} \ddot{x} \\ \ddot{z} \end{bmatrix} \right)^{-1}
    \begin{bmatrix} \delta \dot{x} \\ \delta \dot{z} \end{bmatrix}
\end{equation}

\section{Invariant Manifolds}

\subsection{Overview}

Each point along the periodic Halo orbit is connected with an unstable and stable
invariant manifold -- this manifold is a collection of spacecraft trajectories
which converge to the point on the Halo orbit (stable manifold), or diverge from
the point on the Halo orbit (unstable manifold) \cite{rund2018interplanetary}.
We could calculate the eigenvectors of the Jacobian at each time step to give us the
range of perturbations which would shift the spacecraft onto the manifold, as previously
discussed. As Rund points out, this is very computationally expensive. Instead, we can
use the monodromy matrix -- the state transition matrix after one period $T$
\cite{rund2018interplanetary}.

\subsection{Algorithm}

To find the range of perturbations (in state space) which will shift the spacecraft
onto the unstable or stable manifolds at each time step along the Halo orbit,
we simply need to numerically integrate the full $42$ element state vector (with $\Phi$ set
to the identity matrix as an initial value) for one full period. We then note the
monodromy matrix is equivalent to the final state transition matrix. The eigenvector
associated with the maximum eigenvalue of the monodromy matrix, $V^U$, is used to find perturbations
to reach the unstable manifold, and the eigenvector associated with the minimum eigenvalue, $V^S$,
is used to find perturbations to reach the stable manifold
at each time step $i$, and some small scalar perturbation magnitude
$\epsilon$ \cite{rund2018interplanetary}. This is shown in equations $(20)$ through $(24)$.

\subsection{Usage}

Once the perturbing state delta has been found for each time step, and for both the
unstable and stable invariant manifolds, we can numerically integrate many trajectories
along each point in the Halo orbit to visualize the stable and unstable invariant manifolds.
This can be seen in Rund's Figure 3.6 \cite{rund2018interplanetary}. Trajectories within
unstable manifolds are propagated forward in time, and trajectories within stable manifolds are
propagated backwards in time. As Rund shows, these invariant manifolds about Halo
orbits can be utilized to form low-cost transfers from one Halo orbit to another
\cite{rund2018interplanetary}. Substantial $\Delta v$ savings are reported when
compared with traditional trajectory design methods; Rund's Masters Thesis provides
more detail \cite{rund2018interplanetary}.

\begin{equation}
    V_i^{S} = \Phi(t_0 + t_i, t_0) V^S
\end{equation}
\begin{equation}
    V_i^{U} = \Phi(t_0 + t_i, t_0) V^U
\end{equation}
\begin{equation}
    X_i^{S} = X_i \pm \epsilon \frac{V_i^S}{|V_i^S|}
\end{equation}
\begin{equation}
    X_i^{U} = X_i \pm \epsilon \frac{V_i^U}{|V_i^U|}
\end{equation}

\begin{equation}
M = \Phi(t_0 + T, t_0)
\end{equation}

\section{Conclusion}
Halo orbits have many desirable properties \cite{zimovan2020near} \cite{rund2018interplanetary}.
They are also incredibly difficult to find numerically with high precision. A method for
finding analytical solutions for Halo orbits was summarized and implemented
\cite{rund2018interplanetary}. A method for iterating on this analytical initial guess
to find a numerically periodic Halo orbit was summarized and implemented, though
the implemented code is not fully functional \cite{rund2018interplanetary} \cite{howell1984three}.

Common linear analysis techniques can be used to find manifolds about Halo orbits
\cite{rund2018interplanetary} \cite{howell1984three}. These
manifolds can then be utilized to form low-cost transfers between Halo orbits
\cite{rund2018interplanetary}. Future work includes fixing the numerical Halo solver code,
and exploring potential cost benefits for different interplanetary transfers.

\end{multicols}

\section{References}
\bibliography{sources}

\newpage
\section{Appendix}

Code to evaluate analytical and numerical Halo solutions are shown below. Only the
analytical function is working at the time of writing. If the unicode characters in
variables names are cutoff in your PDF version of this document, you can find
the same code on GitHub at \href{https://github.com/cadojo/UnitfulAstrodynamics.jl/blob/dev/src/ThreeBody/ThreeBodyCalculations.jl}{cadojo/UnitfulAstrodynamics.jl}.
The numerical function, "halo", currently has bugs that have not yet been resolved.
All orbits produced by this function seem to be shifted such that they are
no longer encircling the Lagrange point.

\inputminted[breaklines=true]{julia}{../../../../../../FOSS/UnitfulAstrodynamics/src/ThreeBody/Halo.jl}

\end{document}


\documentclass[]{article}
\usepackage{hyperref}

%opening
\title{F1 Fee Distribution Draft-00}
\author{Dev Ojha}

\begin{document}

\maketitle

\begin{abstract}
	In a proof of stake blockchain, validators need to split the rewards gained from transaction fees each block. Furthermore, these fees must be fairly distributed to each of a validator's constituent delegators. They accrue this reward throughout the entire time they are delegated, and they have a special operation to withdraw accrued rewards.

	The F1 fee distribution scheme works for any algorithm to split funds between validators each block, with minimal iteration, and the only approximations being due to finite decimal precision. Per block there is a single iteration over the validator set, which enables only rewarding validators who signed a given block. No iteration is required to delegate, and withdrawing only requires iterating over all of that validators slashes since delegation it began. State usage is minimal as well, one state update per validator per block, and one state record per delegator.
\end{abstract}

\section{F1 Fee Distribution}

In a proof of stake model, each validator has an associated stake, with delegators each contributing some amount to a validator's stake.
The validator is rewarded transaction fees every block for the service they are providing the network.
In the F1 distribution, each validator is permitted to take a commission from the fees they receive, and the remaining fees should be evenly distributed across the validator's delegators, such that every delegator the percentage of the validator's stake that came from the delegator is the proportion of that validator's remaining tx fees which they are getting.
Iterating over all delegators for every validator each block is too expensive for a blockchain environment.
Instead there is an explicit withdraw fees action which a delegator can take, which will give the delegator the same total amount of fees as though they were receiving it every block.

Suppose a delegator delegates $x$ stake to a validator at block $h$.
Let the amount of stake the validator has at block $i$ be $s_i$ and the amount of fees they receive at this height be $f_i$.
Then if a delegator contributing $x$ stake decides to withdraw at block $n$, the rewards they receive is
$$\sum_{i = h}^{n} \frac{x}{s_i}f_i = x \sum_{i = h}^{n} \frac{f_i}{s_i}$$

However $s_i$ will not change every block.
It only changes if the validator gets slashed, or if someone new has bonded or unbonded.
Handling slashes is relegated to \autoref{ssec:slashing}.
Define a period as the set of blocks between two changes in a given validator's total stake.
A new period begins every time that validator's total stake changes.
The above iteration will be converted to iteration over periods.
Let the total amount of stake for the validator in period $p$ be $n_p$.
Let $T_p$ be the total fees this validator accrued within this period.
Let $h$ be the start of period $p_{init}$, and height $n$ be the end of $p_{final}$.
It follows that
$$x \sum_{i = h}^{n} \frac{f_i}{s_i} = x \sum_{p = p_{init}}^{p_{final}} \frac{T_p}{n_p}$$

Let $p_0$ represent the period from when the validator first bonded until the first change to the validators stake.
The central idea to the F1 model is that at the end of the $k$th period, the following is stored at a state location indexable by $k$: $\sum_{i=0}^{k}\frac{T_i}{n_i}$.
When a delegator wants to delegate or withdraw their reward, they first create a new entry in state to end the current period. Let the index of the current period be $f$.
Then this entry is created using the previous entry as follows: $$\sum_{i=0}^{f}\frac{T_i}{n_i} = \sum_{i=0}^{f-1}\frac{T_i}{n_i} + \frac{T_f}{n_f} = entry_{f-1} + \frac{T_f}{n_f}$$
Where $T_f$ is the fees the validator has accrued in period $f$, and $n_f$ is the validators total amount of stake in period $f$.

The withdrawer's delegation object has the index $k$ for the period which they started accruing fees for.
Thus the reward they should receive when withdrawing is:

$$x\left(entry_f - entry_k\right)  = x\left(\left(\sum_{i=0}^{f}\frac{T_i}{n_i}\right) - \left(\sum_{i=0}^{k}\frac{T_i}{n_i}\right)\right) = x \sum_{i = k}^{f} \frac{T_i}{n_i}$$

The first summation is the state entry for $f$, and the second sum is the state entry at $k$.
It is clear from the equations that this payout mechanism maintains correctness, and required no iterations.

$T_f$ is a separate variable in state for the amount of fees this validator has accrued since the last update to its power.
This variable is incremented at every block by however much fees this validator received that block.
On the update to the validators power, this variable is used to create the entry in state at $f$.

This fee distribution proposal is agnostic to how all of the blocks fees are divied up between validators.
This creates many nice properties, for example only rewarding validators who signed that block.

\section{Additional add-ons}
\subsection{Commission Rates}
Commission rates are the idea that a validator can take a fixed $x\%$ cut of all of their received fees, before redistributing evenly to the constituent delegators.
This can easily be done as follows:

In block $h$ a validator receives $f_h$ fees.
Instead of incrementing that validators ``total accrued fees this period variable" by $f_h$, it is instead incremented by $(1 - commission\_rate) * f_p$.
Then $commission\_rate * f_p$ is deposited directly to the validator.
This scheme allow for updates to a validator's commission rate every block if desired.

\subsection{Slashing}
\label{ssec:slashing}
Slashing is distinct from withdrawals, since not only does it lower the validators total amount of stake, but it also lowers each of its delegator's stake by a fixed percentage.
Since noone is charged gas for slashes, a slash cannot iterate over all delegators.
Thus we can no longer just multiply by $x$ over the difference in stake.
The solution here is to instead store each period created by a slash in the validators state.
Then when withdrawing, you must iterate over all slashes between when you started and ended.
Suppose you delegated at period $0$, a y\% slash occured at period $2$, and your withdrawal is period $4$.
Then you receive funds from $0$ to $2$ as normal.
The equations for funds you receive for $2$ to $4$ now uses $(1 - y)x$ for your stake instead of just $x$ stake.
When there are multiple slashes, you just account for the accumulated slash factor.

In practice this will not really be an efficiency hit, as we can expect most validators to have no slashes.
Validators that get slashed a lot will naturally lose their delegators.
A malicious validator that gets itself slashed many times would increase the gas to withdraw linearly, but the economic loss of funds due to the slashes should far out-weigh the extra overhead the withdrawer must pay for due to the gas.

\subsection{Inflation}
Inflation is the idea that we want every staked coin to grow in value as time progresses. Each block, every staked token should each be rewarded $x$ staking tokens as inflation, where $x$ is calculated from function which takes state and the block information as input. This also allows for many seemless upgrade's to $x$'s algorithm. This can be added efficiently into the fee distribution model as follows:

Make each block have an inflation number, by which every staked token should produce $x$ additional staking tokens. In state there is a variable for the sum of all such inflation numbers. Then each period will store this total inflation sum in addition to $\sum_{i=0}^{end}\frac{T_i}{n_i}$. When withdrawing perform a subtraction on the inflation sums at the end and at the start to see how many new staking tokens to produce per staked token.

This works great in the model where the inflation rate should be dynamic each block, but apply the same to each validator. Inflation creation can trivially be epoched as long as inflation isn't required within the epoch, through changes to the $x$ function.

Note that this process is extremely efficient.

The above can be trivially amended if we want inflation to proceed differently for different validators each block. (e.g. depending on who was offline) It can also be made to be easily adapted in a live chain. It is unclear if either of these two are more desirable settings.

\subsection{Delegation updates}
Updating your delegation amount is equivalent to withdrawing earned rewards and a fully independent new delegation occuring in the same block.

\subsection{Jailing / being kicked out of the validator set}
This basically requires no change. In each block you only iterate over the currently bonded validators. So you simply don't update the "total accrued fees this period" variable for jailed / non-bonded validators. Withdrawing requires \textit{no} special casing here!

\section{State pruning}
You will notice that in the main scheme there was no note for pruning entries from state.
We can in fact prune quite effectively.
Suppose for the sake of exposition that there is at most one delegation / withdrawal to a particular validator in any given block.
Then each delegation is responsible for one addition to state.
Only the next period, and this delegator's withdrawal could depend on this entry. Thus once this delegator withdraws, this state entry can be pruned.
For the entry created by the delegator's withdrawal, that is only required by the creation of the next period. Thus once the next period is created, that withdrawal's period can be deleted.

This can be easily adapted to the case where there are multiple delegations / withdrawals per block. Keep a counter per state entry for how many delegations need to be cleared. (So 1 for each delegation in that block which created that period, 0 for each withdrawal) When creating a new period, check that the previous period (which had to be read anyway) doesn't have a count of 0. If it does have a count of 0, delete it. When withdrawing, decrement the period which created this delegation's counter by 1. If that counter is now 0, delete that period.

The slash entries for a validator can only be pruned when all of that validator's delegators have their bonding period starting after the slash. This seems ineffective to keep track of, thus it is not worth it. Each slash should instead remain in state until the validator unbonds and all delegators have their fees withdrawn.

\section{Implementers Considerations}

This is an extremely simple scheme with many nice benefits.
\begin{itemize}
	\item The overhead per block is a simple iteration over the bonded validator set, which occurs anyway. (Thus it can be implemented ``for-free" with an optimized code-base)
	\item Withdrawing earned fees only requires iterating over slashes since when you bonded. (Which is a negligible iteration)
	\item There are no approximations in any of the calculations. (modulo minor errata resulting from fixed precision decimals used in divisions)
	\item Supports arbitrary inflation models. (Thus could even vary upon block signers)
	\item Supports arbitrary fee distribution amongst the validator set. (Thus can account for things like only online validators get fees, which has important incentivization impacts)
	\item The above two can change on a live chain with no issues.
	\item Validator commission rates can be changed every block
	\item The simplicity of this scheme lends itself well to implementation
\end{itemize}

Thus this scheme has efficiency improvements, simplicity improvements, and expressiveness improvements over the currently proposed schemes. With a correct fee distribution amongst the validator set, this solves the existing problem where one could withhold their signature for risk-free gain.

\end{document}


\chapter{Information Theory}

\begin{multicols}{2}[\subsubsection*{Contents of this chapter}]
   \printcontents{}{1}{\setcounter{tocdepth}{2}}
\end{multicols}

\section{Entropy}

\section{Mutual Information}

\section{Kullback-Leibler Divergence}


\hypertarget{classnumpp_1_1differentiation_1_1symbolic_1_1variable}{}\section{numpp\+:\+:differentiation\+:\+:symbolic\+:\+:variable$<$ T, Number $>$ Class Template Reference}
\label{classnumpp_1_1differentiation_1_1symbolic_1_1variable}\index{numpp\+::differentiation\+::symbolic\+::variable$<$ T, Number $>$@{numpp\+::differentiation\+::symbolic\+::variable$<$ T, Number $>$}}

Class representing variable of any type.

{\ttfamily \#include $<$types.\+hpp$>$}

\subsection*{Public Types}
\begin{DoxyCompactItemize}
\item
\mbox{\Hypertarget{classnumpp_1_1differentiation_1_1symbolic_1_1variable_a982b8fd32deea706ecd44786a0a4b35a}\label{classnumpp_1_1differentiation_1_1symbolic_1_1variable_a982b8fd32deea706ecd44786a0a4b35a}}
{\footnotesize template$<$std\+::size\+\_\+t Active$>$ }\\using {\bfseries derivative} = std\+::conditional\+\_\+t$<$ Active==Number, \hyperlink{classnumpp_1_1differentiation_1_1symbolic_1_1constant}{constant}$<$ 1 $>$, \hyperlink{classnumpp_1_1differentiation_1_1symbolic_1_1constant}{constant}$<$ 0 $>$ $>$
\end{DoxyCompactItemize}
\subsection*{Static Public Member Functions}
\begin{DoxyCompactItemize}
\item
static constexpr auto \hyperlink{classnumpp_1_1differentiation_1_1symbolic_1_1variable_ad73b5d668d3bcf9802a0464ea3389ac6}{calculate} (auto \&\&values)
\begin{DoxyCompactList}\small\item\em Returns value of a certain variable. \end{DoxyCompactList}\end{DoxyCompactItemize}

\subsection{Detailed Description}
\subsubsection*{template$<$typename T, std\+::size\+\_\+t Number$>$\newline
class numpp\+::differentiation\+::symbolic\+::variable$<$ T, Number $>$}

Class representing variable of any type.

\begin{DoxyTemplParams}{Template Parameters}
{\em T} & type of value in variable (e.\+g. double) \\
\hline
{\em Number} & integer value differentiating variable from every other variable object\\
\hline
\end{DoxyTemplParams}

\begin{DoxyCode}
\textcolor{preprocessor}{#include"numpp/differentiation/symbolic.hpp"}
\end{DoxyCode}

\subsection{Member Function Documentation}
\mbox{\Hypertarget{classnumpp_1_1differentiation_1_1symbolic_1_1variable_ad73b5d668d3bcf9802a0464ea3389ac6}\label{classnumpp_1_1differentiation_1_1symbolic_1_1variable_ad73b5d668d3bcf9802a0464ea3389ac6}}
\index{numpp\+::differentiation\+::symbolic\+::variable@{numpp\+::differentiation\+::symbolic\+::variable}!calculate@{calculate}}
\index{calculate@{calculate}!numpp\+::differentiation\+::symbolic\+::variable@{numpp\+::differentiation\+::symbolic\+::variable}}
\subsubsection{\texorpdfstring{calculate()}{calculate()}}
{\footnotesize\ttfamily template$<$typename T , std\+::size\+\_\+t Number$>$ \\
static constexpr auto \hyperlink{classnumpp_1_1differentiation_1_1symbolic_1_1variable}{numpp\+::differentiation\+::symbolic\+::variable}$<$ T, Number $>$\+::calculate (\begin{DoxyParamCaption}\item[{auto \&\&}]{values }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [inline]}, {\ttfamily [static]}}

Returns value of a certain variable.

For example\+:
\begin{DoxyCode}
\textcolor{keyword}{auto} z = \hyperlink{classnumpp_1_1differentiation_1_1symbolic_1_1variable_ad73b5d668d3bcf9802a0464ea3389ac6}{nds::variable<double, 3>::calculate}(\textcolor{keywordtype}{double}[5]\{0,1,2,3,4\});
std::cout << z << std::endl;
\textcolor{comment}{//Will return 2, because it's the 3rd index specified in variable}
\end{DoxyCode}

\begin{DoxyReturn}{Returns}
int
\end{DoxyReturn}

The documentation for this class was generated from the following file\+:\begin{DoxyCompactItemize}
\item
differentiation/symbolic/types.\+hpp\end{DoxyCompactItemize}