\documentclass[12pt]{article}
\usepackage{fullpage}
\usepackage{microtype}      % microtypography
\usepackage{array}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{amsthm}

%% Header
\usepackage{fancyhdr}
\fancyhf{}
\fancyhead[C]{COMP 136 - 2020s - HW2 Submission}
\fancyfoot[C]{\thepage} % page number
\renewcommand\headrulewidth{0pt}
\pagestyle{fancy}

%% Hyperlinks always blue, no weird boxes
\usepackage[hyphens]{url}
\usepackage[colorlinks=true,allcolors=black,pdfborder={0 0 0}]{hyperref}

%%% Doc layout
\usepackage{parskip}
\usepackage{times}

%%% Write out problem statements in blue, solutions in black
\usepackage{color}
\newcommand{\officialdirections}[1]{{\color{blue} #1}}

%%% Avoid automatic section numbers (we'll provide our own)
\setcounter{secnumdepth}{0}

\begin{document}
~~\\ %% add vert space

{\Large{\bf Student Name: TODO}}

{\Large{\bf Collaboration Statement:}}

Turning in this assignment indicates you have abided by the course Collaboration Policy:

\url{www.cs.tufts.edu/comp/136/2020s/index.html#collaboration-policy}

Total hours spent: TODO

I consulted the following resources:
\begin{itemize}
\item TODO
\item TODO
\item $\ldots$
\end{itemize}

FYI Official instructions for all problems can be found at:

\url{www.cs.tufts.edu/comp/136/2020s/hw2.html}

\tableofcontents

\newpage
\officialdirections{
\subsection*{1a: Problem Statement}

Consider the estimator $\hat{\sigma}^2$:
\begin{align}
\hat{\sigma}^2(x_1, \ldots x_N) = \frac{1}{N} \sum_{n=1}^N (x_n - \mu_{\text{true}})^2
\end{align}
Compute the expected value of this estimator under the sampling distribution of the observations $x$. You should assume each $x_n$ are drawn i.i.d from $x_n \sim \mathcal{N}( \mu_{\text{true}}, \sigma^2_{\text{true}})$.
}

\subsection{1a: Solution}

\newpage
\officialdirections{
\subsection*{1b: Problem Statement}
Given your result above, describe if the estimator is biased or unbiased.
}

\subsection{1b: Solution}

TODO

\officialdirections{
\subsection*{1c: Problem Statement}
How does your answer about bias in 1b compare to what we learned about whether the maximum likelihood estimator for the variance is biased? Provide some justification.
}

\subsection{1c: Solution}
TODO

\newpage
\officialdirections{
\subsection*{2a: Problem Statement}

Show that the SGD estimator is an *unbiased* estimator of the whole dataset gradient. That is, show:
\begin{align}
\mathbb{E}_{i \sim U}[ \hat{G}(\theta, x_i) ] = G(\theta, x)
\end{align}
}

\subsection{2a: Solution}

\newpage
\officialdirections{
\subsection*{2b: Problem Statement}
How will the $\theta$ value estimated by whole dataset gradient descent (which has no randomness) typically compare to resulting $\theta$ from SGD? Why is it important that the SGD estimator is unbiased?
}

\subsection{2b: Solution}

\newpage
\officialdirections{
\subsection*{3a: Problem Statement}
Given the log PDF below, show that $x$ has a univariate Gaussian distribution.
\begin{align}
\log p(x) = \text{const} - \frac{1}{2} a x^2 + bx
\end{align}
where $a > 0$ and $b$ is any number.
}

\subsection{3a: Solution}

\newpage
\officialdirections{
\subsection*{3b: Problem Statement}
Given the log PDF below, show that vector $x$ has a multivariate Gaussian distribution.
\begin{align}
\log p(x) = \text{const} - \frac{1}{2} x^T A x + b^T x
\end{align}
where $A$ symmetric and positive definite, and $b$ any vector.
}

\subsection{3b: Solution}

\newpage
\officialdirections{
\subsection*{4a: Problem Statement}
For the case $M=1$, show that we can write $S_{N+1}^{-1} = S_N^{-1} + v^2$ for some scalar value $v$.
}

\subsection{4a: Solution}

\newpage
\officialdirections{
\subsection*{4b: Problem Statement}
For the case $M=1$, write an expression for $\sigma^2_{N+1} - \sigma^2_{N}$, simplifying as much as possible.
}

\subsection{4b: Solution}

\newpage
\officialdirections{
\subsection*{4c: Problem Statement}
By studying the terms of your expression from **4b**, prove the following statement:

$$
\sigma_{N+1}^2(x_*) \leq \sigma_N^2(x_*)
$$
}

\subsection{4c: Solution}

\newpage
\officialdirections{
\subsection*{5a: Problem Statement}
Show that we can write $S_{N+1}^{-1} = S_N^{-1} + vv^T$ for some vector $v$.
}

\subsection{5a: Solution}

\newpage
\officialdirections{
\subsection*{5b: Problem Statement}
\begin{align}
(A + vv^T)^{-1} &= A^{-1} - \frac{ (A^{-1}v)(v^T A^{-1})}{ 1 + v^T A^{-1} v}
\end{align}

Substitute $A = S_N^{-1}$ and $v$ from 5a, then simplify to write an expression for $S_{N+1}$ in terms of $S_{N}$.
}

\subsection{5b: Solution}

\officialdirections{
\subsection*{5c: Problem Statement}
Show that $\sigma^2_{N+1}(x_*) - \sigma^2_{N}(x_*) = \phi(x_*)^T \left[ S_{N+1} - S_{N} \right] \phi(x_*)$

}

\subsection{5c: Solution}

\newpage
\officialdirections{
\subsection*{5d: Problem Statement}
Finally, plug your result from 5c into 5d, plus the fact that $S_N$ must be positive definite, to show that:
\begin{align}
\sigma_{N+1}^2(x_*) \leq \sigma_N^2(x_*), \quad \text{or equivalently} \quad \sigma_{N+1}^2(x_*) - \sigma_N^2(x_*) \leq 0
\end{align}
}

\subsection{5d: Solution}

\end{document}


\documentclass[12pt]{article}

\usepackage{eurosym}
\include{preamble}

\title{Math 241 Fall 2016 \\ Final Examination}
\author{Professor Adam Kapelner}

\date{December 15, 2016}

\begin{document}
\maketitle

\noindent Full Name \line(1,0){270} ~~~ Section (A or B)~ \line(1,0){30}

\thispagestyle{empty}

\section*{Code of Academic Integrity}

\footnotesize
Since the college is an academic community, its fundamental purpose is the pursuit of knowledge. Essential to the success of this educational mission is a commitment to the principles of academic integrity. Every member of the college community is responsible for upholding the highest standards of honesty at all times. Students, as members of the community, are also responsible for adhering to the principles and spirit of the following Code of Academic Integrity.

Activities that have the effect or intention of interfering with education, pursuit of knowledge, or fair evaluation of a student's performance are prohibited. Examples of such activities include but are not limited to the following definitions:

\paragraph{Cheating} Using or attempting to use unauthorized assistance, material, or study aids in examinations or other academic work or preventing, or attempting to prevent, another from using authorized assistance, material, or study aids. Example: using a cheat sheet in a quiz or exam, altering a graded exam and resubmitting it for a better grade, etc.
\\

\noindent I acknowledge and agree to uphold this Code of Academic Integrity. \\

\begin{center}
\line(1,0){250} ~~~ \line(1,0){100}\\
~~~~~~~~~~~~~~~~~~~~~signature~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ date
\end{center}

\normalsize

\section*{Instructions}

This exam is 120 minutes and closed-book. You are allowed three pages (front and back) of a \qu{cheat sheet.} You may use a graphing calculator of your choice but \emph{no cell phones}. Please read the questions carefully. If the question reads \qu{compute,} this means the solution will be a number otherwise you can leave the answer in choose, permutation, factorial, summation or any other notation which could be resolved to a number with a computer. I advise you to skip problems marked \qu{[Extra Credit]} until you have finished the other questions on the exam, then loop back and plug in all the holes. I also advise you to use pencil. The exam is 100 points total plus extra credit. Partial credit will be granted for incomplete answers on most of the questions. \fbox{Box} in your final answers. Good luck!

\pagebreak

\problem You are the biostatician for a drug company that is trying to develop a drug to treat skin cancer which has spread to lymph nodes (we will call this the \qu{disease}). There are no drugs currently available for the disease so the company is developing a drug and wants to run a trial in the future.

\begin{center}
\includegraphics[width=2in]{pills.png}
\end{center}

\noindent The recovery rate right now for patients with the disease is 63\%. Assume this is the truth known with certainty.

\benum
\subquestionwithpoints{2} Bob who has the disease wants to be part of the drug trial. Model Bob's recovery as of now as 1 if he recovers and 0 if he does not recover as a r.v. below. Be sure to indicate parameter value(s). \spc{1}

\subquestionwithpoints{3} 100 people just like Bob with the disease want to be part of the trial. Model the total number of people of those 100 who will recover as of now below as the r.v. $X$. Be sure to indicate parameter value(s). \spc{1}

\subquestionwithpoints{4} What two assumptions did you make about the 100 people in order to build the model in part (b)? Explain each in this context.

\begin{table}[h]
\centering
\begin{tabular}{l|l}
Assumption Name: \hspace{2.0in} & Assumption Name: \hspace{2.0in} \\ \hline
Discussion: & Discussion: \\
& \\
& \\
& \\
& \\
& \\
& \\
& \\
\end{tabular}
\end{table}

\subquestionwithpoints{2} Calculate the expected number of people of the 100 who will recover.  \spc{1}

\subquestionwithpoints{3} Compute explicitly the percentage that exactly 63 people will recover. Round to two decimal places. \spc{3}

%\subquestionwithpoints{3} Why is your answer to (f) so low? Discuss. \spc{3}

\subquestionwithpoints{5} For those 100 people just like Bob with the disease, model the \textit{proportion} of people who will recover below as the r.v. $\Phat$ and provide its \textit{approximate} distribution. Be sure to indicate parameter values.\spc{5}

\subquestionwithpoints{2} What theorem did you use to answer (h)? No need to discuss. \spc{1}

\subquestionwithpoints{2} Find $\prob{\Phat = 0.63}$ below assuming the approximate distribution (and not the distribution of the r.v. in part b). \spc{0.1}

\subquestionwithpoints{2} We will now start testing our drug. Denote the proportion of people who truly recover as the population parameter $p$. We are unsure if the drug works and want to be scientifically honest. Thus our null hypothesis will be that the proportion of people who recover when taking the drug will be \textit{at most} the same as the population overall. Write this in our notation we used in class below. \spc{1}

\subquestionwithpoints{2} Write the alternative hypothesis using the notation we learned in class. \spc{0.1}

\subquestionwithpoints{1} What is the official name of this statistical test? \spc{1}

\subquestionwithpoints{2} We will set $\alpha = 2.5\%$ since the scientific community is very conservative when it comes to new drugs. What is the probability of a Type I error?\spc{2}

\subquestionwithpoints{3} Explain what a Type I error would be in this context.\spc{1}

\subquestionwithpoints{3} Explain what a Type II error would be in this context.\spc{1}

\subquestionwithpoints{3} Discuss what you believe the cost of a type II error would be in this case. \spc{2}

\subquestionwithpoints{5} For the case where we have $n=100$ people who take the drug, find the retainment region for $H_0$ in this hypothesis test. Round to three digits. \spc{8}

\subquestionwithpoints{5} Imagine 72 people of the 100 in the drug trial recovered. Run the hypothesis test, state the conclusion and then write a sentence which explains your conclusion in the context of drug trial. \spc{4}

\subquestionwithpoints{1} Regardless of the conclusion of the hypothesis test, you \emph{feel} deep down inside that the drug is effective in increasing the recovery rate of the disease. What type of error do you \emph{feel} you made here? No explanation necessary. \spc{1}

\subquestionwithpoints{3} If you were to design another experiment to prove that the drug is effective, what would you do differently and why? Hint: increasing $\alpha$ is \textit{not} the answer. \spc{3}

\subquestionwithpoints{2} Regardless of the conclusion of the hypothesis test, we will pretend that the point estimate you computed, i.e. $\phat = 0.72$, represents the real probability of recovery under the drug. Denote $R$ as the event of recovery and $D$ as the event that the drug was taken. Thus $R^C$ denotes the person did not recover and $D^C$ denotes the person did not take the drug. What is $\cprob{R}{D}$? \spc{1}

\subquestionwithpoints{1} Find $\cprob{R^C}{D}$.\spc{2}

\subquestionwithpoints{6} Let's say the drug is on the market and you estimate 20\% of the people who have the disease take the drug. You see someone who recovers, what is the probability they took the drug? \spc{7}

\eenum

\problem You are part of a call center for a very large company.

\begin{center}
\includegraphics[width=4in]{call.png}
\end{center}

\noindent You have millions of clients but the probability each of them call in at any moment is very small. If there are 3,000,0000 clients and the probability any of them call in any second is 1 in 1,000,000 then you can say $\lambda = 3$ and model time in seconds until the next call as an \emph{exponential} distribution.

\benum
\subquestionwithpoints{2} Based on the description above, denote the time until the next call as r.v. $X$. Notate its distribution below as $X \sim$ something.\spc{5}

\subquestionwithpoints{4} Provide a rough illustration of $f(x)$. Label the axes and label one point on the y-axis that is not 0. Do not worry about scale. \spc{6}

\subquestionwithpoints{4} Provide a rough illustration of $F(x)$. Label the axes and label one point on the y-axis that is not 0. Do not worry about scale.  \spc{5}

\subquestionwithpoints{4} What is the probability we will wait more than 5 seconds for the next call that comes in? No need to compute explicitly. \spc{3}

\subquestionwithpoints{4} 4 seconds just elapsed. Given this information, what is the probability we will wait more than 5 seconds for the next call that comes in? No need to compute explicitly. \spc{3}

\eenum

\problem Some theoretical exercises are below.

\benum
\subquestionwithpoints{3} On the homework you were introduced to the r.v. $X \sim \poisson{\lambda}$. The PMF is $p(x) = \lambda^x e^{-\lambda} / x!$. If $\support{X} = \naturals \cup \braces{0}$, write an expression for $\expe{X}$ but \textit{do not solve}.\spc{2}

\subquestionwithpoints{5} On the homework you were given that if $X \sim \poisson{\lambda}$ then $M_X(t) = e^{\lambda(e^t - 1)}$. Find $\expe{X}$ by any way you believe to be best.\spc{5}

\subquestionwithpoints{3} Calculate

\beqn
\int_2^\infty \oneoversqrt{2\pi} e^{-\half\squared{x-2}}dx = \quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad
\eeqn\spc{3}

\subquestionwithpoints{2} Let $X \sim \uniform{0}{1}$. Compute $\se{X}$ to two decimal places.\spc{3}

\subquestionwithpoints{2} Assume $\prob{A} > 0$. Compute $\cprob{A}{A}$. \spc{2}

\subquestionwithpoints{5} You are riding the F train from Roosevelt Ave Jackson Heights to Forest Hills 71Av/Continental.

\begin{center}
\includegraphics[width=5in]{f_train.png}
\end{center}

\noindent That means you are traveling for 6 stops. The traveling time of each stop can be modeled as a r.v. with distribution $\normnot{1}{0.2042^2}$ minutes and we will assume independent travel time for each stop. Find the probability it takes you more than 7 minutes to get from Roosevelt Ave Jackson Heights to Forest Hills 71Av/Continental.\spc{10}

\subquestionwithpoints{5} [Extra credit] Prove or disprove: $X \sim \uniform{a}{b}$ has the memorylessness property. \spc{7}

\subquestionwithpoints{5} [Extra credit] If $X \sim \poisson{\lambda}$, find $\var{X}$. \spc{7}

\subquestionwithpoints{5} [Extra credit] Find the skewness of the standard normal.

\eenum

\end{document}

\problem Imagine a bag with 171 marbles each unique.

\begin{center}
\includegraphics[width=2in]{marbles.jpg}
\end{center}
\benum

\subquestionwithpoints{3} Imagine you select 19 balls with replacement. How many ways are there to select balls if their order matters and selection order matters? \spc{1}

\subquestionwithpoints{3} Imagine you select 19 balls with replacement. How many ways are there to select balls if their order does not matter? \spc{1}

\subquestionwithpoints{3} Imagine you select 19 balls without replacement. How many ways are there to select balls if their order matters? \spc{1}

\subquestionwithpoints{3} Imagine you select 19 balls without replacement. How many ways are there to select balls if their order does not matter? \spc{1}

%\subquestionwithpoints{2} Although they're all different, 13 of them are red and the red ones are special. If you pull out 10 marbles, how many red ones are you able to get? \spc{1}

%\subquestionwithpoints{3} Although they're all different, 29 of them are red and the red ones are special. If you pull out 150 marbles without replacement, how many red ones are you able to get? \spc{1}

%\subquestionwithpoints{2} What is the probability of getting 11 red ones on a draw of 150 without replacement? No need to compute explicitly. \spc{1}

%\subquestionwithpoints{3} 23 of them are red and the red ones are special. Imagine you have already drawn 10 red marbles on a draw of 10 without replacement. What is the chance you now get another 10 marbles in a draw of 140 without replacement? No need to compute explicitly. \spc{3}

%\subquestionwithpoints{2} Imagine you have already drawn 10 red marbles on a draw of 10 without replacement. What is the chance you now get another 10 marbles in a remaining draw of 140 \textit{with} replacement? No need to compute explicitly. \spc{1}

\eenum

\problem Some theoretical exercises are below.

\benum

\subquestionwithpoints{2} Consider the following r.v.,

\beqn
X \sim \begin{cases}
2 \withprob 1/7 \\
4 \withprob 4/7 \\
-3 \withprob 2/7 \\
\end{cases}
\eeqn

Provide a set $\Omega$ for the domain of this r.v. \spc{1}

\subquestionwithpoints{3} For the r.v. above, describe or illustrate (any way you can) of a physical device that will create the r.v. Be sure to use $\Omega$ in your illustration from (a).\spc{5}

\subquestionwithpoints{2} Could it be possible that $\abss{\support{X}} < \abss{\Omega}$? Write \qu{yes} or \qu{no} only. \spc{1}

\eenum

\problem Some more theoretical exercises are below.

\benum

\subquestionwithpoints{4} Consider $X \sim \uniformdiscrete{a_1, a_2, a_3, a_4}$ i.e. $\abss{\support{X}} = 4$. Find the MGF of $X$ denoted by $M_X(t)$. \spc{2}

\subquestionwithpoints{7} Consider a large number of $X_1, \ldots, X_n \iid \uniformdiscrete{a_1, a_2, a_3, a_4}$. Consider $X_1 + \ldots + X_n$. What is its approximate distribution? Answer in as much detail as you can provide (partial credit will be given). Use sum notation for compactness.  \spc{6}

\subquestionwithpoints{7} Consider $Y_1$ being distributed as 5 or 10 with equal probability and $Y_2 \sim \bernoulli{\half}$ where $Y_1$ and $Y_2$ are independent. Show that $Y_1 + Y_2$ is a uniform discrete r.v. with $\abss{\support{X}} = 4$ using MGF's. \spc{8}

\subquestionwithpoints{3} [E.C.] Give examples of $a_1, a_2, a_3, a_4$ above which would most likely break the approximation from part (d) for a modest size $n$. \spc{2}

\subquestionwithpoints{3} [E.C.] Find $\expe{\tothepow{\frac{X-\mu}{\sigma}}{13}}$ where $\mu$ and $\sigma$ are the mean and standard error of $X$ respectively. \spc{1}

\eenum

\problem We will investigate more about aliens and UFOs by looking at data from the \href{http://www.nuforc.org/webreports/ndxevent.html}{National UFO Reportion Center Online Database}.

\begin{figure}[htp]
\centering
\includegraphics[width=2in]{ufo.jpg}
\end{figure}

Below is the number of events in each month in 2014 and represents the number of \qu{UFO sightings} across America, a country with 319 million people. Assume that each sighting is unique --- that is each person either makes a sighting for a given or does not and there are no duplicates. That is, a person cannot make more than one sighting per 12-month period.

\begin{table}[htp]
\small
\centering
\begin{tabular}{c|c}
Month & Number \\ \hline
12/2014 &	521 \\
11/2014	&543 \\
10/2014	&786 \\
09/2014	&829 \\
08/2014	&922 \\
07/2014	&1096 \\
06/2014	&776 \\
05/2014	&652 \\
04/2014	&666 \\
03/2014	&517 \\
02/2014	&554 \\
01/2014	&715 \\
\end{tabular}
\end{table}

\benum

\subquestionwithpoints{2} We are interested in the true proportion of people who sight a UFO per year (not per month). Provide an estimate of this proportion below. Round to the nearest two digits. \spc{0.5}

\subquestionwithpoints{6} Provide a 99.7\% confidence interval for the true proportion of people worldwide who see a UFO yearly. \spc{5}

\subquestionwithpoints{3} Give two possible concerns as to why the confidence interval you produced in (b) may not be valid.

\begin{enumerate}
\item [1.] ~\\~\\
\item [2.]
\end{enumerate}

\subquestionwithpoints{4} Regardless of the issues in (c), provide four possibe interpretations of the CI from (b).

\begin{enumerate}
\item [1.] ~\\~\\~\\
\item [2.]~\\~\\~\\
\item [3.]~\\~\\~\\
\item [4.]~\\~\\~\\
\end{enumerate}

\subquestionwithpoints{3} Let's assume the point estimate of (a) is equal to the real population proportion (assume $p = \phat$ for the remainder of the problem). Assume 2015 is the same as 2014 with regards to UFO sightings (and the population of America remained the same to the nearest million people). What is the probability of less than 100,000 sightings in 2015? No need to compute exactly. Sum notation allowed.\spc{2}

\subquestionwithpoints{2} It is possible some states have more UFO sightings than other states. Hawaii had 67 sightings in 2014 for a population of 1.42 million people. Here are some examples:

\begin{table}[htp]
\centering
\footnotesize
\begin{tabular}{lllll}
Timestamp & City & Shape & Duration & Description \\ \hline
1/15/14 03:30&	Paia	HI&	Sphere	&2 min	&  I Saw a golden sphere \\
&&&&shaped object hovering over the water \\
&&&&on the North Shore at 3:30am.	 \\
1/15/14&	Kealia	HI	&Fireball	&20 min&	Fireball w- chinese laturns and parade \\
&&&& of triangles.	 \\
1/14/14 20:50&	Waimanalo	HI	&Fireball&	5 sec&	I saw an orange fireball with flames \\
&&&& trailing as it shot diagonally across \\
&&&& the sky and disappeared behind the\\
&&&& nearby mountain range.	 \\
%1/10/14 22:58&	Kaneohe	HI&	Fireball&	90 sec&	A silent red ball of light traveled \\
%&&&&off the pacific ocean from the east \\
%&&&&to the south below the clouds for \\
%&&&&about 90 seconds.	 \\
\end{tabular}
\end{table}

Find the sample proportion for seeing a UFO sighting in a year in Hawaii. Round to two digits. \spc{2}

\subquestionwithpoints{7} Test whether this proportion is different than the proportion in America overall.

\begin{enumerate}
\item [$H_0$:] ~\\
\item [$H_a$:] ~\\
\item [$\alpha =$] ~~~~~~~~ $\Rightarrow z_{\alpha/2} = $
\end{enumerate}~ \spc{2.5}

\subquestionwithpoints{2} Based on your result in (g), is the event of being in Hawaii and the event of seeing a UFO over the course of a year independent or dependent? Explain using the definition of dependence and independence. \spc{2}

\eenum

\problem We return to our discussion of Chevalier de Mere and his compulsive dice gambiling.

\begin{figure}[htp]
\centering
\includegraphics[width=1.31in]{chevalier.jpg}~\includegraphics[width=2in]{dice.jpg}
\end{figure}

\benum

\subquestionwithpoints{3} Recall the dice game he played: you win if you get at least one 6-6 in 24 rolls of two dice. What is the probability of winning? Compute to four decimal places. \spc{3}

\subquestionwithpoints{6} Chevalier de Mere most likely thought the probability of winning was 0.5 (not your answer from the previous question). Assuming Chevalier de Mere went easy on the wine and cigarettes and kept perfect records of wins and losses of everyone playing this game, how many games (denoted $n$) did he have to watch to be sure (at the 5\% level) that the probability of winning was less than 50\%. You can leave as an algebraic equation of $n$; you do not need to solve.\spc{7}

\eenum

\problem We learn about a bank alert system here for detecting fraudulent charges on a credit card.
\begin{figure}[htp]
\centering
\includegraphics[width=3in]{credit.jpg}
\end{figure}

\benum

\subquestionwithpoints{2} Assume each charge is legitimate from the start. What are the null and alternative hypotheses for this situation with credit card charges? No credit given for the general definition. Answer in English. \spc{1.5}

%\subquestionwithpoints{2} Describe a Type I error for this situation with credit card charges. No credit given for the general definition. \spc{2}

\subquestionwithpoints{3} Describe a Type II error for this situation with credit card charges. No credit given for the general definition. Answer in English. \spc{2}

\subquestionwithpoints{3} Assume the Type I error rate is $\alpha$ with cost $C_I \sim \normnot{\$100}{\$50^2}$ and the Type II error rate is $\beta$ with cost $C_{II} \sim \normnot{\$500}{\$38^2}$. What is the expected cost to the bank for a single credit card transaction? \spc{3}

%\subquestionwithpoints{3} Assuming the bank cannot change the two costs ($C_I$ and $C_{II}$). Give a strategy for the bank to minimize the expected cost in (d) without any chance of inadvertently increasing it. \spc{1}

\eenum

\problem Some questions about continuous r.v.'s

\benum

\subquestionwithpoints{2} The chi-squared distribution (denoted $\chi^2_k$) has a PDF given by

\beqn
\chi^2_k := f(x) = \frac{1}{2^{\frac{k}{2}}\Gamma\left(\frac{k}{2}\right)}\; x^{\frac{k}{2}-1} e^{-\frac{x}{2}}
\eeqn

and its support is all non-negative numbers. Note that the gamma function (denoted $\Gamma$) is evaluated to a number using a computer.  \\

What are the parameter(s) of the $\chi^2_k$ model?\spc{2}

\subquestionwithpoints{4} Write an integral expression for $\expe{X}$ where $X \sim  \chi^2_k$. \spc{4}

\subquestionwithpoints{4} Write an integral expression for $F(x)$ where $X \sim  \chi^2_k$. \spc{4}

\subquestionwithpoints{3} Consider $T \sim \exponential{\lambda}$ which represents the time spent waiting for the Q64 in minutes. What is the probability of waiting more than 10 minutes? \spc{3}

\subquestionwithpoints{4} A stopwatch beeps every minute. After each beep, which r.v. models the waiting time in minutes for the Q64? Assume of course the bus hasn't come by the beep.  \spc{2}

%\subquestionwithpoints{2} A stopwatch beeps every 1$\mu$s (that's one-millionth of a second). After each beep, which r.v. models the waiting time in minutes for the Q64? Assume of course the bus hasn't come by the beep.  \spc{2}

\eenum

\problem Some theoretical questions below

\end{document}


%!TEX root = ../thesis.tex
%*******************************************************************************
%****************************** Third Chapter **********************************
%*******************************************************************************
\chapter{Sparsification Lemma} \label{chap:sparsication}

% **************************** Define Graphics Path **************************
\ifpdf
    \graphicspath{{Chapter4/Figs/Raster/}{Chapter4/Figs/PDF/}{Chapter4/Figs/}}
\else
    \graphicspath{{Chapter4/Figs/Vector/}{Chapte4/Figs/}}
\fi

\section*{Introduction}
% State how it would be used
In this chapter we will explain the sparsification lemma that allowed
us to show lower bounds on problems in $P$ conditioned on the SETH in Chapter \ref{chap:complexity}.
To do this we will first consider an example problem which again motivates the need
for the sparsification lemma. The example is the following.
Recall the definition of $s_k$ from Equation \ref{eq:ETH}.
\begin{theorem}
    If there exists a $k \geq 3$ such that $s_k > 0$, then $s_3 > 0$ \cite{impagliazzo2001complexity}.
\end{theorem}
To show this, we would have to prove that if there are no subexponential
time algorithms for $k$-SAT for some $k > 3$ then there is no subexponential algorithm for 3-SAT.
To make things simpler, we will show the contrapositive, that if there is a subexponential time algorithm
for 3-SAT then there is a subexponential time algorithm for $k$-SAT for all $k \geq 3$.

Assume then that we have an algorithm $A$ that solves 3-SAT in subexponential time
i.e. it takes $\mathcal{O}^{\ast}(2^{\epsilon n})$ time and we want to solve an
instance of $k$-SAT where $k > 3$. We can attempt to solve this
instance via a reduction to 3-SAT.
To reduce k-SAT to 3-SAT consider a k-CNF formula $F_{k}$ and consider a specific clause
$C = (x_{1} \lor x_{2} \lor \ldots \lor x_{k})$ from $F_k$. We will then introduce new variables
$y_{1}, y_{2}, \ldots, y_{k-3}$ and define $k-2$ new clauses as follows:

\begin{align*}
C'_{1} &= (x_{1} \lor x_{2} \lor y_{1}) \\
C'_{2} &= (\overline{y_{1}} \lor x_{3} \lor y_{2}) \\
C'_{3} &= (\overline{y_{2}} \lor x_{4} \lor y_{3}) \\
&\hspace{30pt}\vdots \\
C'_{k-2} &= (\overline{y_{k-3}} \lor x_{k-1} \lor x_{k})
\end{align*}

Observe that the conjunction of the clauses $C'_{1}, C'_{2}, \dots, C'_{k-2}$
is satisfiable if and only if the original clause $C$ is satisfiable.
This procedure can then be repeated for all clauses in $F_{k}$ to convert
the $k$-SAT formula with $n$ variables and $m$ clauses to a 3-SAT formula with $n + (k-3)m$ variables
and $(k-2)m$ clauses.
If we now use algorithm $A$, then we solve the instance in time $\mathcal{O}^{\ast}(2^{\epsilon \cdot (n + (k - 3)m)})$.
This is an issue, if we consider the fact that for an arbitrary $k$-SAT formula the number of
clauses could be significantly larger than the number of variables. Consider the case where $m = n^2$.
This then means that $A$ would run in time $2^{\mathcal{O}(n^2)}$ which is certainly not subexponential.

Fundamentally, the issue is that the reduction introduced too many new variables and these new variables
came from the number of clauses in the original formula. If we could guarantee that the number of
clauses in original formula was linear in the number of variables, i.e. $m = \mathcal{O}(n)$,
then the reduction would work.

\section{Statement of the lemma}
% State the lemma
The sparsification lemma as stated by Impagliazzo, Paturi and Zane originally relates to
$k$-Set Cover, from which they derive the k-SAT version as a corollary \cite{impagliazzo2001problems}.
The original formulation is stated here for completeness. Note that although this problem is called
$k$-Set Cover by Impagliazzo et al. \cite{impagliazzo2001problems}, it is typically refered to as $k$-Hitting Set.

An instance of $k$-Set Cover has a universe of $n$ elements $x_1, x_2, \dots, x_n$ and a collection $\mathcal{S}$
of subsets $S \subseteq \{x_1, x_2, \dots, x_n\}$. However, $\forall S \in \mathcal{S} : |S| \leq k$.
A $k$-set cover is a set $C \subseteq \{x_1, x_2, \dots, x_n\}$ such that $\forall S \in \mathcal{S} : S \cap C \neq \emptyset$.
Let $\sigma(\mathcal{S})$ be the collection of all such sets that cover $\mathcal{S}$. The goal is to find a minimal size $k$-set cover.
Let $\mathcal{T}$ be a restriction on $\mathcal{S}$ if for each $S \in \mathcal{S}$ there is a $T \in \mathcal{T}$ such that $T \subseteq S$.

\begin{theorem} \label{thm:kset_sparse}
    (Sparsification Lemma for $k$-Set Cover)
    For all $\epsilon > 0$ and $k \in \mathbb{N}^{+}$ there is a constant $C$ and an algorithm that given an instance $\mathcal{S}$
    of $k$-Set Cover on a universe of size n, produced a list of $t \leq 2^{\epsilon n}$ restrictions $\mathcal{T}_1, \mathcal{T}_2, \dots, \mathcal{T}_t$
    of $\mathcal{S}$ so that $\sigma(\mathcal{S}) = \bigcup_{i = 1}^{t} \sigma(\mathcal{T}_i)$ and so that $\forall i: |\mathcal{T}_i| \leq Cn$.
    Additionally the algorithm runs in time $\mathcal{O}^{\ast}(2^{\epsilon n})$ \cite{impagliazzo2001problems}.
\end{theorem}

This essentially states that for each $k$ and $\epsilon$ there exists an algorithm that can compute a subexponential number
of new collections such that each new collection contains a number of subsets linear in $n$.
The corollary for k-SAT can be obtained by considering the universe of elements in $k$-Set Cover to be
the $2n$ literals in the formula. $\mathcal{S}$ is then the formula with each $S \in \mathcal{S}$ being a clause.
We also require an additional set for each variable, $\{x_i, \overline{x_i}\}$, thus every cover of size exactly $n$
covers exactly one literal for each variable. Each restriction $\mathcal{T}_i$ can be thought of as a subformula of $\mathcal{S}$.

\begin{corollary} \label{cor:ksat_sparse}
    (Sparsification Lemma for $k$-SAT)
    For all $k \in \mathbb{N}^{+}$ and $\epsilon > 0$ there exists an algorithm that takes in a $k$-CNF formula $F$
    with $n$ variables and $m$ clauses and returns a collection of $k$-CNF formulas
    $F'_1, F'_2, \dots, F'_t$, with the following properties.
    \begin{enumerate}
        \item $F$ is satisfiable if and only if $\bigvee_{i=1}^{t} F'_i$ is satisfiable.
        \item $F'_i$ has no more variables than $F$
        \item Number of clauses in $F'_i$ is less than $c(k, \epsilon)n$, where $c(k, \epsilon)$
        is a constant that does not depend on $n$.
        \item The algorithm takes time $\mathcal{O}^{\ast}(2^{\epsilon n})$ and $t \leq 2^{\epsilon n}$
    \end{enumerate}
\end{corollary}

To see how this could be used, consider the issue from earlier.
Recall that we had an instance of $k$-SAT $F$ with $m = n^2$.
After a reduction to 3-SAT this instance could be solved in time $2^{\mathcal{O}(n^2)}$.
Consider first applying the lemma $F'_1, F'_2, \dots, F'_t = \texttt{Sparsify}(F, \epsilon_2)$ for some $\epsilon_2 > 0$.
Apply the reduction to each $F'$ and solve them with the subexponential algorithm for 3-SAT.
Since, by the lemma, the number of clauses in each $F'_i$ is linear in $n$ we get that the time to solve a single
instance after the reduction to 3-SAT is $\mathcal{O}^{\ast}(2^{\epsilon_1 \cdot (n + (k-3)c(k, \epsilon_2)n )})$,
where $\epsilon_1$ is the $\epsilon$ that we choose for our subexponential time algorithm for 3-SAT.
Choosing $\epsilon_1 > 0$ small enough we can eliminate the other constants to show that
a single $F'_i$ can be solved in time $\mathcal{O}^{\ast}(2^{\epsilon_3 n})$
for any $\epsilon_3 > 0$.
Sparsification can be done in time $\mathcal{O}^{\ast}(2^{\epsilon_2 n})$, the reduction to 3-SAT takes polynomial time.
Hence the total time is
\begin{equation} \label{eq:run_time}
    \mathcal{O}^{\ast}(2^{\epsilon_2 n}) + 2^{\epsilon_2 n} \cdot poly(n) \cdot \mathcal{O}^{\ast}(2^{\epsilon_3 n})
\end{equation}
By choosing constants $\epsilon_2$ and $\epsilon_3$ this can be simplified
to $\mathcal{O}^{\ast}(2^{\epsilon n})$ for any $\epsilon > 0$. So we can solve $k$-SAT in subexponential time
which is what we wanted to show.
Hence, what remains is to detail the sparsification algorithm and to prove the sparsification lemma.

% clean up notation wrt polynomial factors

\section{The Sparsification Algorithm}
\subsection{Flowers}
% Define some notation (formula as a set, clauses as a set, vars and lits as a function)
Before moving forward, it makes sense to define some new notation which will
be helpful later. This notation borrows from the $k$-Set Cover formulation of the
Sparsification Lemma. We will consider a Formula $F$ with $n$ variables and $m$ clauses
as a set of clauses $\{C_1, C_2, \dots, C_m\}$
with each $C$ being a set of literals $\{l_1, l_2, \dots, l_s\}$
with a literal being either a variable or its negation and $|C| \leq k$.
Let $$vars(F) = \{x \hspace{3pt} : x \in C \lor \neg x \in C, C \in F\}$$
and let $lits(F) = \bigcup_{C \in F}C$. It is then easy
to see that $|F| = m$ and $|vars(F)| = n$.

We can now being to describe the workings of the algorithm. Let the constants
$k$ and $\epsilon > 0$ be given. Define a sequence of $\theta_1, \theta_2, \dots, \theta_k$.
Their values will be decided at a later point
and will depend \textit{only} on k and our choice of $\epsilon$
but for now consider $\theta_i$ to be much larger than $\theta_{i-1}$.

% Descriptions of flowers (and maybe a nice diagram)
% What makes a good flower
\begin{definition}
    (Flower) A flower is a set of clauses $G = \{C_1, C_2, \dots, C_t\}$ with the following properties:
    \begin{enumerate}
        \item $\exists l \in \mathbb{N}: \forall C \in G: |C| = l$. All clauses have the same size $l$.
        \item $H = \bigcap_{C \in G} C \neq \emptyset$. The intersection of the clauses is non-empty.
    \end{enumerate}
    Denote $H = \bigcap_{C \in G} C$ as the heart and $P_i = C_i \setminus H$ as the petals. A flower
    is considered good if it has many short petals. More formally a flower is good if and only if
    \begin{equation} \label{eq:good_flower}
        |G| \geq \theta_{l - |H|}
    \end{equation}
\end{definition}

Of good flowers $G_1$ and $G_2$, we say that $G_1$ is better than $G_2$ if it has shorter clauses
$l_1 < l_2$ or if $l_1 = l_2$ but $|H_1| > |H_2|$.
It is worth noting that all of these intersections are over literals, so $\{x\} \cap \{\neg x\} = \emptyset$.

\begin{example}
    We can consider a simple formula
    \begin{align*}
    F = &\{C_1, C_2, C_3, C_4, C_5\} \\
    F = \{ &\{w, x, y, z\}, \{w, \neg x, y, \neg z\}, \{\neg w, \neg x, \neg y, \neg z\} \\
    &\{\neg w, x, y, z\}, \{\neg w, x, y, \neg z\} \}
    \end{align*}
    We can form a flower with $G = \{C_1, C_2, C_4, C_5\}$, such that the heart $H = \{y\}$
    and the petals $P = \{\{w, x, z\}, \{w \neg x, \neg z\}, \{\neg w, x, z\}, \{\neg w, x, \neg z\}\}$.
    Note that in this case $l = 4$ and for the flower to be good we would need to satisfy equation
    \ref{eq:good_flower}, so we must have $|G| \geq \theta_{4 - |H|}$ or $5 \geq \theta_{3}$.
    This flower can be seen in Figure \ref{eq:good_flower}.
\end{example}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \draw[] (-1,0) ellipse (2 and 1) node[anchor=east]{$w, x, z$}
                (0,1) ellipse (1 and 2) node[anchor=south]{$w, \neg x, \neg z$}
                (1,0) ellipse (2 and 1) node[anchor=west]{$\neg w, x, z$}
                (0,-1) ellipse (1 and 2) node[anchor=north]{$\neg w, x, \neg z$};
        \node (0,0) {$y$};
    \end{tikzpicture}
    \caption{Visualisation of a flower with 4 petals and $H = \{y\}$.}
    \label{fig:flower}
\end{figure}

\subsection{Algorithm}
% Describe the recursive algorithm
The Sparsification Algorithm at each step finds the best flower and then adds
the petals to one copy of the formula and the heart to the other copy of the formula,
removing redundant clauses. The algorithm then applies itself recursively to the two
new formulas. If there are no good flowers, then we are done. This algorithm can
be seen in Algorithm \ref{alg:sparse}.

\begin{algorithm}
\caption{Sparsification Algorithm (\texttt{sparsify})}
\label{alg:sparse}
\vspace{5pt}
\KwIn{$k$-CNF Formula $F$, $k \in \mathbb{N}$, $\epsilon > 0$}
\KwOut{$F'_1, F'_2, \dots, F'_t$}
\hrulefill\\

\nl $G = \{C_1, C_2, \dots C_s\} \gets \texttt{best\_flower}(F, \epsilon, k)$ \\
\nl \If{$F$ contains a good flower}{
    \nl $H \gets \bigcap_{C \in G}C$ \\
    \nl $P \gets \{C \setminus H : C \in G\}$ \\
    \nl $F_{\textit{heart}} \gets \texttt{reduce}(F \cup \{H\})$ \\
    \nl $F_{\textit{petals}} \gets \texttt{reduce}(F \cup P)$ \\
    \nl \Return{ }$\texttt{sparsify}(F_{\textit{heart}}, k, \epsilon) \cup$ \\
    $\hspace{30pt} \texttt{sparsify}(F_{\textit{petals}}, k, \epsilon)$ \\
}
\nl \Else{
    \nl \Return{$F$}
}
\end{algorithm}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \node[draw, circle] (A) at (0,0)     {$F$};
        \node[draw, circle] (B) at (-2,-1)   {$F_h$};
        \node[draw, circle] (C) at (2,-1)    {$F_p$};
        \node[draw, circle] (D) at (-3,-2.5) {$F_{hh}$};
        \node[draw, circle] (E) at (-1,-2.5) {$F_{hp}$};
        \node[draw, circle] (F) at (1,-2.5)  {$F_{ph}$};
        \node[draw, circle] (G) at (3,-2.5)  {$F_{pp}$};

        \node (H) at (-3, -3.5) {\vdots};
        \node (H) at (-1, -3.5) {\vdots};
        \node (H) at (1, -3.5) {\vdots};
        \node (H) at (3, -3.5) {\vdots};

        \path[->]   (A) edge (B);
        \path[->]   (A) edge (C);
        \path[->]   (B) edge (D);
        \path[->]   (B) edge (E);
        \path[->]   (C) edge (F);
        \path[->]   (C) edge (G);
    \end{tikzpicture}
    \caption{Visualisation of the first two recursive levels of Algorithm \ref{alg:sparse}}
    \label{fig:tree}
\end{figure}

A clause $D \in F$ is considered redundant if there is a clause $C \in F$ such that $C \subseteq D$.
Clause $D$ can be removed from $F$ without changing the satisfiability of $F$. This is because
satisfying $C$ automatically satisfies $D$, hence $D$ has no effect on the satisfiabilty of $F$.
The algorithm for removal of redundant clauses can be seen in Algorithm \ref{alg:reduce}.

\begin{algorithm}
\caption{Formula Reduction (\texttt{reduce})}
\label{alg:reduce}
\vspace{5pt}
\KwIn{$F$ a formula}
\KwOut{$F'$ a formula equivalent to $F$ with redundant clauses removed}
\hrulefill\\

\nl \For{$C, D \in F^2$}{
    \nl \If{$C \subseteq D$}{
        \nl $F \gets F \setminus D$ \\
    }
}
\nl \Return{$F$}
\end{algorithm}

To find the best flower we can take advantage of the fact that the largest possible heart will
at most be of size $k$. So we don't need to consider every subset of the clauses in $F$, but
just all all possible hearts which is polynomial in $n$. This algorithm can be seen in Algorithm \ref{alg:best_flower}, and
it runs in time $\mathcal{O}(k^3(2n)^{k}m\log k)$. Since $k$ is a constant then this is $\mathcal{O}(n^{k}m)$, which even when
$m = poly(n)$ is still polynomial in $n$. Correctness of Algorithm \ref{alg:best_flower} follows from the application of the
definition of a good flower from Equation \ref{eq:good_flower}, and from the fact that flowers are considered in
order from best to worst, so the best flower will always be returned if one exists.

\begin{algorithm}
\caption{Finding the best flower (\texttt{best\_flower})}
\label{alg:best_flower}
\vspace{5pt}
\KwIn{$k$-CNF formula $F$, $k \in \mathbb{N}, \epsilon > 0$}
\KwOut{$G = \{C_1, C_2, \dots, C_s\}$, the best flower good flower in $F$ if any}
\hrulefill\\

\nl \For{$l \in \{1, 2, \dots, k\}$}{
    \nl \For{\text{heart\_size} $\in \{k, k-1, \dots, 1\}$}{
        \nl $F^{\ast} \gets \{C \in F: |C| = l\}$ \\
        \nl \For{$H \in \{H \subseteq F^* : |H| = \text{heart\_size}\}$}{
            \nl $G \gets \emptyset$ \\
            \nl \For{$C \in F^{\ast}$}{
                \nl \If{$H \subseteq C$}{
                    \nl $G \gets G \cup C$ \\
                }
            }
            \nl \If{$|G| \geq \theta_{l - \text{heart\_size}}$}{
                \nl \Return{$G$} \\
            }
        }
    }
}
\nl \Return{\text{$F$ contains no good flower}} \\
\end{algorithm}

\section{Execution of Algorithms}
To better understand Algorithm \ref{alg:sparse} it can be helpful to go through a simple example.
We will start with a small CNF formula that we want to sparsify. We let $k = 3$, $n=3$, we let $\epsilon$ be sufficiently large
so that the sequence of $\theta$s become small enough for any flower with at last 2 petals to be good.
The example CNF formula is given as follows.
\begin{align*}
    F = &(\neg x_{1} \lor \neg x_{3} \lor x_{2}) \land (\neg x_{1} \lor \neg x_{2} \lor x_{2}) \land \\
    &(\neg x_{1} \lor x_{3} \lor x_{1}) \land (\neg x_{1} \lor \neg x_{2} \lor x_{1}) \land\\
    &(\neg x_{3} \lor x_{2} \lor x_{1})\\
\end{align*}
Notice that the literals $\neg x_1$ and $\neg x_2$ appears in 2 clauses in $F$, this becomes the heart of the first flower.
The two subformulas obtained after applying Algorithm \ref{alg:reduce} are given as follows where $h$ denotes the
subformula resulting from adding the heart of the first flower and $p$ denotes the subformula resulting from adding
the petals.
\begin{align*}
    h : &(\neg x_{1} \lor \neg x_{3} \lor x_{2}) \land (\neg x_{3} \lor x_{2} \lor x_{1}) \land\\
    &(\neg x_{1} \lor \neg x_{2}) \land (\neg x_{1} \lor x_{3} \lor x_{1})\\
    p : &(x_{2}) \land (x_{1})\\
\end{align*}
The subformula resulting from adding the heart still has a good flower with $(\neg x_3 \lor x_2)$ as the heart,
so the algorithm does not terminate.
\begin{align*}
    hh : &(\neg x_{1} \lor \neg x_{2}) \land (\neg x_{1} \lor x_{3} \lor x_{1})\land (\neg x_{3} \lor x_{2})\\
    hp : &(x_{1}) \land (\neg x_{1})\\
\end{align*}
No more flowers can be formed at this point, so the algorithm terminates. Note that not every subformula is satisfiable,
namely $hp$, but as long as $F$ is satisfiable the disjunction of all the subformulas is satisfiable.
In this case the subformula formed at the first petal branch $p$ is satisfiable and
yields a satisfying assignment to $F$.

\section{Proof}
\subsection{Satisfiability of \texorpdfstring{$F$}{Formula}}
To prove point 1 from Corollary \ref{cor:ksat_sparse}
we shall aim to show that $F$ is satisfiable if and only if $\bigvee_{i}^{t}F'_i$ is satisfiable.
\begin{proof}
    Consider a non-leaf node in the tree of recursive calls, pictured in Figure \ref{fig:tree}, let
    the formula at this point be $F^{\ast}$. since this is an non-leaf node, then it has a good
    flower $G$ and two children $F_h^{\ast}$ and $F_p^{\ast}$ for the heart branch and petal branch respectively.
    If either $F_h^{\ast}$ or $F_p^{\ast}$ is satisfiable then either the heart or all of the petals of $G$
    are satisfiable. This means that all clauses in $G$ are satisfiable and any clauses that were made
    redundant in $F_h^{\ast}$ or $F_p^{\ast}$ will also be satisfiable in $F^{\ast}$. All other clauses
    remain the same between $F_h^{\ast}$ or $F_p^{\ast}$ and $F^{\ast}$. Hence these clauses must also be
    satisfiable. Thus we have that $F^{\ast}$ is satisfiable if $F_h^{\ast}$ or $F_p^{\ast}$ is satisfiable.
    Since our choice of $F^{\ast}$ was arbitrary, this holds of all non-leaf nodes. Therefore, via induction, we have that if
    $\bigvee_{i}^{t}F'_i$ is satisfiable then $F$ is satisfiable.

    To see the converse, let $F$ be satisfiable and let $\alpha$ be the satisfying assignment.
    If $F$ is not also a leaf node then it has a good flower $G$.
    Notice that for a flower $G \subseteq F$, $\alpha$ must either satisfy the heart of the flower
    or all of the petals. So either $F_h$ or $F_p$ must be satisfied. By repeated application of this argument on $F_h$ and $F_p$
    we find that at least one of the leaves must be satisfiable. Which is what we require.
\end{proof}

The proof of point 2 that the algorithm does not add any new variables is obvious from looking at
Algorithm \ref{alg:sparse}.

\subsection{Formulas are linear in \texorpdfstring{$n$}{n} when the algorithm terminates}

\begin{definition}
    Given a CNF formula $F$, a literal $u$ and an integer $l \in \mathbb{N}$, let
    \begin{align}
        d_l(u, F) &= |\{C \in F : |C| = l \land u \in C\}| \label{eq:largest_flower_u} \\
        d_l(F) &= \max_u \{d_l(u, F)\} \label{eq:largest_flower}
    \end{align}
\end{definition}

We can interpret the first function as the size of the largest flower with clauses of size $l$
and with $u$ in the heart. Note that the flowers do not need to be good.
The second function can therefore be interpreted as the largest
flower with clauses of size $l$. This leads to the first observation.
% no good flower --> d_l(f) <= theta_l-1 - 1

\begin{lemma} \label{lem:no_good}
    Let $F$ be a ($\leq k$)-CNF formula that contains no good flowers with clauses of size $l$,
    then $d_l(F) \leq \theta_{l-1} - 1$.
\end{lemma}
\begin{proof}
    Suppose that $d_l(F) \geq \theta_{l-1}$. From the definition of $d_l(F)$
    in Equation \ref{eq:largest_flower}, this means that
    \begin{equation} \label{eq:many_petals}
        \exists u : |\{C \in F : |C| = l \land u \in C\}| \geq \theta_{l-1}
    \end{equation}
    Consider the flower $G$ of clauses with length $l$ with $u \in H$.
    We know that from Equation \ref{eq:many_petals} this flower has $\theta_{l-1}$ petals,
    however, since the heart is at least of size 1 then the length of the petals is $\leq l-1$.
    Therefore, from equation \ref{eq:good_flower} the flower $G$ is good.
    Thus
    \begin{equation} \label{eq:impl_no_flower}
        d_l(F) \geq \theta_{l-1} \implies F \text{ has a good flower of size $l$}
    \end{equation}
    Therefore, if we take the contrapositive of (\ref{eq:impl_no_flower}) we obtain what we want to show.
    \begin{equation}
        F \text{ does not have a good flower of size $l$}
        \implies d_l(F) \leq \theta_{l-1} - 1
    \end{equation}
\end{proof}
% corollary that |F| is O(n)
\begin{corollary} \label{cor:no_good}
    Let $F$ be a ($\leq k$)-CNF formula without any good flowers.
    Then $|F| \leq 2k\theta_{k-1} \cdot |vars(F)|$.
\end{corollary}
\begin{proof}
    We can apply lemma \ref{lem:no_good} for all clause lengths.
    Thus,
    \begin{align}
        \forall l \in \{1,2,\dots,k\}:& \quad d_l(F) \leq \theta_{l - 1} - 1 \\
        \forall l \in \{1,2,\dots,k\}:& \quad \forall u \in lits(F): d_l(u, F) \leq \theta_{l-1}-1
    \end{align}
    Since there are at most $2 \cdot |vars(F)|$ literals in $F$, summing over $u$ yields
    \begin{equation}
        \forall l \in \{1,2,\dots,k\}: \sum_{u \in lits(F)} d_l(u, F) \leq 2\theta_{l-1}\cdot |vars(F)| \\
    \end{equation}
    Additionally, since every clause contains at least one literal then
    $|\{C \in F : |C| = l\}| \leq \sum_{u \in lits(F)} d_l(u, F)$ since if $u \in C$ then $C$ contributes to $d_l(u, F)$.
    Therefore
    \begin{equation} \label{eq:clauses_size_l}
        \forall l \in \{1,2,\dots,k\}: |\{C \in F : |C| = l\}| \leq 2(\theta_{l-1} - 1) \cdot |vars(F)| \\
    \end{equation}
    Then we can sum up across all $l$ to get
    \begin{equation}
        |F| \leq 2 \sum_{l=1}^{k}(\theta_{l-1} - 1) \cdot |vars(F)|
    \end{equation}
    Recall that $\theta_i > \theta_{i-1}$, so we can upper bound all $\theta_{l-1}$ by $\theta_{k-1}$.
    \begin{align}
        |F| \leq& 2 \sum_{l=1}^{k}(\theta_{k-1} - 1) \cdot |vars(F)| \\
        |F| \leq& 2k\theta_{k-1} \cdot |vars(F)|
    \end{align}
\end{proof}

Observe that $2k\theta_{k-1}$ is a constant that is independent of $n$, hence we satisfy point 3)
from Corollary \ref{cor:ksat_sparse} of the Sparsification Lemma by setting $c(k,\epsilon) = 2k\theta_{k-1}$.

\subsection{There are subexponentially many leaves}

The final point is to show that $t \leq 2^{\epsilon n}$. This is
the most involved section of the proof. We will show two things, which
will uppert bound the number of leaves in the recursion tree. Firstly, we will
claim that the longest path from the root to any leaf is at most $\alpha \cdot n$,
for some constant $\alpha \in \mathbb{N}$
Secondly, we will show that the number of branches that correspond to adding
petals to the formula is low. Specifically, we will show that the number of
petal branches is at most $\frac{kn}{\delta}$. The constants $\alpha$ and $\delta$ will depend only
on the choices of of $k$ and $\epsilon$.

To get a sense for the number of leaves in the recursion tree, consider that
each path from the root to a leaf can be written as a sequence of p's and h's,
with p's corresponding to when the recursion tree branched to add petals and h's
corresponding to the when the recursion tree branched to add the heart of the flower.
The start of this recursion tree can be seen in Figure \ref{fig:tree}. The four
nodes at the bottom show the start of this sequence of p's and h's.
To then count the number of leaves in the recursion tree, we need to just count
the number of sequences. This can be done by summing the number of permutations
of p's and h's for each number of petal branches. The number of
paths from the root to a leaf with exactly $P$ petal branches would be given
by $\binom{\alpha \cdot n}{P}$. So therefore, if we know that there are
at most $\frac{kn}{\delta}$ petal branches then the total number of leaves is given by
\begin{equation} \label{eq:num_leaves_idea}
    \sum_{i}^{kn / \delta} \binom{\alpha \cdot n}{i} \leq 2^{H(\frac{k}{\alpha \cdot \delta}) \alpha n}
\end{equation}
where $H(x) = -x \log(x) - (1-x)\log(1-x)$ is the binary entropy function. This bound
can be obtained from Stirling's approximation of $n!$ (for more details see \cite{flum2006parameterized}).
We will show that $H(k / \alpha \delta) \alpha \leq \epsilon$ which would complete the proof.
% after added clauses of size j, level j remains sparse

\begin{lemma} \label{lem:rem_sparse}
    Let $F$ be the current formula at a non-leaf node occurring in the recursion tree
    as exemplified in Figure \ref{fig:tree}. Let $G = \{C_1, C_2, \dots, C_s\}$ be the flower
    identified as the best flower at this node. Denote $F'$ as a child of $F$ in the recursion
    tree. If $|C_i| = l$, then $F'$ would be formed by adding clauses of size $1 \leq j < l$ to $F$.
    We claim that
    \begin{equation}
        d_j(F') \leq 2 \cdot \theta_{j-1}
    \end{equation}
\end{lemma}
\begin{proof}
    There are two cases that need to be considered. The first is when
    $F' = \texttt{reduce}(F \cup \bigcap_{C \in G} C)$, i.e. by adding the heart of
    the flower to $F$ and removing redundant clauses. The second is when
    $F' = \texttt{reduce}(F \cup \{C \setminus H : C \in G\})$ where $H$ is the heart of the flower,
    i.e. adding all the petals to $F$ and removing redundant clauses.
    \subsubsection{Case: Adding the heart to $F$}
    Recall that a flower is considered better than another if has shorter clauses. Therefore, since
    the best flower is picked at every step, there cannot have been a good flower in $F$ with
    clauses all of size $j$, since $j < l$. Then from Lemma \ref{lem:no_good}, we can say that
    \begin{equation}
        d_j(F) \leq \theta_{j-1} - 1
    \end{equation}
    Then, since only one clauses is added to $F'$, namely the heart and
    this is a clause of size $j$ so then we can conclude that
    \begin{equation}
        d_j(F') \leq \theta_{j-1} \leq 2 \cdot \theta_{j - 1}
    \end{equation}
    which is what we require.
    \subsubsection{Case: Adding petals to $F$}
    For the sake of contradiction, we will assume that $d_j(F') \geq 2 \cdot \theta_{j-1} + 1$.
    Recall, from the definition of $d_j(F)$ in Equation \ref{eq:largest_flower}, this means that there exists some literal
    $u$ that appears in $\geq 2 \cdot \theta_{j-1} + 1$ clauses of size $j$ in $F'$. Since
    we know that there was no good flower with clauses of size $j$ in $F$ then we know
    from Lemma \ref{lem:no_good}, that $d_j(u, F) \leq \theta_{j-1} - 1$. So then, since
    there are $\geq 2 \cdot \theta_{j-1} + 1$ clauses of size $j$ containing a literal $u$ in $F'$
    but only $\leq \theta_{j-1} - 1$ in $F$, then these clauses must have been added to $F'$
    from the petals of the flower, i.e. $d_j(u, \{C_1 \setminus H, C_2 \setminus H, \dots, C_t \setminus H\}) \geq \theta_{j-1}$ where $H$ is the heart of the flower. However, we can form a better flower in $F$ with $\{u\} \cup H$
    as the heart. This is a good flower because the new petals have
    size $j-1$ and we know that there are more than $\theta_{j-1}$ of them,
    so it satisfies Equation \ref{eq:good_flower} and
    is a good flower. This is a contradiction since the heart is larger and hence it is a better flower than
    the one picket in $F$, but Algorithm \ref{alg:best_flower} always picks the best flower.
    We then have that our assumption must be false and
    \begin{equation}
        d_j(F') \leq 2 \cdot \theta_{j - 1}
    \end{equation}
    as required. This concludes the proof.
\end{proof}
The interpretation of this proof is that once a clause of size $j$ is added to a formula
in the recursion tree, then the number of clauses of size $j$ will be linear in $n$. This is because
adding clauses of a different size cannot increase the number of clauses of size $j$, and
adding clauses of size $j$ invokes the lemma to show that this level is still linear in $n$.

We introduce the notion of \textit{Native} and \textit{Immigrant} clauses, meaning clauses
that were in the original formula $F$ at the root of the recursion tree
and clauses that were added as hearts and petals respectively.
Note that only \textit{Immigrants} can cause a clause to be removed from a formula in a call to
\texttt{reduce}.

We will now bound the number of \textit{Immigrant} clauses that can be removed by a single clause
in a call to \texttt{reduce}.
\begin{lemma} \label{lem:max_remove}
    For a formula $F$, in a call to \texttt{reduce($F$)}, a single clause $C$ can remove at most
    $2 \cdot \theta_{j - 1}$ \textit{Immigrant} clauses of size $j$ in $F$
\end{lemma}
\begin{proof}
    If no \textit{Immigrant} clauses of size $j$ exist in $F$ then none can be removed and we are done.
    Otherwise, since a clause of size $j$ has at some point been added to $F$
    we can apply Lemma \ref{lem:rem_sparse} to say that $d_j(u, F) \leq 2 \cdot \theta_{j - 1}$,
    where $u$ is a literal contained in the clause $C$. It then follows that $C$ can be a
    subset of at most $2 \cdot \theta_{j-1}$ clauses of size $j$. Since these are the clauses that
    are removed by $C$, then we are done.
\end{proof}

We will now attempt to bound the number of \textit{Immigrant} clauses added to the formula
along a path from the root to the leaf. To do this, we define a sequence of numbers
$\gamma_1 = 2$ and $\gamma_l = 4 \theta_{l - 1}\gamma_{l-1}$.
\begin{lemma} \label{lem:bound_immigrants}
    For a path from the root to a leaf in the recursion tree and for $1 \leq l \leq k - 1$,
    let $I_{\leq l}$ denote the number of \textit{Immigrant} clauses of size $\leq l$ that are added along the
    path. Then
    \begin{equation}
        I_{\leq l} \leq \gamma_l \cdot n
    \end{equation}
\end{lemma}
% \begin{proof}
%     Let $F$ denote the formula at the root of the recursion tree, let $F'$ denote the formula
%     at the leaf and let $R_l$ be the number of \textit{Immigrant} clauses of size $l$ that are removed
%     along the path from $F$ to $F'$.
%
%     Notice that an \textit{Immigrant} clause of size $l$ is either removed by some other \textit{Immigrant}
%     clause of size $\leq l - 1$ or is still in $F'$ at the end. It therefore follows that
%     \begin{equation*}
%         I_l \leq R_l + \textit{\#number of clauses of size $l$ in $F'$}
%     \end{equation*}
%     From Equation \ref{eq:clauses_size_l} in Corollary \ref{cor:no_good} we know that \#number of clauses of size $l$ in $F'$ is at most
%     $2 \cdot \theta_{l-1} n$. We will consider induction on $l$. Suppose that a clause $D$ of size $l$
%     was removed in the \texttt{reduce} step, this means that an \textit{Immigrant} clause $C$ with
%     $C \subseteq D$ was added to the formula. By Lemma \ref{lem:max_remove}, we know that this
%     clause could have removed at most $2 \cdot \theta_{l-1}$ of size $l$. By the inductive hypothesis,
%     the algorithm adds at most $\gamma_{l-1} n$ clauses of size $\leq l-1$
%     along the path from the root to the leaf. In total these clauses can then remove at most
%     $2 \theta_{l-1}\gamma_{l-1}n$ \textit{Immigrant} clauses of size $l$.
%     \begin{equation}
%         R_l \leq 2 \theta_{l-1}\gamma_{l-1}n
%     \end{equation} % Question why the hell we can go from removing clauses of size l -1 to removing all clauses of size l
%     % it's simply a bound and \theta_{l-1} is greater than all the smaller thetas, so the bound holds
%     Combining bounds for $R_l$ and for the \# number of clauses of size $l$ in $F'$ we get
%     \begin{equation}
%         I_l \leq 2 \theta_{l-1}\gamma_{l-1}n + 2 \theta_{l-1}n \leq 4 \theta_{l-1}\gamma_{l-1}n = \gamma_{l}n
%     \end{equation}
%     The last step follows from our definition of $\gamma_l$. What remains is to show the inductive base,
%     this follows from the fact that a clause is never added twice, so therefore $I_1$ could at most be
%     the number of variables in $F'$ and since there are no more variables in $F'$ than in $F$ we can
%     conclude that $I_1 \leq n = \gamma_1 n$. This concludes the proof.
% \end{proof}
\begin{proof}
    We will show this by induction on $l$.
    Let $F$ denote the formula at the root of the recursion tree and let $F'$ denote the formula
    at a leaf. Let $R_l$ be the number of \textit{Immigrant} clauses of size exactly $l$ that are
    removed along the path from $F$ to $F'$

    The base case where $l = 1$ follows from the fact that there are only $2n$ clauses of size 1,
    so $I_{\leq l} \leq 2n = \gamma_1 n$. We assume that our inductive hypothesis holds for $l - 1$.
    \begin{equation} \label{eq:inductive_hyp}
        I_{\leq l - 1} \leq \gamma_{l-1} n
    \end{equation}
    We now show that this implies that the lemma holds for $l$.

    Denote $I_l$ as the number of \textit{Immigrant} clauses of size exactly $l$ that are added along
    the path from $F$ to $F'$. Each such added clause will either be removed at some point in the path
    or remain in $F'$. Hence we can bound $I_l$ from above.
    \begin{equation} \label{eq:bound_immigrant_exact}
        I_l \leq R_l + |\{C \in F' : |C| = l\}|
    \end{equation}
    From Equation \ref{eq:clauses_size_l} in Corollary \ref{cor:no_good} we know that
    $|\{C \in F' : |C| = l\}| \leq 2 \theta_{l-1}n$.

    We will now bound $R_l$. If $R_l \neq 0$ then there is some clause $D$ of size $l$ that was removed
    by some \textit{Immigrant} clause $C \subset D$ where $|C| \leq l -1$.
    By Lemma \ref{lem:max_remove} we know that this clause
    could have removed at most $2 \theta_{l-1}$ clauses of size $l$. From the inductive hypothesis
    we know that there are at most $\gamma_{l-1} n$ such clauses. Hence we obtain the following bound on $R_l$.
    \begin{equation} \label{eq:bound_removals}
        R_l \leq 2\theta_{l-1}\gamma_{l-1}n
    \end{equation}
    Additionally, we observe that
    \begin{equation} \label{eq:ineq_eq}
        I_{\leq l} = I_{\leq l - 1} + I_{l}
    \end{equation}

    Combining these inequalities we obtain the following:
    \begin{align*}
        I_{\leq l} &= I_{\leq l - 1} + I_{l} \\
        &\leq \gamma_{l-1}n + R_l + |\{C \in F' : |C| = l\}| \\
        &\leq \gamma_{l-1}n + 2\theta_{l-1}\gamma_{l-1}n + 2 \theta_{l-1}n \\
        &\leq 4\theta_{l-1}\gamma_{l-1}n \\
        &\leq \gamma_{l}n
    \end{align*}
    By the principle of induction we know that this holds for all $l \geq 1$, which concludes the proof.
\end{proof}

Additionally, since at every step in the recursion, at least one \textit{Immigrant} clause is added
to the formula and this clause must have size $\leq k - 1$ then it immediately follows that
\begin{corollary}
    Any path from root to leaf has at most $\gamma_{k-1} n$ edges.
\end{corollary}

Since the length of a path from the root to a leaf is at most $\gamma_{k-1}n$,
if we set $\alpha = \gamma_{k} \geq \gamma_{k-1}$ then we achieve our first goal of bounding the length of the longest
path by $\alpha \cdot n$. What remains is to show that the number of petal branches in the recursion tree
is at most $\frac{kn}{\delta}$.

\begin{lemma} \label{lem:bound_petals}
    For any path from the root to a leaf in the recursion tree, at most $\frac{kn}{\delta}$ branches
    are from adding petals.
\end{lemma}
\begin{proof}
    For a fixed path from the root to a leaf in the recursion tree, let $P_l$ be the number of times
    that petals are added to the formula with petals of size $l$.
    Recall the definition of a good flower,
    Equation \ref{eq:good_flower} tells us that such a flower must have had at least $\theta_{l}$
    petals. Therefore, we can conclude that along this path $P_l \cdot \theta_l$ individual petal
    clauses of size $l$ would have been added. Recall from Lemma \ref{lem:bound_immigrants} that
    $I_{\leq l}$ is the number of immigrant clauses of size $\leq l$ added along a path. Since every petal
    clause is an immigrant we obtain
    \begin{equation}
        I_{\leq l} \geq P_l \cdot \theta_l
    \end{equation}
    However, from Lemma \ref{lem:bound_immigrants} we also have that $I_{\leq l} \leq \gamma_l n$, so
    \begin{align*}
        P_l \cdot \theta_l \leq &I_{\leq l} \leq \gamma_l n \\
        P_l \cdot \theta_l &\leq \gamma_l n \\
        P_l &\leq \frac{\gamma_l}{\theta_l}n
    \end{align*}
    Thus, if we were to have that $\delta = \frac{\theta_l}{\gamma_l}$ then we obtain that $P_l \leq \frac{n}{\delta}$.
    If we sum over all $l$ to get the total number of petal branches then we obtain what we require.
    \begin{align*}
        \text{\#petal branches} &= \sum_{l=1}^{k} P_l \\
        \text{\#petal branches} &\leq \sum_{l=1}^{k} \frac{n}{\delta} \\
        \text{\#petal branches} &\leq \frac{kn}{\delta}
    \end{align*}
    This concludes the proof.
\end{proof}

We are now ready to set the constants $\theta_i$. We require that $\frac{\theta_l}{\gamma_l} = \delta$
from Lemma \ref{lem:bound_petals}
and that $\gamma_l = 4\gamma_{l-1}\theta_{l-1}$ from Lemma \ref{lem:bound_immigrants}.
Consider $\gamma_{l} = (4\delta)^{2^{l-1}-1}$ and $\theta_{l} = \delta \gamma_l$. Check that we have $\frac{\theta_l}{\gamma_l} = \delta$
as required. We also check that
\begin{align*}
    \gamma_l &= 4 \gamma_{l-1} \theta_{l-1} \\
    &= 4 \delta \gamma_{l-1}^2 \\
    &= 4 \delta ((4\delta)^{2^{l-2}-1})^2 \\
    &= (4\delta)^{2^{l-1}-1}
\end{align*}
as required.

We have now bounded the length of the longest path in the recursion tree by $\gamma_k n$
and bounded the number of petal branches by $kn / \delta$.
We therefore have that the number of leaves in the recursion tree is bounded by
\begin{equation*}
    \sum_{i=0}^{kn / \delta} \binom{\gamma_k n}{i} \leq 2^{H(\frac{k}{\delta \gamma_k})\gamma_k n}
\end{equation*}

What remains is to show that $H(\frac{k}{\delta \gamma_k})\gamma_k \leq \epsilon$.
If we require that $\delta \geq k$ then we get that $\frac{k}{\delta \gamma_k} \leq \frac{1}{2}$.
Consider the binary entropy function
\begin{equation}
    H(x) = -x\log(x) - (1-x)\log(1-x)
\end{equation}
for small $x$ we have that $-x\log(x) \geq -(1-x)\log(1-x)$, so we can bound
\begin{equation}
    H(x) \leq -2x\log(x)
\end{equation}
Plugging in $x = k / (\delta \gamma_k)$, we obtain
\begin{align*}
    H(\frac{k}{\delta \gamma_k}) &\leq -2 \frac{k}{\delta \gamma_k} \log(\frac{k}{\delta \gamma_k}) \\
    &\leq -2 \frac{k}{\delta \gamma_k} (\log(k) - \log(\delta) - \log(\gamma_k)) \\
    &\leq -2 \frac{k}{\delta \gamma_k} (\log(k) - \log(\delta) - (2^{k-1} - 1)\log(4\delta)) \\
    &\leq -2 \frac{k}{\delta \gamma_k} (- 2^{k}\log(\delta)) \\
    &\leq 2 \frac{k2^{k}\log(\delta)}{\delta \gamma_k} \\
    H(\frac{k}{\delta \gamma_k})\gamma_k &\leq 2 \frac{k2^{k}\log(\delta)}{\delta}
\end{align*}
Therefore, given $k$ and $\epsilon$ all that remains is to choose our $\delta$ such that
\begin{equation}
    2 \frac{k2^{k}\log(\delta)}{\delta} \leq \epsilon
\end{equation}
Which is what we require for our claim that there are at most $2^{\epsilon n}$ leaves.
It immediately follows that the run time of the algorithm is $\mathcal{O}^{\ast}(2^{\epsilon n})$, since
there can be at most $2^{\epsilon n}$ recursive calls and each takes polynomial time.

This concludes the proof. So we can say that Equation \ref{eq:run_time} holds and if 3-SAT
can be solved in subexponential time, then so can $k$-SAT for all $k \geq 3$.
% bound on number of clauses entering (Any path has at most beta_k-1 n edges)
% along a path there are few petal steps
% Bound on the number of leaves using the binary entropy function

\section{Exploration of the Lemma}
To gain a better understanding of the relationship between the constants $k, \epsilon, \delta, \gamma_l$ and $\theta_l$
consider the following.
\begin{align*}
    2 \frac{k2^{k}\log(\delta)}{\delta} &\leq \epsilon \\
    \frac{\log \delta}{\delta} &\leq \frac{\epsilon}{k \cdot 2^{k+1}}
\end{align*}
Using the following upper bound $\frac{\log x}{x} \leq \frac{\log(x + 1)}{x} \leq \frac{1}{\sqrt{1 + x}}$ \cite{topsok2006some}.
We can therefore choose $\delta$ such that
\begin{align*}
    \frac{1}{\sqrt{1 + \delta}} &\leq \frac{\epsilon}{k \cdot 2^{k+1}} \\
    \delta &\geq \frac{k^2 \cdot 2^{2k+2}}{\epsilon^2} - 1
\end{align*}
We can then note that $\delta$ grows exponentially with $k$ and polynomially with $\frac{1}{\epsilon}$.
Thus from the definition of $\gamma_l$ and $\theta_l$, we can see that they grow roughly as $\delta^{2^{l}}$,
so therefore we can justify our original claim that $\theta_l \gg \theta_{l-1}$.
\begin{example}
    We can consider an example where we have a 3-CNF formula that we wish to sparsify, $k = 3$ and we choose
    $\epsilon = 10^{-2}$. We can see that this leads to
    \begin{equation*}
        \delta \geq \frac{3^2 \cdot 2^{2 \cdot 3 + 2}}{{(10^{-2})}^{2}} - 1 = 23\,039\,999
    \end{equation*}
    We can the calculate the terms of $\gamma_l$ and $\theta_l$.
    \begin{table}
        \centering
        \begin{tabular}{l c c}
        \toprule
             $l$ & $\theta_l$ & $\gamma_l$ \\
             \midrule
             1 & 23\,039\,999 & 1  \\
             2 & $\sim 2.123 \cdot 10^{15}$ & 92159996 \\
             3 & $\sim 1.803 \cdot 10^{30}$ & $\sim 7.828 \cdot 10^{23}$ \\
        \bottomrule
        \end{tabular}
        \caption{Growth of Coefficients}
        \label{tab:coeffs_example}
    \end{table}
    The growth of these coefficients for this example can be seen in Table \ref{tab:coeffs_example}.
    Notice, that even for small examples these constants exhibit rapid growth. Therefore, even
    though we know from Corollary \ref{cor:no_good} that at the end of Algorithm \ref{alg:sparse},
    $|F| \leq 2k \cdot \theta_{k-1} n$ which is $\mathcal{O}(n)$, the constant hidden in the Big-$\mathcal{O}$
    is typically significantly larger than most practical instances of SAT which have typically at most on the
    order of $10^6$ clauses \cite{SAT_Comp2017}.
\end{example}

Therefore, due to large constants, the sparsification lemma is not easy to apply to $k$-SAT solvers
in the hopes of achieving better practical performance as measured by wall time. However, it is sufficient
to show that Equation \ref{eq:run_time} holds.

However, one by notice that there exists many upper and lower bounds in this proof which may have better bounds,
leading to smaller constants. For example, in Corollary \ref{cor:no_good} we bounded the sequence $(\theta_1 + \theta_2 + \dots + \theta_{k-1})$
by $k \theta_{k-1}$. Although, due to the rapid growth of $\theta_l$, this is almost $k$ times greater.
Hence it is possible that there exists tighter bounds than ones shown in this chapter.

\subsubsection{Numerical Tests}
To gain a better understanding of how the sparsification lemma behaves we implemented
the algorithms presented in this chapter and ran them on randomly CNF formulas.
Taking inspiration from the problem presented in the introduction, we generate CNF
formulas where $m = n^2$. Each clause picks 3 literals at random with uniform probability
with the additional requirement that all clauses must be unique. We do not concern ourselves
with whether the formulas are satisfiable or not.
Some results from these tests can be seen in Figures \ref{fig:sparsification}, \ref{fig:num_subformulas}, \ref{fig:tree_height}.

\begin{figure}
    \centering
    \includegraphics[scale=0.7]{Chapter4/Figs/sparsification_20.png}
    \caption{The size of the largest subformula compared with the size of the original formula}
    \label{fig:sparsification}
\end{figure}
\begin{figure}
    \centering
    \includegraphics[scale=0.7]{Chapter4/Figs/subformulas_20.png}
    \caption{Number of subformulas returned}
    \label{fig:num_subformulas}
\end{figure}
\begin{figure}
    \centering
    \includegraphics[scale=0.7]{Chapter4/Figs/tree_height_20.png}
    \caption{The height of the recursion tree for varying $n$}
    \label{fig:tree_height}
\end{figure}

As we can see in Figure \ref{fig:sparsification}, for small $n$ the largest subformula
is only slightly smaller than the size of the original formula. However, we note that
the bound for this instance was approximately $600n$, so for small $n$ the algorithm
could have done nothing whilst satisfying the bound. This suggests that often
Algorithm \ref{alg:sparse} will return CNF formulas sparser than required.

Due to computational restrictions, we were unable to test the behaviour of the algorithm
as the length of the original formula approaches that of the bound. We also note that
due to the large hidden constant in the bound on sparsity for small $n$ there are often
not enough unique clauses for the original formula to be above the bound on sparsity.

To get a sense for computational cost see Figures \ref{fig:num_subformulas} and \ref{fig:tree_height},
where we see rapid growth of the number of subformulas returned by Algorithm \ref{alg:sparse} and steady
growth in the height of the recursion tree. However, considering our choice of $\epsilon = 20$ we can
only guarantee that the number of subformulas returned is less than $2^{20n}$ which is a function growing much faster
than what we observe in Figure \ref{fig:num_subformulas}. This again suggests our theoretical bounds can be improved by
some constant factor.

Furthermore, even though Algorithm \ref{alg:best_flower} runs in polynomial time it quickly becomes infeasible
to the best flower of even the first formula. Considering that we let $m = n^2$ and $k=3$ the computational
complexity of Algorithm \ref{alg:best_flower} becomes $\mathcal{O}(n^5)$ where the hidden constant is $>100$.
While this is certainly polynomial time it makes it infeasible to test formulas large enough to be above
the bound on sparsity.

\subsubsection{Intuitions and Ideas for Further Work}
The sparsification lemma seems to capture an idea that dense formulas are somehow easy
since they can be made sparse in subexponential time potentially cutting away many clauses.
This is an idea that will be covered from another perspective in Chapter \ref{chap:structure}.
We can imagine that the hardest CNF formulas are those where $m$ is $\mathcal{O}(n)$, since
this can be enforced by the sparsification lemma, incurring only subexponential cost.

Furthermore, consider that the branching performed in Algorithm \ref{alg:sparse} is similar
to the branching performed by DPLL seen in Chapter \ref{chap:solvers}. However, in the worst case
DPLL runs in $\mathcal{O}(2^n)$. Impagliazzo et al. \cite{impagliazzo2001problems} comment
that branching on a disjunction of variables rather than single variables ends up requiring less
branching in total. This then brings up the question of whether the idea of branching on disjunctions
of variables could applied to SAT solvers. If we relax the requirement that the clause branched on
had to be the best flower and instead picked disjunctions of variables based on sensible heuristics that are efficiently computable,
would this lead to smaller recursion trees in algorithms based on DPLL? We leave this question for further work.


\documentclass[11pt,leqno]{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{amsmath, amsthm, amssymb, latexsym}
\usepackage{enumerate}
\usepackage{color}
\usepackage{hyperref}

\usepackage{url}

\usepackage{float}
% for better float placement, allows "H" ("HERE") as specification
% for float placement, e.g.
% \begin{figure}[H]

\graphicspath{{Graphics/}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% theorem declarations

\newtheorem{defn}{Definition}[section]
\newtheorem{thm}[defn]{Theorem}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\title{
Interactive Visualizations in Mathematica
\\
Final Report, Spring 2021}
\author{Faculty Mentor: A.J. Hildebrand \\
    Project Leader: Efstathios Konstantinos Chrontsios Garitsis \\
	IGL Scholars: Xiaojun Jia, Adithya Swaminathan, \\
	Dimitrios Tambakos, Troy Yang, Sarah Zimmerman}
	\date{May 12, 2021}
\maketitle
\section{Introduction}
This project is part of a long-term
program aimed at creating interactive Mathematica-based visualizations of
interesting mathematical topics and making these available to a broader
audience through publication at the Wolfram Demonstrations website \cite{WD}.
The ultimate goal is to develop a collection of attractive
interactive tools for use in instruction and outreach activities.

This semester we focused on visualizations of Julia sets
and of fractals defined in terms of digital sums.

\section{Julia Sets}
Let $P: \mathbb{C} \rightarrow \mathbb{C}$ be a complex polynomial and
let $P^k$ denote the $k$-th iterate of $P$,
i.e., $P^k = P \circ \dots \circ P$  $k$-times.

\begin{defn}
The \textbf{Julia set} of $P$ is the boundary of the set of points $z \in
\mathbb{C}$ for which the set $\{P^{k}(z) : k \in \mathbb{N}\}$ is
bounded.  The complement of the Julia set is the \textbf{Fatou set} of $P$.
\end{defn}

Julia sets of \emph{quadratic} polynomials of the form ${z^2 + c}$ have
been extensively studied and many visualizations of such Julia sets have
been created; see, for example, the books by Beardon \cite{beardon} and
Carleson and Gamelin \cite{carleson-gamelin} for an introduction to Complex Dynamics and the relevant visualizations in \cite{WD}.

In our project, we focused on Julia sets of \emph{cubic}
polynomials.  We are specifically interested in the nature of the Julia
set graph of polynomials of the form
\begin{equation}
\label{eq:polynomial}
P(z)=z^3+re^{ti}z^2+e^{2 \pi i \theta}z
\end{equation}
as the variables $r>0,\, t\in \mathbb{R},\, \theta \in
\mathbb{R}\setminus\mathbb{Q}$ vary.

We noticed several symmetry patterns in these graphs.  One such pattern
is illustrated in \mbox{Figure \ref{fig:rotational-symmetry}}: When $r=4$,
$\theta=\pi$ and $t$ varies over the interval $[0,2\pi]$, the
corresponding Julia set graph rotates about the origin by an angle $t$.

\begin{figure}[H]
\label{fig:rotational-symmetry}
\centering
\includegraphics[width=0.4\textwidth,height=0.3\textwidth]{1.PNG}
\hspace{1em}
\includegraphics[width=0.4\textwidth,height=0.3\textwidth]{2.PNG}
\\
\includegraphics[width=0.4\textwidth,height=0.3\textwidth]{3.PNG}
\hspace{1em}
\includegraphics[width=0.4\textwidth,height=0.3\textwidth]{4.PNG}
\caption{Julia set graphs for the polynomial \eqref{eq:polynomial}
when $r=4$, $\theta=\pi$, and $t=0$ (top left), $t=\pi/6$ (top right),
$t=\pi/3$ (bottom left), $t=\pi/2$ (bottom right).
}
\end{figure}

The rotational symmetry observed in Figure \ref{fig:rotational-symmetry}
appears to hold for a variety of values of $\theta$ (e.g., square roots of prime numbers, powers of $\pi$, powers of $e$) and most \emph{large} values of $r$ (say, $r>3$),
but seems to fail for most values of $r$ that are closer to $1$. For example,
Figure \ref{fig:rotational-symmetry-fail} shows the Julia set graphs
when $r=1.612$, $\theta=e^\Phi$ (where $\Phi=(\sqrt{5}+1)/2$
is the Golden Ratio), and $t=\pm 1.196$. The two graphs are clearly not
rotations of each other.
\begin{figure}[H]
\label{fig:rotational-symmetry-fail}
\centering
\includegraphics[width=0.4\textwidth]{pi2pi1.png}
\hspace{1em}
\includegraphics[width=0.4\textwidth]{pi2pi2.png}
\caption{Julia set graphs for the polynomial \eqref{eq:polynomial}
when $r=1.612$, $\theta=e^\Phi$, and $t=-1.196$ (left), $t=1.196$ (right).
}
\end{figure}

\section{Sum-of-Digit Fractals}

Given an integer $b\ge 2$, let  $s_{b}(n)$ denote the sum of the digits
of a number $n$ in base $b$.  For example, in base $b=10$ we have
$s_{10}(15) = 1 + 5 = 6$, while in base $b=2$, $s_{2}(15) =  1 + 1 + 1+
1 = 4$ since $15$ has base $2$ representation $1111$.

\begin{defn}
The \textbf{sum-of-digit fractal} $S(b,p,q)$ for base $b$
and parameters $p$ and $q$ is the graph with steps given by
\begin{equation}
\label{eq:steps}
e ^ {2 \pi i (s_b(n)/p + n/q)},
\quad n=1,2,3,\dots.
\end{equation}
\end{defn}
Observe that the steps \eqref{eq:steps} are steps of unit length in
direction given by the angle $2 \pi  (s_b(n)/p + n/q)$.

In the special case when $q=1$, $p$ is a positive integer, and $b$ is
congruent to $0$ or $1$ modulo $p$, the graphs $S(b,p,q)$
turn out to be finite and can be described precisely as follows:

\begin{thm}[Lehmer \& Lehmer \cite{lehmer-lehmer1979}]
\mbox{}
\begin{itemize}
\item If $b \equiv 1 \pmod{p}$, and $q = 1$, the graph $S(b,p,1)$
is a regular $p$-gon.
\item If $b \equiv 0 \pmod{p}$, and $q = 1$, the graph $S(b,p,1)$
consists of $p$ rotations of a regular $p$-gon about its center.
\end{itemize}
\end{thm}

In the case where $p$ and $q$ are both integers, but do not satisfy the
conditions of this theorem, the graph $S(b,p,q)$ is in general infinite,
but does exhibit a fractal-like behavior, as illustrated in  Figure
\ref{fig:sum-of-digit-fractal-integer-case}.
\begin{figure}[H]
\label{fig:sum-of-digit-fractal-integer-case}
\centering
\includegraphics[width=0.4\textwidth]
{n3000v2.png}
\hspace{1em}
\includegraphics[width=0.4\textwidth]
{n(1000)_b(11)_p(11)_q(7).png}
\caption{Examples of sum-of-digit fractals $S(b,p,q)$ with
integer parameters $p$ and $q$. The left figure shows the
first $3000$ steps of $S(3,5,3)$.
The right figure shows the
first $1000$ steps of $S(11,11,7)$.
}
\end{figure}

The most interesting case is the case where at least one of $p$ and $q$
is irrational.  In this case, the graph $S(b,p,q)$ can take on a
remarkable variety of shapes, from fractal-like features to distinctive
geometric patterns to random clouds, as illustrated in  Figure
\ref{fig:sum-of-digit-fractal-irrational-case}.  See Dekking
\cite{dekking1982} and Dekking \& Mend\'es-France
\cite{dekking-mendes-france1981} for further examples and some theoretical results.

\begin{figure}[H]
\label{fig:sum-of-digit-fractal-irrational-case}
\centering
\includegraphics[width=0.4\textwidth]
{n(10000)_b(2)_p(3)_q(ln2).png}
\hspace{1em}
\includegraphics[width=0.4\textwidth]
{n(10000)_b(5)_p(8dot72)_q(6dot34).png}
\caption{Examples of sum-of-digit fractals $S(b,p,q)$ with
irrational parameters. The left figure shows the
first $10000$ steps of $S(2,3,\ln 2)$.
The right figure shows the
first $10000$ steps of $S(5,8.72,6.34)$.
}
\end{figure}

\begin{thebibliography}{99}

\bibitem{beardon}
A.F. Beardon,
``Iteration of Rational Functions: Complex Analytic Dynamical Systems'',
\textit{Springer}, 1991.

\bibitem{carleson-gamelin}
L. Carleson and T.W. Gamelin, ``Complex Dynamics'',
\textit{Springer}, 1993.

\bibitem{dekking1982}
F.M. Dekking, ``On the distribution of digits in arithmetic sequences'',
\textit{Séminaire de théorie des nombres de Bordeaux}, 1--12 (1981).

\bibitem{dekking-mendes-france1981}
F.M. Dekking and M. Mend\`es France, M,
``Uniform distribution modulo one: a geometrical viewpoint'',
\textit{Journal f\"ur die reine und angewandte Mathematik} \textbf{329}
(1981),  143--153.
\bibitem{lehmer-lehmer1979}
D.H. Lehmer and E. Lehmer,  ``Picturesque exponential sums, I'',
\textit{The American Mathematical Monthly} \textbf{86} (1979), 725--733.

\bibitem{WD}
Wolfram Demonstrations Project, \url{https://demonstrations.wolfram.com}.

\end{thebibliography}

\end{document}


\documentclass{article}
\usepackage{fullpage,graphicx}
\usepackage{amsmath,amsfonts,amsthm,amssymb,multirow}
\usepackage{algorithmic,comment,url}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing, shapes}
\usepackage[ruled,vlined,commentsnumbered,titlenotnumbered]{algorithm2e}
\newcommand{\expecting}[1]{\noindent{[\textbf{We are expecting:} \em #1\em]}}
\newcommand{\pts}[1]{\textbf{(#1 pt.)}}

\begin{document}
\noindent
CS 161 \hfill \textbf{Problem Set 2} \newline
{Winter 2019} \hfill \textbf{Due:} Friday, January 25, 2019, at 3pm on Gradescope

\noindent
\rule{\linewidth}{0.4pt}

\section*{Exercises}
Please do the exercises on your own.

\noindent
\rule{\linewidth}{0.4pt}
\begin{enumerate}
\item\pts{2} In your pre-lecture Exercise for Lecture 3, you saw two different ways to solve the recurrence relation $T(n) = 2\cdot T(n/2) + n$ with $T(1) = 1$.  We saw that $T(n)$ is exactly $n(1 + \log(n))$, when $n$ is a power of two.
In this exercise, you'll do the same for a few variants.
\begin{enumerate}
	\item What is the exact solution to $T(n) = 2 \cdot T\left( \frac{n}{2} \right) + n$ with $T(1) = 3$, when $n$ is a power of $2$?
	\item What is the exact solution to $T(n) = 2\cdot T\left( \frac{n}{2} \right) + 3n$ with $T(1) = 1$, when $n$ is a power of $2$?
\end{enumerate}
\expecting{ Your answer---\textbf{no justification required.} Notice that we want the exact answer, so don't give a $O()$ statement. }

%%%%%%%%%%

\item\pts{4} Use any of the methods we've seen in class so far to give big-Oh solutions to the following recurrence relations.  You may treat fractions like $n/2$ as either $\lfloor n/2 \rfloor$ or $\lceil n/2 \rceil$, whichever you prefer.
\begin{enumerate}
\item $T(n) = 2T\left( \frac{n}{4} \right) + \sqrt{n}$ for $n \geq 4$, and $T(n) = 1$ for $n < 4$.
\item $T(n) = T(n-2) + n$ for $n \geq 2$, and $T(n) = 1$ for $n < 2$.  (You may assume $n$ is even if it helps).
\item $T(n) = 7T\left( \frac{n}{3} \right) + n^2$ for $n \geq 3$, and $T(n) = 1$ for $n < 3$.
\item $T(n) = 7T\left( \frac{n}{2} \right) + n^2$ for $n\geq2$, and $T(n) = 1$ for $n < 2$.
\end{enumerate}

\expecting{For each item, the best answer you can give of the form $T(n) = O(\_\_\_)$ and a justification.  (That is, all of these satisfy $T(n) = O(2^n)$, but you can do better).  You do not need to give a formal proof, but your justification should be convincing to the grader.  You may use the Master Theorem if it applies.}

%%%%%%%%

\item \pts{2} Consider the following algorithm, which takes as input an array $A$:
\begin{verbatim}
def printStuff(A):
     n = len(A)
     if n <= 4:
         return
     for i in range(n):
         print(A[i])
     printStuff(A[:n/4])  # recurse on first n/4 elements of A
     printStuff(A[3*n/4:])  # recurse on last n/4 elements of A
     return
\end{verbatim}
What is the asymptotic running time of \texttt{printStuff}?

\expecting{The best answer you can give of the form ``The running time of \texttt{printStuff} is $O(\_\_\_\_)$'' and a short explanation.}

\end{enumerate}

%%%%%%%%%%%%%%%%

\noindent
\rule{\linewidth}{0.4pt}
\newpage
\section*{Problems}
You may talk with your fellow CS161-ers about the problems.  However:
\begin{itemize}
	\item Try the problems on your own \em before \em collaborating.
	\item Write up your answers yourself, in your own words.   You should never share your typed-up solutions with your collaborators.
	\item If you collaborated, list the names of the students you collaborated with at the beginning of each problem.
\end{itemize}

\noindent
\rule{\linewidth}{0.4pt}

\begin{enumerate}
\setcounter{enumi}{3}
\item\pts{4} [\textbf{Substitution Method.}] Consider the function $T(n)$ defined recursively by
\[ T(n) = \begin{cases} T\left( \left\lceil \frac{n}{2} \right\rceil \right) +  T\left( \left\lceil \frac{n}{4} \right\rceil \right) + n  & n > 4 \\ 1  & n \leq 4 \end{cases}\]
Prove using the substitution method that $T(n) = O(n)$.
If it helps \textbf{you may assume that $n$ is a power of $2$.}  You may also take it as given than $T(n) \geq 0$ for all $n$.

\expecting{A formal proof by induction.  Remember to state your inductive hypothesis, base case, inductive step, and conclusion.  Notice that it's fine to prove a big-Oh bound, you don't need to come up with exact formula for $T(n)$.}

%%%%%%%%%5

\item \pts{5} [\textbf{Fishing.}] Plucky the Pedantic Penguin is fishing.  Plucky catches $n$ fish, but plans to keep only the $k$ largest fish and to throw the rest back.  Plucky has already named all of the fish, and has measured their length and entered the (name, length) pairs into an array $F$ of length $n$.  For example, $F$ might look like this:
\[ F = \begin{bmatrix}
\text{(Frederick the Fish, 14.2in)}\\ \text{(Fabiola the Fish, 10in)}\\ \text{(Farid the Fish, 12.35in)} \\ \vdots \\ \text{(Felix the Fish, 6.234523in)} \\ \text{(Finlay the Fish, 6.234524in)} \end{bmatrix} \]
\begin{enumerate}

\item \pts{2} Give an $O(n\log(n))$-time deterministic algorithm that takes $F$ and $k$ as input and outputs the names of the $k$ largest fish.
You may assume that the lengths of the fish are distinct.

\textbf{Note:} Your algorithm should run in time $O(n\log(n))$ even if $k$ is a function of $n$.  For example, if Plucky wants to keep the largest $k = n/2$ fish, your algorithm should still run in time $O(n\log(n))$.

\expecting{Pseudocode \textbf{AND} a short English description of your algorithm.  You may (and, hint, may want to...) invoke algorithms we have seen in class.  You do not need to justify why your algorithm is correct or its running time. }

\item \pts{3} Give an $O(n)$-time deterministic algorithm that takes $F$ and $k$ as input and outputs the names of the $k$ largest fish.
You may assume that the lengths of the fish are distinct.
Your algorithm should also be fundamentally different than your algorithm for part (a).  (That is, don't solve part (b) and then use that as your solution to part (a); part (a) should be easier).

\textbf{Note:} Your algorithm should run in time $O(n)$ even if $k$ is a function of $n$.  For example, if Plucky wants to keep the largest $k = n/2$ fish, your algorithm should still run in time $O(n)$.

\expecting{Pseudocode \textbf{AND} a short English description of your algorithm.  You may (and, hint, may want to...) invoke algorithms we have seen in class.  You do not need to justify why your algorithm is correct or its running time. }
\end{enumerate}

%%%%%%%%%%%%%%%5

\newpage
\item \pts{8} \textbf{[Penguinology.]} On an island, there are $n$ penguins of many different species.  The differences between the species are very subtle, so without help you can't tell the penguins apart at all.  Fortunately, you have an expert with you, and she can tell you whether or not two penguins belong to the same species.  More precisely, she can answer queries of the form:

\[ \text{\texttt{isTheSame( penguin1, penguin2 )}} = \begin{cases} \textbf{True} & \text{ if \texttt{penguin1} and \texttt{penguin2} belong to the same species } \\ \textbf{False} & \text{ if \texttt{penguin1} and \texttt{penguin2} belong to different species } \end{cases} \]

The only way you can get any information about the penguins is by running \texttt{isTheSame}.  You cannot ask them what species they are, or compare them in any other way.

The expert assures you that one species of penguin is in the majority.  That is, there are \em strictly greater \em than $n/2$ penguins of that species.  Your goal is to return a single member of that majority species.  For example, if the population looked like this:
\begin{center}
\begin{tikzpicture}[scale=.8]
\foreach \x in {0,1,2,3,4,5}{
\node at (\x*3,.2) {\includegraphics[width=1.5cm]{penguin}};
\node at (\x*3,0) {\x};
}
\node[cloud, draw, cloud puffs=20, cloud ignores aspect] at (0, 2) {\footnotesize I am species $A$};
\node[cloud, draw, cloud puffs=20, cloud ignores aspect] at (3, 3) {\footnotesize I am species $A$};
\node[cloud, draw, cloud puffs=20, cloud ignores aspect] at (6, 2) {\footnotesize I am species $B$};
\node[cloud, draw, cloud puffs=20, cloud ignores aspect] at (9, 3) {\footnotesize I am species $C$};
\node[cloud, draw, cloud puffs=20, cloud ignores aspect] at (12, 2) {\footnotesize I am species $A$};
\node[cloud, draw, cloud puffs=20, cloud ignores aspect] at (15, 3) {\footnotesize I am species $A$};

\end{tikzpicture}
\end{center}
then species $A$ is in the majority, and your algorithm should return any one of Penguins $0$, $1$, $4$, or $5$.

If there is no species with a strict majority, your algorithm may return whatever it wants.

\begin{enumerate}
\item \pts{4}
Design a deterministic divide-and-conquer algorithm which uses $O(n \log(n))$ calls to \texttt{isTheSame} and returns a penguin belonging to the majority species.  You may assume that $n$ is a power of $2$ if it is helpful.

\expecting{Pseudocode (which calls \texttt{isTheSame}) \textbf{AND} a clear English description of what your algorithm is doing.}

\item \pts{1} Explain why your algorithm calls \texttt{isTheSame} $O(n \log(n))$ times.

\expecting{A short explanation. You may use the Master Theorem if it applies.}

\item \pts{3}
Prove, using an argument by induction, that your algorithm is correct.

\expecting{A rigorous proof by induction.  Make sure to clearly state your inductive hypothesis, base case, inductive step, and conclusion.}

\item \pts{NOT REQUIRED. 1 BONUS} Is $O(n\log(n))$ the best guarantee you can come up with?  Either give an asymptotically faster algorithm which finds a majority-species penguin, or else describe prove that no such algorithm exists.
(Or, if you think that your algorithm from part (a) already does better that $\Theta(n\log(n))$, just write that :) ).

\expecting{Nothing.  This part is not required.}

\end{enumerate}

%%%%%%%%

\end{enumerate}
\newpage
\section*{Feedback}
This part is not worth any points, but it is quick, painless, and anonymous, and we'd really appreciate it if you help us out by giving us feedback!

\begin{enumerate}
\item \pts{0} Please fill out the following poll, which asks about the pace of lectures so far:
\begin{center}
\url{https://goo.gl/forms/mOUlY8r2eeZGP3fB2}
\end{center}
\end{enumerate}

\end{document}


\chapter{Constants}
\section{Global Constants}
Most applications will use particular values throughout the code. Imagine an application which does a lot of calculations with circles: you will definitely use the number pi a lot. You could calculate pi every time you need it, but that's not a good idea because the outcome will be the same every time. You're making your computer do needless calculations. Instead you could declare a global variable:

\begin{code}
int pi = 3.1415926;
\end{code}

With this variable, you can use pi everywhere in your code. But mistakes happen, and what about this one:

\begin{code}
int value = 1;
// ... more code ...
if(pi = value) {
  // do something
}
\end{code}

The code above won't result in an error. But instead of comparing pi, you assign a new value to pi by mistake. Auch! This mistake is easily made, but it might take a while before you realize why every calculation with pi suddenly has the wrong outcome.

It would be much better if we could prevent such a mistake from being made. After assigning a value to pi, there is no reason why it should change. We need a way to prevent changes made to this variable. That's why most programming languages provide a way to make a variable `constant'. A constant can never changes after its declaration. And to increase readability, most programmers will always write constants in capitals.

So how do you declare a constant in C++? You precede it with the word \verb|const|. Esenthel makes it even easier by defining the capital \verb|C| as \verb|const|, just like you can write \verb|T| instead of \verb|this|. In Esenthel, you could declare PI as:

\begin{code}
C PI = 3.1415926;
\end{code}

This approach has two advantages:

\begin{enumerate}
	\item The value of PI can never be changed by mistake.
	\item If, for some crazy reason, you \textit{need} to change the value of PI, you only have to change it this one instance. \textsl{(Highly unlikely in this case, but it will happen with other constant values in your application. Imagine a constant named ATTACK\_RANGE. You probably will want to experiment with that before releasing your game.)}
\end{enumerate}

\begin{note}
Because PI is needed in almost every game, Esenthel already declared it for you. And not only PI is defined, but also a few common calculations like PI\_2 (half of PI) and PI2 (two times PI).
\end{note}

\begin{exercise}
Write an application with the following constants: playerColor, playerSize, enemyColor and enemySize. The player is a rectangle, the enemies are circles \textit{(it is a very abstract game)}. Draw a player and some enemies on the screen using these constants.
\end{exercise}

\section{Const Arguments}
There's another situation in which constants are used. Just look at this function:

\begin{code}
float calculateDistance(Vec2 & pos1, Vec2 & pos2);
\end{code}

Suppose you can use this function to calculate the distance between two points. In chapter \ref{chapter:references} you read that it is often faster to pass a variable by reference as opposed to passing it by value. But there's a downside on that. In principle, you could alter the values of pos1 and pos2 within this function. And if a value is passed by reference, this would also change those variables in the original location. But imagine you are working in a team and the function \eeFunc{calculateDistance} is written by someone else. If something unexpected happens in your code, you would have to double check the work of your colleague to verify the values are not changed in there. The name of the function seems to indicate there's no reason for that, but mistakes happen. A lot.

It would be better if we can now for sure the values we pass to this function will not be changed by it. That way, we can focus on our own code when there's an error. The solution is simple: we can pass these references as constants:

\begin{code}
float calculateDistance(C Vec2 & pos1, C Vec2 & pos2);
\end{code}

This is much better because:

\begin{enumerate}
	\item when creating such a function, you will get an error if you try to change pos1 or pos2;
	\item the programming which uses this function can be sure the values are not changed in there, without reading your code;
	\item you have a strong indication that values \textit{will} be changed inside a function when the reference is not declared as a constant. \textit{(It's either that, or you need to have a serious talk with your fellow developers.)}
\end{enumerate}

Let's agree that, starting right now, you will pass all function arguments as a const reference, unless you have a good reason not to. Your future colleagues will like you a lot more when you do so.

And what is a good reason not to use a constant? Look at the Esenthel \eeFunc{Clamp} function:
\begin{code}
void Clamp(Vec2 & value, C Vec2 & min, C Vec2 & max);
\end{code}

This function will change the first argument when its value is lower than the second or higher than the third. It can be used like this:

\begin{code}
Vec2 pos = Ms.pos();
Clamp(pos, Vec2(-0.4,-0.4), Vec2(0.4,0.4));
pos.draw(RED);
\end{code}

The second and third argument are constant, so the function will never change the minimum or maximum. But the first argument is the one that is meant to change. No const reference there.

\begin{exercise}
\begin{itemize}
	\item Search the Engine code for more functions with non-const references. Try to explain why these references are not constant.
	\item Write a function `ClampToScreen' which changes an argument (a Vec2) when it would be outside the screen. Test your function with a simple program. Can you use a const reference?
	\item Write a function with a string argument. The function will draw the string on the screen. Create a version with a const reference and one with a non-const reference. Test both versions with existing \eeClass{Str} variables as well as with string literals. Why won't the non-const version work with string literals?
\end{itemize}
\end{exercise}