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

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

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

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.

Analysis of Algorithm

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

Function(Continued)

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

Function(Continued)

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.

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

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; }}

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

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

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.

Algorithm 1 for Max. Subseq. Sum Pbm

Algorithm 2 for Max. Subseq. Sum Pbm

Algorithm 3 for Max. Subseq. Sum Pbm

Algorithm 4 for Max. Subseq. Sum Pbm

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

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