CSC 380: Design and Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Discrete Structures Chapter 6 Recurrence Relations
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Analysis of Recursive Algorithms
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Analysis of Recursive Algorithms October 29, 2014
Applied Discrete Mathematics Week 9: Relations
Advanced Counting Techniques
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Fall 2015 COMP 2300 Discrete Structures for Computation Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1.
Analysis of Algorithms
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.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
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
Analysis of Algorithms
Analysis of algorithms
Analysis of algorithms
Analysis of Algorithms
Modeling with Recurrence Relations
Introduction to the Design and Analysis of Algorithms
Analysis of Algorithms
Introduction to Recurrence Relations
Analysis of Algorithms
Analysis of algorithms
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis
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
Design and Analysis of Algorithms
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Divide-and-Conquer The most-well known algorithm design strategy:
Analysis of algorithms
Decrease-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
CS 2210 Discrete Structures Advanced Counting
Solving Recurrence Relations
Divide-and-Conquer The most-well known algorithm design strategy:
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
At the end of this session, learner will be able to:
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
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 Finish up analysis of non-recursive code Analyze recursive code Homework 2 due Sunday night

What is the Big-O of the following code? n = int(input('Enter a number: ')) count = 0 total = 0 for i in range(n): count += 1 total += mysteryMethod(n) print(total/count)

What about this code? myDictionary = {} words = [] file = open('mobydick.txt', 'r') for line in file: tokens = line.split() for w in tokens: w = w.strip().lower() myDictionary[w] = w words.append(w) print(len(words), len(myDictionary)) #print out the index of the first occurrence of each word # what is the big O? for w in myDictionary: print(w, words.index(w))

What about recursive code? Generally, no loops at all. Set up a recurrence relation with an appropriate initial condition expressing the number of times the basic op. is executed. Solve the recurrence (or, at the very least, establish its solution’s order of growth) by backward substitutions or another method

Example 1: Recursive evaluation of n! Definition: n ! = 1  2  …  (n-1)  n for n ≥ 1 and 0! = 1 Recursive definition of n!: F(n) = F(n-1)  n for n ≥ 1 and F(0) = 1 Size: Basic operation: Recurrence relation: Note the difference between the two recurrences. Students often confuse these! F(n) = F(n-1) n F(0) = 1 for the values of n! ------------ M(n) =M(n-1) + 1 M(0) = 0 for the number of multiplications made by this algorithm A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Solving the recurrence for M(n) M(n) = M(n-1) + 1, M(0) = 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.

Analysis of mergesort Let T(N) denote the worst-case running time of mergesort to sort N numbers. Assume that N is a power of 2. Divide step: O(1) time Conquer step: 2 T(N/2) time Combine step: O(N) time Recurrence equation: T(1) = 1 T(N) = 2T(N/2) + N

Solving the Recurrence Relation See board Other important recurrence relations T(n) = T(n – 1) + 1 O(n) T(n) = T(n/2) + 1 O(log n) T(n) = T(n – 1) + n O(n2)

Master Theorem Let T(n) be a monotonically increasing function that satisfies T(n) = a T(n/b) + f(n) T(1) = c where a  1, b  2, c>0. If f(n) is (nd) where d  0 then if a < bd T(n) = If a = bd if a > bd

Example 2: The Tower of Hanoi Puzzle Recurrence for number of moves: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Tree of calls for the Tower of Hanoi Puzzle A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

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

Fig. 2-69, p. 113 18

Fig. 2-70, p. 114 19

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

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, … 21

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 = 3. 22

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? 23

Solution: The number of branches each month in the first year is given in the table and drawn in the figure below. 24

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

Fibonacci Numbers In Nature, cont’d The number of petals on a flower are often Fibonacci numbers. 26

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

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

The Golden Ratio, cont’d 31

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

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

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

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

For Next Class, Friday Homework 2 due Sunday We leave Chapter 2, and move on to Chapter 3 – Brute force algorithms Homework 2 due Sunday