260 likes | 334 Vues
Dive into the fascinating world of Fibonacci numbers and recursion with this detailed study of rabbit populations, the Fibonacci sequence, and solving the Tower of Hanoi puzzle. Understand the connection between the Golden Ratio and Fibonacci numbers.
E N D
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 • Fn= # of rabbit pairs at the beginning of the nth month
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 • Fn= # of rabbit pairs at the beginning of the nth month
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 • Fn= # of rabbit pairs at the beginning of the nth month
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 • Fn= # of rabbit pairs at the beginning of the nth month
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 • Fn= # of rabbit pairs at the beginning of the nth month
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 • Fn= # of rabbit pairs at the beginning of the nth month
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 • Fn= # of rabbit pairs at the beginning of the nth month
Inductive Definition or Recurrence Relation for theFibonacci 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)
Inductive Definition or Recurrence Relation for theFibonacci 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)
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.
Algorithm for Fibonacci Numbers The algorithm for calculating Fibonacci numbers: int fib(int n) { if (n < 2) return n; else return fib(n – 1) + fib(n – 2); }
a b The ratio of the altitude of a face to half the base
Golden Ratio: the divine proportion • = 1.6180339887498948482045… • “Phi” is named after the Greek sculptor Phidias • How is the Golden Ratio related to Fibonacci numbers?
Recursion case study: Tower of Hanoi We need to do 2n – 1 moves to achieve the task, while n = the number of disks.
Tower of Hanoi: The Algorithm • Move n – 1 disks from source to auxiliary General Case • Move one disk from source to destination Base Case • Move n – 1 disks from auxiliary to destinationGeneral Case