Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time...

Preview:

DESCRIPTION

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

Citation preview

Chapter 2: Chapter 2: Algorithm Algorithm AnalysisAnalysis

Application of Big-Oh to program analysis

Logarithms in Running TimeLydia Sinapova, Simpson College

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

2

Binary searchEuclid’s algorithmExponentialsRules to count operations

Logarithms in Running Time

3

Divide-and-conquer algorithms

Subsequently reducing the problem by a factor of two

require O(logN) operations

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

Example: 8 leaves, 3 levels

6

Binary Search

Solution 1:

Scan all elements from left to right, each time comparing with X.

O(N) operations.

7

Binary SearchSolution 2: O(logN)Find the middle element Amid in the list and compare it with X

If they are equal, stop If X < Amid consider the left part If X > Amid consider the right part

Do until the list is reduced to one element

8

Euclid's algorithm

Finding the greatest common divisor (GCD)

GCD of M and N, M > N,

= GCD of N and M % N

9

GCD and recursion

Recursion: If M%N = 0 return NElse return GCD(N, M%N)

The answer is the last nonzero remainder.

10

M N rem 24 15 9

15 9 6

9 6 3

6 3 0

3 0

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

Why O(logN)

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

13

Computing XN

XN = X*(X2) N / 2 ,N is odd

XN = (X2) N / 2 ,N is even

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

Why O(LogN)

If N is odd : two multiplications

The operations are at most 2logN:O(logN)

16

Another recursion for XN

Another recursive definition that reduces the power just by 1:

XN = X*XN -1

Here the operations are N-1, i.e. O(N) and the algorithm is less efficient than the divide-and-conquer algorithm.

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

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

two nested loops each running up to N, with single statements: O(N2)

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

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 = ?