Chapter 7 - Recursion
Chapter 7 - Recursion
AND
ALGORITHM
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.
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.
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 :
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.
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.
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.
•The recursive call must be absolutely the last thing the method
does.
22
Wish
You
Good Luck
23