%%\documentclass[handout]{beamer}
%\documentclass[aspectratio=169,13pt]{beamer}

%\input{./preamble}

\subtitle{Fast Fourier Transform}

\date{}
\begin{document}

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

\section{Model Problem}

\begin{frame}{Sparse Linear Systems and Time-independent PDEs}

\begin{itemize}
\item The Poisson equation serves as a model problem for numerical methods:

\lgcond{
\begin{itemize}
\sitem the 2D Poisson problem and resulting Kronecker product linear system are a common benchmark,
\sitem this system has the form $\B T \otimes \B I + \B I \otimes \B T$ where $\B T$ is tridiagonal.
\end{itemize}
}

\item Dense, sparse direct, iterative, FFT, and Multigrid methods provide increasingly good complexity for the problem:

\lgcond{
\begin{itemize}
\sitem dense linear system solve costs $O(n^3)$ naively,
\sitem nested dissection with Cholesky has $O(n^{3/2})$ complexity and $O(n\log n)$ memory
\sitem Conjugate-Gradient gives $O(n^{3/2})$ complexity with $O(n)$ memory
\sitem FFT achieves $O(n \log n)$ cost and multigrid achieves $O(n)$.
\end{itemize}
}

\end{itemize}

\end{frame}

\section{Multigrid}

\begin{frame}{Multigrid}

\begin{itemize}
\item Multigrid employs a hierarchy of grids to accelerate iterative methods:

\lgcond{
\begin{itemize}
\item the residual equation $\B A \B{\hat{x}} = \B r$ on each fine grid, is approximately solved on the next coarser grid,
\mitem the equation is \coloremph{restricted} by projection matrix $\B P$, so that $\B P \B A \B P^T \B P \B{\hat{x}} = \B P \B r$
\mitem the interpolation operator (often given by $\B P^T$) is used to obtain an approximate $\B{\hat{x}}$ based on the coarse grid approximate solution,
\mitem at each level we perform some smoothing operations (e.g. Jacobi or Conjugate Gradient) before restriction and after interpolation,
\mitem at the coarsest level we typically solve directly.
\end{itemize}
}
\mdcond{}

\item The multigrid method works by resolving high-frequency error components on finer-grids and low-frequency error components on coarser grids:

\mdcond{
\begin{itemize}
\item smoothers are usually effective at reducing local error, but slow at resolving global (low-frequency) components of the error,
\mitem on coarser grids, the low frequency error may be resolved more quickly.
\end{itemize}
}

\end{itemize}

\end{frame}

\begin{frame}{Multigrid}

\begin{itemize}
\item Consider the Galerkin approximation with linear finite elements to the Poisson equation $u''=f(t)$ with boundary conditions $u(a)=u(b)=0$:
\[%\forall i \in \{1,n\} , \quad
\phi_i^{(h)}(t) = \begin{cases} (t-t_{i-1})/h &: t \in [t_{i-1},t_i] \\ (t_{i+1}-t)/h &: t\in [t_{i},t_{i+1}] \\ 0 &: \text{otherwise} \end{cases}\]
where $t_0=t_1=a$ and $t_{n+1}=t_n=b$.
\tlgcond{
The weak form with grid spacing of $h$ is
\begin{align*}
\int_a^b f(t) \phi^{(h)}_i(t) dt &=  - \sum_{j=1}^n x_j\int_a^b {\phi_j^{(h)}}'(t){\phi_i^{(h)}}'(t) dt.
\end{align*}
in multigrid, we define a coarse grid basis of $(n-1)/2$ functions, which are hat functions of twice the width,
\begin{align*}
\phi_i^{(2h)}(t) = \frac 12\phi_{2i-2}^{(h)}(t) + \phi_{2i-1}^{(h)}(t) + \frac 12\phi_{2i}^{(h)}(t) = \begin{cases} (t-t_{i-2})/2h &: t \in [t_{i-2},t_i] \\ (t_{i+2}-t)/2h &: t\in [t_{i},t_{i+2}] \\ 0 &: \text{otherwise} \end{cases}
\end{align*}
}
\lgcond{}
\end{itemize}
\end{frame}

\begin{frame}{Coarse Grid Matrix}

\begin{itemize}
\item Multigrid restricts the residual equation on the fine grid $\B A^{(h)}\B x = \B r^{(h)}$ to the coarse grid:
\tlgcond{
Let $\B{\phi}^{(2h)}=\begin{bmatrix} \phi_1^{(2h)} & \cdots &  \phi_{(n-1)/2}^{(2h)} \end{bmatrix}$ and
$\B{\phi}^{(h)}=\begin{bmatrix} \phi_1^{(h)} & \cdots &  \phi_{n}^{(h)} \end{bmatrix}$ and define \coloremph{restriction matrix} $\B P$ so that $\B{\phi}^{(2h)}=\B P \B{\phi}^{(h)}$, i.e.,
\[\B P = \frac 12\begin{bmatrix} 1 & 2 & 1 & & \\ & 1 & 2 & 1 & \\ & & \ddots & \ddots & \ddots \end{bmatrix} = \begin{bmatrix} {\B{p}^{(1)}} \\ {\B{p}^{(2)}} \\ \vdots \end{bmatrix}.\]
The coarse grid stiffness matrix is given by
\begin{align*}
a^{(2h)}_{ij} &= - \int_a^b {\phi_j^{(2h)}}'(t){\phi_i^{(2h)}}'(t) dt \\
&= - {\B{p}^{(i)}}\underbrace{\bigg(\int_a^b  {\B \phi^{(h)}}'(t) {{\B{\phi}^{(h)}}'}{}^T(t) dt\bigg)}_{-\B A^{(h)}}{\B p^{(j)}}^T, \\
\B A^{(2h)} &= \B P \B A^{(h)} \B P^T.
% &=  - \int_a^b ({\phi_{2i-2}^{(h)}}'(t) + 2{\phi_{2i-1}^{(h)}}'(t) + {\phi_{2i}^{(h)}(t)}'(t))
% ({\phi_{2j-2}^{(h)}}'(t) + 2{\phi_{2j-1}^{(h)}}'(t) + {\phi_{2j}^{(h)}(t)}'(t))dt
\end{align*}

%\begin{align*}
%\int_a^b f(t) \phi^{(h)}_i(t) dt &=  - \sum_i x_i\int_a^b {\phi_j^{(h)}}'(t){\phi_i^{(h)}}'(t) dt.
%\end{align*}
%
%To do so, we need to obtain a suitable basis transformation from $\{\phi^{(h)}_i\}$ to $\{\phi^{(2h)}_i\}$.
%We would like a correct answer at each $t$ and can obtain a system of equations by integrating w.r.t. test functions $\phi^{(h)}_j(h)$,
%\begin{align*}
%\sum_{i=1}^{n/2} x^{(2h)}_i \int_a^b\phi_i^{(2h)}(t)  \phi_j^{(h)}(t)dt = \sum_{i=1}^n x^{(h)}_i\int_a^b \phi_i^{(h)}(t) \phi_j^{(h)}(t)dt
%\end{align*}
}
\lgcond{}
\end{itemize}
\end{frame}

\begin{frame}{Restricting the Residual Equation}

\begin{itemize}
\item Given the fine-grid residual $\B r^{(h)}$, we seek to use the coarse grid to approximate $\B x^{(h)}$ so that $\B A \B x^{(h)} \approx \B r^{(h)}$

\lgcond{
\begin{itemize}
\item
%Let $\B t = \begin{bmatrix} a+h & a+2h & \cdots & b-h \end{bmatrix}^T$.
Given a function in the coarse grid basis, $u^{(2h)} = {\B x^{(2h)}}^T\B{\phi}^{(2h)}$, we can express it in the fine-grid basis via
\begin{align*}
u^{(2h)} &= {\B x^{(2h)}}^T \underbrace{\B P \B{\phi}^{(h)}}_{\B{\phi}^{(2h)}} = \underbrace{{\B x^{(2h)}}^T \B P}_{{\B x^{(h)}}^T} \B{\phi}^{(h)}.
\end{align*}
\item
Consequently, the solution to the restricted residual equation $\B A^{(2h)}\B x^{(2h)} = \B r^{(2h)}$ will lead to an approximate residual equation solution on the fine grid
with $\B x^{(h)} =\B P^T\B x^{(2h)}$.
\mitem
Noting this, we derive the form of the coarse grid residual, %form the coarse grid problem via
\begin{align*}
\B r^{(2h)} &= \B A^{(2h)}\B x^{(2h)} \\
&= \B P\B A^{(h)}\B P^T \B x^{(2h)}  = \B P\B A^{(h)}\B x^{(h)}  \\
&= \B P\B r^{(h)}.
\end{align*}
\end{itemize}
%\[w_i=u^{(2h)}(t_i)={\B x^{(2h)}}^T\B{\phi}^{(2h)}(t_i)={\B x^{(2h)}}^T\B P \underbrace{\B{\phi}^{(h)}(t_i)}_{\B e_i}\]
% so $\B w = \B P^T{\B x^{(2h)}}$.
%Note that for a function in the fine grid basis, $u^{(h)} = {\B x^{(h)}}^T\B{\phi}^{(h)}$, we have $u^{(2h)}(t_i) = x^{(h)}_i$.
%Consequently, we can express the coarse grid function in the fine grid basis via
%\begin{align*}
%u^{(h)}(t) &= \underbrace{{\B x^{(2h)}}^T \B P}_{\B x^{(h)}^T} \B{\phi}^{(h)}
%\end{align*}
}
\lgcond{}
\end{itemize}

\end{frame}

%\begin{frame}{Multigrid Restriction}
%
%\begin{itemize}
%\item Multigrid restricts the residual equation on the fine grid to the coarse grid:
%\tlgcond{
%To do so, we need to obtain a suitable basis transformation from $\{\phi^{(h)}_i\}$ to $\{\phi^{(2h)}_i\}$,
%\begin{align*}
%\B V(\{\phi_{2i-1}^{(2h)}\}_{i=1}^{(n-1)/2},\{t_i\}_{i=1}^n) \B x^{(2h)} \cong  \B V(\{\phi_i\}_{i=1}^n,\{t_i\}_{i=1}^n)  \B x^{(h)}
%\end{align*}
%For linear finite elements $\B V(\{\phi_i^{(h)}\}_{i=1}^n,\{t_i\}_{i=1}^n)= \B I$ so we obtain,
%\begin{align*}
% \B V(\{\phi_{2i-1}^{(2h)}\}_{i=1}^{(n-1)/2},\{t_i\}_{i=1}^n)\B x^{(2h)} =  \B x^{(h)}.
%\end{align*}
%Multigrid requires values of the residual $\{r(t_{2i-1}\}_{i=1}^{(n-1)/2}$ on the coarse grid, whose coefficients $\B x^{(2h)}$ are then given by
%\begin{align*}
%\B r^{(2h)} = \underbrace{\B V(\{\phi_{i}^{(h)}\}_{i=1}^{n},\{t_{2i-1}\}_{i=1}^{(n-1)/2})}_{\B I}\B x^{(h)} &=  \B V(\{\phi_{2i-1}^{(2h)}\}_{i=1}^{(n-1)/2},\{t_i\}_{i=1}^n)\B x^{(2h)}.
%\end{align*}
%}
%\end{itemize}
%\end{frame}

\section{Fast Fourier Transform}

\begin{frame}{Discrete Fourier Transform}

\begin{itemize}
\item The solutions to hyperbolic PDEs like Poisson are wave-like and take on simple representations in the frequency basis, both for continuous and discretized equations. We define the \coloremph{discrete Fourier transform} using
$$
\omega_{(n)} = \cos(2\pi/n) - i\sin(2\pi/n) = e^{-2\pi i/n}.
$$
\lgcond{
The DFT matrix $\B F\in\mathbb{R}^{n\times n}$ is given by
$f_{ij}=\omega_{(n)}^{ij}$,
$$
\B{F} =
\left[ \begin{matrix}
1 & 1 & 1 & 1 \cr
1 & \omega_{(4)}^{1} & \omega_{(4)}^{2} & \omega_{(4)}^{3} \cr
1 & \omega_{(4)}^{2} & \omega_{(4)}^{4} & \omega_{(4)}^{6} \cr
1 & \omega_{(4)}^{3} & \omega_{(4)}^{6} & \omega_{(4)}^{9} \end{matrix}
\right]$$

\begin{itemize}
\item it is complex and symmetric (not Hermitian),
\item it is unitary modulo scaling $\B F^{*} = n\B F^{-1}$.
\end{itemize}
The discrete Fourier transform of vector $\B v$ is $\B F \B v$.
}
\lgcond{}

\end{itemize}

\end{frame}

\begin{frame}{Fast Fourier Transform (FFT)}

\begin{itemize}
\item Consider $\B b=\B{F}\B a$, we have
\[\forall j\in[0,n-1] \quad b_j=\sum_{k=0}^{n-1}\omega_{(n)}^{jk}a_k,\]
the FFT computes this recursively via 2 FFTs of dimension $n/2$, using $\omega_{(n/2)}=\omega_{(n)}^2$,
\lgcond{
\begin{align*}
%\forall j\in[0,n-1] \quad
b_j&=\sum_{k=0}^{n/2-1}\omega_{(n)}^{j(2k)}a_{2k}+\sum_{k=0}^{n/2-1}\omega_{(n)}^{j(2k+1)}a_{2k+1} \\
&=\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{jk}a_{2k}+\omega_{(n)}^j\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{jk}a_{2k+1}
\end{align*}
}
\lgcond{}
%, so
%\[\forall j\in[1,n/2] \quad b(2j)=\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{jk}a_k\]
\end{itemize}
\end{frame}

\begin{frame}{Fast Fourier Transform Derivation}

\begin{itemize}
\item The FFT leverages similarity between the first and second half of the output,
\begin{align*}
b_j&=\underbrace{\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{jk}a_{2k}}_{u_j}+\omega_{(n)}^j\underbrace{\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{jk}a_{2k+1}}_{v_j}
\end{align*}
corresponds closely to the entry shifted by $n/2$,
\begin{align*}
 b_{j+n/2}&=\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{(j+n/2)k}a_{2k}+\omega_{(n)}^{j+n/2}\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{(j+n/2)k}a_{2k+1}
\end{align*}
\lgcond{
Now $\omega_{(n/2)}^{(j+n/2)k}=\omega_{(n/2)}^{jk}$ since $(\omega_{(n/2)}^{n/2})^k=1^k=1$ and using $\omega_{(n)}^{n/2}=-1$,
%$\forall j\in[0,n/2-1]$
\begin{align*}
b_{j+n/2}
&=\underbrace{\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{jk}a_{2k}}_{u_j}-\omega_{(n)}^{j}\underbrace{\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{jk}a_{2k+1}}_{v_j}
\end{align*}
}
\end{itemize}
\end{frame}

\begin{frame}{FFT Algorithm Summary}

%Each of these two summation can be done recursively with an FFT %of dimension $n/2$
% which can then be combined to compute the upper and lower halves of $b$
\begin{itemize}
\item Let vectors $\B u$ and $\B v$ be two recursive FFTs, $\forall j\in[0,n/2-1]$
\begin{align*}
u_j&=\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{jk}a_{2k}, \quad
v_j=\sum_{k=0}^{n/2-1}\omega_{(n/2)}^{jk}a_{2k+1}
\end{align*}
\lgcond{
\begin{itemize}
%\item We can make these two recursive calls simultaneously and without any work
\item Given $\B u$ and $\B v$ scale using "twiddle factors" $z_j=\omega_{(n)}^j\cdot v_j$
\item Then it suffices to combine the vectors as follows
\(\B b=\begin{bmatrix} \B u+\B z \\ \B u-\B z\end{bmatrix}\)
%\item This recombination is an FFT of dimension 2
%\[\B b=\begin{bmatrix} \B{b}_1 \\ \B{b}_2 \end{bmatrix} = \vc{\begin{bmatrix} \B b_1 & \B b_2\end{bmatrix}} =
%\vcop\bigg(\begin{bmatrix} \B u & \B z \end{bmatrix} \underbrace{\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}}_{\B{F_4}[0:2,0:2]}\bigg) %=
%%\vc{\begin{bmatrix} \B u & \B z \end{bmatrix} \B{F_2}}
%\]
%\vspace{-.2in}
%\item Radix-$r$ algorithm for any $\B{A}\in\mathbb{R}^{n/r\times r}$
%\[
%%\vc{\B{B}}=
%\B{F}_{n}\vc{\B{A}} = \vc{\big([\B{F}_{n}[0:r,0:r] \odot (\B{F}_r \B{A})]\B{F}_{n/r})^T}\]
%%\ \text{ if and only if } \ \B{B} = \B{F}_{n/r} \B{A} \B{F}_r\]
\end{itemize}
}
\item The FFT has $O(n \log n)$ cost complexity:

\lgcond{
There are two recursive calls of dimension $n/2$ and $O(n)$ work for application to twiddle factors and final summation, thus
$$T(n)=2T(n)+O(n)=O(n\log n).$$
}
\end{itemize}
\end{frame}

\begin{frame}{Applications of the FFT}
\begin{itemize}
\item We can rapidly multiply degree $n$ polynomials by considering their values $\omega_{(2n-1)}^i$ for $i\in\{0,\ldots,2n-1\}$
\lgcond{
\[p_c(\omega_{(2n-1)}^i)=p_a(\omega_{(2n-1)}^i)p_b(\omega_{(2n-1)}^i)\]
\begin{itemize}
\item The product of coefficients of $p_a,p_b$ with Vandermonde matrix $v_{ij}=(\omega_{(2n-1)}^{i})^{j}$, which is the DFT matrix, gives values of polynomials at $2n-1$ nodes.
\mitem Interpolation to compute coefficients of $p_c$ from the products of values of $p_a$ and $p_b$ at those nodes is multiplication by the inverted DFT matrix and is exact since $p_c$ is degree $2n-2$.
\end{itemize}
}

\item More generally the DFT can be used to solve any Toeplitz linear system (convolution):
\lgcond{
\begin{itemize}
\item
A standard convolution has the form,
\(\forall k\in [0,n-1] \quad c_k = \sum_{j=0}^k a_jb_{k-j}.\)
\mitem Convolution is equivalent to multiplications of polynomials with degree $n/2-1$ and coefficients  $\Vec a$ and $\Vec b$, where
%\[p_a(x) = \sum_{k=0}^{n/2-1} a_kx^k,\quad
%p_b(x) = \sum_{k=0}^{n/2-1} a_kx^k\]
the convolution computes the coefficients $\Vec c$ of the product of the two polynomials.
%\[p_c(x) = p_a(x)p_b(x) = \sum_{k=0}^{n-1} c_kx^k\]
\end{itemize}
}
\end{itemize}
\end{frame}

\begin{frame}{Convolution via DFT}

\begin{itemize}
\item The Fourier transform method for computing a convolution is given by
\begin{align*}
c_k &= %\sum_s D^{-1}_{ks} \Big(\sum_j D_{sj} a_j\Big)\Big(\sum_t D_{st} b_t\Big)
  \frac 1n \sum_s \omega^{-ks}_{(n)} \Big(\sum_j \omega^{sj}_{(n)} a_j\Big)\Big(\sum_t \omega_{(n)}^{st} b_t\Big)
\end{align*}
\lgcond{
\begin{itemize}
\item Rearrange the order of the summations to see what happens to every product of $a$ and $b$
\begin{align*}
c_k &%= \frac 1n \sum_s\sum_j\sum_t \omega^{-ks}_{(n)} \omega_{(n)}^{sj}\omega^{st}_{(n)} a_j b_t
 = \frac 1n \sum_s\sum_j\sum_t \omega^{(j+t-k)s}_{(n)} a_j b_t
\end{align*}
\item For any $u=j+t-k\neq 0$, we observe $\sum_s(\omega_{(n)}^u)^s=0$ %, as for $\Mat F\Mat F^{-1}$
\mitem When $j+t-k=0$ the products $\omega^{(s+t-j)k}_{(n)}=1$, so there are $n$ nonzero terms $a_jb_{k-j}$ in the summation
\end{itemize}
}
\lgcond{}
\end{itemize}
\end{frame}

\begin{frame}{Solving Numerical PDEs with the FFT}

\urcornerlinkdemo{12-fft}{Fast Fourier Transform}

\begin{itemize}
\item 1D finite-difference schemes on a regular grid correspond to convolutions:

\mdcond{
1D model problem is simply convolution with vector $[1, -2, 1]$.
}

\item For the 1D Poisson model problem, the eigenvectors of $\B T$ corresponds to the imaginary part of a minor of a $2(n+1)$-dimensional DFT matrix:

\mdcond{
\begin{itemize}
\item
In particular, $\B T =\B X \B D \B X^{-1}$ where $x_{ij}$ is the imaginary part of $f_{i+1,j+1}$ with $\B X\in \mathbb{R}^{n\times n}$ and $\B F \in \mathbb{R}^{2(n+1)\times 2(n+1)}$.
\mitem
Consequently, $\B T$ can be diagonalized and the overall system solved by FFT with $O(n\log n)$ cost.
\end{itemize}
}

\item Multidimensional Poisson can be handled with multidimensional FFT:

\mdcond{
For example 2D FFT (1D FFT of each row then 1D FFT of each column) suffices to solve the 2D Poisson problem.
}

\end{itemize}

\end{frame}

%\end{document}


% !TEX options=--shell-escape
\documentclass [12pt]{article}

\usepackage {amsmath}
\usepackage {amsthm}
\usepackage {amssymb}
\usepackage {graphicx}
\usepackage {float}
\usepackage {multirow}
\usepackage {xcolor}
\usepackage {algorithmic}
\usepackage [ruled,vlined,commentsnumbered,titlenotnumbered]{algorithm2e} \usepackage {array}
\usepackage {booktabs}
\usepackage {url}
\usepackage {parskip}
\usepackage [margin=1in]{geometry}
\usepackage [T1]{fontenc}
\usepackage {cmbright}
\usepackage [many]{tcolorbox}
\usepackage [colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}
\usepackage {enumitem}
\usepackage {xparse}
\usepackage {verbatim}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{csquotes}
\usepackage[cache=false]{minted}
\usepackage{mdframed}
\newtheorem{theorem}{Theorem}

\BeforeBeginEnvironment{minted}{\begin{mdframed}}
\AfterEndEnvironment{minted}{\end{mdframed}}

\DeclareTColorBox {Solution}{}{breakable, title={Solution}}
\DeclareTColorBox {Solution*}{}{breakable, title={Solution (provided)}}
\DeclareTColorBox {Instruction}{}{boxrule=0pt, boxsep=0pt, left=0.5em, right=0.5em, top=0.5em, bottom=0.5em, arc=0pt, toprule=1pt, bottomrule=1pt}
\DeclareDocumentCommand {\Expecting }{+m}{\textbf {[We are expecting:} #1\textbf {]}}
\DeclareDocumentCommand {\Points }{m}{\textbf {(#1 pt.)}}

\begin {document}

{\LARGE \textbf {COMP 285 (NC A\&T, Spr `22)}\hfill \textbf {Homework 3} }
\vspace {1em}
\begin {Instruction}

\paragraph {Due.} Tuesday, February 9th, 2022 @ 11:59 PM!
\end {Instruction}

\vspace {1em}
\begin {Instruction} \paragraph {Homework Expectations:} Please see \href{https://www.comp285.ml/homework/#general-homework-information}{Homework}.
\end {Instruction}

\vspace {1em}
\begin {Instruction}

\paragraph {Exercises} The following questions are exercises. We encourage you to work with a group and discuss solutions to make sure you understand the material.

\paragraph {Points} This assignment is graded out of 100 points. However, you can get up to 120 points if you complete everything. These are not bonus points, but rather points to help make-up any parts you miss.

\end {Instruction}

\begin{centering}
\section*{Fun with Divide and Conquer, Sorting, and Medians}
\end{centering}

\begin{Instruction}

\paragraph{Written Problems} The following questions are to be submitted in written/typed form to gradescope.

\end{Instruction}

\section{Getting to Know You \Points{10}}

As part of this homework assignment, I'd like to get to know each of you.

Do the following:

\begin{itemize}
    \item \Points{5} Schedule a 10 minute conversation with me in the available slots \href{https://calendly.com/comp285-prof/get-to-know-you?month=2022-02}{here}.
    \item \Points{5} When you're done with the homework, complete this \href{https://forms.gle/KpC8RJ3gvkPgQPYY7}{form}.
\end{itemize}

\Expecting{You should have scheduled a meeting with me for an available slot and submit the form. All of this is tracked and this is how we'll confirm to give you credit for this question.}

\pagebreak
\section{Exercise: A faster MergeSort? \Points{10}}
Inspired by all of our discussion on recursion and sorting, Miah, Ryan, and Jordan get together to try and figure out a faster\footnote{We will not cover in class, but it has been proven that there is no deterministic algorithm faster than $\theta(n \log n)$ for sorting numbers using comparisons.} way to sort! After many attempts, they write down the following sorting function:

\vspace{2em}
\begin{minted}[linenos=true]{cpp}
// MergeSort algorithm!
std::vector<int> mergeSortN(const std::vector<int>& input) {
    const std::size_t n = input.size();
    if (n <= 1) {
        return input;
    }
    std::vector<std::vector<int>> sortedSubvectors = {};
    for (int i = 0; i < n; i++) {
        std::vector<int> temp = mergeSortN(
            {input.begin() + i, input.begin() + i + 1}
        );
        sortedSubvectors.push_back(temp);
    }
    std::vector<int> sortedA = {};
    for (const std::vector<int> subVector : sortedSubvectors) {
        sortedA = merge(sortedA, subVector);
    }
    return sortedA;
}
\end{minted}

\subsection{What is this doing!? \Points{5} }
In your own words, describe that the algorithm above is doing. This should not take more than a few sentences, but should highlight how this is different from the normal implementation of \texttt{MergeSort}.

\Expecting{A few sentences describing, in plain English, what the algorithm above does.}

\subsection{How fast is it?! \Points{5}}
Jordan claims this version of \texttt{MergeSort} is much faster than any versions we've seen before. This is his argument:

\begin{displayquote}
    This modified \texttt{MergeSort} splits the array into $n$ subproblems of size $O(1)$ immediately. We therefore don’t waste time with the `$log(n)$ levels’ worth of splitting. Additionally, in this modified sort, we’re calling \texttt{Merge} on a bunch of subvectors of size $1$. Each merge would therefore take time

    $$
        O(\text{size of subvector A}) + O(\text{size of subvector B}) = O(1) + O(1) = O(1)
    $$
    That's contant time per merge! Yay! We therefore have an algorithm that sorts the input array in $O(n)$ time - $n$ merges of $O(1)$ each!
\end{displayquote}

Sadly, Jordan's analysis is \textbf{wrong}. It was a valiant try, though. Let's help Miah, Ryan, and Jordan out! What mistake are they making in their analysis above, and what is the true runtime of their modified \texttt{MergeSort}.

\Expecting{An explanation (one sentence) that summarizes the mistake in the above reasoning and the correct runtime of \texttt{mergeSortN} along with an explanation (another sentence).}

\pagebreak
\section{Interview Practice: Finding Matching Elements \Points{10}}
Caleb is working on a ``search'' algorithm at Foogle. He is given an array \texttt{input} of $n$ integers that is sorted in ascending order and contains only unique elements. That is to say, if his array is
$$
a = [a_1, a_2, \cdots, a_{n-1}, a_n]
$$

then $a_1 < a_2 < \cdots < a_{n-1} < a_n$.

His project is to search the above array and find the element such that its index is equal to its value, or returns that such $i$ does not exist. That is to say, the algorithm finds $i$ such that $i = a_i$.

\subsection{A first proposal. \Points{5}}
\label{subsec:first_proposal}
Caleb proposes the following pseudo-code for an algorithm that solves the problem described above.

\begin{verbatim}
Algorithm:
    Input: A vector "input" sorted in ascending order with no duplicates.
    Outputs: The index i such that the i-th index contains the value i.

    n = len(input)
    for i = 0 to i = n - 1
        if (i == input[i]):
            return i

    throw "No such index exists"
\end{verbatim}

Describe in plain English how Caleb's algorithm works, and provide the big-Oh running time.

\Expecting{A few sentences describing how the algorithm Caleb wrote works in plain English, as well as $O(\cdots)$ where $\cdots$ is filled in with the proper running time.}

\subsection{Your Proposal. \Points{5}}
\label{subsec:second_proposal}
After looking at Caleb's proposed algorithm, you realize that he's not taking advantage of the fact that the input array is sorted. Give an $O(\log n)$ algorithm that solves the same problem (eg, it finds the index $i$ such that $i = a_i$ or returns that such $i$ does not exist). If there are multiple such $i$s, your algorithm can return any of them \footnote{Consider a divide-and-conquer approach, similar to binary search! If you'd like to refresh your memory on binary search, see this \href{https://www.youtube.com/watch?v=P3YID7liBug}{video}.}.

\Expecting{Pseudocode for you algorithm, similar to what Caleb wrote in Part \ref{subsec:first_proposal}, as well as a plain English description of what your algorithm is doing and why it is correct. You \textbf{do not} need to proof correcness.}

\pagebreak
\section{Interview Practice: Finding Medians \Points{30}}
You are given two arrays, each of length $n$. Your goal is to find the median of all elements of the two arrays.

\subsection{Unsorted Arrays \Points{10}}
If the arrays \textbf{are not sorted}, give a $\Theta(n \log n)$ algorithm that returns the median of the combined arrays.

\Expecting {A short English description of your approach, the pseudocode (similar to Part \ref{subsec:second_proposal}), and an explanation of your algorithm's runtime.}.

\subsection{Unsorted Arrays But Faster \Points{10}}

If the arrays \textbf{are not sorted}, give an $\Theta(n)$ algorithm that returns the median of the combined arrays\footnote{Hint: You can use `Select(A, k)' directly, without explanation, since this was given in lecture}.

\Expecting {A short English description of your approach, the pseudocode (similar to Part \ref{subsec:second_proposal}), and an explanation of your algorithm's runtime.}.

\subsection{Sorted Arrays \Points{10}}

If the arrays \textbf{are sorted}, give an $O(\log n)$ algorithm that returns the median of the combined arrays. Here are a few hints to help you. Try to look at these only after you've given the problem a bit of thought on your own.

\begin{enumerate}[label=Hint \arabic*]
    \item You probably want to use divide-and-conquer.
    \item Calculating the median of a single sorted array can be done in $O(1)$ time (how?).
    \item If the biggest element in one array (eg, the last element) is smaller than the smallest element in another array (eg, the first element), you can also compute the median $O(1)$ time (how?).
    \item If you look at the medians of the two arrays, how does that help you split the problem into parts? What do you know if the median of the two arrays are equal? What if one is bigger than the other?
\end{enumerate}

\Expecting {A short English description of your approach, the pseudocode (similar to Part \ref{subsec:second_proposal}), and an explanation of your algorithm's runtime.}.

\pagebreak
\section {Exercise: Randomized Algorithms \Points {10}}
\label{sec:last}

In this exercise, we'll explore different types of randomized algorithms. We say that a randomized algorithm is a \textbf {Las Vegas algorithm} if it is always correct (that is, it returns the right answer with probability 1), but the running time is a random variable. We say that a randomized algorithm is a \textbf {Monte Carlo algorithm} if there is some probability that it is incorrect. For example, QuickSort (with a random pivot) is a Las Vegas algorithm, since it always returns a sorted array, but it might be slow if we get very unlucky.

We will visit the Majority Element problem to get more insight on randomized algorithms. The Majority Element problem is to find the element in a collection of elements that occurs at least half of the time. See Algorithm \ref{alg:isMajority} for how we would check if an element is the majority.

We will assume that such an element always exists.

\vspace{2em}
\begin{minted}[linenos=true]{cpp}
// Returns 2, because it occurs 4 times which is greater
// than 6/2 = 3.
findMajorityElement({2, 2, 2, 2, 3, 5});
// Returns 10, because it occurs 3 times which is greater
// than 5/2 = 2.5
findMajorityElement({10, 10, 10, 2, 4});
// Returns 4 because it occurs 3 times, which is greater
// than 4/2 = 2.
findMajorityElement({4, 4, 2, 4})
\end{minted}

\vspace{2em}
\begin{center}
\begin{tabular}{|c|p{3cm}|p{2cm}|p{2cm}|p{4cm}|}
\hline
Algorithm & Monte Carlo or Las Vegas? & Expected running time & Worst-case running time & Probability of returning a majority element \\
\hline
\textbf {Algorithm 1} & & & & \\
\hline
\textbf {Algorithm 2} & & & & \\
\hline
\textbf {Algorithm 3} & & & & \\
\hline
\end{tabular}
\end{center}

\Expecting {Your filled in-table. You may use asymptotic notation for the running times.} \newpage

\begin{algorithm}[H]
    \KwIn {A population $P$ of $n$ elements}
    \While {true} {
        Choose a random $p \in P$\;
        \If {\textsc {isMajority}($P$, $p$)}{
            \Return { $p$}\;
        }
    }
    \caption {\textsc {findMajorityElement1}}
\end{algorithm}

\begin{algorithm}[H]
    \KwIn {A population $P$ of $n$ elements}
    \For {100 iterations} {
        Choose a random $p \in P$\;
        \If {\textsc {isMajority}($P$, $p$)}{
            \Return { $p$ }\;
        }
    }
    \Return { $P[0]$\;}
    \caption {\textsc {findMajorityElement2}}
\end{algorithm}

\begin{algorithm}[H]
    \KwIn {A population $P$ of $n$ elements}
    Put the elements in $P$ in a random order.\;
    \tcc{Assume it takes time $\Theta (n)$ to put the $n$ elements in a random order}
    \For { $p \in P$ } {
        \If {\textsc {isMajority}($P$, $p$)}{
            \Return { $p$ }\;
        }
    }
    \caption {\textsc {findMajorityElement3}}
\end{algorithm}

\begin{algorithm}[H]
    \KwIn {A population $P$ of $n$ elements and a element $p \in P$}
    \KwOut {True if $p$ is a member of a majority species}
    count $\gets $ 0\;
    \For { $q \in P$ } {
        \If { $p = q$ }{
            count ++\;
        }
    }
    \If { count $> n/2$ }{
        \Return {True}\;
    } \Else {
        \Return {False}\;
    }
    \caption {\textsc {isMajority}}
    \label{alg:isMajority}
\end{algorithm}

\pagebreak
\begin{Instruction}

\paragraph{Coding Problems} The following questions are to be submitted as a ".zip" file on Gradescope.

\end{Instruction}

\section{Coding \Points{50}}
\Points{50} After completing the written portion of the assignment, you should submit to \href{https://www.gradescope.com/courses/350304}{Gradescope}.

You can get your starter code for the coding portion \href{https://replit.com/team/COMP285/HW3-Code}{here}.

Note that the starter code also include a few test cases you can run on repl.it. However, the full test suite is the one run on Gradescope.

Please reference the \texttt{README.md} included in your starter code for detailed instructions.

\section*{Submitting the Assignment}

This assignment is a combination of written and programming questions. Both portions of the assignment should be submitted through \href{https://www.gradescope.com/courses/350304}{Gradescope}.

The "Homework 3: Fun with Divide and Conquer, Sorting, and Medians" assignment is the written portion, for which you should submit a \textbf{typed} response to the non-coding questions (questions 1-\ref{sec:last}). Each response should clearly be marked with its corresponding number. You are free to use the provided templates, print the questions and write your answers, or to simply type your responses on a blank document (whatever works for you).

The "Homework 3: Coding" is the programming portion of the assignment. For this portion, download the ".zip" file from replit and upload this ".zip" file as your answer to \href{https://www.gradescope.com/courses/350304}{Gradescope}. You can upload the assignment as many times as you want.

\end {document}


%by Maarten Burger, Alexander Apers, and Jos Zuiderwijk
\chapter{Chapter 11. Soundness and Completeness}

\section*{11.7.1 Part A --- Doing}

In the following, I give both full, detailed answers to the questions
and an indication of the expectations I have for a good answer. Note
that my way of answering is very detailed and there might be many
different, equally valid ways of writing up the same result. Note that
the correctness of the answer is always a factor but by far not the
only one. If, as in 11.7.3, the correctness of the answer is one out
of 4 elements of a correct answer, just writing down the correct
answer can give at most 1/4th of the points.

Keep in mind that since this is your first formal course, I give you a
lot of leeway when it comes to precise, mathematical formulations, but
the elements of a good answer should always be there to get decent
points.

\begin{itemize}

\item[11.7.1.1] \emph{Long answer}: In order to determine all variable and
  quantifier occurrences, we first construct the stripped parsing
  tree for the formula:
  \begin{center}
    \Tree [.{$\forall x$}
             [.{$\exists y$}
                [.{$\to$}
                 [.{$R$}
                   [.{$x$} ]
                   [.{$y$} ]
                 ]
                 [.{$\forall x$}
                    [.{$\land$}
                         [.{$P$} [.$x$ ] ]
                         [.{$\exists y$} [.{$R$} [.$x$ ] [.$y$ ] ] ]
                    ]
                 ]
                ]
             ]
             ]
  \end{center}
  The following table contains the information which variable
  occurrence is bound by which quantifier occurrence:
    \begin{longtable}{c | c}
      Variable occurrence      & Quantifier occurrence that binds it\\\hline
      $((r,1,1,1,1), x)$       & $(r, \forall x)$\\
      $((r,1,1,1,2), y)$       & $((r,1), \exists y)$\\
      $((r,1,1,2,1,1,1), x)$   & $((r,1,1,2), \forall x)$\\
      $((r,1,1,2,1,2,1,1), x)$ & $((r,1,1,2), \forall x)$\\
      $((r,1,1,2,1,2,1,2), y)$ & $((r,1,1,2,1,2), \exists y)$
    \end{longtable}

    \emph{Elements of a good answer}:

    \begin{itemize}
    \item Proper naming of the occurrences.
    \item Clear statement which variable occurrence is bound by which
      quantifier occurrence.
    \item Correct answer.
    \item Fully formulated sentences.
    \end{itemize}

  \item[11.7.1.2] \emph{Full answer}:
    \begin{enumerate}
    \item $\forall x(V(x)\to L(x))$
    \item $\neg \forall x(L(x)\to \forall y(\neg L(y)\to I(x,y)))$
    \item $L(x)\land \neg V(x)\land \neg D(x)$
    \item $\exists x(\neg L(x)\land \neg D(x))$
    \end{enumerate}

    \emph{Elements of a good answer}:

    \begin{itemize}
    \item Formalizations are indeed formulas.
    \item Adequate formalizations.
    \item Recognizes $V(x)\to L(x)$ as the best formalization of ``only
      if''
    \item Recognizes ``he'' as an indefinite pronoun, i.e. free
      variable.
      \item Recognizes the subordinate clause indicated by the commas
        in (iv) to indicate an existential quantifier.
    \end{itemize}

   \item[11.7.1.3] \emph{Long answer}: We are asked to determine the value of $\llbracket
     (0+x)\cdot S(0)\rrbracket^\mathcal{M}_\alpha$ for the given model
     and assignment. Applying the recursive definition of the
     denotation of a term in a model under an assignment, we get the
     following calculation:
     \begin{align*}
       \llbracket (0+x)\cdot
       S(0)\rrbracket^\mathcal{M}_\alpha&=\llbracket
                                          0+x\rrbracket^\mathcal{M}_\alpha\cdot^\mathcal{M}
                                          \llbracket
                                          S(0)\rrbracket^\mathcal{M}_\alpha
       \\
       &=(\llbracket
         0\rrbracket^\mathcal{M}_\alpha+^\mathcal{M}\llbracket
         x\rrbracket^\mathcal{M}_\alpha)\cdot^\mathcal{M}
         S^\mathcal{M}(\llbracket 0\rrbracket^\mathcal{M}_\alpha)\\
                                        &=(0^\mathcal{M}+^\mathcal{M}\alpha(x))\cdot^\mathcal{M}S^\mathcal{M}(0^\mathcal{M})\\
                                        &=(1 +^\mathcal{M}3)\cdot ^\mathcal{M}S^\mathcal{M}(1)\\
                                        &= 5\cdot 3\\
                                        &=15
     \end{align*}

     \emph{Elements of a good answer}:

     \begin{itemize}
     \item Explains what's being done.
     \item Applies the recursive clauses in sufficient detail.
     \item Applies the functions from the model correctly.
     \item Gets the correct result.
     \end{itemize}

  \item[11.7.1.4] \emph{(Very) Long Answer}: We claim that
    $\mathcal{M},\alpha\vDash \forall x\forall y(R(x,y)\to R(f(y), f(x)))$.
    In order to determine what we need to show is, we observe the following
    \begin{itemize}
      \item[] $\mathcal{M},\alpha\vDash \forall
        x\forall y(R(x,y)\to R(f(y), f(x)))$
      \item[\emph{iff}] for all $d\in D^\mathcal{M}$,
        $\mathcal{M},\alpha[x\mapsto d]\vDash \forall y(R(x,y)\to
        R(f(y), f(x)))$
      \item[\emph{iff}] for all $d,d'\in D^\mathcal{M}$,
        $\mathcal{M},\alpha[x\mapsto d, y\mapsto d']\vDash R(x,y)\to
        R(f(y), f(x))$
    \end{itemize}
    Since there are only 2 elements in $D^\mathcal{M}=\{1,2\}$,
    there are 4 possible choices for $d$ and $d'$ to consider:
    \begin{itemize}
      \item $d=1, d'=1$
      \item $d=1, d'=2$
      \item $d=2, d'=1$
      \item $d=2, d'=2$
    \end{itemize}
    Since for all choices of $d$ and $d'$ other than $d=1, d'=2$, we
    have that $\mathcal{M},\alpha[x\mapsto d, y\mapsto d']\nvDash
    R(x,y)$, so we don't have to check anything else to see that $\mathcal{M},\alpha[x\mapsto d, y\mapsto d']\vDash R(x,y)\to
    R(f(y), f(x))$.

    For $d=1, d'=2$, we observe that:\[\llbracket
    f(x)\rrbracket^\mathcal{M}_{\alpha[x\mapsto d, y\mapsto
    d']}=f^\mathcal{M}(1)=2\]\[\llbracket
    f(y)\rrbracket^\mathcal{M}_{\alpha[x\mapsto d, y\mapsto
    d']}=f^\mathcal{M}(2)=1.\]
    But then, we have
    $\mathcal{M}, \alpha[x\mapsto d, y\mapsto d']\vDash R(f(y), f(x))$
    and so
    $\mathcal{M},\alpha[x\mapsto d, y\mapsto d']\vDash R(x,y)\to R(f(y), f(x))$.

      So, for each choice of $d,d'\in D^\mathcal{M}$, we have \[\mathcal{M},\alpha[x\mapsto d, y\mapsto d']\vDash R(x,y)\to
      R(f(y), f(x))\] and so \[\mathcal{M},\alpha\vDash \forall
      x\forall y(R(x,y)\to R(f(y), f(x)),\] as desired.

    \emph{Elements of a good answer}:

    \begin{itemize}
    \item Applies the clause for the universal quantifier.
    \item Considers all the values for the variables.
    \item Notices that the only interesting case is when the value of
      $x$ is 1 and the value of $y$ 2.
    \item Gives the correct answer.
    \item Explains reasoning.
    \item Is written in full, comprehensible sentences.
    \end{itemize}

  \item[11.7.1.5]

    \begin{enumerate}

      \item%
        By definition,
        $\exists x(P(x)\land x=c),\forall x(P(x)\to Q(x))\vdash  Q(c)$
        iff the tableau for
        $\{ \exists x(P(x)\land x=c),\forall x(P(x)\to Q(x)),\neg Q(c)\}$
        is closed.
        Here is the tableau:

        \begin{center}
          \begin{prooftree}
            {%
              line numbering=false,
              for tree={s sep'=10mm},
              single branches=true,
              close with=\xmark
            }
            [{\exists x(P(x)\land x=c)}, grouped
                [{\forall x(P(x)\to Q(x))}, grouped
                    [{\neg Q(c)}, grouped
                        [{P(p)\land p=c}
                            [{P(p)}
                                [{p=c}
                                    [{P(p)\to Q(p)}
                                        [{\neg P(p)}, close ]
                                        [{Q(p)}
                                            [{\neg Q(p)}, close]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
          \end{prooftree}
        \end{center}
        Since the tableau is closed,
        we can infer the conclusion from the premises as claimed.

        \emph{Elements of a good answer}:
        \begin{itemize}
          \item%
            Explains why the tableau is done.
          \item%
            Makes a tableau for
            $\Gamma\cup\{\neg\phi\}$
            not for $\Gamma\cup\{\phi\}$.
          \item%
            Applies all rules correctly.
          \item%
            Recognizes the correct application of the identity rule to close the second branch.
          \item%
            Gets the correct answer.
        \end{itemize}

\item By definition, $P(c)\lor (P(c)\land Q(c)), \forall x(Q(x)\to
  \neg P(c))\vdash \neg P(c)$ iff the tableau for $\{P(c)\lor (P(c)\land Q(c)), \forall x(Q(x)\to
  \neg P(c)), \neg\neg P(c)\}$ is closed. Here is the tableau:

  \begin{center}
  \begin{prooftree}
{
line numbering=false,
for tree={s sep'=10mm},
single branches=true,
close with=\xmark
}
[{P(c)\lor (P(c)\land Q(c))}, grouped
     [{\forall x(Q(x)\to \neg
  P(x)}, grouped
          [{\neg\neg P(c)}, grouped
                 [{P(c)}
                     [{P(c)}
                          [{Q(c)\to \neg P(c)}
                               [{\neg Q(c)}]
                               [{\neg P(c)}, close]
                          ]
                     ]
                     [{P(c)\land Q(c)}
                          [{P(c)}
                              [{Q(c)}
                                   [{Q(c)\to \neg P(c)}
                                     [{\neg Q(c)}, close]
                                     [{\neg P(c)}, close]
                                   ]
                              ]
                         ]
                     ]
                 ]
          ]
     ]
]
\end{prooftree}
\end{center}
Since the tableau is open, we cannot infer the conclusion from the
premises, as claimed. The associated model of the only open branch is
given by the following specification:
\begin{itemize}
   \item $D^\mathcal{M}=\{c\}$
   \item $c^\mathcal{M}=c$
   \item $P^\mathcal{M}=\{c\}$
   \item $Q^\mathcal{M}=\emptyset$
   \end{itemize}

   \emph{Elements of a good answer}:

   \begin{itemize}
   \item  Explains why the tableau is done.
  \item Makes a tableau for $\Gamma\cup\{\neg\phi\}$ not for
    $\Gamma\cup\{\phi\}$.
  \item Applies all rules correctly.
    \item Makes a complete tableau (i.e. applies \emph{all} possible
      rules).
  \item Gets the correct tableau.
   \item Specifies the associated model completely (including the
     interpretation of $c$ and $Q$).
    \item Gets the correct model.
   \end{itemize}

    \end{enumerate}

    \item[11.7.1.6] \emph{Long answer}: We're asked to determine whether
      the following
      inference is valid:
      \begin{itemize}
      \item The ball is round, and everything round comes from
        Mars. So, the ball comes from Mars.
      \end{itemize}
      In order to determine the validity of the argument, we first
      formalize it. We make use of the following translation key:
      \begin{center}
        \begin{tabular}[!h]{c c c}
          $b$   & : & the ball\\
          $R^1$ & : & \dots is round\\
          $M^1$ & : & \dots comes from Mars
        \end{tabular}
      \end{center}
      We obtain \[R(b), \forall x(R(x)\to M(x))\therefore M(b)\]

      I claim that this inference is valid, i.e. \[R(b), \forall
        x(R(x)\to M(x))\vDash M(b)\]

      In order to show that I'm making use of the tableau method. We
      know that $R(b), \forall
        x(R(x)\to M(x))\vdash M(b)$ iff the tableau for $\{R(b), \forall
        x(R(x)\to M(x)),\neg  M(b)\}$ is closed. Here is that tableau:

        \begin{center}
  \begin{prooftree}
{
line numbering=false,
for tree={s sep'=10mm},
single branches=true,
close with=\xmark
}
[{R(b)}, grouped
[{\forall x(R(x)\to M(x))}, grouped
[\neg M(b), grouped
[{R(b)\to M(b)}
    [{\neg R(b)}, close]
    [{M(b)}, close]
]
]
]
]
\end{prooftree}
\end{center}
Since the tableau is closed, we can infer that \[R(b), \forall
        x(R(x)\to M(x))\vdash M(b).\] By the soundness theorem, our
        claim that \[R(b), \forall
        x(R(x)\to M(x))\vDash M(b)\] follows from this.

        We have now shown that the formal inference \[R(b), \forall
        x(R(x)\to M(x))\therefore M(b)\] is valid. Since this formal
        inference is a formalization of the natural language inference
        we started with, we can infer that the natural language
        inference is valid, too.

        \emph{Elements of a good answer}:

        \begin{itemize}
        \item Explains what is done.
        \item Formalizes the inference.
        \item Gets a decent formalization.
        \item Checks whether the inferences entail the conclusion with
          a suitable method (tableau or semantics).
        \item Applies that method correctly.
        \item Transfers the results back to the natural language
          inference.
        \item Gets the correct result.
        \end{itemize}

\end{itemize}

\section*{11.7.2 Part B --- Proving}

      Below, I provide a proof for each of the claimed
      theorems. Please keep in mind that, as I said in the last
      lecture, if a mathematical claim is provable, then there is
      always more than one proof of it. This means, the proofs I
      provide are not the only possible proofs. The merit of the answers I formulate below is that they might give you a better
      idea of what I expect a good answer to look like. Also keep in
      mind that my answer are always as explicit as possible, and I
      don't necessarily expect the same level of attention to detail
      from you.

      The
      \emph{elements of a good answer} are the same in every case:
      \begin{itemize}
      \item Recognizes correctly what needs to be shown.
      \item Explains each reasoning step, doesn't have gaps in the
        argumentation.
      \item Uses correct reasoning, doesn't commit fallacies.
      \item Applies the definitions correctly.
      \item Is written in full, grammatical English/Dutch sentences.
      \item Obtains the correct result.
      \end{itemize}

      Here is a (non-exhaustive) list of marking categories that we
      use:
      \begin{longtable}{c | l}
		Abbreviation & Mistake \\
		\hline
		\lightning & Error/mistake (generic)\\
		Df. & Incorrect or imprecise definition \\
		Q\textbf{?} & Question not read correctly\\
		$\not\Rightarrow$ & Non-sequitur, reasoning mistake\\
		$\neq$ & Calculation mistake \\
		$\qedsymbol$? & QED missing, reasoning incomplete\\
		\textbf{x}? & Undeclared variables\\
		$\Rightarrow$\textbf{?} & Right-to-left direction missing \\
		$\Leftarrow$\textbf{?} & Left-to-right direction missing \\
		$\underline{\lor}$ & Distinction by cases not exhaustive\\
		$abc$ & Write complete sentences.\\
		{[squiggles]} & No (unexplained)  paintings!
		\end{longtable}

        \begin{itemize}
                \item[11.7.2.1] We're asked to show that if $\phi$ is an
                  open formula with $y$ as its only free variable,
                  then $\forall x\phi$ is also an open formula
                  (i.e. not a sentence). We
                  show this claim by showing that for every free
                  occurrence of $y$ in $\phi$ there will be a
                  corresponding free occurrence of $y$ in $\forall
                  x\phi$. Since there are free occurrences of $y$ in
                  $\phi$, from this the claim follows.

                  So, consider a free occurrence of $y$ in
                  $\phi$. Clearly, there is a corresponding occurrence
                  of $y$ in $\forall x\phi$. What remains
                  to be shown is that the occurrence is free. Suppose,
                  for indirect proof, that it's not. This would mean
                  that there's a quantifier occurrence of the form
                  $Qy$ in $\forall x\phi$, which binds the occurrence
                  of $y$. That quantifier occurrence cannot have a
                  corresponding occurrence in $\phi$, because then the
                  occurrence of $y$ in $\phi$ would be bound, contrary
                  to our assumption. But the only quantifier
                  occurrence that's in $\forall x\phi$ without a
                  corresponding occurrence in $\phi$ is $(r,\forall
                  x)$. But this occurrence cannot bind any occurrence
                  of $y$ since $x\neq y$. Hence, $y$ needs to be free
                  in $\forall y\phi$, as desired.%$

                  \emph{Alternative strategy (way more complicated)}:
                  Using induction on formulas.

                  \item[11.7.2.2] We're asked to show that for all $\Gamma$, we
                    have $\Gamma\vdash P(c)\lor \neg P(c)$. We know,
                    by definition, that this is the case iff the
                    tableau for $\Gamma\cup\{\neg (P(c)\lor \neg
                    P(c))\}$ is closed. But we can infer that this
                    tableau is closed even without knowing what the
                    members of $\Gamma$ are. To see this, note that
                    the initial list consists in $\Gamma\cup\{\neg (P(c)\lor \neg
                    P(c))\}$, so we can always close the tableau as follows:
                    \begin{center}
  \begin{prooftree}
{
line numbering=false,
for tree={s sep'=10mm},
single branches=true,
close with=\xmark
}
[{\Gamma}, grouped
[{\neg (P(c)\lor\neg P(c)}, grouped
[{\neg P(c)}
[{\neg\neg P(c)}
[{P(c)}, close
]
]
]
]
]
\end{prooftree}
\end{center}
Hence the tableau is always closed, which is what we needed to show.

            \emph{Alternative strategy}: Semantically show that
                $\Gamma\vDash P(c)\lor\neg P(c)$ and then use
                completeness to infer the result.

          \item[11.7.2.3] We aim to show that
            $\forall x(\phi\to\psi)\vDash\neg \exists x(\phi\land
            \neg\psi)$.
            By definition, we know that
            $\forall x(\phi\to\psi)\vDash\neg \exists x(\phi\land \neg\psi)$
            iff for all models $\mathcal{M}$,
            we have that if
            $\mathcal{M},\alpha\vDash \forall x(\phi\to\psi)$, then
            $\mathcal{M},\alpha\vDash\neg \exists x(\phi\land\neg\psi)$
            (for some arbitrary assignment $\alpha$).
            So, let $\mathcal{M}$ be an
            arbitrary model and suppose that
            $\mathcal{M},\alpha\vDash\forall x(\phi\to\psi)$.
            This means, by definition, that for all
            $d\in D^\mathcal{M}$ we have
            $\mathcal{M},\alpha[x\mapsto d]\vDash
            \phi\to\psi$.
            From this, we need to derive that
            $\mathcal{M},\alpha\vDash\neg \exists x
            (\phi\land\neg\psi)$.
            We do this indirectly.
            Suppose
            that $\mathcal{M},\alpha\nvDash\neg \exists x
            (\phi\land\neg\psi)$.
            It follows that  $\mathcal{M},\alpha\vDash \exists x
            (\phi\land\neg\psi)$.
            This means, by definition,
            that there must be a $d\in D^\mathcal{M}$ such that
            $\mathcal{M},\alpha[x\mapsto d]\vDash
            \phi\land\neg\psi$.
            From this it would follow
            that $\mathcal{M},\alpha[x\mapsto d]\vDash
            \phi$ and $\mathcal{M},\alpha[x\mapsto
            d]\nvDash\psi$, and so $\mathcal{M},\alpha[x\mapsto d]\nvDash
            \phi\to\psi$.
            But this would contradict our
            assumption that  $\mathcal{M},\alpha[x\mapsto d]\vDash
            \phi\to\psi$ for all $d\in D^\mathcal{M}$.
            So, by
            indirect proof, we can infer that  $\mathcal{M},\alpha\vDash\neg
            \exists x(\phi\land\neg\psi)$, as desired.

                  \item[11.7.2.4] We want to find a model
                    $\mathcal{M}^+$ that makes
                    $\forall x\exists yR(x,y)$ true and a model
                    $\mathcal{M}^-$ that makes the formula
                    false. There are different ways in which
                    we can achieve this, for example via tableau. You
                    know how that works, so I provide an answer that
                    I found by thinking about what needs to be the
                    case for the formula to be true/false

                    First, consider the model $\mathcal{M}^+$ given by:
                    \begin{itemize}
                    \item $D^{\mathcal{M}^+}=\{\ast\}$
                    \item $R^{\mathcal{M}^+}=\{(\ast,\ast)\}$
                    \end{itemize}
                    I claim that we have $\mathcal{M}^+,\alpha\vDash
                    \forall x\exists yR(x,y)$. To see this, remember
                    that $\mathcal{M}^+,\alpha\vDash
                    \forall x\exists yR(x,y)$ iff for all
                    $d\in D^{\mathcal{M}^+}$, we have
                    $\mathcal{M}^+,\alpha[x\mapsto d]\vDash
                    \exists yR(x,y)$. And we have $\mathcal{M}^+,\alpha[x\mapsto d]\vDash
                    \exists yR(x,y)$ iff for some $d'\in
                    D^{\mathcal{M}^+}$, we have
                    $\mathcal{M}^+,\alpha[x\mapsto d, y\mapsto d']\vDash
                    R(x,y)$. So, we have that $\mathcal{M}^+,\alpha\vDash
                    \forall x\exists yR(x,y)$ iff for all $d\in
                    D^{\mathcal{M}^+}$ there exists a $d'\in
                    D^{\mathcal{M}^+}$ such that  $\mathcal{M}^+,\alpha[x\mapsto d, y\mapsto d']\vDash
                    R(x,y)$. But there is only one element in
                    $D^{\mathcal{M}^+}$, namely $\ast$. And for
                    $d=\ast$, we can easily find a $d'$ such that $\mathcal{M}^+,\alpha[x\mapsto d, y\mapsto d']\vDash
                    R(x,y)$, namely $d'=\ast$. To see this, just note
                    that $\mathcal{M}^+,\alpha[x\mapsto \ast, y\mapsto \ast]\vDash
                    R(x,y)$ since
                    $(\ast, \ast)\in R^\mathcal{M}$. So, $\mathcal{M}^+,\alpha\vDash
                    \forall x\exists yR(x,y)$, as desired.

                    Now, consider the model $\mathcal{M}^-$ given by:
                    \begin{itemize}
                    \item $D^{\mathcal{M}^+}=\{\ast\}$
                    \item $R^{\mathcal{M}^+}=\emptyset$
                    \end{itemize}
                   As in the case of $\mathcal{M}^+$, we can infer
                   that $\mathcal{M}^-,\alpha\vDash
                    \forall x\exists yR(x,y)$ iff for all $d\in
                    D^{\mathcal{M}^-}$ there exists a $d'\in
                    D^{\mathcal{M}^-}$ such that  $\mathcal{M}^-,\alpha[x\mapsto d, y\mapsto d']\vDash
                    R(x,y)$. Again, there is only one element in
                    $D^{\mathcal{M}^-}$, namely $\ast$. But for
                    $d=\ast$, there exists no $d'$ such that $\mathcal{M}^-,\alpha[x\mapsto d, y\mapsto d']\vDash
                    R(x,y)$. For the only possible $d'$ would $\ast$
                    itself and we have $\mathcal{M}^-,\alpha[x\mapsto \ast, y\mapsto \ast]\nvDash
                    R(x,y)$ since $R^{\mathcal{M}-}=\emptyset$. So  $\mathcal{M}^-,\alpha\nvDash
                    \forall x\exists yR(x,y)$, as desired.

                    \item[11.7.2.5]We need to show that if $\Gamma\vDash
                      c\neq c$, then $\Gamma$ is unsatisfiable. By
                      definition $\Gamma$ is satisfiable iff there is
                      a model $\mathcal{M}$ and assignment $\alpha$
                      such that $\mathcal{M},\alpha\vDash \phi$ for
                      all $\phi\in\Gamma$. So, assume that $\Gamma\vDash
                      c\neq c$. We derive that $\Gamma$ is
                      unsatisfiable using indirect proof. So suppose
                      that $\Gamma$ is satisfiable, that is there is
                      a model $\mathcal{M}$ and assignment $\alpha$
                      such that $\mathcal{M},\alpha\vDash \phi$ for
                      all $\phi\in\Gamma$. Since $\mathcal{M},\alpha\vDash \phi$ for
                      all $\phi\in\Gamma$ and $\Gamma\vDash c\neq c$,
                      it follows that $\mathcal{M},\alpha\vDash c\neq
                      c$. But that would entail that
                      $c^\mathcal{M}\neq c^\mathcal{M}$, which is
                      impossible. So, there is no such $\mathcal{M}$
                      and $\alpha$ and $\Gamma$ is therefore unsatisfiable.

                  \item[11.7.2.6] We're asked to prove that for all
                    terms $s$ and $t$ we have, in the given model
                    $\mathcal{M}$ and under the given assignment
                    $\alpha$, that $\llbracket
                    s\rrbracket^\mathcal{M}_\alpha=\llbracket
                    t\rrbracket^\mathcal{M}_\alpha$. In order to prove
                    this fact, we show that for all $t$ we have $\llbracket
                    t\rrbracket^\mathcal{M}_\alpha=a^\mathcal{M}$. From
                    this, the claim follows immediately since then we
                    have: \[\llbracket
                    s\rrbracket^\mathcal{M}_\alpha=a^\mathcal{M}=\llbracket
                    t\rrbracket^\mathcal{M}_\alpha\]

                    So, we want to prove by induction that for all $t$ we have $\llbracket
                    t\rrbracket^\mathcal{M}_\alpha=a^\mathcal{M}$. We
                    have two base cases: (i) either $t$ is a constant
                    or (ii) $t$ a variable. If (i) $t$ is a constant, then
                    $t=a$, since $a$ is the only constant of
                    $\mathcal{S}$. And trivially, if $t=a$,  we have
                    $\llbracket t\rrbracket^\mathcal{M}=\llbracket
                    a\rrbracket^\mathcal{M}=a^\mathcal{M}$. If (ii)
                    $t$ is a variable $x\in \mathcal{V}$, then
                    $\llbracket t\rrbracket^\mathcal{M}=\llbracket
                    x\rrbracket^\mathcal{M}=\alpha(x)=a^\mathcal{M}$,
                    as desired.

                    For the induction step, assume the induction
                    hypothesis that $\llbracket
                    t\rrbracket^\mathcal{M}_\alpha=a^\mathcal{M}$. We
                    need to derive from this that $\llbracket
                    f(t)\rrbracket^\mathcal{M}_\alpha=a^\mathcal{M}$,
                    as well. To see this, we can reason as follows:
                    \[\llbracket
                    f(t)\rrbracket^\mathcal{M}_\alpha=f^\mathcal{M}(\llbracket
                    t\rrbracket^\mathcal{M}_\alpha)=f^\mathcal{M}(a^\mathcal{M})=a^\mathcal{M}\]

                  So, using the principle of induction on terms, we
                  have seen that  $\llbracket
                    t\rrbracket^\mathcal{M}_\alpha=a^\mathcal{M}$ for
                    all terms $t$, from which our main claim follows
                    as explained before.

                  \item[11.7.2.7] We're asked to prove by induction that
                    if $\phi$ is an open formula with $x$ as its only
                    free variable, then $(\phi)[x:=c]$ where $c$ is a
                    constant is a closed formula, i.e. a sentence.

                    For the base case, we need to consider two
                    situations: (i) $\phi$ is of the form $R(t_1,
                    \mathellipsis, x, \mathellipsis, t_n)$ where the
                    $t_i$ are ground terms or (ii)
                    $\phi$ is of the form $t=x$ or $x=t$ where $t$ is
                    a ground term. In case (i),
                    we simply observe that since each $t_i$ is a
                    ground term and thus doesn't contain $x$, we have $(R(t_1,
                    \mathellipsis, x, \mathellipsis,
                    t_n))[x:=c]=R(t_1, \mathellipsis, c,\mathellipsis,
                    t_n)$. Since all of the $t_i$'s are ground-terms
                    and $c$ is a constant, there is no free variable
                    in this formula and the claim holds. In case (ii),
                    we only consider the situation where $\phi$ is of
                    the form $t=x$ since $x=t$ is completely
                    analogous. We simply note that  since $t$ is a
                    ground term and thus doesn't contain $x$, we have
                    $(t=x)[x:=c]=t=c$. And since, again, $t$ is a
                    ground term and $c$ a constant, $t=c$ contains no
                    variables at all and is thus closed.

                    We go through the induction steps one by one:

                    \begin{itemize}
                    \item Assume the induction hypothesis that if
                      $\phi$ has only $x$ free, then $(\phi)[x:=c]$ is a
                    sentence. We need to derive that then also if
                    $\neg\phi$ contains only $x$ free, then
                    $(\neg\phi)[x:=c]$ is a sentence. But suppose that
                    $\neg\phi$ contains only $x$ free. We have by
                    definition that $(\neg\phi)[x:=c]=\neg
                    (\phi)[x:=c]$. And since if $\neg \phi$ contains
                    only $x$ free, then also $\phi$ can only contain
                    $x$ free. Hence $(\phi)[x:=c]$ is a sentence by
                    the induction hypothesis and so $\neg(\phi)[x:=c]$
                    is also sentence.

                    \item Assume the induction hypotheses that (a) if
                      $\phi$ has only $x$ free, then $(\phi)[x:=c]$ is a
                    sentence and that (b) if
                      $\psi$ has only $x$ free, then $(\psi)[x:=c]$ is a
                    sentence. Now consider $\phi\circ\psi$ with only
                    $x$ free for
                    $\circ=\land,\lor,\to,\leftrightarrow$. We know that
                    $(\phi\circ\psi)[x:=c]=(\phi)[x:=c]\circ
                    (\psi)[x:=c]$ by definition. Now if
                    $\phi\circ\psi$ has only $x$ free, each of $\phi$
                    and $\psi$ can only have $x$ free. So, by the
                    induction hypotheses (a) and (b), we have that
                    $(\phi)[x:=c]$ and $(\psi)[x:=c]$ are both
                    sentences. But if $(\phi)[x:=c]$ and
                    $(\psi)[x:=c]$, then
                    $(\phi)[x:=c]\circ(\psi)[x:=c]$ is a sentence,
                    too, as desired.

                    \item Assume the induction hypothesis that if
                      $\phi$ has only $x$ free, then $(\phi)[x:=c]$ is a
                    sentence. Consider $Qy\phi$ with only $x$ free for
                    $Q=\forall,\exists$. Note that if $Qy\phi$
                    contains $x$ free, then we need to have that
                    $y\neq x$. For in $Qx\phi$, every occurrence of
                    $x$ would be bound by $(r,Qx)$. But if $y\neq x$,
                    then we know that
                    $(Qy\phi)[x:=c]=Qy(\phi)[x:=c]$. And since
                    $(\phi)[x:=c]$ is a sentence by the induction
                    hypothesis, so is $Qy(\phi)[x:=c]$.
                    \end{itemize}
                    This completes our proof, we can now infer by the
                    principle of induction over formulas that for all
                    $\phi$ with only $x$ free, $(\phi)[x:=c]$ is a
                    sentence.

          \item[11.7.2.8] We're essentially asked to show that for all
            models $\mathcal{M}$ and assignments $\alpha$,
            we have
            $\mathcal{M},\alpha\vDash \forall xR(x,x)$ iff
            $(d,d)\in R^\mathcal{M}$ for all $d\in
            D^\mathcal{M}$. That is, we need to show two
            things:

            \begin{enumerate}[(a)]
              \item If  $\mathcal{M},\alpha\vDash \forall
                xR(x,x)$, then
                $(d,d)\in R^\mathcal{M}$ for all $d\in
                D^\mathcal{M}$
              \item If
                $(d,d)\in R^\mathcal{M}$ for all $d\in
                D^\mathcal{M}$, then  $\mathcal{M},\alpha\vDash \forall
                xR(x,x)$.
            \end{enumerate}

            To see that (a) holds, assume that
            $\mathcal{M},\alpha\vDash \forall
            xR(x,x)$. This means, by definition, that
            $\mathcal{M},\alpha[x\mapsto d]\vDash
            R(x,x)$. We derive that  $(d,d)\in R^\mathcal{M}$ for all $d\in
            D^\mathcal{M}$ by contradiction. Suppose that
            there exists a $d\in D^\mathcal{M}$ such that
            $(d,d)\notin R^\mathcal{M}$. But then, we'd have
            that $\mathcal{M},\alpha[x\mapsto d]\nvDash
            R(x,x)$, contrary to our assumption that for
            \emph{all} $d\in
            D^\mathcal{M}$, we have $\mathcal{M},\alpha[x\mapsto d]\vDash
            R(x,x)$. Hence $(d,d)\in R^\mathcal{M}$ for all $d\in
            D^\mathcal{M}$, as desired.

            To see that (b) holds assume that $(d,d)\in R^\mathcal{M}$ for all $d\in
            D^\mathcal{M}$. We need to derive that  $\mathcal{M},\alpha\vDash \forall
            xR(x,x)$, i.e. for all $d\in D^\mathcal{M}$ we have
            $\mathcal{M},\alpha[x\mapsto d]\vDash
            R(x,x)$. But this follows immediately since  $\mathcal{M},\alpha[x\mapsto d]\vDash
            R(x,x)$ iff $(\llbracket
            x\rrbracket^\mathcal{M}_{\alpha[x\mapsto
            d]},\llbracket
            x\rrbracket^\mathcal{M}_{\alpha[x\mapsto
            d]})=(d,d)\in R^\mathcal{M}$.

                        \item[11.7.2.9]We're asked to show that in the
                          given model $\mathcal{M}$ we have
                          \[\mathcal{M},\alpha\vDash \exists xP(x)\to
                          P(a)\lor P(b)\lor P(c)\] for each
                          $\alpha$. We know that, by definition, $\mathcal{M},\alpha\vDash \exists xP(x)\to
                          P(a)\lor P(b)\lor P(c)$ iff either (i)
                          $\mathcal{M},\alpha\nvDash \exists xP(x)$ or (ii)
                          $\mathcal{M},\alpha\vDash
                          P(a)\lor P(b)\lor P(c)$. Now clearly, we
                          either have (a) $\mathcal{M},\alpha\vDash
                          \exists xP(x)$ or (b)
                          $\mathcal{M},\alpha\nvDash \exists xP(x)$ by
                          bivalence. In case (b), we can immediately
                          infer that $\mathcal{M},\alpha\vDash \exists xP(x)\to
                          P(a)\lor P(b)\lor P(c)$, so we focus on case
                          (a). Suppose that $\mathcal{M},\alpha\vDash
                          \exists xP(x)$. This means, by definition,
                          that there exists a $d\in D^\mathcal{M}$
                          such that $\mathcal{M},\alpha[x\mapsto d]\vDash
                           P(x)$. Now, since
                           $D^\mathcal{M}=\{1,2,3\}$, we can only have
                           $d=1,2,3$. If $d=1$, then, since
                           $a^\mathcal{M}=1$, we have that
                           $\mathcal{M},\alpha[x\mapsto \llbracket
                           a\rrbracket^\mathcal{M}_\alpha]\vDash
                           P(x)$. By the denotation lemma, this gives
                           us $\mathcal{M},\alpha\vDash
                           (P(x))[x:=a]$, i.e. $\mathcal{M},\alpha\vDash
                           P(a)$. But if $\mathcal{M},\alpha\vDash
                           P(a)$, then $\mathcal{M},\alpha\vDash
                           P(a)\lor P(b)\lor P(c)$ and so $\mathcal{M},\alpha\vDash \exists xP(x)\to
                          P(a)\lor P(b)\lor P(c)$ by (ii), as
                          desired. If $d=2,3$, we can give the same,
                          analogous argument using the denotation
                          lemma. So, either way,  $\mathcal{M},\alpha\vDash \exists xP(x)\to
                          P(a)\lor P(b)\lor P(c)$, as desired.

                          \item[11.7.2.10] We're asked to prove that if
                            $\phi\vdash\psi$ and $\psi\vdash\phi$,
                            then $\phi\vdash\psi$. There are many
                            different ways we could do this, but I'll
                            use soundness and completeness. First,
                            I'll show my (Lemma) that  if
                            $\phi\vDash\psi$ and $\psi\vDash\phi$,
                            then $\phi\vDash\psi$.\footnote{We
                              actually proved this in class, so you
                              could, in principle just use this
                              without proof.} For suppose that
                            $\phi\vDash\psi$ and
                            $\psi\vDash\phi$. By definition, this
                            means that in every
                            model $\mathcal{M}$, (a) if $\mathcal{M}\vDash
                            \phi$, then $\mathcal{M}\vDash \psi$ and (b)
                            if $\mathcal{M}\vDash \psi$, then
                            $\mathcal{M}\vDash\theta$. I want to
                            derive $\phi\vDash\psi$, i.e. for every
                            model $\mathcal{M}$,
                            such that $\mathcal{M}\vDash\phi$, we also
                            have $\mathcal{M}\vDash\psi$. But if
                            $\mathcal{M}\vDash\phi$, then by (a)
                            $\mathcal{M}\vDash\psi$, and then by (b)
                            $\mathcal{M}\vDash\psi$, as desired. Now,
                            to prove our initial claim, suppose that
                            $\phi\vdash\psi$ and $\psi\vdash\phi$. By
                            soundness, I have that  $\phi\vDash\psi$
                            and $\psi\vDash\phi$. So, by my (Lemma), we
                            have $\phi\vDash\theta$. But then, by
                            completeness, we have $\phi\vdash\theta$,
                            as desired.

                            \item[11.7.2.11] We want to show that if
                              $\Gamma\vDash c\neq c$, then for all
                              formulas $\phi$, either $\Gamma\vDash\phi$ or
                              $\Gamma\vDash\neg\phi$ (the either
                              \dots or was meant
                              inclusively, otherwise the claim doesn't
                              hold). We actually show the stronger
                              claim that if $\Gamma\vDash c\neq c$,
                              then $\Gamma\vdash\phi$ for all $\phi$,
                              so certainly also for $\phi$ and
                              $\neg\phi$.

                              Above, we proved that if $\Gamma\vDash
                              c\neq c$, then $\Gamma$ is
                              unsatisfiable. We're going to use this
                              result. Since $\Gamma$ is unsatisfiable,
                              so is $\Gamma\cup\{\neg\phi\}$ for every
                              $\phi$ (we proved this in 6.2.5.(c) for
                              propositional logic, but the proof
                              clearly goes through for first-order
                              logic, too). But we know by the ``I
                              Can't Get No Satisfaction'' Theorem,
                              that $\Gamma\cup\{\neg\phi\}$ iff
                              $\Gamma\vDash\phi$. So, we can conclude
                              that if $\Gamma\vDash
                              c\neq c$, then $\Gamma\vDash\phi$, as
                              desired.

                              \item[11.7.2.12] In order to define the
                                desired function, we first define the
                                auxiliary function
                                $c:\mathcal{T}\to\mathbb{N}$ given by
                                the following recursion:
                                \begin{itemize}
                                \item $c(x)=1$ and $c(c)=1$
                                \item $c(f(t_1, \mathellipsis, t_n))=c(t_1)+\mathellipsis+c(t_n)+1$
                                \end{itemize}
                               We then define the desired function as
                               follows:
                               \begin{itemize}
                               \item $c(R(t_1, \mathellipsis,
                                 t_n))=c(t_1)+\mathellipsis+c(t_n)+1$

                               \item $c(s=t)=c(s)+c(t)+1$

                               \item $c(\neg \phi)=c(\phi)+1$
                               \item
                                 $c(\phi\circ\psi)=c(\phi)+c(\psi)+1$
                               \item $c(\forall x\phi)=c(\phi)+1$
                               \end{itemize}

                               We now need to prove that the number of
                               nodes in $T(\phi)$, $\#T(\phi)$, is
                               $c(\phi)$ for all
                               $\phi$. We do this by induction. Well,
                               first we prove the lemma that
                               for all $\#T(t)=c(t)$.

                               For the induction base, note that the
                               parsing tree for both $x\in\mathcal{V}$
                               and $c\in\mathcal{C}$ contains
                               precisely one node. So the claim
                               holds that $\#T(x)=\#T(c)=c(x)=c(c)=1$.

                               So, assume the induction hypothesis
                               that  $\#T(t_i)=c(t_i)$ for $1\leq
                               i\leq n$ and consider the number of
                               nodes in $T(f(t_1, \mathellipsis,
                               t_n))$. Since $T(f(t_1, \mathellipsis,
                               t_n))=$

                               \begin{center}
                               \Tree[.{$f(t_1, \mathellipsis,
                                 t_n)$} [.{$T(t_1)$} ] [
                               .{\dots} ]
                             [.{$T(t_n)$} ] ]
                           \end{center}
                           We have that $\#T(f(t_1, \mathellipsis,
                               t_n))=\#T(t_1)+\mathellipsis+\#T(t_n)+1$,
                               which, by the induction hypothesis is
                               identical to
                               $c(t_1)+\mathellipsis+c(t_n)+1=c(f(t_1,
                               \mathellipsis, t_n)$, as
                               desired.

                               Now, for our main claim, we prove by
                               induction that $\#T(\phi)=c(\phi)$.

                               For the first base case, we note that
                               $T(R(t_1, \mathellipsis, t_n)=$
                               \begin{center}
                               \Tree[.{$R(t_1, \mathellipsis,
                                 t_n)$} [.{$T(t_1)$} ] [
                               .{\dots} ]
                             [.{$T(t_n)$} ] ]
                           \end{center}
                           And so $\#T(R(t_1, \mathellipsis,
                           t_n)=c(t_1)+\mathellipsis+c(t_n)+1=c(R(t_1,
                           \mathellipsis, t_n)$, as
                           desired. For the second base case, we note
                           that $T(s=t)=$
                           \begin{center}
                               \Tree[.{$s=t$} [.{$T(s)$} ]
                             [.{$T(t)$} ] ]
                           \end{center}
                           And so
                           $\#T(s=t)=\#T(s)+\#T(t)+1=c(s)+c(t)+1=c(s=t)$, as
                           desired.

                           For the induction steps,
                           \begin{itemize}
                           \item Assume that  $\#T(\phi)=c(\phi)$ and
                             consider $T(\neg \phi)=$
                             \begin{center}
                               \Tree[.{$\neg \phi$} [.{$T(\phi)$} ] ]
                             \end{center}
                             and so
                             $\#T(\neg\phi)=\#T(\phi)+1=c(\phi)+1$ by
                             the induction hypothesis, as desired.

                             \item Assume that  $\#T(\phi)=c(\phi)$
                               and that  $\#T(\psi)=c(\psi)$ and
                               consider $T(\phi\circ\psi)=$
                                \begin{center}
                               \Tree[.{$\phi\circ\psi$} [.{$T(\phi)$} ]  [.{$T(\psi)$} ]]
                             \end{center}
                             So,
                             $\#T(\phi\circ\psi)=\#T(\phi)+\#T(\psi)+1=c(\phi)+c(\psi)+1$
                             by the induction hypothesis, as desired.

                              \item Assume that  $\#T(\phi)=c(\phi)$ and
                             consider $T(Qx \phi)=$
                             \begin{center}
                               \Tree[.{$Qx \phi$} [.{$T(\phi)$} ] ]
                             \end{center}
                             and so
                             $\#T(Qx\phi)=\#T(\phi)+1=c(\phi)+1$ by
                             the induction hypothesis, as desired.
                           \end{itemize}

                           So, by induction, we indeed have
                           $\#T(\phi)=c(\phi)$, as desired.

                         \item[11.7.2.13]

                           We show that Betrand is correct using
                           logic. First, we formalize the stranger's
                           claim using the following translation key:

                           \begin{center}
                             \begin{tabular}[!h]{c c c}
                               $K^1$ & \dots is a barber\\
                               $S^2$ & \dots shaves \underline{\phantom{\dots}}
                             \end{tabular}
                           \end{center}

                           The stranger's claim becomes:

                           \[ \exists x(K(x)\land \forall y(\neg
                             S(y,y)\leftrightarrow S(x,y)))\]

                           We now prove that Bertrand is correct, since
                           the stranger's claim is a logical
                           falsehood. We show this using
                           tableaux. More specifically, we show that
                           $\vdash \neg \exists x(K(x)\land \forall y(\neg
                             S(y,y)\leftrightarrow S(x,y)))$. By
                             definition, what we need to show is that
                             the tableau for $\{\neg\neg\exists x(K(x)\land \forall y(\neg
                             S(y,y)\leftrightarrow S(x,y)))\}$
                             closes. This can be seen as follows:

            \begin{center}
              \begin{prooftree}
                {%
                  line numbering=false,
                  for tree={s sep'=10mm},
                  single branches=true,
                  close with=\xmark
                }
                [{\neg \neg \exists x(K(x)\land \forall y(\neg S(y,y)\leftrightarrow S(x,y)))}
                    [{\exists x(K(x)\land \forall y(\neg S(y,y)\leftrightarrow S(x,y)))}
                        [{(K(p)\land \forall y(\neg S(y,y)\leftrightarrow S(p,y)))}
                            [{K(p)}
                                [{\forall y(\neg S(y,y)\leftrightarrow S(p,y))}
                                    [{\neg S(p,p)\leftrightarrow S(p,p)}
                                        [{\neg S(p,p)}
                                            [{S(p,p)}, close ]
                                        ]
                                    [{S(p,p)}
                                        [{\neg S(p,p)}, close ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
              \end{prooftree}
            \end{center}

            Now, since
            $\vdash \neg \exists x(K(x)\land \forall y(\neg S(y,y)\leftrightarrow S(x,y)))$,
            by soundness, we have
            $\vDash \neg \exists x(K(x)\land \forall y(\neg S(y,y)\leftrightarrow S(x,y)))$.
            That is the stranger's claim cannot be true in any model, so also not in the actual world.
            Bertrand is right.

                \end{itemize}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../../logic.tex"
%%% End:


\subsection{Computation: $f(I_t)$}

\begin{frame}{Computation: $f(I_t)$}
    \begin{equation*}
        B: \alert{f(I_t)} \rightarrow O_t
    \end{equation*}
    \begin{columns}[t]
        \begin{column}{0.5\textwidth}
            Extractor function
            \begin{itemize}
                \item \textit{Unbiasing algorithms}
                \item Extracts uniformly distributed sequences
                \item SHA family
                \item Requires known seed
            \end{itemize}
        \end{column}
        \begin{column}{0.5\textwidth}
            Delay function
            \begin{itemize}
                \item Lower bound run time
                \item Inherently sequential
                \item Adjustable to advances in computation
                \item Asymmetrically hard --- slow computation, fast verification
            \end{itemize}
        \end{column}
    \end{columns}
\end{frame}
\note{
    Delay functions are often extractors
}