Presentation is loading. Please wait.

Presentation is loading. Please wait.

and Structural Induction

Similar presentations


Presentation on theme: "and Structural Induction"— Presentation transcript:

1 and Structural Induction
Mathematics for Computer Science MIT 6.042J/18.062J Recursive Definitions and Structural Induction Copyright © Albert Meyer, 2002. Prof. Albert Meyer & Dr. Radhika Nagpal

2 Build something from a simpler version of the same thing:
Recursive Definitions Build something from a simpler version of the same thing: Base case(s) that don’t depend on anything else Induction (Construction) case(s) that depend on simpler cases

3 Example Definition: set E
Define set E  Z, recursively: 0  E Base Case If n  E, then n + 2  E Induction case If n  E, then -n  E E : 0 , 0+2

4 Example Definition: set E
Define set E  Z, recursively: 0  E Base Case If n  E, then n + 2  E Induction case If n  E, then -n  E E : 0, 2, 2+2 , (2+2)+2

5 Example Definition: set E
Define set E  Z, recursively: 0  E If n  E, then n + 2  E If n  E, then -n  E E : 0, 2, 4, 6, … All even numbers Also, -2, -4, -6, …

6 Recursive Definition: Extremal Clause
So, E contains the even integers Anything Else? No! 0  E If n  E, then n + 2  E If n  E, then -n  E That’s All! Implicit part of recursive definition

7 Example Definition: set E
So E is exactly: The Even Integers

8 l  S, (the empty string) axb bxa xy
Another Example Define set of strings, S  {a, b}* l  S, (the empty string) If x, y  S, then the following are in S: axb bxa xy

9 Proof by Structural Induction on S
Lemma: Every x in S has an equal number of a’s and b’s.

10 Structural Induction on S
Proof: Let EQ ::= {strings with equal # of a’s and b’s} P(x) ::= x  S  x  EQ Base Case: x = l. 0 a’s and 0 b’s. OK

11 Structural Induction on S
Inductive Step Assume: P(x) and P(y) To Prove: P(axb), P(bxa), and P(xy) Case 1: axb has k + 1 a’s and b’s if x has k of each. OK

12 Structural Induction on S
Inductive Step Assume: P(x) and P(y) To Prove: P(axb), P(bxa), and P(xy) Case 2: bxa SAME Case 3: xy has kx + ky of each. OK So S  EQ

13 Structural Induction on S
Rest of Story: Theorem: S = EQ Show by strong induction on length of string in EQ. Proof in Notes 5.

14 In-Class Problem Problem 1

15 Recursive Data Types Rooted Binary Trees

16 Defined recursively as follows: A single node r is an RBT:
Rooted Binary Trees Defined recursively as follows: A single node r is an RBT:

17 If t, t’ are RBTs, then so are :
Rooted Binary Trees If t, t’ are RBTs, then so are : makeleft(t) t makeright(t) t makeboth(t,t’) t t’

18 A Rooted Binary Tree Example

19 Recursive Functions on RBT
Recursive def of set of nodes nodes( )::= { }, nodes(makeleft(t))::= nodes(t) { } nodes(makeright(t))::= nodes(t){ } nodes(makeboth(t1, t2))::= nodes(t1)  nodes(t2){ } Remember, is not supposed to be in nodes(t)

20 Example = {{ }, { }, { }, { }, { }, { }, { }, { }, { } } nodes( )

21 Recursive Functions on RBT
depth( )::= 0 depth(makeleft(t))::= 1 + depth(t) depth(makeright(t))::= 1 + depth(t) depth(makeboth(t1, t2))::= 1 + max(depth(t1),depth(t2))

22 Example = 4 depth( )

23 Number of nodes vs Depth
Lemma: |nodes(t)| + 1  2depth(t)+1 Proof by Structural Induction Base Case: t = |nodes(t)| + 1 = 2 = 20+1 = 2depth(t)+1 OK!

24 Number of nodes vs Depth
Induction Case: makeleft(t) Assume: |nodes(t)| + 1  2depth(t) +1 To Prove: |nodes(makeleft(t))| + 1  2depth(makeleft(t)) + 1 |nodes(makeleft(t))| + 1 = (|nodes(t)|+1) + 1 by def. of nodes  (2depth(t)+1 + 1) + 1 by induction hyp. = 2depth(t)  2depth(t)+1 + 2depth(t)+1 = 2depth(t)+1+1 = 2depth(makeleft(t))+1 by def. of depth

25 Number of nodes vs Depth
Induction Case: makeright(t) Same

26 Number of nodes vs Depth
Induction Case: makeboth(t1, t2) Assume: |nodes(t1)|+1  2depth(t1)+1 |nodes(t2)|+1  2depth(t2)+1 To Prove: |nodes(makeboth(t1, t2))| +1  2depth(makeboth(t1,t2))+1

27 |nodes(makeboth(t1,t2))| + 1
Proof of makeboth Case |nodes(makeboth(t1,t2))| + 1 = (|nodes(t1)| + |nodes(t2)| + 1)+1 def. of nodes = |nodes(t1)| +1 + (|nodes(t2)| +1)  2depth(t1)+1 + 2depth(t2)+1 induction hyp.  2max(depth(t1),depth(t2)) max(depth(t1),depth(t2))+1 = 2(max(depth(t1),depth(t2))+1)+1 = 2depth(makeboth(t1,t2)) def. of depth

28 In-Class Problem Problems 2 & 3


Download ppt "and Structural Induction"

Similar presentations


Ads by Google