CPE 130 Algorithms and Data Structures Recursion Asst. Prof. Dr. Nuttanart Facundes
Leonardo Fibonacci In 1202, Fibonacci proposed a problem about the growth of rabbit populations.
Leonardo Fibonacci In 1202, Fibonacci proposed a problem about the growth of rabbit populations.
Leonardo Fibonacci In 1202, Fibonacci proposed a problem about the growth of rabbit populations.
The rabbit reproduction model A rabbit lives forever The population starts as a single newborn pair Every month, each productive pair begets a new pair which will become productive after 2 months old F n = # of rabbit pairs at the beginning of the n th month month rabbits
The rabbit reproduction model A rabbit lives forever The population starts as a single newborn pair Every month, each productive pair begets a new pair which will become productive after 2 months old F n = # of rabbit pairs at the beginning of the n th month month rabbits
The rabbit reproduction model A rabbit lives forever The population starts as a single newborn pair Every month, each productive pair begets a new pair which will become productive after 2 months old F n = # of rabbit pairs at the beginning of the n th month month rabbits
The rabbit reproduction model A rabbit lives forever The population starts as a single newborn pair Every month, each productive pair begets a new pair which will become productive after 2 months old F n = # of rabbit pairs at the beginning of the n th month month rabbits
The rabbit reproduction model A rabbit lives forever The population starts as a single newborn pair Every month, each productive pair begets a new pair which will become productive after 2 months old F n = # of rabbit pairs at the beginning of the n th month month rabbits
The rabbit reproduction model A rabbit lives forever The population starts as a single newborn pair Every month, each productive pair begets a new pair which will become productive after 2 months old F n = # of rabbit pairs at the beginning of the n th month month rabbits
The rabbit reproduction model A rabbit lives forever The population starts as a single newborn pair Every month, each productive pair begets a new pair which will become productive after 2 months old F n = # of rabbit pairs at the beginning of the n th month month rabbits
Inductive Definition or Recurrence Relation for the Fibonacci Numbers Stage 0, Initial Condition, or Base Case: Fib(1) = 1; Fib (2) = 1 Inductive Rule For n>3, Fib(n) = Fib(n-1) + Fib(n-2)n Fib(n)%
Inductive Definition or Recurrence Relation for the Fibonacci Numbers Stage 0, Initial Condition, or Base Case: Fib(0) = 0; Fib (1) = 1 Inductive Rule For n>1, Fib(n) = Fib(n-1) + Fib(n-2)n Fib(n)
Recursion case study: Fibonacci Numbers Fibonacci numbers are a series in which each number is the sum of the previous two numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
Figure 6-9
Algorithm for Fibonacci Numbers The algorithm for calculating Fibonacci numbers: intfib(int n) { if (n < 2) return n; if (n < 2) return n;else return fib(n – 1) + fib(n – 2); return fib(n – 1) + fib(n – 2);}
Great Pyramid at Gizeh
The ratio of the altitude of a face to half the base b a
Notre Dame in Paris: Phi
Golden Ratio: the divine proportion = … “Phi” is named after the Greek sculptor Phidias How is the Golden Ratio related to Fibonacci numbers?
Tower of Hanoi
Recursion case study: Tower of Hanoi We need to do 2 n – 1 moves to achieve the task, while n = the number of disks.
Solution for 2 disks
Solution for 3 disks, Part I
Solution for 3 disks, Part II
Tower of Hanoi: The Algorithm 1.Move n – 1 disks from source to auxiliaryGeneral Case 2.Move one disk from source to destinationBase Case 3.Move n – 1 disks from auxiliary to destinationGeneral Case