0% found this document useful (0 votes)
7 views10 pages

Recursion

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Recursion

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Recursion Void fun (int n)

{
When a function calls itself if (n > O)

{
EXAMPLE # I
-
ASCENDING i. calling
TRAC1NhTRE
Void fund ( int n ) 2- fun In - c )
{
fun 113 )

31
if ( n > O ) returning
\ DESCENDING 3 -

{ 112 ) }
fun
/ Yun
''
); }
"

point fl t .
d
,
n

fund (n -
l ); 2 tu)

It
}

} funk) DlFFERENCEBETWEENLO0PANDRECURS#
1

The main difference between loop and recursion is that recursion allows two phases
void main OUTPUT
-
i -
e
ascending and
descending while loop allows
only ascending
{

HOWSTACKISUTIUSEDINRECVR.si#FUNT0NS?-ExAMPLE
int n =3 ; 32 I

fund In ) ;

:fiE
)
#1 font

EXAMPLEIIZTRACINGTREEVOID X
n
font
void fun 1 ( int n )
-

iii.
fun 2 ( int )

i
n

{
fun 213 ) no ,
/
if ( n > o )
212 )
\ ''
);
fun
"
{ point fl d
-

3 t n
.

, main
N ③ X
fun (n );
/ ) fund ( );
-

2 -
l n -
i
fun A- " Z Code

fun 211 )
z
''
);
"

point fl t .
d
,
n 2
main C) =
-

} }
/ I I

} l
fun 2 (O)
4 calls
Here value
> of n was 3, so there were

l void Maino ( Tracing tree) .

void X {
main

{ output int n =3 ; so for value of n


,
there will be n -11 calls

int n =3 ; fund (n ) ;

I 23 }
fun 2 In ) ;

}
1-
MEcoMPLTY ( using Tree )

EXAMPLE # I
-

TRACINGTREEVOID
fun 1 ( int n )
STATICVARIABLESINRECURSIONTRACINGTREEINT
fun ( int n)

{
fun 113 ) {
fun 157=15
31
if ( n > O ) I
\ if Ln > O)
co /
{ 112 ) { -5=15
I
fun fun ) t
-

(u

/ Yun
'

; );
/ I
"

print fl t .
d n return
fun (n 1) -
th ;
a
tu)
fund fun (3) -4=10
(n -
l ); I -
2 } t

It
} return 0 ;
s / I
} funk) }
funk)
t -3=6
I
I
l l
-

fun (1) -2
-
Maine ) t =3
{
void Maina 3 units
/ l
{ int 5; ( ) -1€
fun
a= O =
I
''

( ) );
"
int n =3 ; Thus , for n calls → n units of time .
print L f. d
, fun a
I
fund In ) ; -
} x
}
//
TlMEcoMPLTY ( using recurrence relation)

Th) n
-

- o Tcn ) -

void fund ( int n )

( n -

1) + ¢ n > 0 This function takes { int fun ( int n)

I
fun (
time )
n I if ( n > O ) { s


. -

static int n
-
- o
;
=
25

Assume it as I { if ( n > O) / I
zo

H
"
"" "
t point " "d )' { ) t -5=25
n sett ;
fun ( u
-

'

1- In)=TLn l) -11 -

Tin t
-
) -

fund (n -

);
l return
fun (
n -
1) th ; is / I
T
Tcn) Tht)
✓ Tln 1)
t I 3 As it is similar to }
fun (3) -5 20
=
=
t

- = 1- ( n -
z ) -11 3 T (n) return 0 ;
w/ I
1- (n) In 2) I -11 } ) -5 15
funk
=
1- - +
-
t =

-
) -12
T Ln) = Tln l
/ L
-

TIN 3) tl-
-
s

Assume
fun (1) -5
n
-
k =
O i . n= k Maine )
t =
10

{ / l
Thi -
Tin -31+1
FYI In
int a= 5;
funolo ) -1-5 = 5

I
"

, ( ) );
"
,
printfl Xd
, fun a

! T ( n) = Ith } x

1- ( n) =
Th k ) -1K
//
-
TYPESOFREIRS.IO/V 2 .

HEADRECURS.IO#

I Tail Recursion It means that the the function does not need to perform operation at the time
.

any
Head Recursion of
2 .

calling .
It has to do all operations at
returning time .

3 .
Tree Recursion

4 .
Indirect Recursion -

! / .FI? dn.m;/
-

|
5 .
Nested Recursion void fun ( int n) void fun Lint n )
{ {
int ie l ; if In > o)

while lie -_
n ) E

flint
!
I. TAILRECURS
''
sis
in ;t"
"d

in the function ,
When the
function is
calling itself and that call is the last call .

fun (n ) I .

Every operation is performed at


calling time .
3
fun 13 )
;
{ fun 13 ) ; -

if ( n > O) 2.Tail recursions can


easily converted into loops .
-

-
Head recursions cannot be so
easily converted into loops .

(n -17 ;
fun
}
} 3. TREE RECURSION

Some compilers convert


your program
-

)/ µ :/
TAILRECURS.io#-ND00Ps to
loop if
you have used tail recursion LINEAR RECURSION
-
TREE RECURSION
-

efficient
as
they are more

-
fun ( n ) fun (n)

//
-

|
void fun (int n ) void fun ( int n ) { {
{ { if ( n >o ) if Ln > o)

While ( n > O ) if ( n > O) { {

{ an

Minniti: :÷÷:
"
" " "" " ""

, , .
, , } =

When the
function calls }
}
fun 3)
; fun ( 3) ; itself
only once .

- -
-
When the function calls itself
0 ( t) Oln) more than time
space
one .

-
4 INDIRECTRECURS.IO#
EXAMPLEOFTREERECURSIONVO
.

id fun ( int n) In indirect recursion ,


there are more than one function and
they are
calling
{ one another in a circular manner .

if ( n > O )

{
prihtfl
"
Id
"
n) ; ①
y
,

fun
fun
(n

(n
-

-
D;

i);
( ③
} ④ ←

-
fun A- ( 20 )
fun (3) I / I
⑧ ACTIVATION RECORDS 20
fun B ( 1a)
I l l
① ) fun Ala)
JPY3 #
19

/ \
3 2
q
fun Bts )
② -

funk) funk)9
#

/ \

/ /
void
fun A- ( int ) g
fun ALU )
'
T
n

'
T
{ /
2
fun (1) g
2
if ( n > O ) 4 Yun B 13 )
funk ) , { /
Yuna
,
,

1/1
"


1/1 ( l)
" ''
t d
lunch print fl ) ; 3
lunch
-

. n
,

"
:c !: ÷
""
/ / l l
funk) funk) / / l l
funny funk) .
7 8
1-
:
14 15
t
⑧ funk) funk) funny funk)
y 5 11 12

. .
{
I -12+4+8 = 15 GP series if In > l )
'
20+2
' '
-12 -123 = 23+1 -

I znt -
1 {
' '

t d
print fl
"
-

,
n );

012 )
"
Time -_ fun A ( nz ) ;

Space = 0 ( n) [ No of levels of activation


] }

-
OUTPUT records htt
-

3 -

parameter
321121 I 4 -
levels
5 .

NESTEDRECURS.IO# SUMOFFIRS.TN/VATURALNUMBER#

In a nested recursion ,
the recursive function will pass parameter as a I -12+3+4 . . . . . . .
t n

recursive call .

sum ( n) =
142+3+4-1 . - . . . .
-1 (n c)
-
th

( ) In 1) th
=
sum n sum -

=L
O
fun (95) = 91 Sumlin ) n=o

fun ( int )

font
int n

(n c) th n> °
(s )
sum
{ ( fun @ 5th) ) fun ( 106 )
-

96 =
sum
← /
!
if ( n > 100 )
I 106

196) int ( int ) (u) 5=15


return n lo
; fun sum n sum
-

{ / 'T
#
}
else Time =
Oln)

fu ( fun ( 96-111 ) ) 97 -_

fun ( lol ) if ( n==o ) Space =


o
( n) sum
'
(3 4 = to

return fun ( fun (nth ) ) ;


107 I
return 0 ; I #
} ( 97) sumlz) -13 6
fun else =

3
I #

fly
return sum (n c) -
th
;

fun ( 95) ; ( fun ( 97 -111 ) ) 98 =


funuos ) } sum ( l) -12 =3
108 ← I
'

X
fun ( 98 ) sum (o) t I =
I

! ( fun 198-111
int sum (int n )

)) fun ( 109 ) {
fu 109
99 -

y n*( htt) Oln )


/ return 12 ;

fun ( 99 ) }

( fun (
99,111 ) ) too -_ fun ( 110)
int sum ( int n )

( 100 ) {
fun
int i 5=0 ; I
-

( fun )) lol fun 1111 ) for li=l;ic=n;itt )


1100,1111a
ntl
-

s Sti ;
-

-_
n

fun ( lol ) returns ; I


-

} Tnt
I
91 Oln )
REWRITING

FACTORIALUSINGRECURSIONFACT
POWER FUNCTION

FASTERMETHODINT
-

*
(n ) 1*2*3 * (n )
( c)
pow ( m , n )
= . . . . -
n -

*
fact ( ) n =
fact ( n -
c) n {
if ( n==o)
return 1 ;

'={
la " " I n o
-_

else

factual 're n" if (n -1.2 ==o )

return pow ( m&m ,


nlz ) ;

else
int fact ( int n ) return mtpowlmsmln -
1) 12 );

{ }
if ( n==o )
return 1 ;
29
else POW 12,9 )
= .

n
*
-
fact
fowl 22,4
return In c) -
n
;
2*28
)y
} 2.
*
= 29

I
( 24,2)
pow
y
POWERUSINCRRECURSIONMN
l

-_m*m*m*m
for
pow 128 ,
try
pow (2) )
Low ( 216
. . . n times a

{
. . .

pow (m ,n)
= mama m . . - . . -
*
(n 1) -
times
*
m I *
,
O ) = 28×1 = 28

pow ( 2,87*2 I
-

powlm ,n ) =
pow (Mon i) m
-

I 1

{
*
pow ( min)
! ,m,n→Am pow (2) 7)
= m -_ o
2

po m > o l
*

Pow 12 , 6) 2

I
int powlintm , int n ) Here to calls are
being pow 12,51*2
{ performed 1
if ( n==o) pow (2,41*2
1; But this I
return
way is
taking
return
pow (m , n
-
i ) ; longer pow 12,37*2
} y
l
28=122 )
2,4*2--6=(2*2)
l
't
L
pow (

Pow (2,11*2 y =

"
29=2*4 ) I f
POW 12,07*2=2
I
1
TAYLORIS ERIES-in-ltu-7.tn?+n?-. . n?e(n,u7-
TAYLORJSERIES.US/NGH0RNER'SRU#

I
en
Ith -1%+34.tn?-----7I1+T-t7I-,IY-xztYzIxsxy
--

:*
ItI+Etn÷tI 4 !
o
:÷÷÷÷÷÷:* :
/
else , 37=1+7-+3+1! 2 ( I -121-3-1 . . . .
n )
3

/
(n > 2) 1+3+1 ( ) (n -11 )

FLEECED )
e = 2 n
2 ' +
a-
/
elm " HF
#
O (n ) Quadratic T
'

T T T
/ Tp pan Mf
f- f.
-_ +
I
e In , o )
O(n)Line#
±
int e ( int n
,
int n ) int e ( int n
,
int n )
int e ( int n int n ) { {
,

{ int 5=1 ; static int 5=1 ;

Static int p =L ,f=l ;

int r ;
f for ( in > o
; n -
-

7 if ( n==o )
py s= It mins ; returns ;

if ( n -

return
o )
1;
# €1 -

returns ;
else
s -_ It minds ;
Naman 1×2×3×4 }
An
else return elm , n - i );
{ USING FOR LOOP }
r=e (n , n -
l );

p= pan ; USING RECURSION


f- = fan ;
Zenon rt Plf ;
}
}
PROGRAMUS.in#ECURS0N

FIBONACCIS.FR/ESfibln
n
) O

O
l

l
l

2
2

3
3

4
5

5
8

6
13
7
int

{
fib ( int

if ( ne =D
n )

return n
;
else

{
fibln) =
o n=o
return fibln 2) -
+
fib In -
l );
I }
filth
n=

-
2) tfibln -
l) n > I

/
5 EXCESSIVE RECURSION
-

fib (5)
2
- -
fib (3) + fiba) z

/ \
Z
PROGRAMUSINGITER.AT#
s / ly s

int fib (int n )


fib ( l ) t
fib 12 ) I
fiblz ) i t
fibt ) z

/ !
{
/ t / !
'

int to -_ o
, -4=1 ,
s
,
i
;
-
I s a is

if ( ne -_ i ) fiblo ) t
fibll ) fibco ) t
fibli ) fibli ) 1-
fib 12 ) t

/ )
return n I O l O l l
;
-

for ( i=2;ic=n ; itt )


-

n 14 15
{ fiblo ) t
fibll )

;]
fib (5) 15 calls
I'finite pattison?
to -1 t
=
there
'

s = I na l ° l
, ;
-

n -

fib ! :} I I gag
to = t I
-

n
;
-

t, = S
;
-

n - I then we have to assume

} that the function fibln 2) -

tfibln -
i );
is
calling itself 2
fib In - i )

t
returns ;
-

} Tn This tree lies in the


category of excessive

(2 )
"
recursion because it calls itself multiple order of

Oln) times for the same


parameters .

To reduce the order of this function ,


we

will write another static


program using
array and initialize all its values with -1 .
COMBINATION FORMULA

ltl
" -

F Cr
(n - r ) :r !
° I 2 3 4 5 6

int C (int n ,
int r )
{
fi
S

int ti tz tz ;

\fi
,

fz
,

t, = fact ( n ) ; -

n
3
+ tz= fact to ) ;
-

11 I \ tz fact (n );
= -

-
r n

t fix ) fi#
' 2
t return tilltzts ) ; -
I
,
} Tn
' l l l l l l Oln)
¥1) fix f¥ ) +
fix ) t
fibX )
PASCAL'S TRIANGLE
o l l
fix t
fi
So , for 5 as n
,
6 calls are made o.o
I I

i .

for n
,
htt calls are made

int f- [IoT ;

O (n ) I 2 1

LAH
int
fib (int )
AH
n

This {
approach of storing result in an
array
is
'
called MEMOIZATION if Inc -_ I ) I 3 33C , 33C I
Co
.

Storing the result of function F- [ n]=n ; ' ' ' ' ' ' ' '
calls ,
so
they can be utilized return n
; I 4 6 4 I
again for avoiding excessive 3 Yco 4C , 44 4C
, 44
calls else

/c§,,?
{ int C ( int n
,
int r )

if ( F[n 2) - = = -
l ) {
F- [n -

2) =
fib In-2) ; if cr==oiin==r ) •

return 1;
I)
if ( F [ n
-
]==
I
-

else

F- [
n -

D= fib (n ) -
l
; return C (n - I
,r 1)
- + C (n -
I ,r );
} -

return F- [ n -

2) + F [n - I ];
}
}
TOWER OF HANOI
Void

TOH ( int n
,
int A ,
int B) int c )

¥
if ( n > O)
{
Touch I A) C) B)
;
-

B 'A ,
'

A I. d
printfl from L)
"
C %d to
'

, ;
TOH (n -
l)
,
B ,
A ,c) ;

l . Move one disk at a time . }


disk }
2- NO
bigger can be there above

a smaller one .
#

A- → I

TOH ( 3,4213) B → z

( → 3

TOH ( I ,A

Move disk
, B,
A
c )
to C
using B

/ / / "

3,13273


.

A B C

TOH ) I to 3
( 2 , A) B , c
2,113,2 2,2 , 1,3
I TOH LI , A) C) B)
¢ / / lo / / lis
31,1 # !z
.

Move disk A to B
z .
C
using .

I lo

3- TOH ( I ,
B , A) c) A B C
, it 1,3 , 1,2 1) 2) 3) I 2103 1) 1) 2,3

! It ! 11 !
TOH ( 3 ,
A ,
B, c )
"

o
!! ,
# 1¥
3102
"

X 2 to l X
"
1to3 Is

# /
. .

/
i .
TOH Cz ,
A
,
C) B )
2- More dish from Abc using B Output

3- TOH (2) B A) C ) B
, A C
.

( 1h03 ) 4102 ) (3,2) ( 1,3 ) (2) l ) (2) 3) ( 113 )


, , , , , ,

FOR-nnvmberofdik.ir
( alls
n =3 15 142+22+23=24 -
I

TOH ( 31 A ,
B, c ) n = z 7 1+2+22 = 23 -
I
,

2nt
n -
l

l -
TOH ( X ,
A
,
C, B )
2- More dish from Abc using B O( 2h )
n -
I

3- TOH ( X , B , A) C ) .

You might also like