Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 2 Algorithm Analysis.

Similar presentations


Presentation on theme: "Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 2 Algorithm Analysis."— Presentation transcript:

1 Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 2 Algorithm Analysis

2 Algorithm Analysis General, This Chapter learn for Running-time. How to estimate the time required for a program. How to reduce the running time of a program from day or years to fractions of a second. The results of careless use of recursion. Very efficient algorithms to raise a number to a power and to compute the greatest common divisor of two numbers.

3 Analysis of Algorithm

4 Asymptotic Notation Convenient for describing the worst-case running-time f(n), T(n) Function ① Big-Oh - O(f(N)) ② Big-Omega - Ω(g(N)) ③ Big-Theta - ϴ(h(N)) ④ Little-Oh - o(p(N))

5 Function(Continued)

6

7

8

9

10 ④ Little-Oh T(N) = o(p(N)) if T(N) = O(p(N)) and T(N) ≠ ϴ(p(N)) Rule1 If T 1 (N) = O(f(N)) and T 2 (N) = O(g(N)) i) T 1 (N) + T 2 (N) = O(f(N) + g(N)) = O(max(f(N), g(N))) ii) T 1 (N) * T 2 (N) = O(f(N) * g(N)) iii) T 1 (N) – T 2 (N) ≠ O(min(f(N), O(g(N))) iv) T 1 (N) / T 2 (N) ≠ O(f(N)/g(N))

11 Function(Continued)

12

13

14

15

16

17

18

19

20

21 Running Time Calculations There are several ways to estimate the running time of a Program. To simplify the analysis we do computing a Big-Oh running time of the program. The presentation is followed by some sample examples and some general rules in calculating running time.

22 For loops the running time of the statements inside the loop times the number of iterations. In Nested loops the total running time of a statement inside a group of nested loops is the running time of the statement multiplied by the product of sizes of all loops. for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ ) k++; The above program fragment is O(N^2).

23 Consecutive statements: As an example, the following programming fragment, which has O(N) followed by O(N^2), is also O(N^2). for( i = 0; i < n; i++ ) { a[ i ] = 0; for( i = 0; i < n; i++ ) { for( j = 0; j < n; j++ ) a[ i ] += a[ j ] + i + j; }}

24 public static long factorial( int n ) { if( n <= 1 ) return 1; else return n * factorial( n - 1 ); }

25 Using recursion: public static long fib( int n ) { if( n <= 1 ) return 1; else return fib( n - 1 ) + fib( n - 2 ); } We have the formula for the running time of fib(n): T(N) = T(N − 1) + T(N − 2) + 2

26 Maximum Subsequence Sum Problem We can present this algorithm to solve the Maximum subsequence sum problem in four ways a) Cubic maximum contiguous subsequence sum algorithm, b) Quadratic maximum contiguous subsequence sum algorithm, c) Recursive maximum contiguous subsequence sum algorithm, d) Linear-time maximum contiguous subsequence sum algorithm.

27 Algorithm 1 for Max. Subseq. Sum Pbm

28 Algorithm 2 for Max. Subseq. Sum Pbm

29 Algorithm 3 for Max. Subseq. Sum Pbm

30 Algorithm 4 for Max. Subseq. Sum Pbm

31 Binary Search It is a search Algorithm used to finf some ‘n’ data in a group of data. Thus to calculate the maximum we need to scan the array once => O(n). Now, if the array is sorted in some order, then we just compare the value to be found with center object of the array. In this comparison we keep eliminating half of the array every step. => O(log n).

32

33 If suppose we have a two dimensional array, We would scan each row and column in the order to be n*n = O(n^2). If the two dimensional array is sorted, it can still be reduced to O(n) by using zig-zag pattern to scan. The worst case we will end up scanning (2n-1) = O(n).


Download ppt "Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 2 Algorithm Analysis."

Similar presentations


Ads by Google