\documentclass[11pt]{article}
\usepackage{macros-course-lectures}

\newtheorem{falsethm}[theorem]{False Theorem}
\renewcommand{\thepage}{\arabic{page}}

%\makeslides

\begin{document}

\onlystudents

\lecture{notes4}{Proof by Induction (cont'd) \& Structural Induction}{February 10, 2000}

\section{Tournaments}

A pure math example, interesting and slightly complicated (ordinary
and strong) inductive proofs. \\

An internet gaming company is conducting a chess tournament on the Internet. \\
Round robin, not elimination. \\
That is, everyone plays everyone exactly once. \\
Can represent the results nicely by arrows, e.g.:\\
\\
A beats B, C, D \\
B beats D \\
C beats D \\
D beats C \\
\\
Structure called a ``digraph''.  Will study them carefully soon.\\
\\
Now, the gaming company wants to use this information to make some
decisions, e.g., award first prize, rank everyone. \\
It wants to do this in such a way that its customers don't complain
about the rankings being unfair. \\
\\
There are some problems with doing this.

\subsection{Cycles}

Problem: The tournament graph can contain cycles. \\
Example: Above, B beats D beats C beats B. \\
\\
If there's a cycle, then any ranking method is a least a little
problematic:  someone who beat someone else would be ranked lower. \\
\\
Of course, the company has no power to avoid cycles in play
results. \\
The gaming company might hope that ``most of the time'', when there is
a cycle, it's very big (so the people involved aren't likely to
discover this). \\
However, tournaments have an unfortunate property: 
\begin{theorem}
Any tournament that contains any cycle, contains a cycle of length $3$
($3$-cycle).
\end{theorem}

That is, three players, e.g., B, C, D, above, have an ``inconsistent''
set of results. \\
\\
This can be proved by ordinary induction, where $P(n)$ is the
predicate ``Any tournament that contains an $n$-cycle contains a
$3$-cycle.''
%[[[Victor:  Slide]]]
\begin{tabbing}\\
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall n \geq 3 (P(n))$ \\
1. (Base) $P(3)$ 
\> \> \> \> \> Trivial by assumption. \\
2. (Inductive step)  $\forall n \geq 3 (P(n) \Rightarrow P(n+1))$ \\
\> 1. Fix $n \geq 3$ \\
\> 2. Assume $P(n)$ \\
\> 3. $P(n+1)$ \\
\> \> 1. Fix $T$, a tournament, and $p_1 \rightarrow p_2 \rightarrow \ldots
\rightarrow p_{n+1} \rightarrow p_1$, an $n+1$-cycle in $T$. \\
\> \> 2. $T$ contains a $3$-cycle. 
      \> \> \> ??? \\
\> \> 3. QED
      \> \> \> Implication, UG \\
\> 4. QED
   \> \> \> \> Implication, UG \\
3. QED
\end{tabbing}
How can we find a $3$-cycle in $T$? \\
Example: (Draw 5-cycle). \\
To get a 3 cycle, notice that $p_1$ and $p_3$ have an arrow, one way
or the other.  \\
If from $p_3$ to $p_1$, then $p_1,p_2,p_3$ form a cycle.\\
If the other way, then can reduce the size of the cycle to omit $p_2$.
Then use induction on the smaller cycle. \\
This generalizes: 
%[[[Victor:  Slide]]]
\begin{tabbing}\\
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
\> \> 2. $T$ contains a $3$-cycle. \\
\> \> \> 1. If $p_3 \rightarrow p_1$ then $T$ contains a $3$-cycle. 
      \> \> Use $p_1 \rightarrow p_2 \rightarrow p_3 \rightarrow p_1$. \\
\> \> \> 2. If $p_1 \rightarrow p_3$ then $T$ contains a $3$-cycle. \\
\> \> \> \> 1. Assume $p_1 \rightarrow p_3$. \\
\> \> \> \> 2. $T$ contains an $n$-cycle. 
            \> Use $p_1 \rightarrow p_3 \rightarrow p_4 \rightarrow p_{n+1} \rightarrow p_1$. \\
\> \> \> \> 3. $T$ contains a $3$ cycle.
            \> By inductive hypothesis (2.2) \\
\> \> \> 3. QED
         \> \> Cases
\end{tabbing}
A detail to be careful about here: 
This argument depends on the fact that the arrow we add between $p_1$ and
$p_3$ is not already part of the original cycle. \\
That's true because we know that the original cycle had length $n+1$,
which is at least $4$. 

\subsection{Locally fair rankings}

The gaming company decides that, although they cannot avoid inconsistent
orders, they would at least like to make the ranking ``locally fair'',
meaning that if one player $p$ is ranked immediately ahead of another
player $q$, then $p$ must have beaten $q$. \\
\\
(They might want to tell each player who the players were that are
ranked immediately above and below him, and don't want someone to
complain that the guy ranked above him is someone he beat.) \\
\\
They are happy to discover: \\
\\
Theorem: Any tournament has a locally fair ranking.\\
\\
Prove by strong induction, where $P(n)$ says ``any $n$-player
tournament has a locally fair ranking''. \\
The strategy is to ``split'' the players into those that beat and
those that lose to a particular player $x$. \\
Rank those two subsets separately, by using the inductive
hypothesis.\\
(Subtournaments) \\
Then put the rankings together. 
%[[[Victor:  Slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall n \geq 1 (P(n))$ \\
1. (Base) $P(1)$ 
\> \> \> \> \> Obvious---just one player to be ranked. \\
2. (Inductive step)
$\forall n \geq 1 [(\forall i, 1 \leq i \leq n (P(n))) \Rightarrow P(n+1)]$ \\
\> 1. Fix $n \geq 1$ \\
\> 2. Assume $\forall i, 1 \leq i \leq n (P(i))$. \\
\> 3. $P(n+1)$ \\
\> \> 1. Fix any $n+1$-player tournament $T$. \\
\> \> 2. $T$ has a locally fair ranking. \\
\> \> \> 1. Choose $x$ to be any player, $B$ the set of players that beat $x$, and \\
\> \> \> \> $L$ the set of players that lose to $x$. \\
\> \> \> 2. $B$ has a locally fair ranking. 
         \> \> Inductive assumption (2.2). \\
\> \> \> 3. $L$ has a locally fair ranking. 
         \> \> Inductive assumption (2.2). \\
\> \> \> 4. Define a ranking $R$ of all the players by: $B$ ranking, then $x$, then $L$ ranking. \\
\> \> \> 5. $R$ is a locally fair ranking for $T$. \\
\> \> \> 6. QED 
         \> \> EG \\
\> \> 3. QED 
      \> \> \> UG \\
\> 4. QED
   \> \> \> \> Implication, UG \\
3. QED                        
\> \> \> \> \> Strong induction 
\end{tabbing}

\subsection{Locally fair rankings and number of wins}

For a while, the customers are happy with just locally fair rankings. \\
But then a game comes up where local fairness doesn't work out so well: \\
\\
Example: \\
A beats B \\
B beats C \\
C beats A,D \\
D beats A,B,E \\
E beats A,B,C \\
(draw) \\
\\
Here, A,B,C,D,E is a locally fair ranking. \\
However, it's in the reverse order according to number of wins. \\
Bad. \\
Now it occurs to the company that it would be nice if the rankings could
also be consistent with number of wins. \\
\\
But can't get this (together with local fairness). \\
\\
Example: 6 players \\
A beats C,D,E,F (4 wins) \\
B beats A,E,F (3 wins) \\
C beats B,F (2 wins) \\
D beats C,B (2 wins) \\
E beats C,D (2 wins) \\
F beats D,E (2 wins) \\
\\
(Draw)
Ordering consistently with number of wins would mean $A$ would have to
be ranked immediately ahead of $B$. \\
But $B$ beats $A$, violating local fairness. \\\
\\
So then they decide they will be happy to settle for a fair ranking in
which the highest ranked player has the largest number of wins (can be
tied). \\
This would rule out the bad example above. \\
\\
Turns out this is possible: \\
In fact, in the event of a tie, the company has the option of choosing
which best player is ranked first: \\
\\
Define $P(n)$: For any $n$-player tournament $T$ and any $z$ that has
the largest number of wins in $T$, there exists a locally fair ranking
with $z$ ranked first. 
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall n \geq 1 (P(n))$ 
\end{tabbing}

This can be proved by induction, but the inductive step is more
complicated than in the previous proof. \\
\\
First proof attempt: \\
Try a proof like the previous one, this time splitting on some $x$
that loses to $z$. \\
Now split all-but-$x$ into $B$ and $L$ as before, based on their
records w.r.t. $x$. \\
We know that $z$ is in $B$, because it beats $x$. \\
Now try to use the inductive hypothesis to rank $B$ while keeping $z$
first, and to rank $L$. \\
Then combine as before. \\
\\
Sounds plausible---but wrong: \\
Applying the inductive hypothesis requires that $z$ have the best
won-lost record in the ``subtournament'' involving just the $B$
elements. \\
But it might not: \\
\\
Example: \\
Players A, B, x, and a lot of p's \\
A and B beat x \\
x beats all of the p's \\
B beats A \\
A beats all the p's \\
all the p's beat B \\
Doesn't matter what happens among the p's. \\
\\
(diagram) \\
\\
A has the best record overall, but in the two-player subtournament, it
is worse than B. \\
\\
Try again: \\
It turns out that a correct proof can be given using ordinary (not
strong) induction: \\
Recall $P(n)$: For any $n$-player tournament $T$ and any $z$ that has
the largest number of wins in $T$, there exists a locally fair ranking
with $z$ ranked first. 
%[[[Victor:  Slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall n \geq 1 (P(n))$ \\
1. (Base) $P(1)$ 
\> \> \> \> \> Only one possibility. \\
2. (Inductive step) $\forall n \geq 1 [P(n) \Rightarrow P(n+1)]$ \\
\> 1. Fix $n \geq 1$. \\
\> 2. Assume $P(n)$. \\
\> 3. $P(n+1)$. \\
\> \> 1. Fix $T$, an $n+1$-player tournament, and $z$, a player with the most wins. \\
\> \> 2. $\exists$ a locally fair ranking of $T$ with $z$ first. 
      \> \> \> ??? \\
\> \> 3. QED 
      \> \> \> UG \\
\> 4. QED   
   \> \> \> \> Implication, UG \\
3. QED                        
\> \> \> \> \> Induction 
\end{tabbing}
The key step will break down into cases, based on whether or not $z$
lost any matches.  \\
The case where $z$ won everything is easy; the other case is tricky:
Here's the easy case:
%[[[Victor:  Slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
\> \> 2. $\exists$ a locally fair ranking of $T$ with $z$ first. \\
\> \> \> 1. If $z$ is undefeated in $T$ then $\exists$ a locally fair
ranking of $T$ with $z$ first. \\
\> \> \> \> 1. Assume $z$ is undefeated in $T$. \\
\> \> \> \> 2. Choose $R$ to be a locally fair ranking of all-but-$z$. \\
\> \> \> \> \> Previous theorem says this exists, EI \\
\> \> \> \> 3. $z$ then $R$ is a locally fair ranking of $T$. \\
\> \> \> \> 4. $\exists$ a locally fair ranking of $T$ with $z$ first. \\
\> \> \> \> \> EG \\
\> \> \> \> 5. QED 
            \> Implication \\
\> \> \> 2. If $z$ is defeated by someone then $\exists$ a locally fair
ranking of $T$ with $z$ first. \\
\> \> \> \> \> ??? \\
\> \> \> 3. QED 
         \> \> Cases
\end{tabbing}

And the tricky case:
%[[[Victor:  Slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
\> \> \> 2. If $z$ is defeated by someone then $\exists$ a locally fair
ranking of $T$ with $z$ first. \\
\> \> \> \> 1. Assume $z$ is defeated by someone. \\
\> \> \> \> 2. Choose $x$ to be someone who defeated $z$. \\
\> \> \> \> 3. $z$ has the best record in the subtournament involving all-but-$x$.\\
\> \> \> \> \> $z$ had the best record in $T$, \\
\> \> \> \> \> no wins are removed. \\
\> \> \> \> 4. $\exists$ ranking of subtournament having $z$ first. \\
\> \> \> \> \> Inductive hypothesis, $z$ has best record. \\
\> \> \> \> 5. Choose $R$ to be a ranking of all-but-$x$ having $z$ first. \\
\> \> \> \> \> EI \\
\> \> \> \> 6. In $T$, $x$ is defeated by someone. 
            \> $z$ was defeated, and $x$'s record in $T$ can't \\
\> \> \> \> \> be better than $z$'s. \\
\> \> \> \> 7. Define ranking $S$ to be $R$, with $x$ inserted right after the lowest-ranked player \\
\> \> \> \> \> that beats $x$. \\
\> \> \> \> 8. $S$ is a locally fair ranking of $T$. \\
\> \> \> \> \> $R$ is locally fair, $x$ is beaten by the player \\
\> \> \> \> \> ranked just above it and beats all ranked \\
\> \> \> \> \> below it. \\
\> \> \> \> 9. $z$ is first in $S$. 
            \> $z$ is first in $R$, and $x$ is not inserted \\
\> \> \> \> \> at the top. \\
\> \> \> \> 10. $\exists$ a locally fair ranking of $T$ with $z$ first. \\
\> \> \> \> \> $S$ works, EG \\
\> \> \> \> QED 
            \> Implication \\
\end{tabbing}

\section{Induction, Strong Induction, and Well-Ordering}

\subsection{Induction vs. strong induction}

Strong induction looks like a more powerful proof method than ordinary
induction, because it allows more information to be assumed in the
inductive step. \\
But it's not really stronger---anything that can be proved with strong
induction can also be proved with ordinary induction. \\
\\
Example: \\
Recall the proof that every natural number $\geq 2$ can be written as
product of primes (factored into primes). \\
Used strong induction, where $P(n)$ was the predicate ``$n$ can be
factored into primes''. \\
The inductive step involved writing $n+1 = a b$, and using $P(a)$ and
$P(b)$ to get factorizations for $a$ and $b$, then multiplying
them. \\
\\
Now change this to an ordinary inductive proof: \\
Define a new predicate $Q(n)$ to be $\forall i, 2 \leq i \leq n$
($i$ can be factored). \\
Prove $Q(n)$ by ordinary induction: \\
\\
$Q(2)$:  Same as $P(2)$, use same proof. \\
\\
$Q(n) \Rightarrow Q(n+1)$: \\
Our goal is now to show: $\forall i, 2 \leq i \leq n+1$ ($i$ can be
factored). \\
What we can assume is: $\forall i, 2 \leq i \leq n$ ($i$ can be factored). \\
\\
Notice that all the needs to be shown is that $n+1$ can be written
(the others already assumed). \\
And we can assume factorizations of all smaller numbers. \\
So the old strong induction step can be used to show this. \\
\\
\\
This idea is quite general: \\
\\
In general, can convert a strong inductive proof of $\forall n \geq k
(P(n))$ to an ordinary inductive proof. \\
Just define $Q(n)$ to be $\forall i, k \leq i \leq n (P(i))$, that is,
$P(k) \wedge P(k+1) \wedge \cdots \wedge P(n)$.\\
\\
Base:\\
Show $Q(k)$, same as $P(k)$, which has already been shown as
part of the strong induction proof. \\
\\
Inductive step:\\
Show $Q(n) \Rightarrow Q(n+1)$, in other words,
$P(k) \wedge P(k+1) \wedge \cdots \wedge P(n) \Rightarrow
 P(k) \wedge P(k+1) \wedge \cdots \wedge P(n) \wedge P(n+1)$. \\
\\
This really amounts to showing
$P(k) \wedge P(k+1) \wedge \cdots \wedge P(n) \Rightarrow P(n+1)$. \\
But this has already been shown in the strong induction proof. \\
\\
\\
Conversely:\\
Of course, any ordinary inductive proof can be done using strong
induction (pretty obvious because strong induction allows more to be
assumed). \\
\\
Induction and strong induction are said to be ``equivalent'' proof
rules, in that each can prove the same things (in the context of some
standard number axioms and logical deduction rules). \\

\subsection{Well-ordering}

There's another principle, usually given as an axiom, which can also
be shown to be ``equivalent'' to induction: \\
\\
Well-ordering: \\
Every nonempty subset $S$ of $N$ has a smallest element. \\
\\
Looks obvious.\\
Well, so do the induction principles.

Example:  Can reprove the theorem about $3$-cycles using well-ordering
instead of induction. \\
\\
Theorem: \\
Any tournament that contains a cycle contains a $3$-cycle.\\
\\
Proof: \\
Assume tournament $T$ contains a cycle. \\
Let $n$ be the smallest cycle length (there is some smallest, by
well-ordering). \\
$n$ must be at least $3$.  \\
If it's $3$, then we're done. \\
\\
Otherwise, it's at least $4$. \\
Now consider $p_1$ and $p_3$ as before. \\
Whichever way the arrow goes, we can use it in constructing a shorter
cycle in $T$ (as we did before). \\
This is a contradiction, since we assumed that $n$ is the smallest
cycle length.\\
\\
\\
This proof is really very similar to the other one. \\
In general, well-ordering is equivalent to induction (and strong
induction), in that they can be used to prove the same theorems (in
the context of...).

\section{Structural induction}

A variant of induction for proving things about other data types
besides $N$, in particular, about data types that are defined recursively. \\
The induction is based explicitly on the recursive structure of the
definition.

\subsection{Some recursive definitions for data types}

Fully parenthesized Boolean algebra expressions (with ands, ors,
nots only). \\
\\
The set $F$ of f.p. BA expressions satisfies: 
\begin{enumerate}
\item The symbols $0$ and $1$ are in $F$. 
\item If $e$ and $e'$ are in $F$ then the expression $(e \wedge e')$ is
in $F$. 
\item
If $e$ and $e'$ are in $F$ then the expression $(e \vee e')$ is
in $F$. 
\item
If $e$ is in $F$ then the expression $(\neg e)$ is in $F$. 
\item
$F$ contains nothing else. 
\end{enumerate}

\noindent
Binary trees. \\
The set $T$ of binary trees satisfies: 
\begin{enumerate}
\item
A single node (just a dot) is in $T$.
\item 
If $t$ is in $T$, then the structure formed from $t$, a single
new node, and an arrow labeled ``left'' from the new node to
$t$, is also in $T$. 
\item
If $t$ is in $T$, then the structure formed from $t$, a single
new node, and an arrow labeled ``right'' from the new node to
$t$, is also in $T$. 
\item
If $t$ and $t'$ are in $T$, then the structure formed from $t$, $t'$,
and a single new node, with an arrow labeled ``left'' from the new
node to $t$ and another arrow labeled ``right'' from the new node to
$t'$, is also in $T$. 
\end{enumerate}
Since the descriptions above are wordy, it's nice to introduce some
notation, like makeleft(t) to denote the thing built up from $t$ in
the first recursive step. \\
Likewise, makeright(t), makeboth(t,t'). \\
\\
(diagram)
\\
\\
\noindent
Strings over an alphabet $A$. \\
The set $S$ of (finite length) strings over alphabet $A$ satisfies: 
\begin{enumerate}
\item  $\lambda$, the empty string, is in $S$. 
\item  If $s$ is in the set $S$ and $a \in A$, then $sa$ is in $S$. \\
\item $S$ contains nothing else. 
\end{enumerate}
\noindent
This is a recursive description (definition) of the set of strings. \\
Notice that it includes every (finite length) string. \\
Moreover, every string has a unique derivation using these rules. \\
Uniqueness is a requirement for a recursive definition. 


\subsection{Inductive proofs based on recursive data type definitions}

Structural induction is used to prove a property of all the elements
of some recursively-defined data type. \\
The proof consists of two steps: \\
Prove for the ``base cases'' of the definition. \\
Prove for the result of any combination rule, assuming that it
is true for all the parts.  \\

For Boolean expressions:
%[[[Victor: slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall e \in F (P(e))$ \\
1. (Base) $P(0)$ \\
2. (Base) $P(1)$ \\
3. (Inductive step) 
$\forall e, e' \in F (P(e) \wedge P(e') \Rightarrow P((e \wedge e')))$\\
4. (Inductive step) 
$\forall e, e' \in F (P(e) \wedge P(e') \Rightarrow P((e \vee e')))$ \\
5. (Inductive step) 
$\forall e \in F (P(e) \Rightarrow P((\neg e)))$\\
6. QED
\> \> \> \> \> Structural induction on Boolean expressions
\end{tabbing}

For binary trees:
%[[[Victor: slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall t \in T (P(t))$ \\
1. (Base) $P(n)$, where $n$ is the single-node tree. \\
2. (Inductive step) 
$\forall t \in T (P(t) \Rightarrow P(makeleft(t)))$ \\
3. (Inductive step) 
$\forall t \in T (P(t) \Rightarrow P(makeright(t)))$ \\
4. (Inductive step) 
$\forall t,t' \in T (P(t) \wedge P(t') \Rightarrow P(makeboth(t,t')))$ \\
5. QED
\> \> \> \> \> Structural induction on binary trees
\end{tabbing}

For strings over alphabet $A = \{ 0,1 \}$:
%[[[Victor: slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall s \in S (P(s))$ \\
1. (Base) $P(\lambda)$ \\
2. (Inductive step) $\forall s \in S (P(s) \Rightarrow P(s 0))$ \\
3. (Inductive step) $\forall s \in S (P(s) \Rightarrow P(s 1))$ \\
4. QED
\> \> \> \> \> Structural induction on strings.
\end{tabbing}
You can see how this would extend to different alphabets.

An example for each of the data types: 

\begin{theorem}
Every Boolean expression has the same number of left and right parentheses. 
\end{theorem}

Define $P(e)$: Expression $e$ has the same number of left and right
parens.
%[[[Victor: slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall e \in F (P(e))$ \\
1. (Base) $P(0)$ 
\> \> \> \> \> The expression $0$ has no parens. \\
2. (Base) $P(1)$ 
\> \> \> \> \> No parens \\
3. (Inductive step) 
$\forall e, e' \in F (P(e) \wedge P(e') \Rightarrow P((e \wedge e'))$\\
\> 1. Fix $e, e' \in F$. \\
\> 2. Assume $P(e) \wedge P(e')$, that is, each of $e$ and $e'$ has the
same number \\
\> \quad of left and right parens. \\
\> 3. $P((e \wedge e'))$, that is, the expression $e \wedge e'$ has the
same number of \\
\> \quad left and right parens. \\
\> \> \> \> \> By inductive hypothesis (3.2), and the fact \\ 
\> \> \> \> \> that we're adding one left and one right \\
\> \> \> \> \> paren. \\
\> 4. QED 
   \> \> \> \> Implication, UG \\
4. (Inductive step) 
$\forall e, e' \in F (P(e) \wedge P(e') \Rightarrow P((e \vee e'))$
\\
\> \> \> \> \> Similar to the previous case. \\
5. (Inductive step) 
$\forall e \in F (P(e) \Rightarrow P((\neg e))$
\> \> \> \> \> Similar to the previous case. \\
6. QED
\> \> \> \> \> Structural induction on Boolean expressions
\end{tabbing}
Be careful of confusion of parens above. \\
Some are used for $P$, some are parts of the actual Boolean
expressions...really needs better notation. 

\begin{theorem}
The number of edges in any binary tree is exactly one fewer than the
number of nodes. 
\end{theorem}

Define $P(t)$: Binary tree $t$ has exactly $1$ fewer edge than nodes.\\
Using notation $\ms{nodes}(t)$, $\ms{edges}(t)$. \\
The key is that the inductive steps all preserve the ``deficit'' of
1. \\
For instance, combining two trees involves adding two edges and one
node; since each subtree had a ``deficit'' of 1, this maintains the
deficit of 1:
%[[[Victor: slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall t \in T (P(t))$ \\
1. (Base) $P(n)$, where $n$ is the single-node tree. 
\> \> \> \> \> $0$ edges and $1$ node \\
2. (Inductive step) 
$\forall t \in T (P(t) \Rightarrow P(makeleft(t))$ \\
\> 1. Fix $t$. \\
\> 2. Assume $P(t)$, that is, $|\ms{edges}(t)|+1 = |\ms{nodes}(t)|$. \\
\> 3. $P(\ms{makeleft}(t))$, that is,
$|\ms{edges}(\ms{makeleft}(t))|+1 = |\ms{nodes}(\ms{makeleft}(t))|$. \\
\> \> 1. $|\ms{edges}(\ms{makeleft}(t))| = |\ms{edges}(t)| + 1$ \\
\> \> 2. $|\ms{nodes}(\ms{makeleft}(t))| = |\ms{nodes}(t)| + 1$. \\
\> \> 3. QED
      \> \> \> Algebra, using 2.2, 2.3.1, 2.3.2 \\
\> 4. QED
   \> \> \> \> Implication, UG \\
3. (Inductive step) 
$\forall t \in T (P(t) \Rightarrow P(makeright(t))$ 
\> \> \> \> \> Similar to the previous case. \\
4. (Inductive step) 
$\forall t, t' \in T (P(t) \wedge P(t') \Rightarrow P(makeboth(t,t'))$ \\
\> 1. Fix $t$. \\
\> 2. Assume $P(t) \wedge P(t')$, that is, $|\ms{edges}(t)|+1 =
|\ms{nodes}(t)|$ and $|\ms{edges}(t')|+1 =|\ms{nodes}(t')|$. \\
\> 3. $P(\ms{makeboth}(t,t'))$, that is,
$|\ms{edges}(\ms{makeboth}(t,t'))|+1 = |\ms{nodes}(\ms{makeboth}(t))|$. \\
\> \> 1. $|\ms{edges}(\ms{makeboth}(t,t'))| = |\ms{edges}(t)| +
|\ms{edges}(t')| + 2$ \\
\> \> 2. $|\ms{nodes}(\ms{makeboth}(t,t'))| = |\ms{nodes}(t)| +
|\ms{nodes}(t')| + 1$. \\
\> \> 3. QED
      \> \> \> Algebra, using 4.2, 4.3.1, and 4.3.2 \\
\> 4. QED
   \> \> \> \> Implication, UG \\
5. QED
\> \> \> \> \> Structural induction on binary trees
\end{tabbing}

\begin{theorem}
In a string of $0$s and $1$s, the number of occurrences of the pattern
$01$ is less than or equal to the number of occurrences of $10$, plus
one.
\end{theorem}

Try to prove this by structural induction: \\
Define $P(s)$: \\
Notation: $\ms{num}(pat,s)$ = number of occurrences of the pattern
string pat in s. \\
$P(s)$: $\ms{num}(01,s) \leq \ms{num}(10,s) + 1$. \\
\\
If you try to prove this by structural induction, you will get
stuck. \\
Why? \\
Consider what happens when you add $1$ at the end.  \\
Could increase the number of $01$s without increasing the number of
$10$s. \\
\\
So, to prove by structural induction on strings, strengthen by adding
another clause: \\
If a string ends in $0$ then the number of $01$s is less than or equal
to the number of $10$s. \\
\\
That solves the problem by weakening what we have to show when the
string ends in $1$. \\
But maybe it causes another problem somewhere else. \\
Let's give it a try: \\
\\
Redefine $P(s)$: \\
$\ms{num}(01,s) \leq \ms{num}(10,s) + 1$, \\
and \\
If $s$ ends in $0$ then $\ms{num}(01,s) \leq \ms{num}(10,s)$. \\
\\
This means that, for each inductive step, we have two things to show:
%[[[Victor: slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall s \in S (P(s))$ \\
1. (Base) $P(\lambda)$ 
\> \> \> \> \> No patterns of either kind. \\
2. (Inductive step) $\forall s \in S (P(s) \Rightarrow P(s 0))$ \\
\> 1. Fix $s$. \\
\> 2. Assume $P(s)$. \\
\> 3. $P(s 0)$ \\
\> \> 1. $\ms{num}(01,s0) \leq \ms{num}(10,s0) + 1$. 
      \> \> \> ??? \\
\> \> 2. If $s 0$ ends in $0$ then $\ms{num}(01,s0) \leq
\ms{num}(10,s0)$. \\
\> \> \> \> \> ??? \\
\> \> 3. QED
      \> \> \> Conjunction \\
\> 4. QED 
   \> \> \> \> Implication, UG \\
3. (Inductive step) $\forall s \in S (P(s) \Rightarrow P(s 1))$ \\
\> 1. Fix $s$. \\
\> 2. Assume $P(s)$. \\
\> 3. $P(s 1)$ \\
\> \> 1. $\ms{num}(01,s1) \leq \ms{num}(10,s1) + 1$. 
      \> \> \> ??? \\
\> \> 2. If $s 1$ ends in $0$ then $\ms{num}(01,s1) \leq \ms{num}(10,s1)$.\\
\> \> \> \> \> ??? \\
\> \> 3. QED
      \> \> \> Conjunction \\
\> 4. QED 
   \> \> \> \> Implication, UG \\
4. QED
\> \> \> \> \> Structural induction on strings.
\end{tabbing}

First let's consider $s1$. \\
This is the case that looks dangerous, because it might increase the
number of $01$s. \\
We have to prove two statements. \\
\\
The second is easy, because the new string doesn't end in $0$. \\
We say it's ``vacuously true''. \\
\\
The first statement now takes some work. \\
We might be adding to the number of $01$s.  \\
However, if we do, the previous string must have ended with $0$.  \\
Then the inductive hypothesis says that the previous string had to
satisfy the stronger inequality in the second statement. \\
Adding one to the LHS of the stronger inequality yields the weaker
inequality we want. \\
\\
The following proof fragment considers cases based on whether $s$ ends
in $0$ or not.  \\
If not, it might end in $1$, or might be empty (don't forget this
possibility). 

\begin{tabbing}
%[[[Victor: slide]]]
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
\> 3. $P(s 1)$ \\
\> \> 1. $\ms{num}(01,s1) \leq \ms{num}(10,s1) + 1$. \\
\> \> \> 1. If $s$ ends in $0$ then $\ms{num}(01,s1) \leq\ms{num}(10,s1) + 1$. \\
\> \> \> \> 1. Assume $s$ ends in $0$. \\
\> \> \> \> 2. $\ms{num}(01,s) \leq \ms{num}(10,s)$ 
            \> Inductive hypothesis (3.2), part 2. \\
\> \> \> \> 3. $\ms{num}(01,s1) = \ms{num}(01,s) + 1$ 
            \> Adding one more $01$. \\
\> \> \> \> 4. $\ms{num}(10,s1) = \ms{num}(10,s)$ \\
\> \> \> \> 5. $\ms{num}(01,s1) \leq\ms{num}(10,s1) + 1$. 
            \> Algebra (combining 3.3.1.1.2, ...3, and ...4) \\
\> \> \> \> QED 
            \> Implication \\
\> \> \> 2. If $s$ ends in $1$ then $\ms{num}(01,s1)\leq\ms{num}(10,s1) + 1$. \\
\> \> \> \> \> Inductive hypothesis, part 1; no new $01$s. \\
\> \> \> 3. If $s = \lambda$ then $\ms{num}(01,s1) \leq\ms{num}(10,s1)+ 1$. \\
\> \> \> \> \> $s1$ is just $1$, which has no $01$s. \\
\> \> \> 4. QED 
         \> \> Cases \\
\> \> 2. If $s 1$ ends in $0$ then $\ms{num}(01,s1) \leq \ms{num}(10,s1)$. \\
\> \> \> \> \> Vacuously true, because $s1$ doesn't end in $0$. \\
\> \> 3. QED
      \> \> \> Conjunction \\
\end{tabbing}
Of course, you could also expand the step for $s$ ending in $1$ into a
careful series of inequalities. \\
\\
Now consider $s0$: \\
We hope that what we did to make the $s1$ case work doesn't mess up
the $s0$ case. \\
We have to check. \\
\\
The first statement is easy. \\
It follows from the first statement of the inductive
hypothesis for $s$, because we are not increasing the number of $01$s. \\
\\
But now the second statement takes more work. \\
The difficulty is that the new string ends in $0$, which means that we
have to show the stronger inequality in the second statement. \\
But to do this, we might only have the weaker inequality for the previous
string. \\
The argument again depends on what the previous string $s$ ended with. \\
So again, we consider cases, based on whether $s$ ends in $0$ or $1$,
or is empty. \\
\\
If $s$ ends in $0$ we rely on the second statement of the inductive
hypothesis for $s$ (with the stronger inequality), whereas if $s$ ends
in $1$ we rely on the first statement (with the weaker inequality). \\
In this case, we have to ``turn the weaker inequality into the
stronger inequality''.
%[[[Victor: slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
\> 3. $P(s 0)$ \\
\> \> 1. $\ms{num}(01,s0) \leq \ms{num}(10,s0) + 1$. 
      \> \> \> \> Inductive hypothesis, part 1; no new $01$s. \\
\> \> 2. If $s 0$ ends in $0$ then $\ms{num}(01,s0) \leq\ms{num}(10,s0)$. \\
\> \> \> 1. $\ms{num}(01,s0) \leq \ms{num}(10,s0)$. \\
\> \> \> \> 1. If $s$ ends in $0$ then $\ms{num}(01,s0) \leq\ms{num}(10,s0)$. \\
\> \> \> \> \> \> Inductive hypothesis, part 2; no new $01$s \\
\> \> \> \> 2. If $s$ ends in $1$ then $\ms{num}(01,s0) \leq\ms{num}(10,s0)$. \\
\> \> \> \> \> 1. Assume $s$ ends in $1$. \\
\> \> \> \> \> 2. $\ms{num}(01,s) \leq \ms{num}(10,s) + 1$. 
               \> Inductive hypothesis, part 1 \\
\> \> \> \> \> 3. $\ms{num}(01,s0) = \ms{num}(01,s)$ \\
\> \> \> \> \> 4. $\ms{num}(10,s0) = \ms{num}(10,s) + 1$ 
               \> Exactly one new occurrence of $10$. \\
\> \> \> \> \> 5. $\ms{num}(01,s0) \leq \ms{num}(10,s0)$ 
               \> Algebra \\
\> \> \> \> \> 6. QED
               \> Implication \\
\> \> \> \> 3. If $s = \lambda$ then $\ms{num}(01,s0) \leq \ms{num}(10,s0)$. \\
\> \> \> \> \> \> $s0$ is just $1$, which has no $01$s or $10$s. \\
\> \> \> \> 4. QED 
            \> \> Cases \\
\> \> \> 2. QED
         \> \> Propositional reasoning (truth table) \\
\> \> 3. QED 
      \> \> \> Conjunction
\end{tabbing}

If you actually write out all these cases in the proof, you will
notice that some facts are stated repeatedly, e.g., that when you add
a $0$ to the end of a string you are not increasing the number of $01$s. \\
To avoid having to state these facts several times, you can move them
earlier in the proof. \\
\\
Note that these proofs aren't very different from ordinary inductive
(or strong inductive) proofs. \\
In fact, you can prove all the same theorems by strong induction on
the number of formation steps. \\
The base cases correspond to $0$ applications of combination operations. 

\subsection{Recursively-defined functions on recursively-defined data
types}

Recursive definitions provide a natural way to define functions whose
domains are recursively-defined data types. \\
\\
Example: Boolean expressions \\
\\
Define $\ms{eval}$, a function from Boolean expressions to $\{ 0, 1 \}$, by: \\
$\ms{eval}(0) = 0$ \\
$\ms{eval}(1) = 1$ \\
$\ms{eval}((e \wedge e')) = 1$ if $\ms{eval}(e) = \ms{eval}(e') = 1$, $0$ otherwise. \\
$\ms{eval}((e \vee e')) = 0$ if $\ms{eval}(e) = \ms{eval}(e') = 0$, $1$ otherwise. \\
$\ms{eval}((\neg e)) = 0$ if $\ms{eval}(e) = 1$, $1$ otherwise. \\
\\
\\
Example: Binary trees \\
\\
Define the function $\ms{numnodes}(t)$
(a formal definition of what we understand intuitively to be the
number of nodes in the tree $t$), by: \\
\\
$\ms{numnodes}(\mbox{single node}) = 1$ \\
$\ms{numnodes}(\ms{makeleft}(t)) = \ms{numnodes}(t) + 1$ \\
$\ms{numnodes}(\ms{makeright}(t)) = \ms{numnodes}(t) + 1$ \\
$\ms{numnodes}(\ms{makeboth}(t,t')) = \ms{numnodes}(t) +\ms{numnodes}(t') + 1$ \\
\\
Define $\ms{numedges}(t)$: \\
$\ms{numedges}(\mbox{single node}) = 0$ \\
$\ms{numedges}(\ms{makeleft}(t)) = \ms{numedges}(t) + 1$ \\
$\ms{numedges}(\ms{makeright}(t)) = \ms{numedges}(t) + 1$ \\
$\ms{numedges}(\ms{makeboth}(t)) = \ms{numedges}(t) + \ms{numedges}(t') + 2$ \\
\\
We can use structural induction to prove properties of functions
defined in this way. \\
For example, we can reprove the relationship between numbers of nodes
and edges in terms of the formal functions $\ms{numnodes}$ and
$\ms{numedges}$. \\
(The argument is pretty much the same as before, just using the formal
function definitions instead of relying on our understanding of the
sizes of sets.) \\
\\
Define $P(t)$: 
$\ms{numnodes}(t) = \ms{numedges}(t) + 1$. 
%[[[Victor: slide]]]
\begin{tabbing}
XXX\=XXX\=XXX\=XXX\=XXXXXXXXXXXXXXXXXXXXX\=    \kill
Prove: $\forall t \in T (P(t))$ \\
1. (Base) $P(t_0)$, where $t_0$ is the single-node tree. 
\> \> \> \> \> By definition, $\ms{numnodes}(t_0) = 1$ \\
\> \> \> \> \> and $\ms{numedges}(t_0) = 0$ \\
2. (Inductive step) 
$\forall t \in T (P(t) \Rightarrow P(\ms{makeleft}(t))$ \\
\> 1. Fix $t$. \\
\> 2. Assume $P(t)$, that is, $\ms{numedges}(t)+1 = \ms{numnodes}(t)$. \\
\> 3. $P(\ms{makeleft}(t))$, that is,
$\ms{numedges}(\ms{makeleft}(t))+1 = \ms{numnodes}(\ms{makeleft}(t))$. \\
\> \> 1. $\ms{numedges}(\ms{makeleft}(t)) = \ms{numedges}(t) + 1$ \\
\> \> \> \> \> Definition of $\ms{numedges}$ \\
\> \> 2. $\ms{numnodes}(\ms{makeleft}(t)) = \ms{numnodes}(t) + 1$ \\
\> \> \> \> \> Definition of $\ms{numnodes}$ \\
\> \> 3. QED
      \> \> \> Algebra \\
\> 4. QED \\
3. (Inductive step) 
$\forall t \in T (P(t) \Rightarrow P(\ms{makeright}(t))$ 
\> \> \> \> \> Similar to the previous case. \\
4. (Inductive step) 
$\forall t, t' \in T (P(t) \wedge P(t') \Rightarrow P(\ms{makeboth}(t,t'))$ \\
\> 1. Fix $t$. \\
\> 2. Assume $P(t) \wedge P(t')$, that is, \\
\> \quad $\ms{numedges}(t)+1 = \ms{numnodes}(t)$ and $\ms{numedges}(t')+1 =\ms{numnodes}(t')$. \\
\> 3. $P(\ms{makeboth}(t,t'))$, that is,
$\ms{numedges}(\ms{makeboth}(t,t'))+1 = \ms{numnodes}(\ms{makeboth}(t))$. \\
\> \> 1. $\ms{numedges}(\ms{makeboth}(t,t')) = \ms{numedges}(t) +
\ms{numedges}(t') + 2$ \\
\> \> \> \> \> Definition of $\ms{numedges}$ \\
\> \> 2. $\ms{numnodes}(\ms{makeboth}(t,t')) = \ms{numnodes}(t) +
\ms{numnodes}(t') + 1$ \\
\> \> \> \> \> Definition of $\ms{numnodes}$ \\
\> \> 3. QED
      \> \> \> Algebra \\
\> 4. QED \\
5. QED
\> \> \> \> \> Structural induction on binary trees
\end{tabbing}

%[[[Should we do it this way in the first place?]]]

%Strings:
%Define general concatenation for pairs of strings based on the
%definition of strings using symbol concat.
%Then prove that length(s1 s2) = length(s1) + length(s2)

\section{Induction and Recursive Algorithms}

We've spent a lot of time studying induction.\\
This is because induction comes up all the time in analyzing
computation.\\
Why?  Well, induction is a ``one step at a time'' proof method.
Computations also evolve ``one step at a time.''\\

In fact, many of our inductive proofs can be seen as \emph{algorithms}
for solving a problem.  

We proved ``any $2^n$ by $2^n$ board can be tiled with Ls.''  \\
If you look at the inductive proof, we actually gave a
  \emph{procedure} for carrying out that tiling.\\
Break the board in four, tile each piece, and merge the tilings.\\
What kind of algorithm is this? \emph{recursive:} it calls
 itself on smaller subproblems.\\

Similarly, the definition of the Fibonacci numbers is also expressing
a recursive algorithm for computing them.  \\
And the inductive proof for Nim gave a game-winning algorithm.

Just about any inductive prove that a problem has a solution is in
fact providing a recursive algorithm for solving the problem.\\

Similarly, when you have a recursive algorithm that you think solves
some problem, and induction is the usual way to prove that it works.\\

In fact, an inductive proof can be thought of as a \emph{recursive
  algorithm} for writing down a proof:
\begin{structproof}
Procedure Write-Proof-For($n$)\\
\\
{\bf if} $n$ is 0\\
\> Write-the-base-case\\
{\bf else}\\
\> Write-Proof-For($n-1$)\\
\> Write-Inductive-Step($n$)\\
\end{structproof}
Assuming we have rule for writing down the base case $P(0)$, and
writing down the inductive step $P(n-1) \implies P(n)$ for any $n$, we
can write down a proof of $P(k)$ recursively: we recursively prove
$P(k-1)$, then we use the inductive step proof to prove
$P(k-1)\implies P(k)$.

\subsection{RSA}

RSA is at the core of many of the secure communication protocols we
use today (SSH, SSL, etc).  RSA works by treating any given message
(sequence of bits) as a big number $x$ and applying a mathematical
function that ``hides'' the number $x$ unless you know a secret key.

More precisely, it computes the remainder $r$ of $x^a$ on division by
$b$, where $a$ and $b$ are two specially constructed \emph{large}
numbers.  It turns out that if someone is given $r$ and knows the
right secret about the origin of $a$ and $b$, they can ``invert'' the
exponentiation and get $x$.

So, how can we compute the exponentiation?\\
The most obvious way is to work from the \emph{recursive definition}:
\begin{itemize}
\item $x^0=1$
\item $x^{n}=x\cdot x^{n-1}$
\end{itemize}
This is a definition of $x^n$, but also gives a recursive algorithm
for computing it:
\begin{structproof}
Procedure $\ms{Exp}(x,n)$\\
\\
{\bf if} $n=0$\\
\> return 1\\
{\bf else}\\
\> return $x*\ms{Exp}(x,n-1)$
\end{structproof}
Now it's pretty ``obvious'', but how would you \emph{prove} that
algorithm $\ms{Exp}$ returns the right answer?\\
Well, it doesn't always: what happens if you call $\ms{Exp}(5,-2)$?
$\ms{Exp}(8,2.5)$?\\ 
(Trying to prove correctness can reveal implicit assumptions.  We
assume $n$ is a natural number).

\begin{theorem}
For all $x,n \in \nats$, a call to $\ms{Exp}(x,n)$ returns $x^n$
\end{theorem}
Note: the procedure $\ms{Exp}$ above can be thought of as (i) a
computational process or (ii) a recursive function definition (defined
as the output of the computational process).  We need to prove that it
defines the \emph{same} function as $x^n$\\
We are actually claiming two distinct things in the theorem:
\begin{itemize}
\item The algorithm returns something (ie, doesn't run forever)
\item The algorithm returns the \emph{right} something.
\end{itemize}
\begin{proof}
By induction on $n$.  The base case, that $\ms{Exp}(x,0)$ returns
$x^0=1$, follows by inspection: on that call, the ``if'' condition is
satisfied, so we return 1.  For the inductive step, assume
$\ms{Exp}(x,n)$ returns $x^n$ and prove for $n+1$.  Consider the call
to $\ms{Exp}(x,n+1)$.  $n+1>0$, so the ``else'' clause is invoked.  We
call $\ms{Exp}(x,n)$ which, by assumption, returns $x^n$.  We multiply
that quantity by $x$, yielding $x^{n+1}$, and return it.  This proves
that $\ms{Exp}(x,n+1)$ returns the right value.
\end{proof}

OK, we've shown the procedure returns the right answer.\\
Is it a good implementation?\\
How many multiplications are done to compute $x^n$?  By induction,
there are $n$ multiplications.\\
Remember those ``128 bits keys'' we aren't allowed to export?  That
means the number $n$ has about 128 bits.\\
So we will be doing about $2^{128}$ multiplications.\\
Still be working on it when 6.042 finishes...\\

\subsection{Fast Exponentiation}

Let's cut down on the number of multiplies.\\
Suppose we want to compute $x^a$ and $a$ is even, say $2c$.\\
So we want to compute $x^a=x^2c$\\
Any obvious way to beat $a$ multiplies?\\
Note $x^{2c}=(x^c)^2$\\
So compute $x^c$ ($c$ multiplies) and then square it (1 multiply).\\
Total: $c+1 =1+ a/2$ multiplies.\\
What if $a$ is odd?\\

Generalize this:

\begin{proofoverhead}
Procedure $\ms{FastExp}(x,n)$\\
\\
{\bf if} $n=0$\\
\> return 1\\
{\bf else}\\
\> {\bf if} $n$ is even\\
\> \> let $y=\ms{FastExp}(x,n/2)$\\
\> \> return $y^2$\\
\> {\bf else}\\
\> \> let $y=\ms{FastExp}(x,(n-1)/2)$\\
\> \> return $x*y^2$
\end{proofoverhead}

\begin{theorem}
For all $x,n \in \nats$, a call to $\ms{FastExp}(x,n)$ returns $x^n$
\end{theorem}
\begin{proof}
  Induction again.  Base case ($n=0$) follows by inspection as before.
  So let's prove the inductive step.\\
What kinds of subproblems are we going to use to prove it works for
  $n$?  {\bf not} just $n-1$, so let's use strong induction.\\
Suppose the theorem is true for every $k < n$, let's prove it for
  $n$\\
Suppose we call $\ms{FastExp}(x,n)$.  We can assume $n\ne 0$ (we
  already did a proof for 0) so the {\bf else} clause is selected in
  the algorithm.\\
Two cases.\\
If $n$ is even (say $n=2m$) then we  call
  $\ms{FastExp}(x,m)$.   By strong induction this returns $x^m$, which
  we assign to $y$.\\
Then we return $y=(x^m)^2=x^{n}$ as required, so the proof works for
  this case.\\
If $n$ is odd (say $n=2m+1$) then we call   $\ms{FastExp}(x,m)$ and
  set $y$ to the result.  
Then we return $x*y^2=x*(x^m)^2=x^{2m+1}=x^n$\\
So the proof works in this case too.\\
These are all the cases.  QED.
\end{proof}

How many multiplications does {\bf this} algorithm do?

\begin{theorem}
If $n$ has $b$ bits, then $\ms{FastExp}(x,n)$ performs at most $2b$
multiplications.  
\end{theorem}
\begin{proof}
Induction on $b$.\\
Base case:  If $b=0$, then $n=0$ and we return without doing
any multiplications.  So the base case is true.
Inductive step: assuming it true for $b$ bits, consider a $b+1$-bit
number $n$.\\
We execute a recursive call to   $\ms{FastExp}$ on either $n/2$
or $(n-1)/2$, depending whether $n$ is odd or even.\\
Both of these numbers have at most $b$ bits.\\
So by induction, the recursion does at most $2b$ multiplications.\\
Then we do either one more (even case) or two more (odd case). So at
most two more.\\
Overall, at most $2b+2 = 2(b+1)$ multiplications.  QED.
\end{proof}

Using this algorithm, a 128-bit key means only 256 multiplications:
quite an improvement!


\subsection{Greatest Common Divisor}


Given 2 positive natural numbers, $a, b \in N^+$, their \emph{greatest
  common divisor} is the largest number that divides both without
remainder.

(Recall notation: $a \mid b$ means $a$ divides $b$.)

Finding GCD is a problem that must be solved to generate the
``secret'' used to decrypt RSA messages.

One of the first recorded algorithms---Euclid's algorithm---finds
greatest common divisors.  Key lemma for ``simplifying'' problem:

\begin{lemma}
Given $x>y$, let $r=x-y$.  Then any common divisor of $x$ and $y$ is
also a common divisor of $y$ and $r$, and vice versa.
\end{lemma}
\begin{corollary}
 GCD$(x,y)=$GCD$(y,r)$
\end{corollary}
\begin{proof} (of the corollary).  $(x,y)$ and $(y,r)$ have exactly
  the same set of divisors (by lemma).  Thus, the biggest number in
  each set, namely the greatest common divisor, is the same.
\end{proof}
\begin{proof} (of the lemma).  Suppose $d \mid x$ and $d \mid y$.  So
  $x=zd$ and $y=wd$.  Thus $r=x-y=d(z-w)$ so $d \mid r$, meaning $d$
  is a common divisor of $y$ (by assumption) and $r$ (by deduction).
  The other direction of proof is similar.
\end{proof}

From this lemma we get Euclid's algorithm:
\begin{structproof}
Procedure $\ms{Euclid}(x,y)$\\
\\
{\bf if} $x=0$ return $y$\\
{\bf else if} $y=0$ return $x$\\
{\bf else if} $x > y$ return $\ms{Euclid}(y,x-y)$\\
{\bf else} return $\ms{Euclid}(x,y-x))$
\end{structproof}

Example: \\
Find $\ms{GCD}(112, 84)$ = ? \\
Step 1: $112 - 84 = 28$, reduce to $(28,84)$. \\
Step 2: $84 - 28 = 56$, reduce to $(28,56)$. \\
Step 3: $56 - 28 = 28$, reduce to $(28,28)$. \\
Step 4: $28 - 28 = 0$, reduce to $28,0$\\
Now done, answer is $28$. \\
\\

Let's prove this algorithm does the right thing.\\
First faint worry: maybe we've got an infinite recursion?\\
First thing to prove for any algorithm: it \emph{terminates}

\begin{lemma}
For any natural number inputs $x$ and $y$, $\ms{Euclid}(x,y)$ returns
a value.
\end{lemma}
Why do we think this is true intuitively?  Because the arguments
``shrink'' on each recursive call and eventually reach 0.  Induction
lets us formalize this.
\begin{proof}
  Induction on $x+y$.\\
  More formally, we prove by strong induction on
  $n$ that for all inputs $x$ and $y$ such that $x+y=n$, the algorithm
  terminates.\\
Base case: if $x+y=0$, then both $x$ and $y$ are 0 so we return
  immediately.\\
Inductive step.  Assume the claim for $x+y<n$ and prove it for
  $x+y=n$.\\  
Now consider two cases.  Either $x \ge y$ or $y \ge x$.  Suppose first
  that $x \ge y$.\\
So consider two subcases.  If $y=0$, then we return immediately and the
  inductive step is proved.  If $y > 0$, then we make a recursive call
  with arguments $y$ and $x-y < x-0 =x$.\\
So the sum of the two new arguments is less than $x+y=n$\\
So the inductive hypothesis holds: the recursive call returns a
  value.\\
So our original call returns (the same) value.\\

What about the second case ($y \ge x$)?  Well, I would have to write
the same proof all over again, swapping $x$ and $y$ everywhere.  To
avoid such tedium, we say ``without loss of generality, assume $x \ge
y$.  This is code for ``the other case works exactly the same way''
and completes the proof.
\end{proof}

\begin{lemma}
For any natural number inputs $x$ and $y$, $\ms{Euclid}(x,y)$ returns
GCD$(x,y)$
\end{lemma}
\begin{proof}
Induction on $x+y$ as before.  The base case is trivial (assuming we
define GCD$(0,0)$ to be 0, which seems fair).

For the inductive step where $x+y= n > 0$, assume without loss of
generality that $x \ge y$.  Then we return the result of a recursive
call on $\ms{Euclid}(y,x-y)$.  This means (by strong induction) that
we return GCD$(y,x-y)$.  But we proved a lemma above that
GCD$(x,y)=$GCD$(y,x-y)$ so we are returning the right value.  
\end{proof}

Wait a minute, what's with the GCD$(0,0)$ definition?  Well,
technically every number is a common divisor of 0 and 0, so it should
be infinite?  

Why doesn't this matter?  Because we never actually make a recursive
call to $(0,0)$---one one number is 0, we terminate.

So even though we still need a base case for the induction pattern, we
never actually apply that base case.  Instead our base case is
implicit in the ``subcase'' of the inductive step where on argument is 0.

\end{document}

