<p> Chapter Four Notes Induction and Recursion Based on: Discrete Math & Its Applications - Kenneth Rosen CSC125 - Spring 2010</p><p>Recursion & Mathematical Induction Compared</p><p>Recursive definition – basic structure</p><p>1. Base case 2. Recursive case</p><p>Recursive function – basic structure</p><p>1. Base case 2. Recursive call Dominant control structure – if-then-else to check for base case Each recursive call must bring us closer to the base case</p><p>Mathematical Induction – basic structure</p><p>1. Base case 2. Inductive step</p><p>Recursive Definitions and Functions</p><p>Recursive definition – Example 1 Example 2, p. 296: Factorial function</p><p>Recursive definition of factorial: fact(n) = 1, if n = 0 n * fact(n), otherwise</p><p>Why is this not a circular definition? Because: 1. n-1 < n, and therefore: 2. We are guaranteed to eventually reach the base case in the definition</p><p>Recursive definition – Example 2 Example 5/Definition 1, p. 297: Fibonacci function</p><p>Recursive definition of Fibonacci numbers:</p><p>1 fib(n) = 0, if n = 0 1, if n = 1 fib(n-1) + fib(n-2), otherwise Note: There are other ways to define the Fibonacci numbers. This follows the definition given in Rosen, p. 297.</p><p>Recursive definition – Example 3</p><p>Recursive definition of Ackermann's function: A(m,n) = 2n, if m = 0 0, if m ≥ 1 and n = 0 2, if m ≥ 1 and n = 1 A(m-1,A(m,n-1)) otherwise Note: Again, there are a number of definitions of Ackermann's function. This follows the definition given in Rosen, p. 310.</p><p>Recursive Functions</p><p>Recursive function – Example 1 Algorithm 1, p. 312: Computing factorial</p><p>Recursive factorial function: def fact(n): if n == 0: return 1 else: return n*fact(n-1)</p><p>How do we know that this function will eventually terminate and give us an answer? Because: 1. n-1 < n, and therefore: 2. With each function call we get closer and closer to the base case.</p><p>Recursive function – Example 2 Algorithm 7, p. 316: Recursive Fibonacci algorithm</p><p>Recursive Fibonacci function: def fib(n): if n == 0: return 0 elif n == 1: return 1 else:</p><p>2 return fib(n-1) + fib(n-2)</p><p>Recursive function – Example 3 Algorithm 4, p. 313: Recursive Euclidean algorithm {gcd}</p><p>Recursive gcd function: def genGcd(a,b): if b > a: return gcd(b,a) else: return gcd(a,b)</p><p> def gcd(a,b): if a == 0: return b else: return gcd(b % a, a) </p><p>Recursive function – Example 4</p><p>Recursive Ackermann's function: def ack(m,n): if m == 0: return 2*n elif n == 0: return 0 elif n == 1: return 2 else: return ack(m-1, ack(m,n-1))</p><p>More recursive algorithms - examples</p><p>Algorithm 6, p. 314: Recursive binary search</p><p>Algorithm 7, p. 316: Recursive Fibonacci algorithm</p><p>Algorithm 8, p. 316: Iterative Fibonacci algorithm</p><p>Mathematical Induction in detail</p><p>Mathematical Induction – proof structure</p><p>1. Prove base case 2. Assume true for k & prove that it then follows for k+1</p><p>3 M.I. – proof structure, Rosen's notation</p><p>P(n) denotes that the proposition is true for n. Thus,</p><p>Prove base case: Prove P(i), for some integer i. Often i = 0.</p><p>Inductive step: Assume: P(k), where k ≥ i Prove: P(k+1) In other words, we prove P(k) P(k+1), for all k ≥ i.</p><p>Mathematical Induction – Example 1</p><p>Prove: The sum of the first n even nonzero integers is n(n+1)</p><p> n In other words, prove: i=1 i = n(n+1)</p><p>Prove base case: 1 i=1 i = 2*1 = 2 If n =1, then n(n+1) = 1*(1+1) = 2 </p><p>Inductive step: k Assume: i=1 i = k(k+1) k+1 To prove: i=1 i = (k+1)[(k+1) + 1] k+1 k Proof: i=1 i = i=1 i + 2(k+1) = k(k+1) + 2(k+1) = (k+1)(k+2) = (k+1)[(k+1) + 1] </p><p>Mathematical Induction – Example 2 Example 3, p. 269: Show 20 +21 +22 +23 + . . + 2n = 2n+1 – 1</p><p> n i n+1 To Prove: i=1 2 = 2 – 1</p><p>Prove base case: 0 i 0 i=0 2 = 2 = 1 </p><p>Inductive step: k i k+1 Assume: i=0 2 = 2 – 1 k+1 i (k+1)+1 To prove: i=0 2 = 2 – 1 k+1 i k i k+1 k+1 k+1 k+1 k+1 Proof: i=0 2 = i=02 + 2 = 2 – 1 + 2 = 2 + 2 – 1 = 2*2k+1 – 1 = 2(k+1)+1 – 1 </p><p>Example 4, p. 270: Prove first formula of Table 2, p. 157:</p><p>4 n j 2 n n+1 j=0 ar = a + ar + ar + . . + ar = (ar – a)/(r – 1), when r ≠ 1.</p><p>Mathematical Induction – Example 3 Example 6, p. 271: Show n! < 2n, for all n > c, where c is some positive integer. Computing n! and 2n for n = 0, 1, 2, 3 & 4, we find that4! > 24.</p><p>To Prove: n! > 2n, for all n > 3.</p><p>Prove base case: 4! = 24 > 16 = 24 </p><p>Inductive step: Assume: k! > 2k, for all k > 3. To prove: (k+1)!> 2k+1. Proof: (k+1)! = (k+1)*k! > (k+1)* 2k 2k2k+1 </p><p>5</p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages5 Page
-
File Size-