1 / 26

CPE 130 Algorithms and Data Structures

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.

Download Presentation

CPE 130 Algorithms and Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  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 • Fn= # of rabbit pairs at the beginning of the nth month

  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 • Fn= # of rabbit pairs at the beginning of the nth month

  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 • Fn= # of rabbit pairs at the beginning of the nth month

  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 • Fn= # of rabbit pairs at the beginning of the nth month

  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 • Fn= # of rabbit pairs at the beginning of the nth month

  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 • Fn= # of rabbit pairs at the beginning of the nth month

  12. 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)

  13. 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)

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

  15. Figure 6-9

  16. 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); }

  17. Great Pyramid at Gizeh

  18. a b The ratio of the altitude of a face to half the base

  19. Notre Dame in Paris: Phi

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

  21. Tower of Hanoi

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

  23. Solution for 2 disks

  24. Solution for 3 disks, Part I

  25. Solution for 3 disks, Part II

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

More Related