Presentation is loading. Please wait.

Presentation is loading. Please wait.

How much does your program take ?

Similar presentations


Presentation on theme: "How much does your program take ?"— Presentation transcript:

1 How much does your program take ?
Analysis How much does your program take ?

2 Data Structure: Analysis
Outline Asymptotic notation Big O, small o ,  How to calculate running time Sequential statements If statements Loops Recursions Examples 4/28/2019 Data Structure: Analysis

3 Data Structure: Analysis
Asymptotic Notations T(N) = O(f(N)) if  positive constants c and n0 such that T(N)  c f(N) when N  n0. Upper bound f(N) T(N) c=1 Running time n0 Input size (N ) 4/28/2019 Data Structure: Analysis

4 Data Structure: Analysis
Asymptotic Notations T(N) = (f(N)) if  positive constants c and n0 such that T(N)  c f(N) when N  n0. T(N) f(N) lower bound c=1 Running time n0 Input size (N ) 4/28/2019 Data Structure: Analysis

5 Data Structure: Analysis
Asymptotic Notations T(N) = (f(N)) if T(N) = O(f(N)) and T(N) = (f(N)). cuT(N) f(N) upper bound clT(N) lower bound Running time nu0 nl0 Input size (N ) 4/28/2019 Data Structure: Analysis

6 Data Structure: Analysis
Asymptotic Notations T(N) = o(f(N)) if T(N) = O(f(N)) and T(N)  (f(N)). clT(N) cuT(N) f(N) lower bound upper bound Running time nu0 Input size (N ) 4/28/2019 Data Structure: Analysis

7 Data Structure: Analysis
What is faster? 4/28/2019 Data Structure: Analysis

8 Data Structure: Analysis
What is faster? 4/28/2019 Data Structure: Analysis

9 Running-time Calculation
Sequential statements S1 S2 Running time: O(T(N)) = O(T1(N)+T2(N)) = O(max(T1(N), T2(N))) O(T1(N)) O(T2(N)) 4/28/2019 Data Structure: Analysis

10 Running-time Calculation
Conditional statements if (E) S1 else S2 Running time: O(T(N)) = O(T3(N)) + O(max(T1(N),T2(N))) = O(max(T1(N), T2(N), T3(N))) O(T1(N)) O(T2(N)) O(T3(N)) 4/28/2019 Data Structure: Analysis

11 Running-time Calculation
Iteration statements for (i=1; i<=n;i++) S Running time: ni=1 T(i) If T(i) = O(n), ni=1 T(i) = O(n2). If T(i) = O(1), ni=1 T(i) = O(n). T(i) 4/28/2019 Data Structure: Analysis

12 Data Structure: Analysis
Examples 1 unit 2 units for (i=1; i<=n;i++) sum = sum+i; Running time: T(n) = 1 + ni=13 = 1+3n = O(n) for (j=i; j<=n; j++) sum = sum+a[i,j]; Running time: T(n) = 1 + ni=1 ((1+nj=i3) +2) = 1 + ni=1 (3+3(n-i+1)) = 1 + ni=1(3n-3i+6) = 1+3n2+6n-3 ni=1i =2.5n2+5.5n+1= O(n2) 2 units 1 unit 4/28/2019 Data Structure: Analysis

13 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) 4/28/2019 Data Structure: Analysis

14 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)). f(N) 4/28/2019 Data Structure: Analysis

15 Running-time Calculation
Recursive call function rec(N) if (…) return (…) else { rec(N/2); …; return (…); } 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 ) f(N) 4/28/2019 Data Structure: Analysis

16 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). 4/28/2019 Data Structure: Analysis

17 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) 4/28/2019 Data Structure: Analysis

18 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) 4/28/2019 Data Structure: Analysis


Download ppt "How much does your program take ?"

Similar presentations


Ads by Google