Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPE 130 Algorithms and Data Structures Recursion Asst. Prof. Dr. Nuttanart Facundes.

Similar presentations


Presentation on theme: "CPE 130 Algorithms and Data Structures Recursion Asst. Prof. Dr. Nuttanart Facundes."— Presentation transcript:

1

2 CPE 130 Algorithms and Data Structures Recursion Asst. Prof. Dr. Nuttanart Facundes

3 Leonardo Fibonacci In 1202, Fibonacci proposed a problem about the growth of rabbit populations.

4 Leonardo Fibonacci In 1202, Fibonacci proposed a problem about the growth of rabbit populations.

5 Leonardo Fibonacci In 1202, Fibonacci proposed a problem about the growth of rabbit populations.

6 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 month1234567 rabbits112358 13131313

7 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 month1234567 rabbits112358 13131313

8 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 month1234567 rabbits112358 13131313

9 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 month1234567 rabbits112358 13131313

10 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 month1234567 rabbits112358 13131313

11 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 month1234567 rabbits112358 13131313

12 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 month1234567 rabbits112358 13131313

13 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)n01234567Fib(n)%112358 13131313

14 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)n01234567Fib(n)0112358 13131313

15 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.

16 Figure 6-9

17 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);}

18 Great Pyramid at Gizeh

19 The ratio of the altitude of a face to half the base b a

20 Notre Dame in Paris: Phi

21 Golden Ratio: the divine proportion  = 1.6180339887498948482045… “Phi” is named after the Greek sculptor Phidias How is the Golden Ratio related to Fibonacci numbers?

22 Tower of Hanoi

23 Recursion case study: Tower of Hanoi We need to do 2 n – 1 moves to achieve the task, while n = the number of disks.

24 Solution for 2 disks

25 Solution for 3 disks, Part I

26 Solution for 3 disks, Part II

27 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


Download ppt "CPE 130 Algorithms and Data Structures Recursion Asst. Prof. Dr. Nuttanart Facundes."

Similar presentations


Ads by Google