% !TEX root = ../main.tex

In this chapter, we consider the set reconciliation protocol sketched in the introduction in greater detail.
We define the protocol in \cref{set-reconciliation-def}, prove its correctness in \cref{set-reconciliation-simple-correct}, and do a complexity analysis in \cref{set-reconciliation-complexity}.  \Cref{set-reconciliation-simple-optimizations} lists some optimizations which do not change the asymptotic complexity but which avoid some unnecessary work. We conclude the chapter with an example application in \cref{set-reconciliation-graphs}, briefly describing how the protocol can be applied to the synchronization of the hash graphs that arise, e.g., in the context of distributed version control systems such as git~\cite{chacon2014pro}.

\section{Recursive Set Reconciliation}
\label{set-reconciliation-def}

The set reconciliation protocol assumes that there is a set $U$, a linear order $\preceq$ on $U$, a node $\mathcal{X}_0$ locally holding some $X_0 \subseteq U$, and a node $\mathcal{X}_1$ locally holding $X_1 \subseteq U$. Both nodes are able to compute fingerprints for sets via some function $\fun{\fpname}{\powerset{U}}{D}$. This function could in principle be any hash function, but in the complexity analysis we will assume that it is instantiated as $\lift{\f}{\mathcal{M}}$ with a suitable underlying hash function $f$ and a suitable monoid $\mathcal{M}$ over the universe $D$ (see \cref{def-lift}).

Recall that $\range{x}{y}{S}$ denotes the set $\set{s \in S \mid x \preceq s \prec y}$ if $x \prec y$, or $S \setminus \range{y}{x}{S}$ if $y \preceq x$ (\cref{def-range}).

$\mathcal{X}_0$ and $\mathcal{X}_1$ exchange messages over multiple rounds, a message consists of an arbitrary number of \defined{range fingerprints} and \defined{range item sets}.
A range fingerprint is a triplet $\ifp{x}{y}{X_i}$ for $x, y \in U$, a range item set a four-tuple $\iis{x}{y}{S}{b}$ for $x, y \in U$, $S \subseteq \range{x}{y}{X_i}$, and $b \in \{0, 1\}$. $b$ indicates whether the range item set is a response to a previous range item set.

When a node $\mathcal{X}_i$ receives a message, it performs the following actions:

\begin{itemize}
  \item For every range item set $\iis{x}{y}{S}{b}$ in the message, all items in $S$ are added to the locally stored set $X_i$. If $b = 0$, the node then adds the range item set $\iis{x}{y}{\range{x}{y}{X_i} \setminus S}{1}$ to the response, unless $\range{x}{y}{X_i} \setminus S = \emptyset$.
  \item For every range fingerprint $\ifp{x}{y}{X_j}$ in the message, it does one of following:
    \begin{caselist}
      \case[Equal Fingerprints] \label{def-fingerprint-eq} If $\fp{\range{x}{y}{X_j}} = \fp{\range{x}{y}{X_i}}$, nothing happens.
      \case[Recursion Anchor] \label{def-recursion-anchor} The node may add the range item set\\
$\iisnatural{x}{y}{X_i}{0}$ to the response. If $\abs{\range{x}{y}{X_j}} \leq 1$, it must do so.
      \case[Recurse] \label{def-recurse} Otherwise, the node selects $m_0 = x \prec m_1 \prec \ldots \prec m_k = y \in U$, $k \geq 2$ such that among all $\range{m_0}{m_1}{X_i}$ for  $0 \leq l < k$ at least two ranges are non-empty. For all $0 \leq l < k$ it adds either the range fingerprint $\ifp{m_l}{m_{l + 1}}{X_i}$ or the range item set $\iisnatural{m_l}{m_{l + 1}}{X_i}{0}$ to the response.
    \end{caselist}
  \item If the accumulated response is nonempty, it is sent to the other node. Otherwise, the protocol has terminated successfully.
\end{itemize}

To initiate reconciliation of a range $[x, y)$, a node $\mathcal{X}_i$ sends a message containing solely the range fingerprint $\ifp{x}{y}{X_i}$.

\Cref{simple-set-reconciliation-example} gives an example run of the protocol.

\begin{figure*}
$X_0 \defeq \{\exampleb, \examplec, \exampled, \examplee, \examplef, \exampleh \}$
\hfill
$X_1 \defeq \{\examplea, \examplee, \examplef, \exampleg\}$

\begin{scaletikzpicturetowidth}{\textwidth}
\begin{tikzpicture}[scale=\tikzscale, font=\tiny]
	\pgfdeclarelayer{background}
	\pgfdeclarelayer{foreground}
	\pgfsetlayers{background,main,foreground}

	\begin{pgfonlayer}{main}
		%vertices
		\node (vroot) at (0, 1) [fpi] {\examplefpi{\examplea}{\examplei}{\{\examplea, \examplee, \examplef, \exampleg\}}};

		\node (v00) at (-4, -0) [fpi] {\examplefpi{\examplea}{\examplee}{\{\exampleb, \examplec, \exampled\}}};
		\node (v01) at (4, -0) [fpi] {\examplefpi{\examplee}{\examplei}{\{\examplee, \examplef, \exampleh\}}};

                \node (v10) at (-4, -1) [iis] {\exampleiis{\examplea}{\examplee}{\{\examplea\}}{0}};
                \node (v11) at (2, -1) [fpi] {\examplefpi{\examplee}{\exampleg}{\{\examplee, \examplef\}}};
                \node (v12) at (6, -1) [iis] {\exampleiis{\exampleg}{\examplei}{\{\exampleg\}}{0}};

                \node (v20) at (-4, -2) [iis] {\exampleiis{\examplea}{\examplee}{\{\exampleb, \examplec, \exampled\}}{1}};
                \node (v21) at (6, -2) [iis] {\exampleiis{\exampleg}{\examplei}{\{\exampleh\}}{1}};
		%edges
                \draw (vroot) edge[edge] (v00);
                \draw (vroot) edge[edge] (v01);

		\draw (v00) edge[edge] (v10);
		\draw (v01) edge[edge] (v11);
		\draw (v01) edge[edge] (v12);

		\draw (v10) edge[edge] (v20);
		\draw (v12) edge[edge] (v21);
	\end{pgfonlayer}

	\begin{pgfonlayer}{background}
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](8, 1) -- (-8, 1);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](-8, -0) -- (8, -0);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](8, -1) -- (-8, -1);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](-8, -2) -- (8, -2);
	\end{pgfonlayer}
\end{tikzpicture}
\end{scaletikzpicturetowidth}

\caption[Detailed synchronization example]{
An example run of the protocol. $\mathcal{X}_1$ initiates reconciliation for all items between \examplea~ and \examplei (ordered alphabetically) by sending its fingerprint for the whole range.

Upon receiving this range fingerprint, $\mathcal{X}_0$ locally computes $\fp{\range{\examplea}{\examplei}{X_0}}$. Since the result does not match the received range, $\mathcal{X}_0$ splits $X_0$ into two parts of equal size and transmits range fingerprints for these subranges.

In the third round, $\mathcal{X}_1$ locally computes fingerprints for the two received ranges, but neither matches. $\abs{\range{\examplea}{\examplee}{X_1}} \leq 1$, so $\mathcal{X}_1$ transmits the corresponding range items set, i.e. $\iis{\examplea}{\examplee}{\examplea}{0}$. $\abs{\range{\examplee}{\examplei}{X_1}} > 1$, so another recursion step is performed. After splitting the range, the lower range is large enough to send its fingerprint, the upper one however only contains one item and thus results in another range item set.

In the fourth and final communication round, $\mathcal{X}_0$ receives two range item sets and answers with the items it holds within those ranges. When it receives the range fingerprint $\ifp{\examplee}{\exampleg}{X_1}$, it computes an equal fingerprint for $\ifp{\examplee}{\exampleg}{X_0}$, so no further action is required for this particular range.
}

\label{simple-set-reconciliation-example}
\end{figure*}

\subsection{Observations}
\label{observations}

Partitioning based on a total order allows the nodes to perform a limited form of queries, i.e. range queries. A node can ask for reconciliation within a certain range, rather than over the whole universe.

If the universe $U$ is not finite, then there are items that require an arbitrary amount of bytes to encode. Since the protocol needs to transmit items to denote range boundaries, no reasonably complexity guarantees can be given for infinite universes. We will thus assume $U$ to be finite and small enough that items can be reasonably encoded. This assumption is not very restrictive in practice because nodes can always synchronize hashes of items rather than the items themselves. The protocol can then be either followed by a phase where hashes of interest are transferred and anwered by the actual items, or the protocol can be made aware of the distinction and use hashes as range boundaries while transmitting actual items for range item sets.

When reconciling hashes in place of actual items, any semantically interesting order on the items would be replaced by an arbitrary order on the hashes. But rather than using only the hashes as range boundaries, one can just as well add additional information. For example if the universe of interest consists of timestamped strings of arbitrary length, the range boundaries can consist of timestamped hashes, ordered by timestamp first and using the numeric order on the hashes as a tiebreaker. \Cref{set-reconciliation-graphs} gives a more detailed example for utilizing this technique.

\section{Proof of Correctness}
\label{set-reconciliation-simple-correct}

\newcommand{\intcount}[1]{\mathit{count}_{#1}}

We now prove the correctness of the protocol. The protocol is correct if for all $x, y \in U$ both nodes eventually hold $\range{x}{y}{X_i} \cup \range{x}{y}{X_j}$ after a node $\mathcal{X}_i$ has received a message pertaining to the range $[x, y)$.

\begin{caselist}
\case[Range Item Set] \label{case-iis}  If the message contains the range item set $\iisnatural{x}{y}{X_j}{0}$, then $\mathcal{X}_i$ adds all items to its set, resulting in $\range{x}{y}{X_i} \cup \range{x}{y}{X_j}$ as desired. $\mathcal{X}_j$ then receives $\iis{x}{y}{\range{x}{y}{X_i} \setminus \range{x}{y}{X_j}}{1}$, ending up with $\range{x}{y}{X_j} \cup (\range{x}{y}{X_i} \setminus \range{x}{y}{X_j}) = \range{x}{y}{X_i} \cup \range{x}{y}{X_j}$ as desired.

\case[Range Fingerprint] \label{case-ifp} Otherwise, the message contains a range fingerprint $\ifp{x}{y}{X_j}$.

\begin{caselist}
\case[Equal Fingerprints] If $\fp{\range{x}{y}{X_j}} = \fp{\range{x}{y}{X_i}}$, the protocol terminates immediately and no changes are performed by any node. Assuming no fingerprint collision occurred, $\range{x}{y}{X_i} = \range{x}{y}{X_j} = \range{x}{y}{X_i} \cup \range{x}{y}{X_j}$ as desired.

\case[Recursion Anchor] \label{case-ifp-anchor} If $\mathcal{X}_i$ adds the range item set $\iisnatural{x}{y}{X_i}{0}$, then \cref{case-iis} applies when the other node receives the response, with the roles of the nodes reversed.

\case[Recurse] Let $\intcount{i} \defeq \abs{\range{x}{y}{X_i}}$ and $\intcount{j} \defeq \abs{\range{x}{y}{X_j}}$. $\intcount{j} \geq 2$, since otherwise $\mathcal{X}_j$ would have sent an item set for the range. Similarly, $\intcount{i} \geq 2$, since we are not in \cref{case-ifp-anchor}. Thus, $\intcount{i} + \intcount{j} \geq 4$, and the protocol has already been proven correct for all cases where $\intcount{i} + \intcount{j} < 4$.

We can thus finish the proof by induction on $\intcount{i} + \intcount{j}$, using the induction hypothesis that for all $x', y' \in U$ such that $\abs{\range{x'}{y'}{X_i}} + \abs{\range{x'}{y'}{X_j}} < \intcount{i} + \intcount{j}$ the protocol correctly reconciles $\range{x'}{y'}{X_i}$ and $\range{x'}{y'}{X_j}$.

$\mathcal{X}_i$ partitions the range into $k \geq 2$ subranges, of which at least two must be nonempty.
Thus $\abs{\range{m_l}{m_{l + 1}}{X_i}} < \intcount{i}$ for all $0 \leq l < k$.
Furthermore, $\range{m_l}{m_{l + 1}}{X_j} \subseteq \range{x}{y}{X_j}$ and thus $\abs{\range{m_l}{m_{l + 1}}{X_j}} \leq \abs{\range{x}{y}{X_j}}$, so overall we have $\abs{\range{m_l}{m_{l + 1}}{X_i}} + \abs{\range{m_l}{m_{l + 1}}{X_j}} < \intcount{i} + \intcount{j}$ and can apply the induction hypothesis to conclude that every subrange is correctly reconciled. Since the subranges partition the original range, the original range is then correctly reconciled as well.
\end{caselist}
\end{caselist}

\section{Complexity Analysis}
\label{set-reconciliation-complexity}

The protocol gives nodes the freedom to respond to a range fingerprint with a range item set even if the range fingerprint is arbitrarily large. For a meaningful complexity analysis we need to restrict the behavior of the node, a realistic modus operandi is for a node to send a range item set whenever it holds a number of items less than or equal to some threshold $t \in \mathbb{N}, t \geq 1$ within the range. Higher choices for $t$ reduce the number of roundtrips, but increase the probability that an item is being sent even though the other node already holds it.

A node is similarly given freedom over the number of subranges into which to split a range when recursing. We will assume a node always splits into at most $b \in \mathbb{N}, b \geq 2$ subranges. As with $t$, higher numbers reduce the number of roundtrips at the cost of potentially sending items or fingerprints that did not need sending.

Because we want to analyze not only the worst-case complexity but also the complexity depending on the similarity between the two sets held by the participating nodes, we define some more fine-grained instance size parameters: $n_0$ and $n_1$ denote the number of items held by $\mathcal{X}_0$ and $\mathcal{X}_1$ respectively. We let $n \defeq n_0 + n_1$, $n_{\min} \defeq \min(n_0, n_1)$ and $n_{\triangle} \defeq \abs{(\range{x}{y}{X_0} \cup \range{x}{y}{X_1}) \setminus (\range{x}{y}{X_0} \cap \range{x}{y}{X_1})}$.

\subsection{Preliminary Observations}

A helpful observation for the following analysis is that the range fingerprints that are being exchanged during a protocol run form a rooted tree where every vertex has at most $b$ children. When a leaf of the tree is reached, an exchange of range item sets follows. Equal fingerprints can also cut the tree short, but for the following worst-case analyses we will assume this does not occur.

Node $\mathcal{X}_i$ can branch at most $\ceil{\log_{b}(n_i)}$ times, so the overall height of the tree is bounded by $2 \cdot\ceil{\log_{b}(n_{\min})}$. The number of vertices of such a complete tree of height $h$ is at most $\sum_{i=0}^{h} b^{i} = \frac{b^{h} - 1}{b - 1}$. For $h \leq 2 \cdot\ceil{\log_{b}(n_{\min})}$, $\frac{b^{h} - 1}{b - 1} \leq 2 \cdot 2 \cdot n_{\min} \leq 2n \in \complexity{n}$.

The parameter $t$ determines when recursion is cut off, and thus influences the height of the tree. For $t = 1$, the protocol recurses as far as possible. For $t = b$, the last level of recursion is cut off, for $t = b^2$ the last two levels, and so on. Overall, the height of the tree is reduced by $\floor{\log_{b}(t)}$.

\subsection{Communication Rounds}

The number of communication rounds clearly corresponds to the height of the tree, plus $2$ to account for the exchange of range item sets, so the worst-case is $2 + 2 \cdot\ceil{\log_{b}(n_{\min})} - \floor{\log_{b}(t)} \in \complexity{\log_{b}(n)}$. This number cannot be bounded by $n_{\triangle}$, as witnessed by problem instances where one node is missing exactly one item compared to the other node. In such an instance, $b - 1$ branches in each recursion step result in equal fingerprints, but the one branch that does continue reaches the recursion anchor only after the full number of rounds. See \cref{fig:worst-rounds} for an example.

\begin{figure*}
$X_0 \defeq \{\examplea, \exampleb, \examplec, \exampled, \examplee, \exampleg, \exampleh \}$
\hfill
$X_1 \defeq \{\examplea, \exampleb, \examplec, \exampled, \examplee, \examplef, \exampleg, \exampleh \}$

\begin{scaletikzpicturetowidth}{\textwidth}
\begin{tikzpicture}[scale=\tikzscale, font=\tiny]
	\pgfdeclarelayer{background}
	\pgfdeclarelayer{foreground}
	\pgfsetlayers{background,main,foreground}

	\begin{pgfonlayer}{main}
		%vertices
		\node (vroot) at (0, 1) [fpi] {\examplefpi{\examplea}{\examplei}{\{\examplea, \exampleb, \examplec, \exampled, \examplee, \examplef, \exampleg, \exampleh\}}};

		\node (v00) at (-4, -0) [fpi] {\examplefpi{\examplea}{\examplee}{\{\examplea, \exampleb, \examplec, \exampled\}}};
		\node (v01) at (4, -0) [fpi] {\examplefpi{\examplee}{\examplei}{\{\examplee, \exampleg, \exampleh\}}};

                \node (v10) at (2, -1) [fpi] {\examplefpi{\examplee}{\exampleg}{\{\examplee, \examplef\}}};
                \node (v11) at (6, -1) [fpi] {\examplefpi{\exampleg}{\examplei}{\{\exampleg, \exampleh\}}};

                \node (v20) at (2, -2) [iis] {\exampleiis{\examplee}{\exampleg}{\{\examplee\}}{0}};

                \node (v30) at (2, -3) [iis] {\exampleiis{\examplee}{\exampleg}{\{\examplef\}}{1}};

		%edges
                \draw (vroot) edge[edge] (v00);
                \draw (vroot) edge[edge] (v01);

		\draw (v01) edge[edge] (v10);
		\draw (v01) edge[edge] (v11);

		\draw (v10) edge[edge] (v20);

		\draw (v20) edge[edge] (v30);
	\end{pgfonlayer}

	\begin{pgfonlayer}{background}
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](8, 1) -- (-8, 1);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](-8, -0) -- (8, -0);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](8, -1) -- (-8, -1);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](-8, -2) -- (8, -2);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](8, -3) -- (-8, -3);
	\end{pgfonlayer}
\end{tikzpicture}
\end{scaletikzpicturetowidth}

\caption{An example run of the protocol that takes the greatest possible number of rounds even though $n_{\triangle} = 1$. $b \defeq 2, t \defeq 1$.}

\label{fig:worst-rounds}

\end{figure*}

\subsection{Communication Complexity}
\label{communication-complexity}

The total number of bits that needs to be transmitted during a protocol run is proportional to the number of vertices in the tree. Every range fingerprint consists of two items and one fingerprint, so assuming $U$ is finite this lies in $\complexity{1}$. Since there are at most $2n$ vertices in the tree, the range fingerprints require at most $\complexity{n}$ bits to be communicated.

The exchange of range item sets consists in the worst case of exchanging every item using $\ceil{\frac{n}{t}}$ range item sets. A range item set needs to transmit two items to encode the boundaries, as well as the items themselves, which lies in $\complexity{1}$ per range item set. All range item sets together thus amount to another $\complexity{n}$, leading to a total of $\complexity{n}$ bits being transmitted in the worst case.

\Cref{fig:worst-bytes} shows a worst-case example in which the tree of height $h \defeq \log_{b}(2 \cdot n_{\min})$ has all $\frac{b^{h} - 1}{b - 1}$ vertices.

\begin{figure*}
$X_0 \defeq \{\examplea, \examplec, \examplee, \exampleg \}$
\hfill
$X_1 \defeq \{\examplea, \exampleb, \examplec, \exampled, \examplee, \examplef, \exampleg, \exampleh\}$

\begin{scaletikzpicturetowidth}{\textwidth}
\begin{tikzpicture}[scale=\tikzscale, font=\tiny]
	\pgfdeclarelayer{background}
	\pgfdeclarelayer{foreground}
	\pgfsetlayers{background,main,foreground}

	\begin{pgfonlayer}{main}
		%vertices
		\node (vroot) at (0, 1) [fpi] {\examplefpi{\examplea}{\examplei}{\{\examplea, \exampleb, \examplec, \exampled, \examplee, \examplef, \exampleg, \exampleh\}}};

		\node (v00) at (-4, -0) [fpi] {\examplefpi{\examplea}{\examplee}{\{\examplea, \examplec\}}};
		\node (v01) at (4, -0) [fpi] {\examplefpi{\examplee}{\examplei}{\{\examplee, \exampleg\}}};

                \node (v10) at (-6, -1) [fpi] {\examplefpi{\examplea}{\examplec}{\{\examplea, \exampleb\}}};
                \node (v11) at (-2, -1) [fpi] {\examplefpi{\examplec}{\examplee}{\{\examplec, \exampled\}}};
                \node (v12) at (2, -1) [fpi] {\examplefpi{\examplee}{\exampleg}{\{\examplee, \examplef\}}};
                \node (v13) at (6, -1) [fpi] {\examplefpi{\exampleg}{\examplei}{\{\exampleg, \exampleh\}}};

                \node (v20) at (-6, -2) [iis] {\exampleiis{\examplea}{\examplec}{\{\examplea\}}{0}};
                \node (v21) at (-2, -2) [iis] {\exampleiis{\examplec}{\examplee}{\{\examplec\}}{0}};
                \node (v22) at (2, -2) [iis] {\exampleiis{\examplee}{\exampleg}{\{\examplee\}}{0}};
                \node (v23) at (6, -2) [iis] {\exampleiis{\exampleg}{\examplei}{\{\exampleg\}}{0}};

                \node (v30) at (-6, -3) [iis] {\exampleiis{\examplea}{\examplec}{\{\exampleb\}}{1}};
                \node (v31) at (-2, -3) [iis] {\exampleiis{\examplec}{\examplee}{\{\exampled\}}{1}};
                \node (v32) at (2, -3) [iis] {\exampleiis{\examplee}{\exampleg}{\{\examplef\}}{1}};
                \node (v33) at (6, -3) [iis] {\exampleiis{\exampleg}{\examplei}{\{\exampleh\}}{1}};
		%edges
                \draw (vroot) edge[edge] (v00);
                \draw (vroot) edge[edge] (v01);

		\draw (v00) edge[edge] (v10);
		\draw (v00) edge[edge] (v11);
		\draw (v01) edge[edge] (v12);
		\draw (v01) edge[edge] (v13);

		\draw (v10) edge[edge] (v20);
		\draw (v11) edge[edge] (v21);
		\draw (v12) edge[edge] (v22);
		\draw (v13) edge[edge] (v23);

		\draw (v20) edge[edge] (v30);
		\draw (v21) edge[edge] (v31);
		\draw (v22) edge[edge] (v32);
		\draw (v23) edge[edge] (v33);
	\end{pgfonlayer}

	\begin{pgfonlayer}{background}
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](8, 1) -- (-8, 1);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](-8, -0) -- (8, -0);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](8, -1) -- (-8, -1);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](-8, -2) -- (8, -2);
		\draw[-{Triangle[width=30pt,length=17pt,color=gray]}, line width=15pt, color=gray](8, -3) -- (-8, -3);
	\end{pgfonlayer}
\end{tikzpicture}
\end{scaletikzpicturetowidth}

\caption{An example run of the protocol that requires transmitting the maximum amount of bytes. $b \defeq 2, t \defeq 1$.}

\label{fig:worst-bytes}
\end{figure*}

This worst-case complexity is no better than the naïve protocol where the two nodes simply exchange their whole item sets. The motivation behind our protocol is to utilize similarities in the sets that are being reconciled, so it makes sense to bound the communication complexity by $n_{\triangle}$.

As discussed in the analysis of the number of communication rounds, if $n_{\triangle} = 1$ this results in the protocol tracing a path from the root to a leaf in the tree. If $n_{\triangle}$ takes on larger values, more paths from the root to leaves occur, each contributing up to $2 + 2 \cdot\ceil{\log_{b}(n_{\min})} - \floor{\log_{b}(t)} \in \complexity{\log_{b}(n)}$ vertices, but the number of vertices in the union of these paths is of course bounded by the total number of vertices in the tree. We thus obtain a communication complexity of $\complexity{\min(n_{\triangle} \cdot \log_b(n), n)}$ bits.

\subsection{Impact of Randomized Range Boundaries}
\label{random-boundaries}

\Cref{why-random-boundaries} argues in favor of randomizing range boundaries to protect against the impact of maliciously crafted fingerprint collisions. We now consider three different randomization strategies and their impacts on the number of communication rounds and the number of bits that need to be transmitted. To describe the boundary selection strategies, first consider that splitting an ordered set $S = \set{s_0 \prec s_1 \prec \ldots \prec s_{n - 1}}$ items into $b$ ranges consists of choosing $b$ indices $i_0 < i_1 < \ldots < i_{b-1}$, thus defining the ranges $\range{s_{i_0}}{s_{i_1}}{S}, \range{s_{i_1}}{s_{i_2}}{S}, \ldots, \range{s_{i_{b - 2}}}{s_{i_{b - 1}}}{S}, \range{s_{i_{b - 1}}}{s_{i_0}}{S}$.

The randomization strategy with the lowest impact creates ranges of identical sizes as the regular strategy (splitting into $b$ subranges of evenly distributed size per communication round), but shifts where these boundaries lie. Precisely: first, range boundaries $i_0, i_1, \ldots, i_{b - 1}$ are computed deterministically, all of which lie $\frac{n}{b}$ apart. Then some random integer $k \in \range{0}{\frac{n}{b}}{\N}$ is chosen, and then the new boundaries are $s_{i_j + k \mod b}$ for all $0 \leq j < b$.

 As the number and size of ranges is identical to that of the deterministic protocol version, communication complexity remains unchanged. Yet there are $\frac{n}{b}$ different possible partions of a range of size $n$, thus reducing the probability that a particular maliciously crafted fingerprint collision affects a given communication round by that factor.

In order to minimize the probability that a collision affects a protocol run, range boundaries can be chosen fully at random, for a total of $\binom{n}{b}$ possible outcomes. The worst-case number of communication rounds occurs when in every round $b-1$ of the range contain exactly one item and the last range contains all the remaining items, leading to $\frac{n}{b - 1}$ rounds. The expected number of rounds is however still $\complexity{\log_b(n)}$ with high probability, as it corresponds to the height of a randomly chosen $b$-complete tree, which is expected to be in $\complexity{\log_b(n)}$~\cite{devroye1990height}.

A simple way of recovering a logarithmic worst-case round number while keeping the number of possible partitions super-linear is to choose the boundaries randomly within equally sized regions of the range: choose $i_j$ randomly from $\range{j \cdot \frac{n}{b}}{(j + 1) \cdot \frac{n}{b}}{\N}$ (and take $i_{b - 1}$ modulo $n$ if necessary). The number of possible outcomes is $(\frac{n}{b})^b \in \complexity{n^b}$. Since any range obtained this way can contain most $2b$ items, the worst-case number of communication rounds is $\complexity{\log_{\frac{b}{2}}(n)}$.

Finally note that if reconciliation is initiated by one node sending the fingerprint over all its items, no randomization is involved in this first round, which could be utilized by the malicious actor crafting fingerprint collisions. To protect against this, the first reconciliation round should already start with sending $b$ (randomized) range fingerprints. Or in other words, a node wishing to initiate reconciliation simply acts as if it had just received a range fingerprint over the whole universe not matching the fingerprint over all its items. In addition to protecting against fingerprint collisions, this also reduces the number of communication rounds by one, though at the cost of having sent an unnecessary amount of data if both nodes are already holding the same set of items.

\subsection{Computational Complexity}

We now analyze the computational cost incurred by a single communication round, i.e. computing the response to a message. This includes both fingerprint comparisons as well as locating the items to transmit. We do however assume that an auxiliary data structure is available to help with this computation, e.g. the fingerprint tree structure presented in \cref{group-fingerprints}. We exclude both space usage and maintenance cost for this data structure from the per-round complexity.

We will assume that transferring an item as part of a range item set requires $\complexity{1}$ time and space. The relevant computational overhead per communication round thus consists of computing $\fp{\range{x}{y}{X_i}}$ for every received range fingerprint $\ifp{x}{y}{X_j}$, as well as partitioning $\range{x}{y}{X_i}$ in case of a mismatch and computing the fingerprints over all subranges. These computations can be performed independently for all received range fingerprints, so in particular they can be performed sequentially, reusing space. The overall space complexity of the per-round computations is equal to that of the computations for a single range fingerprint.

We first address the problem of partitioning ranges. After the indices of the boundaries have been determined as described in the previous section, how can they be efficiently translated into actual items? This can be accomplished by augmenting the auxiliary tree structure by storing in each vertex $v$ the number of vertices in $T_v$. With this information, one can efficiently traverse the tree based on relative offsets on the number of items to traverse.

The problem thus consists of computing $k \cdot (b + 1)$ fingerprints in the worst case, where $k$ is the number of range fingerprints that have been received - one fingerprint computation per received fingerprint to compare the received fingerprint with the local one, and then $b$ further ones for splitting the range.

A naive approach is to query the auxiliary data structure for each fingerprint to compute individually. The maximum amount of queries is upper-bounded by $n$, as $n$ items cannot be split into more than $n$ non-empty ranges. The greatest possible number of range fingerprints within a single round corresponds to the number of leaves in the recursion tree, which is in $\complexity{n}$. The auxiliary data structure requires $\complexity{\log(n)}$ time per query, leading to an overall time complexity of $\complexity{n \cdot \log(n)}$.

Instead, the bulk fingerprint computation from \cref{bulk} should be used, leading to a time complexity of $\complexity{\min(k \cdot \log(\abs{S}), \abs{S})}$.

\subsection{Discussion}

Some observations beyond the pure statement of the complexities are worth pointing out.

First, the choice of branching factor $b$ and the item size $k$ at which range item sets are being sent give a lot of flexibility to the protocol. For $b = 2$ and $k = 1$ the protocol minimizes the work spent on items that lie outside the symmetric difference between the two stored sets, at the cost of more ``meta traffic'' that needs to be transmitted. In contrast, the trivial reconciliation protocol in which both nodes immediately send all their items minimizes this meta traffic by instead transmitting items whose transmission would not have been necessary. Increasing the choices of $b$ and $k$ effectively interpolates between these two extremes. They can thus be chosen based on which resource is more valuable, bandwidth or time.

The second remark is that range-based reconciliation makes good use of the ``free complexity budget'' inherent to the problem: storing a set of items in sorted order for efficient access is usually done via balanced trees, implying a storage overhead of $\complexity{n}$ and insertion/deletion time complexity of $\complexity{\log(n)}$. The auxiliary tree structure for computing hashes requires exactly the same resources (and can in fact be implemented by storing additional data in the vertices of the storage tree) and thus does not increase the overall asymptotic complexity at all. Solutions without such auxiliary data structures are in some sense wasting parts of the available complexity budget.

Intuitively, an ideal solution should have communication complexity proportional to $n_{\triangle}$. The range-based approach does not achieve this goal, but it misses it by merely a logarithmic factor. The weakest aspect is the logarithmic number of communication rounds, considering that the trivial protocol easily achieves a constant number. On the plus side, increasing $b$ and/or $k$ can counteract this up to the point where the increased bandwidth outweighs the reduced number of communication rounds.

\section{Smaller Optimizations}
\label{set-reconciliation-simple-optimizations}

We now give a list of optimizations which do not impact the overall complexity analysis, but which do improve on some constant factors.

\subsection{Non-Uniform Partitions}

When partitioning a range into subranges, the protocol does not specify where exactly to place the boundaries. Splitting into partitions of roughly equal sizes makes a lot of sense if new data could arise anywhere within the linear order with equal probability. If however the items are likely to fall within certain ranges of the order, it can be more efficient to use more fine-grained partitions within those regions. If for example items are sorted by timestamp, and new items are expected to be propagated to every node in the system within a couple of seconds, then all items older than ten seconds can be comfortably lumped together in a large range.

\subsection{Subset Checks}
\label{subset-checks}

When a node $\mathcal{X}_i$ receives a range fingerprint $\ifp{x}{y}{X_j}$, it might have a different fingerprint for that range, but one of the resulting subranges could match the received fingerprint. The node can then ignore that range, transmitting only the remaining ones.

This scenario is a special case of receiving the fingerprint of a subset of the items one holds within a range. In principle, a node can compute the fingerprints of arbitrary subsets of its range, trying to find a match. If a match is found, the node knows that it holds a superset of the items the other node holds within the range. The protocol could be extended with a message part that transmits items and does not warrant a response, this would be used to transmit $\range{x}{y}{X_i} \setminus \range{x}{y}{X_j}$.

Computing the fingerprint of all subsets of a range is infeasible since there are $2^n$ many subsets. As discussed in \cref{specific-monoids} however, if the group operation for fingerprint computation is xor, a node can check whether the fingerprint of a subset of items matches a given fingerprint in $\complexity{n^3}$.
Assuming no fingerprint collisions occurred, checking whether a subset of $\range{x}{y}{X_i}$ matches $\fp{\range{x}{y}{X_j}}$ is equivalent to checking whether $\range{x}{y}{X_i}$ is a superset of $\range{x}{y}{X_j}$.

This leads to a protocol with a per-round computational complexity of $\complexity{n^4}$\footnote{This can be brought down to $\complexity{n^3}$ by using techniques for solving multiple systems of linear equations based on the same matrix more efficiently, e.g. by precomputing a LU decomposition or a QR decomposition.} ($\complexity{n}$ fingerprint computations of complexity $\complexity{n^3}$ each) and the same worst-case guarantees as the basic protocol, but which can skip recursive steps more often than the basic protocol. In particular, whenever two nodes reconcile where one of them holds a subset of the items of the other node, reconciliation terminates after at most three communication rounds.

\subsection{Efficiently Encoding Ranges}

A naive encoding of range fingerprints and range item sets would transmit both boundaries of every range, transmitting $2n$ items if there are $n$ ranges in a message. This can be brought down to $1 + n$ by utilizing that the lower boundary of all but the first range is also the upper boundary of the preceding one. An efficiently encoded message consists of the number of ranges it contains, followed by the lower boundary of the first range, followed by pairs of range information and the upper boundary of the range that the information pertains to, where each \defined{range information} is either a fingerprint, a set of items, or a dummy value signaling that this part of the overall range is already fully synchronized.

The knowledge that range boundaries are transmitted in ascending order can be used for compression. As a simple example, if range boundaries are natural numbers, every upper boundary can be transmitted as the difference to the previous boundary. This way, smaller numbers are transmitted, which admit shorter encodings. The same argument applies for transmitting sets of items in sorted order.

Note additionally that two adjacent range item sets can be merged into a single one, saving on the transmission of the boundary between them.

\subsection{Utilizing Range Boundaries}

Whenever the lower boundary of a range is transmitted, an actual item is transmitted. If the receiving node knew whether the other node held this item, it would automatically be reconciled and could be excluded from further recursion steps. One way for achieving this is to tag each range with a bit to indicate whether the lower boundary is being held by the sender. A different approach is to require that ranges are only split at items which the splitting node holds. This way no explicit bits need to be transmitted, yet many boundaries can be identified as being held by the other node.

Recall that we assume $U$ to be finite. For $x \in U$ we can thus denote by $\successor{x}$ the necessarily unique $z \in U$ such that $x \prec z$ and there exists no $y \in U$ such that $x \prec y \prec z$ if a $z$ with $u \prec z$ exists, or $\min(U)$ otherwise. When sending or receiving a range from $x$ to $y$ for which both nodes know that the sending node holds $x$, both nodes act as if the range went from $\successor{x}$ to $y$, except that the receiving node adds $x$ to its local set of items.

\subsection{Multi-Level Fingerprints}

Since the protocol interprets equal range fingerprints as both nodes holding the same set of items within the range, but never verifies this merely probabilistic assumption, fingerprints need to be long enough to guarantee low collision probability. The longer the fingerprints, the more bits need to be transmitted however.

One can use smaller fingerprints if in case of equal fingerprints the nodes then exchange an additional fingerprint rather than immediately stopping the recursion for this range. Whenever a node receives a first-level range fingerprint that is equal to its own, it answers with the second-level fingerprint for the same range. When a node receives a second-level range fingerprint that is equal to its own, it terminates the recursion. If it is not equal, it recurses as usual, in particular it sends first-level fingerprints for any large subranges.

This scheme can of course be extended to an arbitrary number of fingerprint levels. Every level increases the worst-case number of roundtrips by one, but decreases the average message size. If cryptographically secure fingerprints are desired, low levels of the fingerprint hierarchy do not have to be secure, only the top level fingerprint needs to be so. Alternatively, each fingerprint level can consist of some substring of a long, cryptographically secure fingerprint, such that the concatenation of these substrings yields the original fingerprint.

\section{Reconciling Hash Graphs}
\label{set-reconciliation-graphs}

In this section, we demonstrate how to apply the set reconciliation protocol the problem of reconciling hash graphs. Hash graphs arise in contexts where pieces of data refer to other pieces of data by a secure hash computed over the data to be referred to. Distributed version control systems such as git~\cite{chacon2014pro} for example represent the evolving contents of a directory as a set of deltas which describe how the contents changed from an earlier version, this earlier version is referenced as the hash of another such object. Some objects describe how to merge conflicting concurrent changes, these objects can reference multiple other objects. Since addressing uses a secure hash function, an object can only reference objects which existed prior to it. All in all, objects can thus form arbitrary directed, acyclic graphs (dags).

When two nodes wish to synchronize, they update each other's histories to the union of all objects known to both of them. This is a natural setting for the set reconciliation protocol. Since objects can become arbitrarily large, one would reconcile sets of hashes of these objects, and then in a second stage request the actual objects for all newly obtained hashes.

This approach completely ignores the edges of the dag. While at first glance it might seem inefficient to ignore available structural information, the fact that the dags can take arbitrary shapes makes it hard to utilize the edge structure. The graphs might be dense or sparse, could contain arbitrarily large independent sets, paths, tournaments etc.

Even though arbitrary dags are possible and thus the logarithmically bounded worst-case complexity is important, one can in practice make some reasonable assumptions about the hash graphs arising from an average, version-controlled repository. It seems likely for example that most concurrent new work is performed relative to a rather recent state of the repository, whereas work based off a very old state is rather unlikely. We could hope for better average reconciliation times if the reconciliation algorithm leveraged this expectation.

To that end, rather than reconciling merely hashes of objects, we can reconcile pairs $(\mathit{depth}, \mathit{hash})$, sorting by depth first and hash second. The \defined{depth} of an object is the length of the longest path from that object to a root object, i.e., an object without predecessors. That is, the depth of an object is $1$ greater than the greatest depth of any predecessor object. If most concurrent work is based off similarly new state, then it also falls into a similar range of the linear order. A protocol run does not need to recurse into ranges of low depth then, since no concurrently working peer produces new objects of low depth. If this assumption turns out to be wrong, the protocol nevertheless upholds its worst-case guarantee of a logarithmic number of communication rounds.

Another common scenario is that a node has not produced any new local objects and merely wants to catch up with the current state. Let $d$ be the maximal depth among all objects the node holds. Then rather than reconciling the range $\range{(0, 0)}{(0, 0)}{}$, the node can reconcile the range $\range{(0, 0)}{(d + 1, 0)}{}$ as well as sending an empty range item set for the range $\range{(d + 1, 0)}{(0, 0)}{}$. All new changes of depth $d + 1$ or greater are then fetched in only two communication rounds. Any concurrent work (from a depth perspective) is reconciled as usual, and if all new changes are based off a recent version, the whole reconciliation process only takes a single round trip.


\chapter{Background}
\label{ch:background}

This chapter provides the background knowledge relevant for the thesis work. It
will present concepts of Computational Complexity
(\autoref{sec:computational_complexity_and_approximability}) and \acrlong{LP}
(\autoref{sec:linear_and_mixed_integer_programming}) as well as graph density problems (\autoref{sec:signed_graphs_and_density}) which are significant in the
following used methodologies.

\section{Computational Complexity and \\Approximability}%
\label{sec:computational_complexity_and_approximability}

Complexity Theory deals with the study of the intrinsic complexity of
computational problems. It also elaborates on the relationships between
the complexity of different problems, for example proving that two problems are
computationally equivalent \cite{9780521884730}, through a notion called
\emph{reduction}.

\subsection{Optimization Problems and $\mathcal{NPO} $}%
\label{sub:optimization_problems}

Optimization problems are defined from a problem instance $x$, a set of
feasible solutions $S$ and a cost function that takes as input the problem
instance $x$ and a feasible solution $s \in S$, denoted as cost$_{O} (x, s) $.
Given a minimization (maximization) problem the optimal solution is defined as
the $s$ minimizing (maximizing) the value of cost$_{O} (x, s)$, and we denote
this value by opt$_{O} (x) $\cite{Trevisan2004}.

\paragraph{$\mathcal{NPO} $}%
\label{par:_npo_}

is then the set of optimization problems with the following properties:
\begin{itemize}
	\item instances $x$ can be recognized in polynomial time;
	\item cost$_{O} (x, s)$ can be computed in polynomial time for $s \in S$;
	\item it takes polynomial time to decide if solution $r$ of the instance
	      $x$ is feasible, i.e.\ whether $r \in S$;
	\item for every instance of the problem $x$
	      and feasible solution for that problem $s \in S$ there is a
	      polynomial $q \; s.t.  \; |s| \leq q(|x|)$ (i.e. the size of every
	      solution is bounded by a polynomial in $x$).
\end{itemize}

If $\mathcal{P} \neq \mathcal{NP} $ for many optimization problems there is no
algorithm for finding the optimal solution in polynomial time
\cite{Trevisan2004}. This is again a fundamental limitation about what we can
compute which then requires the definition of some alternative approaches, like
\emph{approximation algorithms}
which in polynomial time
compute a solution which lies in a given factor from the optimal
one \cite{Vazirani2002}.

\paragraph{Approximation.}%
\label{par:r_approximations}

$A$ is an r-approximation algorithm for an $\mathcal{NPO} $ minimization
problem $O$ if, for every instance $x$ of $O$ it holds that
\begin{equation*}
	cost_{O} (x, A(x)) \leq r \cdot opt_{O} (x)
\end{equation*}

\noindent
(or, respectively, cost$_{O} (x, A(x))
	\leq 1/r \cdot $opt$_{O} (x) $ for maximization problems), $A(x)$ being the
optimal solution found by the approximation algorithm \cite{Trevisan2004}.

\subsection{Approximation Preserving Reductions}%
\label{sub:approximation_preserving_reductions}

If $\mathcal{P} \neq \mathcal{NP} $ the approximability of problems varies
widely: while for some of them there exist
constant factor approximations, for some others even a remotely approximate
solution cannot be found \cite{Ausiello2005} (some examples are listed in
\autoref{tab:inapproximability-examples}).

\begin{table}
	\centering
	\caption[Examples of inapproximability]{Examples of known inapproximability results, assuming $\mathcal{P}
			\neq \mathcal{NP} $ \cite{10.1007/3-540-63248-4_10} }
	\label{tab:inapproximability-examples}
	\begin{tabular}{c|p{5cm}|c}
		Problem                                                          & Description               & Inapproximability \\
		\hline
		$ \textsc{MaxClique} $                                           & Biggest complete subgraph & $|V|^{1-
		\epsilon}, \epsilon > 0 $                                                                                \\
		                                                                 &                           &                   \\
		$ \textsc{MaximumIndipendentSet} $                               &
		Biggest set of not connected nodes                               & $|V|^{1-
		\epsilon}, \epsilon > 0 $                                                                                \\
		                                                                 &                           &                   \\
		$ \textsc{MaxCut} $                                              &
		Partition of nodes in two sets $V_1$ and $V_2$ minimizing the number of edges
		between the 2 sets                                               & 1.0624                                        \\
		                                                                 &                           &                   \\
		$ \textsc{MaximumSetPacking} $                                   & Given a collection of
		finite sets $C$,
		finding the biggest collection $C' \subseteq C$ of disjoint sets & $|C|^{1-
		\epsilon}, \epsilon > 0 $                                                                                \\
	\end{tabular}
\end{table}

\emph{Approximation preserving reductions} are a fundamental notion for proving
a partial order among optimization problems \cite{Ausiello2005}. Given a
function $f$ mapping instances of $A$ to $B$ and a function $g$ mapping
solutions of $B$ to solutions of $A$, an
approximation preserving reduction must have
the following properties (when reducing from a problem $A$ to a problem $B$)
\cite{DemaineFall2014}:

\begin{itemize}
	\item any instance $x$ of $A$ should be mapped to an instance $x' = f(x)$
	      of $B$ in polynomial time,
	\item any solution $y' \in $ sol$(f(x))$ of $B$ should be associated to a corresponding
	      solution $y = g(x, y') \in $ sol$(x)$ of $A$ in polynomial time.
\end{itemize}

The process is illustrated in \autoref{fig:tex/img/reduction_scheme}.

\begin{figure}
	\centering
	\includegraphics[width=0.6\linewidth]{tex/img/reduction_scheme.png}
	\caption[Reduction process]{The reduction scheme \cite{Crescenzi1997ASG}.}%
	\label{fig:tex/img/reduction_scheme}
\end{figure}

There are at least nine different kinds of approximation preserving reductions
\cite{DemaineFall2014}(\autoref{fig:tex/img/approximation_preserving_reductions}) but we will
focus only on one type.

\begin{figure}[b]
	\centering
	\includegraphics[width=0.4\linewidth]{tex/img/approximation_preserving_reductions.png}
	\caption[Taxonomy of approximation preserving reductions]{Taxonomy of
		approximation preserving reductions \cite{Crescenzi1997ASG}.}%
	\label{fig:tex/img/approximation_preserving_reductions}
\end{figure}

\subsubsection{S Reductions}%
\label{sub:strict_reductions}

An $S$ \emph{reduction} from problem $A$ to problem $B$ has the following properties \cite{Crescenzi1997ASG}:
\begin{itemize}
	\item for any instance $x$ of problem $A$ it holds that opt$_{A} (x) = $
	      opt$_{B} (f(x))$,
	\item for any instance $x$ of $A$ and solution $y'$ of $B$, cost$_{A} (x,
		      g(x, y')) = $ cost$_{B} (f(x), y')$.
\end{itemize}

$S$ reductions are the strongest type of \emph{approximation preserving
	reductions} and imply all the others \cite{Crescenzi1997ASG}.

\section{Linear and Mixed Integer Programming}%
\label{sec:linear_and_mixed_integer_programming}

\acrfull{LP} is a widely used optimization technique and one of the most
effective; the term refers to problems in which both the constraints and \emph{objective
	function} are linear
\cite{Edgar2001,Vanderbei2008,Dantzig1998,Martin1998}. \acrshort{LP}s are solvable in polynomial time \cite{KHACHIYAN198053,Karmarkar1984}.

\subsection{The Structure of \acrshort{LP}s}%
\label{sub:the_structure_of_a_linear_programming_model}

In a \acrshort{LP} problem we are given a vector $ \mathbf{c} = (c_1,
	\dots, c_n) $ and we want to maximize (or minimize) a linear function over
the variables $ \mathbf{x} = (x_1, \dots, x_n) $ with the coefficients of the
vector $ \mathbf{c} $, i.e.
\begin{equation*}
	\mathbf{cx} = \sum^{n}_{i=1} c_i x_i
\end{equation*}
(known as the \emph{objective function}) while satisfying some linear
constraints over the variables \cite{Bertsimas1997,Vanderbei2008}:

\begin{equation*}
	a_1 x_1 + \dots + a_n x_n \begin{Bmatrix} \leq \\ = \\ \geq \end{Bmatrix} b
\end{equation*}

% There is no \emp{a priori} preference regarding how the problem is formulated
% since it is easy to convert an inequality into a equality constraint through
% additional variables ($\omega $ in the example below) called \emph{slack}
% \cite{Vanderbei2008}. For example one in the form
% \begin{equation*}
%     a_1 x_1 + \dots + a_n x_n \leq b
% \end{equation*}
% is equivalent to
% \begin{equation*}
%     a_1 x_1 + \dots + a_n x_n + \omega = b, \quad \omega \geq 0
% \end{equation*}
% and viceversa an equality
% \begin{equation*}
%     a_1 x_1 + \dots + a_n x_n = b
% \end{equation*}
% can be converted into
% \begin{align*}
%     a_1 x_1 + \dots + a_n x_n \leq b \\
%     a_1 x_1 + \dots + a_n x_n \geq b
% \end{align*}
%
In general it is possible to formulate any \acrshort{LP} problem as follows (called \emph{standard form}) \cite{Vanderbei2008}

\begin{alignat}{3}
	\label{eq:standard-form}
	\text{maximize}   &       & \sum_{i=1}^{n} c_{i}x_{i}                                          \\
	\text{subject to} & \quad & \sum_{i=1}^{n} a_{1i}  x_{i} & \leq b_{1} &                        \\
	                  &       & \vdots                                                             \\
	                  &       & \sum_{i=1}^{n} a_{mi}  x_{i} & \leq b_{m} &                        \\
	                  &       & x_{i}                        & \geq 0,    & \quad i & =1 ,\dots, n
\end{alignat}

The $x_i$ are known also as \emph{decision variables}; a choice of $ \mathbf{x}
$ is called \emph{solution} and \emph{feasible solution} if it satisfies the
constraints, \emph{optimum} if it is \emph{feasible} and maximizes the \emph{objective function}
\cite{Vanderbei2008}.

\subsection{Solving an LP Problem}%
\label{sub:solving_an_lp_problem}

An option for solving an \acrshort{LP} is called the \emph{simplex
	method} which has two different phases.

Starting from the \emph{standard form} \eqref{eq:standard-form}
\emph{slack} variables $x_{n+1}, \dots, $ $x_{n+m} $ are introduced, allowing to express the problem as follows
\cite{Vanderbei2008,Edgar2001}:

\begin{alignat}{3}
	\label{eq:standard-form}
	\text{maximize}   \quad & \sum_{i=1}^{n} c_{i}x_{i}                                                                   \\
	\text{subject to} \quad & x_{n+1}                   = b_{1} - \sum_{i=1}^{n} a_{1i}  x_{i} &                          \\
	                        & \vdots                                                                                \\
	                        & x_{n+m}                   = b_{m} - \sum_{i=1}^{n} a_{mi}  x_{i} &                          \\
	                        & x_{i}                     \geq 0,                                & \quad i & =1 ,\dots, n+m
\end{alignat}

The first phase involves finding a feasible solution for the problem. More
specifically, we look for $m$ variables, called \emph{basic variables}, whose
value we choose in order to satisfy the $m$ equality constraints (while the
remaining variables, the \emph{nonbasic} ones, are set to 0); if no such
feasible solution exists then the problem is \emph{unfeasible}. Let $\mathcal{B}
$ be the set of \emph{basic variables}, $\mathcal{N} $ the set of
\emph{nonbasic variables} \cite{Vanderbei2008,Bertsimas1997} and
$\bar{\zeta}$ the value of the objective function associated to this feasible
solution. Then the
problem can be reformulated as follows:

\begin{alignat}{3}
	\label{eq:standard-form-simplex-new}
	\text{maximize} \quad     & \bar{\zeta} + \sum_{j \in \mathcal{N} }^{n} c_{j}x_{j}                                           \\
	\text{subject to}   \quad & x_{i} = \bar{b}_{i} - \sum_{j \in \mathcal{N} }^{n} \bar{a}_{ij} x_{j} & \quad i \in \mathcal{B} \\
	                          & x_{i} \geq 0,                                                          & \quad i =1 ,\dots, n+m
\end{alignat}

The second phase of the \emph{simplex method} aims at improving the current
solution: if $c_{j} \geq 0$ for all $j \in \mathcal{N} $, then the value of the
objective function cannot be increased and we found an optimum. If, instead,
there is at least one $c_{j}
	> 0$ then we can increase the value of $\zeta$ by increasing $x_{j} $; now
there are two different cases \cite{Vanderbei2008}:
\begin{itemize}
	\item As $x_{j} $ increases there is at least a variable $\tilde{x}_{j} $
	      whose value needs to decrease to satisfy equality constraints. The first
	      of these variables $\tilde{x}_{j} $ reaching $0$ moves from
	      $\mathcal{B} $ to $\mathcal{N} $, while
	      $x_{j} $ moves from $\mathcal{N} $ to $\mathcal{B} $. The problem is reformulated again as in
	      \eqref{eq:standard-form-simplex-new} and the process is repeated
	      \cite{Vanderbei2008}.
	\item If no such $\tilde{x}_{j} $ variable exists then the value of $x_{j}
	      $ can be increased indefinitely and the problem is said to be
	      \emph{unbounded}, i.e.\ it can achieve any arbitrarily large value.
\end{itemize}

The process is illustrated with an example in \autoref{fig:tex/img/simplex}.

\begin{figure}
	\centering
	\includegraphics[width=0.4\linewidth]{tex/img/simplex.png}
	\caption[Simplex method progress]{An example of the progress of the \emph{simplex method}: the
		process moves along the vertices of the polygon defined by the
		constraints while improving the value of the solution. Picture taken
		from \cite{BerndGaertner2006}.}%
	\label{fig:tex/img/simplex}
\end{figure}

\subsection{Mixed Integer Programming}%
\label{sub:mixed_integer_programming}

Many problems involve not only continuous variables but also variables that take binary or
integer values: these are known as \acrfull{MIP} problems. Furthermore, some of
these problems are linear in the constraints and in the objective function and are
known as \acrfull{MILP} problems
\cite{Edgar2001,Wolsey1998}.

A generic \acrshort{MILP} can be expressed as follows \cite{Conforti2016}:

\begin{alignat}{3}
	\label{eq:standard-form-milp}
	\begin{aligned}[t]
		\text{maximize}   &                                     & \sum_{i=1}^{n_{1} } c_{i}x_{i} +
		\sum_{i=1}^{n_{2} } h_{i}y_{i}                                                                                \\
		\text{subject to} & \quad                               & \sum_{i=1}^{n_{1} } a_{1i}  x_{i} + \sum_{i=1}^{n_{2} } g_{1i}  y_{i} & \leq b_{1}       &         \\
		                  &                                     & \vdots                                                                                \\
		                  &                                     & \sum_{i=1}^{n_{1} } a_{mi}  x_{i} + \sum_{i=1}^{n_{2} } g_{1i}  y_{i} & \leq b_{m}       &         \\
		% &       & \sum_{i=1}^{n} a_{mi}  x_{i}                                          & \leq b_{m} &                        \\
		                  &                                     & x_{i}
		                  & \geq 0,                             & \quad i                                                               & =1 ,\dots, n_{1}           \\
		                  &                                     & y_{i}                                                                 & \geq 0,          & \quad i
		                  & =1 ,\dots, n_{2} \; \text{integral}
	\end{aligned}
\end{alignat}

For convenience, we will refer to \acrshort{MILP} problems as \acrshort{MIP} in
the rest of the document.

The \emph{relaxation} of a \acrshort{MIP} problem is defined as the same
problem where the integrality constraints have been removed \cite{Edgar2001}.

Solving a \acrshort{MIP} is a difficult task in general, differently from the
\acrshort{LP} problems. It has been shown also that \acrshort{MIP} is
$\mathcal{NP}$-\textbf{Hard}
\cite{Kannan1978,Liberti2019,Schrijver1998}. This is why the \emph{relaxation} is often considered
for getting an approximation of the exact solution and it can be solved in
polynomial time \cite{Conforti2016}.

\subsection{Solving a MIP}%
\label{sub:solving_a_mip}

One approach that has been proven successful for solving \acrshort{MIP} is the
Branch-and-Bound, which is guaranteed to find an optimal solution
\cite{Conforti2016,Edgar2001}.

Given a problem $P$, the process starts by solving the \emph{relaxation} of $P$
and finding its optimal solution $(\tilde{x}, \tilde{y})$.
Let $S$ and  $\tilde{S}$ be the set of feasible solutions
for the original problem and its relaxation, respectively. By definition,
we have that $S \subseteq \tilde{S} $.
Therefore, \cite{Edgar2001}
\begin{itemize}
	\item If the relaxation problem is not feasible so will be the original
	      problem.
	\item If $\tilde{y}$ has only integer values then we found the optimal
	      solution for the original problem.
	\item If, instead, $\tilde{y}$ contains some fractional values, we start by
	      initializing the value of the best solution so far, $\zeta$, with
	      $-\infty$.  Then we choose one of the fractional variables that are
	      required to be integral in the original problem, say $y_j$ with
	      value $f$, and create two subproblems, respectively adding the
	      constraint $y_{j} \leq \lfloor f \rfloor$ and $y_{j} \geq \lceil f
		      \rceil$. This step is called \emph{branching}. We now consider the
	      solution of each subproblem $(x_j, y_j)$ with value of the objective
	      function $z_{j} $ \cite{Edgar2001,Conforti2016}.

	      \begin{itemize}
		      \item If either of the subproblems is not feasible or its value
		            $z_{j} $ is lower than the best one found so far then it does
		            not need to be considered further. This is called \emph{pruning}.
		      \item If $y_{j} $ are all integer values then $\zeta =
			            z_{j} $.
		      \item Otherwise, we subdivide again in two subproblems as
		            above.
	      \end{itemize}
\end{itemize}

When there are no remaining subproblems to consider then Branch-and-Bound
terminates \cite{Edgar2001}.

\section{Density in Graphs}%
\label{sec:signed_graphs_and_density}

\subsection{The Densest Subgraph Problem}%
\label{sub:densest_subgraphs}

Finding dense subgraphs is a problem which has received a lot of attention and
different definitions of density have been used
\cite{charikar2000greedy,asahiro1995finding,asahiro2000greedily,feige1997densest}.

We will refer to the definition in \cite{charikar2000greedy} and present some
of its results which are used and important for the development of the methods
in the following chapters.

\medskip

Let $G = (V, E)$ be an undirected graph, let $S \subseteq V$ a subset of the
nodes, and let $E(S)$ denote the edges of $G$ induced by $S$, i.e.

\begin{equation*}
	E(S) = \{e_{ij} \in E \; s.t. \; v_i \in S \land \; v_j \in S\}.
\end{equation*}
The \emph{density} $f(S)$ is defined as

\begin{equation}
	f(S) = \frac{|E(S)|}{|S|}.
\end{equation}

According to this definition it is easy to see that $2 \cdot f(S)$ is the
average degree of the subgraph induced by $S$.

The density of the graph $f(G)$ is then defined as

\begin{equation}
	f(G) = \max_{S \subseteq V} {f(S)}.
\end{equation}

The problem of computing $f(G)$ is known as the \emph{Densest Subgraph Problem}
\cite{charikar2000greedy}.

There are different techniques for solving it: a solution based on parametric
maximum flow has been proposed in \cite{Gallo1989}; Charikar in
\cite{charikar2000greedy} proposed an alternative solution based on the
following \acrlong{LP} model (a more in-depth discussion about \acrshort{LP}
can be found in \autoref{sec:linear_and_mixed_integer_programming})

\begin{alignat}{3}
	\label{eq:charikar-model-densest-subgraph}
	\text{maximize}   &       & \sum_{ij \in E} x_{ij}                                          \\
	\text{subject to} & \quad & x_{ij}                  & \leq y_{i} & \quad \forall ij & \in E \\
	                  &       & x_{ij}                  & \leq y_{j} & \quad \forall ij & \in E \\
	\label{eq:charikar-sum}
	                  &       & \sum^{}_{i \in V} y_{i} & \leq 1     &                          \\
	                  &       & y_{i}                   & \geq 0     & \quad \forall i  & \in V \\
	                  &       & x_{ij}                  & \geq 0     & \quad \forall ij & \in E \\
\end{alignat}

Intuitively, the problem associates non-zero $y_i$ to vertices in $S \subseteq V$
and non-zero $x_{ij}$ to edges induced by $S$. The concept of "density" is
introduced by \eqref{eq:charikar-sum}, which distributes a fixed quantity
(one in this case) to the vertices, such that the value $y_i$ of each vertex
generally decreases (and consequently also that of the $x_{ij}$) as the number of non-zero $y_i$ increases.

Let $S(r) \coloneqq \{v_{i} : y_{i} \geq r\} $ and $E(r) \coloneqq \{e_{ij} :
	x_{ij} \geq r\} $. It is easy to see that, given the model as defined
above, $E(r)$ is the set of edges induced by the vertices in $S(r)$.

The set of vertices $S$ maximizing the density $f(S)$ can then be reconstructed
from the results of the \acrshort{LP} by finding the density of $S(r)$
for all choices of $r = y_{i}, \; i \in V $ \cite{charikar2000greedy}.

\paragraph{An approximate algorithm for the Densest Subgraph Problem.}%
\label{par:an_approximate_algorithm_for_the_densest_subgraph_problem}

In \cite{charikar2000greedy}, Charikar also defines a greedy approach for
solving the Densest Subgraph problem which gives a $2$-approximation for
$f(G)$.

The algorithm starts by defining a set of vertices $S$ which is initialized
with $V$ and, through the iterations, it removes from $S$ the vertex $v_i$
which has the lowest degree in the subgraph induced by $S$, until $S$ is empty.
Then it returns the set $S$ which, during the process, was associated with the
highest density $f(S)$.

\subsection{The Densest Common Subgraph Problem}%
\label{sub:the_common_densest_subgraph_problem}

The \acrfull{DCS} Problem was initially introduced by Jethava and Beerenwinke
in \cite{jethava2015finding}
and later studied in \cite{andersson2016finding}, \cite{charikar2018finding}
and \cite{semertzidis2019finding} that also introduced new variants.

\begin{figure}
	\begin{center}
		\begin{subfigure}[b]{0.3\textwidth}
			\centering
			\tikzfig{tex/tikz/graph_history1}
			\caption{$G_1$}
			\label{fig:graph_sequence_example1}
		\end{subfigure}
		\begin{subfigure}[b]{0.3\textwidth}
			\centering
			\tikzfig{tex/tikz/graph_history2}
			\caption{$G_2$}
			\label{fig:graph_sequence_example2}
		\end{subfigure}
		\begin{subfigure}[b]{0.3\textwidth}
			\centering
			\tikzfig{tex/tikz/graph_history3}
			\caption{$G_3$}
			\label{fig:graph_sequence_example3}
		\end{subfigure}
	\end{center}
	\caption[Example sequence graph]{An example of a graph sequence
		$\mathcal{G} = (G_1, G_2, G_3) $ over four vertices.}
	\label{fig:graph_sequence_example}
\end{figure}

Let $\mathcal{G} = (G_1, G_2, \dots, G_T) $ be a sequence of graphs on the same
set of vertices $V$ (an example is shown in
\autoref{fig:graph_sequence_example}), $S \subseteq V$ a
subset of the nodes, $G_i[S]$ the subgraph induced by $S$ in $G_i$,
$\text{deg}_{G_i[S]} (v_{j} )$ the degree of $v_{j} \in S$ in $G_i[S]$ and
$\text{min-deg}(G_i[S])$ the minimum induced degree, i.e.
$\text{min-deg}(G_i[S]) \coloneqq \min _{v_{j}  \in S} \text{deg} _{G_i[S]}
	(v_{j}) $.

Solving the \acrlong{DCS} Problem means finding a subset
of the vertices $S \subseteq V$ that maximizes some aggregate density function
over the graph sequence. More formally, let $g$ be a
function that calculates the density of a set of nodes $S$ in a undirected
graph $G$, i.e.\

\begin{equation*}
	g: S \times G \rightarrow \mathbb{R}.
\end{equation*}

Also, let $h$ be a function aggregating the value of the density of
each snapshot of the graph sequence, i.e.\

\begin{equation*}
	h: g(S, G_1) \times g(S, G_2) \times \dots \times g(S, G_T) \rightarrow
	\mathbb{R}.
\end{equation*}

Then, the \acrshort{DCS} problem consists in finding $S$ maximizing the
following quantity:
\begin{equation*}
	f(S, \mathcal{G} ) = h(\{g(S, G_1 ), \; \dots, \; g(S, G_T) \}).
\end{equation*}

We call $f$ the \emph{density aggregation function}. According to the
definition of $f$ there are different variants
of the problem

\begin{itemize}
	\item \acrshort{DCS}-MM maximizes the Minimum of the Minimum degrees along
	      the graph sequence, i.e.
	      \begin{align}
		      \label{eq:dcs-mm}
		      g & = \text{min-deg} (G_i[S]), & f & = \min_{i \in [T]} \text{min-deg} (G_i[S]).
	      \end{align}
	      A non-trivial solution means finding a set of nodes which are linked
	      in all the graphs $G_i \in \mathcal{G} $.
	      In \cite{semertzidis2019finding} it is shown a simple greedy approach
	      for finding the solution in polynomial time.
	\item \acrshort{DCS}-MA uses the following functions
	      \begin{align}
		      \label{eq:dcs-ma}
		      g        & = \frac{\sum^{}_{v_{j} \in S } \text{deg}_{G_i[S]} (v_{j}
		      )}{|S|}, & f                                                         & = \min_{i \in [T]}
		      \frac{\sum^{}_{v_{j} \in S } \text{deg}_{G_i[S]} (v_{j} )}{|S|}.
	      \end{align}
	      This means finding a set of vertices $S$ which are dense in the sense
	      that they have a non-trivial average degree in all the graphs of the
	      sequence.

	      Charikar, Naamad and Yu in \cite{charikar2018finding} and
	      Semertzidis, Pitoura, Terzi, Tsaparas in
	      \cite{semertzidis2019finding} provide some
	      approximation algorithms with guaranteed bounds;
	      in \cite{charikar2018finding} also they prove its
	      inapproximability to within a $\mathcal{O}(2 ^{\log^{1-\epsilon} n} )
	      $ factor unless $\mathcal{NP} \subseteq \mathbf{DTIME} (n
		      ^{\text{poly}\log n} ) $, $\epsilon > 0$
	      \footnote{They show this results through a reduction from
	      $\textsc{MinRep}$, which has been shown to have the mentioned
	      inapproximability \cite{charikar2018finding,kortsarz2001hardness}.

	      $ \mathbf{DTIME}(f(n)) $ refers to the class of problems that
	      have time complexity $f(n)$ \cite{9780521884730}.
	      }.
	\item \acrshort{DCS}-AM, whose density aggregation function is

	      \begin{equation}
		      \label{eq:dcs-am}
		      f = \sum^{}_{i \in [T]} \text{min-deg} (G_i [S]),
	      \end{equation}
	      while $g$ is the same as in \eqref{eq:dcs-mm}. This choice will push
	      the algorithms to find a set of vertices which have degree greater
	      than $0$ in some of the subgraphs they induce in the graph sequence.

	      Charikar, Naamad and Yu proved in \cite{charikar2018finding}
	      that \acrshort{DCS}-AM is inapproximable within factor $n^{1-\epsilon} $ unless
	      $\mathcal{P} = \mathcal{NP}  $, $\epsilon > 0$
	      \footnote{By reducing from the
		      $\textsc{MaximumIndipendentSet}$ problem.}. For fixed $T$, they also provide a fixed
	      parameter polynomial time algorithm which can be used for solving
	      this problem exactly, as well as a $(1+\epsilon)$-approximation
	      algorithm.
	\item \acrshort{DCS}-AA maximizing

	      \begin{equation}
		      \label{eq:dcs-aa}
		      f = \sum^{}_{i \in [T]} \frac{\sum^{}_{v_{j} \in S} \text{deg}_{G_i[S]}
		      (v_{j} )}{|S|}
	      \end{equation}

	      which puts fewer restrictions than the previous variants on the
	      solutions, requiring only a high average degree on the union of the
	      graphs. Note that $g$ is the same as in \eqref{eq:dcs-ma}.

	      This problem can be solved optimally in polynomial time as it can be
	      reduced to the classical Densest Subgraph problem
	      (\autoref{sub:densest_subgraphs}) \cite{semertzidis2019finding};
	      similarly the approximation algorithm in
	      \autoref{sub:densest_subgraphs}
	      provides a 2-approximation for the optimal solution.

	      More specifically, solving \acrshort{DCS}-AA is the equivalent of solving
	      the \acrfull{DCS} on the average graph $\hat{H}_{\mathcal{G} }  $,
	      which is defined as a weighted graph whose weight of each edge is the
	      fraction of graphs in the sequence $\mathcal{G} $ where the edge is
	      present \cite{semertzidis2019finding}.
\end{itemize}

\subsection{The $\textsc{O}^{2} \textsc{Bff}$ Problem}%
\label{sub:the_o_2_bff_problem}

The \acrshort{DCS} is also known as the \acrfull{BFF} Problem as defined by Semertzidis, Pitoura, Terzi, Tsaparas
\cite{semertzidis2019finding}; in the same paper also another class of similar
problem is defined, the \acrlong{O2BFF} Problem.

Let $\mathcal{G} = (G_1, G_2, \dots, G_T) $ be a sequence of graphs on the same
vertex set $V$. The \acrfull{O2BFF} is defined as the set of
vertices $S \subseteq V$ and the set of $k$ graphs $\mathcal{L}_{k} \subseteq
	\mathcal{G}  $ that maximize some density aggregation function $f(S,
	\mathcal{L}_{k}) $.

As this problem relies again on a function $f$ which aggregates the density
across the graphs $G_i$, similarly to the \acrshort{DCS} Problem four variants
can be defined, using the same functions mentioned in
\autoref{sub:the_common_densest_subgraph_problem}.

We will focus and present only an algorithm for approximating
\acrshort{O2BFF}-AM; the other algorithms can be found in
\cite{semertzidis2019finding}.

Let us first define $\textsc{Score}_{a}  $, a procedure that removes the node
with the lowest degree in a graph while properly updating the degree of the
other nodes. When called for the first time, this
function initializes $\hat{H}_{\mathcal{G} }$, $\hat{E}$ and $\mathcal{F}[d] $,
as described in \autoref{alg:score_a}, that are updated during the subsequent
calls to the function.

Note that we refer to the degree $d$ of a vertex $v_{i} $ in a weighted graph as
the sum of the weights of the edges of the node, i.e.\ $ d_{v_i} = \sum^{}_{(v_{i} , v_{j}
	) \in E} w_{ij}  $.

\begin{algorithm}
	\SetAlgoLined
	\KwResult{The vertex with the lowest degree is removed and returned}
	$\hat{H}_{\mathcal{G} } \leftarrow $ average graph of $\mathcal{G} $ \;
	$\hat{E} \leftarrow $ set of edges of $\hat{H}_{\mathcal{G} }  $ \;
	$\mathcal{F}[d] \leftarrow $ set of nodes with degree
	$d$ in $\hat{H}_{\mathcal{G} }  $\;

	\bigskip

	\SetKwBlock{Function}
	{function \textnormal{$\textsc{ScoreAndUpdate}( \mathcal{G} )$ \{ }}
	{}

	\Function {
		$score_{a} \leftarrow $ smallest $d$ s.t. $\mathcal{F}[d] \neq
			\emptyset$\;
		$u \leftarrow$ a node with degree $d$ \;

		remove $u$ from $\mathcal{F} [d]$ \;

		\bigskip
		\ForEach{$(u, v) \in \hat{E}$}{
			remove $v$ from $\mathcal{F}[d_{v} ] $ \;
			remove $(u, v)$ from $\hat{E}$ and update $d_v$ \;
			add $v$ to $\mathcal{F}[d_{v} ] $ \;
		}

		$V = V \setminus \{ u \}$ \;
		\Return $u$ \;
	}
	\}
	\caption{The $\textsc{Score}_{a}  $ algorithm}
	\label{alg:score_a}
\end{algorithm}

Let us also define $\textsc{FindBff}_{a} $, a greedy approach for finding
the subgraph with the highest density $f$ (which corresponds to
\eqref{eq:dcs-am} in our case, but it can be replaced by any of the other
density aggregation functions). This function repeatedly calls
$\textsc{ScoreAndUpdate}$ to remove the node with the lowest degree and
efficiently update
the graph. When the graph is empty it returns the subset of nodes
that obtained the highest density score~$f$ (\autoref{alg:findbff_a}).

\begin{algorithm}
	\SetAlgoLined
	\KwResult{A subset of nodes $S \subseteq V$}
	$S_{0} = V$ \;

	\For{$i \in \{ 1, \dots, |V|\}$ }{
		$v_{i} $ = $\textsc{ScoreAndUpdate}(\mathcal{G}[S_{i} ]) $ \;
		$S_{i} = S_{i -1} \setminus \{v_{i}\} $ \;
	}

	\Return arg$\max _{i \in \{ 1, \dots, |V|\}} f(S_{i}, \mathcal{G})  $
	\caption{The $\textsc{FindBff}_{a} $ algorithm}
	\label{alg:findbff_a}
\end{algorithm}

Semertzidis, Pitoura, Terzi, Tsaparas in \cite{semertzidis2019finding} present
two different approaches for approximating \acrshort{O2BFF}-AM: an \emph{iterative} one
which starts with a set $\mathcal{L} _{k} \in \mathcal{G} $ of $k$ graphs and improves it to
increase the score, and an \emph{incremental} one in which the set of $k$
graphs $\mathcal{L}_{k} $ is selected along the $k$ iterations, starting with a
pair and adding snapshots $G \in \mathcal{G} $ one by one.

Furthermore, they identify two possible approaches for each of them. We will
focus on the \acrfull{INCO}.

The algorithm starts by solving the \acrshort{DCS} problem on each of the
graphs $G_{i} $ in $\mathcal{G} $ and finding the corresponding set of vertices
$S_{i} $ of the solution. Then $\mathcal{L}_{2}  $ is chosen as the pair of graphs which
have the most similar set of vertices in the respective solutions, where the
similarity is measured through the Jaccard coefficient.

The Jaccard coefficient is measure of similarity between two sets. More specifically, given two sets $C_1$ and $C_2$, the Jaccard coefficient is equal
to \cite{CharuC.Aggarwal2013}
\begin{equation*}
	Jaccard(C_1, C_2) = \frac{|C_1 \cap C_2|}{|C_1 \cup C_2|}.
\end{equation*}

The \acrshort{DCS} problem is now solved on $\mathcal{L} _{2} $ to obtain a set
of vertices $S_C$ which is compared against the other $S_i$ previously computed
solutions to
find the most similar one, as before, and the process continues until
$\mathcal{L}_{k} $ is constructed (\autoref{alg:inc_o_a}). Finally, the
$\textsc{FindBff}_{a} $ is called on $\mathcal{L}_{k}  $ and the resulting set of
vertices is returned along with $\mathcal{L}_{k}  $.

\begin{algorithm}
	\SetAlgoLined
	\KwResult{A subset of nodes $S \subseteq V$ and of graphs $\mathcal{L}_{k}
			\subseteq \mathcal{G}$}
	\For{$i \in \{ 1, \dots, |\mathcal{G}|\}$ }{
		$S_{i} = \textsc{FindBff}_{a} (\{ G_i \})$ \;
	}
	$\mathcal{L}_{2} = \text{arg}\max_{G_{i}, G_{j} \in \mathcal{G}}
		Jaccard(S_i, S_j) $ \;

	\For{$i \in \{ 3, \dots, k\}$ }{
		$S_{C} = \textsc{FindBff}_{a} (\mathcal{L}_{i-1}) $ \;

		$G_{m}  = \text{arg}\max_{G_{j} \in \mathcal{G}, \; G_j \not\in \mathcal{L}_{i-1}}
			Jaccard(S_C, S_j) $ \;

		$\mathcal{L}_{i} =  \mathcal{L}_{i-1} \cup \{G_m\}$ \;
	}

	$S = \textsc{FindBff}_{a} (\mathcal{L}_{k}) $ \;

	\Return $S, \; \mathcal{L}_{k}  $ \;
	\caption{The \acrshort{INCO} algorithm for approximating
		\acrshort{O2BFF}-AM}
	\label{alg:inc_o_a}
\end{algorithm}


\section{Coinductive Data Types}
\label{sec:coinductive-types}
\newcommand{\bisim}{\text{\texttildelow}}
% Thomas
%#########
% Hvad er en coinduktiv type? (eliminationsregler) (typeteori)
% Hvorfor coinduktive typer / hvad kan de bruges til?
% Simple eksempler
% Uortodokse eksempler (fx partiality)
% Lazy evaluation (i.e. Haskell-style) vs. distinction between coinductive and
%% inductive data

% Ny viden: Coinduktive typer
%#########
Inductive types have widespread applications across the field of computer
science in general, and functional programming in particular. Because they are
defined in terms of a finite and disjoint set of constructors, which determine
the internal structure of any element of such type, an induction
principle can be formulated for any inductive type. The implications are that
properties about functions on inductive types can be proven by structural
induction. Indeed, by the Curry-Howard
isomorphism\,\citep{Curry1934,Howard80,Wadler2014}, functions on inductive types
can be \emph{implemented} as proofs by structural induction. By nature, only
finite objects can be constructed from inductive types, since they have an
internal tree structure. As such, a leaf must be reached at some
point. Concretely, an inductively defined standard list structure must be ended
with the empty list in order to be well-defined.

Coinductive types are dual to inductive types. Intuitively, they can be defined
in terms of a finite and disjoint set of \emph{destructors}, which as a whole
determine the observable behaviour of elements of such type. Infinite objects
can be constructed from coinductive types, because they can have an internal
graph structure. This allows for the definition of circular structures, which
makes them suitable for reasoning about values that may unfold indefinitely,
e.g. processes\,\citep{Sangiorgi2011}. Coinductive types are often defined in
terms of abstract mathematics and category theory, and a formal definition of
coinductive types has been given by Jacobs and
Rutten\,\citep{Jacobs97atutorial}. We will not proceed with the formal
definition here, but instead provide a more intuitive explanation.

The duality between inductive and coinductive types are perhaps best understood
by considering how elements of such types are defined. An inductive type is
well-founded, and therefore has at least one constructor without a recursive
argument. As a consequence, an element of an inductive type can be defined by
defining its internal structure, since this structure is inherently finite. In
other words, such an element is defined \emph{by construction}. A coinductive type is not
necessarily well-founded. In particular, if we try to define an element of a
coinductive type by defining its internal structure, we may never finish due to
infinite recursion. Instead, elements of coinductive types can be defined in
terms of their observational behaviour. The observational behaviour of an
element is the collected external behaviour that an object exhibits at any given
time, i.e. the collected outcomes of applying each destructor on said
element. In other words, elements of coinductive types are defined \emph{by
  observation}. To make matters more concrete, consider the following two (valid)
definitions of a list structure in Idris:
\begin{lstlisting}[mathescape,title=\idrisBlock]
-- Well-founded
data List : Type $\to$ Type where
  Nil  : List a
  (::) : a $\to$ List a $\to$ List a
\end{lstlisting}
\begin{lstlisting}[mathescape,title=\idrisBlock]
-- Not well-founded
data List$'$ : Type $\to$ Type where
  (::) : a $\to$ List$'$ a $\to$ List$'$ a
\end{lstlisting}
The top definition is a standard inductive list type, given by construction. The
bottom definition (\texttt{List$'$}) is also inductively defined, but it is not well-founded, so any definition
having \texttt{List$'$} as result type will result in infinite
recursion:
\begin{lstlisting}[mathescape,title=\idrisBlock]
-- Leads to infinite reduction of 'repeat$'$ a'
repeat$'$ : a $\to$ List$'$ a
repeat$'$ a = a :: repeat$'$ a
\end{lstlisting}
Here, the second constructor argument is a recursive call to
\texttt{repeat$'$} itself, so this definition will reduce indefinitely.
However, we can define elements of type \texttt{List$'$} by defining the type
\emph{co}inductively. In order to do so, we must determine its observational
behaviour. Intuitively, for any (non-empty) list we may observe an element (the head of the
list) and the rest of the list (the tail of the list):
\begin{lstlisting}[mathescape,title=\idrisBlock]
corecord List$'$ a where
  head : a
  tail : List$'$ a
\end{lstlisting}
Now, we can have definitions resulting in elements of \texttt{List$'$} which do
not immediately diverge:
\begin{lstlisting}[mathescape,title=\idrisBlock]
repeat$'$ : a $\to$ List$'$ a
&head repeat$'$ a = a
&tail repeat$'$ a = repeat$'$ a
\end{lstlisting}
Since no reduction behaviour is defined directly for \texttt{repeat$'$ a}, this
definition does not result in infinite recursion. Note that instead of defining
the coinductively defined version of \texttt{List$'$} in terms of
observation (i.e. as a coinductive record), we could also have used the lazily
evaluated \texttt{codata} construct currently available in Idris to obtain the
same behaviour:
\begin{lstlisting}[mathescape,title=\idrisBlock]
codata List$'$ : Type $\to$ Type where
  (::) : a $\to$ List$'$ a $\to$ List$'$ a
\end{lstlisting}

% To define a coinductive type, which we may call $B$, means to define its observable behaviour: At any
% given time, what can we observe about an element of $B$? If for any given $B$,
% we wish to observe $n$ different things, we can define $B$ by $n$ destructors,
% as shown in Figure~\ref{fig:coinductive_type_n_destructors}.
% \begin{figure}[H]
%   \centering
%   \includegraphics{figures/general_coind_intuition_prod_coinductive}
%   \caption{An illustration of a coinductive type with $n$ destructors defining
%     its observable behaviour.}
% \label{fig:coinductive_type_n_destructors}
% \end{figure}
% In Idris, a
% definition of $B$ may be given as follows, where T$_1$,T$_2$,$\cdots$,T$_n$ are
% Idris types, either inductive or coinductive:
% \begin{lstlisting}[mathescape]
% corecord B where
%   X$_1$ : T$_1$
%   X$_2$ : T$_2$
%     $\vdots$
%   X$_n$ : T$_n$
% \end{lstlisting}
% However, this definition implies that all of these observations can be made for
% any element of $B$. If we wish to define observations that can only be made on
% \emph{some} elements of a coinductive type, the observable behaviour must
% reflect

% An inductive type is defined in terms of the ways it can be constructed. In the
% simple case where an inductive type $B$ has only one constructor, this may be
% illustrated as in Figure~\ref{fig:inductive_type_one_constructor}. In
% particular, we can construct $B$ by giving ${X_1,\,X_2,\,\cdots,\,X_n}$ as
% arguments to its sole constructor. To define the same $B$ coinductively, we must
% define it in terms of its observable behaviour (i.e. its destructors). What may
% we expect to observe about $B$?

% \begin{figure}[h]
%   \centering
%   \includegraphics{figures/general_coind_intuition_prod_inductive}

%   \caption{An illustration of an inductive type with one constructor which has
%     $n$ input parameters.}
% \label{fig:inductive_type_one_constructor}
% \end{figure}

% Before making matters more concrete, we
% proceed with a formal definition.

% \begin{definition}[\textit{Coinductive data type}]
% \label{def:coinductive_data_type}
%   Given an endofunctor $F$, a coinductive data type is an element of a final
%   coalgebra ($X$, $c$), where $X$ is the carrier and $c$ is a \emph{structure
%     map} $X\,\to\,F(X)$\,\citep{Jacobs97atutorial,Kozen2012}.
% \end{definition}

% While we will not claim to fully understand the category theory underlying
% Definition~\ref{def:coinductive_data_type}, we do have a working intuition of
% its implications. For instance, the endofunctor ${F(X)=A\times X}$ together with
% the final coalgebra ($X$, $(head, tail) : X\to A\times X$) characterizes the set
% of infinite streams, and can be implemented as a coinductive data type in Idris
% as follows:

% \begin{lstlisting}[mathescape]
% codata Stream : Type $\to$ Type where
%   (::) : (head : a) $\to$ (tail : Stream a) $\to$ Stream a
% \end{lstlisting}

% In this example, we define the observable behaviour of \texttt{Stream} as a
% whole to be the conjunction of the behaviour defined by the destructors \texttt{head} and \texttt{tail}. As a consequence, we can informally state
% that the constructor \texttt{(::)} models the structure map. This intuition extends to infinitely branching trees over an endofunctor
% ${F(X)=A\times X\times X}$ and a coalgebra ($X$, ${(value,lefttree,righttree) : X\to
% A\times X\times X)}$, possibly infinite lists over an endofunctor ${F(X)=1 + A\times X}$ and a coalgebra ($X$, ${(unfold) : X\to
% 1 + A\times X)}$, and so on. Note that when the endofunctor has a top-level sum structure,
% as for possibly infinite lists, the structure map is not split into separate
% destructors, since further analysis of the observed behaviour is needed.

\subsection{Coinduction}
% Definition by elimination
% Coinduction principle
Just as inductive types give rise to induction principles, coinductive types
give rise to coinduction principles. We will not provide a formal definition of
coinduction here, but instead give an example of coinductive reasoning. For the
interested reader, a formal definition is given by Kozen and
Silva\,\citep{Kozen2012}. Dual to induction, the Curry-Howard isomorphism
enables us to implement functions on coinductive data by coinduction. In
Figure~\ref{fig:proof_by_coinduction}, we give a simple example of a proof by
coinduction in Idris. Specifically, we show that the infinite stream of
\texttt{ones} is \emph{bisimilar} (\bisim) to the result of mapping the successor
constructor (\texttt{S}) over the infinite stream of zeros. Informally, a bisimulation is a relation between state transition
systems which denotes when two such systems have the same external behaviour. A
formal definition is given by Sangiorgi\,\citep[Section~1.4]{Sangiorgi2011}.
\begin{figure}[h]
\begin{lstlisting}[mathescape]
%default total

corecord BisimStream a (s : Stream a) (t : Stream a) where
   phead : head s = head t
   ptail : BisimStream a (tail s) (tail t)

map : (a $\to$ b) $\to$ Stream a $\to$ Stream b
&head map f s = f (head s)
&tail map f s = map f (tail s)

zeros : Stream Nat
&head zeros = Z
&tail zeros = zeros

ones : Stream Nat
&head ones = S Z
&tail ones = ones

eq : BisimStream Nat ones (map S zeros)
&phead eq = Refl
&ptail eq = eq
\end{lstlisting}
  \caption{An example of a proof by coinduction in Idris. The clauses beginning
    with ``\&'' use our copatterns. Note the use of the \texttt{Stream}
    definition from Figure~\ref{fig:stream}.}
\label{fig:proof_by_coinduction}
\end{figure}
The proof, given by the definition of \texttt{eq} in
Figure~\ref{fig:proof_by_coinduction}, proceeds as follows:
\begin{framed}
\textbf{Theorem}: \texttt{ones} \bisim{} \texttt{map S zeros}. \\\\
\emph{Proof.} The proof proceeds by coinduction on the observed behaviour of \texttt{map~S~zeros}. The definitions used are the
ones given in Figure~\ref{fig:proof_by_coinduction}. First, we prove the case
for \texttt{head}, \texttt{head ones = head~(map~S~zeros)}:
\begin{align*}
   \texttt{head ones} &= \texttt{head (map S zeros)} \\
   \texttt{S Z} &= \texttt{S (head zeros)} \\
   \texttt{S Z} &= \texttt{S Z}
\end{align*}
Next, we show the case for \texttt{tail}, \texttt{tail ones} \bisim{} \texttt{tail
  (map S zeros)}:
\begin{align*}
   \texttt{tail (ones)}\, &\bisim\, \texttt{tail (map S zeros)} \\
   \texttt{ones}\,&\bisim\, \texttt{map\, S\, (tail\, zeros)} \\
   \texttt{ones} \,&\bisim\, \texttt{map S zeros} \qed
\end{align*}
The last step follows immediately from the coinduction hypothesis, i.e. {\texttt{ones}~\bisim{}~\texttt{map~S~zeros}}
\end{framed}
% ones ~ mapStream S zeros

% hd ones = hd (map S zeros)
% hd ones = S (hd zeros)
% S Z = S Z qed

% tl (ones) ~ tl (mapStream S zeros)
% tl (ones) ~ mapStream S (tl zeros)
% ones ~ mapStream S zeros BY COINDUCTION HYPOTHESIS!

By examining the implementation of \texttt{eq}, the reasoning may at first seem
oddly circular: the \texttt{tail} case is proven by an appeal to \texttt{eq}
itself. But since \texttt{eq} is productive, it can essentially provide us with
an infinite stream of proofs by reflexivity (\texttt{Refl}), and therefore holds
for an any unfolding of \texttt{ones} and \texttt{map S zeros}. In
our experience, a proof by coinduction often proceeds by identifying a productive
way of continually generating proofs.

\subsection{Coinductive Types by Lazy Evaluation}
% Coinductive types as inductive types (Jacobs)
Coinductive types can be encoded as lazily evaluated inductive types. This is
the approach followed in Idris (see Section~\ref{sec:coind-data-types}),
ensuring that infinite recursion never arises from an attempt to build an
infinite object by strict evaluation.

% Theoretically, such an encoding is
% unproblematic, since any final coalgebra can be as an
% algebra\,\citep{JacobsCoalgebra}.

In languages where all data constructors are lazily evaluated, such as the
non-strict part of Haskell, coinductive types are almost provided ``for
free''. Indeed, Haskell makes no distinction between inductive and coinductive
types, which may leave us to question whether the distinction matters at
all. However, avoiding the distinction in general has at least two important
consequences. First, we may lose subject reduction in a dependently typed
system, since we cannot selectively disallow pattern matching on coinductive
types (we elaborate upon this issue in
Section~\ref{sec:recov-subj-reduct}). Secondly, we become unable to prove that
our language has the Church-Rosser property\,\citep{CR:36}, seeing as lazily
evaluating an infinitely recursive program may lead to a normal form when
strictly evaluating the same program may not. For Idris, the second problem is
the most dire, inasmuch as the total part of Idris is believed to have the
Church-Rosser property\,\citep{BradyIdrisImpl13}.

%Church-Rosser
%We cannot disallow pattern matching on coinductive data

% Start: Algebraiske data typer
% Hvad er en algebra (algebraic structure)?
% Hvad er en F-algebra?
% F-coalgebra
% Induktive og coinduktive typer
% eksempel
% Category theory nonsense?

% Algebraic data types are ubiquitous in functional programming, providing a
% concise way of defining data types.

% \begin{figure}
% \begin{lstlisting}
% data Lam = Var String
%          | Abs String Lam
%          | App Lam Lam
% \end{lstlisting}
% \caption{The untyped lambda calculus defined as an algebraic data structure in a
% Haskell-like language. For simplicity, strings are used for identifiers.}
% \end{figure}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../../copatterns-thesis"
%%% End: