\documentclass{article}
\usepackage{techexpl}
\input{control}


\begin{document}

\thispagestyle{empty}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{center}
\vspace{1.0in}
\Large\bf
Data Structures and Algorithms ---   CSCI 230 \\
Discrete Mathematics Overview --- Part B
\end{center}


\hdr{Proof by Induction}

\privatenotes{These notes are more thorough than most will be because
the notes in the text are so poor.}

\begin{itemize}

\item Mathematical induction is used to prove statements true for all
integers greater than or equal a certain base value $n_0$.  Here are
three example statements we will prove in class using mathematical
induction:
\begin{itemize}
\item $\displaystyle \sum_{i=0}^n a^i = \frac{a^{n+1} - 1}{a - 1}
\qquad $ for $n \geq 0$ and $a\neq 1$.
\item $n! < n^n$, for $n \geq 2$.
\item A complete binary tree of height $h \geq 0$ contains exactly
$2^{h+1} - 1$ nodes.
\end{itemize}

\overnew
\item An inductive proof always has three main components:
  \begin{description}
  \item[Basis case:]  Prove the statement is true for $n=n_0$ (and
  perhaps $n=n_0+1$, etc.).
  \item[Inductive hypothesis:]  Assume the statement is true for all
  $k$, $n_0 \leq k < n$.  
  \item[Induction step:]  Prove that for all $n \geq n_0$, the
  inductive hypothesis implies that the statement is true for $n$.  
  \end{description}

\item The principle of mathematical induction states that if the basis
case and induction step are both proved, then the statement holds for
all $n \geq n_0$.

\item Notes:
  \begin{itemize}
  \item At first proofs by induction appear to be circular, but in
  fact they are not.  Instead, think of a set of dominoes standing on
  end: the basis case is the act of pushing over the first domino
  (or two);  the induction step is the relationship between the
  positions of the dominoes showing that for every domino, if its
  precedecessors fall over, it will fall over as well.
  \item Sometimes the inductive hypothesis is written $n_0 \leq k \leq
  n$ and the induction step proves that the inductive hypothesis shows
  the statement to be true for $n+1$.
\overnew
  \item At other times, the inductive step is to show for all $n\geq
   n_0$, if the statement is true for $n$ then it is true for $n+1$.
   Students more comfortable with this method of inductive proof 
   are welcome to use it here.
  \item Very often the inductive hypothesis is not written out explicitly.
  \end{itemize}

\end{itemize}
Let's see how the three examples mentioned above are proved
using mathematical induction.

\newpage
\begin{enumerate}

\item $\displaystyle \sum_{i=0}^n a^i = \frac{a^{n+1} - 1}{a - 1}
\qquad $ for $n \geq 0$ and $a\neq 1$.

\begin{description}

\item[Basis case:]  When $n=0$, the summation reduces to $a^0 = 1$,
and the right side of the equation is $( a^{0+1} - 1 ) / (a - 1) =
1$.  Since these are both 1, the basis case is proved.

\item[Induction step:]  For any given $n\geq 1$, if the statement is true
for $0\leq k < n$, then
\begin{eqnarray*}
 \sum_{i=0}^n a^i &= \sum_{i=0}^{n-1} a^i \;+\; a^n \\
    &= \frac{a^{(n-1)+1} - 1}{a - 1} \;+\; a^n  \qquad
	 \text{by the induction hypothesis} \\
    &= \frac{a^{n} - 1}{a - 1} \;+\; a^n \frac{a - 1}{a - 1} \\
    &= \frac{ a^n - 1 + a^{n+1} - a^n}{ a - 1 } \\
    &= \frac{ a^{n+1} - 1}{ a - 1 }
\end{eqnarray*}
completing the induction step.
\end{description}

\item  $n! < n^n$, for $n \geq 2$.

\begin{description}
\item[Basis case:] For $n=2$, the left side of the inequality
evaluates to 2 and the right side evaluates 4.  Since $2<4$, the
basis case is established.
\item[Induction step:] For $n>2$, if $k! < k^k$ for $2 \leq
k < n$ then in particular
\[ (n-1)! < (n-1)^{n-1}. \]
Multiplying both sides of this inequality by $n$ shows that
\[
   n! <  n \cdot (n-1)^{n-1}. \]
But, since $n-1 < n$ (obviously), 
\[
   n! <  n \cdot (n-1)^{n-1} < n \cdot n^{n-1}. \]
So,
\[ n! < n^n. \]
\end{description}


\item A complete binary tree of height $h \geq 0$ contains exactly
$2^{h+1} - 1$ nodes.

\begin{description}
\item[Basis case:]  Any binary tree of height $h=0$ is complete and
has exactly one node.  Since $2^{0+1} - 1 = 2-1 = 1$, the basis case
is established.
\item[Induction step:] For $h>0$, assume any complete binary tree of
height $k$, $0 \leq k \leq h-1$, contains $2^{k+1} - 1$ nodes, and
consider any complete binary tree, $T$, of height $h$.  Removing the
root of $T$ creates two complete binary trees of height $h-1$, one
formed from the left subtree of $T$ and the other formed from the
right.  By the inductive hypothesis each of these has $2^{(h-1)+1} - 1
= 2^h -1$ nodes.  Therefore, $T$ had
\[
   (2^h -1) + (2^h-1) + 1
\]
nodes to start with (the 1 at the end is for the root node).  Adding
these values shows that $T$ had
\[
  2^{h+1} - 1
\]
nodes, which completes the proof.
\end{description}

\end{enumerate}

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

\hdr{Class Exercises on Induction}

\begin{enumerate}

\item Prove each of the following by mathematical induction.
 \begin{enumerate}
 \item  $\displaystyle \sum_{i=1}^n (2i-1) = n^2$ for $n \geq 1$.

  Proof:

  \basis{For $n = 1$, the left hand side is $2 \times 1 - 1 = 1$
and the right hand side is $1^2 = 1$, so the basis case is
established.}

  \istep{Assume $\sum_{i=1}^n (2i-1) = n^2$ for some $n \geq 1$.
We show that $\sum_{i=1}^{n+1} (2i-1) = (n+1)^2$, as follows:
\begin{eqnarray*}
\sum_{i=1}^{n+1} (2i-1) &= & \sum_{i=1}^n (2i-1) + 2(n+1) - 1\\
    & = & \sum_{i=1}^n (2i-1) + 2n + 1\\
    & = & n^2  + 2n + 1  ~~~\mbox{by the induction hypothesis}\\
    & = & (n+ 1)^2.\\
     \end{eqnarray*}
}

 \item $n < 2^n$ for $n \geq 0$.

  Proof:

  \basis{For $n = 0$, we have $0 < 1 = 2^0$, which establishes the basis case.}

  \istep{Assume $n < 2^n$ for some $n \geq 0$, we attempt to show that
$n + 1 < 2^{n+1}$:
\begin{eqnarray*}
2^{n+1} & = & 2 \times 2^n \\
        & > & 2n  \byinduction 
     \end{eqnarray*}
Hmmm, we need to know this is $\geq n + 1$, but in fact $2n \not{\geq}
n+1$ when $n = 0$.  What to do?

 \answer{We must go back and prove another basis case, for $n = 1$.

    \basis{For $n = 1$, we have $1 < 2 = 2^1$, which establishes the second
    basis case.  Next we have to restate and prove the induction step:}

    \istep{Now assume $n < 2^n$ for some $n \geq 1$; we show that
$n + 1 < 2^{n+1}$:
\begin{eqnarray*}
2^{n+1} & = & 2 \times 2^n \\
        & > & 2n \byinduction \\
        & \geq & n + 1 ~~~\mbox{since $n \geq 1$}. 
     \end{eqnarray*}
}}}



 \item Any set containing $n$ elements has $2^n$ different possible
 subsets.  

  \hint{In the inductive step, consider a particular
 element $a$ of the set and count all the subsets that do contain $a$ and
 all those that do not.}

  \basis{For $n = 0$, we have a set of $0$ elements; i.e., the empty set,
which has $1$ subset, itself.  Since $1 = 2^0$, this establishes the
basis case.}

  \istep{Let $S$ be a set with $n > 0$ elements.  Let $a$ be 
a particular element of $S$.  Let $\cal T$ be the set of all
subsets of $S$ and $\cal T_a$ be the set of all
subsets of $S - \{a\}$.  By the induction hypothesis,
$|T_a| = 2^{n-1}$.  Every set in $\cal T_a$ is also a subset 
of $S$, so $\cal T$ has at least $2^{n-1}$ elements. 
For every set $U$ in $\cal T_a$, we also have $U \cup \{a\} \subseteq S$,
which gives $2^{n-1}$ more subsets of $S$ in  $\cal T$, for a total
of $2^{n-1} + 2^{n-1} = 2^n$.}

 \end{enumerate}

\overnew

\item What is wrong with the following inductive proof showing that
all horses are the same color?  (Or, more specifically, in any set $H$
of $n$ horses, each horse in $H$ has the same color as every other
horse in $H$.)

\begin{description}
\item[Basis:] $n=1$.  One horse is trivially the same color as itself.

\overnew
\item[Induction Step:]  We have to prove for each $n \geq 2$ that if in
all sets containing fewer than $n$ horses all horses are the same
color, then in any set of $n$ horses all horses are the same color.
Let $H$ be any set containing $n$ horses, and label the horses $h_1,
h_2, \ldots, h_{n}$.  Let $H_1 = H - \{ h_{n} \}$ and let $H_2 = H -
\{ h_{1} \}$.  By the inductive hypothesis, since $|H_1| = n-1$, all
horses in $H_1$ are the same color; call it $c_1$.  Also by the
inductive hypothesis, since $|H_2| = n-1$, all horses in $H_2$ are the
same color; call it $c_2$.  Also, since $H_1 \cap H_2 = \{ h_2,
\ldots, h_{n-1} \}$, horses $h_2, \ldots, h_{n-1} $ are each both
colors $c_1$ and $c_2$. Hence, $c_1=c_2$.  Therefore, all horses in
$H$ are the same color.
\end{description}

\end{enumerate}


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

\hdr{Recursion}

Here is an outline of five steps I find useful in writing and
debugging recursive functions:

\overnew
\begin{enumerate}
\item
Handle the base case(s) first, at the start of the function.

\item
Define the problem solution in terms of smaller instances of the
problem.  This defines the necessary recursive calls.

\item
Figure out what work needs to be done before making the recursive
call(s).

\item
Figure out what work needs to be done after the recursive call(s)
complete(s) to finish the solution.

\item
Assume the recursive calls work correctly, but make sure they are
progressing toward the base case(s)!
\end{enumerate}

Here are two example recursive functions: factorial and mergesort.

\begin{verbatim}
    int Fact(int n)
    {
      if ( n == 0 || n == 1 ) 
        return 1;
      else
        return n * Fact(n-1);
    }
\end{verbatim}

\overnew

\begin{verbatim}
    template <class T>
    void MergeSort(T * pts, int n)
    {
      MergeSort(pts, 0, n-1);
    }

    templage <class T>
    void MergeSort( T * pts, int low, int high )
    {
      if ( low == high ) return;

      int mid = (low + high) / 2;
      MergeSort(T, low, mid);
      MergeSort(T, mid+1, high);

      //  At this point the lower and upper halves
      //  of "pts" are sorted. All that remains is
      //  to merge them into a single sorted list.
      //  We will discuss the details of this later.
    }
\end{verbatim}


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

\hdr{Class Exercises on Recursion}
\begin{enumerate}
%%  \item
%%  Write code to complete the merge step.  This code should follow the
%%  comments in the main \verb$MergeSort$ function.

\item Assume the following structure for a node in a binary tree:
\begin{verbatim}
    struct TreeNode {
      int value;
      TreeNode * left_child; 
      TreeNode * right_child;
    };
\end{verbatim}
Write a recursive function to count the number of nodes in the binary
tree whose root is pointed to by \verb$T$.  Note the structural
similarity between your solution and the inductive proof about
complete binary trees.

\overnew
\item
The following is a recursive version of binary search.  Will it work
correctly?  Why or why not?
\begin{verbatim}
    template <class T>
    bool RBinSearch(T * pts, int low, int high, 
                   T point, int& loc)
    {
      if (high == low) {
        loc = low;
        return pts[loc] == point;
      }
      int mid = (low + high) / 2;
      if (pts[mid] <= point) 
        return RBinSearch(pts, mid, high, point, loc);
      else
        return RBinSearch(pts, low, mid-1, point, loc);
    }
\end{verbatim}

\overnew
\item
The Fibonacci numbers are defined recursively as
\begin{eqnarray*}
f_0 &= 0 \\
f_1 &= 1 \\
f_n &= f_{n-1} + f_{n-2}, \qquad n \geq 2
\end{eqnarray*}
(The notation $f_n$ means the same thing as $f(n)$, i.e.\ $f$ is a
function of $n$.)
\begin{enumerate}
\item Write a recursive function to calculate $f_n$.

\item How many recursive calls will your function make to calculate
$f_3$, $f_4$, $f_5$, etc?  Can you write a recursive expression
similar to the recursive formula for the Fibonacci numbers to count
the number of recursive calls?  

\privatenotes{Show the implementation of this, and evaluate up to
$n=35$.  A smarter, non-recursive version, takes less than a
millisecond.  Be sure to discuss the implications and emphasize that
this doesn't make recursion a bad thing in general.}

\end{enumerate}

\end{enumerate}

\hdr{Review Problems}

Here are a few review problems which have appeared on homeworks or
tests in previous semesters.  Practice writing solutions carefully and
then compare to solutions provided on-line.  If you can solve these
problems and the problems we worked on in class then you are ready for
the chapter quiz!

\begin{enumerate}

\item Give an inductive proof showing that for all integers $n \geq 5$,
$4 n + 4 < n^2$.

\item  Give an inductive proof showing that for
all positive integers $n$,
\[
\sum_{i=1}^n \frac{1}{(2i-1)(2i+1)} = \frac{n}{2n+1}.
\]
\item Give an inductive proof showing that for
all $n \geq 1$,
\[
\sum_{i=1}^n i(i!) = (n+1)! - 1
\]

\item Evaluate the following summations.

\begin{enumerate}
  \item $\displaystyle \sum_{i=0}^{100} (-1)^i$
  \item $\displaystyle \sum_{i=5}^{2n} i$

  \item $\displaystyle \sum_{i=0}^n ( a^i - 2 i + n ) $

  \item $\displaystyle \sum_{i=0}^{n-1}\bigl( c + \sum_{j=0}^{i-1} (j+2) \bigr)$
\end{enumerate}
  
\item Prove using mathematical induction that $f_n >
(3/2)^n$, for $n \geq 5$.  Here, $f_n$ is the $n$th Fibonacci
number. Use the definition of the Fibonacci numbers given in the text
on page~6.  Study the example inductive proof on page~6 carefully.

\item Write the code necessary to accomplish the merging step in
\verb$MergeSort$.  This code should follow the comments in the main
\verb$MergeSort$ function.

\end{enumerate}

\end{document}
