cpe 130 algorithms and data structures recursion asst. prof. dr. nuttanart facundes
TRANSCRIPT
CPE 130 Algorithms and CPE 130 Algorithms and Data StructuresData Structures
RecursionRecursion
Asst. Prof. Dr. Nuttanart FacundesAsst. Prof. Dr. Nuttanart Facundes
Leonardo FibonacciLeonardo Fibonacci
In 1202, Fibonacci proposed a problem In 1202, Fibonacci proposed a problem about the growth of rabbit populations.about the growth of rabbit populations.
Leonardo FibonacciLeonardo Fibonacci
In 1202, Fibonacci proposed a problem In 1202, Fibonacci proposed a problem about the growth of rabbit populations.about the growth of rabbit populations.
Leonardo FibonacciLeonardo Fibonacci
In 1202, Fibonacci proposed a problem In 1202, Fibonacci proposed a problem about the growth of rabbit populations.about the growth of rabbit populations.
The rabbit reproduction modelThe rabbit reproduction modelA rabbit lives foreverA rabbit lives forever
The population starts as a single newborn pairThe population starts as a single newborn pair
Every month, each productive pair begets a Every month, each productive pair begets a new pair which will become productive after 2 new pair which will become productive after 2 months oldmonths old
FFnn= = # of rabbit pairs at the beginning of the n# of rabbit pairs at the beginning of the n thth
monthmonth
monthmonth 11 22 33 44 55 66 77
rabbitsrabbits 11 11 22 33 55 881133
The rabbit reproduction modelThe rabbit reproduction modelA rabbit lives foreverA rabbit lives forever
The population starts as a single newborn pairThe population starts as a single newborn pair
Every month, each productive pair begets a Every month, each productive pair begets a new pair which will become productive after 2 new pair which will become productive after 2 months oldmonths old
FFnn= = # of rabbit pairs at the beginning of the n# of rabbit pairs at the beginning of the n thth
monthmonth
monthmonth 11 22 33 44 55 66 77
rabbitsrabbits 11 11 22 33 55 881133
The rabbit reproduction modelThe rabbit reproduction modelA rabbit lives foreverA rabbit lives forever
The population starts as a single newborn pairThe population starts as a single newborn pair
Every month, each productive pair begets a Every month, each productive pair begets a new pair which will become productive after 2 new pair which will become productive after 2 months oldmonths old
FFnn= = # of rabbit pairs at the beginning of the n# of rabbit pairs at the beginning of the n thth
monthmonth
monthmonth 11 22 33 44 55 66 77
rabbitsrabbits 11 11 22 33 55 881133
The rabbit reproduction modelThe rabbit reproduction modelA rabbit lives foreverA rabbit lives forever
The population starts as a single newborn pairThe population starts as a single newborn pair
Every month, each productive pair begets a Every month, each productive pair begets a new pair which will become productive after 2 new pair which will become productive after 2 months oldmonths old
FFnn= = # of rabbit pairs at the beginning of the n# of rabbit pairs at the beginning of the n thth
monthmonth
monthmonth 11 22 33 44 55 66 77
rabbitsrabbits 11 11 22 33 55 881133
The rabbit reproduction modelThe rabbit reproduction modelA rabbit lives foreverA rabbit lives forever
The population starts as a single newborn pairThe population starts as a single newborn pair
Every month, each productive pair begets a Every month, each productive pair begets a new pair which will become productive after 2 new pair which will become productive after 2 months oldmonths old
FFnn= = # of rabbit pairs at the beginning of the n# of rabbit pairs at the beginning of the n thth
monthmonth
monthmonth 11 22 33 44 55 66 77
rabbitsrabbits 11 11 22 33 55 881133
The rabbit reproduction modelThe rabbit reproduction modelA rabbit lives foreverA rabbit lives forever
The population starts as a single newborn pairThe population starts as a single newborn pair
Every month, each productive pair begets a Every month, each productive pair begets a new pair which will become productive after 2 new pair which will become productive after 2 months oldmonths old
FFnn= = # of rabbit pairs at the beginning of the n# of rabbit pairs at the beginning of the n thth
monthmonth
monthmonth 11 22 33 44 55 66 77
rabbitsrabbits 11 11 22 33 55 881133
The rabbit reproduction modelThe rabbit reproduction modelA rabbit lives foreverA rabbit lives forever
The population starts as a single newborn pairThe population starts as a single newborn pair
Every month, each productive pair begets a Every month, each productive pair begets a new pair which will become productive after 2 new pair which will become productive after 2 months oldmonths old
FFnn= = # of rabbit pairs at the beginning of the n# of rabbit pairs at the beginning of the n thth
monthmonth
monthmonth 11 22 33 44 55 66 77
rabbitsrabbits 11 11 22 33 55 881133
Inductive Definition or Inductive Definition or Recurrence Relation for theRecurrence Relation for the
Fibonacci NumbersFibonacci Numbers Stage 0, Initial Condition, or Base Case:Fib(1) = 1; Fib (2) = 1
Inductive RuleFor n>3, Fib(n) = Fib(n-1) + Fib(n-2)
nn 00 11 22 33 44 55 66 77
Fib(n)Fib(n) %% 11 11 22 33 55 881133
Inductive Definition or Inductive Definition or Recurrence Relation for theRecurrence Relation for the
Fibonacci NumbersFibonacci Numbers Stage 0, Initial Condition, or Base Case:Fib(0) = 0; Fib (1) = 1
Inductive RuleFor n>1, Fib(n) = Fib(n-1) + Fib(n-2)
nn 00 11 22 33 44 55 66 77
Fib(n)Fib(n) 00 11 11 22 33 55 881133
Recursion case study: Fibonacci Recursion case study: Fibonacci NumbersNumbers
Fibonacci numbers are a series in which Fibonacci numbers are a series in which each number is the sum of the previous each number is the sum of the previous two numbers:two numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
Figure 6-9
Algorithm for Fibonacci NumbersAlgorithm for Fibonacci Numbers
The algorithm for calculating Fibonacci The algorithm for calculating Fibonacci numbers:numbers:
intint fib(int n) {fib(int n) { if (n < 2) return n;if (n < 2) return n;
elseelse return fib(n – 1) + fib(n – 2);return fib(n – 1) + fib(n – 2);
}}
Great Pyramid at GizehGreat Pyramid at Gizeh
The ratio of the altitude of a face to half the base
ba
a
b1.618
Notre Dame in Paris: Phi
Golden Ratio: the divine Golden Ratio: the divine proportionproportion
= = 1.6180339887498948482045…1.6180339887498948482045…
““Phi” is named after the Greek sculptor Phi” is named after the Greek sculptor PhiPhidiasdias
How is the Golden Ratio related to How is the Golden Ratio related to Fibonacci numbers?Fibonacci numbers?
Tower of Hanoi
Recursion case study: Tower of Recursion case study: Tower of Hanoi Hanoi
We need to do 2We need to do 2n n – 1 moves to achieve the – 1 moves to achieve the task, while task, while nn = the number of disks. = the number of disks.
Solution for 2 disks
Solution for 3 disks, Part I
Solution for 3 disks, Part II
Tower of Hanoi: The AlgorithmTower of Hanoi: The Algorithm
1.1. Move n – 1 disks from source to auxiliaryMove n – 1 disks from source to auxiliary General General CaseCase
2.2. Move one disk from source to destinationMove one disk from source to destination Base Base CaseCase
3.3. Move n – 1 disks from auxiliary to destinationMove n – 1 disks from auxiliary to destination General CaseGeneral Case