20
Chapter 2: Chapter 2: Algorithm Algorithm Analysis 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

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

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

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

2

Binary searchEuclid’s algorithmExponentialsRules to count operations

Logarithms in Running Time

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

3

Divide-and-conquer algorithms

Subsequently reducing the problem by a factor of two

require O(logN) operations

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

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)

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

5

Example: 8 leaves, 3 levels

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

6

Binary Search

Solution 1:

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

O(N) operations.

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

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

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

8

Euclid's algorithm

Finding the greatest common divisor (GCD)

GCD of M and N, M > N,

= GCD of N and M % N

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

9

GCD and recursion

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

The answer is the last nonzero remainder.

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

10

M N rem 24 15 9

15 9 6

9 6 3

6 3 0

3 0

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

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)

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

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)

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

13

Computing XN

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

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

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

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

}

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

15

Why O(LogN)

If N is odd : two multiplications

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

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

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.

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

17

How to count operations single statements (not function calls)

: constant O(1) = 1.

sequential fragments: the maximum of the operations of each fragment

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

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

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

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

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

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