+ All Categories
Home > Documents > CPE 130 Algorithms and Data Structures

CPE 130 Algorithms and Data Structures

Date post: 01-Jan-2016
Category:
Upload: mckenzie-davenport
View: 12 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
Popular Tags:
26
CPE 130 Algorithms CPE 130 Algorithms and Data and Data Structures Structures Recursion Recursion Asst. Prof. Dr. Nuttanart Facundes 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


Recommended