\documentclass{article}
\usepackage{amsmath}
\advance\textwidth by1.4in\advance\oddsidemargin by-0.7in
\advance\textheight by1.1in\headheight 0pt\topskip 0pt\headsep 0pt\topmargin 0pt
\begin{document}
\thispagestyle{empty}
\begin{center}
  \large\bf CS 561\ \ Data Structures and Algorithms II\\
  \bigskip

  \large\it Homework \#3: Solutions
\end{center}

\bigskip
\emph{Problem 1.}   
The example with $k=1$ is easily generalized to arbitrary $k$.
Assume that, as we start, the last $k$ items in our list are
(from the back), items $1,2,3,\ldots,k$.  We will generate
the request list\\
\ \ 1 2 3 4 \dots k-1 k 1 2 3 4 \dots k-1 k 1 2 3 4 \dots\\
Observe that, after the first $k$ requests, our list is back
to what it was before we started: every successive request simply
rotates the last $k$ elements by one position.  Now, in the optimal
list, we could just have elements $1,2,3,\ldots,a$k in the \emph{first}
$k$ positions.  The series of $k$ requests then costs us
$\sum_{i=1}^k i = \Theta(k^2)$ in the optimal list, but
$\sum_{i=n-k}^n i = \Theta(kn)$ in our list; the ratio of the cost
in our list to the cost in the optimal list is thus $\frac{n}{k}$,
which is unbounded (it grows as $n$ grows).

To see that moving forward by a fixed fraction works as desired
we use almost the same potential function as in the analysis done in class.
Assume that, at some step in the handling of these searches, we have
to search for item $x$; further assume that $x$ occurs in the $i$th position
in the MTF list and in the $j$th position in the optimal static list.
Then the actual cost of a search with the MTF algorithm for item $x$ at
this step is $i$---the number of nodes in the list that have to be traversed
in order to find $x$; similarly the cost in the optimal list is $j$.
For each item $x$ in position $i$ in the MTF list, we count the number
of items that follow $x$ in the optimal static list, but appear between
positions $\alpha i$ ($\alpha$ is our fraction) and $i-1$
in the MTF list, and multiply the result by some fixed constant $c > 0$
(to be picked later).  If we write $P$ for the overall potential,
it is made of a sum of individual potentials $\Phi = \sum_x \phi_x$,
where $\phi_x$ is the potential contribution of item $x$.

Searching the optimal static list does not alter the potential---only
a change in the position of items can do that.  Searching in the MTF
list does alter the potential, in two ways: first, it brings $x$ to
position $\alpha i$, reducing its contribution to the potential down to some
unknown value $q$;
and secondly, it moves an extra item in front of the items that
used to be in the $(\alpha i)$th through $(i-1)$st positions in
the list, possibly increasing their contribution to the potential.
The contribution of the first is $-c\cdot (\phi_x-q)$.
To see how much the second contributes, note that some item $y$
that sits in front of $x$ in the original MTF list will see its
contribution $\phi_y$ increase after $x$ is moved to the front
only if (i) $y$ sits in one the positions between $\alpha i$ and $i-1$
and (ii) $y$ occurs in front of $x$ in the optimal list as well as in
the original MTF list.  There are $(1-\alpha)i - \phi_x$ such items.
Thus the change in potential caused by moving $x$ to the front of the
MTF list is a decrease of $c\cdot(\phi_x-q)$ and an increase of
$c\cdot((1-\alpha)i-\phi_x)$,
for a total of $\Delta\Phi = c\cdot((1-\alpha)i-2\phi_x+q)$.
The sum of the real cost of searching for $x$ in the MTF list plus the
change in potential is thus
  $$a_{MTF} = i + c\cdot((1-\alpha)i-2p_x+q) =  (1-\alpha c)\cdot i + c\cdot i - 2c\cdot\phi_x + c\cdot q$$
Because the optimal list has at least
$(1-\alpha)i-\phi_x$ items in front of $x$, we can write
$j > (1-\alpha)i-\phi_x$, or $c\cdot i - 4c\cdot\phi-x < 4c\cdot j$.
Combining our inequalities, we can finally write that the amortized
cost in the MTF list is
  $$a_{MTF} = (1-\alpha c)\cdot i + c\cdot i - 2c\cdot\phi_x + c\cdot q < (1-\alpha c)\cdot i + c\cdot i - c\cdot\phi_x < (1-\alpha c)\cdot i + 4c\cdot j$$
If we choose $c =\frac{1}{\alpha}$, the first term on the right-hand side
vanishes and we can write
  $$a_{MTF} <  \frac{4}{\alpha}\cdot j$$
proving our result.  Other choices for the potential function (including
the one we used in the original MTF analysis) also work, but may lead to
other ratios.

Note that, the larger $\alpha$ is, the larger the constant $c$
must be---in the banker's analogy, we have to put down extra coins when
$\alpha$ is larger (and the item is not moved as far forward) in order
to keep the balance working.  With $\alpha=1$ (the original MTF
analysis), we could use $c=1$; with $\alpha=\frac12$, we need
$c=2$.  If we had, say $\alpha=\frac{1}{10}$ (so that the item is
moved only $\frac{1}{10}$ of the way to the front), we would then need
$c=10$.  If we kept $c=1$, we would have to alter the ratio to less
favorable values.

Yet other potential values and choices of multiplicative constants also
work.  Interestingly, for any choice of $\alpha$, we can always find
a combination that will keep a competitive ratio of~2.

The randomized analysis will be left to a future discussion.

\bigskip
\emph{Problem 2.}   
Incrementing word by word with ripple carry is really no different
from incrementing bit by bit within one word---it's just that the
carry ripples from one word to the next much more rarely than from
one bit to the next.

The simplest potential function one can use is just the number of words
(in the binary representation of the number) that have no more room for
an increment---that is, the number of words in which all 32 (or 64) bits
are set to 1---, multiplied by the size of these words (32 or 64 bits).
 Let us then examine the
amortized cost of an increment that ripples the carry over $i$ words
($i\geq 0$) to stop at the next word.  The real cost is proportional to
the total number of words examined (and changed), that is, $i+1$, times
the size of that word (say $32$), plus at most the cost of propagating
the carry through all positions but one of the next word (thus another $31$
bit carries).  Thus the cost is $32(i+1) + 31$.
The change in potential is the change in the number of words in which every
bit is a $1$ (times $32$): this increment operation replaces $i$ such words
by words composed of all $0$s, and might cause the next word to become
a word with all $1$s in it, so the change in potential is a decrease by $32i$
followed by a possible increase by at most $32$, for a total decrease of at
least $32(i-1)$.
We now have
  $$\text{real cost}+\Delta\phi \leq 32(i+1) + 31 - 32(i-1) = 95$$
so that the amortized cost is bounded by a constant ($95$ in this case),
as desired.


\bigskip
\emph{Problem 3.}   
Let us assume that the array size gets doubled when the number of elements
exceeds the size of the current array and gets halved when
the number of elements falls below $\frac14$ of the size of the current array.
Denote by $n$ the number of elements in the array and by $S$ the size of
the array.  Because of our choice of thresholds, we always have
$\frac14 S\leq n\leq S$.  The cost of halving the array is $\frac{S}2$
and that of doubling the array is $2S$---both because we must initialize
the new array.  If the current size is $n$, then we need $n-\frac{S}4$
deletions to cause a halving event of cost $\frac{S}2$ and we need $S-n$
insertions to cause a doubling event of cost $2S$.
We define our potential to be $\alpha\cdot(n-\frac58 S)$---a multiple of the
``distance" to the midpoint between the two trigger points, so that the
potential will be $\alpha\frac38 S$ at either trigger point.
Now we have four kinds of events
\begin{itemize}
  \item
  An insertion that does not trigger a doubling: the cost is one unit
  and the potential goes up or down by $\alpha$---total is
  $1\pm\alpha$ units.
  \item
  An insertion that triggers a doubling: the cost is $2S$ and the potential
  changes from $\alpha\frac38 S$ down to $\alpha(\frac 58 2S - S) =
  \alpha\frac{S}4$---total is
  $2S-\alpha\frac{S}8$.
  \item
  A deletion that does not trigger a halving: the cost is one unit
  and the potential goes down or up by $\alpha$---total is $1\pm\alpha$
  units.
  \item
  A deletion that triggers a halving: the cost is $\frac{S}2$ and the
  potential changes from $\alpha\frac38 S$ down to $\alpha(\frac{S}2 -
  \frac58\frac{S}2) = \alpha\frac{3}{16} S$---total is
  $\frac{S}2 -\alpha\frac{3}{16} S$.
\end{itemize}
If we set $\alpha=16$, then the largest total from any of these operations
is $17$ units ($1+\alpha$) and so all four events take amortized constant
time, as we wanted to show.
\end{document}
