Data Structure and Algorithms (CO2003) : Chapter 3 - Recursion
Data Structure and Algorithms (CO2003) : Chapter 3 - Recursion
Data Structure and Algorithms (CO2003) : Chapter 3 - Recursion
Chapter 3 - Recursion
2. Properties of recursion
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 1 / 41
Outcomes
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 2 / 41
Recursion and the basic
components of recursive
algorithms
Recursion
Definition
Recursion is a repetitive process in which an algorithm calls itself.
• Direct : A → A
• Indirect : A → B → A
Example
Factorial "
1 if n = 0
F actorial(n) =
n × (n − 1) × (n − 2) × ... × 3 × 2 × 1 if n > 0
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 3 / 41
Basic components of recursive algorithms
Example
Factorial "
1 if n = 0 base case
F actorial(n) =
n × F actorial(n − 1) if n > 0 general case
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 4 / 41
Recursion
Figure 1: Factorial (3) Recursively (source: Data Structure - A pseudocode Approach with C++)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 5 / 41
Recursion
i=1
factoN = 1
while i <= n do
factoN = factoN * i
i=i+1
end
return factoN
End iterativeFactorial
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 6 / 41
Recursion
if n = 0 then
return 1
else
return n * recursiveFactorial(n-1)
end
End recursiveFactorial
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 7 / 41
Recursion
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 8 / 41
Properties of recursion
Properties of all recursive algorithms
• A recursive algorithm solves the large problem by using its solution to a simpler
sub-problem
• Eventually the sub-problem is simple enough that it can be solved without applying the
algorithm to it recursively.
→ This is called the base case.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 9 / 41
Designing recursive algorithms
The Design Methodology
Every recursive call must either solve a part of the problem or reduce the size of the problem.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 10 / 41
Limitations of Recursion
• A recursive algorithm generally runs more slowly than its nonrecursive implementation.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 11 / 41
Print List in Reverse
93 19 8 26
26 8 19 93
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 12 / 41
Print List in Reverse
93 19 8 26
26 8 19 93
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 13 / 41
Print List in Reverse
Algorithm printReverse(list)
Prints a linked list in reverse.
Pre: list has been built
Post: list printed in reverse
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 14 / 41
Greatest Common Divisor
Definition
" a if b = 0
gcd(a, b) = b if a = 0
gcd(b, a mod b) otherwise
Example
gcd(12, 18) = 6
gcd(5, 20) = 5
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 15 / 41
Greatest Common Divisor
Algorithm gcd(a, b)
Calculates greatest common divisor using the Euclidean algorithm.
Pre: a and b are integers
Post: greatest common divisor returned
if b = 0 then
return a
end
if a = 0 then
return b
end
return gcd(b, a mod b)
End gcd
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 16 / 41
Fibonacci Numbers
Definition
" 0 if n = 0
F ibonacci(n) = 1 if n = 1
F ibonacci(n − 1) + F ibonacci(n − 2) otherwise
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 17 / 41
Fibonacci Numbers
Fib(n)
Fib(n-1) + Fib(n-2)
Fib(n-3) + Fib(n-4)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 18 / 41
Fibonacci Numbers
Fib(4) 3
Fib(3) 2 + Fib(2) 1
Fib(1) 1 + Fib(0) 0
Result
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 19 / 41
Fibonacci Numbers
Algorithm fib(n)
Calculates the nth Fibonacci number.
Pre: n is postive integer
Post: the nth Fibonnacci number returned
if n = 0 or n = 1 then
return n
end
return fib(n-1) + fib(n-2)
End fib
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 20 / 41
Fibonacci Numbers
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 21 / 41
The Towers of Hanoi
1
2
3
2
3 1
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 23 / 41
The Towers of Hanoi
3 2 1
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 24 / 41
The Towers of Hanoi
1
3 2
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 25 / 41
The Towers of Hanoi
1
2 3
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 26 / 41
The Towers of Hanoi
1 2 3
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 27 / 41
The Towers of Hanoi
2
1 3
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 28 / 41
The Towers of Hanoi
1
2
3
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 29 / 41
The Towers of Hanoi
move(3,
A, C, B)
A -> C
move(n, A, C, B)
Complexity
T (n) = 1 + 2T (n − 1)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 31 / 41
The Towers of Hanoi
Complexity
T (n) = 1 + 2T (n − 1)
=> T (n) = 1 + 2 + 22 + ... + 2n−1
=> T (n) = 2n − 1
=> T (n) = O(2n )
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 32 / 41
The Towers of Hanoi
Definition
A process to go back to previous steps to try unexplored alternatives.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 34 / 41
Eight Queens Problem
Place eight queens on the chess board in such a way that no queen can capture another.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 35 / 41
Eight Queens Problem
Post: all the remaining queens are safely placed on the board; or backtracking to the
previous rows is required
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 36 / 41
Eight Queens Problem
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 38 / 41
Recursion implementation in
C/C++
Fibonacci Numbers
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
long f i b ( l o n g num ) ;
i n t main ( ) {
i n t num ;
c o u t << "What␣ F i b o n a c c i ␣ number ␣ do ␣ you ␣ want ␣ t o ␣ c a l c u l a t e ? ␣ " ;
c i n >> num ;
c o u t << "The␣ " << num << " t h ␣ F i b o n a c c i ␣ number ␣ i s : ␣ " << f i b ( num ) << e n d l ;
return 0;
}
l o n g f i b ( l o n g num ) {
i f ( num == 0 | | num == 1 )
r e t u r n num ;
r e t u r n f i b ( num−1) + f i b ( num−2);
}
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 39 / 41
The Towers of Hanoi
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
v o i d move ( i n t n , c h a r s o u r c e ,
char d e s t i n a t i o n , char auxiliary );
i n t main ( ) {
i n t numDisks ;
c o u t << " P l e a s e ␣ e n t e r ␣ number ␣ o f ␣ d i s k s : ␣ " ;
c i n >> numDisks ;
c o u t << " S t a r t ␣ Towers ␣ o f ␣ H a n o i " << e n d l ;
move ( numDisks , ’A ’ , ’C ’ , ’B ’ ) ;
return 0;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 40 / 41
The Towers of Hanoi
v o i d move ( i n t n , c h a r s o u r c e ,
char d e s t i n a t i o n , char a u x i l i a r y ){
s t a t i c int step = 0;
if ( n == 1 )
c o u t << " S t e p ␣ " << ++s t e p << " : ␣Move␣ f r o m ␣ "
<< s o u r c e << " ␣ t o ␣ " << d e s t i n a t i o n << e n d l ;
else {
move ( n −1, s o u r c e , a u x i l i a r y , d e s t i n a t i o n ) ;
move ( 1 , s o u r c e , d e s t i n a t i o n , a u x i l i a r y ) ;
move ( n − 1 , a u x i l i a r y , d e s t i n a t i o n , s o u r c e ) ;
}
return ;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 41 / 41