Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recursive Functions Part B Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1
Quick Recap of Relevant Topics Use of simple functions in programs Contract-centric view of programming with functions Flow of control in function call and return Activation records and call stack Parameter passing by value and reference Recursive functions 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,
Overview of This Lecture Designing recursive functions Termination and recursive changing of parameters Recursion vs iteration 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,
Acknowledgment Some examples in this lecture are from An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 All such examples indicated in slides with the citation AGRBook Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4
Recall: Encoding Example We want to store quiz 1 and quiz 2 marks of CS101 students in an encoded form Encoding strategy: encode(m, n) = 2 m x 3 n Assume all marks are integers in {1, 2, 10} Observe: encode(m, n) = encode(m, n-1) x 3, if m, n > 1 = encode(m-1, 1) x 2, if m > 1, n=1 = 2 x 3 = 6, if m=1, n=1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5
A Recursive Solution #include <iostream> using namespace std; int newenc(int q1marks,int q2marks); int main() { } for ( ) { cipher = newenc(q1marks, q2marks); } return 0; Are we really sure that every call to newenc that satisfies precondition eventually terminates? // PRECONDITION: int newenc(int q1marks, int q2marks) { switch(q2marks) { case 1: if (q1marks == 1) {return 6;} else {return 2*newEnc(q1Marks 1, 1); } break; default: } } // POSTCONDITION: Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6
Caveats Using Recursive Functions Must specify how to terminate the recursion Otherwise, recursion (calling a function from itself) can go on forever Changing parameters in an Must ensure that in any invocation, the recursion changes parameters in an orderly orderly way way such to that ensure recursion eventually terminates termination encode(m, n) = encode(m, n-1) x 3, if m, n > 1 = encode(m-1, 1) x 2, if m > 1, n=1 = 2 x 3 = 6, if m=1, n=1 Termination case Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7
Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(4, 3) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8 m
Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(4, 2) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9 m
Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(4, 1) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10 m
Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(3, 1) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11 m
Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(2, 1) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12 m
Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(1, 1) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13 m
Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14 m
Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Well-founded ordering of parameters with a least element Move monotonically along order towards least element Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15 m
Caveats Using Recursive Functions encode(m, n) = 2 m x 3 n can also be thought as Changing parameters in this way doesn t ensure termination encode(m, n) = encode(m, n+1)/3, if m, n > 1 = encode(m+1, 1)/2, if m > 1, n = 1 = 2 x 3 = 6, if m = 1, n = 1 Termination case Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16
Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17 m
A Second Example of Recursion Given n, compute factorial(n) = 1 x 2 x x n // PRECONDITION: integer n >= 0 int factorial(int n) { if ( n == 0 ) {return 1;} // factorial(0) = 1 Termination case else { return (n * factorial(n-1)); // Reduce parameter monotonically // to 0, and use recursion } } // POSTCONDITION: return value = factorial(n) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18
A Third Example [Sec 10.3 of AGRBook] Virahanka numbers: V 0 = V 1 = 1, and V n = V n-1 + V n-2 for n >= 2 Also known as Fibonacci numbers (although Virahanka studied these in the context of counting specific types of poetic meters before Fibonacci!) // PRECONDITION: integer n >= 0 int Virahanka(int n) { if ((n == 0) (n == 1)) { return 1; } else { return ( Virahanka(n-1) + Virahanka(n-2) ); } } // POSTCONDITION: return value = V n Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19
Watch Number of Recursive Calls Virahanka(4) Virahanka(3) Virahanka(2) Virahanka(2) Virahanka(1) Virahanka(1) Virahanka(0) Virahanka(1) Virahanka(0) Number of calls required to compute Virahanka(n) grows exponentially with n Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 20
Is There A Better Way? An iterative solution is much better here int Virahanka(int n) { int count, result; int prevvn = 1, prevprevvn = 1; if ((n == 0) (n == 1)) { return 1; } else { for(count = 2; count <= n; count++) { result = prevvn + prevprevvn; prevprevvn = prevvn; prevvn = result; } return result; } } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 21
Recursion vs Iteration Recursive formulation usually clean, intuitive and succinct Need to worry about recursion termination (well-founded ordering of parameter values) Need to worry about number of recursive calls Iterative formulation may be less clean or intuitive (not always!) Need to worry about loop invariants, loop variants and termination Can be very efficient if formulated correctly Best practice: Judicious mix of iteration and recursion Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 22
Summary Recursive functions Termination and ordering of parameter values Recursion: Monotonically move towards termination case Recursion vs iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 23