Hassan Khosravi / Geoffrey Tien Recursion October 23, 2017 Hassan Khosravi / Geoffrey Tien
Function/method calls A function or method call is an interruption or aside in the execution flow of a program: ... int a, b, c, d; a = 3; b = 6; c = foo(a, b); d = 9; int foo(int x, int y) { while (x > 0) { y++; x >>= 1; // bitwise right shift by 1 } return y; October 23, 2017 Hassan Khosravi / Geoffrey Tien
Function calls in daily life How do you handle interruptions in daily life? You're at home, working on your CPSC 259 assignment You stop to look up something in the textbook Your roommate/spouse/partner/parent/etc. asks you for help moving some stuff outside Your neighbour tells you a story The doorbell rings LIFO, that's a stack! You stop what you're doing, you memorize where you were in your task, you handle the interruption, and then you go back to what you were doing October 23, 2017 Hassan Khosravi / Geoffrey Tien
Activation records in daily life I am reading about deleting a linked list node in Thareja p. 172 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien
Activation records in daily life I have moved 2 boxes of old papers to the back alley I am reading about deleting a linked list node in Thareja p. 173 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien
Activation records in daily life I am listening to my neighbour tell me about some wild party he went to last night I have moved 4 boxes of old papers to the back alley I am reading about deleting a linked list node in Thareja p. 173 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien
Activation records in daily life I am signing for a FedEx delivery My neighbour is just about to get to the part where he pukes... I have moved 4 boxes of old papers to the back alley I am reading about deleting a linked list node in Thareja p. 173 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien
Activation records in daily life My neighbour has finally finished his story. "Good for you!" I say sarcastically I have moved 4 boxes of old papers to the back alley I am reading about deleting a linked list node in Thareja p. 173 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien
Activation records in daily life I have moved 6 boxes of old papers to the back alley I have moved 8 boxes of old papers to the back alley I am reading about deleting a linked list node in Thareja p. 173 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien
Activation records in daily life I am reading about deleting the last node in a linked list in Thareja p. 174 I am working on line 177 of my linkedlist.c file October 23, 2017 Hassan Khosravi / Geoffrey Tien
Activation records in daily life I finished my linkedlist.c file! October 23, 2017 Hassan Khosravi / Geoffrey Tien
Activation records on a computer A computer handles function/method calls in the same way ... int a, b, c, d; a = 3; b = 6; c = foo(a, b); d = 9; int foo(int x, int y) { while (x > 0) { y++; x >>= 1; // bitwise right shift by 1 } return y; y = 7 y = 6 y = 8 x = 0 x = 1 x = 3 return 8 d = ? d = 9 c = ? c = 8 b = ? b = 6 a = 3 a = ? October 23, 2017 Hassan Khosravi / Geoffrey Tien
Hassan Khosravi / Geoffrey Tien Rabbits! ...and recursion What happens when you put a pair of rabbits in a field? More rabbits! Let’s model the rabbit population, with a few assumptions: Newly-born rabbits take one month to reach maturity and mate Each pair of rabbits produces another pair of rabbits one month after mating Rabbits never die October 23, 2017 Hassan Khosravi / Geoffrey Tien
Hassan Khosravi / Geoffrey Tien More rabbits... How many rabbit pairs are there after 5 months? Month 1 2 3 4 5 6 Month 1: start – 1 pair Month 2: first pair are now mature and mate – 1 pair 1 1 2 3 5 8 Month 3: first pair give birth to a pair of babies – original pair + baby pair = 2 pairs Month 5: the 3 pairs from month 4, and two new pairs – 5 pairs Month 4: first pair give birth to another pair of babies, pair born in month 3 are now mature – 3 pairs Month 6: the 5 pairs from month 5, and three new pairs – 8 pairs And so on... October 23, 2017 Hassan Khosravi / Geoffrey Tien
Hassan Khosravi / Geoffrey Tien Fibonacci series The nth number in the Fibonacci series, fib(n), is: 0 if n = 0, and 1 if n = 1 fib(n – 1) + fib(n – 2) for any n > 1 e.g. what is fib(23) Easy if we only knew fib(22) and fib(21) The answer is fib(22) + fib(21) What happens if we actually write a function to calculate Fibonacci numbers like this? October 23, 2017 Hassan Khosravi / Geoffrey Tien
Calculating the Fibonacci series Let’s write a function just like the formula fib(n) = 0 if n = 0, 1 if n = 1, otherwise fib(n) = fib(n – 1) + fib(n – 2) int fib(int n) { if (n <= 0) return 0; else if (n == 1) return 1; else return fib(n-1) + fib(n-2); } The function calls itself October 23, 2017 Hassan Khosravi / Geoffrey Tien
Hassan Khosravi / Geoffrey Tien Recursive functions The Fibonacci function is recursive A recursive function calls itself Each call to a recursive method results in a separate call to the method, with its own input Recursive functions are just like other functions The invocation (e.g. parameters, etc.) is pushed onto the call stack And removed from the call stack when the end of a method or a return statement is reached Execution returns to the previous method call October 23, 2017 Hassan Khosravi / Geoffrey Tien
Recursive function anatomy Recursive functions do not use loops to repeat instructions But use recursive calls, in if statements Recursive functions consist of two or more cases, there must be at least one Base case, and Recursive case October 23, 2017 Hassan Khosravi / Geoffrey Tien
Hassan Khosravi / Geoffrey Tien Recursion cases The base case is a smaller problem with a known solution This problem’s solution must not be recursive Otherwise the function may never terminate There can be more than one base case And base cases may be implicit The recursive case is the same problem with smaller input The recursive case must include a recursive function call There can be more than one recursive case if the problem is small enough to be solved directly solve it otherwise recursively apply the algorithm to one or more smaller instances use the solution(s) from smaller instances to solve the problem October 23, 2017 Hassan Khosravi / Geoffrey Tien
Hassan Khosravi / Geoffrey Tien Analysis of fib(5) int fib(int n) { if (n <= 0) return 0; else if (n == 1) return 1; else return fib(n-1) + fib(n-2); } 5 fib(5) 2 3 fib(4) fib(3) 1 2 1 1 fib(3) fib(2) fib(2) fib(1) 1 1 1 1 fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) 1 Later in the course we may see how this is an extremely inefficient way to compute the Fibonacci series fib(1) fib(0) October 23, 2017 Hassan Khosravi / Geoffrey Tien
Hassan Khosravi / Geoffrey Tien Thinking recursively Opening a present Your friends have given you a camera as a birthday present. To prolong the suspense when opening the present, they have bundled it into several nested wrapped packages. How do you open the present? openPresent(pkg) { if you can see the actual gift say "thank you" else open the box to reveal spkg openPresent(spkg) } October 23, 2017 Hassan Khosravi / Geoffrey Tien
Hassan Khosravi / Geoffrey Tien Next class... Recursion, continued! with more examples October 23, 2017 Hassan Khosravi / Geoffrey Tien