% This is a TeX file
% It is essentially a programming language for mathematics documents
% You can ignore this first portion of the file, as it is essentially a bunch of "include" statements.
\documentclass[10pt]{article}

\usepackage{pictex, latexsym, graphicx,amsmath,amssymb,amsbsy,amsfonts,amsthm,verbatim}
\usepackage{graphics}
\usepackage{hyperref}
\usepackage{fullpage}
\usepackage{fancyhdr}
\usepackage{array}

\usepackage{algorithm,algorithmic}
\usepackage{multirow}

\setlength{\parskip}{0.5em}
\setlength{\parindent}{0em}
\setlength{\voffset}{-0.5in}
\setlength{\headsep}{0.25in}

% The following denotes a counter for the problems.
\newcounter{problem}
\newcommand{\problem}{\textbf{Problem \refstepcounter{problem}.}}

% These are macro shortcuts! Helpful!
\def\vu{\mathbf{u}}
\def\vx{\mathbf{x}}
\def\vy{\mathbf{y}}
\def\vz{\mathbf{z}}
\def\vb{\mathbf{b}}
\def\vv{\mathbf{v}}
\def\vw{\mathbf{w}}

\renewcommand{\implies}{\rightarrow}
%\newcommand{\lnot}{\neg}
\renewcommand{\lor}{\vee}
\renewcommand{\land}{\wedge}
\newcommand{\xor}{\oplus}
\newcommand{\symmdiff}{\triangle}
\renewcommand{\iff}{\leftrightarrow}
\newcommand{\TRUE}{\mathbf{T}}
\newcommand{\FALSE}{\mathbf{F}}
\newcommand{\universe}{\mathcal{U}}


\def\N{\mathbb{N}}
\def\Q{\mathbb{Q}}
\def\C{\mathbb{C}}
\def\Z{\mathbb{Z}}
\def\R{\mathbb{R}}
\def\cP{\mathcal{P}}



\begin{document}
\pagestyle{fancyplain}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NOW you can start typing your text that will appear in the PDF!!!

% Note that anything after a '%' is a comment!

% Anything immediately after a '\' is a command. 
% Some denote formatting, some are for special characters.
% The '$' symbol starts and ends "math mode". Use this only for mathematical statements
% In math mode, any sequence of letters is meant to be a list of variables, so use "\sin x" for the sine of x, which will look much different from "sin x"

\lhead{}
\rhead{}
\chead{COM S 330 --- Lecture Notes --- Week of Feb 23--27}

\renewcommand{\arraystretch}{1.25}



\section{Monday, February 23}

\subsection{LLM 5.4: State Machines}

\textbf{Reading:} LLM 5.4.
\vspace{1em}
\subsubsection{State Machine Definition}

\textbf{Def:} A \emph{state machine} is a triple $(S, T, s_0)$ where $S$ is a set of \emph{states}, $T$ is a set of \emph{transitions} between states (so $T$ is a subset of $S \times S$), and $s_0 \in S$ is an \emph{initial state}.
A state machine \emph{executes} by constructing a sequence $s_0,s_1,\dots,s_n,\dots$ where $s_0$ is the initial state, and for all $n \geq 0$, when $s_n$ is a state in $S$, the sequence value $s_{n+1}$ is a state in $S$ such that the pair $(s_n,s_{n+1})$ is in $T$ (that is, we can transition from $s_n$ to $s_{n+1}$).



When talking about state machines, we will make use of the following concept.
\begin{quote}
\fbox{\parbox{5in}{
Let $M = (S, T, s_0)$ be a state machine.

\vspace{0.5em}
\textbf{Preserved Invariant:} 
A property $P : S \to \{ \TRUE, \FALSE\}$ is a \emph{preserved invariant} for $M$ if ``$\forall (s,t) \in T$, $P(s) \implies P(t)$.'' That is, if a state has property $P$, then all states that are reachable from that state by one transition also have that property.

\vspace{0.5em}
\textbf{Invariant Principle:} 
If a preserved invariant of a state machine is true for the start state, then it is true for all reachable states.
}}
\end{quote}

Observe that the above concept is equivalent to induction.
If $s_0,s_1,s_2,\dots,s_n,\dots$ is a sequence of states given by a state machine, then let $Q(n) = P(s_n)$.
The base case is that $Q(0)$ is true, which means the property is held by the initial state $s_0$.
The preserved invariant property means that if $Q(n)$ is true, then $Q(n+1)$ is true.
So, you can use induction explicitly, or you can prove that a property is a preserved invariant.



\subsubsection{GCD Algorithm}

Recall Euclid's algorithm for computing the \emph{greatest common divisor}.

\begin{quote}
\textbf{Input:} Integers $a$ and $b$ where $a \geq b \geq 0$.

If $b \equiv 0$, then return $a$.

Otherwise, let $q,r$ be integers such that $a = qb + r$ and $0 \leq r < b$.

Assign $a \leftarrow b$ and $b \leftarrow r$, then repeat the algorithm.
\end{quote}

We may prove that this algorithm is correct later, but instead let's prove that this algorithm will terminate in a finite number of steps.

\textbf{Thm:} If $a$ and $b$ are integers with $a \geq b \geq 0$, the above algorithm will halt in a finite number of steps.

\begin{proof}
Observe that every time $a$ and $b$ are reassigned, $b$ decreases by at least one (as the value $r$ is guaranteed to be in the range $0 \leq r < b$).
Therefore, the values $a$ and $b$ are reassigned at most $b$ times (and the if statement is tested at most $b+1$ times).
\end{proof}

Let $E = (S,T, s_0)$ be the machine where $S$ is the set of pairs $(a,b) \in \N \times \N$ where $a \geq b$, $s_0 = (a_0,b_0)$ for some pair $(a_0,b_0)\in \N\times \N$, and the transition set $T$ is given by 
\[
	T = \big\{ \big((a,b), (b, a \% b)\big) : a, b\in \N, a \geq b > 0\}.
\]
(Recall that $a \% b$ returns the remainder after integer division of $a$ by $b$.)

The theorem that the GCD Algorithm halts is equivalent to saying this machine will reach a state $(a,0)$ (where there are no transitions out).

\begin{quote}
\fbox{\parbox{5in}{
Let $M = (S, T, s_0)$ be a state machine.

\textbf{Decreasing Functions:}
Let $f : S \to \N$ be a function\footnote{It is important that $f$ goes to natural numbers.}
The function $f$ is \emph{decreasing} if $\forall (s,t)\in T (f(s) > f(t)$. 
That is, for every transition $s \to t$, the value of $f$ decreases from $f(s)$ to $f(t)$ with $f(t) < f(s)$.

\vspace{0.5em}
\textbf{Monotonicity Principle:}
If $f : S \to \N$ is a decreasing function, then the time it takes for a machine to terminate starting at a state $s$ is at most $f(s)$.
}}
\end{quote}

\textbf{Thm:} The GCD Algorithm will terminate in a finite number of steps.

\begin{proof}
Define $f(a,b) = a+b$.
Note that if $(a,b) \to (b,r)$ is a transition, then $0 \leq r < b \leq a$. 
Therefore, $f(b,r) = b+r < a+b = f(a,b)$ and hence $f$ is a decreasing function.

By the monotonicity principle, starting at $(a,b)$ will result in a halt in at most $a+b$ steps. (This is a gross over-count!)
\end{proof}

\textbf{Thm:} Let $d \geq 1$ be a positive integer. 
Let $P_d(a,b)$ be the property ``$a$ and $b$ are both multiples of $d$.''
$P_d(a,b)$ is a preserved invariant for the machine $E$.

\begin{proof}
Suppose that $P_d(a,b)$ is true and $(a,b) \to (b,r)$ is a transition (implying $b > 0$ and $r = a \% b$).
Then there exist nonnegative integers $i,j$ such that $a = di$ and $b = dj$.

By integer division with remainder, there exists an integer $q$ such that $a = qb + r$.
So, $di = qdj + r$, which implies that $r = d(i-qj)$.
Therefore, $r$ is a multiple of $d$, and hence $P_d(b,r)$ is true.
\end{proof}

\textbf{Thm:} The GCD algorithm outputs the correct value.

In order to prove this statement, we need to make a different state machine, one that is \emph{reversible}.

\begin{quote}
\fbox{\parbox{5in}{
Let $M = (S, T, s_0)$ be a state machine.

\textbf{Reversible Invariant:}
A property $P : S \to \{ \TRUE, \FALSE\}$ is a \emph{reversibile invariant} if for all transitions $s \to t$ we have $P(s) = P(t)$.
(Equivalently, $(s\to t)$ implies $P(s) \iff P(t)$.)

\vspace{0.5em}
\textbf{Reversibility Principle:}
If $P$ is a reversible property and $s$ is a state reachable from the initial state $s_0$, then $P(s) = P(s_0)$.
}}
\end{quote}


\begin{proof}
Recall the property $P_d$ is a preserved invariant for the transitions in $T$.
We will show it is also a reversible invariant for the transitions in $T$.
Suppose $(a,b) \to (b,a\%b)$ is a transition in $T$ and $P_d(b,a\%b)$ is true.
Then $b = d\ell$ and $a\%b = r = dk$ for some integers $k$ and $\ell$.
By definition of integer division there is an integer $q$ where $a = qb + r$.
Then $a = qb+r = q(d\ell) + dk = d(q\ell+k)$, so $d$ is a divisor of both $a$ and $b$.
Thus, $P_d(a,b)$ is true.

Let $d$ be the greatest common divisor of $a$ and $b$.
Then $P_d(a,b)$ is true, and $P_{d'}(a,b)$ is false for all $d' > d$.
If we follow transitions from $T$, we will terminate in a state $(r,0)$.
By the Invariant Principal, $P_d(r,0)$ is true, so $d$ is a divisor of $r$, and $d \leq r$.
However, $r$ is a divisor of both $r$ and $0$, so by the Reversibility Principle $P_r(a,b)$ is true.
Therefore, $r \leq d$ (since $d$ is the greatest common divisor of $a$ and $b$) and therefore $r = d$.
\end{proof}



\subsubsection{Suggested Homework}

LLM Problems 5.10,  5.28--38.

\clearpage



\section{Wednesday, February 25}

\subsection{LLM 5.4: State Machines --- Rosen 5.3: Recursive Definitions and Structural Induction}

\textbf{Reading:} LLM 5.4, 6.1. Rosen 5.3.

\subsubsection{Recursively Defined Functions}

If the definition of a function refers to its own output (on different, usually smaller values), then the function is defined \emph{recursively}.

For example, the \emph{Fibonacci sequence} $\{F_n\}_{n=0}^\infty$ is really a function from the natural numbers to the natural numbers, defined as $F(0) = 0$, $F(1) = 1$, and $F(n) = F(n-1) + F(n-2)$ for all $n \geq 2$.
This is well-defined, but you should be very careful when constructing a recursive function that it is well defined!

We will use the following property of the Fibonacci numbers.

\textbf{Thm:} Let $\phi = \frac{1+\sqrt{5}}{2}$. For all $n \geq 3$, $F_n \geq \phi^{n-2}$.

There is a proof of this fact in the book, using strong induction.

\textbf{Lam\'e's Theorem.} 
Let $a$ and $b$ be positive integers with $a \geq b$. 
The number of divisions used by the Euclidean algorithm to find GCD$(a, b)$ is less than or equal to five times the number of decimal digits in $b$ (i.e. at most $5\lceil\log_{10} b\rceil$).

Note: We will prove something slightly weaker: The GCD state machine follows at most $5(\log_{10} b+1)$ transitions. 

\begin{proof}
If we follow the algorithm, and consider all of the pairs we use during the while loop, we will have a sequence $(a_0,b_0), (a_1,b_1), \dots (a_n,b_n), (a_{n+1},b_{n+1})$ where for all $i \in \{0,\dots, n\}$, $a_{i+1} = b_i$ and $b_{i+1} = a_i \%  b_i$.
(Note that now we can replace $a_i$ with $b_{i-1}$.)
Finally, since we halted, $b_{n+1} = 0$.
By the definition of integer division, for every $i \in \{0,\dots,n\}$, there is an integer $q_i$ where $b_{i-1} = q_ib_i + b_{i+1}$.
Also, since $a_i \geq b_i$ for all $i$ (check!) we have that $q_i \geq 1$, so $b_{i-1} \geq b_i + b_{i+1}$.

Specifically in the last equation, we have $b_{n-1} = q_{n}b_n + b_{n+1}$, but since $b_{n-1} > b_n$ and $b_{n+1} = 0$, we have that $q_{n} \geq 2$.

\textbf{Claim:} For all $k \geq 0$, $b_{n-k} \geq F_{k+2}$.

We prove this claim by induction on $k$.

Case $k = 0$: $b_n \geq 1 = F_2$.

Case $k = 1$: $b_{n-1} \geq 2b_n \geq 2 = F_3$.

(Strong Inductive Hypothesis) Suppose that for some $K$ where $0 < K \leq n$ we have $b_{n-k} \geq F_{k+2}$ for all $k$ where $0 \leq k < K$.

Case $K$: $b_{n-K} = q_{n-(K-1)}b_{n-(K-1)} + b_{n-(K-2)} \geq b_{n-(K-1)} + b_{n-(K-2)} \geq F_{K+1} + F_{K+2} = F_{K+3}$.

Thus, $b_{n-k} \geq F_{k-2}$, proving the claim.
Therefore $b_0 = b_{n-n} \geq F_{n-2} \geq \phi^{n-4}$.

We now have an exponential relationship between the value of $b_0$ and the number of steps until halting $(n+1)$.
That is $\log_{10} b_0 \geq \log_{10} \phi^{n-4} = (n-4)\log_{10}\phi$. 
Since $\phi \approx 1.61$, and $\log_{10}\phi \approx 0.209 > \frac{1}{5}$, we have that $\log_{10} b_0 \geq \frac{1}{5}(n-4)$.
Thus, $5(\log_{10}b_0+1) \geq n+1$.
\end{proof}

\subsubsection{Alphabets and Strings}

Let $\Sigma$ be a finite set (here we say $\Sigma$ is the name of a set, not the summation notation). 
We will use $\Sigma$ to denote a special set, called an \emph{alphabet}. 
The elements of $\Sigma$ will be ``letters'' of ``words'' but in a very abstract sense.
Typically, $\Sigma = \{0,1\}$, but $\Sigma$ could be any finite set.

For $k \geq 0$, the set $\Sigma^k$ is the set of $k$-tuples where every entry comes from $\Sigma$. 
The set $\Sigma^*$ is equal to $\cup_{k=0}^\infty \Sigma^k$, the set of all finite strings (note that for every finite string $\vx$ of length $k$, $\vx \in \Sigma^k \subset \Sigma^*$). 
We will denote a string $\vx = (x_1,x_2,\dots,x_k)$ as $x_1x_2\dots x_k$.

If $\Sigma=\{0,1\}$, then $\Sigma^*$ contains all finite-length \emph{binary strings}.
This includes the \emph{empty string} $w$ which has length $0$.

We can define state machines using $\Sigma^*$ as the state space!
A natural class of transitions is to move between strings by adding or removing letters from a string.
Let $\Sigma = \{0,1\}$ and consider the following possible transitions:

\begin{enumerate}
\item If $\vx = x_1x_2\dots x_k$ is a string and $k$ is even, then $x_1x_2\dots x_k \to x_1x_2\dots x_k1$ is an allowed transition. (Append a 1 to the end of the string.)
\item If $\vx = x_1x_2\dots x_k$ is a string and $k$ is odd, then $x_1x_2\dots x_k \to x_1x_2\dots x_k0$ is an allowed transition. (Append a 0 to the end of the string.)
\item If $\vx = x_1x_2\dots x_k$ is a string and $k \geq 1$, then $x_1x_2\dots x_k \to x_2\dots x_k$ is an allowed transition. (Delete the first position.)
\end{enumerate}

\textbf{Thm:} If $\vx = x_1x_2\dots x_k$ is any string in $\Sigma^*$, then $\vx$ is reachable from the empty string by the above transitions.

\begin{proof}
Starting from the empty word $w$, follow $k$  total transitions of type (1) and (2), alternating as necessary to create the string $101010\dots 10$ or $101010\dots 01$ of length $k$.
For each $i \in \{1,2,\dots,k\}$, we will make the following choices:
\begin{itemize}
\item If $x_i = 1$ and the current string has even length, then use (1) to append a 1.
\item If $x_i = 0$ and the current string has odd length, then use (2) to append a 0.
\item If $x_i = 1$ and the current string has odd length, then use (3) to delete the first position, then (1) to append a 1.
\item If $x_i = 0$ and the current string has even length, then use (3) to delete the first position, then (2) to append a 0.
\end{itemize}
During this process, we may have deleted some leading positions, but at most one position was deleted per letter that was added.
Therefore, we deleted no more than $k$ elements, which is the length we had before adding the letters $x_1\dots x_k$.
Thus, there are at least $k$ letters in the string at this point, and the final $k$ letters correspond to the string $\vx$.

Use (3) to delete the first position until the length of the string is $k$.
The resulting string is $\vx$.
\end{proof}



%We can define certain sets \emph{iteratively}, and take their ``limit''.
%
%\textbf{Def:} For $a \leq b$, the closed interval $[a,b]$ is defined as $[a,b] = \{ x \in \R : a \leq x \leq b\}$ and the open interval $(a,b)$ is defined as $(a,b) = \{ x \in \R : a < x < b\}$.
%
%\textbf{Def:} Let $C_0 = (0,1)$, the open interval between $0$ and $1$.
%For $n \geq 0$, define $C_{n+1}$ as follows: for every maximal interval $(a,b) \subseteq C_n$, the intervals $(a, a + (b-a)/3)$ and $(b-(b-a)/3,b)$ are subsets of $C_{n+1}$.
%Let $C = \cap_{n=0}^\infty C_n$.
%(Observe that $x \in C$ if and only if $x \in C_n$ for all $n \geq 0$, which also means $x \notin C$ if and only if $x \notin C_n$ for some $n \geq 0$. Since $C_n \supset C_{n+1}$, we only need to find the \emph{first} $n \geq 0$ where $x \notin C_n$.)
%The set $C$ is called the \emph{Cantor set}.
%
%See \url{http://en.wikipedia.org/wiki/Cantor_set} for more info.
%
%\textbf{Def:} Let $K_0$ be an equilateral triangle with side lengths 1, with its interior.
%For each $n \geq 0$, consider $K_n$ as a simple polygon and for each side $\overline{ab}$ of the polygon, divide the side into three segments $\overline{ac}, \overline{cd}, \overline{cb}$ of equal length, then replace the segment $\overline{cd}$ with two segments $\overline{ce}$ and $\overline{ed}$, where $\triangle cde$ is an equilateral triangle, and the point $e$ is in the exterior of the polygon $K_n$.
%The resulting polygon defines $K_{n+1}$.
%The \emph{Koch snowflake} is the set $K = \cup_{n=0}^\infty$. 
%(Observe that $x \in K$ if and only if $x \in K_n$ for some $n \geq 0$, and since $K_n \subset K_{n+1}$, we only need to determine the \emph{first} $n$ where $x \in K_n$. To demonstrate $x \notin K$, it is more difficult: we must show that $x \notin K_n$ for \emph{any} $n \geq 0$.)
%
%See \url{http://en.wikipedia.org/wiki/Koch_snowflake} for more info.
%
%The Cantor set and the Koch snowflake are defined using an iterative process, then taking the ``limit'' of the process. 
%Mathematical induction is a natural tool to prove something about the set sequences $\{ C_n\}_{n=0}^\infty$ and $\{K_n\}_{n=0}^\infty$, including statements about ``What elements are in $C_n$?'' and ``How much area is inside $K_n$?'' 
%These will require some strong inductive proofs.
%
%However, we will talk about a different way to define sets: \emph{structural induction}.
%We show an example first.


\textbf{Def:} Let $\Sigma$ be a finite alphabet.
Define $W$ as follows: (Basis) the empty word is in $W$, and (Recursive Step) if $x_1\dots x_n \in W$, and $y \in \Sigma$, then $x_1\dots x_ny \in W$.

\textbf{Thm:} $W = \Sigma^*$.

\begin{proof}
We must show $W \subseteq \Sigma^*$ and $\Sigma^* \subseteq W$.

($W \subseteq \Sigma^*$) Here we must claim that all elements of $W$ are finite-length strings in the alphabet $\Sigma$.
We create a state machine $M$ with initial state $w$, the empty word.
If $\vx$ is a reachable state, then we let $\vx \to \vx y$ be a possible transition for every $y \in \Sigma$.
Thus, the elements of $W$ are exactly the reachable states in this state machine!
Our preserved invariant is this: ``$\vx$ is a word in $\Sigma^*$.''
If $\vx$ is a word in $\Sigma^*$ (so $\vx = x_1\dots x_n \in \Sigma^n$ for some $n$), then the concatenation $\vx y = x_1 \dots x_ny \in \Sigma^{n+1}$ is also a word in $\Sigma^*$.
By the invariance principle, every element of $W$ is also an element of $\Sigma^*$.

($\Sigma^* \subseteq W$) For this case, we need to demonstrate that for any word $x_1\dots x_n \in \Sigma^*$, we have that $x_1\dots x_n \in W$.
Here we use regular induction to prove ``$\Sigma^n \subseteq W$'' for all $n \geq 0$.

Case $n = 0$: $\Sigma^0 = \{ w \}$ where $w$ is the empty word. $w \in W$.

(Induction Hypothesis) Let $n \geq 0$ and suppose that $\Sigma^n \subseteq W$.

Case $n+1$: Let $x_1\dots x_nx_{n+1} \in \Sigma^{n+1}$. 
The word $x_1\dots x_n$ is in $\Sigma^n$, and $\Sigma^n \subseteq W$ by the Induction Hypothesis.
By the recursive step, since $x_{n+1} \in \Sigma$ and $x_1\dots x_n\in W$, we have that $x_1\dots x_nx_{n+1} \in W$.
Therefore, $\Sigma^{n+1} \in W$.

Since every element in $\Sigma^*$ is in some set $\Sigma^n$, we have that $\Sigma^* \subseteq W$.
\end{proof}

We can define an invariant, called \emph{length}, on the set $\Sigma^*$.
Define the function $\ell : \Sigma^* \to \N$ as (Basis) $\ell(w) = 0$ where $w$ is the empty word, and (Recursive Step) if $\vx \in W$, then $\ell(\vx1) = \ell(\vx) + 1$ and $\ell(\vx0) = \ell(\vx) + 1$.
The length of a string $\vx$ is usually denoted by $|\vx| = \ell(\vx)$.

We can define a set $S$ recursively by using a Basis: $x_1,\dots,x_n \in S$ and a Recursive Step ``if $x \in S$ then $f(x) \in S$'' (or sometimes, ``if $x \in S$, then $A_x \subseteq S$'' where $A_x$ is a set defined by the element $x$).

\textbf{Note.} In the previous example, we used one element for the Basis: the empty word is in $W$.
In the Recursive Step, we use the set form: for an alphabet $\Sigma$ and a word $\vx = x_1x_2\dots x_n$, let $A_\vx = \{ x_1\dots x_ny : y \in \Sigma\}$. Then our Recursive Step is that for all $\vx \in W$, $A_\vx \subseteq W$.

\textbf{Exclusion Rule.} A recursively defined set contains \emph{only} the elements that are \emph{required} to be in the set by the implications.

An equivalent definition of the exclusion rule is that we have a state machine $M$ whose states are \emph{all possible set elements}, and we have a transition $x \to y$ if and only if the implication step has $y = f(x)$ or $y \in A_x$.
We can then define the set $S$ to be all states reachable from an initial state (defined by the base case).
This allows us to use the Invariance Principle when proving things about elements of our recursively-defined set!



\textbf{Def:} A proof by \emph{structural induction} consists of two parts. These parts are

\textbf{BASIS STEP:} Show that the result holds for all elements specified in the basis step of the
recursive definition to be in the set.

\textbf{RECURSIVE STEP:} Show that if the statement is true for each of the elements used to construct new elements in the recursive step of the definition, the result holds for these new elements.

The Recursive Step of this proof is essentially showing that in our state-machine description, the property ``$x \in S$'' is a preserved invariant.

\textbf{Ex:} Define $S \subseteq \R$ as (Basis) $0 \in S$, and (Recursive Step) If $x \in S$, then $x + 1 \in S$. Prove that $S = \N$.

\textbf{Ex:} Define $S \subseteq \R$ as (Basis) $2 \in S$, and (Recursive Step) If $x \in S$ and $y \in S$, then $xy \in S$. Prove that $S = \{ 2^i : i \geq 1\}$.

\textbf{Ex:} Define $S \subseteq \R$ as (Basis) $1 \in S$, and (Recursive Step) If $x \in S$, then $x+x \in S$ and $\frac{1}{x} \in S$. Prove that $S = \{ 2^i : i \in \Z \}$. 

\textbf{Ex:} Define $S \subseteq \R$ as (Basis) $0 \in S$, and (Recursive Step) If $x \in S$, then $x+1 \in S$, $-x \in S$, and if $x > 0$ then $\frac{1}{x} \in S$. Prove that $S = \Q$.  (Hint: To show that $\Q \subseteq S$, let $\frac{p}{q}$ be a fraction where $p$ and $q$ have no common divisors other than 1 and $p \geq 0$ and $q > 0$. Use strong induction on $p+q \geq 1$ to show that each such fraction $\frac{p}{q}$ is in $S$; use either $\frac{p}{q}-1$ or $\frac{q}{p}-1$ in the induction step.)

\textbf{Ex:} Let $\Sigma = \{0,1\}$. Define $S \subseteq \Sigma^*$ as (Basis) $w \in S$ where $w$ is the empty word, and (Recursive Step) If $x_1\dots x_n \in S$ then $x_1\dots x_n0\in S$ and for every $k \geq 0$, the string $x_1\dots x_n1\underbrace{0\dots0}_{\text{$k$ zeroes}}1 \in S$. Prove that $S = \{ \vx \in \Sigma^* : \text{there are an even number of $1$'s in $\vx$} \}$.

\textbf{Ex:} Let $\Sigma = \{0,1\}$. Define $S \subseteq \Sigma^*$ as (Basis) $0 \in S$, $1\in S$ and  $w \in S$ where $w$ is the empty word, and (Recursive Step) If $\vx \in S$, then $0\vx0\in S$ and $1\vx1\in S$. Prove that $S$ is the set of \emph{palindromes}: strings $x_1\dots x_n$ where $x_1\dots x_n = x_n\dots x_1$.


\textbf{Ex$^*$:} Let $\Sigma = \{0,1\}$. Define $S \subseteq \Sigma^*$ and $T \subseteq \Sigma^*$ as (Basis) $w \in S$ where $w$ is the empty word and $1 \in T$, and (Recursive Step) If $x_1\dots x_n \in S$ then $x_1\dots x_n0\in S$ and $x_1\dots x_n1 \in T$; if $x_1\dots x_n \in T$ then $x_1\dots x_n0\in T$ and $x_1\dots x_n1 \in T$. Prove that $S = \{ \vx \in \Sigma^* : \text{there are an even number of $1$'s in $\vx$} \}$ and $T = \{ \vx \in \Sigma^* : \text{there are an odd number of $1$'s in $\vx$} \}$.

\textbf{Ex$^*$:} (See LLM 6.2) Let $\Sigma = \{ [, ]\}$. Define $M \subseteq \Sigma^*$ as (Basis) $w \in M$ where $w$ is the empty word, and (Recursive Step) If $\vx \in M$ and $\vy \in M$, then $\vx\cdot \vy \in M$ ($\vx \cdot \vy$ is the concatenation of the strings) and $[\vx] \in M$. Prove that the set $M$ is equal to the set of \emph{correctly-nested brackets}, that is in a string $x_1\dots x_n$ the opening brackets $[$ are paired with closing brackets $]$ such that if $(x_i,x_j)$ is such a pair $([,])$ then $i < j$, and if $(x_i,x_j)$ and $(x_a,x_b)$ are two distinct pairs where $i < a$ then $b < j$.



\textbf{Recommended Homework:} Rosen 5.3: 1--4, 5--6, 7--8, 12--19, 20--21, 23--25, 26--27, 28$^*$, 32, 36$^*$, 40$^*$, 47$^*$, 48--52, 55., 60--62, 63--65.


\clearpage
\section{Friday, February 25, 2015}

\subsection{Rosen 5.3: Recursive Definitions and Structural Induction}

\textbf{ReadingL} Rosen 5.3,  Ducks 10.5, LLM 10.1, 

Today, we will define \emph{rooted binary trees} recursively.
We will also define some invariants of binary trees using this recursive construction.

A \emph{rooted tree} $T$ is given by a set $V(T)$ of \emph{vertices}, where every vertex has at most two \emph{children}, if $u$ is a child of $v$, then $v$ is the \emph{parent} of $u$, every vertex has at most one parent, and there is exactly one vertex with no parent; that vertex is called the \emph{root}.

Define the set $T$ of \emph{rooted trees} using the following recursive definition:

\textbf{Basis Step:} The tree on one vertex is in $T$.

\textbf{Recursive Step:} If $T_1,\dots, T_k$ are in $T$, then let $V =\{r\}\cup \{ (i,v) : i \in \{1,\dots,k\}, v \in V(T_i)\}$ and for $i \in \{1,\dots,k\}$ let $(i,u)$ be a child of $(i,v)$ exactly when $u$ is a child of $v$ in $T_i$. Let $r$ be the parent of $(i,r_i)$ for each $i\in \{1,\dots,k\}$ where $r_i$ is the root of $T_i$.

A \emph{rooted binary tree} is a rooted tree where every vertex has at most two children, and each is labeled as a \emph{left} or \emph{right} child (we can have just one child, but not two of the same type).

The \emph{extended binary trees} is the set $B$ defined as:

\textbf{Basis Step:} The empty set $\varnothing$ is in $B$.

\textbf{Recursive Step:} If $T_1, T_2$ are in $B$, then let $V =\{r\}\cup \{ (i,v) : i \in \{1,2\}, v \in V(T_i)\}$ and for $i \in \{1,2\}$ let $(i,u)$ be a child of $(i,v)$ exactly when $u$ is a child of $v$ in $T_i$. 
If $T_1 \neq \varnothing$, then the left child of $r$ is $(1,r_1)$ where $r_1$ is the root of $T_1$.
If $T_2 \neq \varnothing$, then the right child of $r$ is $(2,r_2)$ where $r_2$ is the root of $T_2$.


\textbf{Recommended Homework:} Rosen 5.3: 44.

\end{document} 