Lecture Notes: Structural Induction
Preliminaries
- Name tags
- Sign in
- Announce Quiz 2
Course Content
Review 1
Question: Consider the function: \( f(x) = \frac{1}{\vert x \vert} \) for \( f: ? \rightarrow \mathbb{R} \)
- For what domain is this function defined?
- What is this function’s image?
- Is this function injective? Explain.
- Is this function surjective? Explain.
- If this function is injective, what is its inverse?
Answer:
- Domain: \( \mathbb{R} - \{ 0 \} \)
- Image: \( \{ y \in \mathbb{R} : y > 0 \} \)
- The function is not injunctive. For example: \( f(1) = f(-1) = 1 \)
- The function is not surjective, because the image does not equal the codomain.
- The function is not injective, so it does not have an inverse. The inverse on the image would be: \( f^{-1}(y) = \frac{1}{y} \)
Review 2
Review Written Assignment 6.
Definition of Structural Induction
- So far, our induction proofs have always had an integer “induction variable.”
- In Computer Science, it is useful to write proofs about nonnumerical mathematical objects. For example, strings, arrays, and other data structures.
- Write: Definition: Objects are built up from smaller objects, or are irreducible and atomic.
- For example:
- A string is composed of characters, which are atomic.
- An integer array is composed of integers, which are atomic.
- Write: Definition: In structural induction, base case values are atomic objects. Induction constructs larger objects from smaller objects for which a predicate is already known to be true.
Return of the Thue Sequence
- Remember the Thue Sequence from earlier in the course?
- The Thue sequence describes a sequence of binary strings.
- Write: The Thue sequence is defined as \(T_0 = 0\) and for any \(n \geq 0\),
\[ T_{n+1} = \text{ the concatenation of } T_n \text{ and the complement of } T_n\]
- But what are binary strings? How can they be defined mathematically?
- We will use a mathematical definition of strings to demonstrate structural induction.
Defining Strings
- Write: Definition: An alphabet is any finite set. We refer to members of an alphabet as symbols.
- Examples:
- Binary alphabet: {0, 1}
- Roman alphabet: {a, b, …, y, z, A, B, …, Y, Z}
- Write: Definition: If \( \Sigma \) is an alphabet, then \( \Sigma^* \) is the set of strings over \( \Sigma \), the Kleene star of \( \Sigma \). The set \( \Sigma^* \) is defined as follows:
Base Case (S1): The empty string \( \lambda \) is a string in \( \Sigma^* \).
Constructor Case (S2): If \( s \) is a string in \( \Sigma^* \) and \( a \) is a symbol in \( \Sigma \), then \( \langle a, s \rangle \) is a member of \( \Sigma^* \).
Exhaustion Clause (S3): Nothing else is in \( \Sigma^* \) except as follows from the base and constructor cases.
- Note: the exhaustion clause can be omitted, but the base case and constructor case are required.
- Let’s write some binary strings according to this definition.
- Ask: How to write:
- 0 is \( \langle 0, \lambda \rangle \)
- 1 is \( \langle 1, \lambda \rangle \)
- 10 is \( \langle 1, \langle 0, \lambda \rangle \rangle \)
- 110 is \( \langle 1, \langle 1, \langle 0, \lambda \rangle \rangle \rangle \)
Defining String Operations
- We can give a formal definition of string length.
- Write: Definition: String length
Base Case: \( \lambda = 0 \)
Constructor Case: For any \( a \in \Sigma \) and \( s \in \Sigma^* \), \( \vert \langle a, s \rangle \vert = \vert s \vert + 1 \)
- Use earlier examples of binary strings to illustrate string length.
- Next, a formal definition of string concatenation.
- Write: Definition: String concatenation
Base Case (SC1): If \( t \) is any string in \( \Sigma^* \), then \( \lambda \cdot t = t \)
Constructor Case (SC2): For any strings \( s, t \in \Sigma^* \) and any symbol \( a \in \Sigma \),
\[ \langle a, s \rangle \cdot t = \langle a, s \cdot t \rangle \]
- Let’s see an example of string concatenation.
- To concatenate \( 11 \cdot 00 \), first write them as strings in \( \Sigma^* \).
- Write: \( 11 = \langle 1, \langle 1, \lambda \rangle \rangle \) and \( 00 = \langle 0, \langle 0, \lambda \rangle \rangle \)
\[\begin{aligned}
11 \cdot 00 &= \langle 1, \langle 1, \lambda \rangle \rangle \cdot \langle 0, \langle 0, \lambda \rangle \rangle \\
&= \langle 1, \langle 1, \lambda \rangle \cdot \langle 0, \langle 0, \lambda \rangle \rangle \rangle & \text{ (by SC2)} \\
&= \langle 1, \langle 1, \lambda \cdot \langle 0, \langle 0, \lambda \rangle \rangle \rangle \rangle & \text{ (by SC2)} \\
&= \langle 1, \langle 1, \langle 0, \langle 0, \lambda \rangle \rangle \rangle \rangle & \text{ (by SC1)} \\
&= 1100 \\
\end{aligned}\]
String Associativity by Structural Induction
- Recall the associative law for addition: \( (x + y) + z = x + (y + z) \)
- We will prove the associativity of string concatenation: \( (s \cdot t) \cdot u = s \cdot (t \cdot u) \)
- For example:
- \( (1 \cdot 00) \cdot 11 = 1 \cdot (00 \cdot 11) \) = 10011
- Write: Theorem: String concatenation is associative. That is, for any strings \( s, t, u \):
\[ (s \cdot t) \cdot u = s \cdot (t \cdot u) \]
- Write: Proof:
- Base case: \( s = \lambda \). Then:
\[\begin{aligned}
(\lambda \cdot t) \cdot u &= (t) \cdot u & \text{ (by SC1)}\\
&= (t \cdot u) \\
&= \lambda \cdot (t \cdot u) & \text{ (by SC1)}\\
\end{aligned}\]
- Induction hypothesis: \( s \cdot (t \cdot u) = (s \cdot t) \cdot u \text{ for string } s \)
- Induction step: Consider \( s’ = \langle a, s \rangle \). We will prove that if our I.H. is true, then \( s’ \cdot (t \cdot u) = (s’ \cdot t) \cdot u \) must be true.
\[\begin{aligned}
(s' \cdot t) \cdot u &= (\langle a, s \rangle \cdot t) \cdot u & \text{ (substituting)}\\
&= \langle a, s \cdot t \rangle \cdot u & \text{ (by SC2)}\\
&= \langle a, (s \cdot t) \cdot u \rangle & \text{ (by SC2)}\\
&= \langle a, s \cdot (t \cdot u) \rangle & \text{ (by IH)}\\
&= \langle a, s \rangle \cdot (t \cdot u) & \text{ (by SC2)}\\
&= s' \cdot (t \cdot u) & \text{ (substituting)}\\
\end{aligned}\]
- We have proven the associativity of string concatenation for strings of arbitrary size.
- Alternatively, we could have used conventional induction, with string length as our induction variable.
Structural Induction Schema
- Write: Structural Induction Schema
- Suppose a set S is inductively defined as:
- Base case: Certain base elements b are members of S.
- Constructor case: Certain constructor operations c produce more elements of S from elements already known to be in S. That is, c is a k-place constructor operation with the property that if \( x_1, \ldots, x_k \) are members of S, then \( c(x_1, \ldots, x_k) \in S \).
- Exhaustion clause: Nothing else is in S except as follows from the base case and constructor cases.
- Our definition of strings follows this schema.
- Ask: For the set of strings:
- What is the base element? empty string \( \lambda \)
- What are the constructor operations? The 1-place constructor \( \langle a, s \rangle \) for symbol a and string s.
- To prove that some predicate P holds for all \( x \in S \), it suffices to prove:
- Base case: P(b) holds for each base element \( b \in S \)
- Induction hypothesis: For each k-place constructor c, assume that \( P(x_1), \ldots, P(x_k) \) hold for \( x_1, \ldots, x_k \in S \).
- Induction step: Given the I.H., show that \( P(c(x_1, \ldots, x_k)) \) must hold.
- Ask: For our proof of string concatenation’s associativity:
- What was our predicate? \( (s \cdot t) \cdot u = s \cdot (t \cdot u) \text{ for } s, t, u \in S \)
- What did we show in our induction step? That the predicate holds for \( s’ = \langle a, s \rangle \)
- Note the textbook starts merges the I.H. and I.S. together. For clarity, I will keep them separate.
- This structural induction schema can be explained using regular induction:
- The base case is the first rung of the ladder, where the constructor operations have not been applied.
- In the induction step, we prove we can go up a rung by using the constructor operations.
- The induction variable is the number of times the constructor operations were applied.
Balanced Strings of Parentheses (BSPs)
- Consider the expression:
\[ (1 + 2) / (3 * (4 + 5)) \]
- When evaluating, the parentheses take precedence in the order of operations
- What is wrong with this expression?
\[ 1 + 2) / ((3 * (4 + 5)) \]
- The parentheses are not balanced. How can we express this mathematically?
- We will simplify things by just considering an alphabet of parentheses, which we will use to form balanced strings of parentheses.
- Write: Balanced Strings of Parentheses (BSPs)
- Alphabet: {(, )}
- Base case: The empty string \( \lambda \) is a BSP.
- Constructor cases:
(C1) If x is any BSP then so is (x). That is, the result of putting “(” before x and “)” after x.
(C2) If x and y are BSPs, then so is xy, the result of concatenating x and y.
- Let’s use the definition to form “(()())” and show it is a BSP:
\[\begin{aligned}
\text{Known BSPs} \\
\{ \lambda \} & \text{ by base case} \\
\{ \lambda, () \} & \text{ by applying C1} \\
\{ \lambda, (), ()() \} & \text{ by applying C2} \\
\{ \lambda, (), ()(), (()()) \} & \text{ by applying C1} \\
\end{aligned}\]
BSP Proof 1
- Next, we will use structural induction to prove some theorems about BSPs
- Write: Theorem: Every BSP has equal numbers of left and right parenthesis.
- Write: Proof:
- Base case: The empty string \( \lambda \) has zero left and right parentheses, and thus equal numbers of each.
- Induction hypothesis: Assume that BSPs x and y have equal numbers of left and right parentheses.
- Induction step for C1: Suppose the last step of constructing BSP z used constructor case C1. Then z = (x) for some BSP x that was constructed using fewer steps. By the I.H., x has equal numbers of left and right parentheses. Say x has n of each; then z has \( n + 1 \) left parentheses and \( n + 1 \) right parentheses, and so has equal numbers of each.
- Induction step for C2: Suppose the last step of constructing BSP z used constructor case C2. Then z = xy for some BSPs x and y, each of which was constructed using fewer steps. By the I.H., x and y each have equal numbers of left and right parentheses. If x has n left and n right parentheses and y has m left and m right parentheses, then z has \( n + m \) left and \( n + m \) right parentheses – equal numbers.
BSP Proof 2
- Balancing parentheses is important when programming. You can use this counting rule to check that your parentheses are balanced.
- Write: Definition: Counting Rule
“A string of parentheses is said to satisfy the counting rule if starting from 0 at the left end, adding 1 for each left parenthesis and subtracting 1 for each right parenthesis, gives 0 at the end of the string, without ever going negative.”
- Write: Theorem: A string of parentheses is balanced if and only if it satisfies the counting rule.
- First we show that if x is any balanced string of parentheses, then x satisfies the counting rule. This is a structural induction.
- Base case: \( x = \lambda \). Then the count begins at 0 and ends immediately, satisfying the counting rule.
- Induction step, case 1: x = (y) where y is balanced, and by the I.H. satisfies the counting rule. Then the count for x is +1 after the first symbol, +1 again after the end of y, stays positive in between, and ends at 0 after the final right parenthesis.
- Induction step, case 2: x = yz where y and z are balanced and by the I.H. satisfy the counting rule. Then the count goes from 0 at the beginning to 0 after y to zero again after z, without ever going negative in the middle of y or z.
- Next, we must prove the equivalence in the other direction: If x satisfies the counting rule, then x is balanced. We will perform strong induction on the length of x.
- Base cases: \( \vert x \vert = 0 \). Then \( x = \lambda \), which satisfies the counting rule and is balanced.
- Ask: Can we prove the predicate for \( \vert x \vert = 1 \)?
- \( \vert x \vert = 1 \). x doesn’t satisfy the counting rule, because with only one parenthesis the count will be +1 or -1. So we make no claim about x being balanced.
- Induction hypothesis: Fix \( n \geq 1 \) and assume that for any \( m \leq n \) and any string y of length m, if y satisfies the counting rule, then y is balanced.
- Induction step: Let x be a string of length \( n + 1 \) that satisfies the counting rule. There are two possibilities:
- Induction step, case 1: The count is never 0 except at the beginning and end of x. Then \( x = (y) \) for some string y of length \( \vert x \vert - 2 \), since the count when from 0 to +1 after the first symbol and went from +1 to 0 after the last symbol. So y satisfies the counting rule and by the I.H. is balanced. By constructor C1, \( x = (y) \) is also balanced.
- Induction step, case 2: The count reaches 0 at some point between the beginning and end of x. Then we can write \(x = yz\), where y and z are nonempty and the count goes to zero after y. Then y and z are each shorter than x and each satisfies the counting rule, so by the I.H. each is balanced. Then \(x = yz\) is balanced by constructor C2.
- Note: I think the textbook authors made an error in the bounds for their I.H.