runtime analysis
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 PresentationTRANSCRIPT
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 sortedBest caseAverage caseWorst case
There is an unknowable (machine dependent) constant factor.
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
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
VERY QUICK MATH REVEIW
The Constant Function
f(n) = c
The Linear Function
f(n) = n
The Quadratic Function
f(n) = n2
The Cubic Function
f(n) = n3
The “Polynomial” Function
f(n) = a0+a1n+a2n2+…+adnd
The Exponential Function
f(n) = bn
(ba)c = bac
babc=ba+c
ba/bc = ba-c
The Logarithm Function
f(n) = logbn
x = logbn
If and only if
bx = n
Logartihm Rules
1. logbac = logba + logbc
2. logb(a/c) = logba – logbc
3. logbac = clogba
4. logba = (logda)/(logdb)
5. blogda = alogdb
The N-Log-N Function
f(n) = n log n
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))
Some Others
Example Big-Oh
10n2+50n+100 is O(n2)
Pick witnesses
n0==10
c == 16
Then for any n >= 10, 10n2+50n+100 <= 16n2
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
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
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
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
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
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
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
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
Runtime of programs
Measure some “size” of the input
the value of some integer
The length of a string or array
Program Analysis Basis cases (simple statements) constant time
AssignmentsBreak, continue, returnSystem.in.println()
InductionLoopsBranching (if, if-else)Blocks {….}
Structure Tree
Nodes are complex statements Children are constituent statements
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) {}
public String int2bin(int n) {String rval;while (n>0) {
if((n%2) == 1)rval += “0”;
elserval +=“1”;
n=/2;}return rval;
}
public String int2bin(int n) {String rval;while (n>0) {
if((n%2) == 1)rval += “0”;
elserval +=“1”;
n=/2;}return rval;
}
123456789
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
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
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
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
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
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)
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)
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)
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)