Analysis of Algorithms Review: Adapted From Notes of S. Sarkar of Upenn, Skiena of Stony Brook, Etc
Analysis of Algorithms Review: Adapted From Notes of S. Sarkar of Upenn, Skiena of Stony Brook, Etc
Analysis of Algorithms Review: Adapted From Notes of S. Sarkar of Upenn, Skiena of Stony Brook, Etc
Fall 2005
Analysis of Algorithms
Review
Adapted from Notes of S. Sarkar of
UPenn, Skiena of Stony Brook, etc.
Outline
Why
10
20
0.00001 sec 0.00002 sec
0.0001 sec
0.0004 sec
0.001 sec
0.008 sec
0.1 sec
3.2 sec
0.001 sec
1.0 sec
0.59 sec
58 min
30
0.00003 sec
0.0009 sec
0.027 sec
24.3 sec
17.9 min
6.5 years
40
50
60
0.00004 sec 0.00005 sec 0.00006 sec
0.016 sec
0.025 sec
0.036 sec
0.064 sec
0.125 sec
0.216 sec
1.7 min
5.2 min
13.0 min
12.7 days
35.7 years
366 cent
3855 cent 2 x 108 cent 1.3 x 1013 cent
Notations
Asymptotically less than or equal to
(Big-Oh)
(Big-Omega)
Asymptotically equal to
(Big-Theta)
(Little-Oh)
powers:
n k is O(b n ), for all b > 1, k > 0,
e.g. n 4 is O(2 n ) and n 4 is O(exp(n)).
Logarithms
The
1 + 2 + 3 + . N = N(N+1)/2
(arithmetic series)
Logarithms
A
If
Properties of Logarithms
log c B
log c A
Binary Search
You have a sorted list of numbers
You need to search the list for the number
If the number exists find its position.
Complexity Analysis
T(n) = T(n/2) + c
O(?) complexity
Intractable Algorithms
A problem is said to be intractable if no
algorithm with polynomial time complexity is
known for it
Compare Complexity
Method
1:
A function f(n) is O(g(n)) if there exists a
number n0 and a nonnegative c such that for
all n n0 , f(n) cg(n).
Method 2:
If lim n f(n)/g(n) exists and is finite, then
f(n) is O(g(n))!
is O(np)
if
log n
is O (nr) if r 0,
Is kn O(n2) ?
since limnlog(n)/ nr = 0,
kn is O(n)
n is O(n2)
f(n) + g(n) is O(h(n)) if f(n), g(n) are O(h(n))
f(n) ch(n) , for some c > 0, n m
g(n) dh(n) , for some d > 0, n p
f(n) + g(n) ch(n) + dh(n) , n max(m,p)
(c+d)h(n) , c + d > 0, n max(m,p)
Algorithm 1:
Maxsum = 0;
For (i=0; i < N; i++)
Analysis
Inner loop:
j=iN-1 (j-i + 1) = (N i + 1)(N-i)/2
Outer Loop:
i=0N-1 (N i + 1)(N-i)/2 = (N3 + 3N2 + 2N)/6
Overall: O(N^3 )
Algorithm 2
Maxsum = 0;
For (i=0; i < N; i++)
Complexity?
i=0N-1 (N-i) = N2 N(N+1)/2 = (N2 N)/2
O(N2 )
5 2
-1
2 6 -2
Find maxrightbordersum..
return(max(maxleftsum, maxrightsum,
maxrightbordersum + maxleftbordersum);
Complexity Analysis
T(1)=1
T(n) = 2T(n/2) + cn
= 2(2T(n/4)+cn/2)+cn=2^2T(n/2^2)+2cn
=2^2(2T(n/2^3)+cn/2^2)+2cn = 2^3T(n/2^3)+3cn
= = (2^k)T(n/2^k) + k*cn
(let n=2^k, then k=log n)
T(n)= n*T(1)+k*cn = n*1+c*n*log n = O(n log n)
Algorithm 4
Maxsum = 0; Thissum = 0;
For (j=0; j<N; j++)
{
Maxsum = Thissum;
}
O(?)