



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




This lecture finishes the discussion of recursive definition and structural induction (Rosen 4.3)
At the end of last class, we saw how to define a set of numbers recursively. We can use the same method to define more interesting sorts of sets. For exam- ple, we can define all the legal numerical expressions for some programming language. (These are all intended to be strings of characters.)
(X + Y ) (X − Y ) (X × Y ) (X/Y )
Some sample base-case expressions are b, r, 7, 0. If we apply the inductive rules one time, we get expressions like (m + n) and (7 + q). If we apply the inductive rules more than once, we get expressions like (m + (7/9)) and ((c + d) + ((a + 5) × 8)). Notice that the parentheses are obligatory, because that’s how we’ve set up our inductive rules. This little bitty expression grammar is much simpler than the expression grammar for any actual programming language. For example, real languages
allow multi-character numbers and variable names. However, the real gram- mars are defined in a similar way. Let’s show that expressions generated by this grammar always contain equal numbers of left and right parentheses. We’ll use a technique called structural induction. Structural induction is like normal induction, but there’s no visible induction variable n. Rather, the proof follows the structure of the recursive definition. Suppose we want to prove that P (x) is true for every x defined by some recursive definition D.
For example, here’s a proof of our claim about the parentheses:
Proof: by structural induction on the definition of numerical ex- pression. Base: Single digits and lower-case letters do not contain any parentheses, so the number of left parentheses and the number of right parenthese are both zero. Induction: Suppose that the claim is true for X and Y. That is, X contains the same number of left and right parentheses, and so does Y. Suppose that X contains p left parentheses and Y contains q. Then, the expression (X + Y ) contains p left parentheses from X, q from Y , and one new one. That is, p + q + 1 left parentheses. Similarly, it contains p + q + 1 right parentheses. So the two are equal. The same line of reasoning works for the other recursive cases: (X − Y ), (X × Y ), and (X/Y ).
You may be wondering about the missing induction variable n. Didn’t we say that induction needed an integer? If we wanted to make an explicit integer for this example, we would let n be the number of rule applications
Proof by structural induction. Base: If a tree contains only one node, obviously the largest value in the tree lives in the root! Induction: Suppose that the claim is true for trees X and Y. We need to show that the claim is also true for T = X · Y. Let p, q, and r be the root nodes of X, Y , and T. p and q are the children of r. Since T has the heap property, v(r) ≥ v(p) and v(r) ≥ v(q). By the inductive hypothesis, v(p) is ≥ the value in each node of X and v(q) is ≥ the value in each node of Y. So v(r) ≥ the value in each node of X and each node of Y. The nodes of T are the nodes of X, plus the nodes of Y , plus r. So v(r), the value at the root node of T , is at least as big as the value at any node of T.
In the inductive step, notice that we split up the big tree (T ) at its root, producing two smaller subtrees (X) and (Y ). Some students try to do induction on trees by grafting stuff onto the bottom of the tree. This frequently does not work, especially as you get to examples in more advanced courses. Therefore, we will take off points if you do it on homework or tests.
3 An example with 2D points
Consider the following recursive definition of a set S of 2D points:
What’s in S? Starting with the pair specified in the base case (3, 5), we use rule 3 to add (− 3 , 5). Rule 2 then allows us to add (− 1 , 5) and then (1, 5). If we apply rule 2 repeatedly, we see that S contains all pairs of the form (2n + 1, 5) where x is a natural number. By rule 4, (5, 2 n + 1) must also be in S for every natural number n.
We then apply rules 2 and 3 in the same way, to show that (2m+1, 2 n+1) is in S for every natural numbers m and n. We could make this more formal using a proof by induction, but let’s not bother. The big missing piece of this analysis is showing that S doesn’t contain any other points other than those with odd coordinates. Think of this as proving a set equality. Let T be the set of 2D points with odd coordinates. We’ve shown that T ⊆ S and we still need to show that S ⊆ T. We can do this by structural induction.
Proof by structural induction that all elements of S have both coordinates odd. Base: Both coordinates of (3, 5) are odd. Induction: Suppose that both coordinates of (x, y) are odd. We need to show that both coordinates of (x + 2, y), (−x, y), and (y, x) are odd. But this is (even in the context of this course), obvious.
In this case, the second half of the proof was very easy because we used structural induction. Two things were important. First, it was critical that we did do this second half, otherwise we couldn’t claim to have completely nailed down what’s in S. Second, we needed generate the outline of structural induction and verify the base case and inductive step.
4 Another tree example
I didn’t get to this example in lecture, but it’s a good one to read through. It’s another structural induction on trees, with a slightly more complex claim. Let T be a full binary tree. Suppose that n(T ) is the number of nodes in T and h(T ) is the height of T. Let’s show that n(T ) ≤ 2 h(T^ )+1^ − 1.
Proof by structural induction. Base: Trees containing a single node have no edges and therefore height 0. Notice that n(T ) = 1 and 2h(T^ )+1^ − 1 = 2^1 − 1 = 1. Induction: Let X and Y be full binary trees. Suppose that the claim is true for X and Y. We need to show that the claim is also true for T = X · Y.