lecture aid 1 devon m. simmonds university of north carolina, wilmington csc231 data structures

17
Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures Analysis of Algorithm

Upload: nora-mills

Post on 18-Jan-2018

216 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

1

Devon M. SimmondsUniversity of North Carolina,

Wilmington

CSC231Data Structures

Analysis of Algorithms

Page 2: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

Math Review: logarithmic functions

xdxxd

aaa

na

aba

abb

baababiffbx

e

bbb

an

b

m

ma

xa

1loglog)(loglog

loglog

logloglog

loglogloglog

loglog

Page 3: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

3

• properties of exponentials:a(b+c) = aba c

abc = (ab)c

ab /ac = a(b-c)

b = a loga

b

bc = a c*loga

b

• properties of logarithms:logb(xy) = logbx + logbylogb (x/y) = logbx - logbylogbxa = alogbxlogba = logxa/logxb

Math you need to Review

Page 4: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

Logarithmic/Exponential Problems.

• Evaluate and/or simplify. 2a =8 2a =1024 2a = n2 log832 2log49 log 12 2log 9 2423 == 212 2423 == 27 315 /310

Page 5: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

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)

Page 6: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

Big-Oh Growth Rate• f(N) = O(g(N)) iff there are

positive constants c and n0 such that

f(N) c g(N) when N n0

– 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

Page 7: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

Big Oh: more examples• N2 / 2 – 3N = O(N2) iff c, n∃ 0 > 0 such that N2 / 2 – 3N <= c N2 n > n∀ 0

• Proof• N2 / 2 – 3N <= N2 – 3N (because N2 / 2 is less than N2)• <= N2 (because -3n is negative if n > 0)• = O(N2) when c = 1 and n0 = 1

N N2 N2/2-3N1 1 -2.52 4 -43 9 -4.5

10 100 2050 2500 1100

100 10000 4700200 40000 19400500 250000 123500

1000 1000000 497000

Page 8: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

Big Oh: more examples• N2 + 3N = O(N2) iff c, n∃ 0 > 0 such that N2 + 3N <= c N2 n >= n∀ 0

• Proof• 3n <= N2 n >= 3, so N∀ 2 + 3n < N2 + N2 n >= 3∀• So N2 + 3n < 2N2 • = O(N2) where c = 2 and n0 = 3

N N2 N2 +3N1 1 42 4 103 9 18

10 100 13050 2500 2650

100 10000 10300200 40000 40600500 250000 251500

1000 1000000 1003000

Page 9: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

Big Oh: more examples• 7N2 + 10N + 3 = O(N2) = O(N3)• Proof

• 10N + 3 < cN2 n > 1 and some c > 0∀• i.e, 10N + 3 < N2 n > 10 , c >= 1∀• So, 7N2 + 10N + 3 < 7N2 + N2 n > 10∀• = 8N2 = O(N2) n > 10, c >= 8∀

N N2 10N + 31 1 132 4 233 9 33

10 100 10311 121 11312 144 12315 225 15330 900 30350 2500 503

100 10000 10031000 1000000 10003

Page 10: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

Big Oh: more examples• Prove the following

• 7N2 + 10N + 3 = O(N3)• N3 + 90N2 + 14N = O(N3)

Page 11: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

Big Oh: more examples

• log10 N = O(log2 N) = O(log N)• Proof:

• log10 N = log2 N / log2 10

• = log2 N / 3.32

• < log2 N = O(log2 N) n >= 1, c >= 1∀• = O(log2 N) = O(log N) since log2 N = logk N / logk 2 = O(logk N)

Page 12: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

Big Oh: more examples• log N + N = O(?)

Page 13: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

Big Oh: more examples• N2 / 2 – 3N = O(N2)• 1 + 4N = O(N)• 7N2 + 10N + 3 = O(N2) = O(N3)• log10 N = log2 N / log2 10 = O(log2 N) = O(log N)• sin N = O(1); 10 = O(1), 1010 = O(1)•

• log N + N = O(?)• logk N = O(N) for any constant k• N = O(2N), but 2N is not O(N)• 210N is not O(2N)

)( 321

2 NONNiN

i

)( 21

NONNiN

i

Page 14: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

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

0

5

10

15

20

25

30

35

1 2 3 4 5 6 7

XA

Page 15: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

15

A Prefix Averages Algorithm

Algorithm prefixAverages1(X)Input array X of n integersOutput 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

Page 16: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

16

Another Prefix Averages Algorithm

Algorithm prefixAverages2(X)Input array X of n integersOutput array A of prefix averages of XA new array of n integerss 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

Page 17: Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures

Lecture aid

17

______________________Devon M. Simmonds

Computer Science DepartmentUniversity of North Carolina Wilmington

_____________________________________________________________

Qu es ti ons?

Reading from course text: