CSC 380: Design and Analysis of Algorithms Dr. Curry Guinn
Quick Info Dr. Curry Guinn CIS 2015 guinnc@uncw.edu www.uncw.edu/people/guinnc 962-7937 Office Hours: MWF: 10:00am-11:00m and by appointment
Today Homework 1 Towers of Hanoi Fibonacci Sequence Brute force! https://www.mathsisfun.com/games/towerofhanoi.html Fibonacci Sequence Brute force! Homework 2 due Sunday night!
Solving recurrence for number of moves M(n) = 2M(n-1) + 1, M(1) = 1 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Towers of Hanoi A recursive solution is short and elegant Else moveDisc(HowMany, startPeg, tempPeg, endPeg) if (HowMany == 1) Move directly from startPeg to endPeg Else moveDisc(HowMany-1, startPeg, endPeg, tempPeg) moveDisc(1, startPeg, tempPeg, endPeg) moveDisc(HowMany-1, tempPeg, startPeg, endPeg)
A very famous recursive sequence The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … How do you generate this sequence?
Fibonacci Sequence and Related Numbers The famous Fibonacci sequence is the result of a question posed by Leonardo de Fibonacci, a mathematician during the Middle Ages. If you begin with one pair of rabbits on the first day of the year, how many pairs of rabbits will you have on the first day of the next year? It is assumed that each pair of rabbits produces a new pair every month and each new pair begins to produce two months after birth. 7
Fig. 2-69, p. 113 8
Fig. 2-70, p. 114 9
Fibonacci Sequence, cont’d The solution to this question is shown in the table below. The sequence that appears three times in the table, 1, 1, 2, 3, 5, 8, 13, 21, … is called the Fibonacci sequence. 10
Fibonacci Sequence, cont’d The Fibonacci sequence is the sequence of numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, … The Fibonacci sequence is found in many places in nature. Any number in the sequence is called a Fibonacci number. The sequence is usually written F1, F2, F3, …, Fn, … 11
Fibonacci Sequence, cont’d For the Fibonacci sequence, the starting values are F1 = 0 and F2 = 1. The recursion rule for the Fibonacci sequence is: Fn = Fn-1 + Fn-2 Example: Find the third number in the sequence using the formula. Let n = 5. 12
Another Version Suppose a tree starts from one shoot that grows for two months and then sprouts a second branch. If each established branch begins to sprout a new branch after one month’s growth, and if every new branch begins to sprout its own first new branch after two month’s growth, how many branches does the tree have at the end of the year? 13
Solution: The number of branches each month in the first year is given in the table and drawn in the figure below. 14
Fibonacci Numbers In Nature The Fibonacci numbers are found many places in the natural world, including: The number of flower petals. The branching behavior of plants. The growth patterns of sunflowers and pinecones, …… It is believed that the spiral nature of plant growth accounts for this phenomenon. 15
Fibonacci Numbers In Nature, cont’d The number of petals on a flower are often Fibonacci numbers. 16
Fibonacci Numbers In Nature, cont’d Mature sunflowers have one set of spirals going clockwise and another set going counterclockwise. The numbers of spirals in each set are usually a pair of adjacent Fibonacci numbers. The most common number of spirals is 34 and 55. 19
The Golden Ratio Consider the ratios of pairs of consecutive Fibonacci numbers. Some of the ratios are calculated in the table shown on the following slide. 20
The Golden Ratio, cont’d 21
The Golden Ratio, cont’d The ratios of pairs of consecutive Fibonacci numbers are also represented in the graph below. The ratios approach the dashed line which represents a number around 1.618. 22
The Golden Ratio, cont’d The irrational number, approximately 1.618, is called the golden ratio. Other names for the golden ratio include the golden section, the golden mean, and the divine proportion. The golden ratio is represented by the Greek letter φ, which is pronounced “fe” or “fi”. 23
The Golden Ratio, cont’d The golden ratio has an exact value of (SQRT(5) + 1)/2 The golden ratio has been used in mathematics, art, and architecture for more than 2000 years. 24
Golden Ratio
Da Vinci Code
Fibonacci numbers The Fibonacci numbers: The Fibonacci recurrence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … The Fibonacci recurrence: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 General 2nd order linear homogeneous recurrence with constant coefficients: aX(n) + bX(n-1) + cX(n-2) = 0 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
n-th Order Linear Homogeneous Recurrence Relations An n-th order recurrence relation defines ak in terms of ak – 1, ak – 2, …, ak – n. A linear recurrence relation defines ak as a linear function of ak – 1, …, ak – i. A linear recurrence relation is homogeneous if the constant term of the linear function is 0.
Second-Order Recurrence Relations A second-order linear homogeneous recurrence relation with constant coefficients is of the form an = Aan – 1 + Ban – 2, for all n 2 for some real numbers A and B, B 0, with initial conditions stated for a0 and a1.
Characteristic Equations Suppose there is a solution of the form an = tn. Then an = Aan – 1 + Ban – 2 tn = Atn – 1 + Btn – 2 t2 = At + B t2 – At – B = 0. This equation is the characteristic equation of the recurrence relation.
Example: Characteristic Equation Let a0 = 0, a1 = 1, an = 4an – 1 – 3an – 2, for all n 2. The characteristic equation is t2 – 4t + 3 = 0. The roots of the characteristic equation are r = 1, s = 3.
Example: The Fibonacci Sequence Define {an} by a0 = 0, a1 = 1, an = an – 1 + an – 1, for all n 2. The characteristic equation is t2 – t – 1 = 0. The roots are r = (1 + 5)/2, s = (1 – 5)/2 .
Example: The Fibonacci Sequence The solution is an = C ((1 + 5)/2)n + D ((1 – 5)/2)n for some choice of C and D. Solve for C and D: a0 = C + D = 0. a1 = C(1 + 5)/2 + D(1 – 5)/2 = 1. Therefore, C = 1/5 and D = –1/5.
Example: The Fibonacci Sequence The solution is an = (1/5)[((1 + 5)/2)n – ((1 – 5)/2)n] = [((1 + 5)/2)n – ((1 – 5)/2)n]/5. As n gets large, ((1 – 5)/2)n approaches 0. Therefore, for large n, an ((1 + 5)/2)n/5.
Implementations of Fibonacci #inefficient def fib(n): if n <= 1: return n else: return fib(n-1) + fib(n-2) #efficient def betterFib(n): if n <= 1: return n nMinus2 = 0 nMinus1 = 1 total = 0 for i in range(2, n+1): total = nMinus1 + nMinus2 nMinus2 = nMinus1 nMinus1 = total return total
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing an (a > 0, n a nonnegative integer) Computing n! Multiplying two matrices Searching for a key of a given value in a list A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
For Next Class, Monday Homework 2 due Sunday night! Chapter 3 – Brute force algorithms Homework 2 due Sunday night!