0% found this document useful (0 votes)
24 views

Chapter 7 - Recursion

This document provides a summary of a lecture on data structures and algorithms given by Dr. Irfana Memon of the Department of Computer Science and Engineering at QUEST. It includes a table of contents listing 13 topics covered in the lecture series, from an overview to recursion, complexity analysis, sorting/searching techniques, trees, and graphs. One topic, recursion, is discussed in more detail over several slides explaining recursive functions and examples like calculating factorials and the Fibonacci sequence recursively. The Towers of Hanoi problem is also presented as another classic recursively solved problem.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Chapter 7 - Recursion

This document provides a summary of a lecture on data structures and algorithms given by Dr. Irfana Memon of the Department of Computer Science and Engineering at QUEST. It includes a table of contents listing 13 topics covered in the lecture series, from an overview to recursion, complexity analysis, sorting/searching techniques, trees, and graphs. One topic, recursion, is discussed in more detail over several slides explaining recursive functions and examples like calculating factorials and the Fibonacci sequence recursively. The Towers of Hanoi problem is also presented as another classic recursively solved problem.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

DATA STRUCTURE

AND
ALGORITHM

Dr. Irfana Memon


Department of CSE, QUEST

https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
1
NO TOPIC CLO
01 A General Overview CLO1
02 Introduction to Data Structures and Algorithm CLO1
03 String Processing CLO1
04 Abstract Data Types CLO1
05 Linked list CLO1
06 Stack and Queue CLO1
07 Recursion CLO1
08 Complexity Analysis CLO2
09 Sorting and Searching techniques CLO2
10 Trees CLO2
11 Graph CLO3
12 P & NP CLO3

2
CHAPTER 7
Recursion

3
 A function is said to be recursively defined, if a function
containing either a Call statement to itself or a Call
statement to a second function that may eventually result in
a Call statement back to the original function.
 A recursive function must have the following properties:
1. There must be certain criteria, called base criteria
for which the function does not call itself.
2. Each time the function does call itself (directly or
indirectly), the argument of the function must be
closer to a base value

4
 Factorial function: In general, we can express the
factorial function as follows: n! = n * (n-1)!
 The factorial function is only defined for positive
integers.
 if n<=1, then n! = 1
 if n>1, then n! = n * (n-1)!

5
Procedure : FACTORIAL(FACT, N)
This procedure calculates N! and return the value in the variable FACT.

6
Procedure : FACTORIAL(FACT, N)
This procedure calculates N! and return the value in the variable
FACT.
1. If N=0, then: Set FACT :=1, and Return

2. Set FACT = 1.

3. Repeat for K=1 to N


Set FACT : = K*FACT
4. Return.

7
Fibonacci Sequence : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,…………………

8
Fibonacci Sequence : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,…………………
Definition 6.2
(a) If n=0 or n=1, then Fn =n
(b) If n>1, then Fn =Fn-2 +Fn-1.

9
Fibonacci Sequence : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,…………………
Definition 6.2
(a) If n=0 or n=1, then Fn =n
(b) If n>1, then Fn =Fn-2 +Fn-1.

Procedure: FIBONACCI(FIB, N)
This procedure calculates Fn and returns the value in the first
parameter FIB.

10
Fibonacci Sequence : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,…………………
Definition 6.2
(a) If n=0 or n=1, then Fn =n
(b) If n>1, then Fn =Fn-2 +Fn-1.

Procedure: FIBONACCI(FIB, N)
This procedure calculates Fn and returns the value in the first
parameter FIB.
1. If N=0 or N=1, then : set FIB :=N, and return.

2. Call FIBONACCI(FIBA, N-2).

3. Call FIBONACCI(FIBB, N-1).

4. Set FIB : =FIBA +FIBB.

5. Return.
11
The object is to move all the disks over to another pole.

12
 Disks of different sizes (call the number of disks "n") are placed
on the left hand post,
 arranged by size with the smallest on top.
 You are to transfer all the disks to the right hand post in the
fewest possible moves, without ever placing a larger disk on a
smaller one.

The object is to move all the disks over to another pole. But
you cannot place a larger disk onto a smaller disk.

13
 How many moves will it take to transfer n disks from the
left post to the right post?
 Let's look for a pattern in the number of steps it takes to move
just one, two, or three disks. We'll number the disks starting
with disk 1 on the bottom.1 disk: 1 move
 Move 1: move disk 1 to post C

14
2 disks: 3 moves
Move 1: move disk 2 to post B
Move 2: move disk 1 to post C
Move 3: move disk 2 to post C

15
3 disks: 7 moves
Move 1: move disk 3 to post C
Move 2: move disk 2 to post B
Move 3: move disk 3 to post B
Move 4: move disk 1 to post C
Move 5: move disk 3 to post A
Move 6: move disk 2 to post C
Move 7: move disk 3 to post C

16
3 disks: 3 moves
 Its solution touches on two important topics :

 A. recursive functions and stacks


 B. recurrence relations

 B. Recurrence relations
 Let TN be the minimum number of moves needed to solve the puzzle
with N disks. From the previous section T1 = 1, T2 = 3 and T3 = 7
 A trained mathematician would also note that T0 = 0. Now let us try
to derive a general formula.

 Thus we can define the quantity TN as


 T0 = 0
TN = 2TN-1 + 1 for N > 0 We may compute
 T1 = 2T0 + 1 = 1,
 T2 = 2T1 + 1= 3,
 T3 = 2T2 + 1 = 7
 T4 = 2T3 + 1 = 15 and so on sequentially.
17
 A. Recursive pattern
 From the moves necessary to transfer one, two, and three disks, we can
find a recursive pattern - a pattern that uses information from one step
to find the next step - for moving n disks from post A to post C:
 First, transfer n-1 disks from post A to post B. The number of moves
will be the same as those needed to transfer n-1 disks from post A to
post C. Call this number M moves. [As you can see above, with three
disks it takes 3 moves to transfer two disks (n-1) from post A to post C.]
 Next, transfer disk 1 to post C [1 move].
 Finally, transfer the remaining n-1 disks from post B to post C. [Again,
the number of moves will be the same as those needed to transfer n-1
disks from post A to post C, or M moves.]

for 1 disk it takes 1 move to transfer 1 disk from post A to post C;


for 2 disks, it will take 3 moves: 2M + 1 = 2(1) + 1 = 3
for 3 disks, it will take 7 moves: 2M + 1 = 2(3) + 1 = 7
for 4 disks, it will take 15 moves: 2M + 1 = 2(7) + 1 = 15
for 5 disks, it will take 31 moves: 2M + 1 = 2(15) + 1 = 31
for 6 disks... ?

18
TOWER(N, BEG, AUX, END)
Write procedure gives a recursive solution to the
Towers of Hanoi problem for N disks.

19
Recursion consumes stack space. Every recursive method
call produces a new instance of the method, one with a new set
of local variables. The total stack space used depends upon the
level of nesting of the recursion process, and the number of
local variables and parameters.

Recursion may perform redundant computations. Consider


the recursive computation of the Fibonacci sequence.
In sum, one has to weigh the simplicity of the code delivered by
recursion against its drawbacks as described above. When a
relatively simple iterative solution is possible, it is definitely a
better alternative.

20
• A recursive function is tail recursive when a recursive call is the last
thing executed by the function.
• The tail recursive functions considered better than non tail recursive
functions as tail-recursion can be optimized by the compiler.
• Compilers usually execute recursive procedures by using a stack.
• This stack consists of all the pertinent information, including the
parameter values, for each recursive call.
• When a procedure is called, its information is pushed onto a stack,
and when the function terminates the information is popped out of
the stack.
• Non-tail-recursive functions, the stack depth (maximum amount of
stack space used at any time during compilation) is more.
• The idea used by compilers to optimize tail-recursive functions is
simple, since the recursive call is the last statement, there is nothing
left to do in the current function, so saving the current function’s stack
frame is of no use.

21
• We can convert a recursive algorithm into a non-recursive
algorithm and there are some instances when we can do this
conversion more easily and efficiently.

•Specifically, we can easily convert algorithms that use tail


recursion.

•An algorithm uses tail recursion if it uses linear recursion and


the algorithm makes a recursive call as its very last operation.

•The recursive call must be absolutely the last thing the method
does.

22
Wish
You
Good Luck

23

You might also like