Sie sind auf Seite 1von 29

TSE3124 - DATA STRUCTURES

The objectives of this chapter


are :-
■ To understand that complex problems that may otherwise be
difficult to solve may have a simple recursive solution.
■ To learn how to formulate programs recursively.
■ To understand and apply the three laws of recursion.
■ To understand recursion as a form of iteration.
■ To implement the recursive formulation of a problem.
■ To understand how recursion is implemented by a computer
system.

TSE3124 - DATA STRUCTURES


Before we start
■ Problem: Find a way to back home
■ We can define the operation "find your way home" as:
– If you are at home, stop moving.
– Take one step toward home.
– "find your way home".
■ Here the solution to finding your way home is two steps (three
steps). First, we don't go home if we are already home.
Secondly, we do a very simple action that makes our situation
simpler to solve. Finally, we redo the entire algorithm.

TSE3124 - DATA STRUCTURES


Linear Recursion
■ The simplest form of recursion.
■ A method is defined so that it makes at most one recursive
call each time it is invoked.
■ Useful when we view an algorithmic problem in terms of a first
or last element plus a remaining set that has the same
structure as the original set.
■ Can be (converted to and) written as in iterative solution.

TSE3124 - DATA STRUCTURES


Linear Recursion
■ Test for base cases
– Begin by testing for a set of base cases (there should be at least
one).
– Every possible chain of recursive calls must eventually reach a
base case, and the handling of each base case should not use
recursion.
■ Recur once
– Perform a single recursive call
– This step may have a test that decides which of several possible
recursive calls to make, but it should ultimately make just one of
these calls
– Define each possible recursive call so that it makes progress
towards a base case.

TSE3124 - DATA STRUCTURES


Definition
■ Recursion means "defining a problem in terms of itself". This
can be a very powerful tool in writing algorithms.
■ The process in which a function calls itself directly or
indirectly is called recursion and the corresponding function
is called as recursive function.
■ It is a powerful programming tool (technique) that in many
cases can provide both shorts, simple and efficient
algorithms.
■ A recursive solution uses a stack during its execution.
■ A recursive solution requires a considerable amount of
memory space to accommodate the recursive calls.

TSE3124 - DATA STRUCTURES


Part of Recursive Algorithm
■ All recursive algorithms must have the following:
– Base Case (i.e., when to stop)
– Work toward Base Case
– Recursive Call (i.e., call ourselves)
■ The "work toward base case" is where we make the problem
simpler (e.g., divide list into two parts, each smaller than the
original).
■ The recursive call, is where we use the same algorithm to
solve a simpler version of the problem. The base case is the
solution to the "simplest" possible problem
■ For example, the base case in the problem 'find the largest
number in a list' would be if the list had only one number...
and by definition if there is only one number, it is the largest).

TSE3124 - DATA STRUCTURES


The Recursion in Factorial
■ Recursion: when a method calls itself
■ Classic example--the factorial function:
– n! = 1· 2· 3· ··· · (n-1)· n
■ Mathematic Expression:

TSE3124 - DATA STRUCTURES


Factorials Functions
■ The factorial of 0 is defined to be 1.
■ Factorial numbers grow large very rapidly, as you can see.

TSE3124 - DATA STRUCTURES


■ As a Java method:
// recursive factorial function
public static int recursiveFactorial(int n) {
if (n <= 1) return 1; // base case
else return n * recursiveFactorial(n- 1); //
recursive case
}
■ In recursive program, the solution to base case is provided
and solution of bigger problem is expressed in terms of
smaller problems.
■ In the above example, base case for n < = 1 is defined and
larger value of number can be solved by converting to smaller
one till base case is reached.

TSE3124 - DATA STRUCTURES


Factorial function using basic
iteration
■ How do you eliminate recursion from the Factorial functions?

public static int factorial (int iNo)


{
int iX, ifact = 1;
for (ix = iNo; iX >= 1; iX--)
iFact = iFact * iX;

return iFact;
}

TSE3124 - DATA STRUCTURES


Stack overflow error occurs in
recursion
■ If base case is not reached or not defined, then stack overflow problem
may arise. Let us take an example to understand this.

int fact(int n)
{
// wrong base case (it may cause
// stack overflow).
if (n == 100)
return 1;
else
return n*fact(n-1);
}

■ If fact(10) is called, it will call fact(9), fact(8), fact(7) and so on but


number will never reach 100. So, the base case is not reached. If the
memory is exhausted by these functions on stack, it will cause stack
overflow error.

TSE3124 - DATA STRUCTURES


Example of Linear Recursion

Recursion trace for an execution of LinearSum (A, n) with input


parameters A = {4, 3, 6, 2, 5} and n = 5.

TSE3124 - DATA STRUCTURES


TSE3124 - DATA STRUCTURES
Triangular Numbers
■ It’s said that the Pythagorians, a band of mathematicians in
ancient Greece who worked under Pythagoras (of Pythagorian
theorem fame), felt a mystical connection with the series of
numbers 1, 3, 6, 10, 15, 21
■ The nth term in the triangular number series is obtained by
adding n to the previous term.
■ Thus, the second term is found by adding 2 to the first term
(which is 1), giving 3.
■ The third term is 3 added to the second term (which is 3)
giving 6, and so on.

TSE3124 - DATA STRUCTURES


Triangular Numbers
■ The numbers in this series are called triangular numbers
because they can be visualized as a triangular arrangement
of objects, shown as little squares as follows.

TSE3124 - DATA STRUCTURES


Triangular Numbers
■ The nth term in the series can be found using a loop.

TSE3124 - DATA STRUCTURES


Triangular Numbers
■ Another method would be using recursion.
■ The value of the nth term can be thought of as the sum of
only two things, instead of a whole series.
■ They are
– The first (tallest) column, which has the value n.
– The sum of all the remaining columns.

TSE3124 - DATA STRUCTURES


Triangular Numbers
■ We have to identify the condition that leads to a recursive
method returning without making another recursive call
■ This is referred to as the base case.
■ It’s critical that every recursive method have a base case to
prevent infinite recursion and the consequent demise of the
program.

TSE3124 - DATA STRUCTURES


TSE3124 - DATA STRUCTURES
Tail Recursion

TSE3124 - DATA STRUCTURES


Binary Recursion
■ Binary recursion occurs whenever there are two recursive
calls for each non-base case.
■ These calls can, for example, be used to solve two similar
halves of some problem.

TSE3124 - DATA STRUCTURES


Fibonacci Numbers
■ The Fibonacci Series is a sequence of
numbers first created by Leonardo
Fibonacci in 1202.
■ The first two numbers in the series are one
and one.
■ To obtain each number of the series, you
simply add the two numbers that came
before it.
■ In other words, each number of the series
is the sum of the two numbers preceding it.

TSE3124 - DATA STRUCTURES


Computing Fibonacci
Numbers

TSE3124 - DATA STRUCTURES


Analysis

TSE3124 - DATA STRUCTURES


TSE3124 - DATA STRUCTURES
A Better Fibonacci Algorithm

TSE3124 - DATA STRUCTURES


■ Tracing a recursive program:

TSE3124 - DATA STRUCTURES


TSE3124 - DATA STRUCTURES

Das könnte Ihnen auch gefallen