Data Structure and Algorithms (CO2003) : Chapter 3 - Recursion

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

Data Structure and Algorithms [CO2003]

Chapter 3 - Recursion

Lecturer: Duc Dung Nguyen, PhD.


Contact: nddung@hcmut.edu.vn

Faculty of Computer Science and Engineering


Hochiminh city University of Technology
Contents

1. Recursion and the basic components of recursive algorithms

2. Properties of recursion

3. Designing recursive algorithms

4. Recursion and backtracking

5. Recursion implementation in C/C++

Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 1 / 41
Outcomes

• L.O.8.1 - Describe the basic components of recursive algorithms (functions).


• L.O.8.2 - Draw trees to illustrate callings and the value of parameters passed to them for
recursive algorithms.
• L.O.8.3 - Give examples for recursive functions written in C/C++.
• L.O.8.5 - Develop experiment (program) to compare the recursive and the iterative
approach.
• L.O.8.6 - Give examples to relate recursion to backtracking technique.

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

Using recursion: "


1 if n = 0
F actorial(n) =
n × F actorial(n − 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

Two main components of a Recursive Algorithm


1. Base case (i.e. stopping case)
2. General case (i.e. recursive case)

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

Factorial: Iterative Solution


Algorithm iterativeFactorial(n)
Calculates the factorial of a number using a loop.
Pre: n is the number to be raised factorially
Post: n! is returned - result in factoN

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

Factorial: Recursive Solution


Algorithm recursiveFactorial(n)
Calculates the factorial of a number using a recursion.
Pre: n is the number to be raised factorially
Post: n! is returned

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.

Rules for designing a recursive algorithm


1. Determine the base case (stopping case).
2. Then determine the general case (recursive case).
3. Combine the base case and the general cases into an algorithm.

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.

• BUT, the recursive solution shorter and more understandable.

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

if list is null then


return
end
printReverse (list -> next)
print (list -> data)
End printReverse

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-2) + Fib(n-3) + Fib(n-4)

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(2) 1 + Fib(1) 1 + Fib(0) 0

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

No Calls Time No Calls Time


1 1 < 1 sec. 11 287 < 1 sec.
2 3 < 1 sec. 12 465 < 1 sec.
3 5 < 1 sec. 13 753 < 1 sec.
4 9 < 1 sec. 14 1,219 < 1 sec.
5 15 < 1 sec. 15 1,973 < 1 sec.
6 25 < 1 sec. 20 21,891 < 1 sec.
7 41 < 1 sec. 25 242,785 1 sec.
8 67 < 1 sec. 30 2,692,573 7 sec.
9 109 < 1 sec. 35 29,860,703 1 min.
10 177 < 1 sec. 40 331,160,281 13 min.

Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 21 / 41
The Towers of Hanoi

Move disks from Source to Destination using Auxiliary:

1. Only one disk could be moved at a time.


2. A larger disk must never be stacked above a smaller one.
3. Only one auxiliary needle could be used for the intermediate storage of disks.

1
2
3

Source Auxiliary Destination


Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 22 / 41
The Towers of Hanoi

2
3 1

Source Auxiliary Destination

Moved disc from pole 1 to pole 3.

Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 23 / 41
The Towers of Hanoi

3 2 1

Source Auxiliary Destination

Moved disc from pole 1 to pole 2.

Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 24 / 41
The Towers of Hanoi

1
3 2

Source Auxiliary Destination

Moved disc from pole 3 to pole 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

Source Auxiliary Destination

Moved disc from pole 1 to pole 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

Source Auxiliary Destination

Moved disc from pole 2 to pole 1.

Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 27 / 41
The Towers of Hanoi

2
1 3

Source Auxiliary Destination

Moved disc from pole 2 to pole 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

Source Auxiliary Destination

Moved disc from pole 1 to pole 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)

move(2, move(1, move(2,


A, B, C) A, C, B) B, C, A)

A -> C

move(1, move(1, move(1, move(1, move(1, move(1,


A, C, B) A, B, C) C, B, A) B, A, C) B, C, A) A, C, B)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 41
The Towers of Hanoi : General

move(n, A, C, B)

move(n-1, A, B, C) move(1, A, C, B) move(n-1, B, C, A)

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 )

• With 64 disks, total number of moves:


264 − 1 ≈ 24 × 260 ≈ 24 × 1018 = 1.6 × 1019
• If one move takes 1s, 264 moves take about 5 × 1011 years (500 billions years).

Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 32 / 41
The Towers of Hanoi

Algorithm move(val disks <integer>, val source <character>, val destination


<character>, val auxiliary <character>)
Move disks from source to destination.
Pre: disks is the number of disks to be moved
Post: steps for moves printed
print("Towers: ", disks, source, destination, auxiliary)
if disks = 1 then
print ("Move from", source, "to", destination)
else
move(disks - 1, source, auxiliary, destination)
move(1, source, destination, auxiliary)
move(disks - 1, auxiliary, destination, source)
end
return
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 33 / 41
Recursion and backtracking
Backtracking

Definition
A process to go back to previous steps to try unexplored alternatives.

Figure 3: Goal seeking

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

Algorithm putQueen(ref board <array>, val r <integer>)


Place remaining queens safely from a row of a chess board.

Pre: board is nxn array representing a chess board


r is the row to place queens onwards

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

for every column c on the same row r do


if cell r,c is safe then
place the next queen in cell r,c
if r < n-1 then
putQueen (board, r + 1)
else
output successful placement
end
remove the queen from cell r,c
end
end
return
End putQueen
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 37 / 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

You might also like