You are on page 1of 18

Recursion

12/15/22 SUST-CCSIT 1
Recursive Thinking
 Recursion is a programming technique whereby a
method calls itself
‫ هيت قنية ب رمجية حيثت ستدعيا لدا لة ن فسها‬Recursion
 Infinite recursion occurs when there is no “base
case” which will cause the “calling” to end
 Symptom is : running out of memory during
execution
"‫ الالنهائية عندما ال يكون هناك "حالة أساسية‬Recursion ‫تحدث ال‬ 

"‫تؤدي إلى إنهاء "االستدعاء‬


‫ نفاد الذاكرة أثناء التنفيذ‬:‫ال َع َرض هو‬ 

12/15/22 2
Recursive Thinking-Triangle Numbers

12/15/22 3
Triangle Numbers

 Row 1 has 1 pin,


 Row 2 has 2 pins,
 Row 3 has 3 pins,

Row 4 has 4 pins,
 Row 5 has ________ pins.

Row 5 will have 5 pins in it.

12/15/22 4
12/15/22 5
Base Case
 number of pins in 12 rows = 12 + number of pins in 11 rows
 number of pins in 11 rows = 11 + number of pins in 10 rows

………
………
 number of pins in 3 rows = 3 + number of pins in 2 rows

 number of pins in 2 rows = 2 + number of pins in 1 row

 = 2+1
 The number of pins in 1 row is called a base case.

 A base case is a problem that can solved immediately. In


this example, the number of pins in 1 row is 1.

12/15/22 7
Example

Here is our solution for triangle numbers:
Triangle( 1 ) = 1
Triangle( N ) = N + Triangle( N-1 )

Here is another example, calculating Triangle( 5 ):
Triangle( 5 ) = 5 + Triangle( 4 )
= 5 + ( 4 + Triangle( 3 ) )
= 5 + ( 4 + ( 3 + Triangle( 2 ) ))
= 5 + ( 4 + ( 3 + ( 2 + Triangle( 1 ) )))
=5+(4+(3+(2+ 1 )))

= 5 + ( 4 + ( 3 + 3) )
=5+(4+6)
= 5 + 10
= 15
12/15/22 8
Java Method
class TriangleCalc{
int Triangle( int N )
{
if ( N == 1 ) return 1;
else return N + Triangle( N-1 );
}
public static void main ( String[] args) {
int result =Triangle( 4 );
System.out.println("Triangle(4) is " + result ); }}
C:\>java TriangleCalc
Triangle(4) is 10
12/15/22 9
Recursive method

 A recursive method is a method that calls itself.


 Can make some problems much easier, code much
cleaner.
 Any problem that can be solved with recursion can also
be solved without it.
 The non-recursive version may be much more
complicated!

12/15/22 11
Recursive Code
result = 10 public int sum (int N) {
main
int result;
if (N == 1)
result = 6 result =1;
sum else
sum(4) result = N + sum (N-1);
return result;
result = 3 }
sum
sum(3)

result = 1
sum
sum(2)

sum
sum(1)
12/15/22 12
Recursion versus Iteration

Recursion method Iterative Method


public int sum (int N) { Sum = 0;
int result;
if (N == 1) for (int num = 1; num <= N; num++)
result =1;
else sum += number;
result = N + sum (N-1);
return result;
}

All problems can be solved with an iterative method, but in some


cases recursion provides a simple, elegant solution.

12/15/22 13
Example: Factorials

factorial( 0 ) = 1
factorial( N ) = N * factorial( N-1 )
 For example,

factorial( 5 ) = 5 * factorial( 4 )
= 5 * ( 4 * factorial( 3 ) )
= 5 * ( 4 * (3 * factorial( 2 ) ) )
= 5 * ( 4 * (3 * (2 * factorial( 1 ) ) ) )
= 5 * ( 4 * (3 * (2 * ( 1 * factorial( 0 ) ) ) ) )
= 5 * ( 4 * (3 * (2 * ( 1 * 1 ) ) ) )
= 5 * 4 * 3 * 2 * 1 * 1 = 120
Often factorial(N) is written N!
12/15/22 14
Different Ways To Compute
Factorials

iterative method (i.e. uses loops):
int factorial( int N )
{
int product = 1;
for ( int j=1; j<=N; j++ )
product *= j;
return product;
}
Different Ways To Compute Factorials

As a simple example, we will talk about a different way to compute
factorials -- using recursion.

This example will not convince you that recursion is useful, that will
come with bigger examples later.

This example will show you how recursion works.

12/15/22 15
int factorial( int N )
{
if ( __________ ) return 1;
else return ______ * _____________;
}

12/15/22 16
A Recursive Method in Java

int factorial(int n) {
int result;
if (n <= 1)
{ // base case
result = 1; // 1! is 1
}
else {
result = n;
int partial = factorial(n-1);
result = result * partial;
} // end else
return result;
} // end factorial

12/15/22 17
Fibonacci

Fibonacci Series
N 1 2 3 4 5 6 7 8 9 10
fib(N) 1 1 2 3 5 8 13 21 34 55

fib( N ) = fib( N-1 ) + fib( N-2 )

Here is an expanded version of fib(N):


fib( ___ ) = _____ (base case)
fib( ___ ) = _____ (base case)
fib( N ) = fib( N-1 ) + fib( N-2 )
12/15/22 18
fib( 1 ) = 1 (base case)
fib( 2 ) = 1 (base case)
fib( N ) = fib( N-1 ) + fib( N-2 )

public int fib( int n )


{
if ( ________ ) return _______;
else if ( ________ ) return _______;
else return ________ + ________;
}

12/15/22 19
Complete Fib

public int fib( int n )


{
if ( n == 1 ) return 1;
else if ( n == 2 ) return 1;
else return fib( n-1 ) + fib( n-2 );
}

12/15/22 20

You might also like