Docsity

Recursion in Data Structures and Algorithms, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Recursion in Data Structures and Algorithms and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

LECTURE 11 โ€“ RECURSION

Recursion

  • Recursion is a technique to solve a problem by defining operations which call

themselves

  • Idea is to narrow down the solution into smallest possible version
  • Set of operations are called again and again until an acceptable solution is reached,

similar to a loop

  • However, recursion is required in situations when you don't know when this acceptable

solution will be achieved

Recursive Functions

  • Functions which call themselves.
  • Again, the idea is that a function keeps calling itself until a solution is reached. int f( int x) { int y; if (x==0) //base case return 1; else { y = 2 * f(x-1); //general case return y+1; } }

Recursion

  • Base Case
    • Case in recursive definition in which the solution is obtained directly
    • Stops the recursion
    • It is the condition which you want to reach eventually to obtain the final solution, and then pass the values to the calling body.
    • Can have more than one base cases
    • E.g., if(x==0) return 1; if(x== 23) return 23;

Routine Call

  • How a routine/function is called? int a( int w) { return w+w; } int b( int x) { int z, y; // other statements z = a(x) + y; return z; }

Routine Call

  • An activation record is stored into a stack (runtime stack)
  • The computer has to stop executing function b and start executing function a
    • Since it needs to come back to function b later, it needs to store everything about function b that it is going to need ( x, y, z, and the place to start executing upon return). Stores values of parameters and local variables
    • Then, x from b is copied to w from a
    • Control is transferred to function a
  • 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

Recursive Call

  • Except the fact that the calling and called functions have the same name, there is really

no difference between recursive and non-recursive calls

Recursive Call int f( int x) { int y; if (x==0) return 1; else { y = 2 * f(x-1); return y+1; } }

Linear Recursion

  • Simplest form of recursion
  • A function simply calls itself until it reaches the

termination condition (base condition) โ€“ winding phase

  • After calling the termination condition, the execution of

the program returns to the caller โ€“ unwinding phase

Linear Recursion int Factorial( int no) { // error condition if (no < 0) return - 1; // termination condition if (0 == no) return 1; // linear recursive call return no * Factorial(no - 1); }

Tail Recursion int Factorial( int no, int a) { // error condition if (no < 0) return - 1; // termination condition if (0 == no) return 1; // Tail recursive call return Factorial(no - 1, no * a); }

Mutual / Indirect Recursion

  • Two or more functions call each other in

a circular way

  • The termination condition in this

recursion can be in one or all of the

functions

Binary / Excessive Recursion

  • In Binary recursion the recursive function calls itself twice, not once
  • This type of recursion is very useful in some data structures like traversing a tree in

prefix, postfix, or infix order, generating Fibonacci numbers, etc.

  • Binary recursion is a specific form of exponential recursion where one function calls the

recursive function more than once (in case of binary, two)

  • Recursive functions call exponentially in this type of recursion.
  • E.g., Fibonacci sequence:

Binary / Excessive Recursion

  • Fibonacci Sequence