\documentclass[11pt,fancychapters]{article}
\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{cite}
\usepackage{color}
\usepackage{xcolor}
\usepackage{empheq}
\usepackage{setspace}
\usepackage{hyperref}
\usepackage{minted}
\usepackage{acro}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{subcaption}
\usepackage{cancel}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{tcolorbox}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{parskip}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{pgfplots}
 \geometry{
 a4paper,
 total={170mm,257mm},
 left=20mm,
 top=20mm,
 }
\pgfplotsset{width=8cm,compat=1.9}
\newcommand{\dbar}{{d\mkern-7mu\mathchar'26\mkern-2mu}}
\newcommand{\boxedeq}[2]{\begin{empheq}[box={\fboxsep=6pt\fbox}]{align}\label{#1}#2\end{empheq}}
\def\*#1{\mathbf{#1}}
\def\ab{ab}
\usepackage{tikz}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
    decorations.pathreplacing,decorations.pathmorphing,shapes,%
    matrix,shapes.symbols}
\geometry{top=1.3in,bottom=1.3in}

\begin{document}
\centerline{\huge{2D Project --- 50.004 Introduction to Algorithms}}

\begin{table}[ht]
\centering
\footnotesize
 \begin{tabular}{c c c c c c}
V S Ragul Balaji&James Raphael Tiovalen&Anirudh Shrinivason&Jia Shuyi&Gerald Hoo&Shoham Chakraborty
 \end{tabular}
\end{table}

\section{Part A --- Deterministic Graph-Based Algorithm}
\subsection{Overview of Algorithm}

A solver for a 2-SAT problem can follow many methods. For a 2-SAT problem to be SATISFIABLE in Conjunctive Normal Form (CNF), every clause must be \texttt{true}. Since there are only 2 literals in each clause in a 2-SAT problem, within a single clause, we notice that we can form two implications, thus converting it into an Implicative Normal Form (INF). This sets one of the literals within the clause to be \texttt{false} and for the entire clause to be \texttt{true}, we need the other literal to be \texttt{true}. In other words, we are forbidding the four possible joint assignments of a pair of literals. This defines certain constraints between the variables, which can be propagated throughout the whole implication graph. Each pair of constraints can be considered to be an edge between the variables in an implication graph for the boolean satisfiability problem.\newline

To create the implication graph, we first implement a directed graph using an adjacency list. In the \texttt{Graph} class defined in the \texttt{dfs/kosaraju.py} file, we create the adjacency list by defining the graph's vertices, as well as a function to add edges. Since each variable produces two literals (the variable itself and its negation), we create $2n$ vertices, where $n$ is the number of variables. These vertices are added as the keys of the dictionary $G$ defined in the \texttt{Graph} class. The values for each key would be a list containing consecutive outgoing edges from the vertex defined in the key. This keeps the connection between each individual vertex to the other vertices.

\vspace{4mm}

\begin{figure}[h]\label{fig}
\centering
\includegraphics[width=.9\textwidth]{diagrams/example_cnf_solved.png}
\caption{Guided example of solving 2SAT by hand in polynomial time}
\label{fig:example_problem}
\end{figure}

\vspace{4mm}

In Figure~\ref{fig:example_problem}, we demonstrate this process using an example. This example is also defined in the \texttt{cnf/example.cnf} file. Steps 3 and 4 in Figure~\ref{fig:example_problem} are defined in lines 110-111 in the \texttt{kosaraju.py} file. Using the implication graph, we find the Strongly-Connected Components (SCCs) of the graph and group vertices that are along the same path using Kosaraju's Algorithm. We first create a stack and a depth-first search (DFS) is implemented to traverse through the graph. Adjacent vertices are pushed to the stack. The same procedure is executed on the inverse of the graph, where the direction of all of the edges is reversed. After that, while the stack is not empty, we pop each vertex from the stack. Using this algorithm, we can find all the SCCs of the graph.\newline

Next, we can check for contradiction in each of the SCC. If a variable and its corresponding negation is in the same SCC, the whole 2-SAT formula is deemed to be UNSATISFIABLE, as it is impossible to assign two literals to the same variable. This is because within a single SCC, you can traverse from any vertex to any other vertex. Otherwise, the formula is deemed as SATISFIABLE.\newline

If the formula is SATISFIABLE, we can output a possible solution for the formula. We can do this by grouping the SCCs together and connect them using a directed acyclic graph. Using the graph in Figure~\ref{fig:example_problem} as an example, going in topological order, assign $0$ to the first group of SCC (which is the left SCC in this specific case) and assign $1$ to the second group of SCC (the one highlighted by the red rectangle). We would then get the corresponding output of the variables by equating the literals in any SCC to the assigned value.

\subsection{Time Complexity Analysis}

In general, we know that 2-SAT is in P and is tractable due to the mechanism of forcing the other literal to be \texttt{true} in a single clause by assigning a literal within that same clause as \texttt{true}. When we pick an assignment, 3 cases could happen:

\begin{enumerate}
    \item We reach a contradiction. In this case, it means that there can only be a satisfying assignment if we use the other truth value for that specific variable. Thus, we can simplify the formula using this new assigned value for that variable and repeat the process.
    \item The ``forcing" of the value assignment for a specific variable does not affect other variables and clauses. In this case, adopt these truth values, eliminate the clauses that they satisfy and continue the process.
    \item We find a satisfying assignment.
\end{enumerate}

In Cases 1 and 2, we have spent at most $O(n^2)$ time and have reduced the length of the formula by $\geq 1$. Thus, in total, we have spent at most $O(n^3)$ time.\newline

In fact, our specific implementation method of using a DFS traversal on an implication graph using Kosaraju's Algorithm would take even less time:

\begin{enumerate}
    \item To create the implication graph, we set up the vertices and edges in $O(V + E)$ time, where $V$ is the number of vertices and $E$ is the number of edges in the graph.
    \item We implement DFS to traverse through the implication graph using Kosaraju's Algorithm in $O(V + E)$ time.
    \item We set up the inverse/transpose implication graph in $O(V + E)$ time.
    \item We implement the DFS again through the inverse implication graph in $O(V + E)$ time.
\end{enumerate}

Thus, this reduction of the 2-SAT problem to finding SCCs implemented in our deterministic algorithm would cause the algorithm to take only linear time.\newline

Meanwhile, $k$-SAT problems for $k \geq 3$ would be NP-complete (as shown by the Cook-Levin Theorem) and thus, the time taken would have non-polynomial asymptotics. This is because the size of the CNF formula is exponential in the size of the original boolean formula for $k \geq 3$.\newline

A possible improvement that could be made would be to implement Tarjan's Algorithm to conduct the search for the Strongly-Connected Components. While both Kosaraju's Algorithm and Tarjan's Algorithm would take $O(V+E)$ time, Tarjan's Algorithm has a lower constant factor to the runtime since it would need to go through the whole graph and execute DFS only once (instead of two times for Kosaraju's Algorithm, once for the normal graph and another instance of the DFS traversal for the inverse graph).

\newpage

\section{Part B --- Randomised Algorithm}
\subsection{Overview of Algorithm}
\begin{algorithm}[H]
	\caption*{\textbf{function} RANDOM\_WALK($\mathbb{F}, L$)}
	// $\mathbb{F}: \textit{a list of clauses}$\\
	// $L: \textit{a list of all variables used in } \mathbb{F}$
	\begin{algorithmic}[1]
	\State Store all variables as keys in a dictionary $\mathbb{D}$ with initial values \textbf{false}
	\For {$i \gets 1 \text{ to } 100\times(L.\textit{length})^2$}
	    \State Using $\mathbb{D}$, assign boolean values to $\mathbb{F}$
	    \State $\mathbb{C}\gets $ all invalid clauses in $\mathbb{F}$
	    \If {$\mathbb{C}.length \ne 0$}
	        \State $V\gets$ a random variable from a random clause in $\mathbb{C}$
	        \State $V\gets \neg V$
	        \State Update $\mathbb{D}$ with $V$
	   \Else
	        \State \textbf{return} SATISFIABLE
	   \EndIf
	\EndFor
	\State \textbf{return} UNSATISFIABLE
	\end{algorithmic}
\end{algorithm}

First, we arbitrarily assign Boolean value \texttt{false} to all $n$ variables. In each of the $100n^2$ steps, we randomly choose a variable from a randomly selected invalid clause (that is, the clause evaluates to \texttt{false}) and negate its assignment. We then check if the resultant formula is satisfied or not. If no solution is found after $100n^2$ steps, we return UNSATISFIABLE. If at any point during the $100n^2$ steps the formula is satisfied, we return SATISFIABLE.
\subsection{Time Complexity Analysis}
Let $X_i$ be the number of correct assignments at step $i$. Assuming worst-case initialization, all variables are assigned incorrectly, we have $X_0 = 0$. This forces $X_1 = 1$, since flipping any variable would give us a correct assignment.\newline

For $1\le i \le n-1$, the probability for $X_i$ transiting to $X_{i+1}$ is at least $\frac{1}{2}$ while that to $X_{i-1}$ is at most $\frac{1}{2}$. This can be easily seen from the table below:

\begin{table}[H]
\centering
\begin{tabular}{|l|l|l|l|}
\hline
\multicolumn{2}{|l|}{Wrong Clause $A+B$}            & $A$           & $B$          \\ \hline
\multicolumn{2}{|l|}{Actual Value}                  & T           & T          \\ \hline
\multicolumn{2}{|l|}{Both Wrong Assignment}         & F           & F          \\ \hline
\multicolumn{2}{|l|}{One Wrong Assignment}          & F           & T          \\ \hline
\multirow{2}{*}{$P(X_i \text{ to } X_{i+1})$} & Both Wrong & \multicolumn{2}{l|}{1}   \\ \cline{2-4}
                                       & One Wrong  & \multicolumn{2}{l|}{0.5} \\ \hline
\multirow{2}{*}{$P(X_i \text{ to } X_{i-1})$} & Both Wrong & \multicolumn{2}{l|}{0}   \\ \cline{2-4}
                                       & One Wrong  & \multicolumn{2}{l|}{0.5} \\ \hline
\end{tabular}
\end{table}
Let us suppose the worst case – that the probability $X_i$ goes up is $\frac{1}{2}$, and down is $\frac{1}{2}$. This process is similar to a random walk.\newline

Let $h_i$ be the expected number of steps to reach $n$ on our random walk when we start at step $i$. We have
\begin{equation}\label{eqn2.1}
    h_i = \frac{h_{i-1}}{2} + \frac{h_{i+1}}{2} + 1 \quad \Rightarrow \quad h_i - h_{i+1} = h_{i-1} -h_i + 2.
\end{equation}
Using the base case $h_0 = h_1 + 1$, the next 2 steps are
\begin{align}
    h_1 - h_2 &= h_0 - h_1 + 2 = h_1 + 1 - h_1 + 2 = 3,\label{h1h2} \\
    h_2 - h_3 &= h_1 - h_2 + 2 = 3 +2 = 5,\label{h2h3}
\end{align}
where Eqn.~(\ref{h1h2}) is substituted into Eqn.~(\ref{h2h3}). By careful observation, we formulate the following expression:
\begin{equation}\label{mi}
    h_i - h_{i+1} = 2i+1.
\end{equation}
As shown above, this expression holds for $i=1$. Assume the expression is \texttt{true} for $h_k - h_{k+1} = 2k+1$ for some positive integer $k$, we want to prove that the expression holds for $i = k + 1$.
\begin{align*}
    h_{k+1} - h_{k+2} &= h_k - h_{k+1} + 2 \qquad \text{By Eqn.~(\ref{eqn2.1})}\\
    &= 2k+1+2\\
    &= 2(k+1) + 1.
\end{align*}
Thus, by mathematical induction, Eqn.~(\ref{mi}) is true for all $k \in \mathbb{Z}^+$. Using Eqn.~(\ref{mi}), we sum all the steps from $i = 0$ to $i = n$:
\begin{align*}
    h_0 &= h_n + \sum^{n-1}_{i=0}\left(h_i - h_{i+1}\right)\\
    &= \sum^{n-1}_{i=0}(2i+1)\\
    &= n+2\left(\frac{n^2-n}{2}\right)\\
    &= n^2,
\end{align*}
where $h_n = 0$.\newline

Therefore, the average time complexity of the randomised algorithm is $O(n^2)$. In other words, we will find a solution in $n^2$ steps \textbf{on average}. If we decide to run the algorithm for $2n^2$ steps, the probability of not finding a solution is at most $\frac{1}{2}$. Thus, if we run the algorithm for $100n^2$ steps (as it is in the pseudo-code), the probability of not finding a solution is $\left(\frac{1}{2}\right)^{50} = 2^{-50}$.

\newpage
\section{Performance Comparison}

\begin{figure}[h]
\centering
\includegraphics[width=.95\textwidth]{diagrams/test0.png}
\caption{Benchmarking script used to compare the speed of the two implementation in Part A and Part B. Kosaraju's Algorithm runs faster than the randomised algorithm as predicted by the algorithmic analysis. }
\end{figure}

\vspace{4mm}

Even though the probability of not finding a solution is very small ($2^{-50}$ for $100n^2$ steps), the randomised algorithm is not a practical substitute for the deterministic one since it takes longer time than the deterministic algorithm. There is also some chance that a solution is not found and hence an incorrect conclusion/statement of the problem's satisfiability could be made. The randomised algorithm is also dependent on the size of the variables.\newline

However, we should not dismiss the usefulness of the idea of randomised local search entirely. We should be aware that the strategy of using randomised local search is useful to improve over naive brute-force search for NP-complete $k$-SAT problems, such as the $3$-SAT problem. In fact, for a 3-SAT problem, the naive brute-force method would take $O(2^n)$ time, while a version of the randomised local search with a clever twist (such as Sch\"{o}ning's stochastic local search algorithm) would take $O\left(\frac{4}{3}^n\right)$ time, which is significantly better than $O(2^n)$.

\end{document}


\documentclass{article}
\usepackage[left=6.3em,right=7em,top=7em]{geometry}
\usepackage{amsmath}
\usepackage{bcprules}
\usepackage{amssymb}
\usepackage{stmaryrd}
\usepackage{multicol}
\begin{document}
\section{source language $\mathcal{T}$}
\begin{flalign*}
    \mathtt{n} ::=&\quad n \in \mathcal{N} \\
    \mathtt{e} ::=&\qquad \mathtt{n} \mid \mathtt{true} \mid \mathtt{false} \mid \mathtt{()} \mid \mathtt{x} \\
         &\quad \mid \mathtt{e+e} \mid \mathtt{e-e} \mid \mathtt{e*e} \mid \mathtt{e/e}\\
         &\quad \mid \mathtt{e=e} \mid \mathtt{e<e} \mid \mathtt{e>=e} \mid \mathtt{not\ e}\\
         &\quad \mid \mathtt{if}\ \mathtt{e}\ \mathtt{then}\ \mathtt{e}\ \mathtt{else}\ \mathtt{e}\\
         &\quad \mid \mathtt{let\ x=e\ in\ e} \mid \mathtt{let\ rec\ x\ x=e\ in\ e} \mid \mathtt{fun\ x \rightarrow e} \mid \mathtt{e\ e}
\end{flalign*}

\section{Virtual Machine specificatoin}
\subsection{closure representation}
\[Q = \left\langle I, C, F, \mathit{Um} \right\rangle\]
$I$ is instruction sequense, $C$ is constant table, $F$ is closure table
and $\mathit{Um}$ is just an information, to create upvalues, composed of index and \textit{meta} index, which of the former represents current register value and the latter does the upvalue index.

\subsection{machine state}
\[S = \left\langle Q, \mathtt{pc}, U, R \right\rangle\]
\texttt{pc} is program counter which points nth instruction of $I$, $U$ is upvalues table and $R$ is register list.

\subsection{values}
\begin{flalign*}
    \mathtt{v} ::=\quad \mathtt{null} \mid \mathtt{()} \mid \mathtt{i} \in \mathcal{N} \mid \mathtt{true} \mid \mathtt{false} \mid \mathtt{clos}\left(Q, U\right)
\end{flalign*}

\subsection{Instructions}
\begin{minipage}{\textwidth}
    \begin{multicols}{2}
        \begin{itemize}
            \item \texttt{Load(a, kx)}

                set \texttt{C[kx]} to \texttt{R[a]}
            \item \texttt{SetBool(a, x, p)}

                set boolean \texttt{x > 0} to \texttt{R[a]};
                if \texttt{p = 1} then \texttt{pc++}
            \item \texttt{Unit(a)}

                set \texttt{()} to \texttt{R[a]}
            \item \texttt{Clos(a, cx, p)}

                create $U'$ = $U\ @\ \left\{u \mid i_u \in \mathit{Um} \wedge u = R[i_u]\right\}$
                and set $\left\langle \mathtt{F[cx]}, 0, U', R'\right\rangle$ with environment to \texttt{R[a]}
                if \texttt{p=1} then the closure is recursive
            \item \texttt{Upval(a, ux)}

                set \texttt{U[rx]} to \texttt{R[a]}
            \item \texttt{Add(a, b, c)}

                set \texttt{R[b] + R[c]} to \texttt{R[a]}
            \item \texttt{Sub(a, b, c)}

                set \texttt{R[b] - R[c]} to \texttt{R[a]}
            \item \texttt{Mul(a, b, c)}

                set \texttt{R[b] * R[c]} to \texttt{R[a]}
            \item \texttt{Div(a, b, c)}

                set \texttt{R[b] / R[c]} to \texttt{R[a]}
            \item \texttt{Eq(a, b)}

                if \texttt{R[a] == R[b]} then \texttt{pc++}
            \item \texttt{Lt(a, b)}

                if \texttt{R[a] < R[b]} then \texttt{pc++}
            \item \texttt{Ge(a, b)}

                if \texttt{R[a] >= R[b]} then \texttt{pc++}
        \end{itemize}
    \end{multicols}
\end{minipage}
\begin{minipage}{\textwidth}
    \begin{multicols}{2}
        \begin{itemize}
            \item \texttt{Test(a, p)}

                if \texttt{p > 0 \&\& R[a] || p <= 0 \&\& !R[a]} then \texttt{pc++}

            \item \texttt{Jump(x)}

                \texttt{pc += x}
            \item \texttt{Move(a, b)}

                set \texttt{R[b]} to \texttt{R[a]}
            \item \texttt{Call(a, b)}

                call closure \texttt{R[a]} with argument \texttt{R[b]} and set return value to \texttt{R[a]}
            \item \texttt{Return(a)}

                exit closure evaluation and return \texttt{R[a]}
            \item \texttt{TailCall(a, b)}

                call closure \texttt{R[a]} with argument \texttt{R[b]} and exit closure evaluation and return \texttt{R[a]}
        \end{itemize}
    \end{multicols}
\end{minipage}

\section{compilation: translate source language program to VM initial state (input bytecode)}
Compilation is represented as equation: \[\llceil\mathcal{T}, \Sigma, Q\rrceil = Q', \Sigma' .\]
$\Sigma$ is an environment from source language variable to register index.
$Q_{\mathit{init}}$ generates initial compilation state: \[Q_{\mathit{init}}\left(e\right) = \llceil e, [], \left\langle [], [], [], []\right\rangle \rrceil .\]

\subsection{auxiliary funcion}
We define two auxiliary functions; \textit{mtch} is to get last used register and rest instructions by pattern match,

\begin{center}
    \begin{minipage}{.5\textwidth}
        \begin{flalign*}
            &\mathit{mtch}\left(I; \mathtt{Return\left(a\right)}\right) &&=& \left(\mathtt{a}, I\right)\\
            &\mathit{mtch}\left(I; \mathtt{TailCall\left(a, b\right)}\right) &&=& \left(\mathtt{a}, I; \mathtt{Call\left(a, b\right)}\right)\\
            &\mathit{mtch}\left(\textunderscore\right) &&=& \mathrm{undefined}
        \end{flalign*}
    \end{minipage}
\end{center}
\textit{FV} is to get free variables.
\begin{center}
    \begin{minipage}{.5\textwidth}
        \begin{flalign*}
            &FV\left(\mathtt{n}\right) \mid FV\left(\mathtt{true}\right) \mid FV\left(\mathtt{false}\right) \mid FV\left(\mathtt{()}\right) &&=& \left\{\right\}\\
            &FV(\mathtt{x}) &&=& \left\{\mathtt{x}\right\}\\
            &FV(\mathtt{not\ e}) &&=& FV(\mathtt{e})\\
            &FV(\mathtt{e1 ? e2}) \mid FV\left(\mathtt{e1\ e2}\right) &&=& FV(\mathtt{e1}) \cup FV(\mathtt{e2})\\
            &FV(\mathtt{if\ e1\ then\ e2\ else\ e3}) &&=& FV(\mathtt{e1}) \cup FV(\mathtt{e2}) \cup FV(\mathtt{e3})\\
            &FV\left(\mathtt{fun\ x\rightarrow e}\right) &&=& FV\left(\mathtt{e}\right) \textbackslash \left\{\mathtt{x}\right\}\\
            &FV\left(\mathtt{let\ x\ =\ e1\ in\ e2}\right) &&=& FV(\mathtt{e1}) \cup \left(FV\left(\mathtt{e2}\right) \textbackslash \left\{\mathtt{x}\right\}\right)\\
            &FV\left(\mathtt{let\ rec\ f\ x=\ e1\ in\ e2}\right) &&=& \left(FV\left(\mathtt{e1}\right) \textbackslash \left\{\mathtt{x}\right\} \cup FV\left(\mathtt{e2}\right)\right) \textbackslash \left\{\mathtt{f}\right\}
        \end{flalign*}
    \end{minipage}
\end{center}

\subsection{compilation rules}

\infrule[Int]{%
    \mathtt{a}\ \mathrm{and}\ \mathtt{kx}\ \mathrm{are\ fresh}
}{
    \llceil\mathtt{n}, \Sigma, \left\langle I, C, F, \mathit{Um}\right\rangle \rrceil =%
    \left\langle
        I; \mathtt{Load(a, kx)}; \mathtt{Return(a)}, C; \mathtt{kx} = \mathtt{n}, F, \mathit{Um}
    \right\rangle, \Sigma'
}

\infax[Unit]{
    \llceil\mathtt{()}, \Sigma, \left\langle I, C, F, \mathit{Um}\right\rangle \rrceil =
    \left\langle
        I; \mathtt{Unit(a)}; \mathtt{Return(a)}, C, F, \mathit{Um}
    \right\rangle, \Sigma'
}

\infax[True]{
    \llceil\mathtt{true}, \Sigma, \left\langle I, C, F, \mathit{Um}\right\rangle \rrceil =
    \left\langle
        I; \mathtt{SetBool(a, 1)}; \mathtt{Return(a)}, C, F, \mathit{Um}
    \right\rangle, \Sigma'
}

\infax[False]{
    \llceil\mathtt{false}, \Sigma, \left\langle I, C, F, \mathit{Um}\right\rangle \rrceil =
    \left\langle
        I; \mathtt{SetBool(a, 0)}; \mathtt{Return(a)}, C, F, \mathit{Um}
    \right\rangle, \Sigma'
}

\infrule[Add]{
        \mathtt{a}\ \mathrm{is\ fresh}
    \andalso Q = \left\langle I,C, F, \mathit{Um} \right\rangle
    \andalso\\
        \llceil \mathtt{e1}, \Sigma, Q\rrceil = \langle I_\mathtt{e1}, C', F', \mathit{Um}'\rangle, \Sigma'
    \andalso \mathit{mtch}\left(I_\mathtt{e1}\right) = \left(\mathtt{r_\mathtt{e1}}, I'\right)
    \andalso\\
        \llceil \mathtt{e2}, \Sigma', \langle I', C', F', \mathit{Um}'\rangle \rrceil = \langle I_\mathtt{e2}, C'', F'', \mathit{Um}''\rangle, \Sigma''
    \andalso \mathit{mtch}\left(I_\mathtt{e2}\right) = \left(\mathtt{r_{e2}}, I''\right)
}{
    \llceil \mathtt{e1 + e2}, \Sigma, Q\rrceil = \langle I''; \mathtt{Add(a, r_{e1}, r_{e2})}; \mathtt{Return(a)}, C'', F'', \mathit{Um}''\rangle, \Sigma''
}

\infrule[Sub]{
        \mathtt{a}\ \mathrm{is\ fresh}
    \andalso Q = \left\langle I,C, F, \mathit{Um} \right\rangle
    \andalso\\
        \llceil \mathtt{e1}, \Sigma, Q\rrceil = \langle I_\mathtt{e1}, C', F', \mathit{Um}'\rangle, \Sigma'
    \andalso \mathit{mtch}\left(I_\mathtt{e1}\right) = \left(\mathtt{r_{e1}}, I'\right)
    \andalso\\
        \llceil \mathtt{e2}, \Sigma', \langle I', C', F', \mathit{Um}'\rangle \rrceil = \langle I_\mathtt{e2}, C'', F'', \mathit{Um}''\rangle, \Sigma''
    \andalso \mathit{mtch}\left(I_\mathtt{e2}\right) = \left(\mathtt{r_{e2}}, I''\right)
}{
    \llceil \mathtt{e1 - e2}, \Sigma, Q\rrceil = \langle I''; \mathtt{Sub(a, r_{e1}, r_{e2})}; \mathtt{Return(a)}, C'', F'', \mathit{Um}''\rangle, \Sigma''
}

\infrule[Mul]{
        \mathtt{a}\ \mathrm{is\ fresh}
    \andalso Q = \left\langle I,C, F, \mathit{Um} \right\rangle
    \andalso\\
        \llceil \mathtt{e1}, \Sigma, Q\rrceil = \langle I_\mathtt{e1}, C', F', \mathit{Um}'\rangle, \Sigma'
    \andalso \mathit{mtch}\left(I_\mathtt{e1}\right) = \left(\mathtt{r_{e1}}, I'\right)
    \andalso\\
        \llceil \mathtt{e2}, \Sigma', \langle I', C', F', \mathit{Um}'\rangle \rrceil = \langle I_\mathtt{e2}, C'', F'', \mathit{Um}''\rangle, \Sigma''
    \andalso \mathit{mtch}\left(I_\mathtt{e2}\right) = \left(\mathtt{r_{e2}}, I''\right)
}{
    \llceil \mathtt{e1 * e2}, \Sigma, Q\rrceil = \langle I''; \mathtt{Mul(a, r_{e1}, r_{e2})}; \mathtt{Return(a)}, C'', F'', \mathit{Um}''\rangle, \Sigma''
}

\infrule[Div]{
        \mathtt{a}\ \mathrm{is\ fresh}
    \andalso Q = \left\langle I,C, F, \mathit{Um} \right\rangle
    \andalso\\
        \llceil \mathtt{e1}, \Sigma, Q\rrceil = \langle I_\mathtt{e1}, C', F', \mathit{Um}'\rangle, \Sigma'
    \andalso \mathit{mtch}\left(I_\mathtt{e1}\right) = \left(\mathtt{r_{e1}}, I'\right)
    \andalso\\
        \llceil \mathtt{e2}, \Sigma', \langle I', C', F', \mathit{Um}'\rangle \rrceil = \langle I_\mathtt{e2}, C'', F'', \mathit{Um}''\rangle, \Sigma''
    \andalso \mathit{mtch}\left(I_\mathtt{e2}\right) = \left(\mathtt{r_{e2}}, I''\right)
}{
    \llceil \mathtt{e1 / e2}, \Sigma, Q\rrceil = \langle I''; \mathtt{Div(a, r_{e1}, r_{e2})}; \mathtt{Return(a)}, C'', F'', \mathit{Um}''\rangle, \Sigma''
}

\infrule[Eq]{
        \mathtt{a}\ \mathrm{is\ fresh}
    \andalso Q = \left\langle I,C, F, \mathit{Um} \right\rangle
    \andalso\\
        \llceil \mathtt{e1}, \Sigma, Q\rrceil = \langle I_\mathtt{e1}, C', F', \mathit{Um}'\rangle, \Sigma'
    \andalso \mathit{mtch}\left(I_\mathtt{e1}\right) = \left(\mathtt{r_{e1}}, I'\right)
    \andalso\\
        \llceil \mathtt{e2}, \Sigma', \langle I', C', F', \mathit{Um}'\rangle \rrceil = \langle I_\mathtt{e2}, C'', F'', \mathit{Um}''\rangle, \Sigma''
    \andalso \mathit{mtch}\left(I_\mathtt{e2}\right) = \left(\mathtt{r_{e2}}, I''\right)
}{
    \llceil \mathtt{e1 = e2}, \Sigma, Q\rrceil =
    \langle I''; \mathtt{Eq(r_{e1}, r_{e2})}; \mathtt{SetBool(a, 1)}; \mathtt{SetBool(a, 0)}; \mathtt{Return(a)}, C'', F'', \mathit{Um}''\rangle, \Sigma''
}

\infrule[Lt]{
        \mathtt{a}\ \mathrm{is\ fresh}
    \andalso Q = \left\langle I,C, F, \mathit{Um} \right\rangle
    \andalso\\
        \llceil \mathtt{e1}, \Sigma, Q\rrceil = \langle I_\mathtt{e1}, C', F', \mathit{Um}'\rangle, \Sigma'
    \andalso \mathit{mtch}\left(I_\mathtt{e1}\right) = \left(\mathtt{r_{e1}}, I'\right)
    \andalso\\
        \llceil \mathtt{e2}, \Sigma', \langle I', C', F', \mathit{Um}'\rangle \rrceil = \langle I_\mathtt{e2}, C'', F'', \mathit{Um}''\rangle, \Sigma''
    \andalso \mathit{mtch}\left(I_\mathtt{e2}\right) = \left(\mathtt{r_{e2}}, I''\right)
}{
    \llceil \mathtt{e1 < e2}, \Sigma, Q\rrceil =
    \langle I''; \mathtt{Lt(r_{e1}, r_{e2})}; \mathtt{SetBool(a, 1)}; \mathtt{SetBool(a, 0)}; \mathtt{Return(a)}, C'', F'', \mathit{Um}''\rangle, \Sigma''
}

\infrule[Ge]{
        \mathtt{a}\ \mathrm{is\ fresh}
    \andalso Q = \left\langle I,C, F, \mathit{Um} \right\rangle
    \andalso\\
        \llceil \mathtt{e1}, \Sigma, Q\rrceil = \langle I_\mathtt{e1}, C', F', \mathit{Um}'\rangle, \Sigma'
    \andalso \mathit{mtch}\left(I_\mathtt{e1}\right) = \left(\mathtt{r_{e1}}, I'\right)
    \andalso\\
        \llceil \mathtt{e2}, \Sigma', \langle I', C', F', \mathit{Um}'\rangle \rrceil = \langle I_\mathtt{e2}, C'', F'', \mathit{Um}''\rangle, \Sigma''
    \andalso \mathit{mtch}\left(I_\mathtt{e2}\right) = \left(\mathtt{r_{e2}}, I''\right)
}{
    \llceil \mathtt{e1 >= e2}, \Sigma, Q\rrceil =
    \langle I''; \mathtt{Ge(r_{e1}, r_{e2})}; \mathtt{SetBool(a, 1)}; \mathtt{SetBool(a, 0)}; \mathtt{Return(a)}, C'', F'', \mathit{Um}''\rangle, \Sigma''
}

\infrule[Not]{
        \mathtt{a}\ \mathrm{is\ fresh}
    \andalso Q = \left\langle I,C, F, \mathit{Um} \right\rangle
    \andalso\\
        \llceil \mathtt{e}, \Sigma, Q\rrceil = \langle I_\mathtt{e}, C', F', \mathit{Um}'\rangle, \Sigma'
    \andalso \mathit{mtch}\left(I_\mathtt{e}\right) = \left(\mathtt{r_e}, I'\right)
}{
    \llceil \mathtt{not\ e}, \Sigma, Q\rrceil = \langle I'; \mathtt{Test(r_{e}, 0)}; \mathtt{SetBool(a, 1)}; \mathtt{SetBool(a, 0)}; \mathtt{Return(a)}, C', F', \mathit{Um}'\rangle, \Sigma'
}

\infrule[Fun]{
        \mathtt{a}\ \mathrm{and}\ \mathtt{cx}\ \mathrm{are\ fresh}
    \andalso Q = \left\langle I, C, F, \mathit{Um}\right\rangle
    \andalso fv = FV\left(\mathtt{fun\ x\rightarrow e}\right)
    \andalso \Sigma_0 = \left\{\left(\mathtt{x} = \mathtt{i_r}\right) \mid \left(\mathtt{x} = \mathtt{i_r}\right) \in \Sigma \wedge \mathtt{x} \in fv \right\}
    \andalso\\
        \Sigma' = \left\{ \left(\mathtt{x} = \mathtt{-\left(i + 1\right)}\right) \mid \left(\mathtt{x} = \mathtt{i_r}\right)\ \mathrm{as}\ \mathit{xi} \in \Sigma_0 \wedge \mathit{xi}\ \mathrm{is}\ i\mathrm{th\ element\ of}\ \Sigma_0 \right\}
    \andalso \mathit{Um}' = \left\{ \mathtt{i} \mid \left(\textunderscore = \mathtt{i}\right) \in \Sigma' \right\}
    \andalso\\
        Q_{\mathit{init}}\left(\mathtt{e}\right) = \llceil \mathtt{e}, \Sigma_\mathtt{e}, \left\langle I_\mathtt{e}, C_\mathtt{e}, F_\mathtt{e}, \textunderscore\right\rangle \rrceil
    \andalso \Sigma_\mathtt{e}' = \Sigma_\mathtt{e}; \mathtt{x} = 0
    \andalso \llceil \mathtt{e}, \Sigma_\mathtt{e}', \langle I_\mathtt{e}, C_\mathtt{e}, F_\mathtt{e}, \mathit{Um}' \rangle \rrceil%
        = \left\langle I_\mathtt{e}', C_\mathtt{e}', F_\mathtt{e}', \textunderscore\right\rangle, \Sigma_\mathtt{e}''
    \andalso\\
        \mathit{Clos} = \left\langle I_\mathtt{e}', C_\mathtt{e}', F_\mathtt{e}', \mathit{Um} @ \mathit{Um}' \right\rangle
}{
    \llceil \mathtt{fun\ x \rightarrow e}, \Sigma, Q\rrceil = \langle I; \mathtt{Clos(a, cx, 0)}; \mathtt{Return(a)}, C, F; \mathtt{cx} = \mathit{Clos}, \mathit{Um} \rangle, \Sigma
}

\infrule[Call]{
        Q = \left\langle I,C, F, \mathit{Um} \right\rangle
    \andalso \llceil \mathtt{e1}, \Sigma, Q\rrceil = \langle I_\mathtt{e1}, C', F', \mathit{Um}'\rangle, \Sigma'
    \andalso \mathit{mtch}\left(I_\mathtt{e1}\right) = \left(\mathtt{r_{e1}}, I'\right)
    \andalso\\
        \llceil \mathtt{e2}, \Sigma', \langle I', C', F', \mathit{Um}'\rangle \rrceil = \langle I_\mathtt{e2}, C'', F'', \mathit{Um}''\rangle, \Sigma''
    \andalso \mathit{mtch}\left(I_\mathtt{e2}\right) = \left(\mathtt{r_{e2}}, I''\right)
}{
    \llceil \mathtt{e1\ e2}, \Sigma, Q\rrceil = \langle I''; \mathtt{TailCall(r_{e1}, r_{e2})}, C'', F'', \mathit{Um}''\rangle, \Sigma''
}

\infrule[VarLocal]{
    \Sigma[\mathtt{x}] = \mathtt{i} \wedge \mathtt{i}\geq \mathtt{0}
}{
    \llceil \mathtt{x}, \Sigma, \left\langle I, C, F, \mathit{Um}\right\rangle \rrceil =
    \left\langle
        I; \mathtt{Move(a, i)}; \mathtt{Return(a)}, C, F, \mathit{Um}
    \right\rangle, \Sigma'
}

\infrule[VarUpval]{
    \Sigma[\mathtt{x}] = \mathtt{i} \wedge \mathtt{i < 0}
}{
    \llceil \mathtt{x}, \Sigma, \left\langle I, C, F, \mathit{Um}\right\rangle \rrceil =
    \left\langle
        I; \mathtt{Upval(a, -i - 1)}; \mathtt{Return(a)}, C, F, \mathit{Um}
    \right\rangle, \Sigma'
}

\infrule[Let]{
        \mathtt{a}\ \mathrm{and}\ \mathtt{b}\ \mathrm{are\ fresh}
    \andalso Q = \left\langle I, C, F, \mathit{Um} \right\rangle
    \andalso\\
        \llceil \mathtt{e_1}, \Sigma, Q\rrceil = \left\langle I_\mathtt{e1}, C', F', \mathit{Um}'\right\rangle, \Sigma'
    \andalso \mathit{mtch}\left(I_\mathtt{e1}\right) = \left(\mathtt{r_{e1}}, I'\right)
    \andalso\\
        \llceil \mathtt{e_2}, \Sigma'; \mathtt{x}=\mathtt{a}, \left\langle I'; \mathtt{Move(b, a)}, C', F', \mathit{Um}'\right\rangle\rrceil = Q', \Sigma''
}{
    \llceil \mathtt{let\ x=e_1\ in\ e_2}, \Sigma, Q\rrceil = Q', \Sigma''
}

\infrule[LetRecFun]{
        \mathtt{a}\ \mathrm{and}\ \mathtt{cx}\ \mathrm{are\ fresh}
    \andalso Q = \left\langle I, C, F, \mathit{Um} \right\rangle
    \andalso\\
        fv = \left(FV\left(\mathtt{e_1}\right) \textbackslash \left\{\mathtt{x}\right\} \cap FV\left(\mathtt{e_2}\right)\right) \textbackslash \left\{f\right\}
    \andalso \Sigma_0 = \left\{\left(\mathtt{x=i_r}\right) \mid \left(\mathtt{x=i_r}\right) \in \Sigma \wedge \mathtt{x}\in fv\right\} \cap \left\{\mathtt{\left(f = a\right)}\right\}
    \andalso\\
    \Sigma' = \left\{ \left(\mathtt{x} = \mathtt{-\left(i + 1\right)}\right) \mid \left(\mathtt{x} = \mathtt{i_r}\right)\ \mathrm{as}\ \mathit{xi} \in \Sigma_0 \wedge \mathit{xi}\ \mathrm{is}\ i\mathrm{th\ element\ of}\ \Sigma_0 \right\}
    \andalso\\
        \mathit{Um}' = \left\{\mathtt{i} \mid \left(\mathtt{\textunderscore = i}\right) \in \Sigma'\right\}
    \andalso Q_\mathit{init}(\mathtt{e_1}) = \llceil \mathtt{e1}, \Sigma_\mathtt{e1}, \left\langle I_\mathtt{e1}, C_\mathtt{e1}, F_\mathtt{e1}, \textunderscore\right\rangle \rrceil
    \andalso \Sigma_\mathtt{e1}' = \Sigma_\mathtt{e1}; \mathtt{x} = \mathtt{0}
    \andalso\\
    \llceil \mathtt{e1}, \Sigma_\mathtt{e1}', \left\langle I_\mathtt{e1}, C_\mathtt{e1}, F_\mathtt{e1}, \mathit{Um}' \right\rangle \rrceil = \left\langle I_\mathtt{e1}', C_\mathtt{e1}', F_\mathtt{e1}', \textunderscore\right\rangle, \Sigma_\mathtt{e1}''
    \andalso \mathit{Clos}=\left\langle I_\mathtt{e1},', C_\mathtt{e1}', F_\mathtt{e1}', \mathit{Um} @ \mathit{Um'}\right\rangle
    \andalso\\
        I' = I; \mathtt{Clos(a, cx, 1)}
    \andalso F' = F; \mathtt{cx}=\mathit{Clos}
    \andalso \Sigma' = \Sigma; \mathtt{f=a}
    \andalso \llceil \mathtt{e2}, \Sigma', \left\langle I', C, F', Um \right\rangle \rrceil = Q', \Sigma''
}{
    \llceil \mathtt{let\ rec\ f\ x=e1\ in\ e2}, \Sigma, Q\rrceil = Q', \Sigma''
}

% \section{Evaluation: VM state transition rules}

\end{document}


\listfiles
\documentclass{article}
\author{Arya Stark}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{listings}
\usepackage{float}
\usepackage{tikz}
\usepackage{tikz,fullpage}
\usepackage{tkz-graph}
\usepackage[position=top]{subfig}
\usepackage{graphicx}

\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclareMathOperator{\cl}{cl}
\DeclareMathOperator{\E}{E}
\def\Z{\mathbb{Z}}
\def\N{\mathbb{N}}
\def\R{\mathbb{R}}
\def\Q{\mathbb{Q}}
\def\K{\mathbb{K}}
\def\T{\mathbb{T}}
\def\B{\mathcal{B}}
\def\XX{\mathfrak{X}}
\def\YY{\mathfrak{Y}}
\def\AA{\mathfrak{A}}
\def\ZZ{\mathfrak{Z}}
\def\BB{\mathcal{B}}
\def\UU{\mathcal{U}}
\def\MM{\mathcal{M}}
\def\M{\mathfrak{M}}
\def\l{\lambda}
\def\L{\Lambda}
\def\<{\langle}
\def\>{\rangle}

\usepackage[a4paper,margin=1in]{geometry}

\setlength{\parindent}{0cm}
\setlength{\parskip}{1em}

\title{Virtual Channels and Rebalancing in State Channel Networks}
\date{}

\begin{document}
\maketitle

\section*{Introduction}

This research note was written because there are some unsolved problems in designing state channel networks that are not well-known or being worked on. These problems reveal themselves when we consider how to build channel networks capable of expressing agents' preferences over network topology (payment capacity) and capital costs, expectation of other participants' future liveness, and minimizing blockchain fees.

\section*{Relevant Prerequisite Literature}

This section goes through relevant existing literature. Note that even if you know all this, many definitions will be used in later sections.

\subsection*{Ball-and-bead model of PCN}

In this model, edges represent two-party channel between the endpoints, and a party's balance within the channel is represented by beads near to them.

\begin{center}
\includegraphics[scale=0.4]{beads.png}
\includegraphics[scale=0.4]{beads2.png}
\end{center}

This shows Alice sending Bob 2 beads via Hub. The remaining capacity along this path is $\min(2,1) = 1$.

\subsection*{Rebalancing}

We say that two payment channel networks are value-equivalent if the set of agents is the same and each agent owns the amount of beads summed over all their channels. A channel network is rebalanced when it transitions into a value-equivalent state. This transition can include on-chain transactions or not. In the following on-chain rebalance, Bob increases the capacity of the Bob $\to$ Alice link.

\begin{center}
\includegraphics[scale=0.4]{on-chain-rebalancing-1.png}
\includegraphics[scale=0.4]{on-chain-rebalancing-2.png}
\end{center}

The following is an example of a rebalance that does not require on-chain transactions.

\begin{center}
\includegraphics[scale=0.4]{off-chain-rebalancing-1.png}
\includegraphics[scale=0.4]{off-chain-rebalancing-2.png}
\end{center}

Are off-chain rebalances sufficient? In general, no.

% TBW: define a $a,b$-flow, apply max-flow-min-cut theorem, etc.

% Note some assumptions implicitly made when applying this theorem: the max flow might requrie very global knowledge to compute.

% Fun exercise! A self-payment is a special type of off-chain rebalance. Show that any rebalance can be expressed as a series of self-payments.

\subsection*{Time-based lockup}

There are multiple ways that intermediaries can structure agreements-to-route; we go through two extremal points here. They can agree to route a single payment. When that payment completes successfully, the balances change and they are free of any further commitments. The second way is to agree to lock up funds for a certain period of time, creating a ``virtual channel". The balances in the virtual channel can be updated instantly and many times without the intermediary's participation or knowledge. At the end of the lockup time, the final balances in the virtual channel are agreed to and we update the direct channel balances as though a single set of routed balance updates (routed payments) was made.

\subsection*{Ejection}

There is an upper bound on the fee that the intermediary can charge, since any virtual channel can be transformed into a direct channel (i.e. it can be ``ejected") by some on-chain transactions, without disrupting the internal state of the virtual channel. Here is an example. Suppose we have this system of direct channels. (Note: we switch to labelling edges by their endpoint balances rather than using beads, due to limitations of latex).

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{H}
        \Vertex[x=4,y=0]{B}
        \Edge[label=$1/1$](A)(H)
        \Edge[label=$1/1$](H)(B)
    \end{tikzpicture}
\end{figure}

Here, a virtual channel between A and B can be formed, with balance one bead for each of them, locking up two beads from H. The channel could be ejected by an on-chain transaction that transfers one bead out of the A-H channel and H-B channel and atomically updating the internal states of them.

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge[label=$0/1$](A)(B)
        \Edge[label=$1/0$](B)(C)
        \Edge[style={bend left},label=$1/1$](A)(C)
    \end{tikzpicture}
\end{figure}

\subsection*{Intermediation Fees}

For this reason, thinking of lockups as either completely ``per-payment" or ``time-based" is not strictly accurate; the costs an intermediary would incur in a channel network is both the cost of re-configuring the capacity graph as well as time-based opportunity cost of having their capital in excess of payments they would want to make anyway in the network (e.g. instead of staking), however both can be avoided by on-chain transactions; the instances where on-chain transactions are avoided and off-chain fees paid instead are therefore some complex subset of transactions in general.

Here is a crude fee scheme that captures some of it this: an intermediary charges some large fee $F_1$ to intermediate a virtual channel, and assumes the responsibility of ejecting the channel; if the virtual channel participants have no further use for it, they negotiate a payment of $F_2$ from the intermediary to close the virtual channel offline. If the on-chain fee is $T$, we should have $0 < F_1 - F_2 < T < F_1$. On/off-chain rebalancing is handeled by an independent protocol.

Another design: timeout by default, but eject if agreed to.

\subsection*{State Channels}

By allowing beads to be locked into complex applications (e.g. chess, prediction markets) instead of just payments, the need for virtual channels as well as for ejection is even more apparent. Beads can be freed up and re-used within a virtual channel. The time bound for an application might be long or unknown (e.g., we lock beads up in a bet of whether any US Libertarian Party ever wins a House seat in the next 10 years),

\subsection*{Multipath}

TBW

\subsection*{N-Party}

TBW

\subsection*{Subchannels}

TBW

\subsection*{Plasma}

Plasma can be used to fund channels. If all channels are funded by plasma, then rebalancing is a plasma transaction in the normal case, i.e., the entire network can be rebalanced arbitrarily in a single ethereum transaction. I claim that a super-optimized channel network that does not support funding via plasma will be much more expensive than a very suboptimal one that supports funding via plasma.

\section*{Ejection Test Cases}

We present some ejection test cases and for each one, we show that it could be the optimal way of ejecting the channel given certain assumptions about the participants' preferences over the capacity graph, on-chain fees, and expectation of other participants' future liveness.

\subsection*{Symmetric}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge[label=$1/1$](A)(B)
        \Edge[label=$1/1$](B)(C)
        \Edge[style={bend left}](A)(C)
    \end{tikzpicture}
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge[label=$0/1$](A)(B)
        \Edge[label=$1/0$](B)(C)
        \Edge[style={bend left},label=$1/1$](A)(C)
    \end{tikzpicture}
\end{figure}

Reviewed previously.

\subsection*{Asymetric}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge[label=$1/1$](A)(B)
        \Edge[label=$1/1$](B)(C)
        \Edge[style={bend left}](A)(C)
    \end{tikzpicture}
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge[label=$0/2$](A)(B)
        \Edge(B)(C)
        \Edge[style={bend left},label=$1/1$](A)(C)
    \end{tikzpicture}
\end{figure}

What are the differences between the the assymetric and symmetric ways of ejecting? The symmetric manner might be preferred because it minimal in the number of transactions (1 vs 2). Even if we assume some mechanism to batch two sub-transactions under one (e.g. account abstraction), it is minimal in the number of ERCECOVERS (3 vs 2). On the other hand, the symmetric manner might be preferred for the more ``balanced" resulting capacity graph.

\subsection*{Symmetric Unidirectional}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge[label=$2/0$](A)(B)
        \Edge[label=$2/0$](B)(C)
        \Edge[style={bend left}](A)(C)
    \end{tikzpicture}
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge[label=$0/1$](A)(B)
        \Edge[label=$1/0$](B)(C)
        \Edge[style={bend left},label=$2/0$](A)(C)
    \end{tikzpicture}
\end{figure}

\subsection*{Asymmetric Unidirectional 1}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge[label=$1/0$](A)(B)
        \Edge[label=$1/0$](B)(C)
        \Edge[style={bend left}](A)(C)
    \end{tikzpicture}
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge[label=$0/1$](A)(B)
        \Edge(B)(C)
        \Edge[style={bend left},label=$1/0$](A)(C)
    \end{tikzpicture}
\end{figure}

\subsection*{Asymmetric Unidirectional 2}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge[label=$1/0$](A)(B)
        \Edge[label=$1/0$](B)(C)
        \Edge[style={bend left}](A)(C)
    \end{tikzpicture}
    \begin{tikzpicture}[scale=2.75]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Edge(A)(B)
        \Edge[label=$1/0$](B)(C)
        \Edge[style={bend left},label=$1/0$](A)(C)
    \end{tikzpicture}
\end{figure}

\subsection*{Long-Chain Large-Radius}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=1.5]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Vertex[x=6,y=0]{D}
        \Vertex[x=8,y=0]{E}
        \Vertex[x=10,y=0]{F}
        \Edge[label=$1/0$](A)(B)
        \Edge[label=$1/0$](B)(C)
        \Edge[label=$1/0$](C)(D)
        \Edge[label=$1/0$](D)(E)
        \Edge[label=$1/0$](E)(F)
        \Edge[style={bend left}](A)(F)
    \end{tikzpicture}
    \begin{tikzpicture}[scale=1.5]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Vertex[x=6,y=0]{D}
        \Vertex[x=8,y=0]{E}
        \Vertex[x=10,y=0]{F}
        \Edge[label=$0/1$](A)(B)
        \Edge[label=$0/1$](B)(C)
        \Edge[label=$0/1$](C)(D)
        \Edge(D)(E)
        \Edge[label=$1/0$](E)(F)
        \Edge[style={bend left},label=$1/0$](A)(F)
    \end{tikzpicture}
\end{figure}

\subsection*{Long-Chain Short-Radius}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=1.5]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Vertex[x=6,y=0]{D}
        \Vertex[x=8,y=0]{E}
        \Vertex[x=10,y=0]{F}
        \Edge[label=$1/0$](A)(B)
        \Edge[label=$1/0$](B)(C)
        \Edge[label=$1/0$](C)(D)
        \Edge[label=$1/0$](D)(E)
        \Edge[label=$1/0$](E)(F)
        \Edge[style={bend left}](C)(E)
    \end{tikzpicture}
    \begin{tikzpicture}[scale=1.5]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=0]{A}
        \Vertex[x=2,y=0]{B}
        \Vertex[x=4,y=0]{C}
        \Vertex[x=6,y=0]{D}
        \Vertex[x=8,y=0]{E}
        \Vertex[x=10,y=0]{F}
        \Edge[label=$0/1$](A)(B)
        \Edge[label=$0/1$](B)(C)
        \Edge[label=$0/1$](C)(D)
        \Edge(D)(E)
        \Edge[label=$1/0$](E)(F)
        \Edge[style={bend left},label=$1/0$](C)(E)
    \end{tikzpicture}
\end{figure}

\subsection*{Multipath}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=1.5]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=2,y=0]{D}
        \Vertex[x=0,y=1]{A}
        \Vertex[x=4,y=1]{E}
        \Vertex[x=1,y=2]{B}
        \Vertex[x=3,y=2]{C}
        \Edge(A)(E)
        \Edge(A)(B)
        \Edge(B)(C)
        \Edge(C)(E)
        \Edge(A)(D)
        \Edge(D)(E)
    \end{tikzpicture}
\end{figure}

Balances and ejection to be filled in.

\subsection*{Thanos Star}

New notation: a square node is not a participant, but it means that its neighbours are all connected in a direct channel.

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=1.5]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=2.5]{A}
        \Vertex[x=3,y=0]{B}
        \Vertex[x=3,y=1]{C}
        \Vertex[x=3,y=2]{D}
        \Vertex[x=3,y=3]{E}
        \Vertex[x=3,y=4]{F}
        \Vertex[x=3,y=5]{G}
        \begin{scope}[VertexStyle/.append style = {rectangle}]
        \Vertex[x=6,y=2.5,style={color=red}]{H}
        \end{scope}
        \Edge[label=$1/1$](A)(B)
        \Edge[label=$1/1$](A)(C)
        \Edge[label=$1/1$](A)(D)
        \Edge[label=$1/1$](A)(E)
        \Edge[label=$1/1$](A)(F)
        \Edge[label=$1/1$](A)(G)
        \Edge(H)(B)
        \Edge(H)(C)
        \Edge(H)(D)
        \Edge(H)(E)
        \Edge(H)(F)
        \Edge(H)(G)
    \end{tikzpicture}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=1.5]
        \GraphInit[vstyle=Normal]
        \SetVertexSimple
        \Vertex[x=0,y=2.5]{A}
        \Vertex[x=3,y=0]{B}
        \Vertex[x=3,y=1]{C}
        \Vertex[x=3,y=2]{D}
        \Vertex[x=3,y=3]{E}
        \Vertex[x=3,y=4]{F}
        \Vertex[x=3,y=5]{G}
        \begin{scope}[VertexStyle/.append style = {rectangle}]
        \Vertex[x=6,y=2.5,style={color=red}]{H}
        \end{scope}
        \Edge(A)(B)
        \Edge(A)(C)
        \Edge(A)(D)
        \Edge[label=$2/0$](A)(E)
        \Edge[label=$2/0$](A)(F)
        \Edge[label=$2/0$](A)(G)
        \Edge[label=$1$](H)(B)
        \Edge[label=$1$](H)(C)
        \Edge[label=$1$](H)(D)
        \Edge[label=$1$](H)(E)
        \Edge[label=$1$](H)(F)
        \Edge[label=$1$](H)(G)
    \end{tikzpicture}
\end{figure}

A 6-party virtual channel gets ejected into a 6-party direct channel. This manner of ejecting is minimal in number of transactions.

\section*{Other test cases}

\subsection*{Under-Capacity Three Party Channel}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=1.5]
        \GraphInit[vstyle=Normal]
        \Vertex[x=0,y=0]{A}
        \Vertex[x=0,y=2]{B}
        \Vertex[x=3,y=1]{H}
        \Vertex[x=6,y=1]{C}
        \Edge[label=$1/0$](A)(H)
        \Edge[label=$1/0$](B)(H)
        \Edge[label=$1/0$](H)(C)
    \end{tikzpicture}
\end{figure}

If A,B,C form a virtual channel through H, they end up in a three-party virtual channel where A can send C one bead, and B can send C one bead, but the two cannot happen at the same time. The challenge is that it is inappropirate to represent this as a three-party channel where A and B's balances are both one.

\subsection*{Self Lock-in}

TBW

channel factories: more attack surface, but root nonce => same cost of attack

channels-on-plasma spectrum

\end{document}


\title{LMMP: Stereo Module Testing Plan}
\author{
        Ara V. Nefian \and Michael Broxton
}
\date{\today}

\documentclass[12pt]{article}

\begin{document}
\maketitle

This document describes a testing plan for the LMMP stereo module.
{\underline {\bf Accuracy measures}}

\begin{itemize}
\item {\bf Option 1:} Short term, if ground truth data (Apollo Panoramic images) is available (Feb 15). Let the predicted horizontal and vertical
      disparity map for the $k$th image be denoted as ${\bf P}_{k, h}(i,j)$ and ${\bf P}_{k, v}(i,j)$  respectively with the corresponding ground truth dispariy map ${\bf T}_{k, h}(i,j)$ and ${\bf T}_{k, v}(i,j)$ for all pixels $(i,j)$.

The ${\bf average}$ ${\bf error}$ is
      ${\bf E} =\frac{1}{K}\sum_k\frac{\sum_{ij} (P_{k,v}(i,j)-T_{k,v}(i,j))^2 + (P_{k,h}(i,j)-T_{k,h}(i,j))^2}{N_{k,pred}} $ where $N_{k,pred}$
      is the number of pixels for which the disparity map was computed in the $k$th image, and $K$ is the total number of images for
      which we have ground truth.

      The ${\bf average}$ ${\bf coverage}$ ${\bf C}$ measures the number of pixels for which the disparity is determined:
${\bf C} = \frac{1}{K}\sum_k\frac{N_{k,pred}}{N_{k,true}}$, where $N_{k, true}$ is the resolution of the ground truth images.

     The goal is to have a small average error ${\bf E}$ and large avearge coverage ${\bf C}$ measures.

\item {\bf Option 2:} Very short term, before the ground truth data is made available (Jan 15th).
                    Compute the ${\bf E}$ measure using existing synthetic ground truth data, and the ${\bf C}$
                    measure using the MOC and Apollo Metric data.

\item {\bf Option 3:} On the longer term the ${\bf E}$ measure will be weighted by the confidence score of the Bayesian subpixel correlator.

       ${\bf \tilde E} =\frac{1}{K}\sum_k\frac{\sum_{ij} ((P_{k,v}(i,j)-T_{k,v}(i,j))^2 + (P_{k,h}(i,j)-T_{k,h}(i,j))^2)\frac{p_{ij}}{\sum_{ij}p_{ij}}}{N_{k,pred}} $
\end{itemize}

{\underline {\bf Performance measures}}
 Very short term (Jan 15th). Compute the number of additions, multiplications log and exp operations per pixel and run time required to generate the dense disparity map.
%\begin{abstract}
%This is the paper's abstract \ldots
%\end{abstract}

%\section{Introduction}
%This is time for all good men to come to the aid of their party!

%\paragraph{Outline}
%The remainder of this article is organized as follows.
%Section~\ref{previous work} gives account of previous work.
%Our new and exciting results are described in Section~\ref{results}.
%Finally, Section~\ref{conclusions} gives the conclusions.

%\section{Previous work}\label{previous work}
%A much longer \LaTeXe{} example was written by Gil~\cite{Gil:02}.

%\section{Results}\label{results}
%In this section we describe the results.

%\section{Conclusions}\label{conclusions}
%We worked hard, and achieved very little.

\bibliographystyle{abbrv}
\bibliography{simple}

\end{document}
This is never printed


\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
    \usepackage{xltxtra,xunicode}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
  \newcommand{\euro}{€}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\ifxetex
  \usepackage[setpagesize=false, % page size defined by xetex
              unicode=false, % unicode breaks when used with xetex
              xetex]{hyperref}
\else
  \usepackage[unicode=true]{hyperref}
\fi
\hypersetup{breaklinks=true,
            bookmarks=true,
            pdfauthor={},
            pdftitle={Foo},
            colorlinks=true,
            citecolor=blue,
            urlcolor=blue,
            linkcolor=magenta,
            pdfborder={0 0 0}}
\urlstyle{same}  % don't use monospace font for urls
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\setcounter{secnumdepth}{5}

\title{Foo}
\date{}
\usepackage{graphicx}
\usepackage[all]{hypcap}
\usepackage{tikz}
\usepackage{standalone}
\usepackage[sort&compress, numbers]{natbib}
\usepackage{minted}

\begin{document}
\maketitle

\section{Math}\label{math}

\begin{equation}\label{eq:ellipse}
Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0
\end{equation}

Refering equation \eqref{eq:ellipse}, \(\forall n, x_i^n = x_i\).

\section{Image}\label{image}

See following figure(\autoref{fig:xor}).

\begin{figure}[h]\centering\includegraphics[width=\textwidth]{.//assets/images/xor.png}\caption{Figure Example}\label{fig:xor}\end{figure}

\section{Tex2img}\label{tex2img}

See following figure(\autoref{fig:test}).

\begin{figure}[h]\centering\input{./misc/test.tex}\caption{Test tex figure}\label{fig:test}\end{figure}

\section{Gist}\label{gist}

\inputminted[mathescape, linenos, frame=lines, framesep=2mm]{Python}{.gist-cache/cache.7aeefc0de1bb10005514355a5c4a5dfe-1c74b8336494cb0e9c6d-xor-5d.py}

\section{Post Link}\label{post-link}

\href{http://localhost:4000/2015/04/29/foo/}{Foo}

Awesome paper\cite{mikolov2013efficient}.

\bibliographystyle{unsrt}

\bibliography{assets/printables/references}

\renewcommand{\thefootnote}{}

\footnotetext{Online version at \url{http://localhost:4000/2015/04/29/foo/}}

\end{document}