\documentstyle{article}
\newbox\myanbox\setbox\myanbox\hbox{$a_n$}%
\begin{document}
\hbox to\textwidth{\large\bf CS 463\hfil Fall 1995\hfil Prof. Bernard Moret}

\bigskip
\begin{center}
  \large\it Solved Examples of (Hard) Recurrences
\end{center}

\noindent
{\bf Exercise 2.20.}
  Solve the following recurrences using various methods.
  ({\em Hints\/}: The third recurrence needs two successive
  substitutions---the first obvious, the second involving logarithms;
  treat the solution to the fourth recurrence as a fraction, replacing $a_n$
  with the fraction $b_n/c_n$, and solve for $b_n$ and $c_n$ separately;
  to solve the fifth recurrence, generate several terms.)
  \begin{enumerate}
    \item
      $\displaystyle\cases{a_n=3a_{n/2}-2a_{n/4}+\lg n,& for $n$ a power of 2\cr
			   \hbox to\wd\myanbox{\hfil$a_2$\hfil}=3 & \cr
			   \hbox to\wd\myanbox{\hfil$a_1$\hfil}=3 & \cr}$

      As indicated, this function is only defined for $n$ a power of~2.
      Thus we can set $n=2^k$ and substitute $g(k)$ for $f(n)$, obtaining
      the new recurrence $g(k)=3g(k-1)-2g(k-2)+k$.  The characteristic
      roots are 1 and 2, so that the homogeneous solution has the form
      $g_h(k)=\alpha+\beta 2^k$; the driving function is a polynomial of
      degree~1, so that the particular solution has the form of a polynomial
      of degree~2 (because of the characteristic root of value~1), say
      $ak^2+bk$ (the term of degree 0 is covered in the homogeneous solution
      already).  On substituting this in the recurrence, we get
      $ak^2+bk=3a(k-1)^2+3b(k-1)-2a(k-2)^2-2b(k-2)+k$, or
      $(2a+1)k+(b-5a)=0$, from which we get $a=-\frac12$ and $b=-\frac52$.
      Hence our general solution is of the form
      $g(k) = \alpha - \frac52 k - \frac12 k^2 + \beta 2^k$; using the
      initial conditions $f(1)=g(0)=3$ and $f(2)=g(1)=3$, we can solve
      for the two constants:
      $g(0)=\alpha+\beta=3$ and $g(1)=\alpha-3+2\beta=3$, yielding
      the system
      $\displaystyle\cases{\alpha+\beta=3& \cr
			   \alpha+2\beta=6& \cr}$
      and thus the solution $\alpha=0$ and $\beta=3$.  Hence we have
      $g(k)=-\frac52 k -\frac 12 k^2 +3\cdot 2^k$.  Converting back to $f(n)$,
      we get the desired answer, $f(n)=-\frac52 \log n -\frac12 \log^2 n +3n$.
    \item
      $\displaystyle\cases{a_n = a_{n-1} {\cdot} {a_{n-2}}^2 & \cr
			   \hbox to\wd\myanbox{\hfil$a_1$\hfil}=2 & \cr
			   \hbox to\wd\myanbox{\hfil$a_0$\hfil}=4 & \cr}$

      We clearly need a substitution of variables here.  What we need is
      a substitution that will turn this product of terms into a sum;
      since the product of two exponentials is an exponential with the
      sum of the coefficients, we can try to substitute $f(n)=2^{g(n)}$.
      Upon substitution, we get the recurrence
      $2^{g(n)}=2^{g(n-1)}\cdot {2^{g(n-2)}}^2=2^{g(n-1)+2g(n-2)}$.
      Taking the logarithm on both sides, we obtain the recurrence
      $g(n)=g(n-1)+2g(n-2)$, which is a homogeneous linear recurrence with
      constant coefficients, with general solution of the form
      $\alpha (-1)^n + \beta 2^n$.  Since the initial conditions are
      $f(0)=2^{g(0)}=4$ and $f(1)=2^{g(1)}=2$, we have $g(0)=2$ and $g(1)=1$,
      and thus $\alpha=\beta=1$.  Hence $g(n)=2^n+ (-1)^n$ and thus
      $f(n)=2^{2^n+(-1)^n}=2^{2^n}\cdot 2^{(-1)^n}$; since the second factor
      is either $\frac12$ (for $n$ odd) or 2 (for $n$ even), we can write
      $$f(n)=\cases{2^{2^n+1}& for $n$ even\cr
		    2^{2^n-1}& for $n$ odd\cr}$$
      which is the desired answer.
    \item
      $\displaystyle\cases{a_n = n a^2_{n/2}, & for $n$ a power of 2 \cr
			   \hbox to\wd\myanbox{\hfil$a_1$\hfil}=8 & \cr}$

      Our first step is to write $g(k)=f(2^k)$, obtaining the recurrence
      $g(k)=2^k\cdot g^2(k-1)$; since this remains in an unusable form,
      we use one more transformation as suggested in the exercise, by taking
      logarithms on both sides, obtaining $\log g(k)=k+2\log g(k-1)$
      and letting $h(k)=\log g(k)$, to obtain $h(k)=2h(k-1)+k$,
      with initial condition $h(0)=\log 8=3$.
      This is an easy recurrence to solve (we skip the details); we get
      $h(k)=5\cdot 2^k-k-2$.  Hence $g(k)=2^{h(k)}=2^{5\cdot 2^k-k-2}$.
      In terms of $n$, we thus get
      $$f(n)=2^{5n-\log n-2}={2^{5n}\over 2^{2+\log n}} = {(2^5)^n\over2^2\cdot 2^{\log n}} ={32^n\over 4n},$$
      as desired.
    \item
      $\displaystyle\cases{\displaystyle a_n = {2\over 3-a_{n-1}} & \cr
			   \displaystyle\hbox to\wd\myanbox{\hfil$a_0$\hfil}=1/2 & \cr}$

      Using the hint given in the exercise, we first rewrite the recurrence
      in terms of a fractional solution $f(n)/g(n)$:
      $${f(n)\over g(n)} = {2\over 3-{f(n-1)\over g(n-1)}} = {2g(n-1)\over 3g(n-1)-f(n-1)}$$
      Hence we get $f(n)=2g(n-1)$ and $g(n)=3g(n-1)-f(n-1)$; to get rid of
      the $f(\;)$ term in the recurrence for $g(\;)$, we substitute the first
      equation in the second, obtaining $g(n)=3g(n-1)-2g(n-2)$.  This
      we know how to solve; the general solution has the form
      $g(n)= \alpha +\beta 2^n$---and so we also have $f(n)=2\alpha+\beta 2^n$.
      Hence the general solution to the original equation has the form
      $$a_n = {2\alpha+\beta 2^n\over\alpha+\beta 2^n} = 1+{\alpha\over\alpha+\beta 2^n} = 1+{1\over 1+\gamma 2^n},$$
      where we have written $\gamma=\beta/\alpha$.  This last change restores
      a single unknown parameter, rather than the two we had earlier---which is
      crucial, since we only have one initial condition.  Using this condition,
      we obtain $\gamma=-3$ and thus can write the solution as
      $$a_n = 1+{1\over 1-3\cdot 2^n}.$$
    \item
      $\displaystyle\cases{\displaystyle a_n = {a_{n-1}+1\over a_{n-2}} & \cr
			   \hbox to\wd\myanbox{\hfil$a_1$\hfil}=1 & \cr
			   \hbox to\wd\myanbox{\hfil$a_1$\hfil}=2 & \cr}$
      
      Running the recurrence through the first few values, we obtain the
      values 1, 2, 3, 2, 1, 1, 2, 3, 2, 1, 1, 2, 3, \dots.  Thus the values
      repeat with a periodicity of 5, hence the solution $f(n)=f(n\bmod 5)$.
      To prove this result, we simply expand the recurrence by substitution:
      $$f(n)={f(n-1)+1\over f(n-2)} = {{f(n-2)+1\over f(n-3)}+1\over f(n-2)} = {1+f(n-2)+f(n-3)\over f(n-2)\cdot f(n-3)}$$
      and expand again and simplify
      $${}={1+f(n-3)+f(n-4)+f(n-3)\cdot f(n-4)\over f(n-3)\cdot(f(n-3)+1)}$$
      and one last time, to obtain
      $${}={f(n-5)\cdot\bigl(f(n-4)+f(n-5)+1+f(n-4)f(n-5)+f^2(n-4)+f(n-4)\bigr)\over(f(n-4)+1)\cdot(f(n-4)+f(n-5)+1)}=f(n-5)$$
      as desired.
\end{enumerate}

\noindent
{\bf Exercise 2.27.}
  Consider the recurrence
  \begin{displaymath}
    \cases{f(n) = (i+1)f(i) & \cr
	   f(1) = 1, & \cr}
  \end{displaymath}
  where $i$ is a uniformly distributed random integer between 1 and $n-1$,
  inclusive.  What are the possible values of $f(n)$, and what
  is its average value?

  The smallest value for $f(\;)$ occurs when the random variable $i$ takes
  the smallest value, i.e., when $i=1$, in which case we have $f(n)=2f(1)=2$.
  The largest value for $f(\;)$ occurs when the random variable $i$ takes
  the largest value, i.e., when $i=n-1$, in which case we have $f(n)=nf(n-1)$,
  which gives, using the initial condition, $f(n)=n!$.  Hence there is a huge
  range of values for $f(\;)$.

  Perhaps surprisingly, the average value is a very simple formula: $2^{n-1}$.
  Here is how we would go about deriving it.  Denote the average value of
  $f(n)$ by $g(n)$; since the value of $i$ is uniformly distributed between
  1 and~$n-1$, we can write
  $$g(n)={1\over n-1}\sum_{i=1}^{n-1}(i+1)g(i).$$
  While this is a valid recurrence, it does not have constant coefficients,
  nor does it have a constant number of terms.  Whenever faced with a sum
  of terms like this, the best strategy is to subtract a version of the sum
  (for a lower value of $n$) so as to cancel most terms.  Since each sum is
  divided by a unique factor, we premultiply each term in the subtraction
  to eliminate the divisor and compute the difference $(n-1)g(n)-(n-2)g(n-1)$;
  we get
  $$(n-1)g(n)-(n-2)g(n-1)=ng(n-1)$$
  or $(n-1)g(n)=(2n-2)g(n-1)$ or, finally, $g(n)=2g(n-1)$.
  It follows immediately that $g(n)$ is of the form $\alpha 2^n$;
  by using the initial condition $g(1)=f(1)=1$, we get $\alpha=\frac12$
  and thus the solution $g(n)=2^{n-1}$.
\end{document}
