Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures
Lecture aid Math Review: logarithmic functions
Lecture aid 3 properties of exponentials: a (b+c) = a b a c a bc = (a b ) c a b /a c = a (b-c) b = a log a b b c = a c*log a b properties of logarithms: log b (xy) = log b x + log b y log b (x/y) = log b x - log b y log b x a = alog b x log b a = log x a/log x b Math you need to Review
Lecture aid Logarithmic/Exponential Problems. Evaluate and/or simplify. 2 a =8 2 a =1024 2 a = n 2 log 8 32 2 log 4 9 log 12 2 log 9 == 2 12 == 2 7 3 15 /3 10
Lecture aid 5 Asymptotic Notation Big-Oh f(n) is O(g(n)) if f(n) is asymptotically less than or equal to g(n)
Lecture aid Big-Oh Growth Rate f(N) = O(g(N)) iff there are positive constants c and n 0 such that f(N) c g(N) when N n 0 – The growth rate of f(N) is less than or equal to the growth rate of g(N) – g(N) is an upper bound on f(N) – f(N) grows no faster than g(N) for “large” N
Lecture aid Big Oh: more examples N 2 / 2 – 3N = O(N 2 ) iff ∃ c, n 0 > 0 such that N 2 / 2 – 3N n 0 Proof N 2 / 2 – 3N <= N 2 – 3N (because N 2 / 2 is less than N 2 ) 0) = O(N 2 ) when c = 1 and n 0 = 1 NN2N2 N 2 /2-3N
Lecture aid Big Oh: more examples N 2 + 3N = O(N 2 ) iff ∃ c, n 0 > 0 such that N 2 + 3N = n 0 Proof 3n = 3, so N 2 + 3n = 3 So N 2 + 3n < 2N 2 = O(N 2 ) where c = 2 and n 0 = 3 NN2N2 N 2 +3N
Lecture aid Big Oh: more examples 7N N + 3 = O(N 2 ) = O(N 3 ) Proof 10N and some c > 0 i.e, 10N , c >= 1 So, 7N N = 8N 2 = O(N 2 ) ∀ n > 10, c >= 8 NN2N2 10N
Lecture aid Big Oh: more examples Prove the following 7N N + 3 = O(N 3 ) N N N = O(N 3 )
Lecture aid Big Oh: more examples log 10 N = O(log 2 N) = O(log N) Proof: log 10 N = log 2 N / log 2 10 = log 2 N / 3.32 = 1, c >= 1 = O(log 2 N) = O(log N) since log 2 N = log k N / log k 2 = O(log k N)
Lecture aid Big Oh: more examples log N + N = O(?)
Lecture aid Big Oh: more examples N 2 / 2 – 3N = O(N 2 ) 1 + 4N = O(N) 7N N + 3 = O(N 2 ) = O(N 3 ) log 10 N = log 2 N / log 2 10 = O(log 2 N) = O(log N) sin N = O(1); 10 = O(1), = O(1) log N + N = O(?) log k N = O(N) for any constant k N = O(2 N ), but 2 N is not O(N) 2 10N is not O(2 N )
Lecture aid 14 Computing Prefix Averages Asymptotic analysis with two algorithms for prefix averages The i -th prefix average of an array X is average of the first (i 1) elements of X: A[i] X[0] X[1] … X[i])/(i+1) Computing the array A of prefix averages of another array X has applications to financial analysis
Lecture aid 15 A Prefix Averages Algorithm Algorithm prefixAverages1(X) Input array X of n integers Output array A of prefix averages of X A new array of n integers for i 0 to n 1 do s X[0] for j 1 to i do s s X[j] A[i] s (i 1) return A Algorithm prefixAverages2 runs in O(?) time
Lecture aid 16 Another Prefix Averages Algorithm Algorithm prefixAverages2(X) Input array X of n integers Output array A of prefix averages of X A new array of n integers s 0 for i 0 to n 1 do s s X[i] A[i] s (i 1) return A Algorithm prefixAverages2 runs in O(?) time
Lecture aid 17 ______________________ Devon M. Simmonds Computer Science Department University of North Carolina Wilmington _____________________________________________________________ Qu es ti ons? Reading from course text: