Lecture 2
Lecture 2
Lecture 2
Lecture #2
Analysis of Algorithms
Alon Halevy
Fall Quarter 2000
Analysis of Algorithms
• Analysis of an algorithm gives insight into
how long the program runs and how much
memory it uses
– time complexity
– space complexity
• Why useful?
• Input size is indicated by a number n
– sometimes have multiple inputs, e.g. m and n
• Running time is a function of n
n, n2, n log n, 18 + 3n(log n2) + 5n3
Simplifying the Analysis
• Eliminate low order terms
4n + 5 4n
0.5 n log n - 2n + 7 0.5 n log n
2n + n3 + 3n 2n
• Eliminate constant coefficients
4n n
0.5 n log n n log n
log n2 = 2 log n log n
log3 n = (log3 2) log n log n
Order Notation
• BIG-O T(n) = O(f(n))
– Upper bound
– Exist constants c and n0 such that
T(n) c f(n) for all n n0
• OMEGA T(n) = (f(n))
– Lower bound
– Exist constants c and n0 such that
T(n) c f(n) for all n n0
• THETA T(n) = θ (f(n))
– Tight bound
– θ(n) = O(n) = (n)
Examples
n2 + 100 n = O(n2) = (n2) = (n2)
( n2 + 100 n ) 2 n2 for n 10
( n2 + 100 n ) 1 n2 for n 0
n log n = O(n2)
n log n = (n log n)
n log n = (n)
More on Order Notation
• Order notation is not symmetric; write
2n2 + 4n = O(n2)
but never
O(n2) = 2n2 + 4n
right hand side is a crudification of the left
Likewise
O(n2) = O(n3)
(n3) = (n2)
A Few Comparisons
Function #1 Function #2
n0.1 log n
n + 100n0.1 2n + 10 log n
5n5 n!
n-152n/100 1000n15
82log n 3n7 + 7n
Race I
n3 + 2n2 vs. 100n2 + 1000
Race II
n0.1 vs. log n
Race III
n + 100n0.1 vs. 2n + 10 log n
Race IV
5n5 vs. n!
Race V
n-152n/100 vs. 1000n15
Race VI
82log(n) vs. 3n7 + 7n
The Losers Win
Function #1 Function #2 Better algorithm!
5n5 n! O(n5)
n n
1 n n
n 2
j 1
i 1 i 1
Nested Dependent Loops
for i = 1 to n do
for j = i to n do
sum = sum + 1
n n n n n
i 1 j 1
1 (n i 1) (n 1) i
i i 1 i 1
n(n 1) n(n 1)
n(n 1) n2
2 2
Conditionals
• Conditional
if C then S1 else S2