Ch03 Recursion PDF
Ch03 Recursion PDF
Chapter 3 - Recursion
2. Properties of recursion
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 1 / 38
Outcomes
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 2 / 38
Recursion and the basic
components of recursive
algorithms
Recursion
Denition
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: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 3 / 38
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: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 4 / 38
Recursion
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 5 / 38
Recursion
i=1
factoN = 1
while i <= n do
factoN = factoN * i
i=i+1
end
return factoN
End iterativeFactorial
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 6 / 38
Recursion
if n = 0 then
return 1
else
return n * recursiveFactorial(n-1)
end
End recursiveFactorial
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 7 / 38
Recursion
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 9 / 38
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: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 10 / 38
Limitations of Recursion
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 11 / 38
Print List in Reverse
93 19 8 26
26 8 19 93
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 12 / 38
Print List in Reverse
93 19 8 26
26 8 19 93
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 13 / 38
Print List in Reverse
Algorithm printReverse(list)
Prints a linked list in reverse.
Pre: list has been built
Post: list printed in reverse
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 14 / 38
Greatest Common Divisor
Denition
" 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: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 15 / 38
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: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 16 / 38
Fibonacci Numbers
Denition
" 0 if n = 0
F ibonacci(n) = 1 if n = 1
F ibonacci(n − 1) + F ibonacci(n − 2) otherwise
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 17 / 38
Fibonacci Numbers
Fib(n)
Fib(n-1) + Fib(n-2)
Fib(n-3) + Fib(n-4)
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 18 / 38
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: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 19 / 38
Fibonacci Numbers
Algorithm b(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 b(n-1) + b(n-2)
End b
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 20 / 38
Fibonacci Numbers
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 21 / 38
The Towers of Hanoi
1
2
3
Source Auxiliary Destination
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 22 / 38
The Towers of Hanoi
2
3 1
Source Auxiliary Destination
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 23 / 38
The Towers of Hanoi
3 2 1
Source Auxiliary Destination
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 24 / 38
The Towers of Hanoi
1
3 2
Source Auxiliary Destination
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 25 / 38
The Towers of Hanoi
1
2 3
Source Auxiliary Destination
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 26 / 38
The Towers of Hanoi
1 2 3
Source Auxiliary Destination
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 27 / 38
The Towers of Hanoi
2
1 3
Source Auxiliary Destination
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 28 / 38
The Towers of Hanoi
1
2
3
Source Auxiliary Destination
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 29 / 38
The Towers of Hanoi
move(3,
A, C, B)
A -> C
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 30 / 38
The Towers of Hanoi : General
move(n, A, C, B)
Complexity
T (n) = 1 + 2T (n − 1)
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 31 / 38
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: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 32 / 38
The Towers of Hanoi
Denition
A process to go back to previous steps to try unexplored alternatives.
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 34 / 38
Eight Queens Problem
Place eight queens on the chess board in such a way that no queen can
capture another.
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 35 / 38
Eight Queens Problem
Post: all the remaining queens are safely placed on the board; or
backtracking to the previous rows is required
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 36 / 38
Eight Queens Problem
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 37 / 38
Eight Queens Problem
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 38 / 38