43
Runtime Analysis CSC 172 SPRING 2004 LECTURE 3

Runtime Analysis

Embed Size (px)

DESCRIPTION

Runtime Analysis. CSC 172 SPRING 2004 LECTURE 3. RUNNING TIME. A program or algorithm has running time T( n ), where n is the measure of the size of the input. For sorting algorithms, we typically choose n to be the number of elements sorted Best case Average case Worst case - PowerPoint PPT Presentation

Citation preview

Page 1: Runtime Analysis

Runtime Analysis

CSC 172

SPRING 2004

LECTURE 3

Page 2: Runtime Analysis

RUNNING TIME

A program or algorithm has running time T(n), where n is the measure of the size of the input. For sorting algorithms, we typically choose n to

be the number of elements sortedBest caseAverage caseWorst case

There is an unknowable (machine dependent) constant factor.

Page 3: Runtime Analysis

Problems with Runtime MeasurementAlgorithms can vary depending on the input.

Worst case QuickSort: T(n) = n2 Average case QuickSort: T(n) = n log n

Machine speed increasesFaster machines enable bigger problemsBigger problems bring us closer to the asymptote

Constant factor are importantAnything is ok when input is small

Benchmarking as an alternativeYou have to build it to benchmark it

Page 4: Runtime Analysis

The effect of the constant factors

n Alpha 21164A, “C”, “obvious ald”

TRS-80, “BASIC”,

Linear alg

10 0.6 microsec 200 millisecs

100 0.6 millisecs 2.0 sec

1000 0.6 sec 20 sec

10,000 10 min 3.2 min

100,000 7 days 32 min

1,000,000 19 years 5.4 hrs

Page 5: Runtime Analysis

VERY QUICK MATH REVEIW

Page 6: Runtime Analysis
Page 7: Runtime Analysis

The Constant Function

f(n) = c

Page 8: Runtime Analysis

The Linear Function

f(n) = n

Page 9: Runtime Analysis

The Quadratic Function

f(n) = n2

Page 10: Runtime Analysis

The Cubic Function

f(n) = n3

Page 11: Runtime Analysis

The “Polynomial” Function

f(n) = a0+a1n+a2n2+…+adnd

Page 12: Runtime Analysis

The Exponential Function

f(n) = bn

(ba)c = bac

babc=ba+c

ba/bc = ba-c

Page 13: Runtime Analysis

The Logarithm Function

f(n) = logbn

Page 14: Runtime Analysis

x = logbn

If and only if

bx = n

Page 15: Runtime Analysis

Logartihm Rules

1. logbac = logba + logbc

2. logb(a/c) = logba – logbc

3. logbac = clogba

4. logba = (logda)/(logdb)

5. blogda = alogdb

Page 16: Runtime Analysis

The N-Log-N Function

f(n) = n log n

Page 17: Runtime Analysis
Page 18: Runtime Analysis

Big-Oh Big-Oh is a notation that abstracts away the

unknowable constant factors and focuses on growth rate.

We say “T(n) is O(f(n))” if for large n, T(n) is no more than proportional to f(n)

FormallyThere exists constants n0 and c > 0 such that for all

n>=n0, we have T(n) <= cf(n)n0 and c are called “witnesses” to the fact that T(n) is

O(f(n))

Page 19: Runtime Analysis

Some Others

Page 20: Runtime Analysis

Example Big-Oh

10n2+50n+100 is O(n2)

Pick witnesses

n0==10

c == 16

Then for any n >= 10, 10n2+50n+100 <= 16n2

Page 21: Runtime Analysis

0

2000

4000

6000

8000

10000

12000

14000

16000

18000

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33

10n2+50n+100 and n2

Page 22: Runtime Analysis

0

2000

4000

6000

8000

10000

12000

14000

16000

18000

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33

10n2+50n+100 and n2

n0==10

Page 23: Runtime Analysis

0

2000

4000

6000

8000

10000

12000

14000

16000

18000

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33

n0==10

10n2+50n+100 and n2 and 16n2

Page 24: Runtime Analysis

Example, alternate10n2+50n+100 is O(n2)Pick one witness

n0==110n2+50n+100 <= Cn2

10n2+50n+100 <= 10n2 + 50n + 100n10n2+50n+100 <= 10n2 + 150n10n2+50n+100 <= 10n2 + 150n2

10n2+50n+100 <= 160n2

c == 160

Page 25: Runtime Analysis

Example

n10 is O(2n)

n10<2n

Is the same as saying

log2(n10) < log2(2n)

10 log2n < n

False for n = 32, true n = 64

So, with c==1 and n0 == 64, n10 < 1 * 2n

Page 26: Runtime Analysis

0

10

20

30

40

50

60

70

80

1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70

10log2(n) and n

Page 27: Runtime Analysis

General Rules Any Polynomial is big-oh of its leading term with

coefficient of 1 The base of a logarithm doesn’t matter.

logan is O(logbn) for any bases a and b because

logan= logbn logab , (i.e. n0==1, c = logab) Logs grow slower than powers [log n is O(n1/10)] Exponentials (cn, c>1) grow faster than poly

n10 is O(1.0001n) Generally, polynomial time is tolerable Generally, exponential time is intolerable

Page 28: Runtime Analysis

Disproving Big-Oh

Proof by contradiction

n3 is not O(n2)

If it were then n0 and c would exist n3 < cn2

Choose n1 at least as large as n0

Choose n1 at least as large as 2c

If n13 <= cn1

2 then n1 <= c

But n1 >= 2c

Page 29: Runtime Analysis

Runtime of programs

Measure some “size” of the input

the value of some integer

The length of a string or array

Page 30: Runtime Analysis

Program Analysis Basis cases (simple statements) constant time

AssignmentsBreak, continue, returnSystem.in.println()

InductionLoopsBranching (if, if-else)Blocks {….}

Page 31: Runtime Analysis

Structure Tree

Nodes are complex statements Children are constituent statements

Page 32: Runtime Analysis

Example

Write a method that converts positive integers to binary strings

You have 5 min. Hang it in at the end of class

public String int2bin(int n) {}

Page 33: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”;

n=/2;}return rval;

}

Page 34: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”;

n=/2;}return rval;

}

123456789

Page 35: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”;

n=/2;}return rval;

}

123456789

1-9

Page 36: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”;

n=/2;}return rval;

}

123456789

1-9

2-8 91

Page 37: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”;

n=/2;}return rval;

}

123456789

1-9

2-8 91

3-7

? times

Page 38: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”;

n=/2;}return rval;

}

123456789

1-9

2-8 91

3-7

? times

3-6 7

Page 39: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”;

n=/2;}return rval;

}

123456789

1-9

2-8 91

3-7

? times

3-6 7

O(1) + max

64

Page 40: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”

n=/2;}return rval;

}

123456789

1-9

2-8 91

3-7

? times

3-6 7

O(1) + max

64

O(1)O(1)

Page 41: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”;

n=/2;}return rval;

}

123456789

1-9

2-8 91

3-7

? times

3-6 7

O(1) + max

64

O(1)O(1)

O(1) O(1)

O(1)

Page 42: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”;

n=/2;}return rval;

}

123456789

1-9

2-8 91

3-7

log2n times

3-6 7

O(1) + max

64

O(1)O(1)

O(1) O(1)

O(1)

Page 43: Runtime Analysis

public String int2bin(int n) {String rval;while (n>0) {

if((n%2) == 1)rval += “0”;

elserval +=“1”;

n=/2;}return rval;

}

123456789

1-9

2-8 91

3-7

log2n times

3-6 7

O(1) + max

64

O(1)O(1)

O(1) O(1)

O(1)

O(1) O(1)O(log2n)

O(log2n)