\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 \#2: Solutions
\end{center}

\bigskip
\emph{Problem 1.}   
With all pieces of size $\frac{n}{q}$, the recurrence becomes simply
$$t(n) = p \cdot t(\frac{n}{q}) + g(n)$$
First let us dispose of the case $q=1$: it is not possible, since
then the recurrence never converges (it calls itself with the same argument).

Thus we must have $n = q^{k}$ and so can write
$$f(k) = p \cdot f(k-1) + g(q^{k})$$
The homogeneous solution is then easy, since we have a single characteristic
root $r_1 =p$: the solution is of the form
$f_{h}(k) = \Theta(p^{k})$ or, in terms of $n$, of the form
$t_{h}(n) = \Theta(n^{\log_{q} p})$.

To obtain the form of the homogeneous solution, we need to know
more about the driving function $g(n)$.  The text suggests looking at 7
cases:
\begin{itemize}
  \itemsep 0pt
  \item
   With $g(n) = \Theta(1)$, the answer is simple; we need to check whether
   $r=1$ is a root of the characteristic equation, i.e., whether $p=1$.
   With $p=1$ we have an inhomogeneous solution $f_{\text{inh}}(k) = \Theta(k)$
   and thus $t_{\text{inh}}(n) = \Theta(\log n)$; with $p>1$, the inhomogeneous
   solution is just a constant.
   In either case, the overall solution is dominated by the homogeneous solution
   and we can write $t(n) = \Theta(n^{\log_{q} p})$.
  \item
   With $g(n) = \Theta(\log n)$, we get, in terms of $k$, $g(k) = \Theta(k)$.
   Again, we need to distinguish two cases.  With $p=1$, the particular
   solution becomes $\Theta(k^{2})$ and thus $\Theta(\log^{2} n)$;
   with $p>1$, the particular solution remains $\Theta(k)$ and thus
   $\Theta(\log n)$.  In both cases, the homogeneous solution once again
   dominates, so we can write $t(n) = \Theta(n^{\log_{q} p})$.
  \item
   With $g(n) = \Theta(n)$, we get, in terms of $k$, $g(k) = \Theta(q^{k})$.
   Now the radix of the driving function is $q$, not 1, so we must distinguish
   the cases $p=q$ and $p \not= q$.  With $p=q$, the particular solution
   becomes $\Theta(k\cdot q^{k})$ and thus $\Theta(n\log n)$; with $p \not= q$,
   the particular solution remains $\Theta(q^{k})$ and thus $\Theta(n)$.
   Now we need to be careful when comparing the homogeneous solution and
   the particular solution: either can dominate, depending on the relative
   values of $p$ and $q$.  With $p < q$, the homogeneous term is sublinear,
   so the particular solution dominates and we have $t(n) = \Theta(n)$;
   with $p=q$, the homogeneous solution is linear, so again the particular
   solution dominates and we have $t(n) = \Theta(n\log n)$; finally,
   with $p > q$, the homogeneous solution is a polynomial of (fractional)
   degree larger than 1 and so dominates the particular solution, so that
   we have $t(n) = \Theta(n^{\log_{q} p})$.
  \item
   With $g(n) = \Theta(n\log n)$, we get, in terms of $k$,
   $g(k) = \Theta(k\cdot q^{k})$, an exponential of radix $q$ times
   a polynomial of degree~1.  Once again, we must distinguish two cases
   to obtain the form of the particular solution.  With $p=q$, the
   particular solution is $\Theta(k^{2}\cdot q^{k})$ and thus
   $\Theta(n\log^{2} n)$; with $p\not= q$, the particular solution
   is just $\Theta(k\cdot q^{k})$ and thus $\Theta(n\log n)$.
   Using the same three cases as above, we get:
   with $p<q$, the particular solution dominates and so $t(n)=\Theta(n\log n)$;
   with $p=1$, the particular solution again dominates and so
   $t(n) = \Theta(n\log^{2} n)$; and with $p>q$, the homogeneous solution
   dominates and so $t(n) = \Theta(n^{\log_{q} p})$.
  \item
   With $g(n) = \Theta(n^i)$, we get, in terms of $k$,
   $g(k) = \Theta((q^k)^i) = \Theta(q^{ki}) = \Theta((q^i)^k)$,
   that is, an exponential of radix $q^i$.  We must once again
   test $p$ against the radix.  With $p=q^i$, the particular solution
   is $\Theta(k\cdot (q^i)^k)$ and thus $\Theta(n^i \log n)$;
   with $p\not = q^i$, the particular solution is just $\Theta(n^i)$.
   Again, we have three cases: with $\log_q p < i$ (or. equivalently,
   $p<q^i$), the particular solution dominates and we have
   $t(n) = \Theta(n^i)$; with $p=q^i$, the particular solution again
   dominates and we have $t(n) = \Theta(n^i \log n)$; finally,
   with $p>q^i$, the homogeneous solution dominates and we have
   $t(n) = \Theta(n^{\log_{q} p})$.
  \item
   With $g(n) = \Theta(a^n)$, the particular solution will always be
   an exponential of radix $a$ times some polynomial---and so it
   will always dominate the homogeneous solution.
   We thus have only two cases: the radix equals $p$ or it does not;
   if it does not, the answer it $t(n) = \Theta(a^n)$,
   otherwise it is $t(n) = \Theta(a^n \log n)$.
  \item
   With $g(n) = \Theta(n\cdot a^n)$, the previous analysis can be repeated,
   but now we have an extra degree to the multiplying polynomial.
\end{itemize}

\bigskip
\emph{Problem 2.}   
The sieve must go through every 2nd number, then every 3rd, then
every 4th, all the way to every $k$th, where $k$ is the size of the remaining
collection of numbers.  The first pass eliminates one half of the numbers;
the second pass eliminates a third of what is left; and so forth.
So the size of the remaining set after pass $i+1$ is just
  $$s(i+1) = \frac{i\cdot s(i)}{(i+1)}$$
which we can rewrite as
  $$(i+1)\cdot s(i+1) - i\cdot s(i) = 0$$
with initial condition $s(1) = n$.  Since equality holds, we have,
after $i+1$ passes
  $$(i+1)\cdot s(i+1) - 1\cdot s(1) = 0$$
with solution $s(i) = n/i$

Now, the first implementation has no fast way of removing the $i$th
element during pass $i$---instead, it just searches for an entry
in the array, which takes linear time every time. since the array
never gets smaller.  Thus the loop time is simply the size of the
original array, $n$.  The number of passes is limited by the
constant increase in the step size: there will come a point where
the step size exceeds the number of elements remaining in the array,
i.e., where we have $k\geq s(k)$, that is $k\geq n/k$ or, solving,
$k\geq \sqrt{n}$.  Thus the overall running time is
$\sum_{i=1}^{\sqrt{n}} n$, which is $\Theta(n\sqrt{n})$.

The second implementation searches with the help of a linked list,
whose length equals the number of surviving elements.
The search is only to the current length
of the linked list, so we have, for our $n$ loops,
$\sum_{i=1}^{\sqrt{n}} s(i) = n\sum_{i=1}^{\sqrt{n}} \frac1i$
which is $\Theta(n\log n)$.  (The sum $\sum_{i=1}^k \frac1i$
is the $k$th harmonic number, $H_k$, and is roughly equal to the
natural logarithm of $k$---see p. 89 in the text; but the logarithm
of $\sqrt{n}$ is just half the logarithm of $n$, so it does not matter
whether the sum is taken to $\sqrt{n}$ or to $n$.)

Thus the second implementation is \emph{much} faster.
\end{document}
