UNIT 1 - DATA STRUCTURES (1)
UNIT 1 - DATA STRUCTURES (1)
Total 2rows*cols+2rows+1
Remarks of Time Complexity
Difficulty: the time complexity is not dependent solely on
the number of inputs or outputs
To determine the step count
Best case, Worst case, and Average
Example – binary search program
Asymptotic Notation(O, , )
• motivation
– Target: Compare the time complexity of two programs that
computing the same function and predict the growth in run time
as instance characteristics change
• Asymptotic notation
– Big "oh“ O
• upper bound(current trend)
– Omega
• lower bound
– Theta
• upper and lower bound
Asymptotic Notation O
• Definition of Big "oh"
– f(n)= O(g((n)) iff there exist positive constants c
and n0 such that f(n)<= cg(n) for all n, n>= n0
– g(n) is the least upper bound
Asymptotic Notation
• Definition
– f(n)= (g(n)) iff there exist positive constants c
and n0 such that f(n)>= cg(n) for all n, n>= n0
– The largest lower bound
Asymptotic Notation
• Definition
– f(n)= (g(n)) iff there exist positive constants c1, c2,
and n0 such that c1g(n)<= f(n) <= c2g(n) for all n,
n>= n0
Example of Time Complexity Analysis
Statement Asymptotic complexity
Total (rows*cols)
Example of Time Complexity
Analysis(Cont.)
The more global approach to count steps:
focus the variation of instance characterics.
int binsearch(int list[], int .....)
{ int middle;
while (left<= right){
middle= (left+ right)/2;
switch(compare(list[middle],
searchnum)){
case -1: left= middle+ 1;
break; worst case (log n)
case 0: return middle;
case 1: right= middle- 1;
}
}
return -1;
}
Table 1.7 Function values
Instance characteristic n
Time Name 1 2 4 8 16 32
1 Constant 1 1 1 1 1 1
log n Logarithmic 0 1 2 3 4 5
n Linear 1 2 4 8 16 32
nlog n Log Linear 0 2 8 24 64 160
n2 Quadratic 1 4 16 64 256 1024
n3 Cubic 1 8 61 512 4096 32768
2n Exponential 2 4 16 256 65536 4294967296
n! Factorial 1 2 54 40326 20922789888000 26313*1033
Reference