Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.

Similar presentations


Presentation on theme: "CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh."— Presentation transcript:

1 CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th Oct-151

2 1-Oct-15 2 Big-Oh Notation  Big-Oh was introduced for functions’ growth rate comparison in 1927 based on asymptotic behavior  Big-Oh notation characterizes functions according to their growth rates: different functions with the same growth rate may be represented using the same O notation.  A description of a function in terms of big-Oh notation usually only provides an upper bound on the growth rate of the function. CSC201 Analysis and Design of Algorithms

3 1-Oct-15 3 Upper Bound  In general a function f(n) is O(g(n)) if  positive constants c and n 0 such that f(n)  c  g(n)  n  n 0 e.g. if f(n)=1000n and g(n)=n 2, n 0 > 1000 and c = 1 then f(n 0 ) < 1.g(n 0 ) and we say that f(n) = O(g(n))  The O notation indicates 'bounded above by a constant multiple of.' CSC201 Analysis and Design of Algorithms

4 1-Oct-154 Big-Oh, the Asymptotic Upper Bound  Because big O notation discards multiplicative constants on the running time, and ignores efficiency for low input sizes, it does not always reveal the fastest algorithm in practice or for practically-sized data sets, but the approach is still very effective for comparing the scalability of various algorithms as input sizes become large.  If an algorithm‘s time complexity is in the order of O(n 2 ) then it’s growth rate of computation time will not be faster than a quadratic function for input that is large enough  Some upper bounds may be too broad, for example saying that 2n 2 = O(n 3 )   By definition if c = 1 and n 0 = 2, it is better to say that 2n 2 = O(n 2 ) CSC201 Analysis and Design of Algorithms

5 1-Oct-15 5 For all n>6, g(n) > 1 f(n). f (n) is in big-O of g(n) then, f(n) is in O(g(n)). Example 1 CSC201 Analysis and Design of Algorithms

6 1-Oct-15 6 Example 2 There exists n 0 such that for all n>n 0, If f(n) < 1 g(n) then f(n) is in O(g(n)) CSC201 Analysis and Design of Algorithms

7 1-Oct-15 7 There exists n 0 =5, c=3.5, for all n>n0, if f(n) < c h(n) then f(n) is in O(h(n)). Example 3 CSC201 Analysis and Design of Algorithms

8 1-Oct-15 8 CSC201 Analysis and Design of Algorithms

9 1-Oct-15 9 Exercise on O-notation  Show that f(n)=3n 2 +2n+5 is in O(n 2 ) 10 n 2 = 3n 2 + 2n 2 + 5n 2  3n 2 + 2n + 5 for n  1  f(n) or f(n) ≤10 n 2 consider c = 10, n 0 = 1 f(n) ≤c g(n 2 ) for n  n 0 then f(n) is in O(n 2 ) CSC201 Analysis and Design of Algorithms

10 1-Oct-15 10 Usage of Big-Oh  We should always write Big-Oh in the most simple form e.g. 3n 2 +2n+5 = O(n 2 ) It is not wrong to write these functions in term of Big-Oh as below, but the most appropriate form should be in the most simple form 3n 2 +2n+5 = O(3n 2 +2n+5) 3n 2 +2n+5 = O(n 2 +n) 3n 2 +2n+5 = O(3n 2 ) CSC201 Analysis and Design of Algorithms

11 1-Oct-15 11 Exercise on O-notation  f 1 (n) = 10 n + 25 n 2  f 2 (n) = 20 n log n + 5 n  f 3 (n) = 12 n log n + 0.05 n 2  f 4 (n) = n 1/2 + 3 n log n O(n 2 ) O(n log n) O(n 2 ) O(n log n) CSC201 Analysis and Design of Algorithms

12 1-Oct-15 12 Classification of Function : BIG-Oh  A function f(n) is said to be of at most logarithmic growth if f(n) = O(log n)  A function f(n) is said to be of at most quadratic growth if f(n) = O(n 2 )  A function f(n) is said to be of at most polynomial growth if f(n) = O(n k ), for some natural number k > 1  A function f(n) is said to be of at most exponential growth if there is a constant c, such that f(n) = O(c n ), and c > 1  A function f(n) is said to be of at most factorial growth if f(n) = O(n!). CSC201 Analysis and Design of Algorithms

13 1-Oct-15 13 Classification of Function : BIG-Oh (cont.)  A function f(n) is said to have constant running time if the size of the input n has no effect on the running time of the algorithm (e.g., assignment of a value to a variable). The equation for this algorithm is f(n) = c  Other logarithmic classifications: f(n) = O(n log n) f(n) = O(log log n) CSC201 Analysis and Design of Algorithms

14 1-Oct-15 14 Big O Fact A polynomial of degree k is O(n k ) Proof: ถ้า f(n) = b k n k + b k-1 n k-1 + … + b 1 n + b 0 และให้ a i = | b i | ดังนั้น f(n)  a k n k + a k-1 n k-1 + … + a 1 n + a 0 CSC201 Analysis and Design of Algorithms

15 1-Oct-15 15 Some Rules Transitivity f(n) = O(g(n)) and g(n) = O(h(n))  f(n) = O(h(n)) Addition f(n) + g(n) = O(max { f(n),g(n)}) Polynomials a 0 + a 1 n + … + a d n d = O(n d ) Heirachy of functions n + log n = O(n); 2 n + n 3 = O(2 n ) CSC201 Analysis and Design of Algorithms

16 1-Oct-15 16 Some Rules Base of Logs ignored log a n = O(log b n) Power inside logs ignored log(n 2 ) = O(log n) Base and powers in exponents not ignored 3 n is not O(2 n ) a (n)2 is not O(a n ) CSC201 Analysis and Design of Algorithms

17 1-Oct-15 17 Big-Oh Complexity  O(1) The cost of applying the algorithm can be bounded independently of the value of n. This is called constant complexity.  O(log n) The cost of applying the algorithm to problems of sufficiently large size n can be bounded by a function of the form k log n, where k is a fixed constant. This is called logarithmic complexity.  O(n) linear complexity  O(n log n) n lg n complexity  O(n 2 ) quadratic complexity CSC201 Analysis and Design of Algorithms

18 1-Oct-15 18 Big-Oh Complexity (cont.)  O(n 3 ) cubic complexity  O(n 4 ) quadratic complexity  O(n 32 ) polynomial complexity  O(c n ) If constant c>1, then this is called exponential complexity  O(2 n ) exponential complexity  O(e n ) exponential complexity  O(n!) factorial complexity CSC201 Analysis and Design of Algorithms

19 1-Oct-15 19 Practical Complexity t < 500 CSC201 Analysis and Design of Algorithms

20 1-Oct-15 20 Practical Complexity t < 5000 CSC201 Analysis and Design of Algorithms

21 1-Oct-15 21 Practical Complexity CSC201 Analysis and Design of Algorithms

22 1-Oct-15 22 Things to Remember in Analysis  Constants or low-order terms are ignored if f(n) = 2n 2 then f(n) = O(n 2 )  running time and memory are important resources for algorithm and very large input affects algorithm performance the most  Parameter N, normally means size of input N may refers to degree of polynomial, size of input file for data sorting, or number of nodes in graph CSC201 Analysis and Design of Algorithms

23 1-Oct-15 23 Things to Remember in Analysis  Worst case analysis means the worst-case execution time of particular concern (it is important to know how much time might be needed in the worst case to guarantee that the algorithm will always finish on time)  Average performance,and also worst-case), performance is the most used in algorithm analysis, using probabilistic analysis techniques, especially expected value, to determine expected running times (i.e. the case of typical input data) CSC201 Analysis and Design of Algorithms

24 1-Oct-15 24 General Rules for Analysis (1) 1. Consecutive statements  count only the most time required of the consecutive block of statements  count only the most time required of the consecutive loops Block #1 Block #2 t1t1 t2t2 t 1 + t 2 = max(t 1,t 2 ) CSC201 Analysis and Design of Algorithms

25 1-Oct-15 25 General Rules for Analysis (2) 2. If/Else if cond then S1 else S2 Block #1Block #2 t1t1 t2t2 Max(t 1,t 2 ) CSC201 Analysis and Design of Algorithms

26 1-Oct-15 26 General Rules for Analysis (3) 3. For Loops  Running time of a for-loop is at most the running time of the statements inside the for- loop times number of iterations for (i = sum = 0; i < n; i++) sum += a[i];  for loop iterates n times, executes 2 assignment statements each iteration ==> asymptotic complexity of O(n) CSC201 Analysis and Design of Algorithms

27 1-Oct-15 27 General Rules for Analysis (4) 4. Nested For-Loops Analyze inside-out: total time is the product of time required for each loop for (i =0; i < n; i++) for (j = 0, sum = a[0]; j <= i ; j++) sum += a[j]; printf("sum for subarray - through %d is %d\n", i, sum); CSC201 Analysis and Design of Algorithms

28 1-Oct-15 28 General Rules for Analysis CSC201 Analysis and Design of Algorithms

29 1-Oct-15 29 General Rules for Analysis  Analysis strategy :  analyze from inside out  analyze function calls first CSC201 Analysis and Design of Algorithms

30 1-Oct-15 30 CSC201 Analysis and Design of Algorithms


Download ppt "CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh."

Similar presentations


Ads by Google