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

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

Dec 17, 2015

Download

Documents

Jeremy Hamilton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CPE 130 Algorithms and Data Structures Recursion Asst. Prof. Dr. Nuttanart Facundes.

CPE 130 Algorithms and CPE 130 Algorithms and Data StructuresData Structures

RecursionRecursion

Asst. Prof. Dr. Nuttanart FacundesAsst. Prof. Dr. Nuttanart Facundes

Page 2: CPE 130 Algorithms and Data Structures Recursion Asst. 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.

Page 3: CPE 130 Algorithms and Data Structures Recursion Asst. 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.

Page 4: CPE 130 Algorithms and Data Structures Recursion Asst. 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

Figure 6-9

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

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

}}

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

Great Pyramid at GizehGreat Pyramid at Gizeh

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

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

ba

a

b1.618

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

Notre Dame in Paris: Phi

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

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?

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

Tower of Hanoi

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

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.

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

Solution for 2 disks

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

Solution for 3 disks, Part I

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

Solution for 3 disks, Part II

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

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