Presentation is loading. Please wait.

Presentation is loading. Please wait.

Self-Reference - Induction Cmput 115 - Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.

Similar presentations


Presentation on theme: "Self-Reference - Induction Cmput 115 - Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is."— Presentation transcript:

1 Self-Reference - Induction Cmput 115 - Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 1/3/00

2 ©Duane Szafron 1999 2 About This Lecture In this lecture we will learn about a kind of self-reference called mathematical induction. We will use it to prove analytic formulas and to prove that segments of programs are correct. Cmput 272 covers this topic in much more depth.

3 ©Duane Szafron 1999 3Outline Mathematical induction A template for induction proofs An inductive proof of a formula An inductive proof of method correctness

4 ©Duane Szafron 1999 4 Mathematical Induction Self-reference occurs when the proof of a theorem relies on the application of the same theorem to a simpler case. This situation is called mathematical induction. For example, we could use mathematical induction to prove that:  n i=0 i = n(n+1)/2 We used this result in the average case complexity analysis of a linear search.

5 ©Duane Szafron 1999 5 A Template for Induction Proofs Begin the proof with: “We will prove this result using induction on the problem size, n.” This informs the reader of your approach. Directly prove the necessary base cases. Keep the number of cases small and the proofs simple. State the assumption that the result holds for all values from the base case, up to but not including the n th case. Prove the n th case from simpler cases. State that by mathematical induction on n, the result is true for all n larger than or equal to the base cases. template from Bailey ch. 4

6 ©Duane Szafron 1999 6 An Inductive Proof Prove that: ∑ n i=0 2 i = 2 n+1 - 1 for all n >= 0. We will prove this result using induction on the problem size, n. Basis Step: If n = 0 then [lhs]  n i=0 2 i =  0 i=0 2 i = 2 0 = 1. However, in this case, [rhs] 2 n+1 - 1 = 2 0+1 - 1 = 2 1 - 1 = 2 - 1 = 1 as well, so the base case holds for n = 0.

7 ©Duane Szafron 1999 7 An Inductive Proof Assume that the result holds for all values from the base case, n = 0, up to but not including the n th case. Inductive Step: The n th case holds since:  n i=0 2 i = (  n-1 i=0 2 i ) + 2 n = 2 (n-1)+1 - 1 + 2 n = 2 n - 1 + 2 n = 2 n + 2 n - 1 = 2* 2 n - 1 = 2 n+1 - 1. By mathematical induction on n, the result is true for all n larger than or equal to the base case, n = 0.

8 ©Duane Szafron 1999 8 An Inductive Proof Assume that the result holds for all values from the base case, n = 0, up to and including the n th case. Inductive Step: The n+1 th case holds since:  n+1 i=0 2 i = (  n =0 2 i ) + 2 n+1 = 2 n+1 - 1 + 2 n+1 = 2*2 n+1 - 1 = 2 n+2 - 1. By mathematical induction on n, the result is true for all n larger than or equal to the base case, n = 0.

9 ©Duane Szafron 1999 9 Proving Program Correctness Mathematical induction can also be used to prove that a program segment is correct. For example, we can use induction to prove that this recursive method is correct: public static int sum(int n) // post: return the sum of ints from 1 to the given value if (n < 1) return 0;// 1 else return// 2 sum(// 3 n - 1// 4 ) + n;// 5 } code from Bailey pg. 66

10 ©Duane Szafron 1999 10 A Program Correctness Proof We will prove this result using induction on the problem size, n. Basis: If n = 0 then the condition in the if statement of line 1 is true so the method returns 0 which is correct. Assume that the method is correct for all values from the base case 0, up to but not including the n th case. proof from Bailey ch. 4

11 ©Duane Szafron 1999 11 A Program Correctness Proof Inductive Step: Consider a method call for n > 0. The condition in the if statement of line 1 is false so the else clause starting at line 2 is run. Line 4 constructs the argument n-1 which is less than n. Therefore, our assumption says that the method call on line 3 returns the correct answer: the sum of values from 0 to n- 1. Line 5 adds the value n to this result which gives the correct answer. proof from Bailey ch. 4

12 ©Duane Szafron 1999 12 A Program Correctness Proof Therefore, by induction on n, the method computes the correct answer for all n larger than or equal to the base case, 0. proof from Bailey ch. 4


Download ppt "Self-Reference - Induction Cmput 115 - Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is."

Similar presentations


Ads by Google