Mathematics for Computer Science MIT 6 042 J18

  • Slides: 35
Download presentation
Mathematics for Computer Science MIT 6. 042 J/18. 062 J Recursive Definitions and Structural

Mathematics for Computer Science MIT 6. 042 J/18. 062 J Recursive Definitions and Structural Induction Copyright © Albert Meyer, 2002. Prof. Albert Meyer & Dr. Radhika Nagpal March 6, 2002 1

Recursive Definitions Build something from a simpler version of the same thing: • Base

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 March 6, 2002 2

Example Definition: set E Define set E Z, recursively: • 0 E Base Case

Example Definition: set E Define set E Z, recursively: • 0 E Base Case • If n E, then n + 2 E • If n E, then -n E E: Induction case 0, 0+2 March 6, 2002 3

Example Definition: set E Define set E Z, recursively: • 0 E • If

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, 2+2, (2+2)+2 March 6, 2002 4

Example Definition: set E Define set E Z, recursively: • 0 E • If

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: Also, 0, 2, 4, 6, … All even numbers -2, -4, -6, … March 6, 2002 5

Recursive Definition: Extremal Clause So, E contains the even integers Anything Else? No! •

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 Implicit part of recursive definition: THAT’S ALL! March 6, 2002 6

Example Definition: set E So E is exactly: The Even Integers March 6, 2002

Example Definition: set E So E is exactly: The Even Integers March 6, 2002 7

Another Example Define set of strings, S {a, b}* • l S, (the empty

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 March 6, 2002 8

Proof by Structural Induction on S Lemma: Every x in S has an equal

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

Structural Induction on S Proof: Let EQ : : = {strings with equal #

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

Structural Induction on S Inductive Step Assume: P(x) and P(y) To Prove: P(axb), P(bxa),

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 March 6, 2002 11

Structural Induction on S Inductive Step Assume: P(x) and P(y) To Prove: P(axb), P(bxa),

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 March 6, 2002 12

Theorem: S = EQ Show by strong induction on length of string in EQ.

Theorem: S = EQ Show by strong induction on length of string in EQ. Proof in Notes 5. March 6, 2002 13

In-Class Problem 1 March 6, 2002 14

In-Class Problem 1 March 6, 2002 14

Recursive Data Types Rooted Binary Trees March 6, 2002 15

Recursive Data Types Rooted Binary Trees March 6, 2002 15

Rooted Binary Trees Defined recursively as follows: • A single node r is in

Rooted Binary Trees Defined recursively as follows: • A single node r is in T: March 6, 2002 16

Rooted Binary Trees If t, t’ are RBTs, then the following are RBTs Makeleft(t)

Rooted Binary Trees If t, t’ are RBTs, then the following are RBTs Makeleft(t) t Makeright(t) Makeboth(t, t’) t t’ t March 6, 2002 17

A Rooted Binary Tree Example March 6, 2002 18

A Rooted Binary Tree Example March 6, 2002 18

Recursive Functions on RBT nodes(t) → Vertex Set of t }, nodes(makeleft(t)): : =

Recursive Functions on RBT nodes(t) → Vertex Set of t }, nodes(makeleft(t)): : = nodes(t) { } nodes( Remember, ): : = { is not supposed to be in Nodes(t) nodes(makeright(t)): : = nodes(t) { } nodes(makeboth(t 1, t 2)): : = nodes(t 1) nodes(t 2) { } March 6, 2002 19

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

Example nodes( ) = {{ }, { }, { }, { } } March 6, 2002 20

Recursive Functions on RBT depth(t) → Power Set of t depth( ): : =

Recursive Functions on RBT depth(t) → Power Set of t depth( ): : = 1 depth(makeleft(t)): : = 1 + depth(t) depth(makeright(t)): : = 1 + depth(t) depth(makeboth(t 1, t 2)): : = max(depth(t 1)+ 1, depth(t 2)+ 1) March 6, 2002 21

Example depth( ) =4 March 6, 2002 22

Example depth( ) =4 March 6, 2002 22

Lemma: |nodes(t)| 2 depth(t) Proof by Structural Induction Base Case: t = |nodes(t)| =

Lemma: |nodes(t)| 2 depth(t) Proof by Structural Induction Base Case: t = |nodes(t)| = 1 = 20 = 2 depth(t) OK! March 6, 2002 23

Induction Case: makeleft(t) Assume: |nodes(t)| 2 depth(t) To Prove: |nodes(makeleft(t))| 2 depth(makeleft(t)) |nodes(makeleft(t))| =

Induction Case: makeleft(t) Assume: |nodes(t)| 2 depth(t) To Prove: |nodes(makeleft(t))| 2 depth(makeleft(t)) |nodes(makeleft(t))| = |nodes(t)| + 1 2 depth(t) + 2 depth(t) by def. of makeleft by induction hyp. = 2 depth(t)+1 = 2 depth(makeleft(t)) by def. of depth March 6, 2002 24

Induction Case: makeright(t) Same March 6, 2002 25

Induction Case: makeright(t) Same March 6, 2002 25

Induction Case: makeboth(t 1, t 2) Assume: |nodes(t 1)| 2 depth(t ) |nodes(t 2)|

Induction Case: makeboth(t 1, t 2) Assume: |nodes(t 1)| 2 depth(t ) |nodes(t 2)| 2 depth(t ) 1 2 To Prove: |nodes(makeboth(t 1, t 2))| 2 depth(makeboth(t , t )) 1 2 Proof: VERY SIMILAR March 6, 2002 26

In-Class Problems 2 & 3 March 6, 2002 27

In-Class Problems 2 & 3 March 6, 2002 27

Examples so far: Can reduce structural induction to Strong Induction on SIZE of parts.

Examples so far: Can reduce structural induction to Strong Induction on SIZE of parts. But what if the objects are all infinite? simple example: recursive definition of the set of functions on real numbers from 18. 01. what is the "size" of the COSINE function? March 6, 2002 28

In-Class Problem 4 March 6, 2002 29

In-Class Problem 4 March 6, 2002 29

In-Class Problem 5 March 6, 2002 30

In-Class Problem 5 March 6, 2002 30

Infinitely Wide Trees … … March 6, 2002 31

Infinitely Wide Trees … … March 6, 2002 31

Infinitely Wide Trees March 6, 2002 32

Infinitely Wide Trees March 6, 2002 32

Infinitely Wide Trees March 6, 2002 33

Infinitely Wide Trees March 6, 2002 33

Infinitely Wide Trees … … March 6, 2002 34

Infinitely Wide Trees … … March 6, 2002 34

In-Class Problems 6 & 7 March 6, 2002 35

In-Class Problems 6 & 7 March 6, 2002 35