IT3011 Week02 Chapter2 Recursion Memorized Recursion
IT3011 Week02 Chapter2 Recursion Memorized Recursion
and algorithms
DATA STRUCTURES AND
ALGORITHMS
WEEK 2: RECURSION, MEMORIZED RECURSION
3
CONTENT
• Basic definition
• Memorized recursion
4
BASIC DEFINITION
• Recursive function
• Basic step: Determine the value of a function with some initial parameter values
• Recursive step: Determine the relationship between the function depending on the same function but
with smaller parameters
• Recursion: F(n) = F(n-1) + n, with • Recursion: C(k, n) = C(k-1,n-1) + C(k,n-1), for others
n>1
5
BASIC DEFINITION
• Recursive step: determine the rule indicating that larger word parts belong to the set of original
elements
• Basic: 3 belongs to S
6
GENERAL RECURSIVE DIAGRAM
A recursive algorithm is an algorithm that calls itself with a smaller input size.
Recursive algorithms are often used when needing to deal with recursively defined objects.
Example: The recursive definition of a Fibonacci sequence:
• f(0) = 0, f(1) = 1,
7
GENERAL RECURSIVE DIAGRAM
Recursive(input) {
if (size of input is minimum) then
Do basic steps; /* Solve the problem with the smallest size*/
else {
Recursive(input with smaller size); /* Recursive step for
subproblems*/
/* Note: There might exist other recursive calls*/
Combine results of subproblems to get solution;
return solution;
}
}
8
EXAMPLE
9
EXAMPLE
10
EXAMPLE: HANOI TOWER
The Tower of Hanoi problem is presented as follows: “There are 3 piles a, b, c. On pile a there is a stack of n
disks, the diameter decreasing from bottom to top. It is necessary to move the stack of disks from pile a to
pile c following the rules:
• Only discs with smaller diameters should be placed on top of disk with larger diameters. During the
transfer process, it is allowed to use pile b as an intermediate pile.
The problem is: List the steps to move the disks that need to be performed to complete the task set out in
the problem.
11
EXAMPLE: HANOI TOWER
Moving n disks from pile a to pile c using pile b as an intermediary: HanoiTower(n, a, c, b);
Disc movement consists of 3 stages:
(1) Move n-1 disks from pile a to pile b, using pile c as an intermediary
(2) Move 1 disk (the disk with the largest diameter) from pile a to pile c
(3) Move n-1 disks from pile b to pile c, using pile a as an intermediary
12
EXAMPLE: HANOI TOWER
Moving n disks from pile a to pile c using pile b as an intermediary: HanoiTower(n, a, c, b);
Disc movement consists of 3 stages:
(1) Move n-1 disks from pile a to pile b, using pile c as an intermediary
Solve a problem of size n-1: HanoiTower(n-1,a,b,c)
(2) Move 1 disk (the disc with the largest diameter) from pile a to pile c
Solve a problem of size 1: HanoiTower(1,a,c,b)
(3) Move n-1 disks from pile b to pile c, using pile a as an intermediary
Solve a problem of size n-1: HanoiTower(n-1,b,c,a)
13
EXAMPLE: HANOI TOWER
The algorithm can be described in the following recursive procedure:
HanoiTower(n, a, c, b) {//Move n-1 disks from pile a to pile b, using pile c as an intermediary
if (n==1) then <move a disk from pile a to pile c>
else {
HanoiTower(n-1,a,b,c);
HanoiTower(1,a,c,b);
HanoiTower(n-1,b,c,a);
}
}
Disc movement consists of 3 stages:
(1) Move n-1 disks from pile a to pile b, using pile c as an intermediary
(2) Move 1 disk (the disk with the largest diameter) from pile a to pile c
(3) Move n-1 disks from pile b to pile c, using pile a as an intermediary
14
EXAMPLE: HANOI TOWER
#include<stdio.h>
int i = 0;
void HanoiTower(int n, char source, char target, char inter)
{
if(n==1){
printf("Move a disk from pile %c to pile %c\n", source,
target);
i++;
return;
} else{
HanoiTower(n-1, source, inter, target);
HanoiTower(1, source, target, inter);
HanoiTower(n-1, inter, target, source);
}
}
int main()
{
int n;
printf("Enter the number of disks n = "); scanf("%d", &n);
HanoiTower(n, 'a', 'c', 'b');
printf("The total number of steps to move disks = %d", i);
return 0;
}
15
ANALYZE RECURSIVE ALGORITHMS
In general, we only need a close estimate of the growth rate of T(n), so solving the recursive formula for
T(n) is to give an estimate of the growth rate of T(n) in asymptotic notation.
16
ANALYZE RECURSIVE ALGORITHMS
Let T(n) be the number of multiplication operations that must be performed in the call to factorial(n).
We have:
T(0) = 0,
T(n) = T(n-1) +1, n≥1
17
ANALYZE RECURSIVE ALGORITHMS
Let T(n) be the number of multiplication operations that must be performed in the call to factorial(n).
We have:
T(0) = 0,
T(n) = T(n-1) +1, n≥1
18
ANALYZE RECURSIVE ALGORITHMS
Let T(n) be the number of multiplication operations that must be performed in the call to factorial(n). We
have:
T(0) = 0,
T(n) = T(n-1) +1, n≥1
Solving the recursive formula T(n), we have:
19
MEMORIZED RECURSION
20
MEMORIZED RECURSION
21
MEMORIZED RECURSION
In the two examples above, we have seen that the recursive algorithms for calculating Fibonacci numbers
and calculating binomial coefficients are inefficient.
To increase the efficiency of recursive algorithms, we can use memorized recursion techniques.
Using memorized recursion techniques, in many cases, we can preserve the recursive structure of the
algorithm and at the same time ensure its efficiency. The biggest disadvantage of this approach is the
memory requirement.
22
MEMORIZED RECURSION
23
MEMORIZED RECURSION
24
MEMORIZED RECURSION
Before calling the function C(k, n), call the init() function to
initialize the elements in the array M[ ][ ] = 0
25
THANK YOU !
26