Download presentation
Presentation is loading. Please wait.
1
Recursion Data Structures
2
Two approaches to writing repetitive algorithms:
Recursion is a repetitive process in which an algorithm calls itself. Usually recursion is organized in such a way that a subroutine calls itself or a function calls itself Two approaches to writing repetitive algorithms: Iteration Recursion
3
Problems defined recursively
There are many problems whose solution can be defined recursively Example: n factorial 1 if n = 0 n!= (recursive solution) (n-1)!*n if n > 0 1 if n = 0 n!= (closed form solution) 1*2*3*…*(n-1)*n if n > 0
4
Coding the factorial function
Recursive implementation int Factorial(int n) { if (n==0) // base case return 1; else return n * Factorial(n-1); }
6
Coding the factorial function (cont.)
Iterative implementation int Factorial(int n) { int fact = 1; for(int count = 2; count <= n; count++) fact = fact * count; return fact; }
7
Advantage Through Recursion one can Solve problems in easy way while its iterative solution is very big and complex. Ex : tower of Hanoi You reduce size of the code when you use recursive call. Disadvantage Recursive solution is always logical and it is very difficult to trace.(debug and understand) Before each recursive calls current values of the variables in the function is stored in the PCB, i.e. process control block and this PCB is pushed in the OS Stack. So sometimes allotment of free memory is require for recursive solutions.
8
Another example: Fibonacci Series
int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); }
9
Recursion vs. iteration
Iteration can be used in place of recursion An iterative algorithm uses a looping construct A recursive algorithm uses a branching structure Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code
10
How is recursion implemented?
What happens when a function gets called? int a(int w) { return w+w; } int b(int x) int z,y; ……………… // other statements z = a(x) + y; return z;
11
What happens when a function is called? (cont.)
An activation record is stored into a stack (run-time stack) The computer has to stop executing function b and starts executing function a Since it needs to come back to function b later, it needs to store everything about function b that is going to need (x, y, z, and the place to start executing upon return) Then, x from a is bounded to w from b Control is transferred to function a
12
What happens when a function is called? (cont.)
After function a is executed, the activation record is popped out of the run-time stack All the old values of the parameters and variables in function b are restored and the return value of function a replaces a(x) in the assignment statement
13
What happens when a recursive function is called?
Except the fact that the calling and called functions have the same name, there is really no difference between recursive and nonrecursive calls int f(int x) { int y; if(x==0) return 1; else { y = 2 * f(x-1); return y+1; }
14
2*f(2) 2*f(1) 2*f(0) =f(0) =f(1) =f(2) =f(3)
15
Tower of Hanoi Object Rules
Move 3 disks from first needle to third needle Rules Only one disk can be moved at a time Removed disk must be placed on one of the needles A larger disk cannot be placed on top of a smaller disk Tower of Hanoi problem with three disks
16
Tower of Hanoi (cont’d.)
Case: first needle contains only one disk Move disk directly from needle 1 to needle 3 Case: first needle contains only two disks Move first disk from needle 1 to needle 2 Move second disk from needle 1 to needle 3 Move first disk from needle 2 to needle 3 Case: first needle contains three disks
17
Solution to Tower of Hanoi problem with three disks
18
Solution to Tower of Hanoi problem with n disks
20
Tower of Hanoi (cont’d.)
Generalize problem to the case of 3 disks Recursive algorithm void TowofHan(N, Source, Destination, Middle) { If (N==1) Move a disk from source to destination else TowofHan(N-1, source, middle, destination) TowofHan(N-1, middle, destination, source) }
21
Recursion or Iteration?
Dependent upon nature of the solution and efficiency Efficiency Overhead of recursive function: execution time and memory usage Given speed memory of today’s computers, we can depend more on how programmer imagine solution Use of programmer’s time Any program that can be written recursively can also be written iteratively
22
Assignment Convert a decimal to binary
Find the largest element in the array Given n things, how many different sets of size k can be chosen?
26
int Combinations(int n, int k)
{ if(k == 1) // base case 1 return n; else if (n == k) // base case 2 return 1; else return(Combinations(n-1, k) + Combinations(n-1, k-1)); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.