This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 1. Basic reminder on induction 2. Definition of recursion 3 Illustration with the computation of the factoria Lecture 6: Recursion 3. Illustration with the computation of the factorial 4. Illustration with Fibonacci numbers 5. The quicksort algorithm 6. Recursive implementation of the quicksort algorithm What is induction? There are some properties which you cannot prove or derive for all cases, but which can be handled sequentially. For example, if you want to prove that the following holds n n +1 for all n. First, you would prove a base case, i.e. this relation is true for n=1 (you can check that this is the case). Then you will prove this fact by induction, i.e. you will assume that the property is true up to n, and show that it implies that 1 + 2 + 3 + 4 + + n = n ( n +1) 2 the property is true up to n+1. This proves the property by induction. 2 Recursion: example, computation of factorial A recursive function is a function which calls itself in its own definition. While this might seem circular at a first glance, the previous reminder on induction might help to illustrate it. For example, consider computing the factorial 1! = 1 2! = 2 1! 3! = 3 2! . . . ( n + 1)! = ( n + 1) n ! New operation Previously computed factorial Recursion: example, computation of factorial Consider implementing this in code: function [out] = myFactoria (n If the input is 1, the result is 1 function [out] = myFactorial(n) Base case: if n==1, then return 1; Induction [Recursion]: result = n x myFactorial(n1) End Will this work? I.e. can you do something like this in Matlab? Induction operation (multiplies by n) Induction step (previous value) 3 Recursive implementation of factorial in Matlab Implementation follows directly the pseudo code presented earlier Function calls itself, what does this mean??? What is happening? 1. User makes call to myRecFactorial(3). A new workspace is opened to compute myRecFactorial(3). 2 Input argument value 3 is compared to 1 Since they are not equal 2. Input argument value, 3, is compared to 1. Since they are not equal, else statement is executed. 3. 3*myRecFactorial(2) must be computed. A new workspace is opened to compute myRecFactorial(2). 4. Input argument value, 2, is compared to 1. Since they are not equal, else statement is executed. 5. 2*myRecFactorial(1) must be computed. A new workspace is opened to compute myRecFactorial(1). 6. Input argument value, 1, is compared to 1. Since they are equal, if statement is executed. 4 What is happening? 7. Output variable out is assigned the value 1. myRecFactorial(1) terminates with output 1. 8 2 myRecFactoria (1) can be resolved to 2 1 = 2 Output variable out 8. 2*myRecFactorial(1) can be resolved to 2 1 = 2. Output variable out is assigned the value 2. myRecFactorial(2) terminates with output 2....
View Full
Document
 Spring '08
 HOROWITZ
 Recursion, Sort

Click to edit the document details