Professional Documents
Culture Documents
Analysis: How Much Does Your Program Take ?
Analysis: How Much Does Your Program Take ?
Outline
Asymptotic notation
Big O, small o ,
Examples
2/26/2013 Data Structure: Analysis 2
Asymptotic Notations
T(N) = O(f(N)) if positive constants c and n0 such that T(N) c f(N) when N n0.
f(N)
Running time
c=1
2/26/2013
n0
Input size (N
Data Structure: Analysis
)
3
Asymptotic Notations
T(N) = (f(N)) if positive constants c and n0 such that T(N) c f(N) when N n0.
T(N)
Running time
f(N)
c=1
lower bound
2/26/2013
n0
Input size (N
Data Structure: Analysis
)
4
Asymptotic Notations
T(N) = (f(N)) if T(N) = O(f(N)) and T(N) = (f(N)).
f(N)
clT(N) lower bound
2/26/2013
nu0
nl0
Input size (N
Data Structure: Analysis
)
5
Asymptotic Notations
T(N) = o(f(N)) if T(N) = O(f(N)) and T(N) (f(N)).
cuT(N)
upper bound
Running time
f(N)
2/26/2013
nu0
Input size (N
Data Structure: Analysis
)
6
What is faster?
1000 800
600
400
N log N N2 N3
200
0
2/26/2013
10
20
30
40
50
7
What is faster?
100 90 80 70 60 50 40 30 20 10 0
2/26/2013 Data Structure: Analysis 8
N N log N log N
Running-time Calculation
Sequential statements O(T1(N)) S1 O(T2(N)) S2 Running time: O(T(N)) = O(T1(N)+T2(N)) = O(max(T1(N), T2(N)))
2/26/2013
Running-time Calculation
Conditional statements O(T3(N)) if (E) S1 else S2 O(T1(N))
O(T2(N))
2/26/2013
10
Running-time Calculation
Iteration statements for (i=1; i<=n;i++) T(i) S Running time: i=1 T(i) n If T(i) = O(n), i=1 T(i) = O(n2). n If T(i) = O(1), i=1 T(i) = O(n).
n
2/26/2013
11
Examples
for (i=1; i<=n;i++) sum = sum+i; 1 unit 1 unit n Running time: T(n) = 1 + i=13 = 1+3n = O(n) for (i=1; i<=n;i++) 2 units for (j=i; j<=n; j++) 1 unit sum = sum+a[i,j]; 1 unit 1 unit n n Running time: T(n) = 1 + i=1 ((1+ j=i3) +2) n n = 1 + i=1 (3+3(n-i+1)) = 1 + i=1(3n-3i+6) 2+6n-3 n i =2.5n2+5.5n+1= O(n2) = 1+3n i=1
2/26/2013 Data Structure: Analysis 12
2 units
2 units
Running-time Calculation
Recursive call function rec(N) if () return () else { rec(N'); rec(N''); return (); } Running time: T(N) = T(N) +T(N) + f(N) T(1) = g(N)
2/26/2013
13
Running-time Calculation
Recursive call function rec(N) if () return () else { rec(N-1); ; return (); } Running time: T(N) = T(N-1) + f(N) T(1) = k T(N) = k + f(2) + f(3) ++ f(N-1) + f(N) T(N) = O(N f(N)).
2/26/2013 Data Structure: Analysis 14
f(N)
Running-time Calculation
Recursive call function rec(N) if () return () else { rec(N/2); ; return (); } f(N)
Running time: T(N) = T(N/2) + f(N) T(1) = k T(N) = T(N/2) + f(N) = T(N/4)+ f(N/2)+ f(N) = k+f(2)+f(4)+f(8)++ f(N/4)+ f(N/2)+ f(N) T(N) = O( f(N) log2N )
2/26/2013 Data Structure: Analysis 15
Running-time Calculation
Let T(N) = aT(N/b) + cnk. If a<bk, T (n) = O(nk) . If a=bk, T(n) = O(nk lg n) . If a>bk, T(n) = O(nlogbl).
2/26/2013
16