Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.

Similar presentations


Presentation on theme: "Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the."— Presentation transcript:

1 Recursion and Induction UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. http://jagger.me.berkeley.edu/~pack/e77http://creativecommons.org/licenses/by-sa/2.0/

2 Intro to Recursion and Induction Consider problems/tasks that are parametrized by an integer –Computing the factorial of a number n –Computing the n th row of Pascal’s triangle –Computing the eigenvalues of an n-by-n matrix –Computing the determinant of an n-by-n matrix Assume that –For any k, the solution for the case n=k+1 can be written very clearly in terms of the solution for the n=k case. –Solution for n=0 case is “obvious” Then, a recursive strategy can be used to solve the problem for any value of n. A program is recursive if it calls itself. A program is recursive if it calls itself. Hmmm …

3 A recursive program for factorial function A = rfac(N) if N==0 % The N=0 case is “obvious” A = 1; else % The solution for N is easily % written in terms of the solution % for the N-1 case. A = N*rfac(N-1); end rfac calls itself

4 What happens when a function calls itself? First consider the simpler question: What happens when a function calls another function?” Consider two really simple functions, times2 and t2p5. >> >> D = 3; >> C = t2p5(D); function B = times2(A) B = 2*A; function B = t2p5(A) tmp = times2(A) B = tmp + 5;

5 Function calling function >> >> D=3; >> C=t2p5(D); >> D (value=3) C (value=11) function B=t2p5(A) tmp = times2(A); B = tmp + 5; A (value=3) tmp (value=6) B (value = 11) “function instance #1” workspace for t2p5 function B=times2(A) B = 2*A; A (value=3) B (value = 6) “function instance #1” workspace for times2

6 OK, what happens when a function calls itself? Essentially the same thing –A 2 nd function instance (with its own unique workspace) associated with the same code instructions is created –the code instructions associated with the function are executed in the new workspace. So, at certain times, there are –Two (or even more) function instances of the same function Different workspaces (separate, different sets of variables) Identical copies of code instructions Let’s see it work on >> C = 3; >> D = rfac(C);

7 Function Instances in Recursive Call func A=rfac(N) if N==0 A=1; else A=N*rfac(N-1); end N (value=2) A (value=2) func A=rfac(N) if N==0 A=1; else A=N*rfac(N-1); end N (value=1) A (value=1) func A=rfac(N) if N==0 A=1; else A=N*rfac(N-1); end N (value=0) A (value=1) >> >> C=2; >> D=rfac(C); >> C (value=2) D (value=2) Function instance #1 Function instance #2 Function instance #3

8 Let’s make the progression of variables more interesting function A = fac3(N) if N==0 A = 1; else A = N*fac3(N-3); End So fac3(12) = 12*9*6*3*1 = 1944

9 Function Instances in Recursive Call func A=fac3(N) if N==0 A=1; else A=N*fac3(N-3); end N (value=6) A (value=18) func A=fac3(N) if N==0 A=1; else A=N*fac3(N-3); end N (value=3) A (value=3) func A=fac3(N) if N==0 A=1; else A=N*fac3(N-3); end N (value=0) A (value=1) >> >> C=6; >> D=fac3(C); >> C (value=6) D (value=18) Function instance #1 Function instance #2 Function instance #3

10 A wrong recursive program for factorial function A = rfac(N) % The solution for N is easily % written in terms of the solution % for the N-1 case. A = N*rfac(N-1); What’s wrong?

11 Recursion A problem that can be solved recursively can also be solved without recursion. Often, the code will “look” more complicated. Recursion can be an elegant way to solve a problem (we’ll see more interesting examples soon) Recursive solutions generally use more memory, because at the deepest level, several function workspaces are in existence at the same time. (Nearly) all programming languages allow recursion. Matlab has a built-in recursion limit. This limits the number of simultaneous function instances of a given function. Use >> get(0,’RecursionLimit’)

12 Mathematical Induction Suppose that P is a function that maps an integer k into a statement P(k). If the following assumptions hold: A1: P(0) is true, and A2: for any k, P(k) true implies that P(k+1) is true Then P(k) is true for all integers k≥0. Proof: Let К be the set of integers for which P is false. All of the integers in К are ≥0, so if К is nonempty, let k min be the smallest element of К. So, P(k min ) is false, but P(k min -1) is true. But this violates the assumption A2. Therefore, it must be that К is empty, which means P(k) is true for all integers k≥0. #

13 Induction and Recursion are closely related Induction (establishing the truth of a sequence of statements) If the following assumptions hold: A1: P(0) is true, and A2: for any k, P(k) true implies that P(k+1) is true Then P(k) is true for all integers k≥0. Recursion (solving all instances of a sequence of problems) Assume that For any k, the solution for n=k+1 can be written very clearly in terms of the solution for the n=k. Solution for n=0 case is “obvious” Then, a recursive program can be used to solve the problem for any value of n.

14 Example of Induction Here is an example of a statement P(k) Is P(0) true? In other words, is it true that Can we derive Statement P(k) is that these two expressions are indeed equal. Yes. from P(k)?

15 See if P(k+1) is true, exploiting that P(k) is true. This follows from P(k) is true.

16 Using Induction So, using a “proof by induction”, we have no established that the identity holds for all integers k≥0.


Download ppt "Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the."

Similar presentations


Ads by Google