%!TEX root = ../CombinatoricsNotes.tex

 \section{Representing Sets and Sperner Systems.}
 \lect{1}{11}
% \marginnote{Lecture 2: Monday, January 11, 2016. }
\marginnote{We begin our systematic investigation into extremal combinatorics.}
Let's start with some definitions.
\begin{itemize}[]
\item[Power set:]
Let $X$ be a finite set with cardinality $|X| = n$. The \defn{power set} is $\P(X)$, the collection of subsets of $X$. $|\P(X)|= 2^n$.
\item[$r$-element subsets:]
The set $X^{(r)}$ is the collection of all $r$-element subsets of $X$. The cardinality $|X^{(r)}| = {n\choose r}$.
\item[Set system:]
A \defn{set system} $\F \subset \P(X)$ on $X$ is a collection of subsets of $X$. For example, $\F = \{\emptyset, \{1\}, \{1,2\}, \{2,3\},\{3\}\}$ is a set system on $\{1,2,3\}$.
\item[$r$-graph:]
If $\F\subset X^{(r)}$, then we call $\F$ an $r$-graph or \defn{hypergraph}. 2-graphs are just ordinary graphs: $\F\subset X^{(2)}$ can be thought of as a graph with vertex set $X$, edge set $\F$.
\end{itemize}
We'll also frequently use the notation $[n] = \{1,2,\dotsc,n\}$.

\newthought{Now, given a set system} $\{A_1,A_2,\dotsc,A_m\}$, we want to ``reduce'' the sets such that distinct sets remain distinct.
That is, we want to find $S\subset X$ as small as possible such that $\{A_1\cap S, A_2\cap S, \dotsc, A_m\cap S\}$ are all distinct. \marginnote{Note that $S$ acts by deleting elements from our base set $X$.}
Let's start with two sets, $\{A_1,A_2\}$. We want $S$ as small as possible such that $A_1\cap S\neq A_2\cap S$. In this case, we can simply choose $S$ to be a singleton of an element which is in one set but not the other\sidenote{which always exists because $A_1\neq A_2$.}.
If we use our example earlier, $\{\emptyset, \{1\}, \{1,2\}, \{2,3\},\{3\}\}$ on $[3]$, we cannot remove any; $|\P([2])| = 4$ and we have five elements.
If our set system is $\{\emptyset, \{1,2\}, \{2,3\},\{3\}\}$ on $[3]$, then we may remove $1$; that is, $S = \{2,3\}$.
This motivates a question: How small can $S$ be as a function of $m$?
\begin{theorem}
Let $\F = \{A_1,A_2,\dotsc,A_m\}$ be a set system. Then there exists $S$, $|S|\leq m-1$ such that $A_1\cap S$, $A_2\cap S$, \ldots, $A_m\cap S$ are all distinct.
\end{theorem}
\begin{remark}
The bound is tight: the set system $\{\emptyset, \{1\}, \{2\}, \dotsc, \{m-1\}\}$ has the property that if we remove any element, we collapse two sets to the empty set.
\end{remark}
\begin{proof}
Choose $S$ as small as possible such that $A_1\cap S$, $A_2\cap S$, \ldots, $A_m\cap S$ are all distinct, and assume that $|S| \geq m$.
Let $A_i' = A_i\cap S$. By the minimality of $S$ for every $x\in S$ there exists $i,j\in[m]$ such that $A_i'\setminus \{x\} = A_j'\setminus \{x\}$, with $i\neq j$.

Now, construct a graph  on the vertex set $[m]$ as follows. For each $x\in S$, choose one pair $i$ and $j$ ($i\neq j$) such that $A_i'\setminus \{x\} = A_j'\setminus \{x\}$, and join $i$ and $j$ by an edge\sidenote[][-2cm]{$A_i'\setminus \{x\} = A_j'\setminus \{x\}$ is equivalent to $A_i \symd A_j  =\{x\}$, where the symmetric difference $X\symd Y = (X\cup Y)\setminus (X\cap Y)$.
Because of this, we will make a new edge each time: if $x,y\in S$ yielded the same edge, then $\{x\}=A_i \symd A_j = \{y\} $.}.
This graph has $m$ verticies and $|S|\geq m$ edges, so it contains a cycle\sidenote{Easy to see by picture; draw $m-1$ edges on $m$ vertices, and then if you don't have a cycle yet, you have a line, and no matter how you place the last edge, you get a cycle.}. Without loss of generality, assume there is a cycle on verticies $1,2,\dotsc,k$ in order. Then there exists distinct $x_1,x_2,\dotsc,x_k\in S$ such that $A_1\symd A_2 = \{x_1\}$, $A_2\symd A_3 = \{x_2\}$, \ldots, $A_{k-1}\symd A_k = \{x_{k-1}\}$, and $A_k \symd A_1 = \{x_k\}$.

We can take the symmetric difference of all of them:
\[
\emptyset = (A_1\symd A_2) \symd (A_2 \symd A_3) \symd \dotsm \symd (A_k \symd A_1) = \{x_1,x_2,\dotsc, x_k\}
\]
\marginnote{Note the symmetric difference is commutative and associative.}
On the left, we have two of each set, so we can regroup and commute to obtain the empty set, using $A\symd A = \emptyset$. On the right, we have the symmetric difference of distinct singletons, which is just the union. This is a contradiction, so our minimal $S$ must have $|S|\leq m-1$.
\end{proof}
Before we continue finding ways to represent sets, we'll need some graph theoretic tools. First, some definitions.
% \begin{definition}
\begin{itemize}[]
	\item[Bipartite:]
A graph $G$ is \defn{bipartite}[graph!bipartite] with bipartition $(V_1,V_2)$ if every edge of $G$ contains one vertex of $V_1$ and one vertex of $V_2$.

\item[Matching: ]A collection of edges $M$ of $G$ is a \defn{matching}[graph!matching] of $V_1$ into $V_2$ if for every $v\in V_1$, $M$ contains exactly one edge containing $v$, and for $v\in V_2$, at most one edge. This is illustrated in \cref{fig:bipartite_matching}.

\begin{marginfigure}
\begin{center}
\begin{tikzpicture}[color=black]

\node (rect) at (0,0)[draw,thick,minimum width=3cm, minimum height = 1cm, rounded corners=3pt,label=left:$V_2$]{};

\node (rect2) at (0,1.5)[draw,thick,minimum width=3cm, minimum height = 1cm, rounded corners=3pt,label=left:$V_1$]{};

\pgfmathsetseed{2}
% \def\z{rand}
\foreach \x in {1,...,6}
{
\filldraw (rand*1.4,rand*.4) circle (0.4pt) node(a\x){};
\filldraw (rand*1.4,1.5+rand*.4) circle (0.4pt) node(b\x){};
}

%some extra nodes in the bottom one
\foreach \x in {1,...,6}
{
\filldraw (rand*1.4,rand*.4) circle (0.4pt);
}

\foreach \x in {1,...,6}
{
% \ifthenelse{\x > 1}{\draw[dashed] (a\x) -- (u)}{};
% \foreach \y in {1,...,\x}
% {
\draw (a\x) -- (b\x);
% }
}

\end{tikzpicture}
\end{center}
\caption{Think of elements of $V_1$ as job applicants, and $V_2$ as positions. Then a matching of $V_1$ into $V_2$ is an arrangement so that every job applicant has a position, but some positions could be unfilled.  \label{fig:bipartite_matching}}
\end{marginfigure}

\item[Neighborhood:] For $S\subset V_1$, define the \defn{neighborhood}[graph!neighborhood] $N(S)$ as the set of verticies adjacent to at least one vertex in $S$.
\end{itemize}
The following result\sidenote{\cite{Hallmarriage}} connects these ideas.
\begin{theorem}[Hall's marriage theorem]
Let $G$ be a bipartite graph with bipartition $(V_1,V_2)$. Then $G$ contains a matching of $V_1$ into $V_2$ if and only if
\begin{equation}	 \label{eq:Hall_condition}
|N(S)| \geq |S| \text{ for every } S\subset V_1. \tag{Hall's condition}
\end{equation}
\end{theorem}
\begin{proof}
The condition is necessary because you need to have enough verticies available in $N(S)$ for elements of $S$ to match into. We will prove sufficiency by induction on $|V_1|$. The base case is immediate. For the induction step, we will split into two cases.
\begin{enumerate}[{Case }1:]
	\item For every $S\subset V_1$ with $S\neq \emptyset$ and $S\neq V_1$, we have that $|N(S)| > |S|$.
% \end{enumerate}
In this case, choose  $v\in V_1$; then $v$ has a $w\in V_2$ adjacent to it, because $|N(\{v\})|> |\{v\}|=1$. Apply the induction hypothesis to $G \setminus \{v,w\}$.

We then just need to check Hall's condition on $G' =G \setminus \{v,w\}$. For every $S\subset V_1 \setminus\{v\}$, we have
\[
 |N'(S)| \geq |N(S)| -1 \geq |S|,
\]
 where $N'$ is the neighborhood with respect to $G'$. The first inequality holds because we removed at most one neighbor by removing $w$. The second inequality holds from our assumption in this case. Then the induction hypothesis yields a matching on $V_1\setminus \{v\}$ into $V_2\setminus \{w\}$, which we can extend to a matching on $V_1$ into $V_2$ by matching $v$ to $w$.
% \begin{enumerate}[{Case }1:]\setcounter{enumi}{1}
\item There exists $S\subset V_1$ with $S\neq \emptyset$ and $S\neq V_1$, such that $|N(S)| = |S|$.
% \end{enumerate}
By induction hypothesis,  there exists a matching $M_1$ of $S$ into $N(S)$.

 It remains to find a matching from $V_1\setminus S$ into $V_2 \setminus N(S)$. By induction hypothesis, it is enough to show that for every $T\subset V_1\setminus S$, we have
\[
 |N(T)\cap (V_2 \setminus N(S))| \geq |T|.
\]
 \marginnote{Here's the trick.}Since $S\cup T\subset V_1$, by assumption, we have Hall's condition
\[
 |N(S\cup T)|\geq |S\cup T| = |S| + |T|.
\]

 We know $N(S\cup T) = N(S) \cup N(T) = N(S) \cup (N(T)\setminus N(S))$. So $|N(S\cup T)| = |N(S)| + |N(T)\setminus N(S)| $. So Hall's condition becomes
\[
 |N(T)\setminus N(S)| \geq |T|
\]
 as desired.\qedhere
 \end{enumerate}
\end{proof}

Let's employ Hall's theorem to represent sets. Let
\[
 \F = \{A_1,A_2,\dotsc,A_m\}
 \] be a set system.
% \begin{definition}
A \defn{system of distinct representatives} for $\F$ is a collection $\{x_1,\dotsc,x_m\}$ of elements such that $x_1,\dotsc, x_m$ are pairwise distinct, and $x_i \in A_i$ for $i\in[m]$.
% \end{definition}
Given $\F$, one can consider the bipartite graph $G$ with bipartition $(V_1,V_2)$ such that $V_1 = \F$ and $V_2= \bigcup_{i\in[m]} A_i$. We join $A_i$ to $x$ iff $x\in A_i$.
Then a system of distinct representatives for $\F$ is exactly a matching on $G$ from $V_1$ into $V_2$. Hall's theorem then immediately implies the following result.
\begin{corollary}
A set system $\F = \{A_1,\dotsc,A_m\}$ has a system of distinct representatives if and only if for every $\F'\subset \F$,
\[
|\F'| \leq \left|\bigcup_{A\in \F'}A\right|.
\]
\end{corollary}
\newthought{Given a set $X$}, there is a natural bipartite graph and matching which will prove useful.
\begin{corollary} \label{cor:Xr_matching_exists}
Let $X$ be a set with $|X|=n$. Let $G$ be a bipartite graph with bipartition $(X^{(r)}, X^{(r-1)})$ such that $A\in X^{(r)}$ is adjacent to $B \in X^{(r-1)}$ if $B\subset A$. Then if  $r> n/2$, the graph $G$ has a matching of $X^{(r)}$ into $X^{(r-1)}$.
\begin{marginfigure}
\begin{center}
 \begin{tikzcd}[column sep=tiny]
X^{(3)}  & &\{1,2,3\}  \\
X^{(2)} &\{1,2\} \arrow[dash,green]{d}  \arrow[dash]{rd}& \{1,3\} \arrow[dash]{ld}\arrow[dash]{d} \arrow[dash,green]{rd} & \arrow[dash,green]{ld} \arrow[dash]{d}\{2,3\} \\
X^{(1)} & \{1\}  & \{2\} &\{3\} \\
X^{(0)} & & \emptyset
\end{tikzcd}
\end{center}
\caption{Example of the graph relation on $G= (X^{(2)}, X^{(3)})$, with a matching highlighted in green.}
\end{marginfigure}
\end{corollary}
\begin{remark}
This corollory implicitly shows ${n \choose r} \leq {n \choose r-1}$ if $r> n/2$.
\end{remark}
\begin{proof}
It suffices to check \ref{eq:Hall_condition}. For every $\A\subset X^{(r)}$, we want $|N(\A)| \geq |\A|$. Label
\[
\B:= N(\A) = \{B\in X^{(r-1)}: B\subset A, \text{ for some }A\in \A\}.
\]
Every element of $X^{(r)}$ is incident to $r$ edges of $G$\sidenote{Each edge corresponds to taking an element away from the set.}. So we have $|\A|r$ edges leaving $\A$, ending in $\B$.
Every element of $X^{(r-1)}$ is incident to $n-r+1$ edges of $G$, which can be seen by the fact that there are $n-(r-1)$ possible elements to add to a set $B\in X^{(r-1)}$ to obtain a superset in $X^{(r)}$. So we have at most $|\B|(n-r+1)$ edges leaving $\B$, ending in $\A$\sidenote{Since not every edge leaving $\B$ needs to reach something in $\A$ (it could reach something in $X^{(r)} \setminus \A$), it is only ``at most.''}.
So $|\A|r \leq |\B|(n-r+1)$. But by assumption $r \geq (n-r+1)$, so $|\B| \geq |\A|$ as desired.
\end{proof}

\lect{1}{13}
% \marginnote{Lecture 3: Wednesday, January 13, 2016.}

Recall that $\F\subset \P(X)$ is a \emph{Sperner system} if for all $A,B\in \F$, if $A\leq B$, then $A=B$.
We wish to find $\max |\F|$ such that $\F$ is a Sperner system, as a function $|X| = n$. Note that $X^{(r)}$ is always Sperner, and $|X^{(r)}| = {|X| \choose r}$, which is maximized when $r = \floor{n/2}$.
\begin{theorem}[\cite{sperner1928}] \label{thm:sperner}
If $\F\subset \P(X)$ is Sperner, then $|\F| \leq {n \choose \floor{n/2}}$, where $n=|X|$.
\end{theorem}
\begin{proof}
\marginnote{A Sperner system is a system of sets such that no two are comparable. A dual notion is a system of sets such that all are comparable. }
An ordered collection $(A_1,A_2,\dotsc, A_k)$ of sets  in $\P(X)$  is a \defn{chain} if $A_1\subsetneqq A_2 \subsetneqq A_3 \subsetneqq\dotsm \subsetneqq A_k$.
\begin{marginfigure}
\begin{center}
 \begin{tikzcd}[column sep=tiny]
X^{(3)}  & &\{1,2,3\} \arrow[dash]{d}\arrow[dash]{rd}\arrow[dash,green]{ld} \\
X^{(2)} &\{1,2\} \arrow[dash,green]{d}  \arrow[dash]{rd}& \{1,3\} \arrow[dash]{ld}\arrow[dash]{d} \arrow[dash,blue]{rd} & \arrow[dash,red]{ld} \arrow[dash]{d}\{2,3\} \\
\mathbf{X^{(1)}} & \mathbf{\{1\}} \arrow[dash,green]{rd} & \mathbf{\{2\}} \arrow[dash]{d}&\mathbf{\{3\}}\arrow[dash]{ld} \\
X^{(0)} & & \emptyset
\end{tikzcd}
\end{center}
\caption{Consider $X^{(1)}$, a natural maximal Sperner system. Note that each element of $X^{(1)}$ can form a distinct chain, such as $\emptyset \subsetneqq \{1\} \subsetneqq \{1,2\} \subsetneqq \{1,2,3\}$.}\label{fig:Sperner_chains}
\end{marginfigure}
It is enough to show that $\P(X)$ can be partitioned into ${n \choose \floor{n/2}}$ chains. Indeed, every Sperner system can contain $\leq 1$ element from each chain in the partition; see \cref{fig:Sperner_chains} for an example.
Better yet, we partition $\P(X)$ into chains such that every chain contains an element of $X^{(\floor{n/2})}$.

Let's begin by partitioning all subsets of $X$ of size $\geq \floor{n/2}$.
We will first do this inductively starting from $X^{(\floor{n/2})}$, and extending the partition to $X^{(\floor{n/2})}\cup X^{(\floor{n/2}+1)}\cup\dotsm \cup X^{(k)}$ to $X^{(k+1)}$ using the matching obtained in \cref{cor:Xr_matching_exists} from $X^{(k)}$ to $X^{(k+1)}$ by adding each element of $X^{(k+1)}$ to the chain of the set it's matched to. Note that are chains are not maximal; some (all but one) truncate before they reach the top, $X^{(n)}$.
Then we can extend the partition to sets of size $< \floor{n/2}$ by symmetry.
\end{proof}
\begin{remark}
This proof is instructive and provides the useful technique of partitioning into chains. But we can prove stronger results with slicker proofs.
\end{remark}
Suppose $k< n/2$ and we want to find the maximum size Sperner system such that every set in the system has size $\leq k$.
As one may guess, the maximum size will be $|X^{(k)}| = {n\choose k}$. To show this, we'll use the following result.
\begin{theorem}[Lubell, Meshalkin, Yamamoto, Boll\'obas, and possibly others, A.K.A. the LYM inequality] \marginnote{\cite{LUBELL_LYM,Meshalkin_LYM,yamamoto1954_LYM,Bollab_LYM}}
\label{thm:LYM_inequality}
Let $\F\subset \P(X)$, $|X|=n$ be a Sperner system.
Let $\F_k = \F\cap X^{(k)}$ be the set of $k$ element sets in $\F$, and let $f_k = |\F_k|$. Then
\begin{equation}	\tag{LYM}
\label{eq:LYM}
\sum_{k=0}^n \frac{f_k}{{n\choose k}} \leq 1.
\end{equation}
\end{theorem}
\begin{remark}
We have
\[
1 \geq \sum_{k=0}^n \frac{f_k}{{n\choose k}} \geq \sum_{k=0}^n \frac{f_k}{{n\choose \floor{n/2}}},
\]
thus
\[
{n\choose \floor{n/2}} \geq \sum_{k=0}^n f_k = |\F|
\]
which is Sperner's theorem.
\end{remark}
\begin{proof} Let's assume $X=[n]$ for convenience.
Consider all maximum chains in $\P(X)$ and count how many chains an element of $\F$ belongs to. Each of the maximal chains is of the form $\emptyset = A_0$, $A_1$, \ldots, $A_n = X = [n]$, and $|A_i| = i$. So each maximal chain corresponds to an ordering $a_1,a_2,\dotsc,a_n$ of $[n]$, where $\{a_i\} = A_i \setminus A_{i-1}$, is the element you add to $A_i$ to get the next set in the chain.

Thus, there are $n!$ maximal chains (the number of re-orderings of $[n]$). Consider
$F \in X^{(k)}$.
How many  maximal chains is $F$ in? If $k=0,n$, $F$ is in every chain, so $n!$. If $k=1$, then $F$ is in $(n-1)!$ chains. If $k=2$, then $2!(n-2)!$. In general, $F$ is in $k!(n-k)!$ maximal chains\sidenote{We choose $k$ to get to the set, then $(n-k)$ to finish the chain.} Each maximal chain contains $\leq 1$ element of $\F$. The total number of elements of $\F$ in all maximum chains is
\[
 \sum_{k=0}^n f_k k!(n-k)!\leq n!
\]
Dividing by $n!$, we obtain the LYM inequality.
\end{proof}
\begin{remark}
Let's consider an alternate proof. Let $C$ be a uniformly randomly chosen maximal chain, and consider the expectation value of the number of elements of $C\cap \F$. Of course, there is at most 1 element, since $\F$ is a Sperner system. On the other hand,
\begin{align*}
\E ( | C\cap \F|) &=\sum_{k=0}^n \E( | C \cap \F_k|) = \sum_{k=0}^n f_k \cdot (\text{probability that a set of size $k$ is in $C$})\\
 &=  \sum_{k=0}^n f_k \cdot \frac{1}{(\text{number of sets of size $k$)}} = \sum_{k=0}^n \frac{f_k}{{n \choose k}} \leq 1.
\end{align*}
\end{remark}

When does equality hold in LYM? Certainly when $\F= X^{(r)}$ for any $r$. We'd like to show this condition is necessary as well, but to do so, we'll first prove a more refined inequality in which equality is easier to check. Then we'll use this to show sufficiency for equality in the LYM inequality.

\begin{theorem}[Local LYM inequality] \label{thm:local_LYM}
Let $\A\subset X^{(r)}$, and $|X|=n$. Define
$\partial \A \subset X^{(r-1)}$
the \defn{shadow} of $A$ by
\[
\partial \A := \{B\in X^{(r-1)}: B\supseteq A \text{ for some }A\in \A \}.
\]
Then
\begin{equation}	\label{eq:local_LYM} \tag{Local LYM}
\frac{|\partial \A|}{{n\choose r-1}} \geq \frac{|\A|}{{n \choose r}}
\end{equation}
or equivalently,
\[
 r| \A| \leq |\partial \A| (n-r+1).
\]
Moreover, equality holds if and only if $\A= \emptyset$ or $\A=  X^{(r)}$.
\end{theorem}
\begin{proof}
\[
 r|\A| = | \{ (B,A): B\in \partial A, \, A\in \A, \, B\subset A\}| \leq |\partial A| (n-r+1)
 \] as seen in \cref{cor:Xr_matching_exists}.
If equality holds, then $\A$ contains all supersets in $X^{(r)}$ of all sets in $\partial A$.

\begin{marginfigure}
\begin{center}
\begin{tikzcd}[column sep=tiny]
\A & \{x_1,\dotsc,x_r\} \arrow[dash]{d} \arrow[dash]{rd} & \arrow[dash]{ld} \\
\partial \A & \{x_2,\dotsc,x_n\} & \{x_1,x_2,\dotsc,x_n\}
\end{tikzcd}
\end{center}
\caption{If $\A$ contains all supersets in the $X^{(r)}$ layer of sets in the shadow $\partial \A$, then as long as $\A\neq \emptyset$, it must contain every set; here, for example, $\A$ has to include the endpoint of the edge leaving the bottom left vertex. }
\end{marginfigure}

Consider the graph as in \cref{cor:Xr_matching_exists}:  there are no edges from $A\cup \partial A$ to the remaining verticies, so since $G$ is connected\sidenote{as is easy to check},  we have equality in \eqref{eq:local_LYM}.
\end{proof}

\begin{theorem}
The equality in the LYM inequality \eqref{eq:LYM} holds iff $\F = X^{(r)}$ for some $r$.
\end{theorem}
\begin{proof}
Inductively define $G_n = \F_n$, and for $k < n$, $G_k  = \partial G_{k+1} \cup \F_k$. \marginnote{$G_k$ is the set of all $k$-element sets which are subsets of sets in $\F$.}

Let $\phi_k = \frac{f_k}{{n \choose k}}$ be the proportion of sets of $\F$ in $X^{(k)}$. Similarly, set $\gamma_k = \frac{|G_k|}{{n\choose k}}$. By the local LYM,
\[
\frac{|\partial G_{k+1}|}{{n\choose k}} \geq \frac{|G_{k+1}|}{{n\choose k+1}} = \gamma_{k+1}
\]
So,
\[
\gamma_k = \frac{|\partial G_{k+1} \cup \F_k|}{{n\choose k}} = \frac{|\partial G_{k+1}|}{{n\choose k}} + \frac{|\F_k|}{{n\choose k}} \geq  \gamma_{k+1} + \phi_k
\]
where we are using that $\F$ is Sperner, so that $\partial G_{k+1}$ is disjoint from $\F_k$. Thus, we have $\gamma_k \geq \gamma_{k+1} + \phi_k$, with equality iff $\gamma_{k+1} = 1$ or $\gamma_{k+1} = 0$. \marginnote{Since that is when we have equality in \cref{eq:local_LYM}.}

Note $\gamma_n = \phi_n$. Then
\begin{gather*}
\gamma_{n-1} \geq \gamma_n + \phi_{n-1} = \phi_n + \phi_{n-1}\\
\gamma_{n-2} \geq \gamma_{n-1} + \phi_{n-2} = \phi_n + \phi_{n-1} + \phi_{n-2}\\
\text{etc.}\\
\gamma_k \geq \phi_n + \phi_{n-1} + \dotsm + \phi_k.
\end{gather*}
Hence, $1 \geq \gamma_0 \geq \phi_n + \phi_{n-1} + \dotsb + \phi_0$. This is the LYM inequality. But equality holds if $\gamma_k = \gamma_{k+1} +\phi_k$ for each $k$, i.e. $\gamma_{k+1} = 1$ or $\gamma_{k+1} = 0$ for each $k$. Assuming we have equality, we use that $\gamma_k$ is non-increasing with $k$, so there must exist $k_0$ such that $\gamma_{k_0} = 1$, and $\gamma_{k_0+1} = 0$ (writing $\gamma_{n+1} = 0$). Thus, there are no sets in $\F$ of size at least $k+1$. In other words, $G_{k+1} = \emptyset$, and $G_k = \F_k = X^{(k)}$. But then we must have $\F  = X^{(k)}$ as desired, since $\F$ may not have any super sets or subsets of $X^{(k)}$, i.e., any other set in $\P(X)$.
\end{proof}
\begin{remark}
The matchings with $X^{(r)}$ and $X^{(r-1)}$ are the essential objects here in proving the local LYM, and hence LYM and its equality.
\end{remark}
\begin{exercise}
Prove Sperner's theorem using the original way: partitioning into ${n\choose \floor{n/2}}$ chains by induction on $n$ instead of Hall's theorem.
\marginnote{This is \cref{thm:sym_part}.}
\end{exercise}


\documentclass[11pt,fleqn]{article}
% \usepackage{cs70,latexsym,epsf}
\usepackage{latexsym,epsf,fleqn}
\usepackage{amsmath,amsthm,amsfonts,amssymb}
\usepackage{mathtools}
\usepackage{array}
\usepackage{booktabs}

\usepackage{geometry}
\geometry{
  a4paper,
  total={170mm,257mm},
  left=20mm,
  top=20mm,
}

\newcommand\Set[2]{\{\,#1\mid#2\,\}}
\newcommand\underoverset[3]{\underset{#1}{\overset{#2}{#3}}}
\newcommand{\mbf}[1]{\mbox{{\bfseries #1}}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Q}{\mathbb{Q}}

\begin{document}

\section*{CS 70 homework 1 solutions}

Your full name: Joey Yandle
\newline
Your login name: dragon
\newline
Homework 1
\newline
Your section number: 0
\newline
Your list of partners: Galina Vinnik
\newline

\begin{enumerate}
\item
For each of the following, define proposition
symbols for each simple proposition in the argument (for example, $P$ =
``I will ace this homework''). Then write out the logical form of
the argument. If the argument form corresponds to a known inference
rule, say which it is. If not, show that the proof is correct using
truth tables.
\begin{enumerate}
\item  I will ace this homework and I will have fun doing it.
Therefore, I will ace this homework.
\begin{align}
  P &= \text{``I will ace this homework''}\\
  Q &= \text{``I will have fun doing it''}\\
  P &\land Q \implies P
\end{align}

$P \land Q$ implies both $P$ and $Q$, and $P \implies P$.
\begin{align}
\therefore P \land Q \implies P\qed
\end{align}

\item It is hotter than 100 degrees today or the pollution is
dangerous. It is less than 100 degrees today. Therefore, the pollution
is dangerous.
\begin{align}
  P &= \text{``It is hotter than 100 degrees today''}\\
  Q &= \text{``The pollution is dangerous''}\\
  \neg P &= \text{``It is less than 100 degrees today''}\\
  Q &= \text{``The pollution is dangerous''}\\
  (P &\lor Q) \land \neg P \implies Q
\end{align}

We know that not only is $(P \lor Q)$ true, but also $P$ is not true, so $Q$ must be true.
\begin{align}
\therefore (P \lor Q) \land \neg P \implies Q \qed
\end{align}

(This is ignoring the possibility of the temperature being exactly 100 degrees, in which case it is possible for the pollution to not be dangerous)

\newpage
\item Tina will join a startup next year. Therefore,
Tina will join a startup next year or she will be unemployed.
\begin{align}
  P &= \text{``Tina will join a startup next year''}\\
  Q &= \text{``Tina will be unemployed''}\\
  P &\implies (P \lor Q)
\end{align}

$P \implies P$, and $(P \lor Q)$ is true if $P$ or $Q$ is true.
\begin{align}
\therefore P \implies (P \lor Q) \qed
\end{align}

\item If I work all night on this homework, I will answer all the
exercises. If I answer all the exercises, I will understand the
material. Therefore, if I work all night on this homework, I will
understand the material.

\begin{align}
  P &= \text{``I work all night on this homework''}\\
  Q &= \text{``I will answer all the exercises''}\\
  R &= \text{``I will understand the material''}\\
  (P &\implies Q) \land (Q \implies R) \iff P \implies R
\end{align}

$P \implies Q$, and $Q \implies R$.  So if $P$ is true, $Q$ is true, and if $Q$ is true then $R$ is true.
\begin{align}
\therefore P \implies R\qed
\end{align}
\end{enumerate}

\item
Recall that $\N=\{0,1,\ldots\}$ denotes the set of natural numbers,
and $\Z=\{\ldots,-1,0,1,\ldots\}$ denotes the set of integers.
\begin{enumerate}
\item Define $P(n)$ by
\[ P(n) = \forall m \in \N , \; m<n \implies
\neg (\exists k \in \N , \; n=mk \; \wedge \; k<n) \]
Concisely, for which numbers $n\in\N$ is $P(n)$ true?

$m | n \implies \exists k \in \N \ni n=mk$, which implies $\lnot P(n)$.  Therefore $P(n)$ is true when $m<n \implies m \nmid n$.  Such numbers are called prime numbers.
\begin{align}
\therefore P(n)\;\text{is true}\;\forall n \in \mathbb{P}\qed
\end{align}

\item Rewrite the following in a way that
removes all negations (``$\neg$, $\ne$'') but remains equivalent.
\[ \forall i . \; \neg \forall j . \;
\neg \exists k . \;
(\neg \exists \ell . \; f(i,j) \ne g(k,\ell)). \]

$\forall i,j\;\exists k,l \ni f(i,j) = g(k,\ell)$

\item Prove or disprove:
$\forall m \in \Z . \; \exists n \in \Z . \; m \ge n$.

Let $n = m$.  By reflexion, $m \ge m$.
\begin{align}
\therefore \exists n \ni m \ge n
\end{align}

\item Prove or disprove:
$\exists m \in \Z . \; \forall n \in \Z . \; m \ge n$.

Assume such an $m$ exists.  Let $n = m+1$.  This implies
\begin{align}
  m &\ge m+1\\
  0 &\ge 1
\end{align}

This is false, so using proof by contradiction the proposition fails.

\end{enumerate}

\newpage
\item
Alice and Bob are playing a game of chess,
with Alice to move first.
If $x_1,\dots,x_n$ represents a sequence of possible moves
(i.e., first Alice will make move $x_1$, then Bob will make move $x_2$,
and so on),
we let $W(x_1,\dots,x_n)$ denote the proposition that,
after this sequence of moves is completed,
Bob is checkmated.
\begin{enumerate}
\item State using quantifier notation the proposition that Alice
can force a checkmate on her second move, no matter how Bob plays.
\begin{align}
\forall x_2\;\exists x_1, x_3 \ni W(x_1, x_2, x_3) = \top
\end{align}

\item Alice has many possibilities to choose from on her first move,
and wants to find one that lets her force a checkmate on her second move.
State using quantifier notation the proposition that $x_1$
is \emph{not} such a move.
\begin{align}
\forall x_2, x_3\; W(x_1, x_2, x_3) = \bot
\end{align}

\end{enumerate}

\item
Joan is either a knight or a knave.
Knights always tell the truth, and only the truth;
knaves always tell falsehoods, and only falsehoods.
Someone asks Joan, ``Are you a knight?''  She replies,
``If I am a knight then I'll eat my hat.''
\begin{enumerate}
\item Must Joan eat her hat?
\begin{align}
  P &= \text{``Joan is a knight''}\\
  Q &= \text{``Joan will eat hat''}\\
  P &\implies Q
\end{align}

If Joan is a knight, then she speaks truth, and so by the proposition will eat her hat.  If she is not a knight, then she speaks false, so we must negate her proposition:
\begin{align}
  \lnot (P &\implies Q) \iff (P \land \lnot Q)
\end{align}

So if Joan is a knave, then her statement must be false, but if it is false, then she must be a knight.  So Joan cannot be a knave, and thus must eat her hat.

\item Let's set this up as problem in propositional logic.
Introduce the following propositions:
\begin{eqnarray}
P &=& \text{``Joan is a knight''}\\
Q &=& \text{``Joan will eat her hat''}.
\end{eqnarray}
Translate what we're given into propositional logic,
i.e., re-write the premises in terms of these propositions.
\begin{align}
  P &\implies (P \implies Q) \iff (P \land Q)\\
  \lnot P &\implies \lnot (P \implies Q) \iff (P \land \lnot Q)
\end{align}

\item Using proof by enumeration,
prove that your answer from part (1) follows from the premises
you wrote in part (2).
(No inference rules allowed.)

\begin{tabular}{cccc}
  \toprule
  $P$ & $Q$ & $P \land Q$ & $P \land \lnot Q$ \\
  \midrule
  T & T &      T    &      F          \\
  T & F &      F    &      T          \\
  F & T &      F    &      F          \\
  F & F &      F    &      F          \\
  \bottomrule
\end{tabular}

So $P \implies (P \implies Q)$ is supported by enumeration, while $\lnot P \implies \lnot (P \implies Q)$ leads to contradiction.  Thus Joan will eat the hat.
\end{enumerate}

\newpage
\item
For each claim below,
prove or disprove the claim.
\begin{enumerate}
\item Every positive integer can be expressed as the sum of two perfect squares.
(A perfect square is the square of an integer. 0 may be used in the sum.)
\begin{align}
  a \in \mathbb{Z} &\implies \exists j,k \in \Z \ni a = j^2 + k^2\\
  (a = j^2 + k^2) &\implies |j|,|k| \le \sqrt{a}
\end{align}

This cannot be true for all positive integers, since it is not true for $3$; the only integers $\le \sqrt{3}$ are $0$ and $1$, and the max sum of their squares is $2$.
\begin{align}
  \therefore (j^2 + k^2 < 3)\;\forall j,k \le \sqrt{3}
\end{align}

\item For all rational numbers $a$ and $b$, $a^b$ is also rational.
\begin{align}
  b \in \mathbb{R} &\implies \exists j,k \in \Z \ni b = \frac{j}{k}\\
  k \nmid j &\implies a^b = a^{\frac{j}{k}} = a^j a^{\frac{1}{k}}\\
  |k| > 1 &\implies \exists a \ni a^{\frac{1}{k}} \notin \mathbb{R}\;(\text{e.g.}\;a \in \mathbb{P})\\
  \therefore \exists a, b &\in \mathbb{R} \ni a^b \notin \mathbb{R}
\end{align}

\end{enumerate}

\end{enumerate}
\end{document}


\documentclass{article}
\usepackage[hmargin=1in,vmargin=1.5in]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{bm}
\newcommand{\x}{\bm x}
\title{Homework 4}
\setcounter{MaxMatrixCols}{20}
\author{Xinyi Gu, Songchen Tan}
\date{\today}
\begin{document}
\maketitle
\section{}
See the solution in Figure 1.
\begin{figure}[!ht]
    \includegraphics[width=\textwidth]{1.1.png}
\end{figure}
\begin{figure}[!ht]
    \includegraphics[width=\textwidth]{1.2.png}
    \caption{Solution to Problem 1}
\end{figure}

\section{}
\subsection{}
$$
s_1+ s_2 + s_3 + ... + s_n = 3*i, \quad i \in \mathbb{Z}
$$
i.e, the sum of the set S is divisible by 3.

\subsection{}

Let $f(a, b, c, s)$ be the state with the information that $a, b, c$ represent the remaining sum of integers in each group, while using the first $s$ elements in the set $S$. If this is possible, $f=1$, else $f=0$

The state transition equation would be:

$$
f(a,b,c,s) = f(a-S[s],b,c,s-1) \text{ or } f(a,b-S[s],c,s-1) \text{ or } f(a,b,c-S[s],s-1)
$$

and the base cases would be $f(0,0,0,0)=1$ and $f(a,b,c,0)=0$ if not all $a,b,c$ equals 0.

We can obtain the actual partition method when calculating the state transition: mark the dependence of $f(a,b,c,s)$ with $(s,A)$ if it uses $f(a-S[s],b,c,s-1)$, and similarly for set B and C. We collect all marks toward the final solution and we can get the partition method.

\subsection{}

Let $a_i, b_i, c_i, i= 1, ..., n $ denote to position of each number, if $s_i$ is in the subset A, then $a_i = 1, b_i = 0, c_i = 0$, if in B, $a_i = 0, b_i = 1, c_i = 0$, if C, then $a_i = 0, b_i = 0, c_i = 1$

The integer programming formulation for this problem can be set as:

\begin{align*}
    &\max m\quad \\
    \text{s.t.} \quad& \sum_ia_i\ge m, \sum_ib_i\ge m, \sum_ic_i\ge m  \\
    \quad & \sum_ia_i s_i = \sum_i b_i s_i = \sum_i c_i s_i = \frac{1}{3}\sum_is_i\\
    & a_i+ b_i+ c_i = 1, \quad i = 1, ... n \\
    & a_i, b_i, c_i \in \{0, 1\}
\end{align*}

\section{}
We denote the set defined by the first set, second set and third constraint to be $C_1,C_2,C_3$, and set $0\le x_{ij}\le 1$ to be $C_4$. Denoting the feasible set after four kinds of relaxations to be $X_1=C_1\cap C_2\cap C_4\cap\mathbb Z$, $X_2=C_3\cap C_4\cap\mathbb Z$, $X_3=C_2\cap C_3\cap C_4\cap\mathbb Z$, and $X_4=C_2\cap C_4\cap\mathbb Z$. Let $\x=\{x_{ij}\}$ $z=\sum_{ij}c_{ij}x_{ij}$, we have

\begin{itemize}
    \item $Z_{LP}=\min z$ s.t. $\x\in P_{LP}=C_1\cap C_2\cap C_3\cap C_4$;
    \item $Z_{D1}=\min z$ s.t. $\x\in P_1=C_3\cap \operatorname{CH}(X_1)$;
    \item $Z_{D2}=\min z$ s.t. $\x\in P_2=C_1\cap C_2\cap \operatorname{CH}(X_2)$;
    \item $Z_{D3}=\min z$ s.t. $\x\in P_3=C_1\cap \operatorname{CH}(X_3)$;
    \item $Z_{D4}=\min z$ s.t. $\x\in P_4=C_1\cap C_3\cap \operatorname{CH}(X_4)$;
    \item $Z_{IP}=\min z$ s.t. $\x\in P_{IP}=C_1\cap C_2\cap C_3\cap C_4\cap\mathbb Z$;
\end{itemize}

We first give a lemma on the property of convex hulls: if $A$ is a convex set and $B$ is an arbitrary set, then $\operatorname{CH}(A\cap B)$ is a subset of $A\cap\operatorname{CH}(B)$. This is because for any point in the former set, there exists a convex combination of points in $A\cap B$ that yields this point; and therefore this point is a member of both $\operatorname{CH}(B)$ and $\operatorname{CH}(A)$. Since $A=\operatorname{CH}(A)$, the point is a member of $A\cap\operatorname{CH}(B)$.

Using this lemma, we get $C_1\cap C_2\cap \operatorname{CH}(X_2)\subseteq C_1\cap C_2\cap C_3\cap \operatorname{CH}(C_4\cap\mathbb Z)=C_1\cap C_2\cap C_3\cap C_4$, therefore $P_2\subseteq P_{LP}$, so $Z_{LP}\le Z_{D2}$. Similarly, we can trivially get $Z_{D2}\le Z_{D3}\le Z_{IP}$.

When the third constraint is relaxed, the problem can be reformulated as a network flow problem, and the integrality of such problems implies that solving each subproblem $Z_{D1}(p)$ in $\operatorname{CH}(X_1)$ is equivalent to solving in $C_1\cap C_2\cap C_4$, therefore we have $Z_{D1}=Z_{LP}$. Similarly $Z_{D4}=Z_{LP}$. Assembling all the relationship we got so far gives us the desired order.

\section{}
\subsection{}
\begin{align*}
    \log \mathbb{P}[\Tilde{z_j} = 0] & = \log(\prod_{i \in I^{+}_{j}} \mathbb{P}[\Tilde{y_i} = 0]\prod_{i \in I^{-}_{j}} \mathbb{P}[\Tilde{y_i} = 1]) &\\
    & =  \log(\prod_{i \in I^{+}_{j}} (1-y^{*}_i)\prod_{i \in I^{-}_{j}} y^{*}_i) &\\
    & = \sum_{i \in I^{+}_{j}} \log(1-y^{*}_i) + \sum_{i \in I^{-}_{j}} \log y^{*}_i &\\
    & \leq - \sum_{i \in I^{+}_{j}}y^{*}_i - \sum_{i \in I^{-}_{j}}(1-y^{*}_i) &(logarithm \quad inequality \quad by \quad hint)\\
\end{align*}

\subsection{}

\begin{align*}
    \mathbb{E}[\Tilde{z_j}] & = 0 \times \mathbb{P}[\Tilde{z_j} = 0] + 1 \times \mathbb{P}[\Tilde{z_j} = 1] &\\
    & = \mathbb{P}[\Tilde{z_j} = 1] &\\
    & = 1 - \mathbb{P}[\Tilde{z_j} = 0] &\\
    & =  1 - e^{\log \mathbb{P}[\Tilde{z_j} = 0]} &\\
    & \geq  1 - ({ 1 + (1-\frac{1}{e})\log  \mathbb{P}[\Tilde{z_j} = 0]}) &(logarithm \quad inequality \quad by \quad hint)\\
    & = \frac{e-1}{e}(-\log \mathbb{P}[\Tilde{z_j} = 0])&\\
    & \geq \frac{e-1}{e}( \sum_{i \in I^{+}_{j}}y^{*}_i + \sum_{i \in I^{-}_{j}}(1-y^{*}_i))& (4.1)\\
    & \geq \frac{e-1}{e} z^{*}_{j}
\end{align*}

\subsection{}

\begin{align*}
    Z_{LP} & \geq Z_{IP} & (LP \quad relaxation \quad is \quad an \quad upper-bound \quad of \quad IP)\\
    & \geq \mathbb{E}[Z_H]=\sum_j w_j \mathbb E[\tilde z_{j}] & (\mathbb{E}[Z_H] \leq \max Z_H = Z_{IP})\\
    & \geq \sum_j\frac{e-1}{e} w_jz^{*}_{j} =  \frac{e-1}{e} Z_{LP} & (4.2)\\
\end{align*}

\end{document}


\section{Boundary Behavior of Hierarchical B-Splines}
\label{sec:32notAKnot}

\minitoc{87mm}{8}

\noindent
As we have seen in the last section (see \cref{cor:hierSplittingBSpline}),
the hierarchical splitting equation \eqref{eq:hierSplittingUV}
only holds when restricting the function spaces to
$\rspldomain{l}{p} =
\clint{\tfrac{p-1}{2} \ms{l},\; 1 - \tfrac{p-1}{2} \ms{l}}$,
which is a proper subset of the domain $\clint{0, 1}$ if $p > 1$.
As we will see, the implications of this fact on the approximation quality
of the hierarchical B-spline basis are severe.
In this section, we study the underlying reasons of the restriction, and
we introduce a new hierarchical B-spline basis
that does not suffer from this issue.

\subsection{Approximation Quality of Uniform Hierarchical B-Splines}
\label{sec:321approximation}

\paragraph{Interpolation of polynomials}

Splines are a piecewise generalization of polynomials.
Approximation spaces spanned by splines of degree $p$ should
at least contain all polynomials of degree $\le p$.
Unfortunately, this statement is not true for uniform B-splines
$\bspl{l,i}{p}$ as we have defined them in the last section.
A counterexample is given in \cref{fig:nakInterpolation},
in which a cubic polynomial $\objfun$ is interpolated with
hierarchical cubic B-splines.
We can clearly see deviations of the interpolant from the polynomial
near the boundary,
where the pointwise relative error exceeds \SI{10}{\percent}.
The oscillations are even visible in the interior of the
spline interpolation domain $\rspldomain{l}{p}$.
Obviously, this phenomenon impairs the approximation quality
for other non-polynomial functions as well.

\begin{figure}
  \subcaptionbox{%
    Objective function $\objfun$ \emph{\textcolor{C0}{(blue)},}
    interpolant $\fgintp{l}$ \emph{\textcolor{C1}{(red, dashed)},}
    grid points \emph{(dots),} and
    spline interpolation domain $\rspldomain{l}{p}$
    \emph{(thick line).}%
  }[72mm]{%
    \includegraphics{nakInterpolation_1}%
  }%
  \hfill%
  \subcaptionbox{%
    Pointwise relative error
    $\abs{(\objfun - \fgintp{l})/\objfun}$ on a logarithmic scale.%
  }[72mm]{%
    \includegraphics{nakInterpolation_2}%
  }%
  \caption[%
    Issues when interpolating with uniform hierarchical B-splines%
  ]{%
    Hierarchical cubic B-splines $\bspl{l',i'}{p}$
    ($l' \le l$, $i' \in \hiset{l'}$, $p = 3$)
    fail to replicate a cubic polynomial $\objfun$
    (here: $\objfun(x) \ceq -10.2 x^3 + 14.7 x^2 - 5x + 0.7$)
    \vphantom{$\bspl{l',i'}{p}$}%
    when interpolating on the grid of level $l = 3$.%
    \vphantom{$\bspl{l',i'}{p}$}%
  }%
  \label{fig:nakInterpolation}%
\end{figure}

This issue can be explained as follows:
According to \thmref{cor:hierSplittingBSpline},
we have $\restrictedsplspace{l}{p}
= \bigoplus_{l'=0}^l \restrictspace{\hsbspl{l'}{p}}{\rspldomain{l}{p}}$
with $p = 3$.
Since cubic polynomials are also cubic splines,
it follows $\objfun \in \restrictedsplspace{l}{p}$ and hence
$\objfun \in \bigoplus_{l'=0}^l \restrictspace{\hsbspl{l'}{p}}{\rspldomain{l}{p}}$.
This means that there is a linear combination of hierarchical B-splines
$\bspl{l',i'}{p}$ ($l' \le l$, $i' \in \hiset{l'}$)
that replicates $\objfun$ on the whole domain $\rspldomain{l}{p}$
(not be confused with $\fgintp{l}$ in \cref{fig:nakInterpolation},
which does not replicate $\objfun$ exactly on $\rspldomain{l}{p}$).
However, in general, this interpolant is not equal $\objfun$ outside
$\rspldomain{l}{p}$ (i.e., in $\clint{0, 1} \setminus \rspldomain{l}{p}$),
as \thmref{prop:splineSpace} only holds for $\rspldomain{l}{p}$.
In particular, the interpolant evaluated at $x \in \{0, 1\}$ is not
equal to $\objfun(x)$.
If we now force the additional interpolation conditions in
$\gp{l,0} = 0$ and $\gp{l,2^l} = 1$,
the resulting interpolant $\fgintp{l}$ cannot be the same as the previous
interpolant,
which is why $\objfun$ and $\fgintp{l}$ differ inside $\rspldomain{l}{p}$.

\paragraph{Schoenberg--Whitney conditions}

Formally, the unique existence of an interpolating spline is
described by the \term{Schoenberg--Whitney conditions:}

\begin{proposition}[Schoenberg--Whitney conditions]
  \label{prop:schoenbergWhitneyConditions}
  Let $\knotseq = (\knot{0}, \dotsc, \knot{m+p})$ be a knot sequence
  and $t_0, \dotsc, t_{m-1}$ a sequence of interpolation points with
  $t_0 < \dotsb < t_{m-1}$ and
  $\knot{p} \le t_0 < t_{m-1} \le \knot{m}$.
  Then, there exists a unique interpolating spline
  $\spl = \sum_{k=0}^{m-1} \interpcoeff{k} \nonunifbspl{k,\knotseq}{p}$
  for arbitrary data if and only if
  \begin{equation}
    \knot{k} < t_k < \knot{k+p+1},\quad
    k = 0, \dotsc, m - 1.
  \end{equation}
\end{proposition}

\vspace*{0pt plus 0.3fill}

\begin{proof}
  See \cite{Hoellig13Approximation}.
\end{proof}

\vspace*{0pt plus 1.0fill}

The Schoenberg--Whitney conditions require that the interpolation points
are contained in $\rspldomain{l}{p}$,
which is not the case for $p = 3$ (see \cref{fig:nakInterpolation}),
as $\rspldomain{l}{p}$ does not contain the points $x = 0$ and $x = 1$.
For general degree $p$, the first $\tfrac{p-1}{2}$ and the
last $\tfrac{p-1}{2}$ grid points of level $l$ are
missing from $\rspldomain{l}{p}$,
thus violating the Schoenberg--Whitney conditions.
One possible remedy would be to move these interpolation points inside
$\rspldomain{l}{p}$ without changing the corresponding basis functions
(i.e., the knots stay the same) \cite{Hoellig13Approximation}.
For instance in the cubic case, we could move
$x = 0$ to $x = 1.5 \ms{l}$ and
$x = 1$ to $x = 1 - 1.5 \ms{l}$.
However, with this approach, we would not be able to interpolate
boundary values.
In addition, the condition of the interpolation problem will most likely
worsen if we place interpolation points near the ends of the supports
of the corresponding basis functions.

\pagebreak

\paragraph{Mismatch of dimensions}

To find a solution for this issue,
let $\wholesplspace{l}{p}$ denote the space of all splines of degree $p$
on the grid of level $l$, i.e., the space $\nonunifsplspace{\knotseq}{p}$ with
\begin{equation}
  \label{eq:fullGridKnots}
  \knot{k} \ceq (k - p) \ms{l},\quad
  k = 0, \dotsc, m + p,\quad
  m \ceq 2^l + p.
\end{equation}
We have $\spldomain{\knotseq}{p} = \clint{0, 1}$
for this choice of $\knotseq$.
Hence, the grid points $\gp{l,i}$ ($i = 0, \dotsc, 2^l$) satisfy
the Schoenberg--Whitney conditions for the uniform B-spline basis.
Clearly, the sum $\bigoplus_{l'=0}^l \hsbspl{l'}{p}$
is a subspace of $\wholesplspace{l}{p}$,
but it cannot be equal to $\wholesplspace{l}{p}$ due to
\begin{equation}
  \dim \bigoplus_{l'=0}^l \hsbspl{l'}{p}
  = 2^l + 1
  < 2^l + p
  = m
  = \dim \wholesplspace{l}{p},\quad
  p > 1,
\end{equation}
by \thmref{prop:splineSpace}.
There are too few nodal (and hierarchical) basis functions to
span the whole spline space $\wholesplspace{l}{p}$.

\paragraph{Restriction to spline subspaces}

The key idea is now to impose additional $p - 1$ boundary conditions
on the basis functions to restrict $\wholesplspace{l}{p}$ to a reasonable subspace
with the correct dimension $(2^l + p) - (p - 1) = 2^l + 1$.
``Reasonable'' means that besides this dimension constraint,
two requirements should be met:
First, the Schoenberg-Whitney conditions should be satisfied for
the new subspace and the grid of level $l$.
Second, the new subspace should contain all polynomials of degree $\le p$,
eliminating the issue discussed in \cref{fig:nakInterpolation}.

\subsection{Hierarchical Not-A-Knot B-Splines}
\label{sec:322NAKBSplines}

\paragraph{Not-a-knot conditions}

A suitable subspace can be obtained by incorporating the
so-called \term{not-a-knot boundary conditions} into $\wholesplspace{l}{p}$.
For the cubic case $p = 3$
(for which we need two conditions),
the not-a-knot conditions demand that
for all splines $\spl$ in the subspace,
$\deriv[3]{x}{s}$ is continuous at the first and at the last
interior knot $\gp{l,1} = \ms{l}$ and $\gp{l,2^l-1} = 1 - \ms{l}$
\cite{Hoellig13Approximation}.
This means that
$\gp{l,1}$ and $\gp{l,2^l-1}$ are effectively removed from the
knot sequence, as this is equivalent to requiring that
$\spl$ is a cubic polynomial on $\clint{0, \gp{l,2}}$ and
$\clint{\gp{l,2^l-2}, 1}$ (hence ``not-a-knot'').
For general degree $p$ (for which we need $(p - 1)$ conditions),
we require that the $p$-th derivative $\deriv[p]{x}{s}$
is continuous at the first $\tfrac{p-1}{2}$ and the last $\tfrac{p-1}{2}$
inner grid points
\begin{equation}
  \label{eq:removedNAKKnots}
  \gp{l,i},\quad
  i \in \{1, \dotsc, \tfrac{p-1}{2}\} \cup
  \{2^l - \tfrac{p-1}{2}, \dotsc, 2^l - 1\}.
\end{equation}
This is equivalent to removing these knots
from the knot sequence $\knotseq$,
or, alternatively, to requiring that $\spl$ is a polynomial
of degree $\le p$ on $\clint{0, \gp{l,(p+1)/2}}$ and on
$\clint{\gp{l,2^l-(p+1)/2}, 1}$.

\usenotation{zzzznak}
The knot sequence $\nodalknotseq[\nak]{l}{p}$
with not-a-knot boundary conditions is defined as follows:
\begin{subequations}
  \begin{gather}
    \nodalknotseq[\nak]{l}{p}
    \ceq (\nodalknot[\nak]{l,0}{p}, \dotsc,
    \nodalknot[\nak]{l,m+p}{p}),\quad
    m \ceq 2^l + 1,\\
    \nodalknot[\nak]{l,k}{p}
    \ceq
    \begin{cases}
      \gp{l,k-p},&
      k = 0, \dotsc, p,\\
      \gp{l,k-(p+1)/2},&
      k = p + 1, \dotsc, 2^l,\\
      \gp{l,k-1},&
      k = 2^l + 1, \dotsc, 2^l + p + 1.
    \end{cases}
  \end{gather}
\end{subequations}
This knot sequence $\nodalknotseq[\nak]{l}{p}$
can be obtained by removing the knots
given in \eqref{eq:removedNAKKnots} from the
knot sequence \eqref{eq:fullGridKnots} for the full grid of level $l$.
We show $\nodalknotseq[\nak]{l}{p}$ and the corresponding B-spline functions
in \cref{fig:splineSpaceNotAKnot}.
The resulting spline space
\begin{equation}
  \naksplspace{l}{p}
  \ceq \nonunifsplspace{\nodalknotseq[\nak]{l}{p}}{p}
\end{equation}
is a subspace
of $\wholesplspace{l}{p}$ with the desired dimensionality:
\begin{equation}
  \dim \bigoplus_{l'=0}^l \hsbspl{l'}{p}
  = 2^l + 1
  = \dim \naksplspace{l}{p}.
\end{equation}
The space $\naksplspace{l}{p}$ satisfies our two requirements:
\vspace{0.1em}%
First, the spline interpolation domain
$\spldomain{\nodalknotseq[\nak]{l}{p}}{p}
= \clint{\nodalknot[\nak]{l,p}{p}, \nodalknot[\nak]{l,m}{p}}$
of $\naksplspace{l}{p}$ equals the whole unit interval $\clint{0, 1}$.
\vspace{-0.3em}%
This means that the Schoenberg--Whitney conditions are satisfied
\vspace{0.1em}%
for $\naksplspace{l}{p}$, since all interpolation points
(grid points) are contained in
$\spldomain{\nodalknotseq[\nak]{l}{p}}{p} = \clint{0, 1}$.
\vspace{-0.3em}%
Second, $\naksplspace{l}{p}$ still contains all polynomials of
degree $\le p$, as we have only removed knots compared to
$\wholesplspace{l}{p}$.

\begin{figure}
  \includegraphics{splineSpace_3}%
  \caption[%
    Nodal not-a-knot B-splines and knot sequence%
  ]{%
    Not-a-knot knot sequence $\nodalknotseq[\nak]{l}{p}$
    \emph{(ticks on horizontal axis)}
    and nodal cubic not-a-knot B-splines ($p = 3$)
    of level $l = 3$.
    In the domain $\clint{0, 1}$ \emph{(delimited by dashed lines),}
    the first $\tfrac{p-1}{2}$ and the last $\tfrac{p-1}{2}$ interior grid points
    of the set of grid points $\fgset{l}$
    \emph{\textcolor{mittelblau}{(blue dots)}}
    have been removed from the set of knots.
    The spline interpolation domain
    $\spldomain{\nodalknotseq[\nak]{l}{p}}{p}$
    \emph{(thick line)}
    equals the whole domain $\clint{0, 1}$.%
  }%
  \label{fig:splineSpaceNotAKnot}%
\end{figure}

\vspace*{\fill}

However, $\bigoplus_{l'=0}^l \hsbspl{l'}{p}$ is not a subspace of
$\naksplspace{l}{p}$ anymore,
since the hierarchical basis functions $\bspl{l',i'}{p}$ are not
not-a-knot splines (due to the additional knots that we removed from
$\naksplspace{l}{p}$).
For this reason,
we have to incorporate the not-a-knot boundary conditions into the
hierarchical basis.

\vspace*{\fill}

Before defining the new hierarchical basis functions,
we make two additional observations.
First, $\nodalknotseq[\nak]{l}{p}$ coincides with the
uniform knot sequence $\nodalknotseq{l}{p}$ of \thmref{cor:nodalBSplineSpace}
for the piecewise linear case of $p = 1$.
This is intuitively clear:
For this case,
we do not need to remove any knots as the hierarchical splitting already
holds for the full domain by \cref{cor:hierSplittingHatMV}.
Second, the removal of the knots in \eqref{eq:removedNAKKnots}
is only possible if $p + 1 \le 2^l$,
which is equivalent to $l \ge \ceil{\log_2(p+1)}$.
For coarser levels,
there are not enough interior knots that could be removed.
Without any special treatment,
we would not be able to obtain enough basis functions to span the spline space.

\pagebreak

\paragraph{Definition of hierarchical not-a-knot B-splines}

For the definition of \term{hierarchical not-a-knot B-splines}
$\bspl[\nak]{l,i}{p}$ based on \thmref{def:nonUniformBSpline},
we use global Lagrange polynomials for the coarser levels:
\begin{subequations}
  \label{eq:hierarchicalNotAKnotBSpline}
  \begin{gather}
    \bspl[\nak]{l,i}{p}
    \ceq
    \begin{cases}
      \lagrangepoly{l,i},&
      l < \ceil{\log_2(p+1)},\\
      \nonunifbspl{i,\nodalknotseq[\nak]{l}{p}}{p},&
      l \ge \ceil{\log_2(p+1)},
    \end{cases}\quad
    l \in \natz,\quad
    i \in \hiset{l},\\
    \lagrangepoly{l,i}\colon \clint{0, 1} \to \real,\quad
    \lagrangepoly{l,i}(x)
    \ceq \!\!\prod_{\substack{i'=0,\dotsc,2^l\\i'\not=i}}
    \frac{x - \gp{l,i'}}{\gp{l,i} - \gp{l,i'}}.
  \end{gather}
\end{subequations}
The hierarchical not-a-knot B-spline basis is shown for the
cubic case $p = 3$ in \cref{fig:notAKnotBSpline}.
The function $\lagrangepoly{l,i}$ is the $i$-th
\term{Lagrange polynomial} of level $l$, that is,
the unique polynomial of degree $\le 2^l$ that interpolates the data
$\{(\gp{l,i'}, \kronecker{i}{i'}) \mid i' = 0, \dotsc, 2^l\}$.
Since its degree $\deg \lagrangepoly{l,i}$ is bounded by $2^l$,
we have $\deg \lagrangepoly{l,i} < p + 1$,
as the Lagrange polynomials are employed only when
$l < \ceil{\log_2(p+1)}$.
Due to $2^l$ even (when $l \ge 1$) and $p$ odd,
we can conclude from $\deg \lagrangepoly{l,i} \le 2^l \le p$ that actually
$\deg \lagrangepoly{l,i} \le 2^l < p$
(for $p > 1$; for $p = 1$, the case $l = 0$ is the exception).

\begin{figure}
  \subcaptionbox{%
    Nodal not-a-knot B-splines
    $\bspl[\nak]{l,i}{p}$ ($i \in \hiset{l}$)
    and grid points $\gp{l,i}$ \emph{(dots).}%
  }[67.5mm]{%
    \includegraphics{hierarchicalBasis_8}%
  }%
  \hfill%
  \begin{tikzpicture}
    \draw[decorate,decoration={brace,aspect=0.145}] (0,-8.5) -- (0,0);
    \node[anchor=east,inner sep=0mm] at (-0.15,-7.265) {$= \bigoplus$};
  \end{tikzpicture}%
  \hfill%
  \subcaptionbox{%
    Hierarchical not-a-knot B-splines
    $\bspl[\nak]{l',i'}{p}$ ($l' \le l$, $i' \in \hiset{l'}$)
    and grid points $\gp{l',i'}$ \emph{(dots).}%
  }[71mm]{%
    \includegraphics{hierarchicalBasis_9}%
  }%
  \caption[%
    Nodal and hierarchical not-a-knot B-splines%
  ]{%
    Univariate nodal and hierarchical cubic not-a-knot B-splines ($p = 3$)
    up to level $l = 3$.
    The nodal space $\nsbspl[\nak]{l}{p}$,
    which coincides with the not-a-knot spline space $\naksplspace{l}{p}$,
    decomposes into the direct sum
    of the hierarchical subspaces $\hsbspl[\nak]{l'}{p}$ ($l' \le l$).
    The knots of each level $l'$ are given by removing the
    first $\tfrac{p-1}{2}$ and last $\tfrac{p-1}{2}$
    inner points \emph{(crosses)}
    from the set of grid points $\gp{l',i'}$
    ($i' = 0, \dotsc, 2^{l'}$).%
  }%
  \label{fig:notAKnotBSpline}%
\end{figure}

\vspace*{\fill}

The motivation for using Lagrange polynomials for coarse levels
is that they form a basis of the polynomial space
and that they can be implemented and calculated quickly.
However, the specific choice of basis functions for the levels
$l < \ceil{\log_2(p + 1)}$ is arbitrary,
as long as these functions are linearly independent
(of each other and of the ``true'' not-a-knot B-splines
$\bspl[\nak]{l,i}{p}$, $l \ge \ceil{\log_2(p+1)}$)
and contained in the space $\naksplspace{l}{p}$.

\pagebreak

\paragraph{Implementation}

Note that in each level $l \ge \ceil{\log_2(p+1)}$,
only the first $\tfrac{p+1}{2}$
(indices $i = 1, 3, \dotsc, p$)
and the last $\tfrac{p+1}{2}$
(indices $i = 2^l - p, 2^l - p + 2, \dotsc, 2^l - 1$)
hierarchical basis functions $\bspl[\nak]{l,i}{p}$
differ from $\bspl{l,i}{p}$,
i.e., we have
\begin{equation}
  \bspl[\nak]{l,i}{p} = \bspl{l,i}{p},\quad
  i = p + 2,\; p + 4,\; \dotsc,\; 2^l - p - 2.
\end{equation}

\vspace*{\fill}

\noindent
This means that we can reuse uniform B-spline code
for the inner functions.
Due to $\bspl[\nak]{l,i}{p}(x) = \bspl[\nak]{l,2^l-i}{p}(1-x)$
(because of the symmetry of $\nodalknotseq[\nak]{l}{p}$),
we only have to reimplement $\tfrac{p+1}{2}$ not-a-knot B-splines per level $l$.
As $\bspl[\nak]{l,i}{p}$ and $\bspl[\nak]{l+1,i}{p}$
use the same knots up to an affine transformation for $l$ large enough
($l \ge 3$ suffices for $p = 3$),
only a number of special cases for coarse levels $l$ must be implemented.
In other words, the not-a-knot approach is ``minimally invasive''
with respect to an implementation that already uses uniform B-splines.

\pagebreak

\paragraph{Hierarchical splitting}

The main benefit of the hierarchical not-a-knot B-spline basis
is the validity of the hierarchical splitting.
As usual, we define $\nsbspl[\nak]{l}{p}$ and $\hsbspl[\nak]{l}{p}$
as the nodal and the hierarchical not-a-knot subspace of level $l$,
respectively.

\begin{proposition}[%
  univariate hierarchical splitting for not-a-knot B-splines%
]
  \label{prop:hierSplittingNAKBSplineUV}
  The hierarchical splitting \eqref{eq:hierSplittingUV}
  holds for the hierarchical not-a-knot B-spline basis:
  \begin{equation}
    \naksplspace{l}{p}
    = \nsbspl[\nak]{l}{p}
    = \bigoplus_{l'=0}^l \hsbspl[\nak]{l'}{p},
  \end{equation}
  where for $l < \ceil{\log_2(p+1)}$, $\naksplspace{l}{p}$
  is defined as the space $\polyspace{2^l}$ of polynomials of degree
  $\le 2^l$ on $\clint{0, 1}$.
  (For $l \ge \ceil{\log_2(p+1)}$,
  $\naksplspace{l}{p}$ is the not-a-knot spline space.)
\end{proposition}

\begin{proof}
  For $l < \ceil{\log_2(p+1)}$, all
  three spaces coincide with $\polyspace{2^l}$ and nothing is to prove.

  For $l \ge \ceil{\log_2(p+1)}$,
  we check the two conditions of \thmref{lemma:hierSplittingUV}.
  First, the hierarchical subspace $\hsbspl[\nak]{l'}{p}$ ($l' \le l$)
  is a subspace of $\naksplspace{l}{p} = \nsbspl[\nak]{l}{p}$.
  This is a conclusion of \thmref{prop:splineSpace}, as
  every function $\bspl[\nak]{l',i'}{p}$ ($i' \in \hiset{l'}$)
  is continuous on $\clint{0, 1}$, a polynomial on every knot interval of
  $\nodalknotseq[\nak]{l}{p}$, and at the knots themselves
  at least $(p - 1)$ times continuously differentiable.

  Second, the hierarchical functions $\bspl[\nak]{l',i'}{p}$
  ($l' \le l$, $i' \in \hiset{l'}$) are linearly independent.
  This can be shown similarly to the proof of
  \thmref{prop:hierBSplineLinearlyIndependent}.
  The linear independence of the Lagrange polynomials
  can be checked by inserting grid points into a zero linear combination.
  The linear combination collapses and only one term remains,
  the coefficient corresponding to the grid point.
  Hence, all coefficients must vanish.
\end{proof}

\vspace*{1em}

\begin{corollary}[%
  multivariate hierarchical splitting for not-a-knot B-splines%
]
  \label{cor:hierSplittingNAKBSplineMV}
  It holds
  \begin{equation}
    \naksplspace{\*l}{\*p}
    = \nsbspl[\nak]{\*l}{\*p}
    = \bigoplus_{\*l'=\*0}^\*l \hsbspl[\nak]{\*l'}{\*p},
  \end{equation}
  where $\naksplspace{\*l}{\*p}$ is the
  tensor product space of $\naksplspace{l_t}{p_t}$
  ($t = 1, \dotsc, d$) as defined in \cref{prop:hierSplittingNAKBSplineUV}.
\end{corollary}

\begin{proof}
  Follows directly from \thmref{prop:splittingUVToMV}.
\end{proof}

\paragraph{Sparse grids with not-a-knot B-splines}

Regular sparse grid spaces using the new hierarchical not-a-knot basis
are defined analogously to the uniform case, i.e.,
\begin{equation}
  \label{eq:sparseGridRegularNAK}
  \regsgspace[\*p,\nak]{n}{d}
  \ceq \bigoplus_{\normone{\*l} \le n} \hsbspl[\nak]{\*l}{\*p}.
\end{equation}
If the level $n$ is large enough, then $\regsgspace[\*p,\nak]{n}{d}$
contains the space $\polyspace{\*p}$ of all $d$-variate polynomials of
coordinate degree $\le \*p$ on $\clint{\*0, \*1}$
(i.e., functions $\objfun\colon \clint{0, 1} \to \real$ of the form
$\objfun(\*x) \ceq \sum_{\*q=\*0}^\*p \interpcoeff{\*q} \prod_{t=1}^d x_t^{q_t}$
with $\interpcoeff{\*q} \in \real$).
This means that in contrast to the uniform B-spline basis,
hierarchical not-a-knot B-splines on sparse grids are able to
replicate global polynomials on $\clint{\*0, \*1}$:

\begin{shortcorollary}[%
  sparse grid with not-a-knot B-splines contains polynomials%
]
  \label{cor:sparseGridRegularNAKPolynomials}
  If $n \ge \normone{\ceil{\veclog_\*2(\*p + \*1)}}$,
  then $\polyspace{\*p} \subset \regsgspace[\*p,\nak]{n}{d}$.
\end{shortcorollary}

\begin{proof}
  Let $\*l \ceq \ceil{\veclog_\*2(\*p + \*1)}$ and $n \ge \normone{\*l}$.
  By \cref{cor:hierSplittingNAKBSplineMV}, we have
  $\bigoplus_{\*l'=\*0}^{\*l} \hsbspl[\nak]{\*l'}{\*p} = \naksplspace{\*l}{\*p}$.
  In addition, all $\*l' \in \natz^d$ with $\*l' \le \*l$ satisfy
  $\normone{\*l'} \le n$ and thus,
  $\bigoplus_{\*l'=\*0}^{\*l} \hsbspl[\nak]{\*l'}{\*p} \subset
  \regsgspace[\*p,\nak]{n}{d}$ by \eqref{eq:sparseGridRegularNAK}.
  We conclude
  $\polyspace{\*p} \subset \naksplspace{\*l}{\*p} \subset
  \regsgspace[\*p,\nak]{n}{d}$, which is the asserted claim.
\end{proof}

\subsection{Modified and Non-Uniform Hierarchical Not-A-Knot B-Splines}
\label{sec:323modifiedNAKBSplines}

\paragraph{Modified hierarchical not-a-knot B-splines}

As for uniform and Clenshaw--Curtis B-splines
(\cref{sec:31standardBSplines}),
it is possible to define a modified version of the
hierarchical not-a-knot B-spline basis to obtain
``reasonable'' boundary values without boundary points.
However, we cannot use \thmref{lemma:marsden} similarly to
\eqref{eq:modifiedBSplineConstruction}:
Due to the removal of knots, there is only a single
not-a-knot B-spline $\bspl[\nak]{l,0}{p}$ left of
$\bspl[\nak]{l,1}{p}$.
B-splines $\bspl[\nak]{l,i}{p}$ with index $i < 0$
would vanish on $\clint{0, 1}$.

While we are therefore not able to construct modified functions
whose second derivative vanishes in a neighborhood of $x = 0$,
we can use $\bspl[\nak]{l,0}{p}$ to let the
second derivative vanish in $x = 0$ itself:
\begin{equation}
  \label{eq:modifiedNotAKnotBSpline}
  \bspl[\nak,\modified]{l,i}{p}(x)
  \ceq
  \begin{cases}
    1,&
    l = 1,\quad i = 1,\\
    \bspl[\nak]{l,1}{p}(x)
    - \dfrac{\deriv[2]{x}{\bspl[\nak]{l,1}{p}}(0)}%
    {\deriv[2]{x}{\bspl[\nak]{l,0}{p}}(0)}
    \bspl[\nak]{l,0}{p}(x),&
    l \ge 2,\quad i = 1,\\
    \bspl[\nak]{l,i}{p}(x),&
    l \ge 2,\quad i \in \hiset{l} \setminus \{1, 2^l - 1\},\\
    \bspl[\nak,\modified]{l,1}{p}(1 - x),&
    l \ge 2,\quad i = 2^l - 1.
  \end{cases}
  \hspace*{-2mm}
\end{equation}
The resulting modified hierarchical not-a-knot B-spline basis
$\bspl[\nak,\modified]{l,i}{p}$ is shown with dashed lines
in \cref{fig:modifiedNotAKnotBSpline}.
As before, we have to implement $\bspl[\nak,\modified]{l,1}{p}$
only for a single level $l$, as modified functions of higher levels
are the same up to an affine parameter transformation.
Note again that for $p \ge 5$, we would have to modify additional
interior B-splines as the interior of their support then extends to the
boundary of $\clint{0, 1}$.
We refrain from doing so to keep the definition
\eqref{eq:modifiedNotAKnotBSpline} simple.

\paragraph{Non-uniform hierarchical not-a-knot B-splines}

The not-a-knot construction is completely independent of the
distribution of the grid points at hand.
Consequently, we can define hierarchical not-a-knot B-splines
for non-uniform distributions.
For instance, to define not-a-knot B-splines for
Chebyshev points (see \cref{sec:314nonUniform}),
we first specify the knot sequence as
\begin{subequations}
  \begin{gather}
    \nodalknotseq[\cc,\nak]{l}{p}
    \ceq (\nodalknot[\cc,\nak]{l,0}{p}, \dotsc,
    \nodalknot[\cc,\nak]{l,m+p}{p}),\quad
    m \ceq 2^l + 1,\\
    \nodalknot[\cc,\nak]{l,k}{p}
    \ceq
    \begin{cases}
      \ccgp{l,k-p},&
      k = 0, \dotsc, p,\\
      \ccgp{l,k-(p+1)/2},&
      k = p + 1, \dotsc, 2^l,\\
      \ccgp{l,k-1},&
      k = 2^l + 1, \dotsc, 2^l + p + 1,
    \end{cases}
  \end{gather}
\end{subequations}
and then define hierarchical not-a-knot Clenshaw--Curtis B-splines as
\begin{subequations}
  \begin{gather}
    \bspl[\cc,\nak]{l,i}{p}
    \ceq
    \begin{cases}
      \lagrangepoly[\cc]{l,i},&
      l < \ceil{\log_2(p+1)},\\
      \nonunifbspl{i,\nodalknotseq[\cc,\nak]{l}{p}}{p},&
      l \ge \ceil{\log_2(p+1)},
    \end{cases}\quad
    l \in \natz,\quad
    i \in \hiset{l},\\
    \lagrangepoly[\cc]{l,i}\colon \clint{0, 1} \to \real,\quad
    \lagrangepoly[\cc]{l,i}(x)
    \ceq \!\!\prod_{\substack{i'=0,\dotsc,2^l\\i'\not=i}}
    \frac{x - \ccgp{l,i'}}%
    {\ccgp{l,i} - \ccgp{l,i'}}.
  \end{gather}
\end{subequations}

This definition can even be combined with the modification
of hierarchical not-a-knot B-splines as discussed above.
We can use exactly the same approach as in
\eqref{eq:modifiedNotAKnotBSpline}, if we replace the
not-a-knot basis functions with their non-uniform not-a-knot counterparts
(not-a-knot Clenshaw--Curtis B-splines in the above example).
The hierarchical not-a-knot Clenshaw--Curtis B-spline basis of
cubic degree and the corresponding modified functions are shown in
\cref{fig:clenshawCurtisNotAKnotBSpline}.

\begin{figure}
  \subcaptionbox{%
    $\bspl[\nak]{l',i'}{p}$,
    $\bspl[\nak,\modified]{l',i'}{p}$
    \emph{(dashed),} and $\gp{l',i'}$ \emph{(dots).}%
    \label{fig:modifiedNotAKnotBSpline}%
  }[73mm]{%
    \includegraphics{hierarchicalBasis_10}%
  }%
  \hfill
  \subcaptionbox{%
    $\bspl[\cc,\nak]{l',i'}{p}$,
    $\bspl[\cc,\nak,\modified]{l',i'}{p}$
    \emph{(dashed),} and $\ccgp{l',i'}$ \emph{(dots).}%
    \label{fig:clenshawCurtisNotAKnotBSpline}%
  }[76mm]{%
    \includegraphics{hierarchicalBasis_11}%
  }%
  \caption[%
    Comparison of hierarchical not-a-knot B-splines%
  ]{%
    Comparison of uniform \emph{(left)} and
    Clenshaw--Curtis \emph{(right)} hierarchical cubic not-a-knot
    B-splines $\bspl[\nak]{l',i'}{p}$ and
    $\bspl[\cc,\nak]{l',i'}{p}$
    ($l ' \le l$, $i' \in \hiset{l'}$, $p = 3$) up to level $l = 3$
    together with the respective modified versions
    $\bspl[\nak,\modified]{l',i'}{p}$ and
    $\bspl[\cc,\nak,\modified]{l',i'}{p}$
    \emph{(dashed).}
    The knots of each level $l'$ are given by removing the
    first $\tfrac{p-1}{2}$ and last $\tfrac{p-1}{2}$
    inner points \emph{(crosses)}
    from the set of grid points $\gp{l',i'}$ or
    $\ccgp{l',i'}$
    ($i' = 0, \dotsc, 2^{l'}$), respectively.%
  }%
  \label{fig:uniformAndClenshawCurtisNotAKnotBSpline}%
\end{figure}

\subsection{Other Approaches to Incorporate Boundary Conditions}
\label{sec:324naturalBoundary}

Not-a-knot boundary conditions are not the only approach
to obtain a subspace of $\wholesplspace{l}{p}$ with the right dimension $2^l - 1$.
\term{Natural boundary conditions} are another possibility,
which we want to discuss briefly.
In the cubic case, for which they are usually formulated
\cite{Hoellig13Approximation},
these boundary conditions require that the
second derivatives $\deriv[2]{x}{\basis{l,i}}$ of the
basis functions vanish at the boundary $x \in \{0, 1\}$.
To obtain the necessary number of $p - 1$ constraints also
for higher degrees $p$,
we require that all derivatives
$\deriv[q]{x}{\basis{l,i}}$ of order
$q = 2, \dotsc, \tfrac{p+1}{2}$ vanish at $x \in \{0, 1\}$.

Consequently, we can define hierarchical natural B-splines as
\begin{subequations}
  \begin{gather}
    \bspl[\ntrl]{l,i}{p}(x)
    \ceq
    \begin{cases}
      \lagrangepoly{0,i}(x),&
      l = 0,\\
      \bspl{l,i}{p} +
      \sum_{j \in J_i^{p,\ntrl}} c_{l,i,j} \bspl{l,j}{p},&
      l \ge 1,
    \end{cases}\quad
    l \in \natz,\quad
    i \in \hiset{l},\\
    J_i^{p,\ntrl}
    \ceq \{i-\tfrac{p-1}{2}, \dotsc, i-1\} \cup
    \{i+1, \dotsc, i+\tfrac{p-1}{2}\},
  \end{gather}
\end{subequations}
where the coefficients $c_{l,i,j} \in \real$ are chosen such that
the natural boundary conditions are satisfied:
\begin{equation}
  \deriv[q]{x}{\bspl[\ntrl]{l,i}{p}}(x)
  = 0,\quad
  l \ge 1,\quad
  i \in \hiset{l},\quad
  q = 2, \dotsc, \tfrac{p+1}{2},\quad
  x \in \{0, 1\}.
\end{equation}
The first half of the coefficients $c_{l,i,j} \in \real$
($j < i$) vanishes if the interior of the support of $\bspl{l,i}{p}$
does not contain $x = 0$
(i.e., $i \ge \tfrac{p+1}{2}$).
The second half of the coefficients~($j > i$) vanishes analogously
if $1 \notin \interiorsupp \bspl{l,i}{p} \iff i \le 2^l - \tfrac{p+1}{2}$.
This means that
only the first $\floor{\tfrac{p+1}{4}}$ and the last $\floor{\tfrac{p+1}{4}}$
hierarchical functions have to be altered in each level.

\Cref{fig:naturalBSpline} shows the hierarchical natural spline basis.
The main disadvantage of natural boundary conditions is that
we are not able to replicate arbitrary polynomials exactly on $\clint{0, 1}$
with this approach.
Only polynomials that satisfy natural boundary conditions themselves
(linear polynomials for example)
can be replicated exactly.
For this reason, we do not further consider this basis in the
rest of the thesis.

\begin{SCfigure}
  \includegraphics{hierarchicalBasis_12}%
  \caption[%
    Hierarchical natural B-splines%
  ]{%
    Hierarchical cubic natural B-splines
    \vspace{-0.1em}%
    $\bspl[\ntrl]{l',i'}{p}$
    ($l' \le l$, $i' \in \hiset{l'}$, $p = 3$) and
    \vspace{0.05em}%
    grid points $\gp{l',i'}$ \emph{(dots)} up to level $l = 3$.%
  }%
  \label{fig:naturalBSpline}%
\end{SCfigure}


\documentclass[jou]{apa6}

\usepackage[american]{babel}

\usepackage{csquotes}
\usepackage[style=apa,sortcites=true,sorting=nyt,backend=biber]{biblatex}
\DeclareLanguageMapping{american}{american-apa}
\addbibresource{bibliography.bib}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Discrete Structures
%% The start of RBS stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Working internal and external links in PDF
\usepackage{hyperref}
% Extra math symbols in LaTeX
\usepackage{amsmath}
\usepackage{gensymb}
\usepackage{amssymb}
% Enumerations with (a), (b), etc.
\usepackage{enumerate}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage{xcolor}

\let\OLDitemize\itemize
\renewcommand\itemize{\OLDitemize\addtolength{\itemsep}{-6pt}}

\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=3pt \partopsep=3pt }
\makeatother

% These sizes redefine APA for A4 paper size
\oddsidemargin 0.0in
\evensidemargin 0.0in
\textwidth 6.27in
\headheight 1.0in
\topmargin -24pt
\headheight 12pt
\headsep 12pt
\textheight 9.19in

\title{Sample Quiz 8}
\author{Discrete Structures, Spring 2020}
\affiliation{RBS}

\leftheader{Discrete Sample Quiz 8}

\abstract{%
}

%\keywords{}

\setlength\parindent{0pt}

\begin{document}

%\thispagestyle{empty}

\twocolumn
\section{Final Exam, 2020-04-23}

\vspace{4pt}
{\bf Question 1.}\\
By $U$ we denote the set of all positive integers
between $1$ and $120$. This is the {\em universe} in which
we define several subsets:
$$\left\{ \begin{array}{l}
A = \{ x \in U\;\mid\;2\mid{}x\},\\
B = \{ x \in U\;\mid\;3\mid{}x\},\\
C = \{ x \in U\;\mid\;5\mid{}x\},\\
X = \{ x \in U\;\mid\;2\mid{}x\;\vee\;3\mid{}x\},\\
Y = \{ x \in U\;\mid\;(3\mid{}x\;\wedge\;5\mid{}x)\;\vee\neg(2\mid{}x)\}.
\end{array} \right.$$

{\bf (A)} Express $X$ using the sets $A,B,C$ (using set union $V \cup W$,
set intersection $V \cap W$, set complement $\overline{V}$ operations).\\
{\bf (B)} Express $Y$ using the sets $A,B,C$ in a similar way.\\
{\bf (C)} Find $|X|$ - the size of the set $X$.\\
{\bf (D)} Find $|Y|$ - the size of the set $Y$.

\vspace{10pt}
{\bf Question 2.}\\
Let $A$ and $B$ be sets with sizes $|A| = 8$ and $|B| = 5$
and $|A \cap B| = 3$.

Calculate the largest and the smallest possible values
for each of the following set sizes:

{\bf (A)} $|A \cup B|$.\\
{\bf (B)} $|A \times (B \times B)|$.\\
{\bf (C)} $\left| \mathcal{P}(\mathcal{P}(A \cap B)) \right|$ - the
powerset of a powerset of $A \cap B$.\\
{\bf (D)} $|A \oplus B|$ - the symmetric difference of the sets $A$ and $B$.

\vspace{10pt}
{\bf Question 3.}\\
Consider the following recurrent sequence:

$$\left\{ \begin{array}{l}
a_0 = 3\\
a_1 = 4\\
a_{n+2} = 5a_{n+1} - 6a_n,\;\text{if}\;n \geq 0 \\
\end{array} \right.$$

Assume that $b_n$ is another sequence satisfying the
recurrence rule
$$b_{n+2} = 5b_{n+1} - 6b_n,\;\text{if}\;n \geq 0$$
(The first two members $b_0,b_1$ are not known.)

{\bf (A)} Write the first $6$ members of this sequence ($a_0,\ldots,a_5$).\\
{\bf (B)} Write the characteristic equation for this sequence.\\
{\bf (C)} Write the general expression for an arbirary sequence $b_n$
satisfying the recurrent expression as a sum of two geometric progressions
(you can leave unknown coefficients in your answer; just explain which ones they are).\\
{\bf (D)} Write the formula to compute $a_n$ (that would satisfy
the initial conditions $a_0 = 3$ and $a_1 = 4$).

\vspace{10pt}
{\bf Question 4.}\\
Consider this code snippet in Python:

\begin{verbatim}
n = 1000
sum = 0
for i in range(1, n*n+1):
    for j in range(1,i+1):
        sum += i % j
\end{verbatim}

And a similar one in R:

\begin{verbatim}
n <- 1000
sum <- 0
for (i in 1:(n*n)) {
    for (j in 1:i) {
        sum <- sum + i %% j
    }
}
\end{verbatim}

{\bf (A)} Explain in human language what this algorithm does.\\
{\bf (B)} Denote by $f(n)$ the
number of times the variable `sum` is incremented.
Write the Big-O-Notation for $f(n)$. Find
a function $g(n)$ such that
$f(n)$ is in $O(g(n))$. (If there are multiple functions,
pick the one with the slowest growth.)\\
{\bf (C)} Express the function $f(n)$ precisely -
how many times `sum` is incremented in terms of variable $n$.

\vspace{10pt}
{\bf Question 5.}

Let $A$ be the set of all positive divisors of the number $120$
(including $1$ and $120$ itself).

{\bf (A)} What is the multiplication of all
numbers in the set $A$?\\
{\bf (B)} Express this number as the product of prime powers.

\vspace{10pt}
{\bf Question 6.}

Define the following binary relationship on the set of
integer numbers $\mathbb{Z}$:
We say that $aRb$ (numbers $a,b \in \mathbb{Z}$ are in the relation $R$) iff
$$\left\{ \begin{array}{l}
a - b \equiv 0\,(\text{mod}\;11)\\
a - b \equiv 0\,(\text{mod}\;12)\\
a - b \equiv 0\,(\text{mod}\;13)
\end{array} \right.$$

\begin{tabular}{|l|l|l|} \hline
{\bf Item} & {\bf Statement} & {\bf True or False?} \\ \hline
{\bf (A)} & $R$ is reflexive &  \\ \hline
{\bf (B)} & $R$ is symmetric &  \\ \hline
{\bf (C)} & $R$ is antisymmetric &  \\ \hline
{\bf (D)} & $R$ is transitive &  \\ \hline
{\bf (E)} & $aRb$ iff $a=b$ &  \\ \hline
\end{tabular}

For all items where you answered `FALSE`, specify a counterexample
(values for some numbers that would make the condition true, but the
conclusion false).
If the statement was true, write "none".

{\bf (A)} counterexample: $\ldots$\\
{\bf (B)} counterexample: $\ldots$\\
{\bf (C)} counterexample: $\ldots$\\
{\bf (D)} counterexample: $\ldots$\\
{\bf (E)} counterexample: $\ldots$

\vspace{10pt}
{\bf Question 7.}\\
Four people $A,B,C,D$ each has his own hat.
After the meeting they leave their
building in a hurry, everyone grabs some hat at random
so that all $4!$ permutations of the hats have equal probabilities.

Let the random variable $X$ denote the number of hats that
were picked up correctly. (For example, if
the hat assignment is this: $(A \rightarrow A,
B \rightarrow B, C \rightarrow D, D \rightarrow C)$, then
$X = 2$, because two people got their own hats.)

{\bf (A)}  Find $E(X)$ - the expected value of $X$.\\
{\bf (B)}  Find $V(X)$ - the variance of $X$.

\vspace{10pt}
{\bf Question 8.}\\
There was a crooked man who had a crooked 1 euro coin.
On lucky days it would flip the *heads* with probability $p=\frac{2}{3}$,
and the *tails* with probability $p=\frac{1}{3}$, but on unlucky days
it was the opposite ($p(\mathtt{heads})=\frac{1}{3}$, but
$p(\mathtt{tails})=\frac{2}{3}$).
There were equal probabilities of $\frac{1}{2}$ for lucky and unlucky days.

One morning he flipped the coin $5$ times and altogether got three {\em heads}
and two {\em tails}.

Let us introduce the following events:

\begin{itemize}
\item $E$ (evidence): Five coin tosses result in three {\em heads} and two {\em tails}.
\item $H$ (hypothesis): The current day is lucky.
\end{itemize}

{\bf (A)} Find $P(E|H)$ - the conditional probability of $E$ given that the
day is lucky.\\
{\bf (B)} Find $P(E|H)\cdot P(H)$ - the probability that the day
is lucky and $E$ happens.\\
{\bf (C)} Find $P(E|\overline{H})$ - the conditional probability of $E$
given that the day is not lucky.\\
{\bf (D)} Find $P(E|\overline{H})\cdot P(\overline{H})$ - the probability
that the day is unlucky and $E$ happens.\\
{\bf (E)} Find $P(E)$ - as the sum of two probabilities ($E$ happened
on a lucky day and also $E$ happened on unlucky day).\\
{\bf (F)} Find the conditional probability $P(H|E)$ -
the likelyhood that the croocked man has a lucky day, given
that the event $E$ has happened.

\vspace{10pt}
{\bf Question 9.}

\begin{figure}[!htb]
\center{\includegraphics[width=2in]{final/final-syntax-tree.png}}
\caption{\label{fig:final-syntax-tree} A tree for an expression.}
\end{figure}

The syntax tree describes an algebraic expression (please note
the difference between the unary minus that
flips the value of the variable $y$ and
the binary minus that subtracts the
two subexpressions: $-y$ and $6 \times 3$).

{\bf (A)} Write the preorder DFS traversal of
this tree.\\
{\bf (B)} Write the inorder DFS traversal of
this tree.\\
{\bf (C)} Write the postorder DFS traversal of
this tree.

{\em Note.} In all $3$ answers denote the unary
minus with the tilde sign $\sim$,
but the regular/binary minus with $-$.

\vspace{10pt}
{\bf Question 10.}\\
\begin{figure}[!htb]
\center{\includegraphics[width=2in]{final/prim-weighted-graph.png}}
\caption{\label{fig:prim-weighted-graph} A graph with 9 vertices.}
\end{figure}

Run the Prim's algorithm on the weighted graph in Figure~\ref{fig:prim-weighted-graph},
start growing the tree from the vertex $I$.

\begin{tabular}{|l|l|} \hline
{\bf Step} & {\bf Newly Added Edge} \\ \hline
{\bf Step 1} & \\ \hline
{\bf Step 2} & \\ \hline
{\bf Step 3} & \\ \hline
{\bf Step 4} & \\ \hline
{\bf Step 5} & \\ \hline
{\bf Step 6} & \\ \hline
{\bf Step 7} & \\ \hline
{\bf Step 8} & \\ \hline
\end{tabular}

What is the total weight of the obtained Minimum Spanning Tree?

\mbox{}
\newpage
\subsection{Answers}

Every problem is worth $15$ points. The total for this final is 150 points.

\vspace{4pt}
{\bf Question 1.}\\

{\bf (A)} $X = A \cup B$ (Boolean OR means set union)\\
{\bf (B)} $Y = (B \cap C) \cup \overline{A}$ (Boolean and means set intersection; negation means set complement)\\
{\bf (C)} $|X| = |A| + |B| - |A \cap B| = 60+40-20 = 80$ (principle of inclusion-exclusion).\\
{\bf (D)} $|Y|$ is all odd numbers and also four even numbers divisible by $15$
($30, 60, 90, 120$). The total is $60 + 4 = 64$.

{\scriptsize
{\em Grading.}
\begin{itemize}
\item Each correct answer is $3$ points (total $12$).
\item Explaining both {\bf (C}) and {\bf (D)} is another $3$ points (total $3$).
\item Using wrong set notation ($\wedge$ instead of $\cap$ etc.) divides the number of points in half.
\item $68$ instead of $64$ is $2$ points (instead of $3$).
\end{itemize}
}

\vspace{10pt}
{\bf Question 2.}\\
In all the answers the largest and the smallest value are equal, because
we know exactly how the two sets intersect; how many elements belong to just
one of the sets $A$, $B$, and how many elements belong to the both sets.

{\bf (A)} $|A \cup B| = |A| + |B| - |A \cap B| = 8+5-3 = 10$ (the principle of inclusion-exclusion).\\
{\bf (B)} $|A \times (B \times B)| = 8 \cdot 5 \cdot 5 = 200$\\
(Cartesian product has size that is the product of all participant sets:
one can combine three elements from the sets $A$, $B$ and $B$ in this many ways).\\
{\bf (C)} $2^{2^3} = 2^8 = 256$ (the number of elements in the powerset
of any set $X$ can be obtained by raising $2$ to the power $|X|$).\\
{\bf (D)} $|A \oplus B| = (8-3) + (5-3) = 7$ (we remove the common elements from both $A$ and $B$).

{\scriptsize
{\em Grading.}
\begin{itemize}
\item Each correct answer is $3$ points (total $12$).
\item Expressions or verbal explanations of the answers is $3$ points (total $3$).
\end{itemize}
}

\vspace{10pt}
{\bf Question 3.}\\
{\bf (A)} $a_0 = 3$,\\
$a_1 = 4$,\\
$a_2 = 5\cdot 4 - 6 \cdot 3 = 2$,\\
$a_3 = 5\cdot 2 - 6 \cdot 4 = -14$,\\
$a_4 = 5\cdot(-14) - 6 \cdot 2 = -82$,\\
$a_5 = 5\cdot(-82) - 6 \cdot (-14) = -326$,\\
$a_6 = 5\cdot(-326) - 6 \cdot (-82) = -1138$.

{\bf (B)} The characteristic equation is obtained, if we try to find $a_n$
in the form of a geometric progression $r^n$:
$r^{n+2} = 5r^{n+1} - 6r^n,$ or
$r^2 -5r + 6 = 0$.
It has two roots: $r_1 = 2$, $r_2 = 3$.

{\bf (C)} The general form of the expression for any iterative
sequence $b_n$ satisfying the relationship $b_{n+2} = 5b_{n+1} - 6b_n$ is as follows:
$$b_n = A \cdot 2^n + B \cdot 3^n,$$
where $A,B$ are two constants that depend on the two initial values of the sequence $b_n$.

{\bf (D)} We need to solve a system of two equations, to ensure that the formula
$a_n = A \cdot 2^n + B \cdot 3^n$ has correct values for $n=0$ and $n=1$.
We get the following system:
$$\left\{ \begin{array}{l}
A + B = 3,\\
2A + 3B = 4.\\
\end{array} \right.$$
Substitute $B = 3-A$ into the second equation. We get that
$2A + 9 - 3A = 4$ and $A = 5$. We also get that $B = -2$.
Therefore the exact formula to calculate the sequence $a_n$ is this:
$$a_n  = 5 \cdot 2^n - 2 \cdot 3^n,\;\text{where}\;n \geq 0.$$
This actually works, if we plug in the values calculated in {\bf (A)} for $n = 0,\ldots,6$.

{\scriptsize
{\em Grading.}
\begin{itemize}
\item Answer in {\bf (A)} is $4$ points.
\item Answer in {\bf (B)} is $4$ points.
\item Answer in {\bf (C)} is $3$ points.
\item Answer in {\bf (D)} is $4$ points.
\end{itemize}
}

\vspace{10pt}
{\bf Question 4.}\\
{\bf (A)} The algorithm takes all numbers $i$ from $1$ to $n^2$ and
divides them by all the smaller numbers $j < i$, and adds up all the obtained remainders.

{\bf (C)} The outer loop is repeated $n^2$ times. The inner loop is repeated
$1 + 2 + 3 + \ldots + n^2$ times. This is an arithmetic progression.
The sum of an arithmetic progression is the arithmetic mean of the first and the last
member multiplied by the number of members:
$$f(n) = \frac{1 + n^2}{2} \cdot n^2 = \frac{n^4 + n^2}{2}.$$

{\bf (B)} $f(n)$ is in $O(n^4)$. Therefore we can take $g(n) = n^4$. We can
pick another $g(n)$ that is multiplied by some nonzero constant
(such as $\frac{n^4}{2}$ or $17n^4$ or anything else - that also counts
as a valid answer).
Certainly, $f(n)$ is also in $O(n^k)$ for any $k > 4$, but the function $g(n) = n^4$
is the slowest growing.

{\scriptsize
{\em Grading.}
\begin{itemize}
\item Answer for {\bf (A)} is $5$ points.
\item Answer for {\bf (B)} (any 4th degree polynomial of $n$) is $5$ points.
An attempt to estimate some arithmetic progression with a different upper limit
(say, $1 + 2 + \ldots + n$) gets $2$ points.
\item Answer for {\bf (C)} is $5$ points. If the answer is
provided just for $n=1000$ (not for any variable), then it is $4$ points.
\end{itemize}
}

\vspace{10pt}
{\bf Question 5.}\\
{\bf (A)} If expressed as a product of two positive integers $120 = ab$,
one of the divisors $a$ or $b$ would be smaller than $\sqrt{120} \approx 11$, and the other
one would be bigger. We can easily list all the ways to express $120$
as a product of two integers:
$$1 \cdot 120 = 2 \cdot 60 = 3 \cdot 40 = 4 \cdot 30 = $$
$$= 5 \cdot 24 = 6 \cdot 20 = 8 \cdot 15 = 10 \cdot 12,$$
and there are no other factorizations, since all the divisors less than $11$ are
already listed.
Multiplying them all together would give
$$(120)^8 = 42998169600000000$$

{\bf (B)} As a product of prime factors:
$$(120)^8 = (2^3 \cdot 3 \cdot 5)^8 = 2^{24} \cdot 3^8 \cdot 5^8.$$

{\scriptsize
{\em Grading.}
\begin{itemize}
\item Correct item {\bf (A)} is $7$ points (also floating point answers were fine -
not all had easy access to the big integer arithmetic).
\item Correct item {\bf (B)} is $8$ points.
\end{itemize}
}

\vspace{10pt}
{\bf Question 6.}\\

\begin{tabular}{|l|l|l|} \hline
{\bf Item} & {\bf Statement} & {\bf True or False?} \\ \hline
{\bf (A)} & $R$ is reflexive & TRUE \\ \hline
{\bf (B)} & $R$ is symmetric & TRUE \\ \hline
{\bf (C)} & $R$ is antisymmetric & FALSE \\ \hline
{\bf (D)} & $R$ is transitive & TRUE \\ \hline
{\bf (E)} & $aRb$ iff $a=b$ & FALSE \\ \hline
\end{tabular}

{\bf (A)} Counterexample: None\\
{\bf (B)} Counterexample: None\\
{\bf (C)} Consider counterexample $a=0$, $b = 11 \cdot 12 \cdot 13 = 1716$.
While it is true that $aRb$ and $bRa$, nevertheless $a \neq b$.\\
{\bf (D)} Counterexample: None\\
{\bf (E)} Counterexample is same as in {\bf (C)}: $a=0$, $b = 1716$.

{\scriptsize
{\em Grading.}
\begin{itemize}
\item One correct answer is $2$ points (total $10$).
\item Counterexamples for {\bf (C)} and {\bf (E)} is $5$ points (they are, in fact, the same).
\end{itemize}
}

\vspace{10pt}
{\bf Question 7.} Answer: {\tt 17}\\

\begin{itemize}
\item For $1$ of $24$ permutations $X = 4$ (all hats stay in place),
\item For $0$ permutations $X = 3$ (it is not possible for exactly three hats to stay in place, because
then the 4th hat also returns to its owner),
\item For $6$ of $24$ permutations $X = 2$ (there are ${4 \choose 2} = 6$ ways how to pick $2$ hats
that stay in place; and the remaining two hats can switch places only in one way),
\item For $8$ of $24$ permutations $X = 1$ (there are ${4 \choose 1} = 4$ ways how to pick $1$ hat
that stays in place; and the remaining three hats can rotate in two ways).
\item For the remaining $24 - (1 + 6 + 8) = 9$ permutations $X = 0$ (no hats stay in place).
\end{itemize}

{\scriptsize
\begin{table}[h!]
\begin{center}
\caption{Random Variable $X$ for the Hat Problem}
\begin{tabular}{|l|r|r|r|} \hline
\label{table:t1}
{\bf Permutation} & $X$ & $X-E(X)$ & $(X-E(X))^2$ \\ \hline
$\textcolor{red}{\mathtt{ABCD}}$ & $4$ & $3$ & $9$ \\ \hline
$\textcolor{red}{\mathtt{AB}}\mathtt{DC}$ & $2$ & $1$ & $1$ \\ \hline
$\textcolor{red}{\mathtt{A}}\mathtt{CB}\textcolor{red}{\mathtt{D}}$ & $2$ & $1$ & $1$ \\ \hline
$\textcolor{red}{\mathtt{A}}\mathtt{CDB}$ & $1$ & $0$ & $0$ \\ \hline
$\textcolor{red}{\mathtt{A}}\mathtt{DBC}$ & $1$ & $0$ & $0$ \\ \hline
$\textcolor{red}{\mathtt{A}}\mathtt{D}\textcolor{red}{\mathtt{C}}\mathtt{D}$ & $2$ & $1$ & $1$ \\ \hline

{\tt BACD} & $2$ & $1$ & $1$ \\ \hline
{\tt BADC} & $0$ & $-1$ & $1$ \\ \hline
{\tt BCAD} & $1$ & $0$ & $0$ \\ \hline
{\tt BCDA} & $0$ & $-1$ & $1$ \\ \hline
{\tt BDAC} & $0$ & $-1$ & $1$ \\ \hline
{\tt BDCA} & $1$ & $0$ & $0$ \\ \hline

{\tt CABD} & $1$ & $0$ & $0$ \\ \hline
{\tt CADB} & $0$ & $-1$ & $1$ \\ \hline
{\tt CBAD} & $2$ & $1$ & $1$ \\ \hline
{\tt CBDA} & $1$ & $0$ & $0$ \\ \hline
{\tt CDAB} & $0$ & $-1$ & $1$ \\ \hline
{\tt CDBA} & $0$ & $-1$ & $1$ \\ \hline

{\tt DABC} & $0$ & $-1$ & $1$ \\ \hline
{\tt DACB} & $1$ & $0$ & $0$ \\ \hline
{\tt DBAC} & $1$ & $0$ & $0$ \\ \hline
{\tt DBCA} & $2$ & $1$ & $1$ \\ \hline
{\tt DCAB} & $0$ & $-1$ & $1$ \\ \hline
{\tt DCBA} & $0$ & $-1$ & $1$ \\ \hline\hline
{\bf Mean} & $1$ & $0$ & $1$ \\ \hline
\end{tabular}
\end{center}
\end{table}
}

{\bf (A)} $E(X) = \frac{1}{24} \cdot 4 + \frac{6}{24} \cdot 2 + \frac{8}{24} \cdot 1 = 1$.
This means that the expected number of hats that stay in place is exactly $1$.\\
{\bf (B)} For all $24$ permutations, subtract the value $E(X) = 1$ from
every hat experiment outcome. Define $x_1,\ldots,x_{24}$ - all $24$ values of the
random variable $X$ (exactly one value is $4$, exactly six values are $2$,
exactly $8$ values are $1$, exactly $9$ values are $0$):
$$V(X) = \frac{\sum_{i=1}^{24} (x_i - 1)^2}{24} = \frac{24}{24} = 1.$$

Therefore, $V(X) = 1$ (variance also equals $1$, but the unit of measurement
is not hats but ``hats squared'').

{\em Note.} $E(X)=1$ is the arithmetic mean over the column $X$, but $V(X)=1$
is the arithmetic mean over the column $(X-E(X))^2$ (see Table~\ref{table:t1}).

{\scriptsize
{\em Grading (theoretically max=20, but most results are not that high).}
\begin{itemize}
\item Correctly found $E(X)$ is $5$ point.
\item Correctly found $V(X)$ is $5$ point.
\item Justified computation for $E(X)$ is $5$ points.
\item Justified computation for $V(X)$ is $5$ points.
\end{itemize}
}

\vspace{10pt}
{\bf Question 8.} \\

{\bf (A)} $P(E|H)$ is the outcome of the Binomial distribution:
There are $n=5$ coin-toss experiments; the probability of success for any single experiment
is $p = \frac{2}{3}$ (since we know that the day is lucky and hypothesis $H$ holds).
Therefore,
$$P(E|H) = {5 \choose 3} p^3 (1-p)^2 = 10 \left( \frac{2}{3} \right)^3 \left( \frac{1}{3} \right)^2 =
\frac{80}{243}$$

{\bf (B)} $P(E|H)\cdot P(H) = \frac{80}{243}\cdot\frac{1}{2}= \frac{40}{243}$,
since $P(H) = \frac{1}{2}$ (the {\em a priori} probability of a lucky day is exactly $1/2$).

{\bf (C)} $P(E|\overline{H})$ is the outcome of the Binomial distribution:
Again, there are $n=5$ coin-toss experiments, but now the probability of a single
experiment is just $p = \frac{1}{3}$. Therefore,
$$P(E|\overline{H}) = {5 \choose 3} p^3 (1-p)^2 = 10 \left( \frac{1}{3} \right)^3 \left( \frac{2}{3} \right)^2 =
\frac{40}{243}$$

{\bf (D)} $P(E|\overline{H}) \cdot P(\overline{H}) = \frac{40}{243}\cdot\frac{1}{2} = \frac{20}{243}$.

{\bf (E)} We can compute $P(E)$ as the sum of two mutually incompatible events:
event $E$ can happen either on a lucky day or on an unlucky day:
$$P(E) = P(E|H)\cdot P(H) + P(E|\overline{H}) \cdot P(\overline{H}) =
\frac{40}{243} + \frac{20}{243} = \frac{60}{243}.$$

{\bf (F)} Use Bayes formula:
\begin{align}
P(H|E) = & \frac{P(E|H) \cdot P(H)}{P(E|H) \cdot P(H) + P(E|\overline{H}) \cdot P(\overline{H})} = \nonumber \\
 = & \frac{P(E|H) \cdot P(H)}{P(E)} =  \frac{ \frac{40}{243}}{ \frac{60}{243}} = \frac{2}{3}. \nonumber
\end{align}

Bayes formula is intuitive: It shows the proportion of the
subcase $P(E|H) \cdot P(H)$ (i.e. event $E$ hapens on a lucky day) out of the
whole probability $P(E) = P(E|H)\cdot P(H) + P(E|\overline{H}) \cdot P(\overline{H})$
(i.e. event $E$ happens either on a lucky or unlucky day).

{\scriptsize
{\em Grading (theoretically max=20, but most results are not that high).}
\begin{itemize}
\item Any item from {\bf (A)} to {\bf (E)} is $3$ points.
\item Bayes formula or a similar expression finding the reverse conditional probability
in {\bf (F)} in $5$ points.
\end{itemize}
}

\vspace{10pt}
{\bf Question 9.}\\
{\bf (A)} $\mathtt{+:-\sim{}y\times6\;3\;z\;2}$,\\
{\bf (B)} $\mathtt{y\sim{}-\,6\,\times3:z\,+\,2}$,\\
{\bf (C)} $\mathtt{y\sim{}6\;3\,\times-\,z\,:2\,+}$.

{\bf Note.} In inorder traversal {\bf (B)} we first visit
the first subtree (e.g., $\mathtt{y}$), and only then the
parent node (e.g., unary minus $\sim$).
See (Rosen2019, p.811).

{\scriptsize
{\em Grading.}
\begin{itemize}
\item Each correctly written expression is
\item Item {\bf (B)} with switched order of the unary minus and its child node $y$ is
$3$ points instead of $5$.
\item Minor typos in single characters get $4$ or $5$ points.
\item Any major differences from the correct result do not get points.
\end{itemize}
}

%\vspace{10pt}
\newpage
{\bf Question 10.}\\

We start from vertex $I$. At every step we
grow the tree by a single edge (so that it stays connected
and the newly added edge has the smallest possible weight).

\begin{tabular}{|l|l|} \hline
{\bf Step} & {\bf Newly Added Edge} \\ \hline
{\bf Step 1} & $IG$, $w=1$ \\ \hline
{\bf Step 2} & $IE$, $w=2$ \\ \hline
{\bf Step 3} & $ED$, $w=1$ \\ \hline
{\bf Step 4} & $DC$, $w=2$ \\ \hline
{\bf Step 5} & $CH$, $w=2$ \\ \hline
{\bf Step 6} & $GA$, $w=3$ \\ \hline
{\bf Step 7} & $AB$, $w=2$ \\ \hline
{\bf Step 8} & $IF$, $w=5$ \\ \hline
\end{tabular}

The total weight of all added edges (same as the total weight of the MST) is $18$.

\begin{figure}[!htb]
\center{\includegraphics[width=1.5in]{final/prim-weighted-mst.png}}
\caption{\label{fig:prim-weighted-mst} MST edges shown in blue.}
\end{figure}

{\scriptsize
{\em Grading.}
\begin{itemize}
\item Incorrectly adding up weights could subtract $1$ or $2$ points from the total.
\item Adding $9$ edge weights (or any other number instead of $8$ weights) and getting incorrect sum is $11$ points (instead of $15$).
\item Not showing the edges in answers (or displaying them in an order that differs from Prim's algorithm),
but still getting something close to MST is about $8$ points.
\end{itemize}
}

\end{document}


% !TEX root = ../zeth-protocol-specification.tex

\section{Data structures and representation}\label{preliminaries:structured-data}

\newcommand{\STR}{\mathsf{STR}}

\subsection{Structured data}\label{preliminaries:data-types:formulation}

When describing the operations to be performed and the data to be manipulated as part of the protocol, we commonly employ tuples of related data where each element of the tuple has some associated semantic meaning and which must often satisfy some conditions. In this section, we develop a framework to reason about such \emph{structured} data, where a single datum may consist of one or more logical parts (called \emph{fields}). The framework is built on top of simple mathematical concepts such as sets, and mappings between them, ensuring that we can always reason about structured data in a rigorous way. We also define notation to aid the specification of structured data, and to refer to specific components of a datum. This will be used extensively in the specification of the protocol.

As a simple motivating example, consider a protocol that processes data relating to individual people. This fictional system may send and receive data such as \emph{name}, \emph{age} and \emph{address} for a single person, grouping this data into a logical unit. Further, each piece of data must satisfy specific conditions (\emph{name} must be a series of characters from some alphabet, \emph{age} must be a positive integer, etc.) We shall make use of this example several times during the formulation below.

In what follows, let $\STR = \smallset{a, b, \ldots, y, z}^{*}$ (the Kleene star of the \emph{Roman alphabet}). In our formulation, field names $f_i$ will be elements in this set.

\begin{remark}
  Note that a similar formulation could be made using an arbitrary set, such as the same alphabet augmented with specific symbols, or the alphabet of a different language. Our choice of $\STR{}$ here is for simplicity.
\end{remark}

We begin by defining a data type as a set of values called ``fields'', each with a ``name'' from $\STR$. Abstract sets are used to constrain the values of each field.

\begin{definition}[Structured Data Type]\label{preliminaries:def:datatype}
  Let $f_0, \ldots, f_{n-1}$ be $n$ distinct elements of $\STR$ and let $V_0, \ldots, V_{n-1}$ be sets, for some $n \in \NN$. We define \emph{the structured data type $\datatypestyle{T}$ with fields $\smallset{(f_i, V_i)}_{i \in [n]}$} to be a set of values:
  \[
    \datatypestyle{T} = V_0 \times \cdots \times V_{n-1}
  \]
  with associated post-fix ``dot'' operators $.f_i : \datatypestyle{T} \to V_i$ for $i = 0, \ldots, n-1$, acting on values $\mathbf{x} \in \datatypestyle{T}$ to extract the individual elements:
  \[
    \mathbf{x}.f_i = v_i \text{, where } \mathbf{x} = (v_0, \ldots, v_{n-1}) \in \datatypestyle{T}
  \]

  Here, we say that the $i$-th field has \emph{field name} $f_i$, with \emph{value set} $V_i$. Each ``dot'' operator $.f_i$ \emph{extracts} the $i$-th component, or the \emph{value with field name $f_i$}.
\end{definition}

\begin{example}\label{preliminaries:eg:datatype-person}
  Consider our example protocol that processes information about people. A potentially useful structured data type $\datatypestyle{Person}$ may be defined with fields:
  \[
    \smallset{ (\varstyle{name}, \STR), (\varstyle{age}, \NN), (\varstyle{height}, \RR^+) }
  \]
  Values $\mathbf{p}$ in $\datatypestyle{Person}$ are simply tuples in $\STR{} \times \NN \times \RR^+$, with semantic meaning (name, age, height) assigned to each component of $\mathbf{p}$.

  Examples of valid elements in $\datatypestyle{Person}$ include $\mathbf{a} = (\varstyle{alice}, 28, 1.65)$ and $\mathbf{b} = (\varstyle{bob}, 31, 1.74)$, where the following equalities hold:
  \begin{align*}
    \mathbf{a}.\varstyle{name} &= \varstyle{alice}, \\
    \mathbf{b}.\varstyle{age} &= 31, \\
    \mathbf{b}.\varstyle{height} &= 1.74;
  \end{align*}
\end{example}

For clarity, structured data types may be specified using tables of names, descriptions and value sets, rather than sets of the form $\smallset{(f_i, V_i)}_{i \in [n]}$. Similarly, it is frequently convenient to include the \emph{field names} alongside values when specifying structured data values.

\begin{example}\label{preliminaries:eg:datatype-person-table}
  $\datatypestyle{Person}$ from \cref{preliminaries:eg:datatype-person} might be described in table-form as follows:
  \begin{table}[H]
    \centering
    \begin{tabular}{cp{15em}c}
      Field                & Description        & Data type \\ \toprule
      $\varstyle{name}$   & Name of the person & $\STR{}$ \\ \midrule
      $\varstyle{age}$    & Age in years       & $\NN$ \\ \midrule
      $\varstyle{height}$ & Height in meters   & $\RR^+$ \\ \midrule
    \end{tabular}
  \end{table}
\end{example}

\begin{example}\label{preliminaries:eg:datatype-person-value-with-field-names}
  The values $\mathbf{a}$ and $\mathbf{b}$ in \cref{preliminaries:eg:datatype-person} might be written as follows:
  \begin{align*}
    \mathbf{a} &=
    \{\varstyle{name}:\: \varstyle{alice}, \; \varstyle{age}:\: 28, \; \varstyle{height}:\: 1.65\} \\
    \mathbf{b} &=
    \{\varstyle{name}:\: \varstyle{bob}, \; \varstyle{age}:\: 31, \; \varstyle{height}:\: 1.74\}
  \end{align*}
\end{example}

\begin{remark}[``dot'' operators in assignment]\label{preliminaries:rem:dot-assignment}
  The ``dot'' operators may be used in algorithm descriptions to indicate \emph{assignment to a specific component}. For example $\mathbf{a}.\varstyle{age} \gets 29$ means that the value of the $\varstyle{age}$ field of $\mathbf{a}$ is replaced by the value $29$.

  Formally, for a structured data type $\datatypestyle{T}$ with fields $\smallset{(f_i, V_i)}_{i \in [n]}$ where $\mathbf{x} = (v_0, \ldots, v_{n-1}) \in \datatypestyle{T}$ and $v_i^\prime \in V_i$:
  \[
    \mathbf{x}.f_i \gets v_i^\prime
  \]
  is equivalent to:
  \[
    \mathbf{x} \gets (v_0, \ldots, v_{i-1}, v_i^\prime, v_{i+1}, \ldots, v_{n-1})
  \]
\end{remark}

We define one further operator and related assignment notation, convenient in cases where $V_i = X^m$ for sets $X$ and $m \in \NN$.
\begin{definition}[Square bracket operator]\label{def:squareb-operator}
  For $m \in \NN$ and set $X$, define the operator $[\ ] : X^m \times [m] \to X$ as:
  \[
    \mathbf{x}[i] = x_i \text{ where } \mathbf{x} = (x_0, \ldots, x_m)
  \]
  For the set $X^*$, the operator takes the form $[\ ] : X^* \times \NN \to X$, defined as:
  \[
    \mathbf{x}[i] =
    \begin{cases}
        x_i & \text{ if } \len{\mathbf{x}} > i \text{ where } \mathbf{x} = (x_0, \ldots) \\
        \bot & \text{otherwise}
    \end{cases}
  \]
\end{definition}

\begin{remark}[Square bracket operators in assignment]
  Similarly to \cref{preliminaries:rem:dot-assignment}, we develop assignment notation for the square bracket operator $[\ ]$.
  Let $\mathbf{x} = (x_0, \ldots, x_{m-1})$ be a member of $X^m$, and $x_i^\prime$ be some element in $X$. The statement:
  \[
    \mathbf{x}[i] \gets x_i^\prime
  \]
  is equivalent to:
  \[
    \mathbf{x} \gets (x_0, \ldots, x_{i-1}, x_i^\prime, x_{x+1}, \ldots, x_{m-1})
  \]
  Informally, this can be interpreted as replacing the $i$-th component of $\mathbf{x}$ with $x_i^\prime$.
\end{remark}

\begin{remark}[Deep structures and chained ``dot'' operators]
Consider the case of structured data $\datatypestyle{T}$ with fields $\{(f_i, V_i)\}_{i \in [n]}$ for $n \in \NN$. Let $\datatypestyle{T^\prime}$ be another structured data type with fields $\{(f^\prime_i, V^\prime_i)\}_{i \in [n^\prime]}$ for $n^\prime \in \NN$, and assume that $V_j = \datatypestyle{T^\prime}$ for some $j \in [n]$. Informally, the values of the $j$-th field of elements of $\datatypestyle{T}$ are themselves structured data of type $\datatypestyle{T^\prime}$.

In this case, ``dot'' operators may be \emph{chained}, so that $\mathbf{x}.f_j.f^\prime_k$ refers to the $k$-th field $v^\prime_k$ of the $j$-th field $v_j$ of $\mathbf{x} \in \datatypestyle{T}$.
\end{remark}

\begin{example}\label{preliminaries:eg:datatype-person-deep}
  Define a structured data type $\datatypestyle{Address}$ with fields $(\varstyle{country}, \STR{}), (\varstyle{zipcode}, \STR{})$. We redefine the structured data type $\datatypestyle{Person}$ from \cref{preliminaries:eg:datatype-person}, with an extra field $\varstyle{address}$ of type $\datatypestyle{Address}$. That is, $\datatypestyle{Person}$ is the structured data type with fields:
  \begin{table}[H]
    \centering
    \begin{tabular}{cp{15em}c}
      Field                 & Description           & Data type \\ \toprule
      $\varstyle{name}$    & Name of the person    & $\STR{}$ \\ \midrule
      $\varstyle{age}$     & Age in years          & $\NN$ \\ \midrule
      $\varstyle{height}$  & Height in meters      & $\RR^+$ \\ \midrule
      $\varstyle{address}$ & Address of the person & $\datatypestyle{Address}$ \\ \midrule
    \end{tabular}
  \end{table}
  An example element $\mathbf{a}$ in $\datatypestyle{Person}$ is:
  \begin{align*}
    \mathbf{a} =
    \{ &\\
       & \varstyle{name}:\: \varstyle{alice}, \\
       & \varstyle{age}:\: 28, \\
       & \varstyle{height}:\: 1.65, \\
       & \varstyle{address}:\: (\varstyle{country}:\: \varstyle{UK}, \varstyle{zipcode}:\: \varstyle{SW1A})\\
    \}
  \end{align*}
  In this case, the following equalities using the dot and square bracket operators all hold:
  \begin{align*}
    & \mathbf{a}.\varstyle{name} = \varstyle{alice} \\
    & \mathbf{a}.\varstyle{height} = 1.65 \\
    & \mathbf{a}.\varstyle{address}.\varstyle{country} = \varstyle{UK} \\
    & \mathbf{a}.\varstyle{address}.\varstyle{zipcode} = \varstyle{SW1A} \\
    & \mathbf{a}.\varstyle{address}.\varstyle{country}[1] = \varstyle{K}
  \end{align*}
\end{example}

\subsection{Representations}\label{preliminaries:data-types:representation}

The binary alphabet $\bin$, denoted $\BB$, is used to represent the presence or absence of an electrical signal in a computer. In fact, every piece of information in a computer is represented as a string of bits.
We assume the existence of an efficient binary representation for some set of primitive datatypes (such as the natural numbers $\NN$, or alphanumeric characters). Structured data types built up from primitive types (as described above) can then recursively be assigned similarly efficient representations.
This is used to define the following functions to \emph{encode} data to its bit-string representation, and to \emph{decode} such bit-strings back to elements of the original type.

\begin{definition}
  For a set $X$ of values which are to be represented as bit strings, we define functions:
  \begin{align*}
    \encode{}{X} &: X \to \BB^* \\
    \decode{}{X} &: \BB^* \to X \cup \bot
  \end{align*}
  satisfying
  \[
    \decode{\encode{x}{X}}{X} = x\ \forall x \in X
  \]
  to be the functions which encode (resp.~decode) elements of $X$ into (resp.~from) the bit-string representations chosen above.
  We note that $\decode{}{X}$ may return $\bot$ in the case that the input bit-string is malformed.
\end{definition}

Without ambiguity, we overload the functions $\encode{}{}$ and $\decode{}{}$ to mean $\encode{}{X}$ and $\decode{}{X}$ where the set $X$ is clear from context.

In the following sections, we assume that elements of $\NN$ are encoded as big-endian binary numbers in the natural way. We denote by $\NN_{b}$ the set of natural numbers that can be uniquely encoded in this way using $b$ bits (possibly with padding). In other words,
\[
  \NN_{b} = \smallset{x \in \NN\ \suchthat\ \encode{x}{\NN} \in \BB^{b}}
\]