Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 380: Design and Analysis of Algorithms

Similar presentations


Presentation on theme: "CSC 380: Design and Analysis of Algorithms"— Presentation transcript:

1 CSC 380: Design and Analysis of Algorithms
Dr. Curry Guinn

2 Quick Info Dr. Curry Guinn CIS 2015 guinnc@uncw.edu
Office Hours: MWF: 10:00am-11:00m and by appointment

3 Today Finish up analysis of non-recursive code Analyze recursive code
Homework 2 due Sunday night

4 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 += total += mysteryMethod(n) print(total/count)

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

6 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

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

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

9 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

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

11 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

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

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

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

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

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

17 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

18 Fig. 2-69, p. 113 18

19 Fig. 2-70, p. 114 19

20 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

21 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

22 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

23 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

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

25 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

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

27

28

29 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

30 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

31 The Golden Ratio, cont’d
31

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

33 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

34 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

35 Golden Ratio

36 Da Vinci Code

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

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

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

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

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

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

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

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

45 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 = nMinus1 = total = for i in range(2, n+1): total = nMinus1 + nMinus nMinus2 = nMinus nMinus1 = total return total

46 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


Download ppt "CSC 380: Design and Analysis of Algorithms"

Similar presentations


Ads by Google