Docsity

Recursion in Data Structures and Algorithms, Schemes and Mind Maps of Compiler Design

Partial preview of the text

Download Recursion in Data Structures and Algorithms and more Schemes and Mind Maps Compiler Design in PDF only on Docsity!

SE-

Data Structures & Algorithms Recursion Anum Tariq, Lecturer Department of Software Engineering Mirpur University of Science and Technology (MUST)

Recursion vs. Iteration

  • (^) Repetition in computer programs is

accomplished in one of two ways:

▫ (^) either through recursion or ▫ (^) through iteration.

  • (^) Both approaches result in a process being

repeated several times.

Recursion

  • (^) Recursion refers to an approach in which something refers to itself, but in a slightly simpler format.
  • (^) A mathematical definition of a function is recursive if the function is defined in terms of itself (with a slightly smaller argument).
  • (^) Every recursive definition must have a base case along with the more general recursive definition.
  • (^) Recursive codes have no loops. Repetition is achieved when the subprogram calls itself repeatedly until it reaches the base case.

Recursion

  • (^) A programming technique in which a function calls itself. The function which call itself is called recursive.
  • (^) One of the most effective techniques in programming.
  • (^) Recursive methods solve a problem by calling a copy of itself to work on a smaller problem.
  • (^) It is important to ensure that recursion terminates. Recursion 07/19/

Recursive and Iterative Implementations of Factorial Algorithm Recursive Algorithm: Fact(n) if (n = 1) return 1 else return n * Fact(n-1) Iterative Algorithm: Fact(n) Factorial = 1 while (n >= 1) Factorial = n * Factorial n=n- End-while return Factorial Input: A non-negative number n Output: Value of n!

Factorial Example

By Recursion By Looping int Factorial(int n) { if (n==0) // base case return 1; else return n*Factorial(n-1); } int Factorial(int n) { int fact = 1; for(int count=2; count<=n; count+ +) fact = fact * count; return fact; } 07/19/ Recursion

Recursion vs. Iteration

  • (^) Recursion tends to be a more natural approach if the problem/question/formula is initially expressed in a recursive manner, and
  • (^) Iteration tends to be a more natural approach if the problem/question/formula is expressed as a process done a specified number of times.
  • (^) Recursion tends to be more problematic for a computer to implement (but easier for a programmer to code!),
  • (^) Iteration is much more efficient for a computer to implement.

Recursive vs. Iterative

Functions

  • (^) Recursive function is a function that is

partially defined by itself and consists of some

simple case with a known answer.

▫ (^) Example: Fibonacci number sequence, factorial function, quick sort and more. Some of the algorithms/functions can be represented in an iterative way and some may not.

  • (^) Iterative functions are loop based imperative

repetitions of a process (in contrast to

recursion which has a more declarative

approach).

Recursive Thinking: The General Approach

  1. if problem is “ small enough ”
  2. solve it directly
  3. else
  4. break into one or more smaller subproblems
  5. solve each subproblem recursively
  6. combine results into solution to whole problem Recursion 07/19/

Requirements for Recursive Solution

  • (^) At least one “ small” case that you can solve directly
  • (^) A way of breaking a larger problem down into: ▫ (^) One or more smaller subproblems ▫ (^) Each of the same kind as the original
  • (^) A way of combining subproblem results into an overall solution to the larger problem Recursion 07/19/

Characteristics of Recursive Methods

  • (^) The recursive method calls itself to solve a smaller problem.
  • (^) The base case is the smallest problem that the routine solves and the value is returned to the calling method. (Terminal condition or base case)
  • (^) Calling a method involves certain overhead in transferring the control to the beginning of the method and in storing the information of the return point.
  • (^) Memory is used to store all the intermediate arguments and return values on the internal stack (Stack Frame).
  • (^) The most important advantage is that it simplifies the problem conceptually. Recursion 07/19/
  • (^) Examples include: ▫ (^) Factorial ▫ (^) Powers ▫ (^) Greatest common divisor ▫ (^) Mathematical induction ▫ (^) Fibonacci series Recursion 07/19/

Recursive Design Example:

Code

Recursive algorithm for finding length of a string: public static int length(String str) { if (str == null || str.equals(“”)) return 0; else return length(str.substring(1)) + 1; } Recursion 07/19/

Tracing a Recursive Method

Recursion 07/19/