220 likes | 324 Views
First Questions for Algorithm Analysis. Space Input magnitude (the number of bits needed to represent the input Number of inputs Space required to store the inputs, perform intermediate computations, and store outputs Time. Measurement Units. Definition of g(n) ε O(f(n)).
E N D
Space • Input magnitude (the number of bits needed to represent the input • Number of inputs • Space required to store the inputs, perform intermediate computations, and store outputs • Time Measurement Units
Definition of g(n) ε O(f(n)). The function g(n) is in O(f(n)) if there exist non negative constants c and such that g(n) ≤ c f(n) for all n ≥ . Big O(.)
Is the O() = Φ? • Are there decision questions that cannot be computed by any algorithm? Do things ever get worse?
Definition: g(n) εΩ(f(n)). What does it mean to be bounded below? • The function g(n) is in Ω(f(n)) if there exist constants c>0 and nonnegative such that g(n) ≥ c f(n) for all n ≥ . • Definition: g(n) εΘ(f(n)). What does it mean to be bounded above and below? • The function g(n) is in Θ(f(n)) if there exist non negative constants c1, c2 and such that c1 f(n) ≤g(n) ≤ c2 f(n)for all n ≥ . Other bounds
How should input size be measured? • Number of items in a list • Value of a parameter or the number of bits needed to store the parameter • What is the program’s most frequently occurring operation? • Look inside loops • Are there other factors than input size that affect the frequency of the most frequent operation? • Separate best, worst and average case analyses • How to tally the frequency? • Summations • Profiling Questions to answer when Analyzing non-recursive algorithms
Find max • Given a list containing n>0 integers (It is a separate question to ask the time for set-up. What would it be for this example?) • Pseudo-code max = list[0] for i = 0 to n-1 if list[i]>max max = list[i] • Analysis? Example 1
max = list[0] • O(1) = load + store • if… • Evaluate condition (a difference) • Conditional transfer (a jump, branch or skip depending upon flag values) • Assignment = load + store • O(??) • for… • Iterates n times • Total accumulated time = ? Example 1 Analysis
To understand recursion one must first understand recursion • Questions • How will one measure input size? • What is the core operation? • Do other factors than input size influence the number of repetitions? • What is a recurrence expressing the repetition of the core operation? • Solve the recurrence? Analysis of recursive algorithms
import random • import math • c = [0, 0] • alist=[] • n = int(input('Number of items for list = ' )) • for i in range(n): • x = random.randint(1, 1000) • alist.append(x) • ##print(len(alist)) • ##print('The initial list = ', alist) • defrecursfindmax(alis): • print(alis) • if len(alis) == 1: • c[0]+=1 • return alis[0] • else: • c[0]+=1 • amax = max(alis[0], recursfindmax(alis[1:len(alis)])) • return(amax) • deffindmax(alis): • amax = alis[0] • for i in range(len(alis)): • c[1]+=1 • if amax < alis[i]: • amax = alis[i] • return (amax) • print('Recursive max = ', recursfindmax(alist)) • print('Iterative max = ', findmax(alist)) • print('Count recursive, iterative ', c) Example 2:A recursive algorithm
t(n) = 1 + t(n-1) • t(n-1) = 1+ t(n-2) • t(n-2) = 1 + t(n-3) • t(n-3) = 1 + t(n-4) • … • t(n-(n-2)) = 1 + t(n-1) • t(n-n-1)) = t(1) = 1; this is the anchor • t(n) = 1 + t(n-1) = 2 + t(n-2) = 3 + t(n-3) = … = (n-1) + t(n-(n-1)) = n Analysis
Both methods are O(n), where n is the number of items in the list • How do the algorithms compare with respect to space utilization? Comparison
Given a homogeneous, second order, linear recurrence with constant coefficients (a≠0) of the form a y[n] + b y[n-1] + c y[n-2] = 0The recurrence has a closed form solution depending upon the roots and of the characteristic equationand the initial conditions for y[0] and y[1] Fun fact
If the roots are real and distinct • If there is a repeated root • If the roots are imaginary , and Fun fact (cont’d)
Fib(0) = 0 • Fib(1) = 1 • Fib(n) = Fib(n-1) + Fib(n-2) • Produces the homogeneous recurrence:Fib(n) – Fib(n-1) – Fib(n-2) = 0 • With characteristic equation: • Where a = 1, b = -1 and c = -1 Consider the fibonacci number recurrence
Thus, • The roots are real and distinct so that the recurrence is expressed by Solving the equation
Recall that Fib(0) = 0, Fib(1) = 1, and • Applying the first condition yields: • Applying the second condition yields: • Since , the second becomes: • Thus, and so that Applying initial conditions
Given • Recall and note that, • Thus, and notes
A(n) = 1 (addition)+1 A(n-1)+ 1A(n-2)=1 + 1 +A(n-2)+A(n-3)+ A(n-2)= 2 + 2 A(n-2) + 1 A(n-3)= 2 + 2(1+A(n-3)+A(n-4))+A(n-3)= 4 + 3 A(n-3) + 2 A(n-4)= 4 + 3(1+A(n-4)+A(n-5))+2A(n-4)= 7 + 5 A(n-4)+3 A(n-5)= 7 + 5(1 + A((n-5)+A(n-6))+ 3 A(n-5)= 12 + 8 A(n-5) + 5 A(n-6)= …= (Fib(i+2) -1)+Fib(i+1)A(n-i)+Fib(i)A(n-(i+1)) • Letting i = n-1, yields A(n) = Fib(n+1)-1,since A(0)=A(1)=0 Counting additions in recursive fibonacci
c=[0,0,0,0,0] • def fib(n): • c[0]+=1 • if n>1: • c[1]+=1 • return fib(n-1)+fib(n-2) • elif n==0: • c[2]+=1 • return 0 • else: • c[3]+=1 • return 1 • c[4]+=1 • print(fib(int(input("Supply a number: ")))) • print(c) Empirical Verficiation