Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discrete Structures for Computer Science

Similar presentations


Presentation on theme: "Discrete Structures for Computer Science"— Presentation transcript:

1 Discrete Structures for Computer Science
Presented By: Andrew F. Conn Lecture #14: Recursion and Structural Induction October 24th, 2016

2 There are many uses of induction in computer science!
Proof by induction is often used to reason about: Algorithm properties (correctness, etc.) Properties of data structures Membership in certain sets Determining whether certain expressions are well-formed To begin looking at how we can use induction to prove the above types of statements, we first need to learn about recursion

3 Sometimes, it is difficult or messy to define some object explicitly
Recursive objects are defined in terms of themselves We often see the recursive versions of the following types of objects: Functions Sequences Sets Data structures Let’s look at some examples…

4 Recursive functions are useful
When defining a recursive function whose domain is the set of natural numbers, we have two steps: Basis step: Define the behavior of f(0) Recursive step: Compute f(n+1) using f(0), …, f(n) Example: Let f(0) = 3, f(n+1) = 2f(n) + 3 f(1) = 2f(0) + 3 = 2(3) + 3 = 9 f(2) = 2f(1) + 3 = 2(9) + 3 = 21 f(3) = 2f(2) + 3 = 2(21) + 3 = 45 f(4) = 2f(3) + 3 = 2(45) + 3 = 93 Doesn’t this look a little bit like strong induction?

5 Some functions can be defined more precisely using recursion
Example: Define the factorial function F(n) recursively Basis step: F(0) = 1 Recursive step: F(n+1) = (n+1) × F(n) Note: F(4) = 4 × F(3) = 4 × 3 × F(2) = 4 × 3 × 2 × F(1) = 4 × 3 × 2 × 1 × F(0) = 4 × 3 × 2 × 1 × 1 = 24 Compare the above definition our old definition: F(n) = n × (n-1) × … × 2 × 1 The recursive definition avoids using the “…” shorthand!

6 It should be no surprise that we can also define recursive sequences
Example: The Fibonacci numbers, {fn}, are defined as follows: f0 = 1 f1 = 1 fn = fn-1 + fn-2 Calculate: f2, f3, f4, and f5 f2 = f1 + f0 = = 2 f3 = f2 + f1 = = 3 f4 = f3 + f2 = = 5 f5 = f4 + f3 = = 8 This gives us the sequence {fn} = 1, 1, 2, 3, 5, 8, 13, 21, 34, … This is like strong induction, since we need more than fn-1 to compute fn. Fib numbers in nature

7 Recursion is used heavily in the study of strings
Let: ∑ be defined as an alphabet Binary strings: ∑ = {0, 1} Lower case letters: ∑ = {a, b, c, …, z} We can define the set ∑* containing all strings over the alphabet ∑ as follows: Basis step: λ ∈ ∑* Recursive step: If w ∈ ∑* and x ∈ ∑, then wx ∈ ∑* Example: If ∑ = {0, 1}, then ∑ = {λ, 0, 1, 01, 11, …} λ is the empty string containing no characters

8 We can define sets of well-formed formulae recursively
This is often used to specify the operations permissible in a given formal language (e.g., a programming language) Example: Defining propositional logic Basis step: T, F, and s are well-formed propositional logic statements (where s is a propositional variable) Recursive step: If E and F are well-formed statements, so are (¬E) (E ∧ F) (E ∨ F) (E → F) (E ↔ F)

9 ✔ Example Question: Is ((p ∧ q) → (((¬r) ∨ q) ∧ t)) well-formed?
Basis tells us that p, q, r, t are well-formed 1st application: (p ∧ q), (¬r) are well-formed 2nd application: ((¬r) ∧ q) is well-formed 3rd application: (((¬r) ∨ q) ∧ t) 4th application: ((p ∧ q) → (((¬r) ∨ q) ∧ t)) is well-formed Note: You will learn more about this in CS 1511!

10 Group work! Problem 1: Construct a recursive definition of the sequence {an} where the nth term is a natural number computed by adding the (n-1)st term to twice the (n-3)rd term. Assume that the first three terms of this sequence are 1, 1, 1. Problem 2: Construct a recursive definition of the function r() that reverses a string. E.g., r(cat) = tac.

11 Like other forms of induction, structural induction requires that we consider two cases
Basis step: Show that the result holds for the objects specified in the basis case of the recursive definition Recursive step: Show that if the result holds for the objects used to construct new elements using the recursive step of the definition, then it holds for the new object as well. Note: Many problems with structural induction deal with data structures you have not learned yet.

12 Let’s discuss family trees
Consider a family tree where each node can have at most 2 known parents: Charles Ellen John Ann Frank Mary Andrew

13 Properties of the family tree
Let’s call the generations of the tree 𝑛. Thus in the example tree, Andrew is 3rd generation. Assume that ancestors can be forgotten. This allows for “unbalanced” trees. For example, suppose Mary forgot her mother and father. How many people can a family tree of 𝑛 generations have? It can have at most 2 𝑛 −1 people.

14 Lets prove it! 𝑃 𝑛 ≡ A family tree with 𝑛 generations has at most 2 𝑛 −1 people. Base case: 𝑃(1): 2 1 −1=1 I.H.: Assume that 𝑃(𝑘) holds for a family tree of 𝑘 generations The family tree with 𝑘+1 generations has at most 2 ancestry trees before it with 𝑘 generations. By I.H. each ancestry tree has 2 𝑘 −1 people. Then the current ancestry tree has 2 2 𝑘 −1 +1 people. (Two ancestry trees plus the current person) 2 2 𝑘 −1 +1=2∙ 2 𝑘 −2+1= 2 𝑘+1 −1 Inductive step: We will now show that 𝑃(𝑘) → 𝑃(𝑘+1) Conclusion: Since we have proved the base case and the inductive case, the claim holds by structural induction ❏

15 Data Structures Many common and important Data Structures in Computer Science have recursive definitions. I highly encourage you to come back to this material after you have taken a Data Structures course. For now we are going to move on.

16 Multiples of 3 Consider the recursively defined set: 3∈𝑆
𝑥∈𝑆∧𝑦∈𝑆 → 𝑥+𝑦 ∈𝑆 What are some elements of 𝑆? Clearly 3∈𝑆 6∈𝑆 because 6=3+3 ∧ 3∈𝑆 So, 9∈𝑆 In fact: 𝑆= 3,6,9,…,3𝑛 𝑛∈ℕ =3ℕ

17 Let’s prove it! First, we’ll prove that 3ℕ⊆𝑆 ✔ 𝑃 𝑛 ≡3𝑛∈𝑆 ∀𝑛∈ℕ
Base case: 𝑃 1 : 3∈𝑆 by definition I.H.: Assume that 𝑃(𝑘) holds for an arbitrary integer 𝑘 Inductive step: We will now show that 𝑃(𝑘)→𝑃(𝑘+1) 3𝑘∈𝑆 by I.H. 3 ∈ 𝑆 by definition 3𝑘+3∈𝑆 by recursive definition of 𝑆 3(𝑘+1)∈𝑆 Conclusion: Since we have proved the base case and the inductive case, 3ℕ⊆𝑆 ❏

18 Proving the converse! Now, we’ll prove that 𝑆⊆3ℕ ✔
𝑃 𝑛 : All numbers generated using 𝑛 applications of the recursive definition of 𝑆 are of the form 3𝑗. Base case: 𝑃 0 : 3∈𝑆 by base case for 𝑆, and 3=3∙1. I.H.: Assume that 𝑃 𝑘 holds for an arbitrary integer 𝑘 Assume that 𝑥 and 𝑦 are elements of 𝑆, 𝑥 is generated using 𝑘 applications of the recursive definition of 𝑆, and 𝑦 is generated using no more than 𝑘 applications of the recursive definition of 𝑆 Therefore, 𝑥+𝑦∈𝑆 after 𝑘+1 applications of 𝑆’s definition Since 𝑥=3 𝑗 𝑥 and 𝑦=3 𝑗 𝑦 by the I.H., 𝑥+𝑦=3( 𝑗 𝑥 + 𝑗 𝑦 ) Thus 𝑥+𝑦∈3ℕ Inductive step: We will now show that 𝑃 𝑘 →𝑃 𝑘+1 Conclusion: Since we have proved the base case and the inductive case, S ⊆ A ❏ Since S ⊆ A and A ⊆ S, the claim holds.

19 Final Thoughts Structural induction can be used to prove properties of recursive Functions Sequences Sets Data structures Next time, we start learning about counting and combinatorics (Section 6.1)


Download ppt "Discrete Structures for Computer Science"

Similar presentations


Ads by Google