0% found this document useful (0 votes)
24 views24 pages

Recursion - 2: Lecture-28

The document discusses various recursion problems, including calculating Fibonacci numbers, power functions, stair paths, and maze paths. It provides formulas and recursive relations for each problem, along with examples and base cases. The lecture emphasizes the importance of understanding recursive calls and their complexities.

Uploaded by

makodekrishnag08
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)
24 views24 pages

Recursion - 2: Lecture-28

The document discusses various recursion problems, including calculating Fibonacci numbers, power functions, stair paths, and maze paths. It provides formulas and recursive relations for each problem, along with examples and base cases. The lecture emphasizes the importance of understanding recursive calls and their complexities.

Uploaded by

makodekrishnag08
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

Recursion - 2

Lecture-28

Raghav Garg
*Multiple Calls
Ques : Write a function to calculate the nth
fibonacci number using recursion.
recurrence relation/formula
8 1321 by 55 89...
1 I 23 5

if (n
=
=
1 11n =
=
2) return 1;

return fibo(n-1) fiboln-2);


+
f ibo(n) -
fibo(n-1)+ fibo(n-2)
8

in
S

East
fibo(6)

t
I Recursion
Tree
Free

Diagram
-
More
5 Euler's
n
=
5 5 Tour

s is
·

2
2 it
-X
-
Ques : Power function (logarithmic)
X n -
1
n
x I 22

0(n)
pow(x,n-1) T.c.
+ =

n) x
pow(x,
= -

16
2- 28 20
+ - (2856
I
- 2" 2" - (2")
2

2
+

256x256 *

2x2 + (2)
↓Yu
2'x 2
Ques : Power function (logarithmic)
* 63 method-2
64
22
- -

2
24
=

62
6 Y
2 x23 = n

2* 2
=

63 2
2
loge
=

32
2
=x216
2
I

28
S

2
16 28
= x

8 =
2" x 2"
2

2
2
2
y'
*

22
-

2
n
22
=
x

I
2 20*

+
I
2
2 =


1608 + 4-2-1
64+32-
on calls

64 - 631621611....312+/+ 0 log((64)
Ques : Power function (logarithmic)
formula:

powIx,n) pow(x,
=
n/2) bow(x,
*
n2):

= xx
2
42
x
Ques : Power function (logarithmic)
bow(3,8)
L -
pow 13,4)
pow(3,4) *

-d 13
(3,27
pow/3,2) *pow
*powl3,27

A
pow (3,2)

pow
-Y
(3,1) *pow 13,1) pow
1
(3,1)
-
x
pow 13,17
busine pows. pow 13.7

1 2 4
+ + 8 16...n
+
+

=
· 6

·
Sa essg
.
3 I

It
100 So SO
v
2 2
=
2
x
formula:
(n%2 0)
n/2)
25
if
=

So
23 =

2 r
2 x
2
powIX,
-

pow(x,n/2)
*

pow(x,n) =

⑬ =2
x2" 2
x V
2
if (n%2! 0) =

12
=2 2 powIX, n/2) bow(x, n)2)
pow(x,n)
x =
*
2
x
X
6
I 2
+2
2

x Y
2 2x2
= 2
complexity
L
Ollogn)

it re
T.c.

a
=

the

in S.C. O(logn)
=

x 1
+ terms

2" n
=

flog
-
Ques : Stair Path No. of ways: - 5

Either one step or 2


step I
111
and their combinations
112
*

strin
12 I

21 I

22
choices?
Ques : Stair Path
Either one step or 2
step
and their combinations
1
11 I I !e 2
21
&

i
112 4
4

2 U
I

·"
2 I
-
3

t
⑨ 3

* I

Stair(4) Stair (3) Stair (2)


Ques : Stair Path
formula:
stair (n) stair
= (n-1) stair (n-2)
+

base case: stair (2)


Stair(I)
2
=

=I
fre
&
-

n:1 2 3 Y S 6 7

12 3 S 813 21
Ques : Stair Path
11 I I I

·I 12

1 I 2 I

12 I 18 ways

122

2 I ↑ I

212

I
22
Ques : Maze path
2- directions

"
·

Right

·Down

Base case
it Reaches D

6 return 1;
ways then

RRDD DRRD I
if escape out
of matrix
RDRD DRDR
then return 0;
RDDR DDRR
Ques : Maze path
RD
&
DR
D

* I 2 O I 2 ⑦ 12

0 - ② ⑰ 8
- t
I
d D 7 D , D

(0,0) - (1,2) (0,1) e(1,2)


=

(1,0)
+
-> (1, 2)
Ques : Maze path

col-1
Right

' down row -


*
Pre In Post IUVVIMP/
Output
Dredict the
Output
Raam - Pre

call I
Kaam - In

call 2

Raam- post
Pre In Post

Coldwe
pip(3)

-pipelini i
e Pre 2

Pre

z In

Post I
Post
pre I

pios
pip
i p i o si p i s i p pot p i p sos os pipsos
s
Output

I
Call Stack I
·Pre 2

2 &
Pre I

is
·
In 1

· Post I

· In 2

· Prel

·
In 1

· Post I
main() ·
Post 2
Ques : Print zig-zag
Input Output
1 111
2 211121112
3 321112111232111211123
4 432111211123211121112343211121112321112111234
Next Lecture
Kaam

Recursion on Arrays and Strings call

Kaam
More problems on Recursion call

Kaam

call

Kaam

You might also like