\lesson{6}{Sep 27 2021 Mon (07:30:12)}{Polynomial Operations}{Unit 1}

\subsubsection*{Adding Polynomials}

The most important part of adding or subtracting polynomials is identifying
like terms. Like terms are terms containing the exact same “variable part.”
Exponents for the variables must be exactly the same. Coefficients can, and
probably will, be different. Identify the like terms in the following matching
exercise.

\begin{example}[Adding Polynomials]
    Add: $(2x^3 + 4x^2 - x + 7) + (3x^2 + 6x + 10)$

    \begin{enumerate}
        \item Again, there is an understood $1$ in front of each set of
            parentheses. As you've seen, distributing this $1$ will not change
            the expression. Therefore, the parentheses may simply be removed.
        \item Highlight each pair of terms containing the same variable part.
            Combine the like terms.
    \end{enumerate}

    Note: Since there is no other term with a variable part of $x^3$, the term
    $2x^3$ stays the same in the final answer.

    \begin{align}
        (2x^3 + 4x^2 - x + 7) + (3x^2 + 6x + 10) &= 2x^3 + 4x^2 - x + 7 + 3x^2 + 6x + 10 \\
                                                 &= 2x^3 + 7x^2 + 5x + 17
    \end{align}
\end{example}

\subsubsection*{Subtracting Polynomials}

Subtracting polynomials is almost exactly like adding polynomials. The only
“difference” is that now a negative one must be distributed.

\begin{example}[Subtracting Polynomials]
    Subtract: $(4x^2 - 5) - (x^3 + 2x^2 + 7)$

    The understood $1$ in front of the first set of parentheses will not change
    the binomial within. However, the trinomial in the second set of
    parentheses will change because a negative one ($-1$) must be distributed!
    In effect, it will change the signs of those three terms.

    \begin{enumerate}
        \item Highlight each pair of terms containing the same variable part.
        \item  Combine the like terms. Since $-x^3$ does not have a like term,
            it is written down as is. Arrange terms in descending order, from
            greatest exponent to least.
    \end{enumerate}

    \begin{align}
        1(4x^2 - 5) - 1(x^3 + 2x^2 + 7) &= 4x^2 - 5 - x^3 - 2x^2 - 7 \\
                                        &= 4x^2 - 5 - x^3 - 2x^2 - 7 \\
                                        &= -x^3 + 2x^2 - 12
    \end{align}
\end{example}

\subsubsection*{Inverses of Functions}

Finding the inverse of an integer means performing an operation that will
cancel that number. For example, if you are given the number $7$, you could add
$-7$ to it to cancel it.

Let's look at an example:

\begin{example}[Inverse of Functions]
    \begin{align}
        f(x) &= 3x - 5 \\
             &= y = 3x - 5 \\
             &= x + 5 = 3y \\
             &= \frac{x + 5}{3} = y \\
             &= f^{-1}x = \frac{x + 5}{3}
    \end{align}
\end{example}

\subsubsection*{Operations on Functions}

\paragraph*{Addition}

The addition of two functions $f(x)$ and $g(x)$ is represented using the
notation $f(x) + g(x)$.

Let $f(x) = 4x - 7$ and $g(x) = 10x - 3$. To add $f(x)$ and $g(x)$, the
expressions $4x - 7$ and $10x - 3$ need to be added.

Therefore, this could be written as $f(x) + g(x) - (4x - 7) + (10x - 3)$.

\begin{enumerate}
    \item Simplify the right side by first distributing any coefficients
        outside the parentheses. If no number or variable appears before the
        parentheses, an understood 1 exists.
    \item When 1 is distributed to each term within each set of parentheses,
        the expression remains unchanged.
    \item Identify and combine like terms.
\end{enumerate}

\begin{align}
    f(x) + g(x) &= (4x - 7) + (10x - 3) \\
                &= 1(4x - 7) + 1(10x - 3) \\
                &= 4x - 7 + 10x - 3 \\
                &= 14x - 10 \\
                &= f(x) + g(x) = 14x - 10
\end{align}

\paragraph*{Subtraction}

Subtraction of functions is similar to addition. The only difference is that
you must be very careful of sign changes!

Let $f(x) = 4x - 7$ and $g(x) = 10x - 3$. To subtract $f(x)$ and $g(x)$, the
expressions $4x - 7$ and $10x - 3$ need to be subtracted.

Therefore, this could be written as $f(x) - g(x) = (4x - 7) - (10x - 3)$.

\begin{enumerate}
    \item Simplify the right side by first distributing any coefficients
        outside the parentheses. If no number or variable appears before the
        parentheses, an understood 1 exists.
    \item Be very careful not to forget to distribute the $-1$ to each term in
        the second set of parentheses.
    Identify and combine like terms.
\end{enumerate}

\begin{align}
    f(x) - g(x) &= (4x - 7) - (10x - 3) \\
                &= 1(4x - 7) - 1(10x - 3) \\
                &= 4x - 7 - 10x - 3 \\
                &= -6x - 4 \\
                &= f(x) - g(x) = -6x - 4
\end{align}

\newpage


%auto-ignore
\providecommand{\MainFolder}{..}
\documentclass[\MainFolder/Text.tex]{subfiles}

\newcommand{\Soln}{\mathcal{S}}
\newcommand{\InvSoln}{\Soln_{\mathrm{rot}}}
\newcommand{\InvDR}{\DR_{\mathrm{rot}}}
\newcommand{\InvR}{\R_{\mathrm{rot}}}
\newcommand{\SO}{\mathrm{SO}}
\newcommand{\ArtPrpg}{\mathrm{P}_{\mathrm{art}}}

\begin{document}

\section{Standard Hodge propagator for 1- and 2-sphere}\label{Sec:GrSpgh}
\allowdisplaybreaks

In this section, we denote the Hodge propagator for $\Sph{n}$ constructed in Part~I by $\ArtPrpg$. We would like to use $\ArtPrpg$ to study the standard Hodge propagator $\StdPrpg$ for $\Sph{n}$.

For $\Sph{1}$, we can compute $\KKer_t$ and hence $\StdPrpg$ explicitly.

\begin{Example}[$\StdPrpg$ for $\Sph{1}$]\label{Ex:SADQQ}
We write $\Sph{1} = \R/2\pi\Z$ and use the coordinate $x\in [0,2\pi)$. Because $\Sph{1}$ is flat, we have
$$ \Laplace = - \frac{\partial^2}{\partial x^2}. $$
Solving the eigenvalue problem $\Laplace \omega = \lambda \omega$ for $\omega\in \DR(\Sph{1})$ and $\lambda\in \R$, we get $\lambda\in \{0, n^2 \mid n\in\N\}$ and the corresponding eigenvectors
\begin{align*}
\Bigl\{\frac{1}{\sqrt{2 \pi}}, \frac{1}{\sqrt{\pi}} \cos(nx), \frac{1}{\sqrt{\pi}} \cos(nx) \Diff{x}, \frac{1}{\sqrt{\pi}} \sin(nx), \frac{1}{\sqrt{\pi}} \sin(nx) \Diff{x} \mid n\in \N\Bigr\},
\end{align*}
which we normalized in the $L^2$-norm. Plugging in \eqref{Eq:HK}, we get
\begin{align*}
\KKer_t(x_1,x_2) & = \frac{1}{2\pi} + \frac{1}{\pi}\sum_{n=1}^\infty e^{-n^2 t}\bigl(\cos(nx_1) \cos(nx_2) + \sin(nx_1)\sin(nx_2) \bigr) (\Diff{x_1}-\Diff{x_2}) \\
& = \frac{1}{2\pi} + \frac{1}{\pi}\sum_{n=1}^\infty e^{-n^2 t}\cos(nx_1 - nx_2)(\Diff{x_1}-\Diff{x_2}).
\end{align*}
Applying the product codifferential, we get
\begin{align*}
\CoDd \KKer_t(x_1,x_2) &= \frac{1}{\pi}\sum_{n=1}^\infty e^{-n^2 t}\Bigl[- \frac{\partial}{\partial x_1}\cos(nx_1-nx_2)+\frac{\partial}{\partial x_2}\cos(nx_1-nx_2)\Bigr] \\
& = \frac{2}{\pi} \sum_{n=1}^\infty e^{-n^2 t} n \sin(n x_1 - n x_2).
\end{align*}
Finally, the integration gives
\begin{align*}
\StdPrpg(x_1,x_2) &= \frac{1}{2}\int_0^\infty \CoDd\KKer_t(x_1,x_2) \Diff{t} \\
&=\frac{1}{\pi}\sum_{n=1}^\infty \Bigl(\int_0^\infty e^{-n^2 t} \Diff{t}\Bigr)n\sin(nx_1 - nx_2) \\
&= \frac{1}{\pi}\sum_{n=1}^\infty\frac{\sin\bigl(n(x_1-x_2)\bigr)}{n} \\
& = \frac{1}{2\pi} \begin{cases}
\pi - (x_1 - x_2) & x_1 > x_2, \\
-\pi - (x_1-x_2) & x_1< x_2.
\end{cases} \\
& = \frac{1}{2\pi}\bigl(\alpha(x_1,x_2) - \pi\bigr).
\end{align*}
This is precisely $\ArtPrpg$ from Part~I.
%In particular, the standard Hodge propagator extends smoothly to the blow-up.
% See \cite{https://math.stackexchange.com/questions/566856/is-sum-n-1-infty-frac-sinnxn-continuous} for the computation of the sum.
\end{Example}

For $n\ge 2$, an explicit formula for any of $\StdPrpg$, $\LapGKer$ or $\KKer_t$ seems to be unknown. For $\Sph{2}$, the formula for $\StdPrpg$ on functions was derived by Dr.~A.~Hermann.

Our idea to study $\StdPrpg$ via $\ArtPrpg$ is to examine the uniqueness of Hodge propagators. Consider the Schwartz form $\HKer(x,y) = \frac{1}{V}(\Vol(x) + (-1)^n\Vol(y))$ of the harmonic projection for $\Sph{n}$. Let $C_2(\Sph{n})\coloneqq\Sph{n}\times\Sph{n}\backslash\Diag$ denote the configuration space, and let
\begin{equation}\label{Eq:DifEq}
\Soln_n\coloneqq\{\Prpg\in\DR^{n-1}(C_2(\Sph{n}))\mid\Dd\Prpg=(-1)^n\HKer\}
\end{equation}
be the space of primitives to $(-1)^n\HKer$. We know that $\StdPrpg\in \Soln_n$. The following holds.

\begin{Proposition}[The space of primitives to $\HKer$ for $\Sph{n}$]\label{Prop:SpaceOfSolnSn}
Let
$$ V_n\coloneqq \begin{cases}
\DR^{n-2}(C_2(\Sph{n}))/\Dd \DR^{n-3}(C_2(\Sph{n})) & \text{for } n\ge 3, \\
\DR^{0}(C_2(\Sph{n}))/\R & \text{for }n=2,
\end{cases}$$
where $\R\subset\DR^0(C_2(\Sph{n}))$ denotes the constants. The action $\rho: V_n \times \Soln_n \rightarrow \Soln_n$, $(\lambda,\Prpg)\mapsto \Prpg + \Dd\lambda$ of the additive group $V_n$ on $\Soln_n$ defines the structure of an affine space on $\Soln_n$ for $n\ge 2$. If we require $SO(n+1)$ or $(-1)^n\tau^*$-invariance, then the same holds with $\DR$ replaced by the correspondingly invariant forms.
\end{Proposition}
\begin{proof}
We have to check that the action $\rho$ is free and transitive. For $\Prpg_1$, $\Prpg_2\in \Soln_n$, the difference $\eta \coloneqq \Prpg_1 - \Prpg_2$ is a closed $(n-1)$-form; it is exact because $C_2(\Sph{n})$ is homotopy equivalent to $\Sph{n}$. A primitive $\lambda_1$ is an $n-2$ form. If $\lambda_2$ is another primitive, then $\lambda_1 - \lambda_2$ is closed, and hence it is a constant for $n=2$ and an exact form for $n \ge 3$. Therefore,~$\Soln_n$ is an affine space over $V_n$. As for the invariance, we can average a primitive of an invariant form over $SO(n+1)$ or take $\frac{1}{2}(\Id + (-1)^n \tau^*)$.
\end{proof}

Note that $\Soln_1 \simeq \R$ by adding the constant and that all functions on $C_2(\Sph{1})$ are coexact. Therefore, $\StdPrpg$ for $\Sph{1}$ can not be characterized as a unique coexact solution of the differential equation for the Hodge propagator.

\begin{Proposition}[Coexactness of artificial Hodge propagator]\label{Prop:ArtProsCoexact}
The Hodge propagator $\ArtPrpg\in\DR^{n-1}(\Sph{n}\times\Sph{n}\backslash\Diag)$ constructed in Part~I is coexact for every $n\in \N$.
\end{Proposition}
\begin{proof}
First of all, we rewrite
\begin{align*}
\omega_k(x,y) &= \frac{1}{k!(n-1-k)!}\sum_{\sigma\in \Perm_{n+1}} x^{\sigma_1} y^{\sigma_1} \Diff{x}^{\sigma_3} \dotsb\Diff{x}^{\sigma_{2+k}} \Diff{y}^{\sigma_{3+k}}\dotsb \Diff{y}^{\sigma_{n+1}} \\
& = (-1)^k \sum_{\substack{I\subset \{1,\dotsc,n+1\} \\ \Abs{I} = k + 1}} \iota_x(\Diff{x}^I) \wedge \underbrace{\iota_y \Star^{\R^{n+1}}}_{\Star^{\Sph{n}}}(\Diff{y}^I).
\end{align*}
Recall the formulas $\CoDd \alpha = (-1)^{d(k-1)+1}\Star \Dd \Star \alpha$ and $\Star \Star \alpha= (-1)^{k(n-k)}\alpha$ for $\alpha\in \DR^k(M)$, where $d=\dim(M)$. For all $(x,y)\in \Sph{n}\times\Sph{n}\backslash\Diag$, we compute
\begin{align*}
 \CoDd_y \ArtPrpg(x,y) &= \CoDd_y\Bigl(\sum_{k=0}^{n-1} (-1)^k g_k(x\cdot y) \sum_{\substack{I\subset\{1,\dotsc,n+1\}\\\Abs{I}=k+1}} (\iota_x \Diff{x}^I)\wedge \Star^{\Sph{n}}(\Diff{y}^I) \Bigr) \\
 & =\sum_{k=0}^{n-1}\sum_{\substack{I\subset\{1,\dotsc,n+1\}\\\Abs{I}=k+1}} (\iota_x \Diff{x}^I)\wedge\CoDd_y\bigl(g_k(x\cdot y) \Star^{\Sph{n}}(\Diff{y}^I)\bigr) \\
 & \underset{\mathclap{\qquad\ \; \qquad\subalign{& \Big\uparrow\rule{0pt}{5.5ex} \\ \CoDd_y &= (-1)^{n(n-k)+1}\Star^{\Sph{n}}\Dd \Star^{\Sph{n}}\\
\Star^{\Sph{n}} \Star^{\Sph{n}} &= (-1)^{(k+1)(n-k-1)} \Id\\
\text{tot.~sign} &= (-1)^k}}}{=} \sum_{k=0}^{n-1} (-1)^k \sum_{\substack{I\subset\{1,\dotsc,n+1\}\\\Abs{I}=k+1}} (\iota_x \Diff{x}^I) \wedge \Star^{\Sph{n}} \Dd_y\bigl(g_k(x\cdot y) \Diff{y}^I\bigr) \\
& = \sum_{k=0}^{n-1}(-1)^k g_k'(x\cdot y) \sum_{\substack{I \subset \{1,\dotsc,n+1\}\\\Abs{I}=k+1\\}}\underbrace{\begin{multlined}[t] \sum_{i\in I} \sum_{j\in \{1,\dotsc,n+1\}\backslash I} \varepsilon(i,I)\varepsilon(j,I) x^i x^j \\ \Diff{x}^{I\backslash\{i\}}\wedge\Star^{\Sph{n}}(\Diff{y}^{I\cup \{j\}}) \end{multlined}}_{=0} \\
& = 0.
\end{align*}
The cancellation occurs because the summand $(I, i, j)$ contains the same terms as the summand $(I'=I\backslash\{i\}\cup j, j, i)$, and the signs satisfy
$$ \varepsilon(j,I')\varepsilon(i,I') = - \varepsilon(i,I)\varepsilon(j,I). $$
We have
$$ \H_{n-1}(\DR(\Sph{n}\times\Sph{n}\backslash\Diag),\CoDd) \simeq \HDR^{n+1}(\Sph{n}\times\Sph{n}\backslash\Diag) = 0, $$
and hence any coclosed $(n-1)$-form is coexact.
\end{proof}

\begin{Proposition}[Smooth extension to the blow-up for $\Sph{2}$]\label{Prop:StdS2}
The standard Hodge propagator for $\Sph{2}$ extends smoothly to the blow-up.
\end{Proposition}
\begin{proof}
Let $\Prpg_1$, $\Prpg_2 \in \Soln_2$ be two $\SO(3)$-symmetric solutions. Proposition~\ref{Prop:SpaceOfSolnSn} asserts that there is a smooth $\SO(3)$-symmetric function $\lambda: C_2(\Sph{2})\rightarrow \R$ such that $\Prpg_1 - \Prpg_2 = \Dd \lambda$. Because $\SO(3)$ acts on $\Sph{2}$ transitively, there is a smooth function $f: [-1,1)\rightarrow \R$ such that
$$ \lambda(x,y) = f(x\cdot y)\quad\text{for all }(x,y)\in C_2(\Sph{2}). $$
Note that one can let $f$ explode at $1$ and obtain Hodge propagators which do not extend smoothly to the blow-up. Let us assume, in addition, that $\Prpg_1 - \Prpg_2$ is coexact. We obtain
$$ 0 = \CoDd(\Prpg_1 - \Prpg_2) = \CoDd \Dd\lambda = \Laplace \lambda. $$
Therefore, $\lambda$ is a harmonic function on $C_2(\Sph{2})$. Denoting
\[
B(x,y) \coloneqq x\cdot y,
\]
we can write $\lambda = f\circ B$,  which implies
$$ \Laplace(f \circ B) = f'' \Norm{\Grad B}^2 + f' \Laplace B. $$
The computation of $\Norm{\Grad B}$ and $\Laplace B$ is straightforward and we will do it for any $n\in \N$. If $\tilde{f}: \Sph{n} \rightarrow \R$ is a smooth function and $f: \R^{n+1} \rightarrow \R$ is defined by
$$ f(x)\coloneqq \tilde{f}\Bigl(\frac{x}{\Abs{x}}\Bigr)\quad\text{for all }x\in \R^{n+1}\backslash\{0\}, $$
then
$$ \Laplace^{\Sph{n}} \tilde{f} = \Restr{\bigl(\Laplace^{\R^{n+1}}f\bigr)}{\Sph{n}}\quad\text{and}\quad \Grad^{\Sph{n}} \tilde{f} = \Restr{\bigl(\Grad^{\R^{n+1}}f\bigr)}{\Sph{n}}. $$
Here $\Laplace^{\Sph{n}}$, resp.~$\Grad^{\Sph{n}}$ are the Laplacian, resp.~the gradient on $\Sph{n}$ expressed in terms of the corresponding operators $\Laplace^{\R^{n+1}}$ and $\Grad^{\R^{n+1}}$ on $\R^{n+1}$, where $\Sph{n}$ is embedded into.
We compute
\begin{align*}
\Laplace_x^{\R^{n+1}} \Bigl( \frac{x}{\Abs{x}}\cdot y \Bigr) &= \sum_{i=1}^{n+1} - \frac{\partial}{\partial x^i}\Bigl(\frac{y^i}{\Abs{x}} - \frac{x^i}{\Abs{x}^3}x\cdot y\Bigr) \\
 &= \sum_{i=1}^{n+1} \frac{x^i y^i}{\Abs{x}^3} - 3 \frac{x^i x^i}{\Abs{x}^5} x \cdot y + \frac{x\cdot y}{\Abs{x}^3} + \frac{x^i y^i}{\Abs{x}^3} \\
 & = 0,\\
\Grad^{\R^{n+1}}_x\Bigl(\frac{x}{\Abs{x}}\cdot y\Bigr) & = \sum_{i=1}^{n+1} \Bigl(\frac{y^i}{\Abs{x}} - \frac{x^i}{\Abs{x}^3}x\cdot y \Bigr)\frac{\partial}{\partial x^i}\quad\text{and}\\
\Norm{\Grad (x\cdot y)}^2 &= \Bigl\|\sum_{i=1}^{n+1}(y^i - (x\cdot y) x^i) \frac{\partial}{\partial x^i} + \sum_{i=1}^{n+1}(x^i - (x\cdot y) y^i) \frac{\partial}{\partial y^i}\Bigr\|^2 \\
& = \sum_{i=1}^{n+1} (y^i - (x\cdot y) x^i)^2 + (x^i - (x\cdot y)y^i)^2 \\
& = 1 - 2(x\cdot y)^2 + (x\cdot y)^2 + 1 - 2(x\cdot y)^2 + (x\cdot y)^2 \\
& = 2(1- (x\cdot y)^2).
\end{align*}
Therefore, $\Laplace B = 0$, $\Norm{\Grad B(x_1,x_2)}^2=2(1-(x_1\cdot x_2)^2)$, and we arrive to the equation
$$ 2(1-u^2) f''(u) = 0\quad \text{for all }u\in[-1,1) $$
and a smooth function $f: [-1,1) \rightarrow \R$. The only solution is a linear function, and it must hold
$$ \lambda(x_1,x_2) = a B(x_1, x_2) + b \quad\text{for some }a, b\in \R. $$
We see that $\lambda$ extends smoothly to the blow-up.
\end{proof}

If we determine the constant $a$ in the proof of Proposition~\ref{Prop:StdS2}, then we get a formula relating $\HtpStd$ to $\ArtPrpg$ for $\Sph{2}$.
\ToDo[caption={Do more here!}]{This needs to be computed.}

%For higher spheres we will have $\ArtPrpg - \StdPrpg \in \Im \CoDd \cap \Im\Dd$ and the primitives will be symmetric on the actions.
%
%\Add[caption={Add about Hodge propagator for $\Sph{n}$}]{
%\begin{itemize}
%\item Is a coexact solution of $\Dd \Prpg = H$ which smoothly extends to the blow-up unique?
%\item Can I check for $\Sph{2}$ that $\Im\GOp\subset\Im\CoDd$ or can I prove otherwise for the artificial green kernel. Can I find constants $A$, $B$?
%\item Can I say something about uniqueness on $\Sph{3}$?
%\item What additional condition should specify $\StdPrpg$ as the unique solution.
%\item GARBAGE: But we know uniqueness of the coexact operator. But coexact operator is not implied by coexact kernel because we can not switch integral and codifferential!!
%
%It does not hold that every solution of the equation $\Dd \Prpg = \HKer$ defines a Green operator!. But those who extend smoothly to the blow-up yes.
%\end{itemize}}
\end{document}


\section{Multiple linear regression}

\subsection{Model}
\begin{equation}
\label{eq:multiple_linear_regression_model}
Y = \theta_0 + \theta_1 * X_1 + \theta_2 * X_2
\end{equation}

% Regression without scaling the predictor variables
\subsection{Regression without scaling predictor variables}
\subsubsection{Initial values of parameters}
\begin{equation}
\theta_0 = 0.6520699150884046
\end{equation}
\begin{equation}
\theta_1 = 0.9861396174652116
\end{equation}
\begin{equation}
\theta_2 = 0.9915108605252747
\end{equation}

\subsubsection{Final values of parameters}
\begin{equation}
\theta_0 = 4.311257778431243e+152
\end{equation}
\begin{equation}
\theta_1 = 1.4490623380861671e+153
\end{equation}
\begin{equation}
\theta_2 = 2.685595874504226e+154
\end{equation}

\begin{figure}[!ht]
    \includegraphics[width=\textwidth, height=.4\textheight]{output_2/cost_function_alpha_0_01_unscaled.png}
    \caption{Mean squared error when predictor variables are not scaled}
    \label{fig:mean_squared_without_scaling}
\end{figure}

% Regression after scaling the predictor variables
\subsection{Regression after scaling predictor variables}
\subsubsection{Initial values of parameters}
\begin{equation}
\theta_0 = 0.4409653770961314
\end{equation}
\begin{equation}
\theta_1 = 0.16376353557709156
\end{equation}
\begin{equation}
\theta_2 = 0.2407927690962538
\end{equation}

\subsubsection{Final values of parameters}
\begin{equation}
\theta_0 = 1.545463253882367
\end{equation}
\begin{equation}
\theta_1 = 3.8202281258797064
\end{equation}
\begin{equation}
\theta_2 = 1.0008683090251478
\end{equation}

\begin{figure}[!ht]
    \includegraphics[width=\textwidth, height=.4\textheight]{output_2/cost_function_alpha_0_01_scaled.png}
    \caption{Mean squared error when predictor variables are scaled}
    \label{fig:mean_squared_with_scaling}
\end{figure}

\subsection{Observation}
Equation \ref{eq:multiple_linear_regression_model} was used to model the prediction variable Y as a linear model of predicting variables $X_1$ and $X_2$. At first, multiple linear regression without parameter scaling was used to obtain the prediction line. The cost function value kept increasing and it failed to converge as shown in the figure \ref{fig:mean_squared_without_scaling}.

When parameter scaling was used with multiple linear regression the gradient descent algorithm converged and was able to obtain the optimum values for the $\theta$s. The corresponding graph for cost function when predicting parameters were scaled is shown in figure \ref{fig:mean_squared_with_scaling}.
\subsection{Conclusion}
The data that was used with multiple linear regression shows that the predictor variable $X_2$ had higher magnitude than the values of predictor variable $X_1$. Due to the dissimilarity in magnitude of predictor variables the gradient descent algorithm when used without feature scaling diverges and fails to yield an optimum regression line.

\subsection{Source Code}

\lstinputlisting[language=python]{task_2.py}


\chapter{Lecture 1 May 02nd 2018}
  \label{chapter:lecture_1_may_02nd_2018}

\section{Introduction} % (fold)
\label{sec:introduction}

\subsection{Numbers} % (fold)
\label{sub:numbers}

The following are some of the number sets that we are already familiar with:
\begin{gather*}
  \mathbb{N} = \{1, 2, 3, ...\} \qquad \mathbb{Z} = \{.., -2, -1, 0, 1, 2, ...\} \\
  \mathbb{Q} = \left\{\frac{a}{b} : a \in \mathbb{Z}, b \in \mathbb{N} \right\} \qquad \mathbb{R} = \text{ set of real numbers} \\
  \mathbb{C} = \{a + bi : a, b \in \mathbb{R}, i = \sqrt{-1} \} = \text{ set of complex numbers}
\end{gather*}
For $n \in \mathbb{Z}$, let $\mathbb{Z}_n$ denote the set of integers modulo $n$, i.e.
\begin{equation*}
  \mathbb{Z}_n = \{ [0], [1], ..., [n - 1] \}
\end{equation*}
where the $[r]$, $0 \leq r \leq n - 1$, are the congruence classes, i.e.
\begin{equation*}
  [r] = \{z \in \mathbb{Z} : z \equiv r \mod n\}
\end{equation*}

These sets share some common properties, e.g. $+$ and $\times$. Let's try to break that down to make further observation.

\newthought{Note that} for $R = \mathbb{N}, \, \mathbb{Z}, \, \mathbb{Q}, \, \mathbb{R}, \, \mathbb{C},$ or $\mathbb{Z}_n$, $R$ has 2 operations, i.e. addition and multiplication.

\paragraph{Addition} If $r_1, r_2, r_3 \in R$, then
\begin{itemize}
  \item (\hldefn{closure}) $r_1 + r_2 \in R$
  \item (\hldefn{associativity}) $r_1 + (r_2 + r_3) = (r_1 + r_2) + r_3$
\end{itemize}
Also, if $R \neq \mathbb{N}$, then $\exists 0 \in R$ (the \hldefn{additive identity}) such that
\begin{equation*}
  \forall r \in R \quad r + 0 = r = 0 + r.
\end{equation*}
Also, $\forall r \in R$, $\exists (-r) \in R$ such that
\begin{equation*}
  r + (-r) = 0 = (-r) + r.
\end{equation*}

\paragraph{Multiplication} For $r_1, r_2, r_3 \in R$, we have
\begin{itemize}
  \item (\hlnoteb{closure}) $r_1 r_2 \in R$
  \item (\hlnoteb{associativity}) $r_1 (r_2 r_3) = (r_1 r_2) r_3$
\end{itemize}
Also, $\exists 1 \in R$ (a.k.a the \hldefn{mutiplicative identity}), such that
\begin{equation*}
  \forall r \in R \quad r \cdot 1 = r = 1 \cdot r.
\end{equation*}
Finally, for $R = \mathbb{Q}, \, \mathbb{R},$ or $\mathbb{C}$, $\forall r \in R, \, \exists r^{-1} \in R$ such that
\begin{equation*}
  r \cdot r^{-1} = 1 = r^{-1} \cdot r.
\end{equation*}
Note that for $R = \mathbb{Z}_n$, where $n \in \mathbb{Z}$, not all $[r] \in \mathbb{Z}_n$ have a multiplicative inverse. For example, for $[2] \in \mathbb{Z}_4$, there is no $[x] \in \mathbb{Z}_4$ such that $[2][x] = [1]$.\sidenote{This is best proven using techniques introduced in MATH135/145.}

% subsection numbers (end)

\subsection{Matrices}
  \label{sub:matrices}

For $n \in \mathbb{N} \setminus \{1\}$, an $n \times n$ matrix over $\mathbb{R}$ \sidenote{$\mathbb{R}$ can be replaced by $\mathbb{Q}$ or $\mathbb{C}$.} is an $n \times n$ array that can be expressed as follows:
\begin{equation*}
  A = [a_{ij}] = \begin{bmatrix}
    a_{11} & a_{12} & \hdots & a_{1n} \\
    a_{21} & a_{22} & \hdots & a_{2n} \\
    \vdots & \vdots &        & \vdots \\
    a_{n1} & a_{n2} & \hdots & a_{nn}
  \end{bmatrix}
\end{equation*}
where for $1 \leq i, j \leq n$, $a_{ij} \in \mathbb{R}$. We denote $M_n(\mathbb{R})$ as the set of all $n \times n$ matrices over $\mathbb{R}$.

As in \cref{sub:numbers}, we can perform \hlnotea{addition and multiplication} on $M_n(\mathbb{R})$.

\paragraph{Matrix Addition} Given $A = [a_{ij}], B = [b_{ij}], C = [c_{ij}] \in M_n(\mathbb{R})$, we define matrix addition as
\begin{equation*}
  A + B = [a_{ij} + b_{ij}],
\end{equation*}
which immediately gives the \hlnoteb{closure property}, since $a_{ij} + b_{ij} \in \mathbb{R}$ and hence $A + B \in M_n(\mathbb{R})$. Also, by this definition, we also immediately obtain the \hlnoteb{associativity property}, i.e.
\begin{equation*}
  A + (B + C) = (A + B) + C.
\end{equation*}
We define the zero matrix as
\begin{equation*}
  0 = \begin{bmatrix}
    0      &   0    & \hdots &   0 \\
    0      &   0    & \hdots &   0 \\
    \vdots & \vdots &        & \vdots \\
    0      &   0    & \hdots &   0
  \end{bmatrix}.
\end{equation*}
Then we have that $0$ is the \hlnoteb{additive identity}, i.e.
\begin{equation*}
  A + 0 = A = 0 + A.
\end{equation*}
Finally, $\forall A \in M_n(\mathbb{R})$, $\exists (-A) \in M_n(\mathbb{R})$ (the \hlnoteb{additive inverse}) such that
\begin{equation*}
  A + (-A) = 0 - (-A) + A.
\end{equation*}

Note that in this case, we also have that that the operation is \hlnoteb{commutative}, i.e.
\begin{equation*}
  A + B = B + A.
\end{equation*}

\paragraph{Matrix Multiplication} Given $A = [a_{ij}], B = [b_{ij}], C = [c_{ij}] \in M_n(\mathbb{R})$, we define the matrix multiplication as
\begin{equation*}
  AB = [d_{ij}] \text{ where } c_{ij} = \sum_{k=1}^{n} a_{ik} b_{kj} \in \mathbb{R}.
\end{equation*}
Clearly, $AB \in M_n(\mathbb{R})$, i.e. it is \hlnoteb{closed under matrix multiplication}. Also, we have that, under such a defintion, matrix multiplication is \hlnoteb{associative}, i.e.
\begin{equation*}
  A(BC) = (AB)C.
\end{equation*}
Define the identity matrix, $I \in M_n(\mathbb{R})$, as follows:
\begin{equation*}
  I = \begin{bmatrix}
    1      &   0    & \hdots & 0 \\
    0      &   1    & \hdots & 0 \\
    \vdots & \vdots &        & \vdots \\
    0      &   0    & \hdots & 1
  \end{bmatrix}.
\end{equation*}
Then we have that $I$ is the \hlnoteb{multiplicative identity}, since
\begin{equation*}
  AI = A = IA.
\end{equation*}
However, contrary to matrix addition, $\forall A \in M_n(\mathbb{R})$, it is not always true that $\exists A^{-1} \in M_n(\mathbb{R})$ such that\marginnote{This is especially true if the \hlnotea{determinant} of $A$ is $0$.}
\begin{equation*}
  AA^{-1} = I = A^{-1} A.
\end{equation*}

Also, we can always find some $A, B \in M_n(\mathbb{R})$ such that
\begin{equation*}
  AB \neq BA,
\end{equation*}
i.e. matrix multiplication is not always commutative.

\newthought{The common properties} of the operations from above: \hlimpo{closure, associativity, and existence of an inverse}, are not unique to just addition and multiplication. We shall see in the next lecture that there are other operations where these properties will continue to hold, e.g. \hlnoteb{permutations}.

% subsection matrices (end)

% section introduction (end)

% chapter lecture_1_may_02nd_2018 (end)


\input{templates/ex_template.tex}

\title{BPP Exercise 8 -- Standard Library}
% {YYYY}{MM}{DD}
\setdate{2019}{06}{02}

\begin{document}

\section{Warm-Up (20 points)}

\noindent 1.1. Using \texttt{time}, print the number of seconds that have passed since January 1st 1970 00:00:00.

\vspace{1em}

\begin{solution}

\begin{pythoncode}

import time

print(time.time())

\end{pythoncode}

\end{solution}

\noindent 1.2. Using \texttt{os}, write a function \texttt{make\_absolute} that converts a relative path to an absolute one.

\vspace{1em}

\begin{solution}

\begin{pythoncode}

import os

def make_absolute(relative_path):
    """Makes a relative path absolute."""
    return os.getcwd() + "/" + relative_path

\end{pythoncode}

\end{solution}

\noindent 1.3. Using \texttt{math}, write two functions \texttt{calculate\_x} and \texttt{calculate\_y} that take an \texttt{int n} between 1 and 12 as an argument and return the respective value of the following formulas:

\vspace{1em}

\noindent $calculate\_x(n) = 3 * \sin{\frac{n * \pi}{6}}$

\vspace{1em}

\noindent $calculate\_y(n) = 3 * \cos{\frac{n * \pi}{6}}$

\vspace{1em}

\noindent \textbf{Background information (not relevant for the implementation):}

\vspace{1em}

\noindent These functions calculate the x and y position of the label for hour \texttt{n} on a clock of radius 3. The x position of the hour 12 would e.g. be $3 * \sin{\frac{12 * \pi}{6}} = 3 * \sin{2 * \pi} = 0$ and the y position $3 * \cos{\frac{12 * \pi}{6}} = 3 * \cos{2 * \pi} = 3$.

\vspace{1em}

\begin{solution}

\begin{pythoncode}

from math import sin, cos, pi

def calculate_x(n):
    """Calculates the x position of hour n on the clock."""
    return 3 * sin((n * pi) / 6)

def calculate_y(n):
    """Calculates the y position of hour n on the clock."""
    return 3 * cos((n * pi) / 6)

\end{pythoncode}

\end{solution}

\section{os, sys (20 points)}

In the folder Homework Sheets/Resources on StudIP, you will find the file 08\_comp\_tree.zip. Download it and extract it.

\vspace{1em}

\noindent 2.1. Familiarize yourself with the folder structure. Can you find the system behind the comparison tree? Briefly explain the rules in a file \texttt{comp\_tree\_explained.txt}.

\vspace{1em}

\begin{solution}

\noindent There are three layers of folders. The folders on the lowest layer are all named after numbers between 1 and 100, e.g. "26". The folders on the middle layer are either also named after numbers or after comparisons with one specific number, e.g. "larger\_than\_25". The folders on the top layer are all named after comparisons. There are three kinds of comparisons: "larger\_than", "smaller\_than" and "equals". \newline The numbers are sorted according to the comparisons, e.g. the folder "larger\_than\_50" contains only numbers that are larger than 50.

\end{solution}

\vspace{1em}

\noindent 2.2. Write a script \texttt{insert.py} that takes a number as a command-line argument and then creates a folder with this name in the correct position according to the existing folder structure. You should use \texttt{os.listdir()} and \texttt{os.mkdir()} for this, which both take a relative or absolute path as an argument.

\vspace{1em}

\noindent \textbf{Hint 1:} \texttt{sys.argv[0]} is the name of your own script, \texttt{sys.argv[1]} is the first command-line argument.

\noindent \textbf{Hint 2:} With the command \texttt{tree} in the terminal you can get an overview of the folder structure. A screenshot of this can also be found on StudIP in the folder Homework Sheets/Resources.

\vspace{1em}

\begin{solution}

\begin{pythoncode}

import sys
import os

def get_path(n):
    """Returns the path of n in a comp_tree structure."""

    rel_path = "comp_tree/"

    if n > 50:
        rel_path += "larger_than_50/"
        if n > 75:
            rel_path += "larger_than_75/"
        elif n < 75:
            rel_path += "smaller_than_75/"
        else:
            rel_path += "equals_75/"
    elif n < 50:
        rel_path += "smaller_than_50/"
        if n > 25:
            rel_path += "larger_than_25/"
        elif n < 25:
            rel_path += "smaller_than_25/"
        else:
            rel_path += "equals_25/"
    else:
        rel_path += "equals_50/"

    rel_path += str(n)

    return rel_path

n = int(sys.argv[1])
path = get_path(n)

os.mkdir(path)

\end{pythoncode}

\end{solution}

\section{copy, random, time (30 points)}

\noindent 3.1. Write a function \texttt{rand\_change} that takes a nested 3x3 2d list (meaning a list with three row lists in it which each have three elements in it) as an argument and returns a deep copy of it in which one randomly selected cell was assigned a random integer between 1 and 3. Cells are elements of row lists.

\vspace{1em}

\noindent \textbf{Example}:

\begin{pythoncode}

my_list = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]

my_list = rand_change(my_list)

print(my_list)
# Output: [[1, 1, 1], [2, 1, 1], [1, 1, 1]]

\end{pythoncode}

\begin{solution}

\begin{pythoncode}

from copy import deepcopy
import random

def rand_change(array):
    """Returns a copy with one random cell changed."""
    array_copy = deepcopy(array)
    x = random.randint(0, 2)
    y = random.randint(0, 2)
    array_copy[x][y] = random.randint(1, 3)
    return array_copy

\end{pythoncode}

\end{solution}

\noindent 3.2. Write a function \texttt{get\_diff} that takes two nested 3x3 2d lists and returns the number of how many cells (in the same x, y position) have different values.

\vspace{1em}

\noindent \textbf{Example}:

\begin{pythoncode}

list_1 = [[1, 1, 1], [1, 1, 1], [2, 3, 1]]

list_2 = [[3, 3, 3], [3, 3, 3], [1, 2, 3]]

print(get_diff(list_1, list_2))
# Output: 3

\end{pythoncode}

\begin{solution}

\begin{pythoncode}

def get_diff(array_1, array_2):
    """Returns the number of cells that differ."""
    diff = 0
    for x in range(3):
        for y in range(3):
            if array_1[x][y] != array_2[x][y]:
                diff += 1
    return diff

\end{pythoncode}

\end{solution}

\noindent 3.3. Create two nested 3x3 2d lists \texttt{ones} and \texttt{threes}. \texttt{ones} should be filled with value 1, \texttt{threes} should be filled with the value 3. Change both 2d lists with \texttt{rand\_change} until \texttt{get\_diff(ones, threes)} returns 0. Measure the time this took in seconds and print it.

\vspace{1em}

\noindent \textbf{Hint 1:} In the beginning, \texttt{get\_diff(ones, threes)} should return 9.

\noindent \textbf{Hint 2:} This should take at most 10 seconds. If it takes longer, you made a mistake somewhere.

\vspace{1em}

\begin{solution}

\begin{pythoncode}

# record start time
start = time.time()

ones = []
threes = []

# fill ones and threes
for x in range(3):
    row_ones = []
    row_threes = []
    for y in range(3):
        row_ones.append(1)
        row_threes.append(3)
    ones.append(row_ones)
    threes.append(row_threes)

diff = 1

while diff > 0:
    diff = get_diff(ones, threes)
    ones = rand_change(ones)
    threes = rand_change(threes)

# print time difference in seconds
print("It took {:.4f} seconds".format(time.time() - start))

\end{pythoncode}

\end{solution}

\section{math, time (30 points)}

To test our skills, we will implement a simplified formula for the increase in global temperature levels due to the radiative forcing of CO2 level changes.

\vspace{1em}

\noindent \textbf{Keep in mind that the following is an extremely simplified model which may be outdated and might not represent the current scientific consensus.}

\vspace{1em}

\noindent \textbf{Radiative forcing due to change in CO2 level:}\qquad \qquad \qquad $\Delta F = \alpha * ln(\frac{c_1}{c_0})$

\vspace{1em}

\noindent \textbf{Relation between radiative forcing and temperature:} \qquad $\lambda = \frac{\Delta T}{\Delta F}$

\vspace{1em}

\noindent $\alpha$ is fixed at 5.35 while $\lambda$ differs from model to model - we will simply use 0.5.

\vspace{1em}

\noindent $c_1$ denotes the changed CO2 level, $c_0$ denotes the unchanged CO2 level.

\vspace{1em}

\noindent Let's take the CO2 level of January 1970 as the unchanged CO2 level, so $c_0 = 325.03$.

\vspace{1em}

\noindent Current levels of CO2 were at $c_1 = 411.97$ as of March 2019. We will just assume that the CO2 level has not changed significantly since then for the purposes of our calculations.

\vspace{1em}

\noindent This would result in a 0.63 K increase in global temperature since 1970, which is consistent with the actual change in global temperature, especially considering that there are also other influences on the global climate such as other greenhouse gases.

\vspace{1em}

\noindent \textbf{Your task is to do the following:}

    \begin{enumerate}

        \item Calculate how many hours have passed since January 1st 1970 00:00:00
        \item Use this time difference to calculate the average CO2 increase per hour since 1970
        \item Use this CO2 increase per hour to calculate a projection of what the CO2 level could be in 2100 (assuming that the CO2 increase per hour stays constant)
        \item Calculate the increase in temperature in from 1970 to 2100 (use your projected CO2 level as $c_1$ and the value from 1970 as $c_0$)
        \item Now generalize steps 3. and 4. by writing a function \texttt{predict\_increase} that takes an \texttt{int year} larger than 1970 as an input and returns the increase in temperature from 1970 to \texttt{year}

    \end{enumerate}

\vspace{1em}

\noindent \textit{Formulas taken from: } IPCC (2001) Radiative Forcing of Climate Change, in Climate Change 2001: The Scientific Basis. Contribution of Working Group 1 to the Third Assessment. Report of the Intergovernmental Panel on Climate Change, CUP, pp. 349-416

\vspace{1em}

\noindent \textit{CO2 values taken from: } Dr. Pieter Tans, NOAA/ESRL (www.esrl.noaa.gov/gmd/ccgg/trends/) and Dr. Ralph Keeling, Scripps Institution of Oceanography. \url{https://www.esrl.noaa.gov/gmd/ccgg/trends/data.html}

\vspace{1em}

\begin{solution}

\begin{pythoncode}

from math import log
import time

co2_1970 = 325.03
co2_now = 411.97

alph = 5.35
lamb = 0.5

# hours that have passed since 1970
hours_passed_now = time.time() / 3600

# co2 increase since 1970
co2_increase_now = co2_now - co2_1970

# average co2 increase per hour since 1970
avg_co2_increase = co2_increase_now / hours_passed_now

# hours that have passed between 1970 and 2100
hours_passed_2100 = (2100 - 1970) * 365.25 * 24

# co2 level projection for 2100
co2_2100 = co2_1970 + (hours_passed_2100 * avg_co2_increase)

print("Projected CO2 level in 2100: {:.4f}".format(co2_2100))

# calculate change in radiative forcing
delta_F_2100 = alph * log(co2_2100 / co2_1970)

# calculate change in temperature
delta_T_2100 = lamb * delta_F_2100

print("Projected temperature increase from 1970 to 2100: {:.4f}".format(delta_T_2100))

def predict_increase(year):
    """Predicts the increase in temperature between 1970 and a given year."""

    # calculate projected value of co2 in year
    hours_passed = (year - 1970) * 365.25 * 24
    co2 = co2_1970 + (hours_passed * avg_co2_increase)

    # calculate temperature increase from 1970 to year
    delta_F = alph * log(co2 / co2_1970)
    delta_T = lamb * delta_F

    print("Projected temperature increase from 1970 to {}: {:.4f}".format(year, delta_T))

\end{pythoncode}

\end{solution}

\end{document}


\section{Divergence, Curl and Laplacian}
\subsection{Definitions}
\begin{definition}
    For a vector field $\underline{F}:\mathbb R^3\to\mathbb R^3$, we define the divergence
    $$\operatorname{div}\underline{F}=\nabla\cdot\underline{F}=\frac{\partial F_i}{\partial x_i}$$
    where the summation convention applies.
\end{definition}
\begin{definition}
    For a vector field $\underline{F}:\mathbb R^3\to\mathbb R^3$, we define the curl
    $$\operatorname{curl}\underline{F}=\nabla\times\underline{F}=\epsilon_{ijk}\frac{\partial F_k}{\partial x_j}\underline{e_i}$$
    where again the summation convention applies.
\end{definition}
\begin{definition}
    For a function $f:\mathbb R^3\to\mathbb R$, we define the Laplacian
    $$\Delta f=\nabla^2f=\nabla\cdot(\nabla f)=\frac{\partial^2 f}{\partial x_i\partial x_i}$$
    where yet again we have the summation convention.
\end{definition}
\begin{note}
    All these are in Cartesians.
\end{note}
\begin{example}
    Consider the vector field $\underline{F}(\underline{x})=\underline{x}$, then $\nabla\cdot\underline{F}=3$.
    Also $(\nabla\times\underline{F})_i=\epsilon_{ijk}\frac{\partial F_k}{\partial x_j}=\epsilon_{ijk}\delta_{jk}=0$, hence $\nabla\times\underline{F}=\underline{0}$.
\end{example}
\begin{proposition}
    We have the following identities:
    $$\nabla(fg)=(\nabla f)g+f(\nabla g)$$
    $$\nabla\cdot(f\underline{F})=(\nabla f)\cdot\underline{F}+f(\nabla\cdot\underline{F})$$
    $$\nabla\times (fF)=(\nabla f)\times\underline{F}+f(\nabla\times\underline{F})$$
    $$\nabla(\underline{F}\cdot\underline{G})=\underline{F}\times(\nabla\times\underline{G})+\underline{G}\times(\nabla\times\underline{F})+(\underline{F}\cdot\nabla)\underline{G}+(\underline{G}\cdot\nabla)\underline{F}$$
    $$\nabla\times(\underline{F}\times\underline{G})=\underline{F}(\nabla\cdot\underline{G})-\underline{G}(\nabla\cdot\underline{F})+(\underline{G}\cdot\nabla)\underline{F}-(\underline{F}\cdot\nabla)\underline{G}$$
    $$\nabla\cdot(\underline{F}\times\underline{G})=(\nabla\times\underline{F})\cdot\underline{G}-\underline{F}\cdot(\nabla\times\underline{G})$$
\end{proposition}
\begin{proof}
    Trivial.
\end{proof}
Of course we want, can can compute these three quantities in curvilinear coordinates, but we cannot do it directly since the basis vectors are not constant.
However we can expand everything and get
\begin{proposition}
    For a vector field $\underline{F}$ under a curvilinear coordinate $\underline{F}=F_u\underline{e_u}+F_v\underline{e_v}+F_w\underline{e_w}$,
    $$\nabla\cdot\underline{F}=\frac{1}{h_uh_vh_w}\sum_{u,v,w}^{\rm cyc}\frac{\partial}{\partial u}(h_vh_wF_u)$$
    $$\nabla\times\underline{F}=\sum_{u,v,w}^{\rm cyc}\frac{1}{h_vh_w}\left( \frac{\partial}{\partial v}(h_wF_w)-\frac{\partial}{\partial w}(h_vF_v) \right)\underline{e_u}$$
    And for a scalar function $f$,
    $$\nabla^2f=\frac{1}{h_uh_vh_w}\sum_{u,v,w}^{\rm cyc}\frac{\partial}{\partial u}\left(\frac{h_uh_w}{h_u}\frac{\partial f}{\partial u}\right)$$
\end{proposition}
\begin{proof}
    Trivial calculations.
\end{proof}
If one is bored, one can try and find the formulas for cylindral and spherical coordinates:
$$\nabla^2f=\frac{1}{\rho}\frac{\partial}{\partial\rho}\left( \rho\frac{\partial f}{\partial\rho} \right)+\frac{1}{\rho^2}\frac{\partial^2f}{\partial \phi^2}+\frac{\partial^2f}{\partial z^2}$$
$$\nabla^2f=\frac{1}{r^2}\frac{\partial}{\partial r}\left( r^2\frac{\partial f}{\partial r} \right)+\frac{1}{r^2\sin\theta}\frac{\partial}{\partial\theta}\left( \sin\theta\frac{\partial f}{\partial\theta} \right)+\frac{1}{r^2\sin\theta}\frac{\partial^2f}{\partial\phi^2}$$
The reason why we need these notions is for the generalization of fundamental Theorem of Calculus to general integrals, where some of these operators will be used as a substituent of derivative.\\
The reason we need Laplacians is that the PDE $\nabla^2f=0$, whose solutions are called harmonic functions, is pretty important.
One of their properties that once they are twice differentiable (so as to let the equation make sense), then they are infinitely differentiable.
Even better, they are all analytic, i.e. can be expressed in terms of power series.
\subsection{Relationships between the Operators}
\begin{proposition}
    Let $f:\mathbb R^3\to\mathbb R$ and $\underline{F}:\mathbb R^3\to\mathbb R^3$, then $\nabla\times\nabla f=0$ and $\nabla \cdot(\nabla\times\underline{F})=0$.
\end{proposition}
\begin{proof}
    Trivial.
\end{proof}
Hence if $\underline{F}$ is conservative, then it has zero curl.
The reverse implication is true when the domain is simply connected.
For example, if we take $\mathbb R^3\setminus\{(0,0,z):z\in\mathbb R\}$ as our domain, then this is not simply connected, but $\mathbb R^3\setminus\{(0,0,0)\}$ is.\\
If there exists vector fields $\underline{A}$ such that $\underline{F}=\nabla\times\underline{A}$, we say $\underline{A}$ is a vector potential of $\underline{F}$.
So if $\nabla\cdot\underline{F}=0$, we say $\underline{F}$ is solenoidal.
The existence of a vector potential for $\underline{F}$ implies $\underline{F}$ is solenoidal.
The reverse implication is true when the domain is $2$-connected, that is, it is simply connected and the second homotopy group is trivial.
For example, $\mathbb R^3$ is $2$-connected but $\mathbb R^3\setminus\{(0,0,0)\}$ is not.


\section{Eligibility Traces}

\subsection{$\lambda$-return}
\cindex{eligibility trace} is a short term memory vector that has the same dimension as $\mathbf{W}$.

The \cindex{forward view of TD($\lambda$)} is defined as:
\begin{equation}
	\begin{cases}
		G_t^\lambda = (1-\lambda)\sum\limits_{n=1}^\infty \lambda^{n-1} G_{t:t+n} & \text{, for continuous task}\\
		\\
		G_t^\lambda = (1-\lambda)\sum\limits_{n=1}^{T-t-1} \lambda^{n-1} G_{t:t+n} + \lambda^{T-t-1}G_t & \text{, for episodes}
	\end{cases}
\end{equation}

\subsection{TD($\lambda$)}

In \cindex{backward view of TD($\lambda$)}, the \cindex{eligibility trace} $z_t \in \mathbb{R}^d$ is defined as:
\begin{equation}
	\begin{aligned}
		z_{-1} &= 0 \\
		z_t &= \gamma \lambda z_{t-1} + \nabla \widehat{v} (S_t, \mathbf{W}_t)
	\end{aligned}
\end{equation}

Of which $\gamma$ is the discount rate and $\lambda$ is the $\lambda$ in TD($\lambda$).

The TD error is defined as:
\begin{equation}
	\delta_t = R_{t+1} + \gamma \widehat{v}(S_{t+1}, \mathbf{W}_t) -\widehat{v}(S_t, \mathbf{W}_t)
\end{equation}

The gradient is defined as:
\begin{equation}
	\mathbf{W}_{t+1} = \mathbf{W}_t + \alpha \delta_t z_t
\end{equation}

Here the $\alpha$ is the ratio used for mean value converge calculation.

If $\lambda = 0$, TD($\lambda$) becomes one-step semi-gradient TD.

If $\lambda = 1$, TD($\lambda$) becomes Monte Carlo calculation.

Linear TD($\lambda$) will converge in on-policy case if step size follows formula (\ref{convergenceofsequence}):

\begin{equation}\label{semigradientlrerror}
	\overline{\text{VE}}(\mathbf{W}_{\infty}) \leq \frac{1 - \gamma \lambda}{1-\gamma} \underset{\mathbf{W}}{\min}\ \overline{\text{VE}}(\mathbf{W})
\end{equation}

In practice, do not choose $\lambda = 1$ which is the poorest choice.


%!TEX root = TTK4150-Summary.tex
\section{Passivity}
\begin{align}
	\dot{x} &= f(x,u) \label{eq:passive1} \\
	y       &= h(x,u) \label{eq:passive2}
\end{align}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Memoryless functions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Definition 6.1}
The system $y = h(t,u)$ is
\begin{itemize}
	\item passive if $u\T y \geq 0$,
	\item lossless if $u\T y = 0$,
	\item input-feedforward passive if $u\T y \geq u\T \phi(u)$ for some $\phi(u)$,
	\item input strictly passive if it is IFP and $u\T \phi(u) > 0 \: \forall \: y \neq 0$,
	\item output-feedback passive if $u\T y \geq y\T \rho(y)$ for some $\rho(y)$,
	\item output strictly passive if it is OFP and $y\T \rho(y) > 0 \: \forall \: y \neq 0$.
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{State models}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Definition 6.3}
The system \eqref{eq:passive1}--\eqref{eq:passive2} with storage function $V(x) \geq 0$ is
\begin{itemize}
	\item passive if $u\T y \geq \dot{V}$,
	\item lossless if $u\T y = \dot{V}$,
	\item input-feedforward passive if $u\T y \geq \dot{V} + u\T \phi(u)$ for some function $\phi$,
	\item input strictly passive if it is IFP with $u\T \phi(u) > 0 \: \forall \: u \neq 0$,
	\item output-feedback passive if $u\T y \geq \dot{V} + y\T \rho(y)$ for some function $\rho$,
	\item output strictly passive if it is OFP with $y\T \rho(y) > 0 \: \forall \: y \neq 0$,
	\item strictly passive if $u\T y \geq \dot{V} + \psi(x)$ for some pos. def $\psi$.
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection[\texorpdfstring{$\mathcal{L}_2$ and Lyapunov stability}
	{L2 and Lyapunov stability}]
	{$\mathcal{L}_2$ and Lyapunov stability}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Lemma 6.5 (finite-gain $\mathcal{L}_2$ stable)}
If a system is output strictly passive with $\rho(y) = \delta y$ with $\delta > 0$ then it is finite-gain $\mathcal{L}_2$ stable with gain $\gamma \leq \delta^{-1}$.

\paragraph{Definition 6.5 (zero-state observability)}
The system \eqref{eq:passive1}--\eqref{eq:passive2} is zero-state observable if only the solution $x(t) \equiv 0$ of $\dot{x} = f(x,0)$ can stay in $S = \{ x \in \mathbb{R}^n | h(x,0) = 0 \}$.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Feedback systems}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[htbp]
	\centering
	\includegraphics[width=8cm]{feedback-connection.png}
	\caption{Feedback connection}
\end{figure}

\paragraph{Theorem 6.1}
The feedback connection of two passive systems is passive, with $V = V_1 + V_2$.

\paragraph{Theorem 6.2 ($\mathcal{L}_2$-stability of feedback connection)}
If $H_1$ and $H_2$ satisfy
\begin{equation}
	e_i\T y_i \geq \dot{V}_i + \epsilon_i e_i\T e_i + \delta_i y_i\T y_i
\end{equation}
and
\begin{equation}
	\epsilon_1 + \delta_2 > 0 \mbox{ and } \epsilon_2 + \delta_1 > 0
\end{equation}
then the feedback connection is finite-gain $\mathcal{L}_2$-stable.

TTK4150 Nonlinear control systems/sec-passivity.tex


\newpage
	\section*{}
	\textbf{Task}\\
	Prove the equality:
	\begin{gather*}
		\sum\limits_{k=0}^{n} {{n}\choose{k}}^2 = {{2n}\choose{n}}
	\end{gather*}
	\textbf{Solution}\\
	We know that
	\begin{gather*}
		{{n}\choose{k}} = {{n}\choose{n-k}}
	\end{gather*}
	Then
	\begin{gather*}
		\sum\limits_{k=0}^{n} {{n}\choose{k}}^2 =
		\sum\limits_{k=0}^{n} {{n}\choose{k}}{{n}\choose{n-k}}
	\end{gather*}
	Then we can see that this is the same as choosing $n-k$ objectsfrom the set of power $n$ and $k$ from the other set with the same size. Then, considering the sum of ${{n}\choose{k}}{{n}\choose{n-k}}$ for all possible k, we get the number of ways to select $n$ objects from a set of size $2n$.


\mychapter{3}{Lesson 3} %181003

\newcommand{\ext}{\textup{\textsf{Ext}}}
\newcommand{\statdist}{\ensuremath{\Delta_\textsc{s}}}
\newcommand{\sdtu}{\ensuremath{\Delta_\textsc{u}}}

\section{Randomness Extraction}

In most of our discourse, the subject of uniformly random variables is much recurrent; this chapter/lesson delves deeper into the topic. For starters, we devise some attempts to extract uniform randomness from ``non-uniform'' randomness sources.

Suppose to have a biased coin $B \sim \berdist(p) : p \neq \half$. How to craft a fair coin out of it? In his time, Von Neumann devised a simple algorithm, which is now known as the \emph{Von Neumann extractor}:

\begin{enumerate}
    \item Let $B \sim \berdist(p)$ be a random variable
    \item \label{enum:VNEsample} Sample $b_1 \pickUAR B$
    \item Sample $b_2 \pickUAR B$
    \item If $b_1 = b_2$ go to step \ref{enum:VNEsample}
    \item \label{enum:VNEreturn} Else:
    \begin{itemize}
        \item If $b_1 = 0 \wedge b_2 = 1$ output 1
        \item If $b_1 = 1 \wedge b_2 = 0$ output 0
    \end{itemize}
\end{enumerate}

Some considerations can be made: The probability of both single cases in step \ref{enum:VNEreturn} is $p(1 - p)$, therefore the probability to reach it is $2p(1 - p)$. Also, it is apparent that the number of possible failures in reaching step \ref{enum:VNEreturn} follow a geometric distribution in $p$, thus the probability of an increased number of failures decrease exponentially.

We now get back to our ultimate goal. Let $X$ be any random variable over a space $\Omega$, we wish to design an ``extraction'' algorithm \ext{} such that $U = \ext(X)$ distributes uniformly over $\Omega$. To help ourselves, we will deal with probability spaces of binary strings ($\binary^n$), and define a measure of ``how much'' a distribution is uniform over its space:

\begin{definition} Let X be a random variable from a given probability distribution. Its \emph{min-entropy} is defined as follows:
\[
    H_{\infty}(X) = -\log_2(\max(\Pr[X = x]))
\]
\end{definition}

Using this measure, we can already see an interesting case, which involves ``constant'' random variables:
\begin{align*}
    X \sim \mathcal{C}onst(\overline{x}) \implies & \Pr[X = \overline{x}] = 1                                       \\
                                         \implies & \Pr[X \neq \overline{x}] = 0                                    \\
                                         \implies & H_{\infty}(X) = -\log_2(\Pr[X = \overline{x}]) = -\log_2(1) = 0 \\
\end{align*}

And in fact, a constant variable is useless in creating a uniform distribution: it always gives the same outcome, making everything deterministic. Therefore, such variables must be excluded in our search for a ``universal extractor''.
On the other hand, looking at a uniform distribution:
\begin{align*}
    X \sim \unifdist(\Omega) \implies & \forall x \Pr[X = x] = \oneover{|\Omega|}   \\
                             \implies & H_{\infty}(X) = -\log_2(\oneover{|\Omega|})
\end{align*}

Knowing that $\Omega$ is be our usual domain choice of binary strings of a given length $\binary^n$, the min-entropy becomes exactly $n$\footnote{This also sheds some light in how the string length is a frequent topic in the cryptography realm, as it usually expresses a cryptosystem's strength: the greater its min-entropy, the harder it is to find the right key from scratch for a ciphertext.}.
Using this measure, we can actually seek how much min-entropy we require in the original distribution $X$ in order for the extractor to return a uniform distribution. Ideally, we would like a value as close to 0 as possible, because a min-entropy of zero leads to constant variables, which have been excluded beforehand. Alas, it turns out that:
\begin{claim}
    There is no such universal \ext{} algorithm that returns a uniform distribution from random variables $X$ with min-entropy $H_{\infty}(X) \leq n - 1$
\end{claim}

\begin{proof}
    % Notes: In our domain, the extraction problem to a unifdist reduces to an extraction of a fair coin; from there, creating a unifdist consists in doing as many coin flips as the strings' length in the domain of choice (remember we're still in the domain of binary strings, each coin flip is essentially one bit).

    % Furthermore, extraction of a fair coin reduces to the extraction of a generic bernoulli variable: from there we can simply use the Von Neumann extractor to get a fair coin

    % WHAT'S HAPPENING DOWN HERE?!?!?!

    %Let \ext{} be a candidate extractor which outputs a fair coin from any given random variable $X$ in a fixed-length binary string space. The resulting coin effectively splits $X$'s domain in two parts $X_0$ and $X_1$, in an attempt to balance the probability that $X$ is in either part. Now, pick the biggest one $X_b$

    %ILLUMINATION: fix ext , run with any X, ext bipartitions the domain, define Y to be unif over an arbitrary part b. Ext(Y) will be forced to output the constant RV on b. contradiction

    %AP190904: Don't like this model, appears to not reflect exactly the matter at hand; algorithms =/= functions

    % Let \ext{} be a candidate extractor, assume that $X$ is any random variable for which $\ext(X)$ is a fair coin. Going deeper into the model, we can figure the extraction result to fairly bipartition the "language" that is formed by repeated saplings from X, as in: "\Omega*". For example, the Von Neumann extractor recognizes the language (00|11)*(01)(0|1)* and doesn't recognize (00|11)*(10)(0|1)*.
    %

    \todo{Help with the proof, things don't look good}

    %Let \ext{} be a candidate extractor, assume that $X$ is any random variable for which $\ext(X)$ is a fair coin. We are %in a situation where
    %
    %Let \ext{} be a candidate extractor, Let b be any binary value s.t. $|Ext^-1(b)|$ is maximal ($Ext^-1: \binary \to \binary^n$)\\
    %$\implies |Ext^-1(b)|\geq 2^{n-1}=\frac{2^2}{2}$
    %
    %\begin{figure}[ht]
    %    \centering
    %    \begin{tikzpicture}[>=latex]
    %
    %        \node (a1) {};
    %        \node[below=0.3cm of a1] (a2) {};
    %        \node[below=0.3cm of a2] (a3) {};
    %
    %        \node[below=0.6cm of a3] (a4) {};
    %        \node[below=0.3cm of a4] (a5) {};
    %        \node[below=0.3cm of a5] (a6) {};
    %
    %        \node[below=0.3cm of a3] (l) {};
    %        \node[right=1.4cm of l] (l1) {};
    %        \node[left=1.4cm of l] (l2) {};
    %        \draw[-,black] (l2) -- (l1);
    %
    %        \node[right=4cm of a2] (b1) {$x=0$};
    %        \node[right=4cm of a5] (b3) {$x=1$};
    %
    %        \node[ellipse,line width = 1pt, draw=black,minimum size=3cm,fit={(a1) (a6)}] {};
    %
    %        \node[below=1cm of a6,font=\color{black}\Large] {$\binary^n$};
    %
    %        \draw[-,black] (a3) to[out=-10,in=190] (b1.190);
    %        \draw[-,black] (a2) -- (b1.180);
    %        \draw[-,black] (a1) to[out=10,in=170] (b1.170);
    %
    %        \draw[-,black] (a6) to[out=-10,in=190] (b3.190);
    %        \draw[-,black] (a5) -- (b3.180);
    %        \draw[-,black] (a4) to[out=10,in=170] (b3.170);
    %    \end{tikzpicture}
    %\end{figure}
    %
    %Define $X$ to be uniform over $Ext^-1(b)$ so $H_{\infty}(x)=n-1$ but $Ext(X)=b$ CONSTANT.
    %
    %\dots

\end{proof}

So this approach is doomed unless we factor in a preemptive small amount of true randomness in the algorithm. This is what a \emph{seeded extractor} does:
\[
    \ext: \underbrace{\binary^d}_{seed(public)}\times \underbrace{\binary^n}_{input} \to \underbrace{\binary^l}_{output}
\]

Before giving a formal definition of such an extractor, we require another notion of measure related to probability distributions:

\begin{definition} \emph{Statistical Distance}:
    Let $X$ and $Y$ be two random variables on the same probability space. Their \emph{statistical distance} is defined as follows:
    \[
        \statdist(X, Y) = \half\sum_{x \in \Omega}|\Pr[X = x] - \Pr[Y = x]|
    \]

\end{definition}

From a more visual perspective, this distance amounts to half the area delimited by the two distributions, if drawn one over another on the outcome space.

\todo{Image of the statistical distance}

In most scenarios, given a random variable $X$, it is valuable to know how much it is distant to a uniform random variable $U$ over the same space $\Omega$. To this purpose, the notation $\statdist(X, U)$ will be shortened to $\sdtu(X)$, making any definition of uniform variables implicit.

\begin{definition}
    Let $\ext \in \binary^d \times \binary^n \to \binary^l$ be a seeded extractor, and $S \sim \unifdist(\binary^d)$. Then it is a ($k$, $\varepsilon$)-extractor iff:
    \[
        \forall X : H_{\infty}(X) \geq k \implies \statdist((S, \ext(S, X)), (S, \unifdist(\binary^l))) \leq \varepsilon
    \]
\end{definition}

Do note that $S$ takes part in both sides of the statistical distance: this is to be interpreted that the seed is known at the time of extraction.

\todo{Need to rethink this definition further}

\subsection{Universal hash functions}

Getting back to our hash function families, we see that they too use an argument as a random seed, and attempt to be as uniform as possible; thus they behave in most ways as seeded extractors. Let's further develop the idea:

\begin{definition}
    Let $S$ be a uniform seed. A hash function family $H$ is deemed \emph{universal} iff:
    \[
        \forall a \neq b \in \Omega \implies \Pr[h_S(a) = h_S(b)] = \oneover{\binary^l}
    \]
\end{definition}

\begin{definition}
    Let $X$ and $Y$ be two \iid{} random variables; a \emph{collision} is the event of both variables evaluating to the same outcome. Such event probabilities are denoted as:
    \[
        Col(X) = Col(Y) = \Pr[X = Y]
    \]
\end{definition}

A different formulation of collision probability can be reached by simple manipulations, with the added benefit that it refers to only one of the two \iid{} random variables:
\begin{align*}
    \Pr(X = Y) =& \sum_{x \in \Omega} \Pr[X = x \wedge Y = x] & \text{(Total probability)} \\
               =& \sum_{x \in \Omega} \Pr[X = x] \Pr[Y = x]   & \text{(Independency)}      \\
               =& \sum_{x \in \Omega} \Pr[X = x]^2                                         \\
\end{align*}

\begin{lemma}[Collision bound] \label{lem:colbound}
    Let $X$ be a random variable such that its collision probability is upper bounded by the following function of some positive value $\varepsilon$ arbitrarily close to $0$:
    \[
        Col(X) \leq \frac{1 + 4\varepsilon^2}{|\Omega|}
    \]
    Then $\sdtu(X) \leq \varepsilon$, meaning that $X$ is almost uniform over $\Omega$.
\end{lemma}

\begin{proof}
    By definition of statistical distance:
    \[
        \sdtu(X) = \half \sum_{x \in \Omega} \left| \Pr[X = x] - \oneover{|\Omega|} \right|
    \]

    Decompose each of the above addends into the couple $q_x$ and $s_x$:
    \[
        q_x = \Pr[X = x] - \oneover{|\Omega|} \qquad s_x =
        \begin{cases}
            1  & \text{if $q_x \geq 0$} \\
            -1 & \text{otherwise}
        \end{cases}
    \]

    Then, by the Euclidean variant of the Cauchy-Schwarz inequality:
    \begin{align*}
        \sdtu(X) =&\ \half \sum_{x \in \Omega} q_x s_x                                                                & \text{(Decomposition)}     \\
              \leq&\ \half \sqrt{ \left( \sum_{x \in \Omega} q_x^2 \right) \left( \sum_{x \in \Omega} s_x^2 \right) } & \text{(Cauchy-Schwarz)}    \\
                 =&\ \half \sqrt{|\Omega| \sum_{x \in \Omega} q_x^2}                                                  & (\forall x \; (s_x^2 = 1)) \\
    \end{align*}

    Focusing on the sum $\sum_{x \in \Omega} q_x^2$:
    \begin{align*}
        \sum_{y \in \Omega} q_x^2 =&\ \sum_{x \in \Omega} \left( \Pr[X = x] - \oneover{|\Omega|} \right)^2                                   &                        \\
                                  =&\ \sum_{x \in \Omega} \left( \Pr[X = x]^2 - \frac{2}{|\Omega|} \Pr[X = x] + \oneover{|\Omega|^2} \right) &                        \\
                                  =&\ Col(X) - \frac{2}{|\Omega|} + \frac{1}{|\Omega|}                                                       &                        \\
                               \leq&\ \frac{1 + 4 \varepsilon^2}{|\Omega|} - \frac{2}{|\Omega|} + \oneover{|\Omega|}                         & \text{(By hypothesis)} \\
                                  =&\ \frac{4 \varepsilon^2}{|\Omega|}                                                                       &                        \\
    \end{align*}

    Thus, getting back to the statistical distance evaluation:
    \begin{align*}
        \sdtu(X) \leq&\ \half \sqrt{|\Omega| \sum_{x \in \Omega} q_x^2}        \\
                 \leq&\ \half \sqrt{|\Omega| \frac{4 \varepsilon^2}{|\Omega|}} \\
                    =&\ \half 2 \varepsilon = \varepsilon                      \\
    \end{align*}

    and by stating that $\varepsilon$ is arbitrarily close to zero, $X$ is statistically close to the uniform distribution over $\Omega$.
\end{proof}

\subsubsection{Leftover hash lemma}

This lemma has been proven by Russell Impagliazzo, Leonid Levin and Michael Luby:

\begin{lemma}[Leftover hash]
    Let $h_s$ be a pairwise-independent hash function with uniform seed $S$, and $\ext \in \binary^d \times \binary^n \to \binary^l$ be a seeded randomness extractor. Then if $\ext(S, x) = h_S(x)$, and $x$ is governed by a random variable $X$ with min-entropy $H_{\infty}(X) \geq k$, where:
    \[
       k = l - 2 \log_2 \varepsilon - 2
    \]
    then $\ext$ is a ($k$, $\varepsilon$)-seeded randomness extractor.
\end{lemma}

\begin{proof}
    The extractor definition requires that:
    \[
        \statdist(Y, (S, U_l)) \leq \varepsilon
    \]
    where $U_l$ distributes uniformly over $\binary^l$. Although the appearance of $S$ in both operands may seem to complicate things, the distance formulation can be changed to remove such constraint, and become simpler:
    \begin{align*}
         &\ \statdist((S, h_S(X))), (S, U_l))                                                                                &                \\
        =&\ \half \sum_{(s, t) \in \binary^{d + l}} \left| \Pr[(S, h_S(X))) = (s, t)] - \Pr[(S, U_l) = (s, t)] \right|                      &                \\
        =&\ \half \sum_{(s, t) \in \binary^{d + l}} \left| \Pr[(S, h_S(X))) = (s, t)] - \oneover{|\binary|^d} \oneover{|\binary^l|} \right| & (S \indep U_l) \\
        =&\ \half \sum_{(s, t) \in \binary^{d + l}} \left| \Pr[(S, h_S(X))) = (s, t)] - \Pr[U_{d + l} = (s, t)] \right|                     &                \\
         &                                                                                & \mathllap{(U_{d + l} \sim \unifdist(\binary^{d + l}))} \\
        =&\ \statdist((S, h_S(X)), U_{d + l})                                                                                &                \\
        =&\ \sdtu((S, h_S(X)))                                                                                &                \\
    \end{align*}

    At this point the \hyperref[lem:colbound]{collision bound} can be used to put an upper bound on this statistical distance. To this end, let $Y = (S, h_S(X))$ and $Y = (S, h_S(X))$ be two \iid{} random variables; the collision probability of $Y$ equates to:
    \begin{align*}
         &\ Col(Y)                                               &                                               \\
        =&\ \Pr[Y = Y']                                          &                                               \\
        =&\ \Pr[S = S' \wedge h_S(X) = h_{S'}(X')]               & \mathllap{\text{(By definition)}}             \\
        =&\ \Pr[S = S'] \Pr[h_S(X) = h_{S'}(X') \knowing S = S'] & \mathllap{\text{(Conditional prob.)}}         \\
        =&\ \Pr[S = S'] \Pr[h_S(X) = h_S(X')]                    & \mathllap{\text{(Condition collapse)}}        \\
        =&\ 2^{-d} \Pr[h_S(X) = h_S(X')]                         & \mathllap{\text{(Uniform collision prob.)}}   \\
        =&\ 2^{-d} \left( \Pr[h_S(X) = h_S(X') \wedge X = X'] + \Pr[h_S(X) = h_S(X') \wedge X \neq X'] \right) & \\
         &                                                       & \mathllap{\text{(Total probability)}}         \\
    \end{align*}

    The two addends are tackled separately. For the first one:

    \begin{align*}
         &\ \Pr[h_S(X) = h_S(X') \wedge X = X']               &                               \\
        =&\ \Pr[X = X'] \Pr[h_S(X) = h_S(X') \knowing X = X'] & \text{(Conditional prob.)}    \\
        =&\ Col(X) \Pr[h_S(X) = h_S(X') \knowing X = X']      & \text{(Collision def.)}       \\
        =&\ Col(X) \Pr[h_S(X) = h_S(X)]                       & \text{(Condition collapse)}   \\
        =&\ Col(X)                                            & \text{(Prob. of a tautology)} \\
    \end{align*}

    \begin{proposition}
        Given any random variable $X$:
        \[
            H_\infty(X) \geq k \implies Col(X) \leq 2^{-k} \qedhere
        \]
    \end{proposition}

    \begin{proof}
        By definition of min-entropy\footnote{The definition specifies explicitly a base-2 logarithm, but can actually be any base; in case, the statement to be proved shall correct the collision probability bound accordingly by changing its exponential basis}:
        \begin{align*}
            -\log \max_{x \in \Omega}(\Pr [X = x]) &\geq k      \\
            \log \max_{x \in \Omega}(\Pr [X = x])  &\leq -k     \\
            \max_{x \in \Omega}(\Pr [X = x])       &\leq 2^{-k} \\
        \end{align*}

        On the other hand:
        \begin{align*}
            Col(X) =&\ \sum_{x \in \Omega} \Pr[X = x]^2                               \\
                \leq&\ \sum_{x \in \Omega} \Pr[X = x] \max_{y \in \Omega}(\Pr[X = y]) \\
                   =&\ \max_{y \in \Omega}(\Pr[X = y]) \sum_{x \in \Omega} \Pr[X = x] \\
                   =&\ \max_{y \in \Omega}(\Pr[X = y])                                \\
        \end{align*}

        Therefore:
        \[
            Col(X) \leq \max_{x \in \Omega}(\Pr[X = x]) \leq 2^{-k}
        \]
    \end{proof}

    Shifting the focus to the second addend:

    \begin{align*}
            &\ \Pr[h_S(X) = h_S(X') \wedge X \neq X']                  &                                                  \\
           =&\ \Pr[X \neq X'] \Pr[h_S(X) = h_S(X') \knowing X \neq X'] & \text{(Conditional prob.)}                       \\
        \leq&\ \Pr[h_S(X) = h_S(X') \knowing X \neq X']                & \mathllap{\text{(Prob. is not greater than 1)}}  \\
    \end{align*}

    \begin{proposition}
        If $h_S$ is a pairwise independent hash function, then:
        \[
            \Pr[h_S(X) = h_S(X') \knowing X \neq X'] \leq 2^{-l}
        \]
    \end{proposition}

    \begin{proof}
        \todo{Left as an exercise.}
    \end{proof}

    Going back to the collision probability of $Y$, and using the two propositions above:
    \begin{align*}
        Col(Y) \leq&\ 2^{-d} \left( Col(X) + \Pr[h_S(X) = h_S(X') \knowing X \neq X'] \right) & \\
               \leq&\ 2^{-d} (2^{-k} + 2^{-l}) & \\
                  =&\ 2^{-d - l} (2^{-k + l} + 2^{-l + l}) & \\
                  =&\ 2^{-d - l} (2^{-(l - 2 \log_2(\varepsilon) - 2) + l} + 1)  & \\
                  =&\ 2^{-d - l} (2^{2 \log_2(\varepsilon) + 2} + 1)  & \\
                  =&\ 2^{-d - l} (4\varepsilon^2 + 1) & \\
    \end{align*}

    Applying the collision bound entails that $\sdtu(Y) \leq \varepsilon$, therefore $h_S$ is a $(k, \varepsilon)$-seeded randomness extractor.
\end{proof}

Note that for smaller values of $\varepsilon$ we have greater values of min-entropy. A problem that is still open is to find an extractor with $k \approx l$.


%!TEX root = da2020-07.tex

\Chapter{7}{Covering Maps}

\noindent
Chapters \chapterref{3}--\chapterref{6} have focused on positive results; now we will turn our attention to techniques that can be used to prove negative results. We will start with so-called covering maps\mydash we will use covering maps to prove that many problems cannot be solved at all with deterministic $\PN$-algorithms.

\section{Definition}

A covering map is a topological concept that finds applications in many areas of mathematics, including graph theory. We will focus on one special case: covering maps between port-numbered networks.

Let $N = (V,P,p)$ and $N' = (V'\!,P'\!,p')$ be port-numbered networks, and let $\phi \colon V \to V'$. We say that $\phi$ is a \emph{covering map from $N$ to $N'$} if the following holds:
\begin{enumerate}\raggedright
    \item $\phi$ is a surjection: $\phi(V) = V'$.
    \item $\phi$ preserves degrees: $\deg_{N}(v) = \deg_{N'}(\phi(v))$ for~all~$v \in V$.
    \item $\phi$ preserves connections and port numbers: $p(u,i) = (v,j)$ implies $p'(\phi(u),i) = (\phi(v),j)$.
\end{enumerate}
See Figures \ref{fig:covering-map}--\ref{fig:covering-map3} for examples.

\begin{figure}
    \centering
    \includegraphics[page=\PCoveringMap]{figs.pdf}
    \caption{There is a covering map $\phi$ from $N$ to $N'$ that maps $a_i \mapsto a$, $b_i \mapsto b$, $c_i \mapsto c$, and $d_i \mapsto d$ for each $i \in \{1, 2\}$.}\label{fig:covering-map}
\end{figure}

\begin{figure}
    \centering
    \includegraphics[page=\PCoveringMapB]{figs.pdf}
    \caption{There is a covering map $\phi$ from $N$ to $N'$ that maps $v_i \mapsto v$ for each $i \in \{1, 2, 3\}$. Here $N$ is a simple port-numbered network but $N'$ is not.}\label{fig:covering-map2}
\end{figure}

\begin{figure}
    \centering
    \includegraphics[page=\PCoveringMapC]{figs.pdf}
    \caption{There is a covering map $\phi$ from $N$ to $N'$ that maps $v_i \mapsto v$ for each $i \in \{1, 2\}$. Again, $N$ is a simple port-numbered network but $N'$ is not.}\label{fig:covering-map3}
\end{figure}

We can also consider labeled networks, for example, networks with local inputs. Let $f\colon V \to X$ and $f'\colon V' \to X$. We say that $\phi$ is a covering map from $(N,f)$ to $(N'\!,f')$ if $\phi$ is a covering map from $N$ to $N'$ and the following holds:
\begin{enumerate}[resume*]
    \item $\phi$ preserves labels: $f(v) = f'(\phi(v))$ for all $v \in V$.
\end{enumerate}

\section{Covers and Executions}

Now we will study covering maps from the perspective of deterministic $\PN$-algorithms. The basic idea is that a covering map $\phi$ from $N$ to $N'$ fools any $\PN$-algorithm $A$: a node $v$ in $N$ is indistinguishable from the node $\phi(v)$ in $N'$.

Without further ado, we state the main result and prove it\mydash many applications and examples will follow.

\begin{theorem}\label{thm:cover}
    Assume that
    \begin{enumerate}[itemsep=0ex]\raggedright
        \item $A$ is a deterministic $\PN$-algorithm with $X = \Input_A$,
        \item $N = (V,P,p)$ and $N' = (V'\!,P'\!,p')$ are port-numbered networks,
        \item $f\colon V \to X$ and $f'\colon V' \to X$ are arbitrary functions, and
        \item $\phi\colon V \to V'$ is a covering map from $(N,f)$ to $(N'\!,f')$.
    \end{enumerate}
    Let
    \begin{enumerate}[resume*]
        \item $x_0, x_1, \dotsc$ be the execution of $A$ on $(N,f)$, and
        \item $x'_0, x'_1, \dotsc$ be the execution of $A$ on $(N'\!,f')$.
    \end{enumerate}
    Then for each $t = 0, 1, \dotsc$ and each $v \in V$ we have $x_t(v) = x'_t(\phi(v))$.
\end{theorem}

\begin{proof}
    We will use the notation of Section~\longref{3.3.2}{ssec:execution}; the symbols with a prime refer to the execution of $A$ on $(N'\!,f')$. In particular, $m'_t(u',i)$ is the message received by $u' \in V'$ from port $i$ in round $t$ in the execution of $A$ on $(N'\!,f')$, and $m'_t(u')$ is the vector of messages received by $u'$.

    The proof is by induction on $t$. To prove the base case $t = 0$, let $v \in V$, $d = \deg_N(v)$, and $v' = \phi(v)$; we have
    \[
        x'_0(v') = \Init_{A,d}(f'(v')) = \Init_{A,d}(f(v)) = x_0(v).
    \]

    For the inductive step, let $(u,i) \in P$, $(v,j) = p(u,i)$, $d = \deg_N(u)$, $\ell = \deg_N(v)$, $u' = \phi(u)$, and $v' = \phi(v)$. Let us first consider the messages sent by $v$ and $v'$; by the inductive assumption, these are equal:
    \[
        \Send_{A,\ell}(x'_{t-1}(v')) = \Send_{A,\ell}(x_{t-1}(v)).
    \]

    A covering map $\phi$ preserves connections and port numbers: $(u,i) = p(v,j)$ implies $(u',i) = p'(v',j)$. Hence $m_t(u,i)$ is component $j$ of $\Send_{A,\ell}(x_{t-1}(v))$, and $m'_t(u',i)$ is component $j$ of $\Send_{A,\ell}(x'_{t-1}(v'))$. It follows that $m_t(u,i) = m'_t(u',i)$ and $m_t(u) = m'_t(u')$. Therefore
    \begin{align*}
        x'_t(u')
        &= \Receive_{A,d}\bigl(x'_{t-1}(u'), m'_t(u') \bigr) \\
        &= \Receive_{A,d}\bigl(x_{t-1}(u), m_t(u) \bigr)
        = x_t(u). \qedhere
    \end{align*}
\end{proof}

In particular, if the execution of $A$ on $(N,f)$ stops in time $T$, the execution of $A$ on $(N'\!,f')$ stops in time $T$ as well, and vice versa. Moreover, $\phi$ preserves the local outputs: $x_T(v) = x'_T(\phi(v))$ for all $v \in V$.

\section{Examples}

We will give representative examples of negative results that we can easily derive from Theorem~\ref{thm:cover}. First, we will observe that a deterministic $\PN$-algorithm cannot break symmetry in a cycle\mydash unless we provide some symmetry-breaking information in local inputs.

\begin{lemma}\label{lem:cycle-symmetric}
    Let $G = (V,E)$ be a cycle graph, let $A$ be a deterministic $\PN$-algorithm, and let $f$ be a constant function $f\colon V \to \{0\}$. Then there is a simple port-numbered network $N = (V,P,p)$ such that
    \begin{enumerate}
        \item the underlying graph of $N$ is $G$, and
        \item if $A$ stops on $(N,f)$, the output is a constant function $g\colon V \to \{c\}$ for some $c$.
    \end{enumerate}
\end{lemma}
\begin{proof}
    Label the nodes $V = \Set{ v_1, v_2, \dotsc, v_n }$ along the cycle so that the edges are
    \[
        E = \bigSet{ \{v_1, v_2\},\ \{v_2, v_3\},\ \dotsc,\ \{v_{n-1}, v_n\},\ \{v_n, v_1\} }.
    \]
    Choose the port numbering $p$ as follows:
    \begin{align*}
        p\colon &(v_1, 1) \mapsto (v_2, 2),\ (v_2, 1) \mapsto (v_3, 2),\ \dotsc, \\
                &(v_{n-1}, 1) \mapsto (v_n, 2),\ (v_n, 1) \mapsto (v_1, 2).
    \end{align*}
    See Figure~\ref{fig:covering-map2} for an illustration in the case $n = 3$.

    Define another port-numbered network $N' = (V'\!,P'\!,p')$ with $V' = \{v\}$, $P' = \{ (v,1), (v,2) \}$, and $p(v,1) = (v,2)$. Let $f'\colon V' \to \{0\}$. Define a function $\phi\colon V \to V'$ by setting $\phi(v_i) = v$ for each $i$.

    Now we can verify that $\phi$ is a covering map from $(N,f)$ to $(N'\!,f')$. Assume that $A$ stops on $(N,f)$ and produces an output~$g$. By Theorem~\ref{thm:cover}, $A$ also stops on $(N'\!,f')$ and produces an output~$g'$. Let $c = g'(v)$. Now
    \[
        g(v_i) = g'(\phi(v_i)) = g'(v) = c
    \]
    for all~$i$.
\end{proof}

In the above proof, we never assumed that the execution of $A$ on $N'$ makes any sense\mydash after all, $N'$ is not even a simple port-numbered network, and there is no underlying graph. Algorithm $A$ was never designed to be applied to such a strange network with only one node. Nevertheless, the execution of $A$ on $N'$ is formally well-defined, and Theorem~\ref{thm:cover} holds. We do not really care what $A$ outputs on $N'$, but the existence of a covering map can be used to prove that the output of $A$ on $N$ has certain properties. It may be best to interpret the execution of $A$ on $N'$ as a thought experiment, not as something that we would actually try to do in practice.

Lemma~\ref{lem:cycle-symmetric} has many immediate corollaries.

\begin{corollary}\label{cor:cycle-symmetric}
    Let $\calF$ be the family of cycle graphs. Then there is no deterministic $\PN$-algorithm that solves any of the following problems on~$\calF$:
    \begin{enumerate}[noitemsep]
        \item maximal independent set,
        \item \Apx{1.999} of a minimum vertex cover,
        \item \Apx{2.999} of a minimum dominating set,
        \item maximal matching,
        \item vertex coloring,
        \item weak coloring,
        \item edge coloring.
    \end{enumerate}
\end{corollary}
\begin{proof}
    In each of these cases, there is a graph $G \in \calF$ such that a constant function is not a feasible solution in the network $N$ that we constructed in Lemma~\ref{lem:cycle-symmetric}.

    For example, consider the case of dominating sets; other cases are similar. Assume that $G = (V,E)$ is a cycle with $3k$ nodes. Then a minimum dominating set consists of $k$ nodes\mydash it is sufficient to take every third node. Hence a \Apx{2.999} of a minimum dominating set consists of at most $2.999k < 3k$ nodes. A solution $D = V$ violates the approximation guarantee, as $D$ has too many nodes, while $D = \emptyset$ is not a dominating set. Hence if $A$ outputs a constant function, it cannot produce a \Apx{2.999} of a minimum dominating set.
\end{proof}

\begin{lemma}
    There is no deterministic $\PN$-algorithm that finds a weak coloring for every \Reg{3} graph.
\end{lemma}
\begin{proof}
    Again, we are going to apply the standard technique: pick a suitable \Reg{3} graph $G$, find a port-numbered network $N$ that has $G$ as its underlying graph, find a smaller network $N'$ such that we have a covering map $\phi$ from $N$ to $N'$, and apply Theorem~\ref{thm:cover}.

    However, it is not immediately obvious which \Reg{3} graph would be appropriate; hence we try the simplest possible case first. Let $G = (V,E)$ be the \emph{complete graph} on four nodes: $V = \Set{s,t,u,v}$, and we have an edge between any pair of nodes; see Figure~\ref{fig:three-reg}. The graph is certainly \Reg{3}: each node is adjacent to the other three nodes.

    \begin{figure}
        \centering
        \includegraphics[page=\PThreeReg]{figs.pdf}
        \caption{Graph $G$ is the complete graph on four nodes. The edges of $G$ can be partitioned into a $2$-factor $X$ and a $1$-factor $Y$. Network $N$ has $G$ as its underlying graph, and there is a covering map $\phi$ from $N$ to $N'$}\label{fig:three-reg}
    \end{figure}

    Now it is easy to verify that the edges of $G$ can be partitioned into a $2$-factor $X$ and a $1$-factor $Y$. The $2$-factor consists of a cycle and a $1$-factor consists of disjoint edges. We can use the factors to guide the selection of port numbers in~$N$.

    In the cycle induced by $X$, we can choose symmetric port numbers using the same idea as what we had in the proof of Lemma~\ref{lem:cycle-symmetric}; one end of each edge is connected to port $1$ while the other end is connected to port $2$. For the edges of the $1$-factor $Y$, we can assign port number $3$ at each end. We have constructed the port-numbered network $N$ that is illustrated in Figure~\ref{fig:three-reg}.

    Now we can verify that there is a covering map $\phi$ from $N$ to $N'$, where $N'$ is the network with one node illustrated in Figure~\ref{fig:three-reg}. Therefore in any algorithm $A$, if we do not have any local inputs, all nodes of $N$ will produce the same output. However, a constant output is not a weak coloring of $G$.
\end{proof}

In the above proof, we could have also partitioned the edges of $G$ into three $1$-factors, and we could have used the $1$-factorization to guide the selection of port numbers. However, the above technique is more general: there are \Reg{3} graphs that do not admit a $1$-factorization but that can be partitioned into a $1$-factor and a $2$-factor.

So far we have used only one covering map in our proofs; the following lemma gives an example of the use of more than one covering map.

\begin{lemma}\label{lem:cycles-and-covers}
    Let $\calF = \Set{G_3, G_4}$, where $G_3$ is the cycle graph with $3$ nodes, and $G_4$ is the cycle graph with $4$ nodes. There is no deterministic $\PN$-algorithm that solves the following problem $\Pi$ on $\calF$: in $\Pi(G_3)$ all nodes output $3$ and in $\Pi(G_4)$ all nodes output $4$.
\end{lemma}
\begin{proof}
    We again apply the construction of Lemma~\ref{lem:cycle-symmetric}; for each $i \in \{3,4\}$, let $N_i$ be the symmetric port-numbered network that has $G_i$ as the underlying graph.

    Now it would be convenient if we could construct a covering map from $N_4$ to $N_3$; however, this is not possible (see the exercises). Therefore we proceed as follows. Construct a one-node network $N'$ as in the proof of Lemma~\ref{lem:cycle-symmetric}, construct the covering map $\phi_3$ from $N_3$ to $N'$, and construct the covering map $\phi_4$ from $N_4$ to $N'$; see Figure~\ref{fig:cycles-and-covers}. The local inputs are assumed to be all zeros.

    \begin{figure}
        \centering
        \includegraphics[page=\PCyclesAndCovers]{figs.pdf}
        \caption{The structure of the proof of Lemma~\ref{lem:cycles-and-covers}.}\label{fig:cycles-and-covers}
    \end{figure}

    Let $A$ be a $\PN$-algorithm, and let $c$ be the output of the only node of $N'$. If we apply Theorem~\ref{thm:cover} to $\phi_3$, we conclude that all nodes of $N_3$ output $c$; if $A$ solves $\Pi$ on $G_3$, we must have $c = 3$. However, if we apply Theorem~\ref{thm:cover} to $\phi_4$, we learn that all nodes of $N_4$ also output $c = 3$, and hence $A$ cannot solve $\Pi$ on $\calF$.
\end{proof}

We have learned that a deterministic $\PN$-algorithm cannot determine the length of a cycle. In particular, a deterministic $\PN$-algorithm cannot determine if the underlying graph is bipartite.

\section{Quiz}

Let $G = (V, E)$ be a graph. A set $X \subseteq V$ is a \emph{$k$-tuple dominating set} if for every $v \in V$ we have $|\ball_G(v,1) \cap X| \ge k$. Consider the problem of finding a minimum 2-tuple dominating set in \emph{cycles}. What is the best (i.e.\ smallest) approximation ratio we can achieve in the $\PN$ model?

\section{Exercises}

We use the following definition in the exercises. A graph $G$ is \emph{homogeneous} if there are port-numbered networks $N$ and $N'$ and a covering map $\phi$ from $N$ to $N'$ such that $N$ is simple, the underlying graph of $N$ is $G$, and $N'$ has only one node. For example, Lemma~\ref{lem:cycle-symmetric} shows that all cycle graphs are homogeneous.

\begin{ex}[finding port numbers]\label{ex:cover-three-reg1}
    Consider the graph $G$ and network $N'$ illustrated in Figure~\ref{fig:cover-ex-three-reg}. Find a simple port-numbered network $N$ such that $N$ has $G$ as the underlying graph and there is a covering map from $N$ to $N'$.

    \begin{figure}
        \centering
        \includegraphics[page=\PCoverExThreeReg]{figs.pdf}
        \caption{Graph $G$ and network $N'$ for Exercises \ref{ex:cover-three-reg1} and \ref{ex:cover-reg}b.}\label{fig:cover-ex-three-reg}
    \end{figure}
\end{ex}

\begin{ex}[homogeneity]
    Assume that $G$ is homogeneous and it contains a node of degree at least two. Give several examples of graph problems that cannot be solved with any deterministic $\PN$-algorithm in any family of graphs that contains $G$.
\end{ex}

\begin{ex}[regular and homogeneous]\label{ex:cover-reg}
    Show that the following graphs are homogeneous:
    \begin{subex}
        \item graph $G$ illustrated in Figure~\ref{fig:cover-ex-four-reg},
        \item graph $G$ illustrated in Figure~\ref{fig:cover-ex-three-reg}.
    \end{subex}

    \hint{(a)~Apply the result of Exercise~\longref{2.8}{ex:2fact}. (b)~Find a $1$-factor.}

    \begin{figure}
        \centering
        \includegraphics[page=\PCoverExFourReg]{figs.pdf}
        \caption{Graph $G$ for Exercise~\ref{ex:cover-reg}a.}\label{fig:cover-ex-four-reg}
    \end{figure}
\end{ex}

\begin{ex}[complete graphs]\label{ex:cover-complete}
    Recall that we say that a graph $G = (V,E)$ is \emph{complete} if for all nodes $u, v \in V$, $u \ne v$, there is an edge $\{u,v\} \in E$. Show that
    \begin{subex}
        \item any $2k$-regular graph is homogeneous,
        \item any complete graph with $2k$ nodes has a $1$-factorization,
        \item any complete graph is homogeneous.
    \end{subex}
\end{ex}

\begin{ex}[dominating sets]\label{ex:domset}
    Let $\Delta \in \{2,3,\dotsc\}$, let $\epsilon > 0$, and let $\calF$ consist of all graphs of maximum degree at most $\Delta$. Show that it is possible to find a \Apx{(\Delta+1)} of a minimum dominating set in constant time in family~$\calF$ with a deterministic $\PN$-algorithm. Show that it is not possible to find a \Apx{(\Delta+1-\epsilon)} with a deterministic $\PN$-algorithm.

    \hint{For the lower bound, use the result of Exercise~\ref{ex:cover-complete}c.}
\end{ex}

\begin{ex}[covers with covers]\label{ex:cover-cover}
    What is the connection between covering maps and the vertex cover 3-approximation algorithm in Section~\longref{3.6}{sec:vc3}?
\end{ex}

\begin{exs}[\Reg{3} and not homogeneous]\label{ex:cover-three-reg-b}
    Consider the graph $G$ illustrated in Figure~\ref{fig:cover-ex-three-reg-b}.
    \begin{subex}
        \item Show that $G$ is not homogeneous.
        \item Present a deterministic $\PN$-algorithm $A$ with the following property: if $N$ is a simple port-numbered network that has $G$ as the underlying graph, and we execute $A$ on $N$, then $A$ stops and produces an output where at least one node outputs $0$ and at least one node outputs $1$.
        \item Find a simple port-numbered network $N$ that has $G$ as the underlying graph, a port-numbered network $N'$, and a covering map $\phi$ from $N$ to $N'$ such that $N'$ has the smallest possible number of nodes.
    \end{subex}
    \hint{Show that if a \Reg{3} graph is homogeneous, then it has a $1$-factor. Show that $G$ does not have any $1$-factor.}

    \begin{figure}
        \centering
        \includegraphics[page=\PCoverExThreeRegB]{figs.pdf}
        \caption{Graph $G$ for Exercise~\ref{ex:cover-three-reg-b}.}\label{fig:cover-ex-three-reg-b}
    \end{figure}
\end{exs}

\begin{exs}[covers and connectivity]
    Assume that $N = (V,P,p)$ and $N' = (V'\!,P'\!,p')$ are simple port-numbered networks such that there is a covering map $\phi$ from $N$ to $N'$. Let $G$ be the underlying graph of network $N$, and let $G'$ be the underlying graph of network~$N'$.
    \begin{subex}
        \item Is it possible that $G$ is connected and $G'$ is not connected?
        \item Is it possible that $G$ is not connected and $G'$ is connected?
    \end{subex}
\end{exs}

\begin{exs}[$k$-fold covers]
    Let $N = (V,P,p)$ and $N' = (V'\!,P'\!,p')$ be simple port-numbered networks
    such that the underlying graphs of $N$ and $N'$ are connected, and
    assume that $\phi\colon V \to V'$ is a covering map from $N$ to $N'$.
    Prove that there exists a positive integer $k$ such that the following holds:
    $|V| = k |V'|$ and for each node $v' \in V'$ we have $|\phi^{-1}(v')| = k$.
    Show that the claim does not necessarily hold if the underlying graphs are not connected.
\end{exs}

\section{Bibliographic Notes}

The use of covering maps in the context of distributed algorithm was introduced by Angluin~\cite{angluin80local}. The general idea of Exercise~\ref{ex:cover-three-reg-b} can be traced back to Yamashita and Kameda~\cite{yamashita96computing}, while the specific construction in Figure~\ref{fig:cover-ex-three-reg-b} is from Bondy and Murty's textbook~\cite[Figure~5.10]{bondy76graph-theory}. Parts of exercises \ref{ex:cover-three-reg1}, \ref{ex:cover-reg}, \ref{ex:cover-complete}, and \ref{ex:domset} are inspired by our work \cite{suomela10eds,astrand10weakly-coloured}.


\section{Graph and network optimization}

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{figures/graphsIntroduction.png}
\caption{Introduction to Graphs}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/weightedAdjecency.png}
\caption{Example Graph with weight and Adjecency Matrix }
\end{figure}

\subsection{Depth-First Search (DFS)}

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{figures/dfs.png}
\caption{Depth-First Search}
\end{figure}

\subsection{Breadth-First Search (BFS)}

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{figures/bfs.png}
\caption{Depth-First Search}
\end{figure}

\subsection{Spanning trees}

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{figures/spanningtree.png}
\caption{Spanning trees}
\end{figure}

\subsubsection{Minimal weight Optimistic approach (And Kruskal’s algorithm)}

\textbf{The Kruskal algorithm is exactly the same approach like the optimistic approach.} \\
\textit{The Prim algorithm just differ a little bit since you have a starting node and then search within the connected edges.}

\begin{enumerate}
    \item Find the next edge with the lowest weight in the whole graph
    \item Check if the edge is redundant or not
    \item If the edge is not redundant, mark it as usable. If it is redundant, mark it as unusable
    \item Repeat until you have all edges checked
\end{enumerate}

\begin{figure}[H]
\centering
\includegraphics[width=0.3\textwidth]{figures/optimisticGraphAlg.png}
\caption{Minimal weight - Optimistic Approach}
\end{figure}

\subsubsection{Minimal weight Pessimistic approach}

\begin{enumerate}
    \item Find the next edge with the highest weight in the whole graph
    \item Check if you can remove the edge without splitting the graph into two graphs
    \item If you can remove the edge, mark it as unusable. If you can't remove the edge, mark it as usable
    \item Repeat until you have all edges checked
\end{enumerate}

\begin{figure}[H]
\centering
\includegraphics[width=0.3\textwidth]{figures/pessimisticGraphAlg.png}
\caption{Minimal weight - Pessimistic Approach}
\end{figure}

\clearpage
\subsubsection{Minimal weight Prim's algorithm}
Choose an arbitrary start vertex $v_0$ and set $M = \{v_0\}$.
Iteratively add to $M$ a vertex in $V \setminus M$ that can be reached the
cheapest from the current set $M$. Select the corresponding
edge. Continue until $M = V$ .

\begin{figure}[H]
\centering
\includegraphics[width=0.3\textwidth]{figures/primsGraphAlg.png}
\caption{Minimal weight - Prim's Algorithm}
\end{figure}

\subsection{Shortes path problem}

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{figures/shortestPathProblem.png}
\caption{Spanning trees}
\end{figure}

\clearpage
\subsubsection{Dijkstra's algorithm}
Computes shortest paths from one vertex $v_0$ to all other vertices (i.e., a shortest-paths tree from $v_0$).
We iteratively compute the shortest distance $l(v)$ for the vertex
$v$ closest to $v_0$ that has not been reached yet, as follows:

\begin{enumerate}
    \item Set $V_0 = \{v_0\}, E_0 = \{\}$ and $I(v_0) = 0$
    \item Do the following $n-1$ times: \\
    $V_i = \{v_0, ..., v_i\}$ and $E_i = \{e_1, ..., e_i\}$ are the sets of vertices and edges already visited. For each edge $e = (u, v)$ with $u \in V_i$ and $v \in V \setminus V_i$, compute $l(u) + weight(e)$. Choose the edge minimizing this as $e_{i+1} = (u_{i+1}, v_{i+1})$. \\
    Set $V_{i+1} = V_i  \cup \{v_{i+1}\}, E_{i+1} = E_i \cup \{e_{i+1}\}$ and $l(v_{i+1}) = l(u_{i+1}) + weight(e_{i+1})$
\end{enumerate}

\begin{figure}[H]
\centering
\includegraphics[width=0.8\textwidth]{figures/dijkstra.png}
\caption{Shortest Path - Dijkstra's Algorithm}
\end{figure}

\clearpage
\subsubsection{Floyd-Warshall algorithm}

The Floyd-Warshall algorithm solves the all-pairs shortest paths
problem. Unlike Dijkstra’s algorithm, it can also handle the case of
negative edge-weights.

\begin{enumerate}
    \item Initialization: \\
    $minPath(i, j, 0) =
  \begin{cases}
    w(v_i, v_j)       & \quad \text{if } (v_i, v_j) \in E\\
     \infty   & \quad \text{else}
  \end{cases}$
  \item For $k = 1,2,..., n$ compute: \\
  $minPath (i, j, k )$ for all $i$ und $j$ according iteration formula
\end{enumerate}

\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{figures/floyd1.png}
\caption{Shortest Path - Floyd-Warshall Matrix 1}
\end{figure}

\begin{itemize}
    \item Now I start with the first node $a$ and compare every other node if I can find a shorter path over $a$
    \item Starting this iteration with $b$, I check if I can find e.g. a shorter path from $b$ to $d$ over $a$.
    \item In this example, I see from $b$ to $d$ the weight is $5$. From $a$ to $b$ it is $3$, from $a$ to $d$ it is $9$, which would make a total of $12$. Since $12 \geq 5$, we leave $5$ and check the next node.
\end{itemize}

\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{figures/floyd2.png}
\caption{Shortest Path - Floyd-Warshall Matrix 2}
\end{figure}

\begin{itemize}
    \item After we have done this for every entry in the matrix compared to $a$, we start the next iteration step and comparing it with $b$
    \item In the next example, we found a improvement for the connection from $a$ to $d$. $a$ to $d$ has a weight of $9$, but if you go first from $a$ to $b$ with $3$, and then from $b$ to $d$ with $5$, you have a weight of $8$.
\end{itemize}

\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{figures/floyd3.png}
\caption{Shortest Path - Floyd-Warshall Matrix 3}
\end{figure}

\begin{itemize}
    \item After every iteration step, write the improvements directly into the matrix
    \item After comparing the entries in the matrix to every node, you should not have any $\infty$ anymore.
\end{itemize}

\clearpage
\subsection{Network flow}
\begin{figure}[H]
\centering
\includegraphics[width=0.8\textwidth]{figures/networkFlow.png}
\caption{Network Flow}
\end{figure}

\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
An \textbf{st-network} $(G, w, s, t)$ is a weighted graph $G(V,E)$ with weight function $w$ and two distinguished vertices $s,t, \in V$, where $s$ is the source and $t$ is the target.
\end{tcolorbox}

\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
A graph $G(V,E)$ is \textbf{bipartite} if its vertex set can be split in two parts $A$ and $B$ such that all edges have one vertex in $A$ and one in $B$.
\end{tcolorbox}

\subsubsection{Maximum Flow}

\begin{figure}[H]
\centering
\includegraphics[width=0.3\textwidth]{figures/maximumFlow.png}
\caption{Maximum Network Flow Example}
\end{figure}

Finding the first best maximum network flow is not the best solution all the time. Sometimes, there are multiple paths and therefore you need to check more than one path. The Ford-Fulkerson algorithm checks already used paths for this.

\subsubsection{Ford-Fulkerson algorithm}
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{figures/ford-fulkerson.png}
\caption{Ford Fulkerson algorithm}
\end{figure}

\subsubsection{Edmonds-Karp algorithm}
The Edmonds-Karp algorithm finds the path from $s$ to $t$ with the fewest number of edges. The algorithm is using Breadth-First Search for finding this.

\begin{figure}[H]
\centering
\includegraphics[width=0.3\textwidth]{figures/Edmonds-Karp.png}
\caption{Edmonds-Karp algorithm}
\end{figure}

\subsubsection{Distribution Problem}

\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{figures/distributionProblem.png}
\caption{Network Flow - Distribution Problem}
\end{figure}

\subsubsection{Vertices with restrictions}

\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{figures/restrictionModelling.png}
\caption{Network Flow - Vertices with restrictions}
\end{figure}

\subsubsection{Bipartite Matching}

\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{figures/bipartiteMatching.png}
\caption{Network Flow - Bipartite Matching}
\end{figure}

\subsubsection{ST-Cut}

\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{figures/stCut.png}
\caption{Network Flow - ST-Cut}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{figures/st-cut.png}
\caption{Network Flow - ST-Cut Example}
\end{figure}

\subsubsection{Min-Cost Max-Flow}

With the Ford-Fulkerson, there can be multiple solutions. You can now add costs to the edges and use this to find the best solution with the Ford-Fulkerson algorithm respecting the costs.

\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{figures/min-cost-max-flow.png}
\caption{Network Flow - Min-Cost Max-Flow}
\end{figure}

\clearpage