\documentclass{beamer}

\usepackage{default}
\usepackage[german]{babel}
\usepackage[utf8]{inputenc}                   % replace by the encoding you are using

\usetheme{Berlin}

%Header Settings
%\setbeamertemplate{headline}{}

%Footer Settings
\setbeamertemplate{navigation symbols}{
	\usebeamerfont{footline}%
	\usebeamercolor[fg]{footline}%
	\hspace{1em}%
	\insertframenumber/\inserttotalframenumber
}

\title[Java]{Java - Variables}
\author[W. Bombardelli]{William Bombardelli}
\institute[Schweizerschule Mexiko]
{
	\vskip 12pt
	Schweizerschule Mexiko, Ciudad de México, Mexico \\
	\texttt{\url{https://github.com/wbombardellis/java-unterricht}}
}
\date{25 September 2019}

\makeatletter
\hypersetup{
	pdftitle = {\@title}, pdfkeywords = {Java}, pdfauthor = {\@author}
}
\makeatother

\begin{document}
	\begin{frame}
		\titlepage
	\end{frame}

	\begin{frame}
		\frametitle{Organization}
		\tableofcontents
	\end{frame}

	%-------------------
	% Review
	%-------------------
	\begin{frame}
		\frametitle{Algorithm Review}
		\url{https://www.youtube.com/watch?v=CvSOaYi89B4} (Until 3:12)
	\end{frame}

	%-------------------
	% Model of Computation
	%-------------------
	\section{Model of Computation}
	\begin{frame}
		\frametitle{Model of Computation}
		\centering
		\vspace{-4px}
		\includegraphics[width=.9\textwidth]{Memory-CPU-Scheme}
	\end{frame}

	%-------------------
	% Variables
	%-------------------
	\section{Variables in Java}

	\begin{frame}
		\frametitle{Variables in Java}
		$int\ a;$\\
		$a = 10;$\\
		$int\ b;$\\
		$b = 1;$\\
		$int\ c;$\\
		$c = a + b;$\\
		$System.out.println(c);$
	\end{frame}

	\begin{frame}
		\frametitle{Variables in Java with Input}
		$Scanner\ reader = new\ Scanner(System.in);$\\
		$int\ a =reader.nextInt();$\\
		$int\ b = 1;$\\
		$int\ c = a + b;$\\
		$System.out.println(c);$
	\end{frame}

	\begin{frame}
		\frametitle{Exercises}
		\begin{itemize}
			\item Write a program in Java that reads two integer values from the keyboard and prints their sum, difference, product and division at the screen/ console.
			\pause
			\item Write a program in Java that calculates and prints the area $A$ of a triangle after reading its base $b$ and height $h$ from the user's keyboard. Remember: $A = \frac{b \times h}{2}$
			\pause
			\item Write a program in Java that calculates and prints the area $A$ of a triangle with side lengths $a, b$ and $c$. For that you can use the Heron's formula $A = \sqrt{s \cdot (s-a) \cdot (s-b) \cdot (s-c)}$ where $s = \frac{a+b+c}{2}$ is the semi-perimeter of the triangle.
		\end{itemize}
	\end{frame}

	%-------------------
	% Summary
	%-------------------
	\section{Summary}

	\begin{frame}
		\frametitle{Summary}
		\centering
		\includegraphics[width=.7\textwidth]{Memory-CPU-Scheme}
		\begin{itemize}
			\item Next Week: Data Types
		\end{itemize}
	\end{frame}

	\begin{frame}
		\frametitle{References}
		\begin{itemize}
			\item W3C Tutorial:
			\begin{itemize}
				\item \url{https://www.w3schools.com/java/java\_variables.asp}
				\item \url{https://www.w3schools.com/java/java\_operators.asp}
				\item \url{https://www.w3schools.com/java/java\_math.asp}
			\end{itemize}
			\item Exercises: \url{https://www.w3schools.com/java/exercise.asp}
			\begin{itemize}
				\item Java Variables
				\item Java Operators
			\end{itemize}
		\end{itemize}
	\end{frame}

\end{document}


\documentclass[10pt,landscape]{article}
\usepackage{multicol}
\usepackage{calc}
\usepackage{ifthen}
\usepackage[a3paper,landscape]{geometry}
\usepackage{hyperref}
\usepackage{amssymb}
\usepackage{stmaryrd}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amsthm}

% COMANDI UTILI

\newcommand{\bumper}{\triangleright}
\newcommand{\impl}{\Rightarrow}
\newcommand{\conv}{\downarrow}
\newcommand{\dive}{\uparrow}
\newcommand{\Var}{\text{Var}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\V}{\mathcal{V}}
\renewcommand{\O}{\mathcal{O}}
\newcommand{\D}{\mathcal{D}}
\newcommand{\E}{\mathcal{E}}
\renewcommand{\P}{\mathcal{P}}
\newcommand{\NP}{\mathcal{NP}}
\newcommand{\LOGSPACE}{\mathrm{LOGSPACE}}
\newcommand{\SPACE}{\mathrm{SPACE}}
\newcommand{\PSPACE}{\mathrm{PSPACE}}
\newcommand{\NSPACE}{\mathrm{NSPACE}}
\newcommand{\NPSPACE}{\mathrm{NPSPACE}}
\newcommand{\EXP}{\mathrm{EXP}}
\newcommand{\YES}{\mathrm{YES}}
\newcommand{\NO}{\mathrm{NO}}
\newcommand{\TIME}{\mathrm{TIME}}
\newcommand{\PTIME}{\mathrm{PTIME}}
\newcommand{\NTIME}{\mathrm{NTIME}}

\newcommand{\EXPTIME}{\mathrm{EXPTIME}}

\DeclarePairedDelimiter\abs{\lvert}{\rvert}

\renewcommand{\H}{\mathcal{H}}

\newcommand{\undef}{\text{undefined}}
\newcommand{\red}[1]{\leqslant_{#1}}
%\newcommand{\C}{\mathbb{C}}
\renewcommand{\iff}{\Leftrightarrow}

\theoremstyle{plain}% default
\newtheorem*{thm}{Theorem}
\newtheorem*{lem}{Lemma}
\newtheorem*{prop}{Prop}
\newtheorem*{cor}{Corollary}
\theoremstyle{definition}
\newtheorem{defn}{Definizione}[section]
\newtheorem{conj}{Congettura}[section]
\newtheorem{exmp}{Esempio}[section]
\newtheorem{exrc}[exmp]{Esercizio}
\theoremstyle{remark}
\newtheorem*{comm}{Commento}
\newtheorem*{note}{Note}
\newtheorem{caso}{Caso}

% This sets page margins to .5 inch if using letter paper, and to 1cm
% if using A4 paper. (This probably isn't strictly necessary.)
% If using another size paper, use default 1cm margins.
\ifthenelse{\lengthtest { \paperwidth = 11in}}
	{ \geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} }
	{\ifthenelse{ \lengthtest{ \paperwidth = 297mm}}
		{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
		{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
	}

% Turn off header and footer
\pagestyle{empty}

% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
                                {-1ex plus -.5ex minus -.2ex}%
                                {0.5ex plus .2ex}%x
                                {\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
                                {-1explus -.5ex minus -.2ex}%
                                {0.5ex plus .2ex}%
                                {\normalfont\normalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
                                {-1ex plus -.5ex minus -.2ex}%
                                {1ex plus .2ex}%
                                {\normalfont\small\bfseries}}
\makeatother

% Define BibTeX command
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

% Don't print section numbers
\setcounter{secnumdepth}{0}

\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}

% -----------------------------------------------------------------------

\begin{document}

\raggedright
\footnotesize
\begin{multicols}{4}

% multicol parameters
% These lengths are set only within the two main columns
%\setlength{\columnseprule}{0.25pt}
\setlength{\premulticols}{1pt}
\setlength{\postmulticols}{1pt}
\setlength{\multicolsep}{1pt}
\setlength{\columnsep}{2pt}

\begin{center}
     \Large{\textbf{Complexity Cheat Sheet}} \\
     \small{Alessandro Cheli - 2020 - University of Pisa}
\end{center}

\section{Turing Machines}
\textbf{Deterministic} TMs are seen in the \textit{Computability Theory Cheat Sheet}.

\subsection{K-tapes Turing Machines}
Let $k \geq 1$ be the number of tapes. A k-tape TM is a quadruple $(Q, \Sigma, \delta, q_0)$.
Special symbols $\#, \bumper, \in \Sigma$, while $L,R,- \notin \Sigma$. Since we are
only considering decision problems, $\YES,\NO \notin Q$ are the halting states.
The transition function differs but is subject to the same restrictions as a classical TM.
\[\delta : Q \times \Sigma^k \to Q \cup \{\YES, \NO\} \times (\Sigma \times \{L, R, -\})^k \]

\subsection{IO Turing Machines}
Are useful to study space complexity.
Any k-tape TM $M = (Q, \Sigma, \delta, q_0)$ is of IO type $\iff \delta$ is subject
to the following constraints. Consider $\delta(q_1, \sigma_1, \hdots, \sigma_k) = (q', (\sigma_1', D_1), \hdots, (\sigma_k', D_k))$

\begin{tabular}{@{}ll@{}}
    $\sigma_1' = \sigma_1$ & First tape is read-only \\
    $D_k = R$ or $(D_k = -) \impl \sigma'_k = \sigma_k$ & Last tape is write-only \\
    $\sigma_1 = \# \impl D_1 \in \{L, -\}$ & Input tape is right-bounded
\end{tabular}

\subsection{Nondeterministic Turing Machines}
A \textbf{nondeterministic} TM is a quadruple $N = (Q, \Sigma, \Delta, q_0)$ where
$Q, \Sigma, q_0$ are the same as in other turing machines. They can be of IO and k-tape type.
The difference rilies in the fact that

\[ \Delta \subseteq (Q \times \Sigma) \times ((Q \cup \{\YES, \NO\}) \times \Sigma \times \{L,R, -\}) \]

Is not a transition function but a \textbf{transition relation}.
The same syntax as for other TMs is used for nondet. TMs configurations and computations.
A nondeterministic TM $N$ decides $I$ if and only if $x \in I \iff $ there exists \textbf{at least} a computation
such that $(q_0, \underline{\bumper}, x, \bumper, \hdots, \bumper) \to^*_N (\YES, w_1, \hdots, w_k)$

\begin{note}
    A single computation on a nondeterministic TMs is not a tree.
    Many computations together, can be arranged in a tree.
\end{note}

The \textbf{degree} $d$ of nondeterminism of a NDTM $N = (Q, \Sigma, \Delta, q_0)$ can now be defined as
$d = \max\{ \deg{(q,\sigma)} \mid q \in Q, \sigma \in \Sigma\}$, where
$\text{deg}(q, \sigma) = \#\{(q', \sigma', D) \mid ((q, \sigma), (q', \sigma', D)) \in \Delta\}$

\subsection{Computation Table}
A computation table $T_M$ is a square matrix where rows represent the configuration
at current computation step number (index $i$) of a polynomial time deterministic, 1-tape TM $M$ on an input $x$.
The column (index $j$) represent the $j$-th slot on tape.
Since $M$ decides $I$ in time $\O(\abs{x}^k)$, and space is limited by time, the matrix is
of size $\abs{x}^k$. To define it, the definition of the deterministic TM has to be slightly changed.
First we use $\Sigma' = \Sigma \cup \{\sigma_q \mid \sigma_q \in \Sigma \times (Q \cup \{\epsilon\})\}$ as the
alphabet, enriching symbols with a subscript containing the current state. These \textit{subscript} symbols
keep track of the head position and current state at time $i$.
A new machine $M'$ can be built from $M$, and this new construction does not take $M'$ out of $\P$.
Computation tables follow these rules:
\begin{tabular}{@{}ll@{}}
    $\abs{x} \geq 2$ & $\forall i . T(i,1) = \bumper $  \\
    $\forall j.\ 2 \leq j \leq \abs{x} + 1$ & $T(1,j)$ contains the $j-1$-th symbol of $x$ \\
    $\forall j.\ \abs{x} + 2 \leq j \leq \abs{x}^k$ & $T(1,j)$ contains $\#$ \\
    $\forall i . T(i,\abs{x}^k) = \# $ & Always use less space than time \\
    $\forall i \geq 2, j \geq 2.\ T(i,j)$ & depends only on $T(i-1,j-1),T(i-1,j)$\\
    & and $T(i-1,j+1)$.
\end{tabular} \\
Initial configuration starts from $\bumper \underline{\sigma_{q_0}}$ and not from $\underline{\bumper} \sigma_{q_0}$ \\
The head never positions on column 1 (on $\bumper$).\\
If $T(i,j) \in \{\sigma_{\YES},\sigma_{\NO}\}$, add an auxiliary state that moves
the tape head to $T(i, 2)$.
$M$ accepts $x \iff \exists i .\ T(i,2) = \sigma_{\YES} = T(\abs{x}^k, 2)$, therefore
all lines between that index $i$ and $\abs{x}^k$ are equal to line $i$.

%==================== TIME AND SPACE MEASURES =======================

\section{Time and Space Measures}

The size of a datum $x$, is a total (easily) computable function $\abs{x}$, which returns $n \in \N$, computed
from memory usage, number of components of the datum, or other criteria.
Given a TM $M$ (the same applies for IO and k-tape machines), $t$ is the time needed by a  to decide $x \in I$, given by

\[(q_0, \underline{\bumper} x) \to^t (H, w) \text{ with } H \in \{\YES, \NO\}\]

\subsection{Deterministic Time Measures}

A measuring function $f : \N \to \N$ is said to be \textbf{appropriate} $\iff$
it is strictly increasing and $\exists$ a TM $M$ that $\forall x . \ M(x) = \diamond^{f(\abs{x})}$,
in time $\O(f(\abs{x}) + \abs{x})$ and space $\O(f(\abs{x}))$.

$M$ decides $I$ in \textit{deterministic time} $f$ if $\forall x$,
the time $t$ required to compute $M(x)$ is $\leq f(\abs{x})$.
A \textbf{Complexity Class} can now be defined:

\[\TIME(f) = \{I \mid \exists M \text{ deciding } I \text{ in deterministic time } f\}\]

Asymptotic notation is commonly used to denote time complexity and to simplify.
Constants are commonly ignored (treated as $r$)

\[\O(f) = \{g \mid \exists r \in \R^+ . \ g(n) < rf(n) \text{ almost everywhere}\}\]

\subsection{Nondeterministic Time Measures}

A nondet. TM $N$ decides $I$ in \textbf{nondeterministic time} $f(n) \iff N$ decides $I$ and
$\forall x \in I . \ \exists t $ such that $N$ \textit{halts and accepts} $x$ in $t$ steps,
with $t \leq f(\abs{x})$. Because of nondeterminism, there can be many computations that
satisfy (or don't) those requirements. We consider only the shortest accepting computation.

\[\NTIME(f) = \{I \mid \exists N \text{ deciding } I \text{ in non deterministic time } f\}\]

\subsection{Deterministic and Nondeterministic Space Measures}

To measure space, we would have to edit the definition of TMs,
by adding an additional \textit{right bumper} $\triangleleft$ that
denotes the end of the tape. This way ($k$-tape and IO) TMs
will remember \textit{how many slots} were visited.
$\forall$ computation halting in a state $(H, w_1, \hdots, w_n)$ with $H \in \{\YES, \NO\}$,
the space required by a $k$-tape IO TM is $\sum_{i=2}^{k-1}\abs{w_i}$.
$M$ decides $I$ in deterministic space $f(n)$ if $\forall x $ the spaced required
to compute $M(x)$ is $\leq f(\abs{x})$, and it follows that $I \in \SPACE(f(n))$.
The same idea can be applied to nondeterministic machines, considering the additional
restraint on accepting, to define $\NSPACE$.

\section{Complexity Classes}

\begin{equation*}
    \begin{aligned}
            \begin{matrix}
            \P = \bigcup_{k\geq1} \TIME(n^k) &\quad& \NP = \bigcup_{k\geq1} \NTIME(n^k) \\
            &&\\
            \PSPACE = \bigcup_{k\geq1} \SPACE(n^k) &\quad& \NPSPACE = \bigcup_{k\geq1} \NSPACE(n^k)
            \end{matrix} \\
            \begin{matrix}
                \EXP = \bigcup_{k\geq1} \TIME(2^{n^k}) &
                \LOGSPACE = \bigcup_{k\geq1} \SPACE(k \times \log(n))
            \end{matrix}
    \end{aligned}
\end{equation*}

$\P, \PSPACE, \LOGSPACE$ are closed with respect to model transformations and are therefore robust classes of problems.
$\NP$ can also be defined as the set of problems that allow \textit{verification in polynomial time}

\subsection{Hierarchy}

\[ \LOGSPACE \subseteq \P \subseteq \NP \subseteq \PSPACE = \NPSPACE \subset \EXP \subset R \subset RE \]

Since $\LOGSPACE \subsetneq \PSPACE$, at least one of those inclusions is strict:
\[ \LOGSPACE \subseteq \P, \P \subseteq \NP, \NP \subseteq \PSPACE \]

It is not yet know which one of those is a strict inclusion. \\[.2cm]

\textbf{Theorem. } $\LOGSPACE \subseteq \P$. \textit{Proof.}
Let $I \in \LOGSPACE$. There $\exists$ a TM that computes $x \in I$
in $\O(\log \abs{x})$ space. $M$ can range through $\O(\abs{x} \log\abs{x} \#Q \#\Sigma^{\log\abs{x}})$
configurations. A halting computation cannot cycle on configurations. So M computes in $\O(\abs{x}^k)$ for some $k$.
\\[.2cm]

\textbf{Theorem. (Savitch)} $\NPSPACE = \PSPACE$.
\\[.2cm]

\textbf{Theorem. Hierarchy of time and space}.
If $f$ is appropriate, then $\TIME(f(n)) \subsetneq \TIME((f(2n+1))^3)$, and
$\SPACE(f(n)) \subsetneq \SPACE(f(n) \times \log f(n))$.
\textbf{Corollary. } $\P \subsetneq \EXP$\\
\textit{Proof.}
$\P \subset \EXP$ is obvious because $2^n$ grows faster than any polynomial. It is
strict because $\P \subseteq \TIME(2^n) \subsetneq \TIME((2^{(2n+1)})^3) \subseteq \TIME(2^{n^2}) $.
This corollary, together with the fact that $\NTIME(f(n)) \subseteq \TIME(c^{f(n)})$ lets us
conclude that $\NP \subseteq \EXP$.
\\[.2cm]

\textbf{Theorem. Hierarchy 2}. Let $f$ be an appropriate measuring function, and $k$ a constant. Then
    \begin{tabular}{@{}ll@{}}
        $\SPACE(f(n)) \subseteq \NSPACE(f(n))$  & $\TIME(f(n)) \subseteq \NTIME(f(n))$ \\
        $\NSPACE(f(n)) \subseteq \TIME(k^{\log n + f(n)})$ & \\
    \end{tabular}
\\[.2cm]

\textbf{Theorem. Arbitrarily Hard Problems}: $\forall g $ total computable func.
    $\exists I \in \TIME(f(n))$
    and $I \notin \TIME(g(n))$, with $f(n) > g(n)$ almost everywhere.

Using arbitrary measuring functions entails bizarre consequences:
\\[.2cm]

\textbf{Theorem. Blum speedup} $\forall h$ total computable func. $\exists$ a problem $I$
such that $\forall M$ algorithm deciding $I$ in time $f$, $\exists M'$ deciding
$I$ in time $f'$ such that $f(n) > h(f'(n))$ almost everywhere. This theorem guarantees
that there are problems that have no optimal algorithm. Although
given an $h$, the problems built for this theorem are artificial and not useful,
and we do not know how to construct them. \\[.2cm]

\textbf{Theorem. Borodin's Gap} There exists $f$ total computable such that
$\TIME(f(n)) = \TIME(2^{f(n)})$ \\[.2cm]

These last two theorems are also valid for \textit{space measures.}

%===================== MACHINES THEOREMS ======================

\section{Theorems on Complexity of Turing Machines}

\begin{thm} \textbf{Reduction of tapes}
    Let $M$ be a $k$-tape TM, deciding $I$ in deterministic time $f$,
    then $\exists$ a 1 tape TM $M'$ that decides $I$ in time $\O(f^2)$

    \begin{proof} (Only a draft):
        Build a 1 tape TM $M'$ by introducing two symbols $\bumper'$ and
        $\triangleleft'$ to denote the start and end of the $k$-th tape.
        Introduce $\#\Sigma$ new symbols $\overline{\sigma_i}$ to remember
        each tape's head location.
        To generate the initial configuration $(q, \bumper \bumper' x \triangleleft' (\bumper' \triangleleft' )^k )$,
        $2k + \#\Sigma$ states and $\O(\abs{k})$ steps are needed.
        To simulate a move of $M$, $M'$ iterates the input datum from left to right, and back,
        2 times: find the marked $\overline{\sigma_i}$ symbols, the second time write the new
        symbols. If a tape has to be extended, the $\triangleleft'$ parens have to be moved
        and a cascade happens. This takes $\O(f(\abs{x}))$ time, for each move of $M$. Since
        $M$ takes $\O(f(\abs{x}))$ time to compute an answer, $M'$ will take $\O(f(\abs{x})^2)$
    \end{proof}
\end{thm}

\begin{cor}
    Parallel machines are polinomially faster than sequential machines.
\end{cor}

\framebox{
    \parbox[t][]{5.8cm}{
    A machine cannot use more space than time
    }
} \\[.2cm]

\textbf{Theorem. Linear speedup}
If $I \in \TIME(f)$, then $\forall \epsilon \in \R^+  . \ I \in \TIME(\epsilon \times f(n) + n + 2)$.
\textit{Proof.}
        (Draft): Build $M'$ from $M$ solving $I$, compacting $m$ symbols of $M$ into 1 of $M'$, in function of $\epsilon$.
        The states of $M'$ will be triples $[q,\sigma_{i_1},\hdots,\sigma_{i_m},k]$ meaning the TM is in state $q$
        and has cursor on $k$-th symbol of $\sigma_{i_1},\hdots,\sigma_{i_m}$. $M'$ then needs 6 steps to simulate
        $m$ steps of $M$. Therefore, $M'$ will take $\abs{x} + 2 + 6 \times \lceil \frac{f(\abs{x})}{m}\rceil$.
        Then $m = \lceil \frac{6}{\epsilon} \rceil$.
        \qed \\[.2cm]

Same principle can apply to $\SPACE$ with \textbf{linear space compression}.
If $I \in \SPACE(f(n))$, then $\forall \epsilon \in \R^+  . \ I \in \SPACE(\epsilon \times f(n) + 2)$

\begin{thm}
    For each k-tape TM $M$ that decides I in deterministic time $f$ there exists
    an IO TM $M'$ with k+2 tapes that computes I in time $c \times f$ for some constant $c$.

    \begin{proof}
        $M'$ copies the first $M$ tape to the second tape, in $\abs{x} + 1$ steps.
        Then, $M'$ operates identically to $M$. If and when $M$ halts,
        $M'$ copies the result to the tape $k+2$, in at most $f(\abs{x})$ steps.
        $M'$ computation was $2f(\abs{x}) + \abs{x} + 1$ steps long.
    \end{proof}
\end{thm}

\begin{thm} \textbf{Exponential loss in determinization, or bruteforce}
    If $I \in NTIME(f(n))$ and is computed by $k$-tape $N$, it can also be computed by a deterministic
    TM $M$ with $k+1$ tapes in
    time $\O(c^{f(n)})$ with
    an exponential loss of performance. In short, $\NTIME(f(n)) \subseteq \TIME(c^{f(n)})$

    \begin{proof}
        Let $d$ be the degree of nondeterminism of $N$.
        $\forall q \in Q, \sigma \in \Sigma$ sort $\Delta(q,\sigma)$ lexicographically.
        Every computation of length $t$ carried by $N$ is a sequence of choices that can
        be seen as a sequence of natural numbers $(c_1, \hdots, c_t)$ with $c_i \in [0..d-1]$.
        The det. TM $M$ considers these successions in an increasing order, visiting
        the tree of nondeterministic computations, one at a time.
        Therefore $M(x)\conv \ \iff N(x)\conv$, also, all the possible simulations
        can be $\O(d^{f(n) + 1})$.
    \end{proof}
\end{thm}

\section{Problems in P and NP}
A problem $I$ \textit{reduces efficiently} to $I'$ ($I \red{\text{logspace}} I'$) if
$\exists f \in \LOGSPACE$ such that $x \in I \iff f(x) \in I'$.
Let $\D, \E \in \{\P, \NP, \EXP, \PSPACE, \NPSPACE\}$ and $\D \subseteq \E$, then
$\red{\text{logspace}}$ classifies $\{\LOGSPACE\}$ and $\E$.
Also, $\red{\text{logspace}}$ and $\red{\P}$ classify $\D$ and $\E$. (See the
\textit{computability theory cheat sheet}).

\begin{note}
    A TM operating in logarithmic space, composed to another $\LOGSPACE$ machine is still in $\LOGSPACE$.
\end{note}

\subsection{NP complete problems}

\textbf{Traveling Salesman Problem}
Let $G$ be a directed weighted graph with $n$ vertices, all connected to each other.
Let $d(i,j)$ be the cost function for (weight) on edge $(i,j)$. The problem consists in finding
the \textit{hamiltonian cycle} (permutation of nodes) with the minimum cost: $\sum_{1 \leq i \leq n-1} d(i,i+1)$.
To see this as a decision problem we have to check if the total cost of the path is $\leq$ of a treshold $B$.
A \textit{deterministic} TM that bruteforces the problem first builds \textit{all permutations} of $[1..n]$
in $\frac{(n-1)!}{2}$ steps, and then searches for the first permutation with cost $\leq B$
with costs $\O(n^3)$. A \textit{nondeterministic} TM solves the problem in $\O(n^3)$
by first generating all sequences of numbers $[1..n]$ of length $n$ \textit{nondeterministically},
and then polynomially verifying that one of the sequence is: a path (in $\O(n^2)$) and
costs less than $B$ (in $\O(n^3)$).  \\[.2cm]

\textbf{SAT or Satisfiability Problem}:
Given a boolean expression $B$, the problem consists in deciding if there $\exists$ a
boolean assignment $\V \vDash B$. We consider only boolean expressions given in conjunctive
normal form ($(a \lor b) \land (c \lor d \lor e) \hdots$), which is guaranteed to exist for
any boolean expression. \\[.2cm]

\textbf{Theorem. (Cook) SAT is $\NP$ complete}. \textit{Proof.}
Since CIRCUIT SAT $\red{\text{logspace}}$ SAT, proving that $\text{CIRCUIT SAT}$ is $\NP$-complete
is enough. Then, $\forall I \in \NP, I \red{\text{logspace}}$ CIRCUIT SAT.
Let $I \in \NP$, solved by $N$ in time $n^k$, we build $f \in \LOGSPACE$ such that $x \in I \iff f(x) $ is a satisfiable boolean formula.
We assume that $N$ has degree of nondeterminism $d=2$ (if not, an equivalent machine can be built), so we can encode
a succession of choices of length $\leq \abs{x}^k$, as a sequence of binary bits $B$. Only if $B$ is fixed,
we can build the \textit{computation table} of $(N(x),B)$. As seen in the proof of $\P$-completeness
of CIRCUIT VALUE, one can build a boolean circuit $C$ for each cell, but it also depends on $b_{i-1}$ since
$\Delta_N$ is not a function and $d=2$. Analogously, a circuit for the complete computation of
$C_N$, can be built with $(\abs{x}^k-1)(\abs{x}^k-1)$ copies of $C$, but each copy will have $3m+1$ inputs.
\qed. \\[.2cm]

\textbf{CLIQUE Problem}: Determine if a undirected graph $G = (V, E)$ has a clique
of degree $k$.
\textbf{Theorem: SAT $\red{\text{logspace}}$ CLIQUE}. \textit{Proof.}
Given a bool. expr. $B$ in CNF $\bigwedge_{1 \leq k \leq n} C_k$, build $f(B) = (V, E)$ such that
$V$ is the set of literal occurences in $B$ and $E = \{(i,j) \mid i \in C_k \impl (j \notin C_k \land i \neq \neg j)\}$.
$(\V \vDash B) \iff f(B)$ has a clique of degree $k$. $f \in \LOGSPACE$ since 2 counters are needed.
\qed \\[.2cm]

\textbf{HAM or Hamiltonian Path}:
The problem consists in deciding if a direct graph there exists a path,
called \textit{hamiltonian}, that goes through every vertex a single time.
(A variant called \textit{hamiltonian cycle} requires the path to go back to the
starting node). \\[.2cm]

\textbf{Theorem: HAM $\red{\text{logspace}}$ SAT}. \textit{Proof.}
Given the direct graph $G$, we construct $f \in \LOGSPACE$ such that
$f(G)$ is a boolean formula in conjunctive normal form
 that is satisfiable $\iff G$ has an hamiltonian path.
If $G$ has $n$ vertices, $f(G)$ has $n^2$ variables, written as
$x_{i,j}$ with $1 \leq i,j \leq n$, which represents if the $i$-th element
of the path is the $j$-th vertex in the graph. $f(G)$ will be the conjuction of these clauses:
\begin{tabular}{@{}ll@{}}
$(\neg x_{i,j} \lor \neg x_{k,j})\  i \neq k$ & Same node appears once in the path\\
$(\neg x_{i,j} \lor \neg x_{i,k})\  j \neq k$ & Two nodes cannot be the $i$-th \\
$(x_{i,1} \lor \hdots \lor \neg x_{i,n})\  1 \leq i \leq n$ & Some node is the $i$-th \\
$(x_{1,j} \lor \hdots \lor \neg x_{n,j})\  1 \leq j \leq n$ & Every node is in path \\
$(\neg x_{k,i} \lor \neg x_{k+1, j})$ & $\forall k.\ 1 \leq k \leq n-1$ and $\forall(i,j) \notin A$\\
& If $(i,j)$ is not an edge of $G$, it must not \\
& appear in the path.
\end{tabular}
It is then immediate to see that $(\V \vDash f(G)) \iff G$ has an hamiltonian path,
and that $\V$ represents a permutation of nodes of $G$. To verify that $f \in \LOGSPACE$,
we build a $k$-tape IO TM computing $f$ that writes on the output tape the first 4 clauses.
To do so, $\Sigma = \{tt,ff, \lor, \land, \neg, (,), 0, 1\}$ and it needs a work tape containing $n$, and 3 work tapes containing a binary representation
of the 3 variables (counters) seen in the clauses: $i,j,k$. This requires $\O(\log n)$ bits on work
tapes. \qed \\[.2cm]

\textbf{CIRCUIT SAT Problem}:
Let $C$ a \textit{boolean circuit}, or a direct acyclic graph $(V,E)$ where the $n$ vertices are
called ports and edges are sorted pairs. Ports have 0,1 or 2 inputs, and are of sort
$s(i) \in \{tt,ff,\neg,lor,\land\} \cup X$ where $X$ is the set of variables. Circuit
input ports are only variables or truth values and have no inputs. The circuit output
is by convention the last port $n$. A denotational semantic $\llbracket i \rrbracket_{\V}$
given a boolean assignment is straightforward to define.
The \textit{CIRCUIT SAT} problem consists in deciding if an assignment $\V$ exists such that
$\V(C) = tt$. A nondeterministic TM solving the problem will first generate all possible assignments
at the same time and then verifies \textit{polinomially} that an assigment satisfies $C$ by
letting the truth values \textit{flow} through the circuit. \\[.2cm]

\textbf{Theorem. CIRCUIT SAT $\red{\text{logspace}}$ SAT}: \textit{Proof. }
Given $C = (V, E)$, with variables in $X$, build $f \in \LOGSPACE$ such that
$\exists \V .\ \llbracket C \rrbracket_{\V} = tt \iff \exists V' .\ \forall x \in X .\ V'(x) = V(x) \land (V' \vDash f(C))$.
For each port $g$ in $C$ build the conjuncts of $f(C)$ as follows:
\begin{tabular}{@{}ll@{}}
    $g$ & $g$ is the output port in $C$ \\
    $(\neg g \lor x) \land (g \lor \neg x)$ & if $s(g) = x \in X$, $g \iff x$ \\
    $(\neg g \lor \neg h) \land (g \lor x)$ &  if $s(g) = \neg$, $(h,g) \in E$, then $g \iff \neg h$ \\
    $(\neg h \lor  g) \land (\neg k \lor g) \land (h \lor k \lor \neg g)$ &  if $s(g) = \lor$ and $(h,g), (k,g) \in E$, \\
    & then $g \iff (h \lor k)$ \\
    $(\neg g \lor  h) \land (\neg g \lor k) \land (\neg h \lor \neg k \lor g)$ &  if $s(g) = \land$ and $(h,g), (k,g) \in E$, \\
    & then $g \iff (h \land k)$
\end{tabular}
See the reduction from HAM to SAT to verify that this reduction is logarithmic in space. \\[.2cm]

\textbf{Corollary. CIRCUIT VALUE $\red{\text{logspace}}$ SAT, CLIQUE}

\subsection{P Complete Problems}

\textbf{Theorem. CIRCUIT Value is $\red{\text{logspace}}$-complete for $\P$}:
CIRCUIT Value problems are specific cases of CIRCUIT SAT problems where the input
ports are only truth values $s(i) \in \{tt, ff\}$ and \textit{can not be variables.}

\textit{Proof.} For an arbitrary $I \in \P$, build the computation table for the machine $M$ deciding it.
Encode each $\rho \in \Sigma'$ as a string of binary bits of length $m = \log_2(\#\Sigma')$.
With this representation the computation table will be rectangular, with width $m \times \abs{x}^k$.
Since the transition function $\delta_M$ is fixed, as in the computation table we can build
the boolean representation $S_{i,j}$ of $T_{i,j}$ that depends only on the values in the previous line $i-1$ and columns
corresponding to T's $j-1,j,j+1$.
We can then build a boolean circuit $C$, with $3m$ inputs that computes the transition function and
outputs $m$ bits corresponding to $S_{i,j}$. To build $f$, transform $T$ into a circuit $C_I$ that
is composed by copies of $C$ for each cell $i,j$. $(\abs{x}^k - 1)(\abs{x}^k - 2)$ copies are enough because
the first and last columns are fixed, and the first row contains the input on tape.
By simple induction it is provable than $C_{i,j}$ has $S_{i,j}$ as output $\iff T(i,j) = \rho$, that is
encoded as $S_{i,j}$. Since we need a single copy of $C$ for each index in the table $i,j$, $f$ can be defined
in logarithmic space because a TM computing it only needs these index counters on work tapes, stored in
base 2. \qed \\[.2cm]

\textbf{Theorem. CIRCUIT VALUE $\red{\text{logspace}}$ CIRCUIT SAT}: \textit{Proof.} Obvious generalization.

\end{multicols}
\end{document}


\documentclass[12pt]{article}
\usepackage[top=1in,bottom=1in,left=0.75in,right=0.75in,centering]{geometry}
\usepackage{fancyhdr}
\usepackage{epsfig}
\usepackage[pdfborder={0 0 0}]{hyperref}
\usepackage{palatino}
\usepackage{wrapfig}
\usepackage{lastpage}
\usepackage{color}
\usepackage{ifthen}
\usepackage[table]{xcolor}
\usepackage{graphicx,type1cm,eso-pic,color}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{wasysym}

\def\course{CS 4102: Algorithms}
\def\homework{Divide and Conquer / Sorting Basic: Recurrence Relations}
\def\semester{Spring 2021}

\newboolean{solution}
\setboolean{solution}{false}

% add watermark if it's a solution exam
% see http://jeanmartina.blogspot.com/2008/07/latex-goodie-how-to-watermark-things-in.html
\makeatletter
\AddToShipoutPicture{%
\setlength{\@tempdimb}{.5\paperwidth}%
\setlength{\@tempdimc}{.5\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%
\ifthenelse{\boolean{solution}}{
\makebox(0,0){\rotatebox{45}{\textcolor[gray]{0.95}%
{\fontsize{5cm}{3cm}\selectfont{\textsf{Solution}}}}}%
}{}
}}
\makeatother

\pagestyle{fancy}

\fancyhf{}
\lhead{\course}
\chead{Page \thepage\ of \pageref{LastPage}}
\rhead{\semester}
%\cfoot{\Large (the bubble footer is automatically inserted into this space)}

\setlength{\headheight}{14.5pt}

\newenvironment{itemlist}{
\begin{itemize}
\setlength{\itemsep}{0pt}
\setlength{\parskip}{0pt}}
{\end{itemize}}

\newenvironment{numlist}{
\begin{enumerate}
\setlength{\itemsep}{0pt}
\setlength{\parskip}{0pt}}
{\end{enumerate}}

\newcounter{pagenum}
\setcounter{pagenum}{1}
\newcommand{\pageheader}[1]{
\clearpage\vspace*{-0.4in}\noindent{\large\bf{Page \arabic{pagenum}: {#1}}}
\addtocounter{pagenum}{1}
\cfoot{}
}

\newcounter{quesnum}
\setcounter{quesnum}{1}
\newcommand{\question}[2][??]{
\begin{list}{\labelitemi}{\leftmargin=2em}
\item [\arabic{quesnum}.] {} {#2}
\end{list}
\addtocounter{quesnum}{1}
}

\definecolor{red}{rgb}{1.0,0.0,0.0}
\newcommand{\answer}[2][??]{
\ifthenelse{\boolean{solution}}{
\color{red} #2 \color{black}}
{\vspace*{#1}}
}

\definecolor{blue}{rgb}{0.0,0.0,1.0}

\begin{document}

\section*{\homework}

\question[3]{
You are a hacker, trying to gain information on a secret array of size $n$. This array contains $n-1$ ones and exactly $1$ two; you want to determine the index of the two in the array.\\
\\
Unfortunately, you don't have access to the array directly; instead, you have access to a function $f(l1, l2)$ that compares the sum of the elements of the secret array whose indices are in $l1$ to those in $l2$. This function returns $-1$ if the $l1$ sum is smaller, $0$ if they are equal, and $1$ if the sum corresponding to $l2$ is smaller.\\
\\
For example, if the array is $a=[1,1,1,2,1,1]$ and you call $f([1,3,5],[2,4,6])$ then the return value is $1$ because $a[1]+a[3]+a[5]=3<4=a[2]+a[4]+a[6]$. Design an algorithm to find the index of the $2$ in the array using the least number of calls to $f()$. Suppose you discover that $f()$ runs in $\Theta(max(|l1|,|l2|))$, what is the overall runtime of your algorithm?
}

\vspace{12pt}

\question[3]{
In class, we looked at the \emph{Quicksort algorithm}. Consider the \textbf{worst-case scenario} for quick-sort in which the worst possible pivot is chosen (the smallest or largest value in the array). Answer the following questions:

\begin{itemize}
\item What is the probability of choosing one of the two worst pivots out of $n$ items in the list?
\item Extend your formula. What is the probability of choosing the one of the worst possible pivots \emph{for EVERY recursive call} until reaching the base case. In other words, what is the probability quicksort fully sorts the list while choosing the worst pivot choice every time it attempts to do so?
\item What is the limit of your formula above as the size of the list grows. Is the chance of getting Quicksort's worst-case improving, staying constant, or converging on some other value.
\item Present one sentence on what this means. What are the chances that we actually get Quicksort's worst-case behavior?
\end{itemize}
}

\vspace{12pt}

%----------------------------------------------------------------------
\noindent Directly solve, by unrolling the recurrence, the following relation to find its exact solution.

\question[2]{
$T(n) = T(n-1) + n$
}

\vspace{12pt}

%----------------------------------------------------------------------

\noindent Use induction to show bounds on the following recurrence relations.

\question[2]{
Show that $T(n)=2T(\sqrt{n})+log(n) \in O(log(n)*log(log(n)))$. \emph{Hint: Try creating a new variable m and substituting the equation for m to make it look like a common recurrence we've seen before. Then solve the easier recurrence and substitute n back in for m at the end.}
}

\answer[0 in]{
...
}

\question[2]{
Show that $T(n)=4T(\frac{n}{3})+n \in \Theta(n^{log_3(4)})$. You'll need to subtract off a lower-order term to make the induction work here. \emph{Note: we are using big-theta here, so you'll need to prove the upper AND lower bound.}
}

\answer[0 in]{
...
}

\vspace{12pt}

%----------------------------------------------------------------------

\noindent Use the master theorem (or main recurrence theorem if applicable) to solve the following recurrence relations. State which case of the theorem you are using and why.

\question[2]{
$T(n)=2T(\frac{n}{4})+1$
}

\answer[0 in]{
...
}

\question[2]{
$T(n)=2T(\frac{n}{4})+\sqrt{n}$
}

\answer[0 in]{
...
}

\question[2]{
$T(n)=2T(\frac{n}{4})+n$
}

\answer[0 in]{
...
}

\question[2]{
$T(n)=2T(\frac{n}{4})+n^2$
}

\answer[0 in]{
...
}
\end{document}


\section{Language}
\label{sec-lang}
\label{sec-decl}

A {\GF} language is a first-order language.
Its terms, atomic formulae, and
well-formed formulae are built from primitive symbols representing
variables, constants, functions, predicates,
connectives and quantifiers. The various syntactic categories of expressions,
corresponding to the different logical categories, are
recursively defined in terms of each other,
these definitions determining the written syntax of the
language\footnote{By written syntax we mean the syntax of the
expressions as they are typed rather than as they are represented
internally.}.

Each primitive symbol has a {\bf syntype}
which corresponds to its
logical status. Symbols of certain {\bf syntype}s
must also have other associated
information (such as their arity) which together with the {\bf syntype}
determines
how the symbol is to be used in the construction of compound expressions.

As every {\GF} language is a first-order language there are certain
logical symbols (such as connectives and quantifiers)
which are predefined. The other primitive
symbols of a language must be user defined by means of declarations
(see the command {\bf declare}) which
specify a symbol together with its {\bf syntype}
(and any other information
required).

\subsection{{\GF} symbols}

{\GF} commands accept symbols that we will identify with
{\em sym}. The {\GF} scanner recognizes different {\em types} of
characters: {\em identifiers}, {\em numbers} (positive integers),
{\em escape characters},
{\em delimiters} and special delimiters that
may be regarded as tokens: {\em identifier-delimiters}.
{\GF} symbols {\em sym}s are sequences of {\em identifiers}
and {\em numbers}. Identifiers are for instance:
\begin{verbatim}
                       a b c d
                       A B C D
                       ! " # $ % &
\end{verbatim}    %%% $ (fake emacs' hilite mode)
{\em delimiters} are used to separate symbols.
Some delimiters are:
\begin{verbatim}
                       ( ) , . : ; [ ]
\end{verbatim}
{\em identifier-delimiters} may be used to separate
symbols or may identify a particular token, as for instance
\begin{verbatim}
                       < > = + - * / ? @
\end{verbatim}
Escape characters turn a character into an identifier.
The character $\backslash$ is {\GF}'s escape character.

Types are assigned to characters in the file {\tt asciitab.fol}
in the {\tt fol} directory.

An example:
\begin{verbatim}
***** declare sentconst a!b;
a!b has been declared to be a Sentconst
***** declare sentconst a_b;
a_b has been declared to be a Sentconst
***** declare sentconst a-b;
a has been declared to be a Sentconst
- has been declared to be a Sentconst
b has been declared to be a Sentconst
***** declare sentconst c/d;
c has been declared to be a Sentconst
/ has been declared to be a Sentconst
d has been declared to be a Sentconst

***** declare sentconst e\f;
ef has been declared to be a Sentconst
\end{verbatim}

\subsection{{\GF} special symbols}

The following strings are treated as special symbols:

\begin{itemize}
\item The logical connectives:
{\tt not, and, or, imp, iff, wffif, trmif, forall, exists};
\item The sentential constants for truth and falsity:
{\tt FALSE, TRUE};
\item The equality predicate symbol: {\tt =};
\item Sorts: {\tt UNIVERSAL}, {\tt NATNUMSORT};
\item Numerals: {\tt 1}, {\tt 2}, {\tt 3}, ...;
\item Representations: {\tt UNIVERSALREP}, {\tt NATNUMREP};
\item Natural numbers: {\tt 1}, {\tt 2} {\tt 3}, ...;
\item Various: {\tt TRUTHSORT}, {\tt TRUTHREP}, {\tt NOTNAMED\&}, {\tt UNDEF\&};
\end{itemize}

Using these symbols not in ``reading mode",
that is modifying their meaning,
is very dangerous. The behavior
of the system becomes unpredictable.

\subsection{Syntypes of primitive symbols}

The different {\bf syntype}s of primitive symbols are:

\gap
\begin{center}
\fbox{
\parbox{15cm}{
\begin{itemize}

\item {\indvar} --- individual variables.

\item {\indpar} --- individual parameters.

\item {\indconst} --- individual constants.

\item {\funpar} --- function parameters.

\item {\funconst} --- function constants.

\item {\predpar} --- predicate parameters.

\item {\predconst} --- predicate constants.

\item {\sentpar} --- sentential parameters.

\item {\sentconst} --- sentential constants.

\item {\sentconn} --- sentential connectives.

\item {\quant} --- quantifiers.

\end{itemize}
}}
\end{center}

\gap
Some of the above {\bf syntype}s are grouped together into hybrid syntactic
categories according to the role they play
in defining logical expressions.

\gap
\begin{center}
\fbox{
\parbox{15cm}{
{\indsym} ::= {\indvar} \I {\indpar} \I {\indconst}

{\funsym} ::= {\funpar} \I {\funconst}

{\predsym} ::= {\predpar} \I {\predconst}

{\sentsym} ::= {\sentpar} \I {\sentconst}
}}
\end{center}

\subsection{Logical expressions}

The various categories of compound logical expressions are defined
recursively in terms of each other and in terms of the categories of
primitive symbols. The most important categories, from a logical point of
view, are: {\wff} (the category of well-formed formulae), {\awff} (the
category of atomic formulae) and {\term} (the category of terms).

The following definition gives the
grammar of expressions accepted by {\GF}. In all cases $n$
is a natural number $\geq 1$.
Superscripts of
---$^n$, ---$^P$, ---$^I$ are used with certain categories,
the categories of operator symbols, to specify other properties which the
permitted symbols must possess; the three superscripts require
respectively: that the
symbol has arity $n$, that the symbol is a unary prefix operator, that the
symbol is a binary infix operator. Operator symbols and their associated
properties are treated in full in section \ref{opsym}.

\gap
\begin{center}
\fbox{
\parbox{15cm}{
{\wff} ::= {\bf (} {\wff} {\bf )} \I {\connappl} \I {\quantwff}
\I {\wffif} \I {\awff}

{\connappl} ::= {\sentconn}$^{P}$ {\wff} \I {\wff}$_1$
{\sentconn}$^{I}$ {\wff}$_2$

{\quantwff} ::= {\quantprefix} {\wff}

{\quantprefix} ::= {\quant} {\indvar}$_1$ ... {\indvar}$_n$ {\bf .}

{\wffif} ::= {\bf wffif} {\wff}$_1$ {\bf then} {\wff}$_2$ {\bf else}
{\wff}$_3$

{\awff} ::= {\sentsym} \I {\predappl}

{\predappl} ::= {\predsym}$^{P}$ {\term} \I
                {\term}$_1$ {\predsym}$^{I}$ {\term}$_2$ \I
                {\predsym}$^n${\bf (} {\term}$_1$ [{\bf ,}] ...
                [{\bf ,}] {\term}$_n$ {\bf )}

{\term} ::= {\bf (} {\term} {\bf )} \I {\funappl} \I {\termif} \I {\indsym}

{\termif} ::= {\bf trmif} {\wff} {\bf then} {\term}$_1$ {\bf else}
{\term}$_2$

{\funappl} ::= {\funsym}$^{P}$ {\term} \I
               {\term}$_1$ {\funsym}$^{I}$ {\term}$_2$ \I
               {\funsym}$^n${\bf (} {\term}$_1$ [{\bf ,}] ...
               [{\bf ,}] {\term}$_n$ {\bf )}
}}
\end{center}

\subsection{Operator symbols}
\label{opsym}

The primitive symbols of the categories  {\funsym},
\predsym, {\sentconn} are operator symbols; each operator
symbol has an associated arity which is a natural number $\geq 1$.
\funsym$^n$, \predsym$^n$, and \sentconn$^n$ are used to denote the
subclasses of {\funsym}, {\predsym} and {\sentconn} of operators with
arity $n$.

An operator symbol with arity $1$ must be defined to be prefix. \funsym$^P$,
\predsym$^P$, and \sentconn$^P$ are used to denote the categories of prefix
operators, these are subclasses of \funsym$^1$, \predsym$^1$, and
\sentconn$^1$ respectively.

An operator symbol with arity $2$ may be defined to be infix. \funsym$^I$,
\predsym$^I$, and \sentconn$^I$  are used to denote the categories of infix
operators, these will be
subclasses of \funsym$^2$, \predsym$^2$, and \sentconn$^2$ respectively.

The use of prefix and infix operators allows the following ambiguous
expressions:

\begin{enumerate}

\item {\term}$_1$ {\funsym}$^I_1$ {\term}$_2$ {\funsym}$^I_2$ {\term}$_3$

\item {\wff}$_1$ {\sentconn}$^I_1$ {\wff}$_2$ {\sentconn}$^I_2$ {\wff}$_3$

\item {\funsym}$^P$ {\term}$_1$ {\funsym}$^I$ {\term}$_2$

\item {\sentconn}$^P$ {\wff}$_1$ {\sentconn}$^I$ {\wff}$_2$

\item {\quantprefix} {\wff}$_1$ {\sentconn}$^I$ {\wff}$_2$~\footnote{This is
a special case as a {\quantprefix} behaves like a prefix operator.}

\end{enumerate}

These ambiguities are avoided through the assignment of additional
information to certain operator symbols declared to be prefix or infix.
Each symbol in \funsym$^P$ and \sentconn$^P$ must
have an associated prefix binding priority (a natural number
$\geq 1$)\footnote{In fact each prefix and infix {\predsym} also has
associated binding priorities but this information is redundant as
ambiguities can not arise.}, this
will be denoted by prbp(---). Each symbol in \funsym$^I$ and \sentconn$^I$
must have two associated binding priorities: the left binding
priority and the right binding priority (both natural numbers $\geq 1$);
these will be denoted by lbp(---) and rbp(---) respectively.

The five ambiguous cases above are disambiguated in the following way:

\begin{enumerate}

\item If rbp({\funsym}$^I_1$) $>$ lbp({\funsym}$^I_2$)
then the expression is parsed as:

({\term}$_1$ {\funsym}$^I_1$ {\term}$_2$) {\funsym}$^I_2$ {\term}$_3$

Otherwise, if
rbp({\funsym}$^I_1$) $\leq$ lbp({\funsym}$^I_2$)
then the expression is parsed as:

{\term}$_1$ {\funsym}$^I_1$ ({\term}$_2$ {\funsym}$^I_2$ {\term}$_3$)

\item If rbp({\sentconn}$^I_1$) $>$ lbp({\sentconn}$^I_2$)
then the expression is parsed as:

({\wff}$_1$ {\sentconn}$^I_1$ {\wff}$_2$) {\sentconn}$^I_2$ {\wff}$_3$

Otherwise, if
rbp({\sentconn}$^I_1$) $\leq$ lbp({\sentconn}$^I_2$)
then the expression is parsed as:

{\wff}$_1$ {\sentconn}$^I_1$ ({\wff}$_2$ {\sentconn}$^I_2$ {\wff}$_3$)

\item If prbp({\funsym}$^P$) $>$ lbp({\funsym}$^I$)
then the expression is parsed as:

({\funsym}$^P$ {\term}$_1$) {\funsym}$^I$ {\term}$_2$

Otherwise, if
rbp({\sentconn}$^I_1$) $\leq$ lbp({\sentconn}$^I_2$)
then the expression is parsed as:

{\wff}$_1$ {\sentconn}$^I_1$ ({\wff}$_2$ {\sentconn}$^I_2$ {\wff}$_3$)

\item If prbp({\sentconn}$^P$) $>$ lbp({\sentconn}$^I$)
then the expression is parsed as:

({\sentconn}$^P$ {\wff}$_1$) {\sentconn}$^I$ {\wff}$_2$

Otherwise, if
prbp({\sentconn}$^P$) $\leq$ lbp({\sentconn}$^I$)
then the expression is parsed as:

{\sentconn}$^P$ ({\wff}$_1$ {\sentconn}$^I$ {\wff}$_2$)

\item If $1000 >$ lbp({\sentconn}$^I$)
then the expression is parsed
as \footnote{1000 is the default prefix binding
priority.}:

({\quantprefix} {\wff}$_1$) {\sentconn}$^I$ {\wff}$_2$

Otherwise, if
$1000 \leq$ lbp({\sentconn}$^I$)
then the expression is parsed as:

{\quantprefix}  ({\wff}$_1$ {\sentconn}$^I$ {\wff}$_2$)

\end{enumerate}

\subsection{The logical symbols}
\label{sec-log-symb}

The logical symbols are hardwired into the system.
The two categories {\quant} and {\sentconn}
consist entirely of logical constants and are thus fixed in advance for each
language.

\gap
\begin{center}
\fbox{
\parbox{15cm}{
{\quant} ::= {\bf forall} \I {\bf exists}

{\sentconn} ::= {\bf not} \I
                {\bf and} \I {\bf or} \I {\bf imp} \I {\bf iff}
}}
\end{center}

The logical connectives are all operator symbols
of arity 2 and declared as infix with the exception of {\bf not}
which has arity 1 and is prefix. The binding priorities of these symbols are
given in the table below.

\begin{center}
\begin{tabular}{|l|c|c|ccc|} \hline
\multicolumn{1}{|c}{Symbol} &
\multicolumn{1}{c}{Arity} &
\multicolumn{1}{c}{Type} &
\multicolumn{1}{c}{prbp} & lbp & rbp \\ \hline
{\bf not} & 1 & prefix & 1000 & n/a & n/a \\ \hline
{\bf and} & 2 & infix & n/a  & 750 & 755 \\ \hline
{\bf or} & 2 & infix & n/a  & 740 & 745 \\ \hline
{\bf imp} & 2 & infix & n/a  & 730 & 735 \\ \hline
{\bf iff} & 2 & infix & n/a  & 720 & 725 \\ \hline
\end{tabular}
\end{center}

The two truth values {\tt TRUE} and {\tt FALSE} and the equality
predicate, {\tt =}, are also logical symbols, {\tt TRUE} and
{\tt FALSE} belong to the category {\sentconst} and {\tt =} belongs to the
category {\predconst}; however, the categories {\sentconst} and
{\predconst} are not fixed
(because other primitive symbols in these categories
can be user declared).

The equality predicate, {\tt =}, has arity 2 and is declared as infix.


\documentclass[aspectratio=43]{beamer}

\usetheme[progressbar=frametitle]{metropolis}
\usepackage{appendixnumberbeamer}

\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{xcolor}

\usepackage{transparent}
\usepackage{pdfpages}

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

\usepackage{array}
\usepackage{booktabs}

\usepackage{multicol}
\usepackage{vwcol}
\usepackage{wrapfig}
\usepackage[export]{adjustbox}

\usepackage{graphicx}

  % declare the path(s) where your graphic files are
\graphicspath{{media/}}
  % and their extensions so you won't have to specify these with
  % every instance of \includegraphics
\DeclareGraphicsExtensions{.pdf,.jpeg,.png}

\usepackage[utf8]{inputenc}

\usepackage[font=small,labelfont=bf]{caption}

\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}

\DeclareCaptionFont{tiny}{\tiny}

\DeclareMathOperator*{\argmax}{argmax}
\DeclareMathOperator*{\argmin}{argmin}

%\setbeamercolor{background canvas}{bg=white}

\usebackgroundtemplate%
{%
    \includegraphics[width=\paperwidth,height=\paperheight]{lmu_background}%
}
\newcommand\pro{\item[$+$]}
\newcommand\con{\item[$-$]}

\newcommand{\themename}{\textbf{\textsc{metropolis}}\xspace}

\newcommand{\backupbegin}{
   \newcounter{finalframe}
   \setcounter{finalframe}{\value{framenumber}}
}
\newcommand{\backupend}{
   \setcounter{framenumber}{\value{finalframe}}
}

\title{Missing Data Imputation}
\date{11.12.2019}
\author{Alexander Hanf, Arber Qoku}

\begin{document}
%
\maketitle

\begin{frame}{Agenda}
\tableofcontents
\end{frame}

\section{Statistical background}

\begin{frame}{Missing data}
\begin{itemize}
\item Very common in practical problems
\item Data can be missing for many reasons
\begin{itemize}
\item Survey participant is unreachable or refuses to answer
\item Question does not apply to patient (sex-specific)
\item Subset of sensors not working for a period of time
\end{itemize}
\item Complicates statistical analysis (bias, statistical power, ...)
\item Many machine learning models can not handle missing data.
\end{itemize}
\end{frame}

\begin{frame}{Types of missing data}
\begin{itemize}
\item Missing completely at random (MCAR): the pattern of missing values is independent of all other covariates (both observed and unobserved)
\item Missing at random (MAR): the pattern of missing values depends only on observed covariates
\item Missing not at random (MNAR): the pattern of missing values also depends on unobserved covariates
\end{itemize}
\end{frame}

\begin{frame}{Idea of Imputation}
\begin{itemize}
\item Use statistical techniques to fill in the missing values
\item Make the most of the data we have
\item Caution is required! We can bias our analysis or create nonsensical data
\end{itemize}
\end{frame}

\section{Imputation Methods}

\begin{frame}{Complete case analysis}
\begin{itemize}
\item Row-wise or column-wise deletion
\end{itemize}
\begin{itemize}
\pro Very simple
\pro Valid inferences when MCAR
\end{itemize}
\begin{itemize}
\con Biased results on MAR or MNAR
\con Too few datapoints (if any) left!\\
If only $5\%$ missing independently on a dataset with 20 columns, we end up with $0.95^{20} \approx 35\%$ of the original data\\
\end{itemize}
\textcolor{red}{Big problem in predictive modelling!}
\end{frame}

\begin{frame}{Imputation of constant values}
\begin{itemize}
\item Substitute mean, median, mode, constant value
\end{itemize}
\begin{itemize}
\pro Simple
\pro Retains all observed values
\pro Can preserve statistical quantities
\end{itemize}
\begin{itemize}
\con Underestimates standard errors
\con May impute unrealistic values
\end{itemize}
\end{frame}

\begin{frame}{Hot-Deck Imputation}
\begin{itemize}
	\item General idea: reuse values from other observations
	\item Assumption: identically distributed data points
	\item "Donor" observations can be chosen in multiple ways
	\begin{itemize}
		\item Naively: imputing value from randomly chosen observation
		\item Distance-based methods
		\item Predictive mean matching (Regression \& Distance)
	\end{itemize}
\end{itemize}
$$X = \underbrace{\begin{pmatrix}
	x_{11} 	& x_{12} 	& x_{13} \\
	x_{21} 	& x_{22} 	& x_{23} \\
	- 		& x_{32} 	& x_{33} \\
	\end{pmatrix}}_\text{original design matrix}
\rightarrow
\underbrace{
	\begin{pmatrix}
	x_{12} 	& x_{13} \\
	x_{22} 	& x_{23} \\
	\end{pmatrix}
}_\text{design matrix},
\underbrace{\begin{pmatrix}
	x_{11} \\
	x_{21} \\
	\end{pmatrix}}_\text{response}
\rightarrow
\underbrace{
	\begin{pmatrix}
	\textcolor{blue}{\hat{x}_{11}}		& x_{12} 	& x_{13} \\
	\textcolor{blue}{\hat{x}_{21}} 		& x_{22} 		& x_{23} \\
	\textcolor{blue}{\hat{x}_{31}} & x_{32} 	& x_{33} \\
	\end{pmatrix}}_\text{compute distances}
$$

\begin{itemize}
	\pro Easy to implement and computationally efficient
\end{itemize}
\begin{itemize}
	\con Donor values might get reused often
	\con Underestimates standard errors
\end{itemize}
\end{frame}

\begin{frame}{Multiple Imputation}
\begin{itemize}
\item Use given data to quantify uncertainty of imputations
\end{itemize}
\phantom{This text will be invisible}
\centering
\includegraphics[width=0.9\paperwidth]{MI}
\end{frame}

\begin{frame}{Specification Overhead}
\begin{itemize}
%\item Missingness pattern: MCAR, MAR, MNAR
\item Imputation model: linear regression, random forest, ...
\item Predictor vs response variables: leave-one-out, interactions, auxiliary, ...
\item Order of variable imputation: random, least/most missing first, ...
\item Initial imputations, number of iterations (convergence condition)
\item Number of multiply imputed datasets (cycles)
\end{itemize}
\phantom{This text will be invisible invisible invisible invisible invisible invisible}
\onslide<2> $\Rightarrow$ Multiple Imputation by Chained Equations (MICE)
\end{frame}

\begin{frame}{Multiple Imputation by Chained Equations (MICE)}
$$X = \underbrace{\begin{pmatrix}
x_{11} 	& - 		& x_{13} \\
x_{21} 	& x_{22} 	& - \\
- 		& x_{32} 	& - \\
x_{41} 	& x_{42} 	& x_{43} \\
\end{pmatrix}}_\text{original design matrix}
\rightarrow
\underbrace{\begin{pmatrix}
x_{11} 		& \textcolor{red}{\bar{x}_{.2}} 	& x_{13} \\
x_{21} 		& x_{22} 		& \textcolor{red}{\bar{x}_{.3}} \\
\textcolor{red}{\bar{x}_{.1}} & x_{32} 	& \textcolor{red}{\bar{x}_{.3}} \\
x_{41} 		& x_{42} 	& x_{43} \\
\end{pmatrix}}_\text{initial imputation}
\rightarrow
\underbrace{\begin{pmatrix}
\textcolor{red}{\bar{x}_{.2}} 	& x_{13} \\
x_{22} 		& \textcolor{red}{\bar{x}_{.3}} \\
x_{42} 	& x_{43} \\
\end{pmatrix}}_\text{design matrix},
\underbrace{\begin{pmatrix}
x_{11} \\
x_{21} \\
x_{41} \\
\end{pmatrix}}_\text{response}$$

$$
\rightarrow
\begin{pmatrix}
x_{11} 		& \textcolor{red}{\bar{x}_{.2}} 	& x_{13} \\
x_{21} 		& x_{22} 		& \textcolor{red}{\bar{x}_{.3}} \\
\textcolor{blue}{\hat{x}_{31}} & x_{32} 	& \textcolor{red}{\bar{x}_{.3}} \\
x_{41} 		& x_{42} 	& x_{43} \\
\end{pmatrix}
\rightarrow
\begin{pmatrix}
x_{21} 		& \textcolor{red}{\bar{x}_{.3}} \\
\textcolor{blue}{\hat{x}_{31}} & \textcolor{red}{\bar{x}_{.3}} \\
x_{41} 		& x_{43} \\
\end{pmatrix},
\begin{pmatrix}
x_{22} \\
x_{32} \\
x_{42} \\
\end{pmatrix}
\rightarrow
\cdots
\underbrace{\begin{pmatrix}
x_{11} 		& \textcolor{blue}{\hat{x}_{12}} 	& x_{13} \\
x_{21} 		& x_{22} 		& \textcolor{blue}{\hat{x}_{23}} \\
\textcolor{blue}{\hat{x}_{31}} & x_{32} 	& \textcolor{blue}{\hat{x}_{33}} \\
x_{41} 		& x_{42} 	& x_{43} \\
\end{pmatrix}}_{X_i^{*}}
$$
%\onslide<2>\textcolor{red}{Do we actually need multiple datasets in predictive modelling?}
\end{frame}

\begin{frame}{Deep Learning for Imputation}
\begin{itemize}
	\item Recent advances in deep learning improved model performance on a wide variety of NLP tasks
	\item This can be used to impute textual features or other categorical features
	\item Example: searching for "yellow dress" on Amazon - textual description might exist, but "colour" is missing in database
\end{itemize}
\end{frame}

\begin{frame}{Amazon DataWig}
\small scalable, high-precision, language-agnostic, end-to-end pipeline

\centering\includegraphics[width=0.7\paperwidth]{datawig}
\end{frame}

\section{Tools and libraries for dealing with missing data}

\begin{frame}{Useful libraries}
Exploratory data analysis
\begin{itemize}
\item General purpose: \href{https://github.com/pandas-profiling/pandas-profiling}{pandas-profiling}
\item Specific to missing data: \href{https://github.com/ResidentMario/missingno}{missingno}
\end{itemize}
Imputation
\begin{itemize}
\item General purpose ($\sim$\href{https://cran.r-project.org/web/packages/mice/mice.pdf}{\texttt{ mice}} in R): \href{https://scikit-learn.org/stable/modules/impute.html}{scikit-learn}
\item Hot-deck with KNNs: \href{https://github.com/iskandr/fancyimpute}{fancyimpute}
\item Random Forest ($\sim$\href{https://cran.r-project.org/web/packages/missForest/missForest.pdf}{\texttt{ missForest}} in R): \href{https://github.com/epsilon-machine/missingpy}{missingpy}
\item Imputation of Time Series Data (WIP): \href{https://github.com/eltonlaw/impyute}{impyute}
\end{itemize}
\end{frame}

\section{Live Demo}

\appendix
\begin{frame}{References}
\scriptsize
\begin{itemize}
	\item Biessmann, Felix, et al. "Deep Learning for Missing Value Imputation in Tables with Non-Numerical Data." Proceedings of the 27th ACM International Conference on Information and Knowledge Management. ACM, 2018.
	\item Buuren, S. van, and Karin Groothuis-Oudshoorn. "mice: Multivariate imputation by chained equations in R." Journal of statistical software (2010): 1-68.
	\item Wulff, Jesper N., and Linda Ejlskov. "Multiple Imputation by Chained Equations in Praxis: Guidelines and Review." Electronic Journal of Business Research Methods 15.1 (2017).
	\item Joenssen, Dieter William Hermann. Hot-Deck-Verfahren zur Imputation fehlender Daten: Auswirkungen des Donor-Limits. Diss. 2015.
\end{itemize}
\end{frame}
\end{document}


\input{preamble}
\usepackage{multirow}
\usepackage{array}

\title{Would I ever Lie Group to you?}
\author{\small{Apurva Nakade}}
\date{}
\begin{document}
\maketitle
\tableofcontents
\vspace{2em}
\epigraph{It has long been an axiom of mine that the little things are infinitely the most important.}{Sherlock Holmes\\}

\section{Introduction}
\vspace{2em}

Lie Groups are groups that are also smooth manifolds. We further require that the product and the inverse maps be smooth. Lie Groups typically arise as symmetries of smooth spaces along with some extra structure, the simplest such mathematical objects are vector spaces over $\R$ and $\C$ whose symmetries groups are the matrix groups. In this class, we'll start by analyzing the group theoretic properties of matrix groups and then focus on some of their special subgroups which have direct connections to geometry, physics, and number theory.

This is an IBL class. All the relevant theory will be built through problems. The table on the next page has all the definitions which you should read and understand as you need. The (*) on the problems indicate their difficulty level. You're free to do the problems that you find interesting and assume the rest to be true, or ask for answers or hints or whatever. {Live your own life.}

\newpage
\noindent \textbf{Definitions: }
\begin{center}
	\begin{tabular}{l c l}
		$I $ & $=$ & Identity Matrix \\
		$A^T$ & $=$ & Transpose of $A $ \\
		$A^*$ & $=$ & Transpose + complex conjugate of $A$ \\
		$\det A$ & $=$ & Determinant of $A$\\
	\end{tabular}
\end{center}
\begin{center}
	\setlength\extrarowheight{10pt}
	\begin{tabular}{|l|c|l|}
		\hline
		                                & $M_{n \times n}(\R)$        & $n \times n$ matrices with entries in $\R$                         \\\hline
		\multirow{2}{*}{General Linear Group}
		                                & \multirow{2}{*}{$GL_n(\R)$} & $\{ n \times n \mbox{ invertible matrices with entries in } \R \}$ \\
		                                &                             & $\cong \{ A \in M_{n \times n}(\R) : \det A \neq 0\}$              \\\hline
		Orientation Preserving Matrices & $GL^+_n(\R)$                & $\{ A \in GL_{n \times n}(\R) : \det A > 0\}$                      \\\hline
		Orthogonal Group                & $O(n)$                      & $\{ A \in GL_{n \times n}(\R) : A^TA = I\}$                        \\\hline
		Special Linear Group            & $SL_n(\R)$                  & $\{ A \in GL_{n \times n}(\R) : \det A =1\}$                       \\\hline
		Special Orthogonal Group        & $SO(n)$                     & $O(n) \cap SL_n(\R)$                                               \\\hline
    Upper Triangular Matrices & $UT_n(\R)$ & Invertible matrices with only 0's below the diagonal \\\hline
		                                & $M_{n \times n}(\C)$        & $n \times n$ matrices with entries in $\C$                         \\\hline
		\multirow{2}{*}{General Linear Group}
		                                & \multirow{2}{*}{$GL_n(\C)$} & $\{ n \times n \mbox{ invertible matrices with entries in } \C \}$ \\
		                                &                             & $\cong \{ A \in M_{n \times n}(\C) : \det A \neq 0\}$              \\\hline
		Unitary Group                   & $U(n)$                      & $\{ A \in GL_{n \times n}(\C) : A^*A = I\}$                        \\\hline   Special Linear Group & $SL_n(\C)$ & $\{ A \in GL_{n \times n}(\C) : \det A =1\}$ \\\hline
		Special Unitary Group           & $SU(n)$                     & $U(n) \cap SL_n(\C)$                                               \\\hline
	\end{tabular}
\end{center}

\begin{align*}
                                   \xymatrix{
  SO(n) \ar@{^{(}->}[r] \ar@{^{(}->}[dr] & SL(n) \ar@{^{(}->}[r]  & GL_n^+(\R) \ar@{^{(}->}[r] & GL_n(\R) \\
	                                       & O(n) \ar@{^{(}->}[urr]
	}
\end{align*}
\begin{align*}
                                   \xymatrix{
  	SU(n) \ar@{^{(}->}[r]                  & U(n) \ar@{^{(}->}[r]   & GL_n(\C)
  	}
\end{align*}

\newpage
\section{Group Theoretic Properties of Matrix Groups}
\begin{question}
	Prove that $GL_n(\R)$ is a group under matrix multiplication. Is it a group under addition?
\end{question}

\begin{question}
	Prove that the map $\det$ defines a group homomorphism $GL_n(\R) \rightarrow \R^\times$, the group of non-zero reals. Hence prove that $SL_n(\R)$ is a normal subgroup of $GL_n(\R)$.
\end{question}

\begin{question}
	Identify $GL^+_n(\R)$ as the kernel of a group homomorphism $GL_n(\R) \rightarrow G$ from some group $G$. Hence prove that $GL^+_n(\R)$ is a normal subgroup of $GL_n(\R)$. Further show that $SL_n(\R)$ is a normal subgroup of $GL^+_n(\R)$.
\end{question}

\begin{question}
  Prove that $O(n)$ and $SO(n)$ are groups.
\end{question}

\begin{question}
	Using the map $\det$ identify the quotient groups
	\begin{enumerate}
		\item $GL_n(\R)/SL_n(\R)$
		\item $GL_n(\R)/GL^+_n(\R)$
		\item $GL^+_n(\R)/SL_n(\R)$
		\item $O(n)/SO(n)$
	\end{enumerate}
\end{question}

Note that $M_{n \times n}(\R)$ is naturally isomorphic to $\R^{n^2}$, hence $GL_n(\R)$ can be naturally thought of as a subset of $\R^{n^2}$ , so we can talk about connectedness of its subsets.
\begin{question}
	Prove that $GL_n(\R)$ and $O(n)$ are not connected.\footnote{A subset $X \subseteq \R^n$ is not connected if there exists two points $p,q \in X$ such that there is no continuous path lying entirely in $X$ connecting $p$ to $q$.}
\end{question}
\begin{remark}
	$GL_n(\C)$ and $GL^+_n(\R)$ are connected spaces. This fact is surprisingly hard to prove. Can you think of a way to do this?
\end{remark}

\begin{question}
	Prove that $GL_n(\C)$ is a group under matrix multiplication. Is it a group under addition?
\end{question}

\begin{question}
	Prove that $\det$ is a group homomorphism from $GL_n(\C)$ to $\C^\times$, the group of non-zero complex numbers. Hence prove that $SL_n(\C)$ is a normal subgroup of $GL_n(\C)$.
\end{question}

\begin{question}
  Prove that $U(n)$ and $SU(n)$ are groups.
\end{question}

\begin{question}
	Using the map $\det$ identify the following quotient groups
	\begin{enumerate}
		\item $GL_n(\C)/SL_n(\C)$
		\item $U(n)/SU(n)$
	\end{enumerate}
\end{question}

\begin{question}$ $
  \begin{enumerate}
    \item Show that the inclusion $\R \hookrightarrow \C$ induces a natural inclusion
    \begin{align*}
      \mathcal{I}: GL_n(\R) \hookrightarrow GL_n(\C)
    \end{align*}
    Show that $\mathcal{I}$ is a group homomorphism.
    \item Show that $\mathcal{I}(SL_n(\R)) \subseteq SL_n(\C)$, $\mathcal{I}(O(n)) \subseteq U(n)$ and $\mathcal{I}(SO(n)) \subseteq SU(n)$.
    \item* As a vector space over $\R$, the complex numbers $\C$ are isomorphic to $\R^2$. Show that this induces a natural map
    \begin{align*}
      \mathcal{J}: GL_n(\C) \rightarrow GL_{2n}(\R)
    \end{align*}
    Show that $\mathcal{J}$ is a group homomorphism. Is $\mathcal{J}$ a surjection?
    \item Explicitly describe the compositions $\mathcal{I} \circ \mathcal{J}$ and $\mathcal{J} \circ \mathcal{I}$.
		\item** Show that $\mathcal{J}(SL_n(\C)) \subseteq SL_{2n}(\R)$, $\mathcal{J}(U(n)) \subseteq O(2n)$ and $\mathcal{J}(SU(n)) \subseteq SO(n)$.
  \end{enumerate}
\end{question}

\newpage
\section{Orthogonal Groups}
\begin{question}
	Show that $A \in GL_n(\R)$ if and only if the columns of $A$ form a basis for $\R^n$.
\end{question}

\begin{question}
		Show that $A \in O(n)$ if and only if the columns of $A$ form an orthonormal basis for $\R^n$. Find the corresponding statement for $U(n)$.
\end{question}

\begin{question}
	Explicitly describe the matrices in $O(2)$. Which of these matrices are in $SO(2)$?\\ What linear transformations do these geometrically represent?
\end{question}

\begin{thm}[Spectral Theorem]
	For every matrix $A$ in $U(n)$ there exists a matrix $P \in U(n)$ and a diagonal matrix $D \in GL_n(\C)$ such that
	\begin{align*}
		A = P^* D P
	\end{align*}
	The entries of $D$ are called the \textbf{eigenvalues} of $A$. This is also true for $O(n)$ thought of as a subgroup of $U(n)$ ($P$ still needs to be in $U(n)$ and $D$ can have complex entries). For $A \in O(n)$ it is further true that if $\lambda \in \C$ is an eigenvalue then so is $\conj \lambda$.
\end{thm}

\begin{question}
	What can you say about the eigenvalues of matrices in $U(n)$, $SU(n)$, $O(n)$, $SO(n)$?
\end{question}

\begin{question}
	\label{q:so3_rotations}
	Show that if $A \in SO(3)$ then 1 is an eigenvalue of $A$. Argue that, there exists a basis of $\R^3$ in which $A$ looks like
	\begin{align*}
		\begin{bmatrix}
			1 & 0 & 0 \\
			0 & \cos \theta & -\sin \theta \\
			0 & \sin \theta & \cos \theta
		\end{bmatrix}
	\end{align*}
	i.e. every matrix in $SO(3)$ is a rotation of $\R^3$ about an axis by some angle $\theta$.
\end{question}

\begin{question}*
  Prove that $UT_n(\R)$ is a group.
\end{question}

\begin{question} \label{q:uniqueness_qr} $ $
  \begin{enumerate}
    \item
    Find $SO(n) \cap UT_n(\R)$.
    \item* Suppose we are given matrices $Q_1, Q_2 \in O(n)$ and $R_1, R_2 \in UT_n(\R)$ such that all the diagonal entries of $R_1, R_2$ are positive. Prove that if
    \begin{align*}
      Q_1 R_1 = Q_2 R_2
    \end{align*}
    then $Q_1 = Q_2$ and $R_1 = R_2$.
  \end{enumerate}
\end{question}

\newpage
\begin{definition}
	The \textbf{Gram-Schmidt orthonormalization} process takes as input an arbitrary basis for $\R^n$ and produces an orthonormal basis. Let $v_1, v_2, \dots, v_n$ be a basis of $\R^n$. The orthonormal basis is constructed by the following inductive algorithm:
	\begin{mdframed}
		\begin{enumerate}
			\item Let $e_1 = \frac{v_1}{\norm{v_1}}$.
			\item Assume that we have computed $e_1, \dots, e_k$. Define
			\begin{align*}
				e'_{k+1} = v_{k+1} - (v_{k+1} \cdot e_1)e_1 - (v_{k+1}\cdot e_2)e_2 \dots - (v_{k+1} \cdot e_k)e_k
			\end{align*}
			(Here $\cdot$ stands for the dot product.)
			\item Let $e_{k+1} = \frac{e'_{k+1}}{\norm{e'_{k+1}}}$.
			\item Repeat steps b) and c) until we have $e_1, \dots, e_n$.
		\end{enumerate}
	\end{mdframed}
\end{definition}

	\begin{question}*
	  Prove that the tuple $(e_1, \dots, e_n)$ that is the output of the Gram-Schmidt orthonormalization is an orthonormal basis of $\R^n$.
	\end{question}

	\begin{question}*
	  \label{q:Gram-Schmidt}
	  Prove that the Gram-Schmidt orthonormalization is equivalent to the following statement:

	  For every matrix $A \in GL_n(\R)$ there exists an orthogonal matrix $Q$ and an upper triangular matrix $R$ with positive diagonal entries, such that $$A = QR$$
		Combining this with Question \ref{q:uniqueness_qr} we get what is called the \textbf{QR decomposition} of a matrices.
	\end{question}

\begin{question}*
    \label{q:normality}
  Prove that $O(2)$ and $UT_2(\R)$ are not normal subgroups of $GL_2(\R)$. This is more generally true for all higher dimensions.
\end{question}

\begin{question}
  Question \ref{q:Gram-Schmidt} seems to suggest that $GL_n(\R)/O(n) \cong UT_n(\R)$ and $GL_n(\R)/UT_n(\R) \cong O(n)$. But this would imply that $O(n)$ and $UT_n(\R)$ are normal subgroups of $GL_n(\R)$, however this contradicts Question \ref{q:normality}. How does one resolve this paradox?
\end{question}

\newpage
\section{Mobius transforms and $SL_2(\C)$}
\begin{definition}
	For $\begin{bmatrix}a & b \\ c & d\end{bmatrix} \in SL_{2}(\C)$ define the \textbf{Mobius transform} $M^{a,b}_{c,d}(z)$ as\footnote{Here we are setting $ M^{a,b}_{c,d}(-d/c) = \infty\quad$ and $ M^{a,b}_{c,d}(\infty) = a/c$ etc.}
	\begin{align*}
		M^{a,b}_{c,d}: \C \cup \{ \infty \} \rightarrow \C \cup \{ \infty \} \qquad
		z \mapsto \dfrac{az + b}{cz + d}
	\end{align*}
		Define the set $\mathcal{M}$ of Mobius transformations to be the set of such transformations i.e. $z \mapsto \dfrac{az + b}{cz + d}$ with $ad - bc = 1$.

\end{definition}
\begin{question}
	Show that if $\begin{bmatrix}p & q \\ r & s\end{bmatrix} \begin{bmatrix}a & b \\ c & d\end{bmatrix}  = \begin{bmatrix}e & f \\ g & h\end{bmatrix}$ then $M^{a,b}_{c,d} \left( M^{p,q}_{r,s} (z) \right)= M^{e,f}_{g,h}(z)$.
\end{question}
\begin{question}
	Show that the Mobius transformations form a group and
			there a natural surjective group homomorphism $$ \varphi: SL_2(\C) \rightarrow \mathcal{M}$$
\end{question}

\begin{question}
	Show that the kernel of $\varphi$ is isomorphic to $\Z/2$.
\end{question}
\begin{remark}
	The above problems imply that the group of Mobius transforms is isomorphic to $SL_2(\C)/(\Z/2)$. This group is called the \textbf{Projective Special Linear Group}, denoted $PSL_2(\C)$.
\end{remark}

\begin{question}
	Show that the Mobius transforms corresponding to the matrices in $SL_2(\R)$ send the upper half plane to the upper half plane i.e.
	\begin{align*}
		\mbox{ if $\im(z) \ge 0$ and $\begin{bmatrix}a & b \\ c & d\end{bmatrix} \in SL_{2}(\R)$ then $\im\left(M^{a,b}_{c,d}(z)\right) \ge 0$. }
	\end{align*}
\end{question}
Mobius transforms can also be thought of as taking the unit disk to the unit disk after a ``change of basis''.
\begin{question}$ $
	\begin{enumerate}
		\item Show that the Mobius transform $\sigma(z) = i\dfrac{1+z}{1-z}$ sends the unit disk in $\C$ ($\norm{z} \le 1$) to the upper half plane ($\im(z) \ge 0$).
		\item Argue that for $\begin{bmatrix}a & b \\ c & d\end{bmatrix} \in SL_{2}(\R)$ the transformation $\sigma \circ M^{a,b}_{c,d}(z) \circ \sigma^{-1}$ takes the unit disc to itself.
	\end{enumerate}
\end{question}

The upper half plane and the unit disc are models of universal hyperbolic (constant Gaussian curvature -1) surfaces. Other hyperbolic surfaces can be obtained from these by quotienting by subgroups of $PSL_2(\R)$. Of particular importance are the discrete subgroups of $PSL_2(\R)$ called the Fuchsian groups. Quotienting the upper half plane by these gives rise to hyperbolic Riemann surfaces.

\newpage

If we further restrict the entries $a, b, c, d$ to be integers we get the subgroup of $PSL_2(\R)$ called the \textbf{modular group} $PSL_2(\Z)$.

\begin{enumerate}
	\item Show that $S=\begin{bmatrix}1 & 1 \\ 0 & 1\end{bmatrix}$, $T=\begin{bmatrix}0 & 1 \\ -1 & 0\end{bmatrix}$ are in $SL_2(\Z)$.
	\item** Show that $S, T$ generate $SL_2(\Z)$.
\end{enumerate}

\begin{question}
	For a positive integer $n$, show that the sets of matrices of the form $A = \begin{bmatrix}a & b \\ c & d\end{bmatrix}$ in $SL_2(\Z)$ as defined below
	\begin{align*}
		\Gamma(n) &:= \{ \quad a,d \equiv 1 \mod n, \quad b,c \equiv 0 \mod n \quad \} \\
		\Gamma_0(n) &:= \{ \quad  c \equiv 0 \mod n  \quad \} \\
		\Gamma_1(n) &:= \{ \quad  a,d \equiv 1 \mod n, \quad c \equiv 0 \mod n  \quad \} \\
		\Lambda(n) &:= \{ \quad  ac \equiv 0 \mod n, \quad bd \equiv 0 \mod n  \quad \} \\
		\end{align*}
		are groups. These are examples of \textbf{congruence subgroups} of the modular group.
\end{question}

The modular group and the congruence subgroups are extremely important in number theory as the quotient of the upper half plane by these groups is the space on which various modular forms live.

\newpage
\section{$\mathrm{Spin}(3)$: The double cover of $SO(3)$}
\begin{definition}
	Define the algebra of \textbf{quaternions} as
	\begin{align*}
		\mathbb{H} = \{ \: a_0 + a_i i + a_j j + a_k k :& \: a_0, a_1, a_2, a_3 \in \R, \\
		&i^2 = j^2 = k^2 = ijk = -1 \: \}
	\end{align*}
	Here $i,j,k$ are formal variables. (Just to clarify the notation: $-1 = -1 + 0i + 0j + 0k$.)
	A quaternion is a unit quaternion if $a_0^2 + a_1^2 + a_2^2 + a_3^2 = 1$. Denote the set of \textbf{unit quaternions} by $u \mathbb{H}$.
\end{definition}
\textbf{Notation: } We'll denote the quaternions by bold letters $\mathbf{a}, \mathbf{b}$ etc. \begin{align*}
	\mathbf{a} &= a_0 + a_i i + a_j j + a_k k \\
	\conj{\mathbf{a}} &:= a_0 - a_i i - a_j j - a_k k\\
	\norm{\mathbf{a}} &:= a_0^2 + a_1^2 + a_2^2 + a_3^2\\
	\mathrm{Re}(\mathbf{a}) &:= a_0 \qquad\mbox{ (the real part of $\mathbf{a}$)}
\end{align*}

\begin{question}
	Show that $\mathbf{a} \conj{\mathbf{a}} = \norm{a}$ and hence if $\mathbf{a} \in u \mathbb{H}$ then $\mathbf{a}^{-1} \in u \mathbb{H}$.
\end{question}
Define
\begin{align*}
	\tau (i) = \begin{bmatrix}
	\sqrt{-1} & 0 \\
	0 & -\sqrt{-1}
\end{bmatrix}, \qquad
\tau (j) = \begin{bmatrix}
0 & -1 \\
1 & 0
\end{bmatrix}, \qquad
\tau (k) = \begin{bmatrix}
0 & \sqrt{-1} \\
\sqrt{-1} & 0
\end{bmatrix}
\end{align*}
and extend it linearly to all of $\mathbb{H}$. The matrices $\tau(i), \tau(j), \tau(k)$ are called the \textbf{Pauli matrices}.
\begin{question}
	Explicitly write down $\tau(\mathbf{a})$ and show that $\tau(\mathbf{a}) \in SU(2)$ for all $\mathbf{a} \in u\mathbb{H}$.
\end{question}
\begin{question}
	Show that $\tau(\mathbf{a}) \tau(\mathbf{b}) = \tau(\mathbf{a} \mathbf{b})$ and that $\norm{\mathbf{a}} = \det (\tau (\mathbf{a}))$. Conclude that $\norm{\mathbf{a}} \norm{\mathbf{b}} = \norm{\mathbf{a} \mathbf{b}}$.
\end{question}

\begin{question}
	Combine the above results to show that $u \mathbb{H}$ is closed under multiplication and inverses and hence is a group. In fact, argue that $\tau$ is an isomorphism and hence $u \mathbb{H} \cong SU(2)$.
\end{question}
\begin{remark}
	Note that as a topological space $u \mathbb{H}$ is isomorphic to $S^3 \subseteq \R^4$ and thus we've shown that $SU(2)$ is the 3 dimensional unit sphere!!!
\end{remark}

Quaternions (and hence $SU(2)$) also define rotations of spaces. For each $\mathbf{a} \in u \mathbb{H}$ we can define a transformation \begin{align*}
	\Phi(\mathbf{a}): \mathbb{H} &\rightarrow \mathbb{H} \\
	\mathbf{v} &\mapsto \mathbf{a}^{-1} \mathbf{v} \mathbf{a}
\end{align*}

\begin{question}
	As a vector space over $\R$, the space $\mathbb{H} \cong \R^4$. Using this identification, show that $\Phi$ defines a group homomorphism $u \mathbb{H} \rightarrow GL_4(\R)$.
\end{question}
\newpage
We can do better than this. We say that a quaternion $\mathbf{a}$ is \textbf{purely imaginary} if $\mathrm{Re}(\mathbf{a}) = 0$.

\begin{question}
	Show that $\mathbf{v}$ is purely imaginary and $\mathbf{a} \in u \mathbb{H}$ then $\mathbf{a}^{-1} v \mathbf{a}$ is also purely imaginary.
\end{question}

\begin{question}
	As a vector space over $\R$, the space of purely imaginary quaternions is isomorphic to $\R^3$. Using this identification, show that $\Phi$ defines a group homomorphism $$\pi: u \mathbb{H} \rightarrow GL_3(\R)$$
\end{question}

\begin{question}
	\label{q:double_cover}
	Show that if $\Phi(\mathbf{a})(i) = i$, $\Phi(\mathbf{a})(j) = j$, $\Phi(\mathbf{a})(k) = k$ then $\mathbf{a} = \pm 1$. Conclude that the kernel of $\pi$ is isomorphic to $\Z/2$.
\end{question}

In fact, a much stronger statement is true: The image of $\pi$ is precisely $SO(3)$. We won't prove this as the proof is quite long (but easy). Hence $\pi$ defines a surjective homomorphism
\begin{align*}
	\pi : SU(2) \rightarrow SO(3)
\end{align*}
By Question \ref{q:double_cover} kernel of this homomorphism is $\Z/2$. We say that $SU(2)$ (and hence $u \mathbb{H}$) is a \textbf{double cover} of $SO(3)$.

This has several important ramifications in various areas of mathematics, especially in physics. As we saw in Question \ref{q:so3_rotations}, every matrix in $SO(3)$ is a rotation matrix. In quantum mechanics, the group $SO(3)$ is the symmetry group that gives rise to angular momentum. Because $SU(2)$ double covers $SO(3)$, in the quantum world particles can have a \textbf{spin angular momentum} and there are two spin states for each angular momentum state. There is no classical analogue of this as the mathematical model for classical Newtonian mechanics does not inherently rely on linear algebra, but the one for quantum mechanics does.

Because of this the group $SU(2)$ (and hence $u \mathbb{H}$) is also called the \textbf{spin group} $\mathrm{Spin}(3)$. In general, there exists a group that non-trivially double covers $SO(n)$ called $\mathrm{Spin}(n)$.

$SU(2)$ is not the only linear group that leads to physically observable quantities in quantum mechanics. In the current standard model, the electroweak forces arise from the symmetry group $U(1) \times SU(2)$ and the strong nuclear forces arise from the symmetry group $SU(3)$.

% \section{Symmetries of Spaces}
% Now let's go back and understand why one would think of defining these matrix groups in the first place.
%
% $GL_n(\R)$ is simply the group of automorphisms of $\R^n$ which preserve the vector space structure.
% $GL^+_3(\R)$ is the group of automorphisms

\end{document}


\documentclass[12pt,a4paper]{article}

\usepackage[T1]{fontenc}
\usepackage[charter]{mathdesign}
\usepackage{amsmath,amsthm,enumitem,titlesec,xcolor}
\usepackage{microtype}
\usepackage[a4paper,margin=25mm]{geometry}
\usepackage[unicode]{hyperref}

\hypersetup{
    hidelinks,
    pdftitle={Distributed Algorithms},
    pdfauthor={Jukka Suomela},
}

\definecolor{titlecolor}{HTML}{0088cc}
\definecolor{hlcolor}{HTML}{f26924}

\newcommand{\q}[2]{\paragraph{\mbox{Question #1: }#2.}}
\newcommand{\sep}{{\centering \raisebox{-3mm}[0mm][0mm]{$*\quad*\quad*$}\par}}
\newcommand{\hl}[1]{\textbf{\emph{#1}}}
\newcommand{\cemph}[1]{\textcolor{hlcolor}{\textbf{\emph{\boldmath #1}}}}

\DeclareMathOperator{\diam}{diam}

\setitemize{noitemsep,leftmargin=3ex}

\titleformat{\paragraph}[runin] {\normalfont\normalsize\bfseries\color{titlecolor}}{\theparagraph}{1em}{}

\begin{document}

\noindent
\emph{CS-E4510 Distributed Algorithms / Jukka Suomela\\
midterm exam, 27 October 2021}

\paragraph{Instructions.}

There are three questions; please \cemph{try to answer something in each of them}. If you cannot solve a problem entirely, please try to solve at least some special case or simpler version of the problem (see the hints for suggestions), or failing that, please at least explain what you tried and what went wrong. Do not spend too much time with one problem; the problems are not listed in order of difficulty and they do not depend on each other.

You are free to look at any source material (this includes lecture notes, textbooks, and anything you can find with Google), but you are not allowed to collaborate with anyone else or ask for anyone's help (this includes collaboration with other students and asking for help in online forums). You are free to use any results from the lecture notes directly without repeating the details.

In each questions, it is sufficient to give a \cemph{brief, informal description} of the algorithm, and a \cemph{brief, informal explanation} of why it solves the problem correctly. You do not need to specify algorithms in the state-machine formalism, or give a complete proof of correctness. It is enough that a friendly, cooperative reader can understand your idea correctly and see why it makes sense. As usual, $n$ denotes the number of nodes in the input graph.

\q{1}{PN}

Give a deterministic distributed algorithm that solves the following problem in the PN model (any running time is fine):
\begin{itemize}
    \item Graph family: \cemph{path graphs}.
    \item Local inputs: nothing.
    \item Local outputs: label the \cemph{edges} of the graph with \cemph{integers} such that
    \begin{enumerate}[noitemsep]
        \item \cemph{adjacent} edges have different labels and
        \item the total sum of all labels is $0$.
    \end{enumerate}
\end{itemize}
\hl{Hints:} If you have a path with $4$ nodes and $3$ edges, then examples of valid labelings include $0,+1,-1$ and $-123,+246,-123$. Please note that we did not promise that the path is directed---but if you cannot solve the general case, please try to solve at least the case of a directed path (i.e., a path where for each internal node there would be exactly one successor and one predecessor). Alternatively, you can try to solve at least the case of paths with an odd number of edges or the case of paths with an even number of edges.

\q{2}{LOCAL}

Give a deterministic distributed algorithm that solves the following problem in the LOCAL model in \cemph{$o(n)$} rounds:
\begin{itemize}
    \item Graph family: \cemph{cycle graphs}.
    \item Local inputs: each node gets as input its own unique identifier and the value of $n$.
    \item Local outputs: label the \cemph{nodes} of the graph with labels from the set \cemph{$\{-1, 0, +1\}$} such that
    \begin{enumerate}[noitemsep]
        \item \cemph{adjacent} nodes have different labels and
        \item the total sum of all labels is $0$.
    \end{enumerate}
\end{itemize}
\hl{Hints:} Please note that the running time has to be $o(n)$, this is little-$o$, not big-$O$. So for example $\log n$ or $\log^* n$ is good. Also note that we did not promise that the cycle is directed---but if you cannot solve the general case, please try to solve at least the case of a directed cycle. If you cannot design a deterministic algorithm, try to design a randomized algorithm. In general, it may help to first think about the case of large values of $n$ and then solve the case of a small $n$ by brute force.

\q{3}{CONGEST}

Give a deterministic distributed algorithm that solves the following problem in the CONGEST model in \cemph{$O(\diam(G))$} rounds:
\begin{itemize}
    \item Graph family: \cemph{connected graphs}.
    \item Local inputs: each node gets as input its own unique identifier.
    \item Local outputs: label the \cemph{nodes} of the graph with \cemph{integers} such that
    \begin{enumerate}[noitemsep]
        \item \cemph{all} nodes have different labels and
        \item the total sum of all labels is $0$.
    \end{enumerate}
\end{itemize}
\hl{Hints:} If you cannot solve this problem in diameter time, try to at least design an algorithm that solves it in $O(n)$ rounds.

\end{document}