Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.

Similar presentations


Presentation on theme: "Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data."— Presentation transcript:

1 Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

2 2 Binary search Euclid’s algorithm Exponentials Rules to count operations Logarithms in Running Time

3 3 Divide-and-conquer algorithms Subsequently reducing the problem by a factor of two require O(logN) operations

4 4 Why logN? A complete binary tree with N leaves has logN levels. Each level in the divide-and- conquer algorithms corresponds to an operation Hence the number of operations is O(logN)

5 5 Example: 8 leaves, 3 levels

6 6 Binary Search Solution 1: Scan all elements from left to right, each time comparing with X. O(N) operations.

7 7 Binary Search Solution 2: O(logN) Find the middle element A mid in the list and compare it with X If they are equal, stop If X < A mid consider the left part If X > A mid consider the right part Do until the list is reduced to one element

8 8 Euclid's algorithm Finding the greatest common divisor (GCD) GCD of M and N, M > N, = GCD of N and M % N

9 9 GCD and recursion Recursion: If M%N = 0 return N Else return GCD(N, M%N) The answer is the last nonzero remainder.

10 10 M N rem 24 15 9 15 9 6 9 6 3 6 3 0 3 0

11 11 long gcd ( long m, long n) { long rem; while (n != 0) { rem = m % n; m = n; n = rem; } return m; } Euclid’s Algorithm (non-recursive implementation)

12 12 Why O(logN) M % N <= M / 2 After 1 st iteration N appears as first argument, the remainder is less than N/2 After 2 nd iteration the remainder appears as first argument and will be reduced by a factor of two Hence O(logN)

13 13 Computing X N X N = X*(X 2 ) N / 2,N is odd X N = (X 2 ) N / 2,N is even

14 14 long pow (long x, int n) { if ( n == 0) return 1; if (is_Even( n )) return pow(x * x, n/2); else return x * pow ( x * x, n/2); }

15 15 Why O(LogN) If N is odd : two multiplications The operations are at most 2logN: O(logN)

16 16 Another recursion for X N Another recursive definition that reduces the power just by 1: X N = X*X N -1 Here the operations are N-1, i.e. O(N) and the algorithm is less efficient than the divide-and-conquer algorithm.

17 17 How to count operations single statements (not function calls) : constant O(1) = 1. sequential fragments: the maximum of the operations of each fragment

18 18 single loop running up to N, with single statements in its body: O(N) single loop running up to N, with the number of operations in the body O(f(N)): O( N * f(N) ) How to count operations

19 19 two nested loops each running up to N, with single statements: O(N 2 ) divide-and-conquer algorithms with input size N: O(logN) Or O(N*logN) if each step requires additional processing of N elements How to count operations

20 20 Example: What is the probability two numbers to be relatively prime? tot = 0; rel = 0; for ( i = 0; i <= n; i++) for (j = i+1; j <= n; j++) { tot++; if ( gcd( i, j ) ==1) rel++; } return (rel/tot); Running time = ?


Download ppt "Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data."

Similar presentations


Ads by Google