CSC 380: Design and Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin Introduction to the Design & Analysis of Algorithms, 2 nd ed., Ch. 1 Chapter 2.
Advertisements

Introduction to Algorithms
1 Recursive Algorithm Analysis Dr. Ying Lu RAIK 283: Data Structures & Algorithms September 13, 2012.
CPE 130 Algorithms and Data Structures Recursion Asst. Prof. Dr. Nuttanart Facundes.
Discrete Structures Chapter 6 Recurrence Relations
Lecture 3, Tuesday, Aug. 29. Chapter 2: Single species growth models, continued 2.1. Linear difference equations, Fibonacci number and golden ratio. Required.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
Applied Discrete Mathematics Week 9: Relations
Advanced Counting Techniques
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
CSE 2813 Discrete Structures Recurrence Relations Section 6.1.
RECURRENCE Sequence Recursively defined sequence
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
CSE 2813 Discrete Structures Solving Recurrence Relations Section 6.2.
Agenda Lecture Content:  Recurrence Relations  Solving Recurrence Relations  Iteration  Linear homogenous recurrence relation of order k with constant.
Fibonacci Sequence and Related Numbers
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
RECURRENCE Sequence Recursively defined sequence
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Math Journal Is this graph a Function? Yes or No Domain: ______________________ Range: _______________________.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 RECURRENCE 1. Sequence 2. Recursively defined sequence 3. Finding an explicit formula for recurrence relation.
Mathematical Analysis of Recursive Algorithm CSG3F3 Lecture 7.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
CSG523/ Desain dan Analisis Algoritma
CSG523/ Desain dan Analisis Algoritma
Fibonacci’s rabbits Fibonacci posed the following problem:
Analysis of algorithms
Analysis of algorithms
ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms Chapter 10 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second.
Introduction to the Design and Analysis of Algorithms
Introduction to Recurrence Relations
Analysis of algorithms
Introduction to the Design and Analysis of Algorithms
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Divide-and-Conquer The most-well known algorithm design strategy:
9 The Mathematics of Spiral Growth
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Decrease-and-Conquer
CMSC Discrete Structures
Recursion: The Mirrors
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Analysis of algorithms
CSC 380: Design and Analysis of Algorithms
Analysis of algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Recurrence Relations Discrete Structures.
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Presentation transcript:

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!