\documentclass[a4paper, 10pt, pagesize, smallheadings]{article}  
\usepackage{graphicx}
%\usepackage[latin1]{inputenc}
\usepackage{amsmath, amsthm, amssymb}
\usepackage{typearea}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{fullpage}
\usepackage{mathtools}
\usepackage[all]{xy}
\title{Theory I, Sheet 6 Solution}
\author{Eugen Sawin}
\renewcommand{\familydefault}{\sfdefault}
\include{pythonlisting}

\pagestyle{empty}
\begin{document}
\maketitle

\section*{Exercise 6.1}
\begin{align*}
\Phi(T) &= |2 \cdot num - size|\\
a_i &= \Phi_i - \Phi_{i-1} + t_i\\
a_i &= |2k_i - s_i| - |2k_{i-1} - s_{i-1}| + t_i\\
\end{align*}
We calculate the amortized costs for the two distinct cases.

\subsection*{case 1 (no contraction required)}
Since there was no contraction we know that $s_i = s_{i-1}$ and it follows that $\frac{1}{3}s_i \leq k_i \leq s_i - 1$. Since we have deleted an element we know that $k_i = k_{i-1} - 1$. Because we have only removed an element it follows that $t_i = 1$. We reduce the formula using these substitutions.
\begin{align*}
a_i &= |2k_i - s_i| - |2k_{i-1} - s_{i-1}| + t_i\\
&= |2k_i - s_i| - |2k_i + 2 - s_i| + 1\\
\end{align*}
Now we look for the minimum and maximum value of the above formula. We choose the obvious values for $k_i$ which are within valid range.
\begin{align*}
(k_i = \frac{s_i}{2}) \implies a_i &= |s_i - s_i| - |s_i - s_i + 2| + 1\\
&= 0 - 2 + 1 = -1\\
(k_i = \frac{s_i}{2} - 1) \implies a_i &= |s_i - 2 - s_i| - |s_i - 2 - s_i + 2| + 1\\
&= 2 - 0 + 1 = 3\\
\implies &-1 \leq a_i \leq 3\\
\end{align*}
\newpage
\subsection*{case 2 (contraction required)}
Since there was a contraction we know that $s_i = \frac{2}{3}s_{i-1}$ and it follows that $k_i = \frac{1}{3}s_{i-1} - 1$. Since we have deleted an element we know that $k_i = k_{i-1} - 1$. Because we have removed an element and additionally copied all previously contained elements it follows that $t_i = \frac{1}{3}s_{i-1} + 1$. We reduce the formula again using these substitutions.
\begin{align*}
a_i &= |2k_i - s_i| - |2k_{i-1} - s_{i-1}| + t_i\\
&= |2k_i - \frac{2}{3}s_{i-1}| - |2k_i + 2 - s_{i-1}| + t_i\\
&= |\frac{2}{3}s_{i-1} - 2 - \frac{2}{3}s_{i-1}| - |\frac{2}{3}s_{i-1} - 2 + 2 - s_{i-1}| + \frac{1}{3}s_{i-1} + 1\\
&= |-2| - |-\frac{1}{3}s_{i-1}| + \frac{1}{3}s_{i-1} + 1\\
(\text{since } s_{i-1} > 0) \implies a_i &= 2 - \frac{1}{3}s_{i-1} + \frac{1}{3}s_{i-1} + 1\\
&= 2 + 1 = 3 \leq 3\\
\end{align*}
By calculating the amortized costs for both Table-Delete cases, we have have shown that the amortized costs are bounded by constant 3. \qed

\section*{Exercise 6.2}
\begin{align*}
\alpha &= \frac{k}{s}\\
\Phi(T) &= 
\left\{ 
\begin{array}{l l} 
  2k-s & \text{if $\alpha \geq \frac{1}{2}$}\\
  \frac{s}{2}-k & \text{if $\alpha < \frac{1}{2}$}\\
\end{array} \right.\\
\end{align*}

We show $\sum a_i \geq \sum t_i$.
\begin{align*}
a_i &= \Phi_i - \Phi_{i-1} + t_i\\
\implies \sum_{i=1}^{n}a_i &= \sum_{i=1}^{n}\Phi_i - \Phi_{i-1} + t_i\\
&= \sum_{i=1}^{n}\Phi_i - \sum_{i=0}^{n-1}\Phi_i + \sum_{i=1}^{n}t_i\\
&= \Phi_n - \Phi_0 + \sum_{i=1}^{n}t_i\\
\end{align*}
By definition we know that $\Phi_0 = -1$ and since $2k-s \geq 0$ for $\alpha \geq \frac{1}{2}$ and $\frac{s}{2}-k \geq 0$ for $\alpha < \frac{1}{2}$ it follows that $\Phi_n - \Phi_0 \geq 0$.
\[\implies \Phi_n - \Phi_0 + \sum_{i=1}^{n}t_i \geq \sum t_i\]\qed

\section*{Exercise 6.3}
We show $\sum_{k=2}^{n-1}k\,lg\,k \leq \frac{1}{2}n^2\,lg\,n - \frac{1}{8}n^2$.
\begin{align*}
\sum_{k=2}^{n-1}k\,lg\,k &\leq \frac{1}{2}n^2\,lg\,n - \frac{1}{8}n^2\\
\sum_{k=2}^{n-1}k\,lg\,k &= \sum_{k=2}^{\lceil \frac{n}{2} \rceil - 1}k\,lg\,k\ + \sum_{k=\lceil \frac{n}{2} \rceil}^{n-1}k\,lg\,k\\
\sum_{k=2}^{n-1}k\,lg\,k &\leq \sum_{k=2}^{\lceil \frac{n}{2} \rceil - 1}k\,lg\,\lceil\frac{n}{2}\rceil + \sum_{k=\lceil \frac{n}{2} \rceil}^{n-1}k\,lg\,n\\
&= lg\,\lceil \frac{n}{2} \rceil \sum_{k=2}^{\lceil \frac{n}{2} \rceil - 1}k + lg\,n \sum_{k=\lceil \frac{n}{2} \rceil}^{n-1}k\\
&= (lg\,n - lg\,2) \sum_{k=2}^{\lceil \frac{n}{2} \rceil - 1}k + lg\,n \sum_{k=\lceil \frac{n}{2} \rceil}^{n-1}k\\
&= lg\,n \sum_{k=2}^{\lceil \frac{n}{2} \rceil - 1}k - \sum_{k=2}^{\lceil \frac{n}{2} \rceil - 1}k + lg\,n \sum_{k=\lceil \frac{n}{2} \rceil}^{n-1}k\\
&= lg\,n \sum_{k=2}^{n-1}k - \sum_{k=2}^{\lceil \frac{n}{2} \rceil - 1}k\\
&\leq lg\,n \frac{(n-1)(n-2)}{2} - \frac{(\frac{n}{2} - 1)(\frac{n}{2} - 2)}{2}\\
&= \frac{1}{2}lg\,n (n^2 - 3n + 2) - \frac{1}{2}(\frac{n^2}{4} - n - \frac{n}{2} + 2)\\
&= \frac{1}{2}n^2\,lg\,n - \frac{3}{2}n\,lg\,n + lg\,n - \frac{1}{2}(\frac{n^2}{4} - n - \frac{n}{2} + 2)\\
&= \frac{1}{2}n^2\,lg\,n - \frac{3}{2}n\,lg\,n + lg\,n - \frac{1}{8}n^2 + \frac{n}{2} + \frac{n}{4} - 1\\
&= \frac{1}{2}n^2\,lg\,n - \frac{1}{8}n^2 + lg\,n - \frac{3}{2}n\,lg\,n + \frac{3}{4}n - 1\\
&\leq \frac{1}{2}n^2\,lg\,n - \frac{1}{8}n^2 + lg\,n - \frac{3}{2}n + \frac{3}{4}n - 1\\
&= \frac{1}{2}n^2\,lg\,n - \frac{1}{8}n^2 + lg\,n - \frac{3}{4}n - 1\\
\implies \sum_{k=2}^{n-1}k\,lg\,k &\leq \frac{1}{2}n^2\,lg\,n - \frac{1}{8}n^2 + lg\,n - \frac{3}{4}n - 1 \leq \frac{1}{2}n^2\,lg\,n - \frac{1}{8}n^2\\
\implies &\frac{1}{2}n^2\,lg\,n - \frac{1}{8}n^2 + lg\,n - \frac{3}{4}n - 1 \leq \frac{1}{2}n^2\,lg\,n - \frac{1}{8}n^2\\
\iff &lg\,n - \frac{3}{4}n - 1 \leq 0\\
\text{since } \forall{n \in \mathbb{N}}:\, lg\,n - \frac{3}{4}n \leq 0 &\implies \, lg\,n - \frac{3}{4}n - 1 \leq -1 \leq 0\\
\end{align*}
\qed
\end{document}
