2. Recursive Functions
Recall factorial function:
IterativeAlgorithm
Loop construct (while)
can capture computation in a
set of state variables that
update on each iteration
through loop
3. Recursive Functions
factorial(n) = n * factorial(n-1)
Alternatively:
Consider
5! = 5x4x3x2x1
can be re-written as 5!=5x4!
In general n! = nx(n-1)!
11. Recursive Functions
• Size of the problem reduces at each step
• The nature of the problem remains the same
• There must be at least one terminating condition
• Simpler, more intuitive
– For inductively defined computation, recursive algorithm
may be natural
• close to mathematical specification
• Easy from programing point of view
• May not efficient computation point of view
12. GCD Algorithm
gcd (a, b) =
b if a mod b = 0
gcd (b, a mod b) otherwise
Iterative
Algorithm
RecursiveAlgorithm
13. GCD Algorithm
gcd (a, b) =
b if a mod b = 0
gcd (b, a mod b) otherwise
gcd (6, 10)
gcd (10, 6)
gcd (6, 4)
gcd (4, 2)
2
2
2
2
Courtesy Prof PR Panda CSE Department IIT Dehi
14. Fibonacci Numbers
fib (n) =
0
1
n = 1
n = 2
fib (n-1) + fib (n-2) n > 2
RecursiveAlgorithm
Courtesy Prof PR Panda CSE Department IIT Dehi
16. Power Function
• Write a function power (x,n) to compute the
nth power of x
power(x,n) =
1 if n = 0
x * power(x,n-1) otherwise
17. Power Function
• Efficient power function
• Fast Power
– fpower(x,n) = 1 for n = 0
– fpower(x,n) = x * (fpower(x, n/2))2 if n is odd
– fpower(x,n) = (fpower(x, n/2))2 if n is even
19. Towers of Hanoi Problem
• 64 gold discs with different
diameters
• Three poles: (Origin, Spare, Final)
• Transfer all discs to final pole
from origin
– one at a time
– spare can be used to temporarily
store discs
– no disk should be placed on a
smaller disk
• Intial Arrangement:
– all on origin pole, largest at bottom,
next above it, etc.
Origin
Courtesy Prof PR Panda CSE Department IIT Dehi
Spare Final
20. 3-Step Strategy
Origin Spare Final Origin Spare Final
Origin Spare Final Origin Spare Final
Solve for (n-1) disks. Move to Spare.
Use Final as Spare.
Move bottom disk to Final
Move (n-1) disks to Final.
Use Origin as Spare.
Courtesy Prof PR Panda CSE Department IIT Dehi
21. Recursive Solution
Courtesy Prof PR Panda CSE Department IIT Dehi
• Use algorithm for (n-1) disks to solve n-disk problem
• Use algorithm for (n-2) disks to solve (n-1) disk problem
• Use algorithm for (n-3) disks to solve (n-2) disk problem
• ...
• Finally, solve 1-disk problem:
– Just move the disk!
22. Problem solving with Top Down Design
Top down design approach, also called as Stepwise refinement, is
essential to develop a well structured program.
This approach is a problem solving technique that systematically breaks
a complicated problem into smaller, more manageable pieces.
If any of these subproblems is not easier to solve, we further divide the
subproblem into smaller parts.
We repeat the subdividing process until all small parts are not dividable.
Then, we can use a few lines of code to solve every trivial problem. In the
end, we put all these little pieces together as a solution to the original
problem.
This approach makes it easy to think about the problem, code the
solution read the code and identify bugs.
23. Steps to solve a problem using Top
Down Design:
1. Start with an initial problem statement.
2. Define subtask at the first level.
3. Divide subtasks at a higher level into
more specific tasks.
4. Repeat step (3) until each subtask is
trivial.
5. Refine the algorithm into real code.
29. Access Modes
r – read mode
only for reading the content
file pointer points at the beginning of the file
file should exist before opening in read mode