Time Complexity
Time Complexity
Time Complexity
UC Berkeley
Fall 2004, E77
http://jagger.me.berkeley.edu/~pack/e77
Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to
Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
Time complexity of algorithms
Dependency of
– the time it takes to solve a problem
– as a function of the problem dimension/size
Examples:
– Sorting a list of length n
– Searching a list of length n
– Multiplying a n×n matrix by an n×1 vector
Time to solve problem might depend on data
– Average-case time
– Best-case time
• data is well suited for algorithm (can’t be counted on)
– Worst-case time
• data is such that algorithm performs poorly (time-wise)
Worst-Case gives an upper bound as to how much time
will be needed to solve any instance of the problem.
Example: N-by-N matrix, N-by-1 vector, multiply
(3 accesses, 1 add, 1 multiply)
N times
N times
Y(i) = Y(i) + A(i,j)*x(j); c4
end End of loop, return/exit, c5
end End of loop, return/exit, c5
Total = c1N+c2N+N(c3+c2N+N(c4+c5)+c5)
= (c2+c4+c5)N2 + (c1+c2+c3+c5)N
= c6N2 + c7N
Asymptotic Time complexity of algorithms
Dependency of
– the time it takes to solve a problem
– as a function of the problem dimension/size
but
– formula may only be valid for “large” problems
Write f ( n ) = O( g ( n ) )
or
31 = O(1)
Big O: Examples
Example: f ( n ) = 4n 2 + 7 n + 12
g ( n) = n 2
f ( n) 5 g ( n)
1200
Write f ( n ) = O( g ( n ) )
600
400
or f = O( g )
( )
200
or 4 n 2
+ 7 n + 12 = O n 2
0
0 5 10 15
Big O: Relationships
Suppose f1 and f2 satisfy: There is a value n0
f1 ( n ) ≤ f 2 ( n ) for all n > n0
Hence f1 + f 2 = O( f 2 )
Example 3: 27 log( n ) + 19 + n = O( n )
f1 ( n ) f 2 ( n)
Generalization: f1 = O( f 2 ) ⇒ f1 + f 2 = O( f 2 )
Big O: Relationships
Suppose positive functions f1, f2, g1, g2, satisfy
f1 = O( g1 ) and f 2 = O( g 2 )
Then f1 ⋅ f 2 = O( g1 ⋅ g 2 )
Example:
(12n 2
)
+ 2 log n ( 27 log( n ) + 19 + n ) = O n 3 ( )
f1 ( n ) f 2 ( n)
Asymptotic: Relationships
Obviously, for any positive function g
g = O( g )
Example 4: 13n = O n
3
( )
3
log n
Example 5: = O( log n )
512
Example: N-by-N matrix, N-by-1 vector, multiply
Y = zeros(N,1); initialize space, c1N
for i=1:N initialize “for” loop, c2N
Y(i) = 0.0; Scalar assignment, c3
for j=1:N initialize “for” loop, c2N
N times
N times
Y(i) = Y(i) + A(i,j)*x(j); c4
end End of loop, return/exit, c5
end End of loop, return/exit, c5