%% AMAT 415 Assignment 3
% Safian Omar Qureshi
% ID 10086638

\documentclass{article}
\usepackage{graphicx}
\usepackage{color}

\sloppy
\definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}

\begin{document}

\section*{AMAT 415 Assignment 3}

\begin{par}
Safian Omar Qureshi ID 10086638
\end{par} \vspace{1em}

\subsection*{Contents}

\begin{itemize}
\setlength{\itemsep}{-1ex}
   \item Question 3(a)
   \item Question 3(b)
   \item Question 3(c)
   \item Question 3(d)
\end{itemize}

\subsection*{Question 3(a)}

\begin{verbatim}
N = 64;
a = 200;
n = linspace(0,63,64);
tn = n/N;
Xn = exp((-a)*(tn - 0.5).^2);

figure;
plot(tn,Xn);
xlabel('tn')
ylabel('Xn')
title('Xn vs tn')
\end{verbatim}

\includegraphics [width=4in]{untitled_01.eps}
\begin{verbatim}
DFTXn = fft(Xn);
imDFTXn = imag(DFTXn);

figure;
plot(tn,DFTXn);
xlabel('tn')
ylabel('DFTXn')
title('DFTXn vs tn')

figure;
plot(tn,imDFTXn);
xlabel('tn')
ylabel('imDFTXn')
title('imDFTXn vs tn')
\end{verbatim}

        \color{lightgray} \begin{verbatim}Warning: Imaginary parts of complex X and/or Y arguments ignored
\end{verbatim} \color{black}

\includegraphics [width=4in]{untitled_02.eps}

\includegraphics [width=4in]{untitled_03.eps}

\subsection*{Question 3(b)}

\begin{verbatim}
shiftDFTXn = fftshift(DFTXn);
shiftimDFTXn = fftshift(imDFTXn);

figure;
plot(tn,shiftDFTXn);
xlabel('tn')
ylabel('Shifted DFTXn')
title('Shifted DFTXn vs tn')

figure;
plot(tn,shiftimDFTXn);
xlabel('tn');
ylabel('Shifted imDFTXn');
title('Shifted imDFTXn vs tn');
\end{verbatim}

        \color{lightgray} \begin{verbatim}Warning: Imaginary parts of complex X and/or Y arguments ignored
\end{verbatim} \color{black}

\includegraphics [width=4in]{untitled_04.eps}

\includegraphics [width=4in]{untitled_05.eps}

\subsection*{Question 3(c)}

\begin{verbatim}
DubXn = exp((-2*a)*(tn - 0.5).^2);
HafXn = exp((-0.5*a)*(tn - 0.5).^2);

figure;
plot(tn,DubXn);
xlabel('tn')
ylabel('Double a Xn')
title('Double a Xn vs tn')

figure;
plot(tn,HafXn);
xlabel('tn')
ylabel('Half a Xn')
title('Half a Xn vs tn')
\end{verbatim}

\includegraphics [width=4in]{untitled_06.eps}

\includegraphics [width=4in]{untitled_07.eps}
\begin{verbatim}
DFTDubXn = fft(DubXn);
DFTHafXn = fft(HafXn);
IMG_DFTDubXn = imag(DFTDubXn);
IMG_DFTHafXn = imag(DFTHafXn);

shiftDFTDubXn = fftshift(DFTDubXn);
shiftDFTHafXn = fftshift(DFTHafXn);
IMG_shift_DFTDubXn = fftshift(IMG_DFTDubXn);
IMG_shift_DFTHafXn = fftshift(IMG_DFTHafXn);

figure;
plot(tn, shiftDFTDubXn);
xlabel('tn')
ylabel('Shifted Double a DFTXn')
title('Shifted Double a DFTXn vs tn')

figure;
plot(tn, shiftDFTHafXn);
xlabel('tn')
ylabel('Shifted Half a DFTXn')
title('Shifted Half a DFTXn vs tn')
\end{verbatim}

        \color{lightgray} \begin{verbatim}Warning: Imaginary parts of complex X and/or Y arguments ignored
Warning: Imaginary parts of complex X and/or Y arguments ignored
\end{verbatim} \color{black}

\includegraphics [width=4in]{untitled_08.eps}

\includegraphics [width=4in]{untitled_09.eps}
\begin{verbatim}
figure;
plot(tn, IMG_shift_DFTDubXn);
xlabel('tn')
ylabel('Imaginary Shifted Double a DFTXn')
title('Imaginary Shifted Double a DFTXn vs tn')

figure;
plot(tn, IMG_shift_DFTHafXn);
xlabel('tn')
ylabel('Imaginary Shifted Half a DFTXn')
title('Imaginary Shifted Half a DFTXn vs tn')
\end{verbatim}

\includegraphics [width=4in]{untitled_10.eps}

\includegraphics [width=4in]{untitled_11.eps}

\subsection*{Question 3(d)}

\begin{verbatim}
alternating_row = (-1).^[0:63];
alternatingDFTXn = (alternating_row).*(DFTXn);
inv_alternatingDFTXn = ifft(alternatingDFTXn);

figure;
plot(tn, inv_alternatingDFTXn);
xlabel('tn')
ylabel('Inverse DFT of Xn multiplied by [1,-1,...]')
title('Inverse DFT of Xn multiplied by [1,-1,...]  vs tn')
\end{verbatim}

\includegraphics [width=4in]{untitled_12.eps}

\end{document}

math courses/AMAT415 - Mathematical methods/assignments/as3/assignment3.tex


\section{Modeling probabilistic design}
\label{sect:prob-formulation}

To verify a deterministic design,
properties are often asserted at the primary outputs of a circuit to examine whether
there exists an assignment to the primary inputs that falsifies some of the properties.
If there exists such an assignment,
a counterexample to a property is found.

However, when it comes to probabilistic design,
the same approach is not fully adequate.
Since a probabilistic circuit could produce different output responses for the same input stimulus,
computing the probability of property violation is more meaningful than searching for a counterexample.
Hence we pose the following question:
\textit{
    Given a probabilistic circuit and a property to be verified,
    what is the average or maximum probability for the property to be violated?
}
We model a probabilistic design as a probabilistic Boolean network and
formalize \textit{probabilistic property evaluation} (PPE) to answer the question.
While the evaluation of combinational probabilistic design is of our primary interest,
the proposed framework is also extensible to sequential probabilistic design.

\subsection{Probabilistic Boolean network}
We define a probabilistic Boolean network to model circuits with logic gates that exhibit probabilistic behavior.
A \textit{probabilistic Boolean network} (PBN) is a Boolean network $G=(V,E)$ with random variables annotated to its vertices.
The probabilistic behavior of a PI $v \in V_I$ is modeled by a Bernoulli random variable
$B_v\sim\textit{Bernoulli}(p_v)$ with $p_v=\Pr[v=\top]$.
The probabilistic behavior of the output of a vertex $v \in V\setminus V_I$ is modeled with a Bernoulli random variable $B_v\sim\textit{Bernoulli}(p_v)$ with $p_v$ corresponding to the error rate of $v$.

In general,
two random variables $B_u$ and $B_v$ for $u,v \in V$ and $u \neq v$ can be dependent,
and their joint distribution has to be considered.
In this paper,
we first focus on the simplified situation where the random variables of vertices are mutually independent,
and refer to a PBN whose random variables are mutually independent as an \textit{independent PBN}.
We will show how to extend the proposed framework to a PBN with mutually dependent random variables later.

\begin{figure}[t]
    \centering
    \includegraphics{fig/build/prob-distillation.pdf}
    \caption{Distillation of a \nand gate with an error rate $p$}
    \label{fig:prob-distillation}
\end{figure}

Given a PBN $G=(V,E)$,
without loss of generality,
we standardize $G$ by converting it to a \textit{standardized PBN} (SPBN) $G'=(V',E')$
with the \textit{distillation operation} depicted in~\cref{fig:prob-distillation},
using an erroneous \nand gate as an example.
For each node $v \in V\setminus V_I$ of $G$ with an error rate $p_v$,
its probabilistic behavior is distilled into an error source modeled by an auxiliary Boolean input $z$
with $B_z\sim\textit{Bernoulli}(p_v)$ for $\Pr[z=\top]=p_v$.
Moreover, an \xor gate is used to conditionally invert the output of the error-free node $v$
if and only if $z$ valuates to $\top$.
Note that an auxiliary input differs from a primary input in that
the error rate $p_z$ of an auxiliary input is fixed with respect to the characteristics of a probabilistic node
rather than determined by the environmental input behavior.
In the following, we let $V_Z$ be the set of auxiliary inputs (AIs) of an SPBN.

\subsection{Probabilistic property evaluation}
To formally reason about probabilistic design,
we formulate \textit{probabilistic property evaluation} (PPE) by capturing the degree of property violation with probability.
We distinguish between \textit{input assignment} and \textit{parameter assignment} in PPE.
An \textit{input assignment} assigns a truth value to a Boolean variable $v$ for every PI.
On the other hand,
a \textit{parameter assignment} specifies a probability $p_v$ for a PI $v$ to be \textsc{true}.
Note that the probability of an error source $z \in V_Z$ is determined by the underlying PBN and needs not be assigned.

To analyze a probabilistic design,
we define \textit{signal probability} as follows.
\begin{definition}[Signal Probability (parameterized)]
    \label{def:prob-signal-prob}
    Given an SPBN $G=(V,E)$ and a parameter assignment $\pi:V_I\mapsto[0,1]$,
    the \textit{signal probability} or \textit{satisfying probability} of a node $v \in V$
    with respect to the parameter assignment $\pi$ is $\Pr[v=\top]$ under $\pi$.
\end{definition}
It is natural to ask under what parameter assignment the signal probability of some node is maximized.
It corresponds to the following definition.
\begin{definition}[Signal Probability (maximized)]
    \label{def:prob-signal-prob-max}
    Given an SPBN $G=(V,E)$,
    the \textit{maximum signal probability} or \textit{maximum satisfying probability} of a node
    $v\in V$ is $\Pr[v=\top]$ maximized over all parameter assignments.
\end{definition}

Notice that, given a parameter assignment to the PIs of an SPBN $G=(V,E)$,
we could associate a random variable $R_v\sim\textit{Bernoulli}(p)$ with each node $v \in V$
such that $p=\Pr[v=\top]$ under $\pi$.
Those random variables can be mutually dependent (even for an SPBN derived from an independent PBN)
due to the reconvergent paths in the Boolean network.

According to~\cref{def:prob-signal-prob-max},
we have the following proposition on parameter assignments that maximize signal probability.

\begin{proposition}
    Given an SPBN $G=(V,E)$ and an arbitrary $v \in V$,
    there exists a parameter assignment $\pi$ that maximizes the signal probability of $v$
    such that $\pi(u)$ equals either probability 0 or 1 for any $u \in V_I$.
\end{proposition}
\begin{proof}
    Assume there exists an optimal parameter assignment $\pi$ not in such a form.
    That is, there exists some $u \in V_I$ such that $0<\pi(u)<1$.
    Denote the signal probabilities of $v$ under $\pi(u)=0$ and $\pi(u)=1$ by $p_0$ and $p_1$, respectively.
    Note that $\Pr[v=\top]$ under $\pi$ equals $(1-\pi(u)) \times p_0 + \pi(u) \times p_1$,
    and $\min\{p_0,p_1\}\leq\Pr[v=\top]\leq\max\{p_0,p_1\}$.
    If $p_0 \neq p_1$,
    there is a contradiction since $\Pr[v=\top]<\max\{p_0,p_1\}$,
    and $\pi$ is not an optimal assignment.
    If $p_0=p_1$, W.L.O.G., set $\pi(u)=0$.
    So the optimal assignment must be in the stated form.
\end{proof}

\begin{figure}[t]
    \centering
    \includegraphics{fig/build/prob-spbn-miter.pdf}
    \caption{A miter SPBN for probabilistic property evaluation}
    \label{fig:prob-spbn-miter}
\end{figure}

To formulate the PPE problem,
we construct a \textit{miter SPBN} as shown in~\cref{fig:prob-spbn-miter}.
Given a \textit{design SPBN} $G_D$ modeling a probabilistic circuit and
a \textit{property SPBN} $G_P$ specifying a property under verification,
a miter SPBN $G_M$ is built by connecting relevant primary outputs of $G_D$ to the primary inputs of $G_P$.
The property SPBN is used to monitor the design SPBN.
Let $X$ be the set of PIs of $G_D$,
$Z$ be the set of AIs of $G_D$,
$W$ be a subset of PIs of $G_P$,
and $Y$ be the set of all other vertices.
Note that $G_P$ may have additional inputs $W$ to enrich the expressiveness of property.
For example, they can be used to prioritize the primary outputs of $G_D$.
Based on signal probability,
two versions of the PPE problem are defined below.

\begin{definition}[PPE (maximized)]
    Given a miter SPBN $G_M$ with the vertex sets $X,Y,Z,W$ defined as above,
    the \textit{maximum probabilistic property evaluation} (MPPE) problem asks to find the maximum satisfying probability of the output of $G_M$.
    That is,
    the signal probability of the miter output corresponds to the maximum probability of property violation under all parameter assignments.
\end{definition}

\begin{definition}[PPE (parameterized)]
    Given a miter SPBN $G_M$ with the vertex sets $X,Y,Z,W$ defined as above and a parameter assignment $\pi:X\mapsto[0,1]$,
    the \textit{probabilistic property evaluation} (PPE) problem asks to find the satisfying probability of the output of $G_M$.
    That is,
    the signal probability of the miter output corresponds to the probability of property violation under the given parameter assignment.
\end{definition}

\subsection{Extension to sequential probabilistic design}
Although the above PPE and MPPE frameworks mainly focus on combinational design,
they are extensible to analyze sequential design by \textit{circuit unrolling}~\cite{Clarke2001},
similar to the soft-error reliability analysis for sequential circuits~\cite{Miskov-Zivanov2008}.
For example,
to find the probability of property violation after $T$ clocks of execution,
the sequential circuit is \textit{unrolled} into a combinational circuit by connecting $T$ copies of the combinational block of the sequential circuit to mimic the state transitions in $T$ time frames.
For simplicity,
the occurrences of errors among different time frames are assumed to be temporally independent,
i.e., the random variables governing the probabilistic behavior of errors among different time frames are mutually independent.
After unrolling,
the proposed PPE and MPPE frameworks can be applied to the effective combinational circuit and
analyze the satisfying probability of the output at the $T^\mathrm{th}$ time frame.
The result corresponds to the probability of property violation of the sequential design after $T$ clocks of execution.


\subsection{Counting in Graph}

\lem{Average of Degrees}{
    In a graph $ G $ with $ n $ vertexes, let $ E $ be the set of all edges.
    Assign an integer $ f_i $ to every vertex $ v_i $ such that $ f_i $ equals to
    the everage degree of the neighbors of $ v_i $. We have, \[ \sum_{i=1}^{n} f_i
    \geq 2|E| \]
}\label{lemma:graph_lemma_1}

\lem{}{In a graph $ G $ with $ n $ vertexes, let $ E $ be the set of all edges. Assign an integer $ g_i $ to every vertex $ v_i $ such that $ g_i $ equals to the maximum degree among its neighbors. We have, \[ \sum_{i=1}^{n} g_i \geq 2|E| \] }

\prob{https://artofproblemsolving.com/community/c6h568277p3332307}{USA TST 2014 P3}{H}{Let $n$ be an even positive integer, and let $G$ be an $n$-vertex graph with exactly $\tfrac{n^2}{4}$ edges, where there are no loops or multiple edges (each unordered pair of distinct vertices is joined by either 0 or 1 edge). An unordered pair of distinct vertices $\{x,y\}$ is said to be amicable if they have a common neighbor (there is a vertex $z$ such that $xz$ and $yz$ are both edges). Prove that $G$ has at least $2\textstyle\binom{n/2}{2}$ pairs of vertices which are amicable.}

\solu{Define friendship in a different way, bounding below, keeping in mind the equality case. Then using the previous lemma.}

\begin{minipage}{.6\linewidth}
    \theo{https://en.wikipedia.org/wiki/Turan's_theorem}
    {Turan's theorem}{
        Let $ G $ be any graph with $ n $ vertices, such that $ G $ is $ K_{r+1} $
        -free. Then $ G $ is the ``Turán's Graph'' and is a complete $ r $ partite
        graph. And the number of edges in $ G $ is at most

        \[\frac {r-1}{r}\cdot \frac {n^{2}}{2}=\left(1-\frac {1}{r}\right)\cdot
        \frac {n^{2}}{2}\]

        A special case of Turán's theorem for $ n=2 $ is the \textbf{Mantel's
        Theorem}. It states that the maximal triangle free graph is a complete
        bipartite graph with at most $ \left\lfloor\dfrac{n^2}{4}\right\rfloor $
        edges.
    }
\end{minipage}\hfill%
\begin{minipage}{.37\linewidth}
    \figdf{.9}{Turan_13-4}{Turán's Graph}
\end{minipage}

\vspace{1em}

\proof{We need to prove that the maximal graph is the $ r $ partite one, and the rest will follow. We can directly try to prove that this graph is $ r $ colorable, but that is quite troublesome. Instead, we try to show that, we can partition the vertices of $ G $ into equivalence classes based on their non-neighbors. Since this is imply the former. So we need to prove that \hrf{lemma:criteria_of_partition_equiv}{this} holds for this graph.\\

The way it is done is quite interesting. We need to show that if the criteria doesn't hold in this graph, then this graph is not the maximal graph. How are we going to do that? We compare the degrees of $ u, w $, and replace either $ u $ by $ w $ or $ w $ by $ u $ to get a graph with more edges and without the nasty situation.}

\prob{}{}{E}{$ 155 $ birds $ P_1, P_2, \dots, P_{155} $ are sitting down no the boundary of a circle $ C $. Two birds $ P_i, P_j $ are mutually visible if the angle at the center of their cord, $ m(P_iP_j)\le 10^\circ $. Find the smallest number of mutually visible pairs of birds.}

\prob{}{}{E}{For a pair $ A = (x_1, y_1) $ and $ B = (x_2, y_2) $ of points on the coordinate plane, let $ d(A, B)  = |x_1 - x_2| + |y_1 - y_2|$. We call a pair $ (A,B) $  of unordered points harmonic if $ 1<d(A,B)\le 2 $. Determine the maximum number of harminc pairs among $ 100 $ points in the plane.}

\prob{www.hehe.com}{Swell coloring}{E}{Let $ K_n $ denote the complete graph on $ n $ vertices, that is, the graph with $ n $ vertices's such that every pair of vertices's is connected by an edge. A swell coloring of $ K_n $ is an assignment of a color to each of the edges such that the edges of any triangle are either all of distinct colors or all the same color. Further, more than one color must be used in total (otherwise trivially if all edges are the same color we would have a swell coloring). Show that if $ K_n $ can be swell colored with $ k $ colors, then $ k \geq \sqrt{n} + 1 $.}\label{problem:forget_and_focus_5}

\solu{Concentrate on only one vertex.}

\prob{www.hehe.com}{Belarus 2001}{MH}{Given $ n $ people, any two are either friends or enemies, and friendship and enmity are mutual. I want to distribute hats to	them, in such a way that any two friends possess a hat of the same color but no two enemies possess a hat of the same color. Each person can receive multiple hats. What is the minimum number of colors required to always guarantee that I can do this?}\label{problem:extremal_case_whole_6}

\solu{In this problem, finding the worst case is a big help, because once the answer is guessed, the things become really clear.}

\prob{https://artofproblemsolving.com/community/c6h1468154p8509521}{ELMO 2017 P5}{M (8/10)}{The edges of $ K_{2017} $ are each labeled with $ 1, 2 $ or $ 3 $ such that any triangle has sum of labels at least $ 5. $ Determine the minimum possible average of all labels. (Here $ K_{2017} $ is defined as the complete graph on 2017 vertices's, with an edge between every pair of vertices's.)}\label{problem:induction_type1_13}

\solu{A starting idea to get the ans: if we discard of all the $ 2 $-edges, we see that in any triangle, one edge has to be a $ 3 $-edge. So... Turan-kinda...}

\solu{After getting the ans, and thinking about approaching inductively, if we remove only one vertex, there will be pairs to consider. But if we remove two vertices, we will only need to consider single vertices after the removal of these two vertices.\\

    Now which pair of vertices are the best choice to remove? Before doing that, lets first think how much change will we get in the sum after we remove two vertices. Since we have the ans, we do quick maffs:
    \[m(4m+1) - (m-1)(4m-3) = 8m -3 = 4\times (2m-1) + 1\]

Doesn't this indicate that we remove a $ 1 $-edge, so the other edges coming out of the two vertices will sum up to be at least $ 4*(2m-1) $.}

\solu{The solution by bern is very pretty. What he probably had thought was:\\

    If we pick a vertex, say $ u $, and take an $ 1 $-edge from this vertex to another vertex $ v $, we see that there are at least as many $ 3 $-edges in $ u $ than there are $ 1 $-edges in $ v $. Now if to get a more accurate value of $ d_3(u) $ (defined naturally), we need to take the maximum of the values $ d_1(v) $ for all $ v $'s connected to $ u $. \\

    Now we need to evaluate the number of $ 3 $ edges from the $ d_1 $ values. Can we put a bound on this sum? We have \hrf{lemma:graph_lemma_1}{\textbf{this lemma}}, does this help? Turns out that it does.\\

What left is to sum it all up to see if we can get the ans.}

\prob{https://artofproblemsolving.com/community/c6h35320p220234}{ARO 2005 P9.4}{M (7/10)}{ $ 100 $ people from $ 50 $ countries, two from each countries, stay on a circle. Prove that one may partition them onto $ 2 $ groups in such way that neither no two countrymen, nor three consecutive people on a circle, are in the same group.\\

\textbf{Variant:} There are $ 100 $ people from $ 25 $ countries sitting around a circular table. Prove that they can be separated into four classes, so that no two countrymen are in the same class, nor any two people sitting adjacent in the circle.}\label{problem:hall_marriage_2}

\solu{Thinking of the most natural way of eliminating the consecutive condition -- pair two consecutive verices.}

\prob{https://artofproblemsolving.com/community/c6h478015p2676752}{Romanian TST 2012 P4}{E (7/10)}{Prove that a finite simple planar graph has an orientation so that every vertex has out-degree at most $ 3 $.}

\prob{https://artofproblemsolving.com/community/c6h148822p841243}{USA TST 2006 P1}{E-M (8/10)}{A communications network consisting of some terminals is called a $3$-connector if among any three terminals, some two of them can directly communicate with each other. A communications network contains a windmill with $n$ blades if there exist $n$ pairs of terminals $\{x_{1},y_{1}\},\{x_{2},y_{2}\},\ldots,\{x_{n},y_{n}\}$ such that each $x_{i}$ can directly communicate with the corresponding $y_{i}$ and there is a hub terminal that can directly communicate with each of the $2n$ terminals $x_{1}, y_{1},\ldots,x_{n}, y_{n}$ . Determine the minimum value of $f (n)$, in terms of $n$, such that a $3$ -connector with $f (n)$ terminals always contains a windmill with $n$ blades.}

\solu{Windmills won't be there if among any $ 2n+1 $ vertices, there were one vertex that were not connected to any of the other $ 2n $ vertices. So that means that we are dealing Turan-kinda config here. So we can make several `compact' graphs that are mutually disconnected, and each have at most $ 2n $ verices. Guessing from this, the ans is probably of some form $ k*2n + 1 $. Now we have another condition to consider, $ 3 $-connector. Lets see, if we had $ 3 $ disconnected componets, the resulting graph wouldn't be a $ 3 $-connector. Done...}

\prob{}{}{E}{Graph $ G $ on $ n $ vertices has the property that the degree of every vertex is greater than $ 2 $. Prove that for every $ 0 < k < n $, there is a simple path with lenght at least $ n/k $ or, $ k $ cycles, such that every cycle has at least one node which none of the other cycles has, and its lenght is not divisible by $ 3 $.}

\prob{https://artofproblemsolving.com/community/c6h126200p715463}{ISL 2005 C4}{E}{Let $n\geq 3$ be a fixed integer. Each side and each diagonal of a regular $n$-gon is labelled with a number from the set $\left\{1;\;2;\;...;\;r\right\}$ in a way such that the following two conditions are fulfilled:
    \vspace{-1em}
    \begin{itemize}
        \setlength{\itemindent}{-1.5em}
    \itemsep0em
    \item Each number from the set $\left\{1,2,\dots r\right\}$ occurs at least once as a label.
    \item In each triangle formed by three vertices of the $n$-gon, two of the sides are labelled with the same number, and this number is greater than the label of the third side.
\end{itemize}
\vspace{-1em}
\begin{enumerate}
    \setlength{\itemindent}{-1.2em}
\itemsep0em
\item Find the maximal $r$ for which such a labelling is possible.
\item For this maximal value of $r$, how many such labellings are there?
        \end{enumerate}
    }

    \solu{[Extremal]Take the edges labeled with $ r $, and delete them. Study what is left. For the second part, formulate a recursive function, and try out small cases to find pattern.}

\prob{https://artofproblemsolving.com/community/c6h2091306p15108216}
{St Petersburg 2020 P11.7}{}{
    $N$ oligarchs built a country with $N$ cities with each one of them owning
    one city. In addition, each oligarch built some roads such that the
    maximal amount of roads an oligarch can build between two cities is $1$
    (note that there can be more than $1$ road going through two cities, but
    they would belong to different oligarchs).

    A total of $d$ roads were built. Some oligarchs wanted to create a
    corporation by combining their cities and roads so that from any city of
    the corporation you can go to any city of the corporation using only
    corporation roads (roads can go to other cities outside corporation) but
    it turned out that no group of less than $N$ oligarchs can create a
    corporation. What is the maximal amount that $d$ can have?
}

\begin{solution}
    At first I thought about ``cuts'' where we can only have roads owned by one
    oligarch, but it proved to be really complex to work with. So I thought
    about constructing the best solution. Trying it out for $3, 4$ immediately
    gave the idea to construct optimally. Now on forward to proving it.\\

    The proof is roughly as followed. We will show that if we remove the
    oligarch indexed $N$, then we need to remove at most ${N \choose 2}$
    roads. Since there is no road owned by $N$ that connects to city $N$, the
    roads owned by $N$ forms a forest of graphs with the other cities.\\

    We show that for every edge in that forest, there is one less road leaving
    city $N$. Which we do by induction. We take the set $\left\{1, 2, \dots
    N-1\right\}$, one of these cities has no road with $N$. WLOG, it is $1$.
    Then inductively we can assume that city $i$ can have at most $i-1$ roads
    with $N$.\\

    Now for each $i$, starting with $N-1$, and ending at $1$, we show that in
    reality, $i$ can have at most $i-1 - V_N(i)$ where $V_N(i)$ is the number
    of roads owned by $N$ leaving $i$. It works inductively, and so after it,
    we can just remove $N$, and assume our inductive hypothisis. Which gives
    us our answer of \[\boxed{{N \choose 3}}\]
\end{solution}


\section{Countable and Uncountable Sets}

\begin{definition}
  A set $A$ is said to be \textbf{infinite} if it is not finite. It is said to
  be \textbf{countably infinite} if there is a bijective correspondence
  \begin{equation}
    f:A \to \Zp
  \end{equation}
\end{definition}

\begin{definition}
  A set is said to be \textbf{countable} if it is either finite or countably
  infinite. A set that is not countable is said to be \textbf{uncountable}.
\end{definition}

\begin{theorem}
  Let $B$ be a nonempty set, then the following are equivalent:
  \begin{enumerate}[label=(\arabic*)]
    \item $B$ is countable
    \item There is a surjective function $f : \Zp \to B$
    \item There is an injective function $g : B \to \Zp$
  \end{enumerate}
\end{theorem}

\begin{theorem}
  A countable union of countable sets is countable.
\end{theorem}

\begin{theorem}
  A finite product of countable sets is countable.
\end{theorem}

\section*{Exercises}

\bx{
  Lazy way to prove this, but you can make an injective function
  $f : \mathbb{Q} \to \Zp \times \Zp \times \Zp$
  where given any
  \begin{equation*}
    f(\pm \frac{m}{n}) = (m, n, \pbrac{1 \text{ if negative}, 2 \text{ if positive}})
  \end{equation*}

  And then we know that $\Zp \times \Zp \times \Zp$
  is a finite product of countable sets, so it is also countable. Since $f$ is
  an injective function into a countably infinite set, we conclude that
  $\mathbb{Q}$ is also countably infinite.
}

\bx{
  Checking for bijections. I'm not going to explicitly show this, but you can find inverse functions to show bijectivity.
  \TODO.
}

\bx{
  The bijection is whether or not some $n \in \Zp$ is included in a set
  or not, which corresponds to 0 (not included) and 1 (included) in the tuple of
  $X^\omega$.
}

\bx{
  \ea{
    \item For a given $n$, the number of algebraic numbers is $\prod_{i=0}^{n-1}
    \mathbb{Q}$. Then taking the union over $\Zp$, this would be a
    countable union of countable sets, which is also countable. Therefore there
    are a countable number of algebraic numbers.
    \item AFSOC transcendental numbers are countable. Then the real numbers are
    a union of two countable sets, which could make $\mathbb{R}$ countable,
    which is a contradiction.
  }
}

\bx{
  \ea{
    \item This set of functions can be bijected to $\Zp \times \Zp$, which is countable.
    \item This would be $\prod_{i=1}^n \Zp$, which is a finite product of countable sets, which is countable.
    \item This is a countable union of countable sets, which is countable.
    \item Not countable, you can use diagonalization to construct a function that is not included in a countable map.
    \item Not countable, can also use a diagonalization argument to construct a function that differs in the $i^\text{th}$ spot for $f_i$.
    \item Countable, since this is essentially a subset of the countable union of $\Zp$. \label{chap1:sec7:prob5:partf}
    \item Countable, same reasoning as \ref{chap1:sec7:prob5:partf}
    \item This is a countable union of a countable union of countable sets, which is still countable.
    \item Countable, this is a subset of $\Zp \times \Zp$.
    \item Countable, this is just $\cup_{i=0}^\infty \prod_{j=1}^i \Zp$, which is a countable union of a finite product of countable sets.
  }
}

\bx{
  \ea{
    \item Originally I was going to define the identity map $I : B \to A$, since
    $B \subset A$, and say that injections both ways implies bijection, but
    that's the theorem we are trying to prove in the next step, so let's just do
    what the problem tells us here.

    To understand the hint a bit more, the reason we can plug in $B$ values for
    $f$ is because $B \subset A$. And the reason that we get an $A_i$ value from
    the result of $f$ is also because $B \subset A$.

    Now, if we define this new function $h$,
    \begin{equation}
      h(x) = \begin{cases}
        f(x) &\text{if $x \in A_n - B_n$}\\
        x &\text{otherwise}
      \end{cases}
    \end{equation}

    We have to check this function is bijective.
    \begin{itemize}
      \item \textbf{Injective}: We have 2 cases for $x$. Either it is in $B$, in which case we just spit out $x$, or it is in $A_n$ but not in $B_n$, in which case we apply $f(x)$, and make $x \in B$.
      This is injective, because cases, for some $x_1, x_2 \in A$,
      \begin{enumerate}
        \item $x_1, x_2 \in A_n - B_n$, then we are just looking at $f$, which we know is injective
        \item $x_1 \in A_n - B_n, x_2 \not\in A_n - B_n$, then $x_1 \neq x_2$,
        and since $x_1 \in A_n - B_n$, it is never mapped to itself, because it
        if were, then it would imply it is $\not\in A_n - B_n$, which is a
        contradiction.
        \item $x_1, x_2 \not\in A_n - B_n$, then we are using the identity map, which is injective
      \end{enumerate}

      \item \textbf{Surjective}: This is the more difficult part of the proof, but also the more fun.

      The idea of the recursive definition is that we want to partition $B$ into parts, $A_i, B_i$.
      The only part that we have trouble with is $A_1 - B_1$, since this part is
      not in $B$. However, by applying $f$ to this portion, we can have it map
      into $B$, which helps us map $B$. However, since $f(A_1)$ ``takes the
      spot'' of $A_2$, we then apply $f$ on $A_2$, so that $f(A_2) \subset B$.
      $f(A_2)$ takes the spot of $A_3$, so we keep applying this recursive
      definition. Eventually, we can see that all of $B$ is mapped, so $h$ is
      also surjective on $B$.
      I know this is an informal proof, but Figure
      \ref{chap1:sec7:prob6:parta:fig} explains the hardest part of the proof.

      \begin{figure}[H]
        \centering
        \begin{tikzpicture}
          \node[rectangle, draw, fill=gray!40] (r_1) at (0, 0) {$A_1$};
          \node[rectangle, draw, anchor=north west] (r_2) at (r_1.north east) {$B_1$};
          \node[rectangle, draw, anchor=north west, fill=gray!40] (r_3) at (r_2.north east) {$A_2$};
          \node[rectangle, draw, anchor=north west] (r_4) at (r_3.north east) {$B_2$};
          \node[rectangle, draw, anchor=north west, fill=gray!40] (r_5) at (r_4.north east) {$A_3$};
          \node[rectangle, draw, anchor=north west] (r_6) at (r_5.north east) {$B_3$};
          \node[rectangle, draw, anchor=north west, fill=gray!40] (r_7) at (r_6.north east) {$A_4$};
          \node[rectangle, draw, anchor=north west] (r_8) at (r_7.north east) {$B_4$};

          \draw[-stealth] (r_1.north) to[bend left=60] node[above] {$f$} (r_3.north);
          \draw[-stealth] (r_3.north) to[bend left=60] node[above] {$f$} (r_5.north);
          \draw[-stealth] (r_5.north) to[bend left=60] node[above] {$f$} (r_7.north);

          \draw[-stealth] (r_2.south) -- ++(0, -0.5);
          \draw[-stealth] (r_3.south) -- ++(0, -0.5);
          \draw[-stealth] (r_4.south) -- ++(0, -0.5);
          \draw[-stealth] (r_5.south) -- ++(0, -0.5);
          \draw[-stealth] (r_6.south) -- ++(0, -0.5);
          \draw[-stealth] (r_7.south) -- ++(0, -0.5);
          \draw[-stealth] (r_8.south) -- ++(0, -0.5);

          \draw ($(r_2.south west) + (0, -0.5)$) rectangle
            ($(r_8.south east) + (0, -1)$) node[pos=.5] {$B$};
        \end{tikzpicture}
        \caption{Showing the subset partitions for $A_i, B_i$}
        \label{chap1:sec7:prob6:parta:fig}
      \end{figure}
    \end{itemize}

    \textbf{Note:} To be honest, I'm not sure why we had to go through the
    trouble of defining the recursive definition. To me, it seems like with $f$,
    the scenario is that $A$ maps to $B$ injectively, except there are some some
    elements in $B$ that are not mapped.

    \textbf{Update:} It seems like the reason is that with a naive definition of
    just $h(x)$ mapping $f(x)$ for $x \in A-B$ and identity for $x \in A \cap
    B$, we cannot claim that this combo of $f(x), x$ maps $B$ surjectively. See
    the surjective part of the proof to see why this proof is so fun.

    \label{chap1:sec7:prob6:parta}

    \item We have two cases, either $A \subset C$ or $C \subset A$. In either
    case, we can apply part \ref{chap1:sec7:prob6:parta}.
  }
}

\bx{
  We can see that $E \subset D$. Need $f: D \to E$. We can construct such an $f$
  by representing each $x \in \Zp$ in binary, and mapping it to the
  corresponding 0-1 tuple in $\pbrac{0, 1}$.
}

\bx{
  \TODO. I'm stuck. I assume we want to do the double bijective proof to show equal cardinalities.
}

\bx{
  \ea{
    \item First rearrange, the formula to be easier to apply:
    \begin{equation*}
      h(n+1) = \sqrt{
        h(n) + h(n-1)^2
      }
    \end{equation*}
    Trying out some values,
    \begin{align*}
      h(3) &= \sqrt{2 + 1} = \sqrt{3}\\
      h(4) &= \sqrt{4 + \sqrt{3}}\\
      h(5) &= \sqrt{3 + \sqrt{4 + \sqrt{3}}}
    \end{align*}
    so we know that there exists such a function

    \item Notice that when we took the square root, we could have had $\pm$, so there it is not well defined which one we should take.
    \item If we try to solve for
    \begin{align*}
      h(3) &= \sqrt{
        2 - 1
      } = \pm 1\\
      h(4) &= \sqrt{
        \pm 1 - 2
      } \implies \text{square root of negative number...}
    \end{align*}
    imaginary numbers? Never heard of them.
  }
}


%% LyX 2.0.6 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[letterpaper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{amsmath}
\usepackage{esint}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
\pdfpageheight\paperheight
\pdfpagewidth\paperwidth

\makeatother

\usepackage{babel}
\begin{document}
Jon Allen

Homework 4

September 25

\section*{2.5}

\subsection*{29}

Solve $(t^{2}-y^{2})\mathrm{d}y+(y^{2}+ty)\mathrm{d}t=0$.

$(x^{2}t^{2}-x^{2}y^{2})=x^{2}(t^{2}-y^{2})=(x^{2}y^{2}+xtxy)$

The equation is homogeneous so we substitute $t=vy$

\begin{align*}
(v^{2}y^{2}-y^{2})\,\mathrm{d}y+(y^{2}+vy^{2})(y\,\mathrm{d}v+v\,\mathrm{d}y) & =0\\
(v^{2}y^{2}-y^{2})\,\mathrm{d}y+y^{2}y\,\mathrm{d}v+vy^{2}y\,\mathrm{d}v+y^{2}v\,\mathrm{d}y+vy^{2}v\,\mathrm{d}y & =0\\
(v^{2}y^{2}-y^{2}+y^{2}v+v^{2}y^{2})\,\mathrm{d}y+(y^{3}+vy^{3})\,\mathrm{d}v & =0\\
y^{2}(v^{2}-1+v+v^{2})\,\mathrm{d}y+y^{3}(1+v)\,\mathrm{d}v & =0\\
y^{2}(2v^{2}+v-1)\,\mathrm{d}y+y^{3}(1+v)\,\mathrm{d}v & =0\\
y^{2}(2v^{2}+v-1)\,\mathrm{d}y & =-y^{3}(1+v)\,\mathrm{d}v\\
\frac{1}{y}\,\mathrm{d}y & =-\frac{1+v}{2v^{2}+v-1}\,\mathrm{d}v\\
\frac{1}{y}\,\mathrm{d}y & =-\frac{1+v}{(v+1)(2v-1)}\,\mathrm{d}v\\
\frac{1}{y}\,\mathrm{d}y & =-\frac{1}{2v-1}\,\mathrm{d}v & u=2v-1\\
\int\frac{1}{y}\,\mathrm{d}y & =-\frac{1}{2}\int\frac{1}{u}\,\mathrm{d}u & \mathrm{d}u=2\mathrm{d}v\\
\ln y & =\ln\frac{1}{\sqrt{2v-1}}+C_{0}\\
y & =e^{C_{0}}\frac{1}{\sqrt{2v-1}} & e^{C_{0}}=C\\
y & =C\frac{1}{\sqrt{2\frac{t}{y}-1}}
\end{align*}

\subsection*{36}

Solve $(t+y)\,\mathrm{d}t-t\,\mathrm{d}y=0,y(1)=1$
\begin{align*}
(tx+yx)-tx & =xM(x,y)+xN(x,y)
\end{align*}
The equation is homogeneous and $N(x,y)$is simpler so set $y=wt$and
$\mathrm{d}y=t\mathrm{\, d}w+w\,\mathrm{d}t$
\begin{align*}
(t+wt)\,\mathrm{d}t-t(t\mathrm{\, d}w+w\,\mathrm{d}t) & =0\\
t\,\mathrm{d}t-t^{2}\,\mathrm{d}w & =0\\
\frac{1}{t}\,\mathrm{d}t & =\mathrm{d}w\\
\int\frac{1}{t}\,\mathrm{d}t & =\int\mathrm{d}w\\
\ln t & =\frac{y}{t}+C_{0} & C=-C_{0}\\
y & =t(\ln t+C)\\
1 & =1(0+C)=C\\
y & =t\ln t+t
\end{align*}

\section*{2.6}

Do only the case for h=0.1

\subsection*{2}

Approximate the solution to the IVP $y'=4x-y+1,y(0)=0$at $x=1$for
$h=0.1$
\begin{align*}
y_{0} & =0\\
y_{1} & =0.1*(4*0-0+1)+0=0.1\\
y_{2} & =0.1*(4*0.1-0.1+1)+0.1=0.23\\
y_{3} & =0.1*(4*0.2-0.23+1)+0.23=0.387\\
y_{4} & =0.1*(4*0.3-0.387+1)+0.387=0.5683\\
y_{5} & =0.1*(4*0.4-0.5683+1)+0.5683=0.77147\\
y_{6} & =0.1*(4*0.5-0.77147+1)+0.77147=.9943230000000001\\
y_{7} & =0.1*(4*0.6-.994323+1)+.994323=1.2348907\\
y_{8} & =0.1*(4*0.7-1.2348907+1)+1.2348907=1.49140163\\
y_{9} & =0.1*(4*0.8-1.49140163+1)+1.49140163=1.762261467\\
y_{10} & =0.1*(4*0.9-1.762261467+1)+1.762261467=2.0460353203\\
y(1) & \approx2.0460353203
\end{align*}

\subsection*{3}

Approximate the solution to the IVP $y'-x=y^{2}-1,y(0)=1$at $x=1$for
$h=0.1$
\begin{align*}
y_{0} & =1\\
y_{1} & =0.1*(y_{0}^{2}-1+0)+y_{0}=1\\
y_{2} & =0.1*(y_{1}^{2}-1+0.1)+y_{1}=1.01\\
y_{3} & =0.1*(y_{2}^{2}-1+0.2)+y_{2}=1.03201\\
y_{4} & =0.1*(y_{3}^{2}-1+0.3)+y_{3}=1.06851446401\\
y_{5} & =0.1*(y_{4}^{2}-1+0.4)+y_{4}=1.122686779989858\\
y_{6} & =0.1*(y_{5}^{2}-1+0.5)+y_{5}=1.198729340586257\\
y_{7} & =0.1*(y_{6}^{2}-1+0.6)+y_{6}=1.302424543784494\\
y_{8} & =0.1*(y_{7}^{2}-1+0.7)+y_{7}=1.442055513009718\\
y_{9} & =0.1*(y_{8}^{2}-1+0.8)+y_{8}=1.630007923269891\\
y_{10} & =0.1*(y_{9}^{2}-1+0.9)+y_{9}=1.885700506262153\\
y(1) & \approx1.885700506262153
\end{align*}

\section*{3.1}

\subsection*{5}

Suppose that the half-life of an element is 1000 h. If there are initially
100 g, how much remains after 1 h? How much remains after 500 h?
\begin{align*}
y(0) & =100\\
y(1000) & =50\\
y(t) & =100e^{kt}\\
50 & =100e^{1000m}\\
\left(\frac{5}{10}\right)^{1/1000} & =e^{m}\\
\frac{\ln\frac{1}{2}}{1000} & =m\\
y(1) & =100e^{m}=99.93070929904525\mathrm{g}\\
y(500) & =100e^{500m}=70.71067811865476\mathrm{g}
\end{align*}

\subsection*{6}

Suppose that the population of a small town is initially 5000. Due
to the construction of an interstate highway, the population doubles
over the next year. If the rate of growth is proportional to the current
population, when will the population reach 25,000? What is the population
after 5 years?
\begin{align*}
y(0) & =5000\\
y(1) & =10000\\
y(t) & =y_{0}e^{mt}\\
10000 & =5000e^{m}\\
\ln2 & =m\\
25000 & =5000e^{t\ln2}\\
\ln5 & =t\ln2\\
t & =\frac{\ln5}{\ln2}\approx2.3\mathrm{years}\\
y(5) & =5000e^{5\ln2}=5000*2^{5}=160000\mathrm{people}
\end{align*}

\end{document}


\section{Planar Approximation}
It is time to deal with the pole situation. The north and south poles that is, not the lovely people over in Poland. We run into problems because the latitude longitude grid cells become to small
near the poles. Therefore, the magnitudes no longer fit into one cell and overflow into other cells which makes everything kind of funky. So we need to fix that, and we do that by a planar
approximation.

\subsection{The Initial Theory}
As said earlier, the grid cells on the latitude longitude grid get closer together the closer you get to the poles which poses problems. To fix this, we will be using a planar approximation of
the poles. What this means is that we will map the 3D grid near the poles onto a 2D plane parallel to the poles, as if we put a giant flat plane in the exact center of the poles and draw lines
from the grid directly upwards to the plane. For a visual representation, please consult the stream with timestamp 1:38:25 \cite{polarPlane}, which includes some explanation. In the streamm we
use $r$ to indicate the radius of the planet (which we assume is a sphere), $\theta$ for the longitude and $\lambda$ for the latitude. So we have spherical coordinates, which we need to transform
into $x$ and $y$ coordinates on the plane. We also need the distance between the center point (the point where the plane touches the planet which is the center of the pole) and the projected
point on the plane from the grid (the location on the plane where a line from the gird upwards to the plane hits it). This distance is denoted by $a$ (Simon chose this one, not me). We then get
the following equations as shown in \autoref{eq:polar distance}, \autoref{eq:polar x} and \autoref{eq:polar y}.

\begin{subequations}
    \begin{equation}
        \label{eq:polar distance}
        a = r \cos(\theta)
    \end{equation}
    \begin{equation}
        \label{eq:polar x}
        x = a \sin(\lambda)
    \end{equation}
    \begin{equation}
        \label{eq:polar y}
        y = a \cos(\lambda)
    \end{equation}
\end{subequations}

But what if we know $x$ and $y$ and want to know $\theta$ and $\lambda$? Pythagoras' Theorem then comes into play \cite{pythagoras}. We know that (due to Pythagoras) \autoref{eq:pythagoras} must
always be true. Then if we substitue $a$ by $\sqrt{x^2 + y^2}$ in \autoref{eq:polar distance} we get \autoref{eq:polar theta1}. Then we transform that equation such that we only have $\theta$ on
one side and the rest on the other side (since we want to know $\theta$) and we get \autoref{eq:polar theta3}.
\begin{equation}
    \label{eq:pythagoras}
    x^2 + y^2 = a^2
\end{equation}

\begin{subequations}
    \begin{equation}
        \label{eq:polar theta1}
        \sqrt{x^2 + y^2} = r\cos(\theta)
    \end{equation}
    \begin{equation}
        \label{eq:polar theta2}
        \frac{\sqrt{x^2 + y^2}}{r} = \cos(\theta)
    \end{equation}
    \begin{equation}
        \label{eq:polar theta3}
        \arccos(\frac{\sqrt{x^2 + y^2}}{r}) = \theta
    \end{equation}
\end{subequations}

For $\lambda$ we need another trigonometric function which is the tangent ($\tan$). The tangent is defined in \autoref{eq:tan}. If we then take a look at \autoref{eq:polar x} and
\autoref{eq:polar y}, we see that $\lambda$ is present in both equations. So we need to use both to get $\lambda$ \footnote{Yes you could only use one but since we both know $x$ and $y$ it is a
bit easier to use both than to only use one as you need to know $\theta$ at that point as well which may or may not be the case.}. So let's combine \autoref{eq:polar x} and \autoref{eq:polar y}
in \autoref{eq:polar lambda1}, transform it such that we end up with only $\lambda$ on one side and the rest on the other side and we end up with \autoref{eq:polar lambda3}.

\begin{equation}
    \label{eq:tan}
    \tan(\alpha) = \frac{\sin(\alpha)}{\cos(\alpha)}
\end{equation}

\begin{subequations}
    \begin{equation}
        \label{eq:polar lambda1}
        \frac{x}{y} = \frac{a\sin(\lambda)}{a\cos(\lambda)} = \frac{\sin(\lambda)}{\cos(\lambda)}
    \end{equation}
    \begin{equation}
        \label{eq:polar lambda2}
        \frac{x}{y} = \tan(\lambda)
    \end{equation}
    \begin{equation}
        \label{eq:polar lambda3}
        \lambda = \arctan(\frac{x}{y})
    \end{equation}
\end{subequations}

\subsection{The Grid Code}
To start the planar approximation, we first need to create a grid. One for the north pole, and one for the south pole. Now since the project is made in Python, Simon uses a function to generate
a grid from two coordinate vectors (lists with a coordinate as elements). Since the documentation is not language specific I instead opt to use words instead of function calls. What that comes
down to is use your favourite language and import the libraries that do this for you, or start coding your own after finding out how to do that (your mileage may vary). To implement the grid
function in the exact same way as the numpy packages does, please refer to the following two references \cite{meshgridDoc} \cite{meshgridGFG}. Anyway, the code for the grid can be found in
\autoref{alg:polar grid south}. To convert $x, y$ coordinates into $lat, lon$ coordinates, we make use of \autoref{eq:polar theta3} and \autoref{eq:polar lambda3}. Keep in mind that the equations
themselves assume that the angles are in radians, whereas the model uses the angles in degrees so they need to be converted. To convert from $lat, lon$ back into $x, y$ we need to combine
\autoref{eq:polar distance}, \autoref{eq:polar x} and \autoref{eq:polar y}.

\begin{algorithm}[htb]
    \caption{Generating the grid for polar approximation of the south pole}
    \label{alg:polar grid south}
    \SetKwComment{Comment}{//}{}
    $poleLowIndexS \leftarrow $ find first index where $lat > poleLowerLatLimit$ \;
    $poleHighIndexS \leftarrow $ find first index where $lat > poleHigherLatLimit$ \;
    $polarGridResolution \leftarrow dx[-poleLowIndexS] $ \Comment*[l]{Will be reused for the north pole}
    $gridSize \leftarrow r \cos(lat[-poleLowIndexS] \frac{\pi}{180})$ \Comment*[l]{Will be reused for the north pole}
    \BlankLine

    $gridXAxisS \leftarrow $ array going from $-gridSize$ to $gridSize$ with steps of $polarGridResolution$ \;
    $gridYAxisS \leftarrow $ array going from $-gridSize$ to $gridSize$ with steps of $polarGridResolution$ \;
    $gridXValuesS, gridYValuesS \leftarrow $ generate the grid from the two axis vectors $gridXAxissS$ and $gridYAxissS$ \;
    $gridSideLength \leftarrow gridXValuesS.length $ \Comment*[l]{Is globally available\dots}
    \BlankLine

    $gridLatCoordsS \leftarrow $ empty list \;
    $gridLonCoordsS \leftarrow $ empty list \;
    \For{$i \leftarrow 0$ \KwTo $gridXValuesS.length$}{
        \For{$j \leftarrow 0$ \KwTo $gridXValuesS[i].length$}{
            $x \leftarrow gridXValuesS[i, j]$ \;
            $y \leftarrow gridYValuesS[i, j]$ \;
            $latPoint \leftarrow -\arccos(\frac{\sqrt{x^2 + y^2}}{r})\frac{180}{\pi}$ \;
            $lonPoint \leftarrow 180 - \arctan(\frac{x}{y})\frac{180}{\pi}$ \;
            $gridLatCoordsS.append(latPoint)$ \;
            $gridLonCoordsS.append(lonPoint)$ \;
        }
    }

    \BlankLine
    $polarXCoordsS \leftarrow$ empty list \;
    $polarYCoordsS \leftarrow$ empty list \;
    \For{$i \leftarrow 0$ \KwTo $poleLowIndexS$}{
        \For{$j \leftarrow 0$ \KwTo $nlon$}{
            $polarXCoordsS.append(r \cos(lat[i] \frac{\pi}{180}) \sin(lon[j]\frac{\pi}{180}))$ \;
            $polarYCoordsS.append(-r \cos(lat[i] \frac{\pi}{180}) \cos(lon[j]\frac{\pi}{180}))$ \;
        }
    }
\end{algorithm}

We need to do a similar thing for the north pole and insert a few changes to some of the equations to correct for different angles and similar things. The code can be found in
\autoref{alg:polar grid north}. Again, see the references at the south pole explanation on how to generate the grid itself.

\begin{algorithm}[htb]
    \caption{Generating the grid for polar approximation of the north pole}
    \label{alg:polar grid north}
    \SetKwComment{Comment}{//}{}
    $poleLowIndexN \leftarrow $ find last index where $lat < -poleLowerLatLimit$ \;
    $poleHighIndexN \leftarrow $ find last index where $lat < -poleHigherLatLimit$ \;
    \BlankLine

    $gridXAxisN \leftarrow $ array going from $-gridSize$ to $gridSize$ with steps of $polarGridResolution$ \;
    $gridYAxisN \leftarrow $ array going from $-gridSize$ to $gridSize$ with steps of $polarGridResolution$ \;
    $gridXValuesN, gridYValuesN \leftarrow $ generate the grid from the two axis vectors $gridXAxisN$ and $gridYAxisN$ \;
    \BlankLine

    $gridLatCoordsN \leftarrow $ empty list \;
    $gridLonCoordsN \leftarrow $ empty list \;
    \For{$i \leftarrow 0$ \KwTo $gridXValuesN.length$}{
        \For{$j \leftarrow 0$ \KwTo $gridXValuesN[i].length$}{
            $x \leftarrow gridXValuesN[i, j]$ \;
            $y \leftarrow gridYValuesN[i, j]$ \;
            $latPoint \leftarrow -\arccos(\frac{\sqrt{x^2 + y^2}}{r})\frac{180}{\pi}$ \;
            $lonPoint \leftarrow 180 - \arctan(\frac{x}{y})\frac{180}{\pi}$ \;
            $gridLatCoordsN.append(latPoint)$ \;
            $gridLonCoordsN.append(lonPoint)$ \;
        }
    }

    \BlankLine
    $polarXCoordsN \leftarrow$ empty list \;
    $polarYCoordsN \leftarrow$ empty list \;
    \For{$i \leftarrow 0$ \KwTo $poleLowIndexN$}{
        \For{$j \leftarrow 0$ \KwTo $nlon$}{
            $polarXCoordsN.append(r \cos(lat[i] \frac{\pi}{180}) \sin(lon[j]\frac{\pi}{180}))$ \;
            $polarYCoordsN.append(-r \cos(lat[i] \frac{\pi}{180}) \cos(lon[j]\frac{\pi}{180}))$ \;
        }
    }
\end{algorithm}

In both algorithms it is important to make a distinction between $gridLatCoords$ and $polarXCoords$ and their respective variants. $gridLatCoords$ are the latitudinal coordinates on the
$lat, lon$ grid corresponding to the $x$ and $y$ coordinates on the polar grid. Whereas $polarXCoords$ are the $x$ coordinates on the polar grid corresponding to the latitude and longitude on
the $lat, lon$ grid. Those are different to the $gridXValues$ as they represent the values on the $x$ axis as integers which may or may not directly correspond to the $polarXCoords$. So we need
a way of mapping the $polarXCoords$ to $gridXValues$ and their respective values and vice versa. This is done in the next section.

\subsection{Switching between grids}
Now that we have defined the polar plane grid, we need code to convert the values from the $lat, lon$ grid to the polar plane grid and vice versa. Let's start with converting to the polar grid.
We need 2 versions of the algorithm for that. One that converts in 2 dimensions, and one that converts in 3 dimensions. The code for the 2 dimensional case is shown in \autoref{alg:beam up 2d}.
Here we use bivariate spline interpolation \cite{bivariatespline} which is a different form of linear interpolation than discussed in \autoref{sec:interpolation} though the same principle
applies.

\begin{algorithm}[htb]
    \caption{Converting from $lat, lon$ grid to polar plane grid in 2 dimensions}
    \label{alg:beam up 2d}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Latitude coordinates $lat$, Longitude coordinates $lon$, Values of the $lat, lon$ grid $data$, Length of one axis of the polar grid? $gridSize$, Latitude coordinates on the polar grid
        $gridLatCoords$, Longitude coordinates on the polar grid $gridLonCoords$}
    \Output{Double array representing the values of the $lat, lon$ grid on the polar grid}
    \SetKwComment{Comment}{//}{}
    $f \leftarrow $ \texttt{BivariateSpline}($lat, lon, data$) \Comment*[l]{Do the interpolation}
    $polarPlane \leftarrow f(gridLatCoords, gridLonCoords).$\texttt{reshape}(($gridSize$, $gridSize$)) \Comment*[l]{Check the values of the interpolation at the specified coordinates and force
    them to align to the polar grid}
    \Return{$polarPlane$}
\end{algorithm}

The 3 dimensional algorithm is quite similar to the 2 dimensional algorithm, which can be found in \autoref{alg:beam up 3d}

\begin{algorithm}[htb]
    \caption{Converting from $lat, lon$ grid to polar plane grid in 3 dimensions}
    \label{alg:beam up 3d}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Latitude coordinates $lat$, Longitude coordinates $lon$, Values of the $lat, lon$ grid $data$, Length of one axis of the polar grid? $gridSize$, Latitude coordinates on the polar grid
        $gridLatCoords$, Longitude coordinates on the polar grid $gridLonCoords$}
    \Output{Triple array representing the values of the $lat, lon, layer$ grid on the polar grid}
    \SetKwComment{Comment}{//}{}
    $polarPlane \leftarrow $ 3 Dimensional array where the $1^{\text{st}}$ and $2^{\text{nd}}$ dimensions have length $gridSize$ and the $3^{\text{rd}}$ dimension has length $data[0][0].length$ \;
    \For{$k \leftarrow 0$ \KwTo $data[0][0].length$}{
        $f \leftarrow $ \texttt{BivariateSpline}($lat, lon, data[:, :, k]$) \Comment*[l]{Do the interpolation on this layer}
        $polarPlane[:, :, k] \leftarrow f(gridLatCoords, gridLonCoords).$\texttt{reshape}(($gridSize$, $gridSize$)) \Comment*[l]{Check the values of the interpolation at the specified
        coordinates and force them to align to the polar grid}
    }
    \Return{$polarPlane$}
\end{algorithm}

Having dealt with converting to the polar grid, we now also need to deal with converting from the polar grid. In contrast to converting to the polar griod, this is only done in 3 dimensions so
we do not need a 2 dimensional algorithm. How we convert from the polar grid can be found in \autoref{alg:beam down}.

\begin{algorithm}[htb]
    \caption{Converting from the polar plane grid to the $lat, lon$ grid in 3 dimensions}
    \label{alg:beam down}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Longitude coordinates $lon$, Values of the $lat, lon$ grid $data$, Polar $x$ value indices $gridXValues$, Polar $y$ value indices $gridYValues$, Polar $x$ coordinates $polarXCoords$,
        Polar $y$ coordinates $polarYCoords$}
    \Output{Triple array representing the values of the polar grid on the $lat, lon, layer$ grid}
    \SetKwComment{Comment}{//}{}
    $resample \leftarrow $3 Dimensional array where the $1^{\text{st}}$ dimension has length $\lfloor \frac{polarXCoords}{lon.length} \rfloor$, the $2^{\text{nd}}$ dimension has length
    $lon.length$ and the $3^{\text{rd}}$ dimension has length $data[0][0].length$ \;
    \For{$k \leftarrow 0$ \KwTo $data[0][0].length$}{
        $f \leftarrow $ \texttt{BivariateSpline})($gridXValues, gridYValues, data[:, :, k]$) \Comment*[l]{Do the interpolation on this layer}
        $resample[:, :, k] \leftarrow f(polarXCoords, polarYCoords).$\texttt{reshape}(($\lfloor \frac{polarXCoords}{lon.length} \rfloor, lon.length$)) \Comment*[l]{Check the values of the
        interpolation at the specified coordinates and force them to align to the polar grid}
    }
    \Return{$resample$}
\end{algorithm}

\subsection{Gradually changing grids}
Now that we can convert between grids we also need a way to do so gradually. Otherwise we would move the hard border we had previously around the poles further down the $lat, lon$ grid. Instead
we have to do some interpolation between the two grids in order to ensure a smooth transition in the final output, so that there are no hard borders. This interpolation is done in , using the
linear interpolation technique as discussed in \autoref{sec:interpolation}.

\begin{algorithm}[htb]
    \caption{Gradually transition from the $lat, lon$ grid to the polar grid}
    \label{alg:polar interpolation}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Index when to start using the polar grid $poleLowIndex$, Index when to only use the polar grid $poleHighIndex$, Polar data $polarData$, $lat, lon$ data $sphericalData$}
    \Output{3 Dimensional array representing the values on the polar grid with the part that gradually transitions to the $lat, lon$ grid}
    \SetKwComment{Comment}{//}{}
    $output \leftarrow$ 3 Dimensional array with the exact same structure as $polarData$ \;
    $overlap \leftarrow $ $|poleLowIndex - poleHighIndex|$ \;

    \BlankLine
    \uIf(\Comment*[h]{Determine whether we are talking about the north or south pole}){$lat[poleLowIndex] < 0$}{
        \DontPrintSemicolon
        \Comment*[l]{South pole}
        \PrintSemicolon
        \For{$k \leftarrow 0$ \KwTo $output[0][0].length$}{
            \For{$i \leftarrow 0$ \KwTo $poleLowIndex$}{
                \uIf{$i < poleHighIndex$}{
                    $\lambda \leftarrow 0$ \;
                } \uElse{
                    $\lambda \leftarrow \frac{i - poleHighIndex}{overlap}$ \;
                }
                $output[i, :, k] \leftarrow (1 - \lambda) sphericalData[i, :, k] + \lambda polarData[i, :, k]$ \;
            }
        }
    } \uElse{
        \DontPrintSemicolon
        \Comment*[l]{North pole}
        \PrintSemicolon
        \For{$k \leftarrow 0$ \KwTo $output[0][0].length$}{
            \For{$i \leftarrow 0$ \KwTo $nlat - poleLowIndex$}{
                \uIf{$i + poleLowIndex + 1 > poleHighIndex$}{
                    $\lambda \leftarrow 0$ \;
                } \uElse{
                    $\lambda \leftarrow \frac{i}{overlap}$ \;
                }
                $output[i, :, k] \leftarrow (1 - \lambda) sphericalData[i, :, k] + \lambda polarData[i, :, k]$ \;
            }
        }
    }

    \BlankLine
    \Return{$output$}
\end{algorithm}

\subsection{Gradients on the grid}
With our new found ability to convert to and from the polar grid, while also gradually transitioning, we now get to the part we are doing it all for. Calculations on the polar grid. In order for
that to work, we need some utility functions specifically for the polar grid first. We will need gradients in all 3 dimensions, those being the $x$ dimension, the $y$ dimension and the $p$
dimension (pressure). All of the gradients will be quite similar to \autoref{alg:gradient x}, \autoref{alg:gradient y} and \autoref{alg:gradient z} though some small tweaks are required as we
are not differentiating over a spehere but over a plane. These changes are reflected in \autoref{alg:polar gradient x}, <Y> and <Z>.

\begin{algorithm}
    \caption{Gradient in the $x$ dimension on the polar grid}
    \label{alg:polar gradient x}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Triple array of data $data$, first index $i$, second index $j$ and third index $k$}
    \Output{Gradient in the $x$ dimension for the value of the grid point at the specified coordinates}
    \uIf{$p = 0$}{
        $value \leftarrow \frac{data[i, j + 1, k] - data[i, j, k]}{polarGridResolution}$ \;
    } \uElseIf{$y = gridSideLength - 1$}{
        $value \leftarrow \frac{data[i, j, k] - data[i, j - 1, k]}{polarGridResolution}$ \;
    } \uElse{
        $vale \leftarrow \frac{data[i, j + 1, k] - data[i, j - 1, k]}{2 polarGridResolution}$ \;
    }
    \Return{$value$}
\end{algorithm}


\section{Results and Discussion}

We present results with different types of flow field data sets to evaluate our approach, including synthetic data and spatially aggregated data sets. To demonstrate the effectiveness of the proposed algorithm, we compared it with the Monte Carlo (MC) method, which is the general approach to stochastically trace particles in uncertain flow fields modeled in probability distributions. We performed quantitative comparisons on the resulting streamlines generated by our approach and the MC method with different settings and distance measurements. We also qualitatively compare the most likely streamlines as well as the distributions of possible traces produced by our approach and the MC method by visualizing sample traces on different data sets.

\subsection{Synthetic Data}

We first evaluate the proposed algorithm on the analytical static double-gyre data set proposed by Shadden et al. in~\cite{Shadden2005271}. Gaussian noise is added into the vector field to synthesize the uncertainty. The uncertain flow can be described as the stream-function
\begin{equation}
  \psi(x,y) = A\sin(\pi x)\sin(\pi y) + N(0,{\sigma ^2})
\end{equation}
over the domain $[0,2]\times[0,1]$. In order to quantitatively evaluate the robustness of the particle filtering algorithm under the influence of noise, we generate streamlines starting from regularly sampled seed positions for the certain double-gyre data set and use those streamlines as our ground truth. Then, a set of sample traces are generated by the MC method and the particle filtering method starting from the same seed position presented above for the uncertain double-gyre data set with different noise level, which is controlled by the standard deviation $\sigma$ of the Gaussian noise. All the streamlines were generated with a step size of $0.005$ and a maximum step number of $100$. For the particle filtering method, a concentration parameter $\kappa = 60$ and a resampling threshold $N_t = 4.0$ are used. For the Monte Carlo method and the proposed algorithm, a critical parameter is the number of particles used for each seed. Indeed, more particles will give a more accurate presentation of the target distribution but will take more time to generate the results. Hence, a particle count that balances the accuracy and the computation time need to be studied. Based on~\cite{journals/mia/PontabryROSKD13}, we use $100$ particles for both of the methods in the experiments. To compare the accuracy of the resulting traces, two pairwise distance metrics between streamlines $L_i$ and $L_j$ were used:

1. Hausdorff distance $d_H$~\cite{Roessl:2012:TVCG}, which measures how far two streamlines are from each other:
\begin{equation}
\begin{split}
  {d_H}({L_i},{L_j}) = max({d_h}({L_i},{L_j}),{d_h}({L_j},{L_i})) \\
  \text{with  } {d_h}({L_i},{L_j}) = ma{x_{{p_l} \in {L_i}}}{\min _{{p_k} \in {L_j}}}\left\| {{p_k} - {p_l}} \right\|
\end{split}
\end{equation}

2. Mean of the closest point distance $d_M$~\cite{Corouge04towardsa}, which gives the average distance between two streamlines:
\begin{equation}
\begin{split}
  {d_M}({L_i},{L_j}) = mean({d_m}({L_i},{L_j}),{d_m}({L_j},{L_i})) \\
  \text{with  } {d_m}({L_i},{L_j}) = mea{n_{{p_l} \in {L_i}}}{\min _{{p_k} \in {L_j}}}\left\| {{p_k} - {p_l}} \right\|
\end{split}
\end{equation}

Figure~\ref{gerror} gives the average of the distances presented above between the most likely streamlines generated from each method and the ground truth, with increasing $\sigma$ values for the noise in the vector field. The figure reveals that our method can produce most likely traces that are closer to the ground truth and the average of the distances increases more slowly than the MC method as the noise increases.

Besides comparing the accuracy of the most likely traces, it is also important to compare the whole distribution of possible traces generated by each method. We evaluate the accuracy of uncertain streamlines starting from a given seed position by measuring the distance between each individual trace and the ground truth, then we compute the weighted sum of all the distances. For the MC method, all traces are equally weighted by $\frac{1}{N_s}$. For the proposed method, the weights of the traces described above are used. Figure~\ref{gerror_r} shows that the proposed method can generate more accurate traces with less uncertainty.

\begin{figure}[!htb]
  \centering
  \begin{subfigure}[b]{0.24\textwidth}
    \centering
    \includegraphics[height=1.0in]{../figures/doublegyre_h.eps}
    \caption{}
  \end{subfigure}~
  \begin{subfigure}[b]{0.24\textwidth}
    \centering
    \includegraphics[height=1.0in]{../figures/doublegyre_m.eps}
    \caption{}
  \end{subfigure}
  \caption{Comparison of the distance between the most likely traces and the ground truth using Hausdorff and mean of the closest point measurements for our method and the MC method.}
  \label{gerror}
\end{figure}

\begin{figure}[!htb]
  \centering
  \begin{subfigure}[b]{0.24\textwidth}
    \centering
    \includegraphics[height=1.0in]{../figures/doublegyre_hr.eps}
    \caption{}
  \end{subfigure}~
  \begin{subfigure}[b]{0.24\textwidth}
    \centering
    \includegraphics[height=1.0in]{../figures/doublegyre_mr.eps}
    \caption{}
  \end{subfigure}
  \caption{Comparison of overall trace accuracy. For each method, distances of all sample traces to the ground truth are measured and summed by their weights.}
  \label{gerror_r}
\end{figure}

Figure~\ref{case_1} shows sample traces generated by the MC method and the proposed method at a given seed location in the double-gyre flow field. As we can see in the figure, our method can generate more concentrated traces which are also closer to the ground truth compared with the MC method. The most likely trace generated by the proposed method is also closer to the ground truth, as shown in~\ref{case_1_c}.

\begin{figure}[!htb]
  \centering
  \begin{subfigure}[!htb]{0.25\textwidth}
    \centering
    \includegraphics[height=0.8in]{../figures/double_gyre_mc35.eps}
    \caption{}
    \label{case_1_a}
  \end{subfigure}~
  \begin{subfigure}[!htb]{0.25\textwidth}
    \centering
    \includegraphics[height=0.8in]{../figures/double_gyre_smc35.eps}
    \caption{}
    \label{case_1_b}
  \end{subfigure}

  \begin{subfigure}[!htb]{0.5\textwidth}
    \centering
    \includegraphics[height=1.0in]{../figures/double_gyre_opt35.eps}
    \caption{}
    \label{case_1_c}
  \end{subfigure}
  \caption{(a): Sampled streamlines computed by the MC method starting from seeding position $x=0.3, y=0.5$ in the analytical double-gyre data set. (b): Sampled streamlines computed by our method from the same seeding position in (a). (c): The most likely traces generated by both methods compared with the ground truth.}
  \label{case_1}
\end{figure}

\subsection{Spatially aggregated Data Sets}

In this section, experiments were done on two real-world flow field data sets, including Hurricane Isabel and 2D Ocean. Hurricane Isabel is a data set with a resolution of $500 \times 500 \times 100$ that models a strong hurricane in the west Atlantic region in September 2003. The 2D Ocean data set is a flow data with a resolution of $574 \times 289$ produced by a 2D computational fluid dynamics simulation. In order to test the performance of our algorithm on uncertain data which represented by non-gaussian distributions, we decompose the data into small cubic blocks and construct a histogram for each block. To compute a histogram from a set of vector directions, we consider the surface patches of a unit sphere as histogram bins. We make use of the recursive zonal equal area sphere partitioning algorithm~\cite{leopardi2006} to partition the unit sphere into patches of equal area and use them as our histograms bins. Partition results for different number of patches on the $2$-dimensional unit sphere are shown in Figure~\ref{rzeasp}. In the experiments, we partition the sphere into $2048$ patches to make an accurate histogram. As Carr et at. presented in~\cite{Carr:2006:HIS:1187627.1187777}, histograms can be poor representations of data since they represent the distribution using the nearest neighbor interpolation. To address this issue, we densely sample the data in each local block to make the histogram converge to the true local distribution. For the test data sets, distribution-based data are generated with three different block size $8^3$, $16^3$, and $32^3$ to evaluate the performance of the proposed algorithm under the influence of uncertainty.

Since the data are represented as block-wise histograms, how to interpolate between neighboring blocks to generate a more accurate probability distribution becomes a challenging question. Developing efficient and accurate interpolation technique from probability density functions is still an active area for research. A variety of techniques~\cite{10.1109/TVCG.2013.208, 10.1109/TVCG.2012.249} have been proposed to perform linear interpolation on different types of distributions. However, they are only focused on either uniform or parametric distributions. Furthermore, these techniques can only work on point based data sets. They may give incorrect value ranges for block-based data sets used in the experiments. Here, we perform the interpolation by aggregating the contributions of neighboring histograms to the distribution of interest based on the method presented by Clemen et al. in~\cite{aggregation99}
\begin{equation}
  {h(\theta)} = k\prod\limits_{i = 1}^n {h{{_i}(\theta)}^{{\alpha _i}}}
\end{equation}
where $k$ is a normalizing constant and the weights ${{\alpha _i}}$ represent the relative quality of the different histograms. Typically, the weights are restricted to sum to one. The contribution of each histogram is estimated by applying convolution operations on it with a von Mises-Fisher kernel, where the number of the convolution operations is determined based on the distance between the neighboring block and target position.

\begin{figure}[htb!f]
  \centering
  \begin{subfigure}[b]{0.16\textwidth}
    \centering
    \includegraphics[width=0.8in]{../figures/rzeasp_16.eps}
    \caption{16 patches}
  \end{subfigure}~
  \begin{subfigure}[b]{0.16\textwidth}
    \centering
    \includegraphics[width=0.8in]{../figures/rzeasp_64.eps}
    \caption{64 patches}
  \end{subfigure}~
  \begin{subfigure}[b]{0.16\textwidth}
    \centering
    \includegraphics[width=0.8in]{../figures/rzeasp_256.eps}
    \caption{256 patches}
  \end{subfigure}
  \caption{Recursive zonal equal area sphere partitioning with different number of patches.}
  \label{rzeasp}
\end{figure}

As presented above, we regularly sample a set of seed locations and compute the streamlines for both the raw data and the spatially down sampled data. The sample seed positions and the number of samples for the two test data sets are given in Table~\ref{seed_position}. To perform the quantitative analysis, we treat the streamlines computed from the raw data as the ground truth and compute the distance between the stochastic particle traces with the ground truth. $100$ particles were used with a integration step size $1.0$ and a maximum step number of $1000$ for the streamline computation. For the particle filtering method, a concentration parameter $\kappa = 60$ and a resampling threshold $N_t = 4.0$ are used. Figure~\ref{berror_r} gives the mean of the distances' weighted sum between sample streamline bundles generated from the test methods and the ground truth on different data sets with different block sizes. The figure reveals that the proposed method can produce traces that are closer to the ground truth.

\begin{table}[ht!b]
\centering
\begin{tabular}{|l|l|l|}
\hline
Data Set & Number of Seeds                 & Interval of Seed Positions  \\ \hline
Isabel   & $12 \times 12 \times 9$ (1296)  & $40 \times 40 \times 10$    \\ \hline
Ocean    & $28 \times 14$ (392)            & $20 \times 20$              \\ \hline
\end{tabular}
\caption{Sample seed positions for the two test data sets.}
\label{seed_position}
\end{table}

% \begin{figure}[!htb]
%   \centering
%   \begin{subfigure}[b]{0.24\textwidth}
%     \centering
%     \includegraphics[height=0.9in]{../figures/doublegyre_h.eps}
%     \caption{}
%   \end{subfigure}~
%   \begin{subfigure}[b]{0.24\textwidth}
%     \centering
%     \includegraphics[height=0.9in]{../figures/doublegyre_m.eps}
%     \caption{}
%   \end{subfigure}

%   \begin{subfigure}[b]{0.24\textwidth}
%     \centering
%     \includegraphics[height=0.9in]{../figures/doublegyre_h.eps}
%     \caption{}
%   \end{subfigure}~
%   \begin{subfigure}[b]{0.24\textwidth}
%     \centering
%     \includegraphics[height=0.9in]{../figures/doublegyre_m.eps}
%     \caption{}
%   \end{subfigure}
%   \caption{Hausdorff and mean of the closest point distances between the ground truth and most likely traces generated by our method and the MC method for the two spacial down-sampled data sets.}
%   \label{berror}
% \end{figure}

\begin{figure}[!htb]
  \centering
  \begin{subfigure}[b]{0.24\textwidth}
    \centering
    \includegraphics[height=1.0in]{../figures/ocean_h.eps}
    \caption{}
  \end{subfigure}~
  \begin{subfigure}[b]{0.24\textwidth}
    \centering
    \includegraphics[height=1.0in]{../figures/isabel_h.eps}
    \caption{}
  \end{subfigure}

  \begin{subfigure}[b]{0.24\textwidth}
    \centering
    \includegraphics[height=1.0in]{../figures/ocean_m.eps}
    \caption{}
  \end{subfigure}~
  \begin{subfigure}[b]{0.24\textwidth}
    \centering
    \includegraphics[height=1.0in]{../figures/isabel_m.eps}
    \caption{}
  \end{subfigure}
  \caption{Hausdorff and mean of the closest point distances between the ground truth and sample traces generated by our method and the MC method for the two spatially aggregated data sets.}
  \label{berror_r}
\end{figure}

Figure~\ref{data_overview} shows the streamlines generated from the two test data sets on the seed positions presented above. In Figure~\ref{data_overview} (a) and (d), the ground truth streamlines are generated from the raw data. The most likely streamlines generated by the MC method on the distribution-based data set with block size $16^3$ are given in Figure~\ref{data_overview} (b) and (e); as expected, streamlines generated by the MC method are generally not as smooth as the ground truth and some flow features looks quiet different compare with the ground truth. Figure~\ref{data_overview} (c) and (f) show the streamlines produced by our method with the same block size, which give more accurate and smoother results. In addition to the most likely traces, the distribution approximated by the estimated sample traces is also important for understanding the distribution-based flow field. The visualization of the estimated streamline bundles on different flow field data sets are shown in Figure~\ref{case_5} and~\ref{case_4} to demonstrate the advantage of the proposed method. The resulting trajectories are generated with $1000$ particles for the Monte Carlo method and the particle filtering algorithm. By considering the correlation of vector directions in the spacial domain, our method is less sensitive to the local uncertainty, when MC traces can scatter to wrong directions, as show in ~\ref{case_5}. Figure~\ref{case_4} shows that our algorithm can produce more concentrated results than the basic MC method, because the correlation between consecutive integration steps (the prior information) are exploited.

\begin{figure*}[!htb]
  \centering
  \begin{subfigure}[!htb]{0.32\textwidth}
    \centering
    \includegraphics[width=2.2in]{../figures/ocean_gt.eps}
    \caption{}
  \end{subfigure}~
  \begin{subfigure}[!htb]{0.32\textwidth}
    \centering
    \includegraphics[width=2.2in]{../figures/ocean_mc.eps}
    \caption{}
  \end{subfigure}~
  \begin{subfigure}[!htb]{0.32\textwidth}
    \centering
    \includegraphics[width=2.2in]{../figures/ocean_smc.eps}
    \caption{}
  \end{subfigure}

  \begin{subfigure}[!htb]{0.32\textwidth}
    \centering
    \includegraphics[width=2.2in]{../figures/isabel_gt.eps}
    \caption{}
  \end{subfigure}~
  \begin{subfigure}[!htb]{0.32\textwidth}
    \centering
    \includegraphics[width=2.2in]{../figures/isabel_mc.eps}
    \caption{}
  \end{subfigure}~
  \begin{subfigure}[!htb]{0.32\textwidth}
    \centering
    \includegraphics[width=2.2in]{../figures/isabel_smc.eps}
    \caption{}
  \end{subfigure}

  \caption{Streamlines generated on the 2D Ocean and the Hurricane Isabel data sets. The color is used to enhance the contrast among streamlines. (a) and (d): The ground truth streamlines generated on the raw data. (b) and (e): Results produced by the Monte Carlo method on the distribution data with block size $16^3$, introduce noisy patterns on the streamlines due to the local uncertainty and give inaccurate overview of the flow features. Our particle filter method gives more accurate overview results and smoother streamlines by exploiting the spatial coherence of the vector directions, shown in (c) and (f).}
  \label{data_overview}
\end{figure*}

\begin{figure}[!htb]
  \centering
  \begin{subfigure}[!htb]{0.5\textwidth}
    \centering
    \includegraphics[width=2.8in]{../figures/ocean_mc1.eps}
    \caption{}
    \label{case_5_a}
  \end{subfigure}

  \begin{subfigure}[!htb]{0.5\textwidth}
    \centering
    \includegraphics[width=2.8in]{../figures/ocean_smc1.eps}
    \caption{}
    \label{case_5_b}
  \end{subfigure}
  \caption{(a): Sampled streamlines computed by the MC method starting from seeding position $x=280, y=140$ in the 2D Ocean data set. (b): Sampled streamlines computed by our method from the same seeding position in (a).}
  \label{case_5}
\end{figure}

\begin{figure}[!htb]
  \centering
  \begin{subfigure}[!htb]{0.25\textwidth}
    \centering
    \includegraphics[height=1.4in]{../figures/isabel_mc1.eps}
    \caption{}
    \label{case_4_a}
  \end{subfigure}~
  \begin{subfigure}[!htb]{0.25\textwidth}
    \centering
    \includegraphics[height=1.4in]{../figures/isabel_smc1.eps}
    \caption{}
    \label{case_4_b}
  \end{subfigure}
  \caption{(a): Sampled streamlines computed by the MC method starting from seeding position $x=250, y=150, z=45$ in the Isabel data set. (b): Sampled streamlines computed by our method from the same seeding position in (a).}
  \label{case_4}
\end{figure}

\subsection{Performance}

All the experiments were performed on a desktop computer with an Intel(R) Core(TM) i7-4790K CPU 4.0GHz processor, 16GB memory, and an NVIDIA GTX 970 GPU. In Table~\ref{timing}, we compare the performance measurements between the particle filtering algorithm and the Monte Carlo method for streamlines estimated for a given seed position with $100$ sample points for all the test data sets used in this paper. In all the datasets, our approach is almost as fast as the Monte Carlo algorithm.

\begin{table}[!htb]
\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
\multirow{2}{*}{Data Set}    & \multirow{2}{*}{Method}     & \multicolumn{3}{c|}{Timing(sec)}  \\ \cline{3-5}
                             &                             & 40 Steps  & 80 Steps & 120 Steps  \\ \hline
\multirow{2}{*}{Double Gyre} & MC                          & 0.0026    & 0.005    & 0.008      \\ \cline{2-5}
                             & Particle Filter             & 0.0035    & 0.007    & 0.01       \\ \hline
\multirow{2}{*}{Ocean 2D}    & MC                          & 2.8       & 5.9      & 9.1        \\ \cline{2-5}
                             & Particle Filter             & 2.9       & 6.1      & 9.3        \\ \hline
\multirow{2}{*}{Isabel}      & MC                          & 3.3       & 6.7      & 10.1       \\ \cline{2-5}
                             & Particle Filter             & 3.4       & 6.8      & 10.7       \\ \hline

\end{tabular}
\caption{Overview of the performance for the particle filtering algorithm and the Monte Carlo method tested on all the distribution-based data sets used in this paper.}
\label{timing}
\end{table}


\chapter{Graph}

\section{Fundamentals}
	\kactlimport{BellmanFord.h}
	\kactlimport{FloydWarshall.h}
	\kactlimport{TopoSort.h}

\section{Euler walk}
	\kactlimport{EulerWalk.h}

\section{Network flow}
	\kactlimport{PushRelabel.h}
	\kactlimport{MinCostMaxFlow.h}
	\kactlimport{EdmondsKarp.h}
	% \kactlimport{Dinic.h}
	\kactlimport{MinCut.h}
	\kactlimport{GlobalMinCut.h}
	\kactlimport{GomoryHu.h}

\section{Matching}
	\kactlimport{hopcroftKarp.h}
	\kactlimport{DFSMatching.h}
	\kactlimport{MinimumVertexCover.h}
	\kactlimport{WeightedMatching.h}
	\kactlimport{GeneralMatching.h}

\section{DFS algorithms}
	\kactlimport{SCC.h}
	\kactlimport{BiconnectedComponents.h}
	\kactlimport{2sat.h}

\section{Heuristics}
	\kactlimport{MaximalCliques.h}
	\kactlimport{MaximumClique.h}
	\kactlimport{MaximumIndependentSet.h}

\section{Trees}
	\kactlimport{BinaryLifting.h}
	\kactlimport{LCA.h}
	\kactlimport{CompressTree.h}
	\kactlimport{HLD.h}
	\kactlimport{LinkCutTree.h}
	\kactlimport{DirectedMST.h}

\section{Math}
	\subsection{Number of Spanning Trees}
		% I.e. matrix-tree theorem.
		% Source: https://en.wikipedia.org/wiki/Kirchhoff%27s_theorem
		% Test: stress-tests/graph/matrix-tree.cpp
		Create an $N\times N$ matrix \texttt{mat}, and for each edge $a \rightarrow b \in G$, do
		\texttt{mat[a][b]--, mat[b][b]++} (and \texttt{mat[b][a]--, mat[a][a]++} if $G$ is undirected).
		Remove the $i$th row and column and take the determinant; this yields the number of directed spanning trees rooted at $i$
		(if $G$ is undirected, remove any row/column).

	\subsection{Erdős–Gallai theorem}
		% Source: https://en.wikipedia.org/wiki/Erd%C5%91s%E2%80%93Gallai_theorem
		% Test: stress-tests/graph/matrix-tree.cpp
		A simple graph with node degrees $d_1 \ge \dots \ge d_n$ exists iff $d_1 + \dots + d_n$ is even and for every $k = 1\dots n$,
		\[ \sum _{i=1}^{k}d_{i}\leq k(k-1)+\sum _{i=k+1}^{n}\min(d_{i},k). \]