CS243: Discrete Structures Structural Induction Review Strings and

Review
CS243: Discrete Structures
I
Recursive definitions define functions, sets, sequences, etc. in
terms of themselves
Structural Induction
I
Just like inductive proofs, recursive definitions consist of base
case and recursive step
I
Example: Recursive definition of the set of natural numbers N:
Işıl Dillig
0∈N
If x ∈ N, then x + 1 ∈ N
Işıl Dillig,
CS243: Discrete Structures
Structural Induction
Işıl Dillig,
1/30
Strings and Alphabets
Recursive definitions play important role in study of strings
I
Strings are defined over an alphabet Σ
I
Example: Σ1 = {a, b}
I
Example: Σ2 = {0}
I
Examples of strings over Σ1 : a, b, aa, ab, ba, bb, . . .
I
Set of all strings formed from Σ forms language called Σ∗
2/30
I
The language Σ∗ has natural recursive definition:
I
Base case: ∈ Σ∗ (empty string)
I
Recursive step: If w ∈ Σ∗ and x ∈ Σ, then wx ∈ Σ∗
I
Consider the alphabet Σ = {a, b}
I
How is the string ”abb” formed according to this definition?
Σ∗2 = {, 0, 00, 000, . . .}
Işıl Dillig,
CS243: Discrete Structures
Structural Induction
Işıl Dillig,
3/30
Recursive Definitions of String Operations
Işıl Dillig,
Structural Induction
Recursive Definition of Strings
I
I
CS243: Discrete Structures
CS243: Discrete Structures
Structural Induction
Another Example
I
Many operations on strings can be defined recursively.
I
The reverse of a string s is s written backwards.
I
Consider function l (w ) which yields length of string w
I
Example: Reverse of ”abc” is ”bca”
I
Example: Give recursive definition of l (w )
I
Give a recursive definition of the reverse(s) operation
I
Base case:
I
Base case:
I
Recursive step:
I
Recursive step:
CS243: Discrete Structures
Structural Induction
4/30
5/30
Işıl Dillig,
1
CS243: Discrete Structures
Structural Induction
6/30
Palindromes
Structural Induction Motivation
I
We’ve illustrated many recursively defined sets and structures
I
A palindrome is a string that reads the same forwards and
backwards
I
But how do we prove universally quantified properties about
these structures?
I
Examples: ”mom”, ”dad”, ”abba”, ”Madam I’m Adam”, . . .
I
When we proved properties about recursively defined functions
f (n), we performed induction on integer n
I
Give a recursive definition of the set P of all palindromes over
the alphabet Σ = {a, b}
I
But if we are talking about sets or strings, there is no explicit
integer n to perform induction on
I
Example:
I
Base cases:
I
Recursive step:
Işıl Dillig,
3∈S
If x ∈ S , and y ∈ S , then x + y ∈ S
CS243: Discrete Structures
Structural Induction
7/30
Işıl Dillig,
Structural Induction to the Rescue
CS243: Discrete Structures
Stuctural induction is a technique that allows us to apply
induction on recursive definitions even if there is no integer
I
Structural induction is also no more powerful than regular
induction, but can make proofs much easier
I
I
CS243: Discrete Structures
Structural Induction
9/30
I
a property P we’d like to prove about S
Structural induction works as follows:
CS243: Discrete Structures
Structural Induction
Consider the following recursively defined set S :
I
Consider the set S defined recursively as follows:
I
Base case: 3 ∈ S
I
Recursive step: If x ∈ S and y ∈ S , then x + y ∈ S
Prove by structural induction that every element in S contains
an equal number of right and left parantheses.
I
Prove S is set of all positive integers that are multiples of 3
I
Base case: a has 0 left and 0 right parantheses
I
Let A be the set of all positive integers divisble by 3
I
Inductive step: By the inductive hypothesis, x has equal
number, say n, of right and left parantheses.
I
We want to show that A = S
I
To do this, we need to prove S ⊆ A and A ⊆ S
I
10/30
Example 2
2. If x ∈ S , then (x ) ∈ S
Işıl Dillig,
a recursively defined structure S
Işıl Dillig,
1. a ∈ S
I
I
2. Inductive step: Assuming P holds for sub-structures used in
the recursive step of the definition, show that P holds for the
recursively constructed structure.
Example 1
I
Suppose we have:
1. Base case: Prove P about base case in recursive definition
Called structural induction because we do induction on the
structure of the definition
Işıl Dillig,
8/30
Structural Induction Overview
I
I
Structural Induction
Thus, (x ) has n + 1 left and n + 1 right parantheses.
CS243: Discrete Structures
Structural Induction
Işıl Dillig,
11/30
2
CS243: Discrete Structures
Structural Induction
12/30
Proof, Part I
Proof, Part II
Consider the set S defined recursively as follows: 3 ∈ S and if
x ∈ S and y ∈ S , then x + y ∈ S
I
We showed that all integers in S are multiples of 3, but still
need to show S includes all positive multiples of 3
I
Therefore, need to prove that 3n ∈ S for all n ≥ 1
I
We’ll prove this by strong induction on n:
I
Let’s first prove S ⊆ A, i.e., any element in S is divisible by 3
I
For this, we’ll use structural induction
I
Base case (n=1):
I
Base case:
I
Inductive hypothesis:
I
Inductive step:
I
Need to show:
Işıl Dillig,
CS243: Discrete Structures
Structural Induction
Işıl Dillig,
13/30
Proving Correctness of Reverse
I
I
Structural Induction
14/30
Proof of Correctness of Reverse, cont.
Earlier, we defined a reverse(w ) function for length of strings:
P (y) : ∀x ∈ Σ∗ . reverse(xy) = reverse(y) · reverse(x )
I
Base case: reverse() = I
Recursive step: reverse(wx ) = x · reverse(w ) where w ∈ Σ∗
and x ∈ Σ
I
Base case:
I
Need to show:
I
What is reverse(x · )?
∀x ∈ Σ∗ . reverse(xy) = reverse(y) · reverse(x )
I
What is reverse() · reverse(x )?
We’ll prove by structural induction that ∀y ∈ Σ∗ . P (y) holds, which
is the desired property
I
Thus, P (y) holds for base case
I
Prove ∀x , y ∈ Σ∗ . reverse(xy) = reverse(y) · reverse(x )
I
Let P (y) be the property
I
CS243: Discrete Structures
Işıl Dillig,
CS243: Discrete Structures
Structural Induction
Işıl Dillig,
15/30
Proof of Correctness of Reverse, cont.
CS243: Discrete Structures
Structural Induction
16/30
One More Reverse Example
P (y) : ∀x ∈ Σ∗ . reverse(xy) = reverse(y) · reverse(x )
Işıl Dillig,
I
Inductive step: y = za where z ∈ Σ∗ and a ∈ Σ
I
Want to show:
I
reverse(xza) =
I
By the inductive hypothesis, reverse(xz ) =
I
Thus, a · reverse(xz ) = a · reverse(z ) · reverse(x )
I
By definition, a · reverse(z ) =
I
Hence, reverse(xza) = reverse(za) · reverse(x )
CS243: Discrete Structures
Structural Induction
Işıl Dillig,
17/30
3
I
Prove that reverse(reverse(s)) = s
I
We’ll prove this by structural induction
I
But need previous lemma for the proof to go through!
I
Base case:
I
Need to show:
I
reverse(reverse()) = reverse() = CS243: Discrete Structures
Structural Induction
18/30
One More Reverse Example, cont.
I
Inductive step: s = wx where w ∈ Σ , x ∈ Σ
I
Want to show:
I
Using previously shown lemma,
Structural vs. Strong Induction
∗
reverse(reverse(wx )) = reverse(reverse(x ) · reverse(w ))
I
I
Structural induction may look different from other forms of
induction, but it is an implicit form of strong induction
I
Intuition: We can define an integer k that represents how
many times we need to use the recursive step in the definition
I
For base case, k = 0; if we use recursive step once, k = 1 etc.
I
Viewed this way, structural induction is just strong induction
I
In inductive step, assume P (i ) for 0 ≤ i ≤ k and prove
P (k + 1)
I
But when using structural induction, you don’t have to make
this argument in every proof!
Again, using previous lemma,
reverse(reverse(x ) · reverse(w )) =
I
By inductive hypothesis, reverse(reverse(w )) =
I
Also by IH, reverse(reverse(x )) =
I
Thus, reverse(reverse(w )) · reverse(reverse(x )) = wx
Işıl Dillig,
CS243: Discrete Structures
Structural Induction
19/30
Işıl Dillig,
Applications of Structural Induction
CS243: Discrete Structures
Structural induction will come up over and over again in CS
classes
I
Algorithms: proving properties about data structures (e.g.,
trees)
I
Programming languages: proving properties about type
systems
I
Automata theory: Proving properties about regular and
context-free languages
Işıl Dillig,
CS243: Discrete Structures
Structural Induction
So far, saw three types of induction:
1. Mathematical (standard) induction
2. Strong induction
3. Structural induction
21/30
I
(1), (2): used to prove properties about Z+ , N
I
(3): proofs about recursively defined structures
I
But all of them are special instances of a more general
principle of induction
Işıl Dillig,
Well-Ordered Sets
Işıl Dillig,
20/30
Mathematical vs. Strong vs. Structural Induction
I
I
Structural Induction
CS243: Discrete Structures
Structural Induction
22/30
Generalized Induction
I
Inductive proofs can be used for any well-ordered set
I
More on these later, but intuitively, well-ordered sets:
I
Can use induction to prove properties of any well-ordered set:
1. have a least element
I
Base case: Prove property about least element in set
2. total order between elements: either a ≤ b or b ≤ a
I
Inductive step: To prove P (e), assume P (e 0 ) for all e 0 < e
I
Example: Z+ is well-ordered set with least element 1
I
Example: Even positive integers is well-ordered set with least
element 2
CS243: Discrete Structures
Structural Induction
I
23/30
Işıl Dillig,
4
Mathematical induction is just a special case of this
CS243: Discrete Structures
Structural Induction
24/30
Ordered Pairs of Natural Numbers
Generalized Induction Example
I
I
Consider the set N × N, pairs of non-negative integers
I
Let’s define the following order ≤ on this set:
x1 < x2
(x1 , y1 ) ≤ (x2 , y2 ) if
or x1 = x2 ∧ y1 ≤ y2
I
This is an example of lexicographic order, which is a kind of
total order
I
Therefore, (N × N, ≤) is a well-ordered set
I
Question: What is the least element of this set?
Işıl Dillig,
CS243: Discrete Structures
Structural Induction
a0,0
am,n
25/30
I
I
Proof is by induction on (m, n) where (m, n) ∈ (N × N, ≤)
I
Base case:
I
By recursive definition, a0,0 = 0
I
0 + 0 · 1/2 = 0; thus, base case holds.
CS243: Discrete Structures
a0,0
am,n
I
j (j + 1)
2
Want to show:
CS243: Discrete Structures
Structural Induction
27/30
am,n
26/30
Since recursive step of definition has two cases, we need to do
proof by cases:
I
Case 1: k2 = 0, k1 > 0
I
Case 2: k2 > 0
CS243: Discrete Structures
Structural Induction
28/30
Example, cont.
Show am,n = m + n(n + 1)/2 for:
Show am,n = m + n(n + 1)/2 for:
a0,0
Structural Induction
= 0
am−1,n + 1 if n = 0 and m > 0
=
am,n−1 + n if n > 0
Işıl Dillig,
Example, cont.
a0,0
= 0
am−1,n + 1 if n = 0 and m > 0
=
am,n−1 + n if n > 0
I
Case 1: k2 = 0, k1 > 0. Then, ak1 ,k2 = ak1 −1,k2 + 1
I
Since (k1 − 1, k2 ) < (k1 , k2 ), inductive hypothesis applies.
I
By the IH, we know:
ak1 −1,k2 = k1 − 1 +
Işıl Dillig,
Show that am,n = m + n(n + 1)/2
Show am,n = m + n(n + 1)/2 for:
Inductive hypothesis: For all (0, 0) ≤ (i , j ) < (k1 , k2 ):
Işıl Dillig,
I
I
= 0
am−1,n + 1 if n = 0 and m > 0
=
am,n−1 + n if n > 0
ai,j = i +
I
if n = 0 and m > 0
if n > 0
Example, cont.
Show am,n = m + n(n + 1)/2 for:
am,n
= 0
am−1,n + 1
=
am,n−1 + n
Işıl Dillig,
Inductive Step
a0,0
Suppose that am,n is defined recursively for (m, n) ∈ N × N:
k2 (k2 + 1)
2
But then ak1 ,k2 = ak1 −1,k2 + 1 = k1 +
CS243: Discrete Structures
am,n
k2 (k2 +1)
2
Structural Induction
29/30
Işıl Dillig,
5
= 0
am−1,n + 1
=
am,n−1 + n
if n = 0 and m > 0
if n > 0
I
Case 2: k2 > 0. Then, ak1 ,k2 = ak1 ,k2 −1 + k2
I
Since (k1 , k2 − 1) < (k1 , k2 ), inductive hypothesis applies.
I
By the IH, we know: ak1 ,k2 −1 =
I
But then ak1 ,k2 = k1 +
I
ak1 ,k2 = k1 +
k22 −k2 +2k2
2
k2 (k2 −1)
2
= k1 +
+ k2
k2 (k2 +1)
2
CS243: Discrete Structures
Structural Induction
30/30