Foundations of Computer Science Last Time Lecture 8 Proofs with Recursive Objects Structural Induction: Induction on Recursively Defined Objects Proving an object is not in a recursive set Examples: sets, sequences, trees 1 Recursion. 2 Recurrences are recursive functions on N . 3 Recursive programs. 4 Recursive sets. 5 Rooted binary trees (RBT). Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 2 / 15 Today → Today: Proofs with Recursive Objects Two Types of Questions About a Recursive Set Recursive definition of A . A = { 0 , 4 , 8 , 12 , 16 , . . . } . 1 0 ∈ A . 2 x ∈ A → x + 4 ∈ A . Two Types of Questions About Recursive Sets 1 (i) What is in A ? Is some feature common to every element of A ? Is everything in A even? Matched Parentheses 2 x ∈ A → x is even ( t ) Structural Induction 3 (ii) Is everything with some property in A ? Is every even number in A ? N Palindromes x is even → x ∈ A ( f ) Arithmetic Expressions Very very different statements! Rooted Binary Trees (RBT) 4 Every leopard has 4 legs. Everything with 4 legs is a leopard? Structural induction shows every member of a recursive set has a property, question (i). Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 3 / 15 Two Types of Questions → Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 4 / 15 Orks and blue Eyes →
Orks and blue Eyes Matched Parentheses M Recursive definition of M . 1 ε ∈ M . [basis] The first two Orks had blue eyes. 2 x, y ∈ M → [ x ] • y ∈ M . [constructor] When two Orks mate, if they both have blue eyes, then the child has blue eyes. The strings in M are the matched (in the arithmetic sense) parentheses. For example: Do all Orks have blue eyes? [ ] ( set x = ε , y = ε to get [ ε ] ε = [ ] ) [[ ]] ( set x = [ ] , y = ε ) When could a green-eyed ork have arisen? [ ] [ ] ( set x = ε , y = [ ] ) Let’s list the strings in M as they are created, Structural Induction M = { ε, [ ] , [[ ]] , [ ] [ ] , [[ ]] [ ] , . . . , s n , . . . } The ancestors have a trait. ↑ ↑ ↑ ↑ ↑ The trait is passed on from parents to children. s 1 s 2 s 3 s 4 s 5 Conclusion: Everyone today has that trait. To get s n , we apply the constructor to two prior (not necessarily distinct) strings. Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 5 / 15 Matched Parentheses M → Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 6 / 15 Strings in M are Balanced → Strings in M are Balanced Proof: Strings in M are Balanced M = { s 1 , s 2 , s 3 , s 4 , s 5 , . . . , s n , . . . , } P ( n ) : string s n is balanced, i.e., the number of ‘ [ ’ equals the number of ‘ ] ’ Balanced means the number of opening and closing parentheses are equal Proof . Strong induction on n . 1: [Base case] The base case is s 1 = ε which is clearly balanced, so P (1) is t . The constructor, x, y ∈ M → [ x ] • y ∈ M 2: [Induction step] Show P (1) ∧ · · · ∧ P ( n ) → P ( n + 1) (direct proof). Assume P (1) ∧ P (2) ∧ · · · ∧ P ( n ) : s 1 , . . . , s n are all balanced. adds one opening and closing parenthesis. Show P ( n + 1) : s n +1 is balanced. s n +1 is the child of two earlier strings: s n +1 = [ s k ] • s ℓ (constructor rule) s k , s ℓ appeared earlier than s n +1 , so s k and s ℓ are balanced (induction hypothesis). If the “parent” strings x and y are balanced, then the child [ x ] • y is balanced. Therefore s n +1 is balanced (because you add one opening and closing parenthesis). (Orks inherit blue eyes. Here, parents pass along balance to the children.) 3: By induction, P ( n ) is t ∀ n ≥ 1 . Just as all Orks will have blue eyes, all strings in M will be balanced. Question. Is every balanced string in M ? Exercise. Prove that [[ ] �∈ M . Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 7 / 15 Proof of this → Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 8 / 15 Structural Induction →
Structural Induction Every String in M is Matched Strong induction with recursively defined sets is called structural induction . Let S be a recursive set. This means you have: [ [ ] ] [ ] 1 Bases cases s 1 , . . . , s k that are in S . opening:3 closing:3 2 Constructor rules that use elements in S to create a new element of S . Going from left to right: Let P ( s ) be a property defined for any element s ∈ S . To show P ( s ) for every [ [ ] ] [ ] element in S , you must show: opening:3 closing:3 1: [Base cases] P ( s 1 ) , P ( s 2 ) , . . . , P ( s k ) are t . Opening is always at least closing: parentheses are arithmetically matched. 2: [Induction step] For every constructor rule, show: if P is t for the parents, then P is t for children Important Exercise. Prove this by structural induction. Key step is to show that constructor preserves “matchedness”. 3: By structural induction, conclude that P ( s ) is t for all s ∈ S . MUST show for every base case. Question. Is every string of matched parentheses in M ? MUST show for every constructor rule. Hard Exercise. Prove this. (see Exercise 8.3). Structural induction can be used with any recursive set. Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 9 / 15 Every String in M is Matched → Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 10 / 15 Structural Induction on N → Structural Induction on N Palindromes P “Was it a rat I saw” N = { 1 , 2 , 3 , . . . } is a recursively defined set, 1 1 ∈ N . (01100) r = 00110 not a palindrome 2 x ∈ N → x + 1 ∈ N . (0110) r = 0110 palindrome Recursive definition of palindromes P Consider any property of the natural numbers, for example P ( n ) : 5 n − 1 is divisible by 4 . 1 There are three base cases: ε ∈ P , 0 ∈ P , 1 ∈ P . 2 There are two constructor rules: (i) x ∈ P → 0 • x • 0 ∈ P ; (ii) x ∈ P → 1 • x • 1 ∈ P . Structural induction to prove P ( n ) holds for every n ∈ N : 1: [Prove for all base cases] Only one base case P (1) . Constructor rules preserves palindromicity: • 0) r = 001100 (0 • 0110 2: [Prove every constructor rule preserves P ( n ) ] Only one constructor: (i) � �� � x (1 • 0110 • 1) r = 101101 if P is t for x (the parent), then P is t for x + 1 (the child). (ii) 3: By structural induction, P ( n ) is t ∀ n ∈ N . Therefore, we can prove by structural induction that all strings in P are palindromes. That’s just ordinary induction! Hard Exercise. Prove that all palindromes are in P (Exercise 8.7). Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 11 / 15 Palindromes P → Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 12 / 15 Arithmetic Expressions →
Arithmetic Expressions Rooted Binary Tree with n ≥ 1 Vertices Have n − 1 Edges 1 The empty tree ε is an RBT. Fact known to all kindergartners: ((1 + 1 + 1) × (1 + 1 + 1 + 1 + 1)) = 15 , T 1 T 2 2 Disjoint RBTs T 1 , T 2 give a new RBT by � � ((1 + 1 + 1) × (1 + 1 + 1 + 1 + 1)) = 15 , T 1 T 2 value linking their roots to a new root. A recursive set of well formed arithmetic expression strings A odd : P ( T ) : if T is a rooted binary tree with n ≥ 1 vertices, then T has n − 1 links. 1 One base case: 1 ∈ A odd . 1: [Base case] P ( ε ) is vacuously t because ε is not a tree with n ≥ 1 vertices. 2 There are two constructor rules: (i) x ∈ A odd → ( x + 1 + 1) ∈ A odd ; 2: [Induction step] Consider the constructors with parent RBTs T 1 and T 2 . (ii) x, y ∈ A odd → ( x × y ) ∈ A odd . Parents: T 1 with n 1 vertices and ℓ 1 edges and T 2 with n 2 vertices and ℓ 2 edges. Child: T with n vertices and ℓ edges. Case 1: T 1 = T 2 = ε . Child is a single node with n = 1, ℓ = 0, and ℓ = n − 1. ✓ 1 → (1 + 1 + 1) → ((1 + 1 + 1) + 1 + 1) Case 2: T 1 = ε ; T 2 � = ε . The child one more node and one more link, n = n 2 + 1 and (1 × 1) ((1 × 1) + 1 + 1) . . ℓ = ℓ 2 + 1 ih = n 2 − 1 + 1 = n 2 = n − 1 . ✓ . Case 3: T 1 � = ε ; T 2 = ε . (Similar to case 2.) n = n 1 + 1 and ℓ = ℓ 1 + 1 ih = n 1 − 1 + 1 = n 1 = n − 1 . ✓ Case 4: T 1 � = ε ; T 2 � = ε . Now, n = n 1 + n 2 + 1 and there are two new links, so The constructors add 2 to the parent or multiply the parents. ℓ = ℓ 1 + ℓ 2 + 2 ih = n 1 − 1 + n 2 − 1 + 2 = n 1 + n 2 = n − 1 . ✓ If the parents have odd value, then so does the child. Constructor always preserves property P . Constructors preserve “oddness” → all strings in A odd have odd value. 3: By structural induction, P ( T ) is true ∀ T ∈ RBT. Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 13 / 15 Rooted Binary Trees → Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 14 / 15 Checklist for Structural Induction → Checklist for Structural Induction Analogy: if the first ancestors had blue eyes, and blue eyes are inherited from one generation to the next, then all of society will have blue eyes. You have a recursively defined set S . ✓ You want to prove a property P for all members of S . ✓ Does the property P hold for the base cases? ✓ Is the property P preserved by all the constructor rules? ✓ Structural induction is not how to prove all objects with property P are in S . ✘ Creator: Malik Magdon-Ismail Proofs with Recursive Objects: 15 / 15
Recommend
More recommend