 # CSE 311 Lecture 19: Structural Induction

## Topics

Start early!
Recursively defined sets
Recursive definitions of sets.
Structural induction
A method for proving properties of recursive structures.
Using structural induction
Example proofs about recursively defined numbers, strings, and trees.

$\newcommand{hilite}{\color{#1}{#2}}$ $\newcommand{qed}{\square}$ $\newcommand{\N}{\mathbb{N}}$ $\newcommand{\Z}{\mathbb{Z}}$ $\newcommand{\mod}{#1\ \text{mod}\ #2}$ $\renewcommand{\div}{#1\ \text{div}\ #2}$ $\newcommand{\congruent}{#1\equiv #2\,(\text{mod}\,#3)}$ $\newcommand{\gcd}{\text{GCD}(#1, #2)}$ $\newcommand{ra}{\color{SteelBlue}{#1}}$ $\newcommand{rb}{\color{MediumSeaGreen}{#1}}$ $\newcommand{rule}{\begin{array}{c}#1\\ \hline \therefore #2\end{array}}$ $\newcommand{op}{\mathsf{#1}}$ $\newcommand{Node}{\bullet}$ $\newcommand{Tree}{\op{Tree}}$ $\newcommand{Size}{\vert #1\vert}$ $\newcommand{Height}{\lceil #1\rceil}$

Start early!

### Homework 6 isn’t necessarily harder …

But you may find it to be more work than most other assignments.
Pay special attention to Problem 6.4.
Requires keeping careful track of
(1) what you know and
(2) what you need to prove.

## Recursively defined sets

Recursive definitions of sets.

### Giving a recursive definition of a set

A recursive definition of a set $S$ has the following parts:
Basis step specifies one or more initial members of $S$.
Recursive step specifies the rule(s) for constructing new elements of $S$ from the existing elements.
Exclusion (or closure) rule states that every element in $S$ follows from the basis step and a finite number of recursive steps.

The exclusion rule is assumed, so no need to state it explicitly.

### Examples of recursively defined sets

Natural numbers
Basis: $0\in S$
Recursive: if $n\in S$, then $n+1\in S$
Even natural numbers
Basis: $0\in S$
Recursive: if $x\in S$, then $x + 2\in S$
Powers of 3
Basis: $1\in S$
Recursive: if $x\in S$, then $3x\in S$
Fibonacci numbers
Basis: $(0,0)\in S, (1,1)\in S$
Recursive: if $(n-1,x)\in S$ and $(n-2,y)\in S$, then $(n,x+y)\in S$

### More examples of recursively defined sets

Strings
An alphabet $\Sigma$ is any finite set of characters.
The set $\Sigma^*$ of strings over the alphabet $\Sigma$ is defined as follows.
Basis: $\varepsilon \in \Sigma^*$, where $\varepsilon$ is the empty string.
Recursive: if $w\in\Sigma^*$ and $a\in\Sigma$, then $wa\in\Sigma^*$
Palindromes (strings that are the same forwards and backwards)
Basis: $\varepsilon\in S$ and $a\in S$ for every $a\in\Sigma$
Recursive: if $p\in S$, then $apa\in S$ for every $a\in\Sigma$
All binary strings with no 1’s before 0’s
Basis: $\varepsilon\in S$
Recursive: if $x\in S$, then $0x\in S$ and $x1\in S$

### Functions on recursively defined sets

Length
$\op{len}(\varepsilon) = 0$
$\op{len}(wa) = \op{len}(w) + 1$ for $w\in\Sigma^*$, $a\in\Sigma$
Reversal
$\varepsilon^{\op{R}} = \varepsilon$
$(wa)^{\op{R}} = aw^{\op{R}}$ for $w\in\Sigma^*$, $a\in\Sigma$
Concatenation
$x\bullet \varepsilon = x$ for $x\in\Sigma^*$
$x\bullet (wa) = (x\bullet w)a$ for $x,w\in\Sigma^*$, $a\in\Sigma$
Number of $c$’s in a string
#$_ c(\varepsilon) = 0$
#$_ c(wc) =$ #$_ c(w) + 1$ for $w\in\Sigma^*$
#$_ c(wa) =$ #$_ c(w)$ for $w\in\Sigma^*$, $a\in\Sigma$, $a\neq c$
Define $\Sigma^*$ by
Basis: $\varepsilon \in \Sigma^*$, where $\varepsilon$ is the empty string.
Recursive: if $w\in\Sigma^*$ and $a\in\Sigma$, then $wa\in\Sigma^*$

### Rooted binary trees and functions on them

Rooted binary trees
Basis: $\Node\in S$
Recursive: if $L\in S$ and $R\in S$, then $\Tree(\Node, L, R) \in S$
Size of a rooted binary tree
$\Size{\Node} = 1$
$\Size{\Tree(\Node, L, R)} = 1 + \Size{L} + \Size{R}$
Height of a rooted binary tree
$\Height{\Node} = 0$
$\Height{\Tree(\Node, L, R)} = 1 + \max(\Height{L}, \Height{R})$

## Structural induction

A method for proving properties of recursive structures.

### How can we prove properties of recursive structures?

Suppose that $S$ is a recursively defined set.
And we want to prove that every element of $S$ satisfies a predicate $P$.
Can we use ordinary induction to prove $\forall x\in S. P(x)$?
Yes! Define $Q(n)$ to be “for all $x\in S$ that can be constructed in at most $n$ recursive steps, $P(x)$ is true.”
• But this proof would be long and cumbersome to do!
• So we use structural induction instead.
• Follows from ordinary induction (on $Q$), while providing a more convenient proof template for reasoning about recursive structures.
• As powerful as ordinary induction, which is just structural induction applied to the recursively defined set of natural numbers.

### Proving $\forall x\in S. P(x)$ by structural induction

① Let $P(x)$ be [ definition of $P(x)$ ].
We will show that $P(x)$ is true for every $x\in S$ by structural induction.
② Base cases:
[ Proof of $P(s_0), \ldots, P(s_m)$. ]
③ Inductive hypothesis:
Assume that $P(y_0), \ldots, P(y_k)$ are true for some arbitrary $y_0, \ldots, y_k \in S$.
④ Inductive step:
We want to prove that $P(y)$ is true.
[ Proof of $P(y)$. The proof must invoke the structural inductive hypothesis. ]
⑤ The result follows for all $x\in S$ by structural induction.

Recursive definition of $S$
Basis step: $s_0\in S, \ldots, s_m\in S$.
Recursive step:
if $y_0, \ldots, y_k\in S$, then $y\in S$.

If the recursive step of $S$ includes multiple rules for constructing new elements from existing elements, then
assume $P$ for the existing elements in every rule, and
prove $P$ for the new element in every rule.

## Using structural induction

Example proofs about recursively defined numbers, strings, and trees.

### Prove that every $x\in S$ is divisible by 3

① Let $P(x)$ be $3 \vert x$.
We will show that $P(x)$ is true for every $x\in S$ by structural induction.
② Base cases ($x=6$, $x=15$):
$3 \vert 6$ so $P(6)$ holds, and $3 \vert 15$ so $P(15)$ holds.
③ Inductive hypothesis:
Assume that $P(x), P(y)$ are true for some arbitrary $x,y \in S$.
④ Inductive step:
We want to prove that $P(x+y)$ is true.
By the inductive hypothesis, $3\vert x$ and $3\vert y$, so $x = 3i$ and $y = 3j$ for some $i,j\in\Z$. Therefore, $x + y = 3i + 3j = 3(i+j)$ so $3\vert (x+y)$. Hence, $P(x+y)$ is true.
⑤ The result follows for all $x\in S$ by structural induction.

Define $S$ by
Basis: $6\in S$, $15\in S$.
Recursive: if $x,y\in S$, then $x+y\in S$.

### Prove $\op{len}(x\bullet y) = \op{len}(x) + \op{len}(y)$ for all $x,y\in\Sigma^*$

What object ($x$ or $y$) to do structural induction on?

① Let $P(y)$ be $\forall x\in\Sigma^* . \op{len}(x\bullet y) = \op{len}(x) + \op{len}(y)$.
We will show that $P(y)$ is true for every $y\in \Sigma^*$ by structural induction.
② Base case ($y=\varepsilon$):
For every $x\in \Sigma^*$, $\op{len}(x\bullet \varepsilon)$ $=$ $\op{len}(x)$ $=$ $\op{len}(x) + \op{len}(\varepsilon)$ since $\op{len}(\varepsilon) = 0$. So $P(\varepsilon)$ is true.
③ Inductive hypothesis:
Assume that $P(w)$ is true for some arbitrary $w \in \Sigma^*$.
④ Inductive step:
We want to prove that $P(wa)$ is true for every $a\in\Sigma$.
Let $a\in\Sigma$ and $x\in\Sigma^*$ be arbitrary. Then
So $\op{len}(x\bullet wa)=\op{len}(x) + \op{len}(wa)$ for all $x\in\Sigma^*$, and $P(wa)$ is true.
⑤ The result follows for all $y\in \Sigma^*$ by structural induction.

Define $\Sigma^*$ by
Basis: $\varepsilon \in \Sigma^*$.
Recursive:
if $w\in\Sigma^*$ and $a\in\Sigma$,
then $wa\in\Sigma^*$
Length
$\op{len}(\varepsilon) = 0$
$\op{len}(wa) = \op{len}(w) + 1$
Concatenation
$x\bullet \varepsilon = x$
$x\bullet (wa) = (x\bullet w)a$

### Prove $\Size{t}\leq 2^{\Height{t}+1}-1$ for every rooted binary tree $t$

① Let $P(t)$ be $\Size{t}\leq 2^{\Height{t}+1}-1$.
We will show that $P(t)$ is true for every $t\in S$ by structural induction.
② Base case ($t=\Node$):
$\Size{\Node} = 1 = 2^1 - 1 = 2^{0+1}-1 = 2^{\Height{\Node}+1}-1$ so $P(\Node)$ is true.
③ Inductive hypothesis:
Assume that $P(L)$ and $P(R)$ are true for some arbitrary $L, R \in S$.
④ Inductive step:
We want to prove that $P(\Tree(\Node,L,R))$ is true.
⑤ The result follows for all $t\in S$ by structural induction.

Define $S$ by
Basis: $\Node \in S$.
Recursive:
if $L, R\in S$, then
$\Tree(\Node,L,R)\in S$
Size
$\Size{\Node} = 1$
$\Size{\Tree(\Node,L,R)} =$
$\quad 1 + \Size{L} + \Size{R}$
Height
$\Height{\Node} = 0$
$\Height{\Tree(\Node,L,R))} =$
$\quad 1 + \max(\Height{L}, \Height{R})$

## Summary

To define a set recursively, specify its basis and recursive step.
Recursive set definitions assume the exclusion rule.
We use recursive functions to operate on elements of recursive sets.
Use structural induction to prove properties of recursive structures.
Structural induction follows from ordinary induction but is easier to use.
To prove $\forall x\in S. P(x)$ using structural induction:
Show that $P$ holds for the elements in the basis step of $S$.
Assume $P$ for every existing element of $S$ named in the recursive step.
Prove $P$ for every new element of $S$ created in the recursive step.