Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Analysis

How much does your program take ?

Outline
Asymptotic notation
Big O, small o ,

How to calculate running time


Sequential statements If statements Loops Recursions

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

Upper bound T(N)

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)).

cuT(N) upper bound


Running time

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

clT(N) lower bound

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

Data Structure: Analysis

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

Data Structure: Analysis

Running-time Calculation
Conditional statements O(T3(N)) if (E) S1 else S2 O(T1(N))

O(T2(N))

Running time: O(T(N)) = O(T3(N)) + O(max(T1(N),T2(N))) = O(max(T1(N), T2(N), T3(N)))

2/26/2013

Data Structure: Analysis

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

Data Structure: Analysis

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

Data Structure: Analysis

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

Data Structure: Analysis

16

Example: Binary Search


public static int Bserach(int low, high, x) { int mid = (low+high)/2; if (a[mid]<x) return(Bsearch(mid+1, high, x)); else if (a[mid]>x) return(Bsearch(low, mid-1, x)); else return(mid); }

T(N) = T(N/2) + k T(N) = O(log N)


2/26/2013 Data Structure: Analysis 17

Example: Max Subsequence Sum


public static int maxSum(int left, int right) { if (left==right) if (a[left]>0) return(a[left]); else return(0); int center=(left+right)/2; int leftBorder =leftBorderSum(left, center); int rightBorder=rightBorderSum(center+1, right); return(max3(maxSum(left, center), maxSum(center+1, right), leftBorder+rightBorder); }

T(N) = 2T(N/2) + O(N) T(N) = O(N log N)


2/26/2013 Data Structure: Analysis 18

You might also like