Download presentation

Presentation is loading. Please wait.

Published byAshlee Tweed Modified over 2 years ago

1
LEVEL II, TERM II CSE – 243 MD. MONJUR-UL-HASAN LECTURER DEPT OF CSE, CUET Algorithm Complexity

2
A motivation for complexity Talking about running time and memory space of algorithms is difficult. Different computers will run them at different speeds and memory Different problem sizes run for a different amount of time and memory(even on same computer). Different inputs of the same size run for a different amount of time and memory. 2 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET

3
Definition: Complexity Efficiency or complexity of an algorithm is stated as a function relating the input length to the number of steps (time complexity) or storage locations (space complexity). 3 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET log nn n log n n2n2 n3n3 2n2n

4
Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET 4 n log n exp (n) n f(n) More Curve Fig 1.3 Fundamental Computer Algorithm By, E. Horowitz S. Sahni S. Rajasekaran

5
Using Upper and Lower Bounds to talk about running time. Problem Size Running time (and memory) on some computer Different inputs Upper Bound Lower Bound 5 Md. Monjur-ul-hasan. Lecturer, Dept of CSE CUET

6
Bounds on running time We want to bound the running time: “ for arrays of size n it takes my computer 45·n 2 milliseconds at most to sort the array with bubblesort ” But what about other computers? (faster/slower) Instead we say: “ On any computer, there is a constant c such that it takes at most c·n 2 time units to sort an array of size n with bubblesort ” How is this sentence useful? 6 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET

7
Big O notation. We say that running time of an alg. is O(f(n)) If there exists c such that for large enough n: RunningTime(n) < c·f(n) Translation: From some point c·f(n) is an upper bound on the running time. Why only for “large enough n”? 7 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET

8
Big-O notation Do not assume that because we analyze running time asymptotically we no longer care about the constants. If you have two algorithms with different asymptotic limits – easy to choose. (may be misleading if constants are really large). Usually we are happy when something runs in polynomial time and not exponential time. O(n 2 ) is much much better than O(2 n ). 8 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET

9
Use of big-O notation Example: The running time of bubble sort on arrays of size n is O(n 2 ). Translation: for a sufficiently large array, the running time is less than c·n 2 for some c (No matter how bad the input is). 9 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET

10
Big Omega For lower bounds we use Ω(f(n)). I.e., if we say the running time of an algorithm is Ω(f(n)), we mean that RunningTime(n)>c·f(n) for some c. 10 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET

11
An Example: Checking a the number n is prime. We can try and divide the number n by all integers in the range 2, …,n 1/2. Best case: sometimes we find it isn ’ t prime really quickly (e.g. it divides by 2). Worst case: sometimes the number is prime and we try n 1/2 different divisions. Running time is O(n 1/2 ), and is also Ω(1). 11 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET

12
Bounds might not be tight We saw that finding out if a number n is prime takes us O(n 1/2 ) time. It also takes us O(n 5 ) time. The second bound guarantees less. It is not tight. 12 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET

13
Big Theta Notation The function f(n) = Θ(g(n)) iff there exists positive constants c1 and c2 such that c1Xg(n) < f(n) < c2Xg(n). Average time 13 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET

14
Measurement: Tabular Method Algorithm Algorithm Sum(a,n) { sum := 0.0 for i:= 1 to n do sum := sum +a[i]; return sum } 14 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET SignificantFrequencyTotal n+1 n n+1 n 1 0 Total2n+3 Complexity: O(n)

15
Bounds might not be tight Algorithm Algorithm Add(a,b,c,m,n) { for i:= 1 to m do for j:=1 to n do c[I,j] := a[i,j] + b[i,j]; } 15 Md. Monjur-ul-hasan. Lecturer, Dept. of CSE CUET SignificantFrequencyTotal m+1 n+1 n 0 m+1 mn+m mn Total2mn+2m+1 Complexity: O(mn)

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google