Presentation is loading. Please wait.

Presentation is loading. Please wait.

Runtime Analysis CSC 172 SPRING 2002 LECTURE 9 RUNNING TIME A program or algorithm has running time T(n), where n is the measure of the size of the input.

Similar presentations


Presentation on theme: "Runtime Analysis CSC 172 SPRING 2002 LECTURE 9 RUNNING TIME A program or algorithm has running time T(n), where n is the measure of the size of the input."— Presentation transcript:

1

2 Runtime Analysis CSC 172 SPRING 2002 LECTURE 9

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  Worst case MergeSort: T(n) = n log n  Worst case QuickSort: T(n) = n 2 There is an unknowable (machine dependent) constant factor.

4 Why measure runtime? 1.3n 3 10n 2 47nlog 2 n48n Sec92010,0001.0*10 6 2.1*10 7 min360077,0004.9*10 7 1.3*10 9 hr14,0006.0*10 5 2.4*10 9 7.6*10 10 day41,0002.9*10 6 5.0*10 10 1.8*10 12 Max Problem (Bently)

5 Problems with Runtime Measurement Algorithms can vary depending on the input.  Worst case QuickSort: T(n) = n 2  Average case QuickSort: T(n) = n log n Machine speed increases  Faster machines enable bigger problems  Bigger problems bring us closer to the asymptote Constant factor are important  Anything is ok when input is small Benchmarking as an alternative  You have to build it to benchmark it

6 The effect of the constant factors nAlpha 21164A, “C”, “obvious ald” TRS-80, “BASIC”, Linear alg 100.6 microsec200 millisecs 1000.6 millisecs2.0 sec 10000.6 sec20 sec 10,00010 min3.2 min 100,0007 days32 min 1,000,00019 years5.4 hrs

7 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)  Formally  There exists constants n 0 and c > 0 such that for all n>=n 0, we have T(n) <= cf(n)  n 0 and c are called “witnesses” to the fact that T(n) is O(f(n))

8 Example 10n 2 +50n+100 is O(n 2 ) Pick witnesses n 0 ==10 c == 16 Then for any n >= 10, 10n 2 +50n+100 <= 16n 2

9 10n 2 +50n+100 and n 2

10 n 0 ==10

11 10n 2 +50n+100 and n 2 and 16n 2

12 Example, alternate 10n 2 +50n+100 is O(n 2 ) Pick one witness n 0 ==1 10n 2 +50n+100 <= Cn 2 10n 2 +50n+100 <= 10n 2 + 50n + 100n 10n 2 +50n+100 <= 10n 2 + 150n 10n 2 +50n+100 <= 10n 2 + 150n 2 10n 2 +50n+100 <= 160n 2 c == 160

13 Example n 10 is O(2 n ) n 10 <2 n Is the same as saying log 2 (n 10 ) < log 2 (2 n ) 10 log 2 n < n False for n = 32, true n = 64 So, with c==1 and n0 == 64, n 10 < 1 * 2 n

14 10log 2 (n) and n

15 General Rules  Any Polynomial is big-oh of its leading term with coefficient of 1  The base of a logarithm doesn’t matter. log a n is O(log b n) for any bases a and b because log a n= log b n log a b, (i.e. n 0 ==1, c = log a b)  Logs grow slower than powers [log n is O(n 1/10 )]  Exponentials (c n, c>1) grow faster than poly n 10 is O(1.0001 n )  Generally, polynomial time is tolerable  Generally, exponential time is intolerable

16 Disproving Big-Oh (A&U p. 101)  Proof by contradiction n 3 is not O(n 2 ) If it were then n 0 and c would exist n 3 < cn 2 Choose n 1 at least as large as n 0 Choose n 1 at least as large as 2c If n 1 3 <= cn 1 2 then n 1 <= c But n 1 >= 2c

17 Runtime of programs Measure some “size” of the input the value of some integer The length of a string or array

18 Program Analysis  Basis cases (simple statements) constant time  Assignments  Break, continue, return  System.in.println()  Induction  Loops  Branching (if, if-else)  Blocks {….}

19 Structure Tree  Nodes are complex statements  Children are constituent statements

20 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; }

21 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } 123456789123456789

22 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } 123456789123456789 1-9

23 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } 123456789123456789 1-9 2-891

24 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } 123456789123456789 1-9 2-891 3-7 ? times

25 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } 123456789123456789 1-9 2-891 3-7 ? times 3-67

26 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } 123456789123456789 1-9 2-891 3-7 ? times 3-67 O(1) + max 64

27 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1” n=/2; } return rval; } 123456789123456789 1-9 2-891 3-7 ? times 3-67 O(1) + max 64 O(1)

28 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } 123456789123456789 1-9 2-891 3-7 ? times 3-67 O(1) + max 64 O(1)

29 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } 123456789123456789 1-9 2-891 3-7 log 2 n times 3-67 O(1) + max 64 O(1)

30 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } 123456789123456789 1-9 2-891 3-7 log 2 n times 3-67 O(1) + max 64 O(1) O(log 2 n)


Download ppt "Runtime Analysis CSC 172 SPRING 2002 LECTURE 9 RUNNING TIME A program or algorithm has running time T(n), where n is the measure of the size of the input."

Similar presentations


Ads by Google