+ All Categories
Home > Documents > CSE 326 Asymptotic Analysis

CSE 326 Asymptotic Analysis

Date post: 17-Jan-2016
Category:
Upload: yestin
View: 36 times
Download: 1 times
Share this document with a friend
Description:
CSE 326 Asymptotic Analysis. David Kaplan Dept of Computer Science & Engineering Autumn 2001. Housekeeping. Homework 1 status Join cse326@cs Poll: Slide format(s)? Office hours today?. Analyzing Algorithms. Analyze algorithms to gauge: Time complexity (running time) - PowerPoint PPT Presentation
26
CSE 326 Asymptotic Analysis David Kaplan Dept of Computer Science & Engineering Autumn 2001
Transcript
Page 1: CSE 326 Asymptotic Analysis

CSE 326Asymptotic Analysis

David Kaplan

Dept of Computer Science & EngineeringAutumn 2001

Page 2: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 20012

Housekeeping Homework 1 status Join cse326@cs Poll:

Slide format(s)? Office hours today?

Page 3: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 20013

Analyzing AlgorithmsAnalyze algorithms to gauge:

Time complexity (running time) Space complexity (memory use)

Input size is indicated by a number n sometimes have multiple inputs, e.g. m

and n

Running time is a function of nn, n2, n log n, 18 + 3n(log n2) + 5n3

Page 4: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 20014

RAM ModelRAM (random access machine)

Ideal single-processor machine (serialized operations)

“Standard” instruction set (load, add, store, etc.)

All operations take 1 time unit (including, for our purposes, each C++ statement)

Page 5: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 20015

Order Notation (aka Big-O)

Big-O

T(n) = O( f(n) )Exist positive constants c, n0 such that

T(n) cf(n) for all n n0

Upper bound

Omega

T(n) = ( f(n) )Exist positive constants c, n0 such that

T(n) cf(n) for all n n0

Lower bound

ThetaT(n) = θ( f(n) )T(n) = O(f(n)) AND T(n) = (f(n))

Tight bound

little-oT(n) = o( f(n) )T(n) = O(f(n)) AND T(n) != θ(f(n))

Strict upper bound

Page 6: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 20016

Simplifying with Big-OBy definition, Big-O allows us to:

Eliminate low order terms 4n + 5 4n 0.5 n log n - 2n + 7 0.5 n log n

Eliminate constant coefficients 4n n 0.5 n log n n log n log n2 = 2 log n log n log3 n = (log3 2) log n log n

But when might constants or low-order terms matter?

Page 7: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 20017

Big-O Examplesn2 + 100 n = O(n2)

follows from … ( n2 + 100 n ) 2 n2 for n 10

n2 + 100 n = (n2)follows from …( n2 + 100 n ) 1 n2 for n 0

n2 + 100 n = (n2)by definition

n log n = O(n2)n log n = (n log n)n log n = (n)

Page 8: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 20018

Big-O UsageOrder notation is not symmetric:

we can say 2n2 + 4n = O(n2) … but never O(n2) = 2n2 + 4nRight-hand side is a crudification of the left

Order expressions on left can produce unusual-looking, but true, statements:

O(n2) = O(n3)(n3) = (n2)

Page 9: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 20019

Big-O Comparisons

Function A

n3 + 2n2

n0.1

n + 100n0.1

5n5

n-152n/100

82log n

Function #2

100n2 + 1000

log n

2n + 10 log n

n!

1000n15

3n7 + 7n

vs.

Page 10: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200110

Race 1100n2 + 1000vs.n3 + 2n2

Page 11: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200111

Race 2n0.1 log nvs.

In this one, crossover point is very late! So, which algorithm is really better???

Page 12: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200112

Race Cn + 100n0.1 2n + 10 log nvs.

Is the “better” algorithm asymptotically better???

Page 13: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200113

Race 45n5 n!vs.

Page 14: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200114

Race 5n-152n/100 1000n15vs.

Page 15: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200115

Race VI82log(n) 3n7 + 7nvs.

Page 16: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200116

Big-O Winners (i.e. losers)

Function A

n3 + 2n2

n0.1

n + 100n0.1

5n5

n-152n/100

82log n

Function #2

100n2 + 1000

log n

2n + 10 log n

n!

1000n15

3n7 + 7n

vs.

Winner

O(n2)

O(log n)

O(n) TIE

O(n5)

O(n15)

O(n6) why???

Page 17: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200117

Big-O Common Namesconstant: O(1)logarithmic: O(log n) linear: O(n)log-linear: O(n log n)superlinear: O(n1+c) (c is a constant

> 0)quadratic: O(n2)polynomial: O(nk) (k is a constant)exponential: O(cn) (c is a constant

> 1)

Page 18: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200118

Kinds of AnalysisRunning time may depend on actual input,

not just length of input

Distinguish Worst case

Your worst enemy is choosing input Average case

Assume probability distribution of inputs Amortized

Average time over many runs Best case (not too useful)

Page 19: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200119

Analyzing CodeC++ operations

Consecutive stmts

ConditionalsLoops

Function callsRecursive functions

constant timesum of timeslarger branch plus

testsum of iterationscost of function bodysolve recursive

equation

Page 20: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200120

Nested Loops

2

11

1

1 nnn

i

n

j

n

i

for i = 1 to n do

for j = 1 to n do

sum = sum + 1

Page 21: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200121

Dependent Nested Loops

for i = 1 to n do

for j = i to n do

sum = sum + 1

ininn

i

n

i

n

i

n

ij

n

i 1111

)1()1(1

2

2

)1(

2

)1()1( n

nnnnnn

Page 22: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200122

Recursion A recursive procedure can often be

analyzed by solving a recursive equation

Basic form:T(n) =

base case: some constantrecursive case: T(subproblems) + T(combine)

Result depends upon how many subproblems how much smaller are subproblems how costly to combine solutions

(coefficients)

Page 23: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200123

Sum of QueueSumQueue(Q) if (Q.length == 0 ) return 0 else return Q.dequeue() + SumQueue(Q)

One subproblem

Linear reduction in size (decrease by 1)

Combining: constant (cost of 1 add)

T(0) b

T(n) c + T(n – 1) for n>0

Page 24: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200124

Sum of Queue Solution

T(n) c + c + T(n-2) c + c + c + T(n-3) kc + T(n-k) for all k nc + T(0) for k=n cn + b = O(n)

T(0) b

T(n) c + T(n – 1) for n>0

Equation:

Solution:

Page 25: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200125

Binary SearchBinarySearch(A, x) Search A, a sorted array, for item x

One subproblem, half as large

7 12 30 35 75 83 87 90 97 99

T(1) b

T(n) T(n/2) + c for n>1

Equation:

Page 26: CSE 326 Asymptotic Analysis

Asymptotic Analysis

CSE 326 Autumn 200126

Binary Search: Solution

T(n) T(n/2) + c T(n/4) + c + c T(n/8) + c + c + c T(n/2k) + kc T(1) + c log n where k = log n b + c log n = O(log n)

T(1) b

T(n) T(n/2) + c for n>1

Equation:

Solution:


Recommended