Docsity

Asymptotic Analysis of Algorithms: Worst, Best, and Average Case Analysis, Cheat Sheet of Design

Partial preview of the text

Download Asymptotic Analysis of Algorithms: Worst, Best, and Average Case Analysis and more Cheat Sheet Design in PDF only on Docsity!

Analysis of Algorithms

CS 477/

Asymptotic Analysis Instructor: George Bebis (Chapter 3, Appendix A)

Analysis of Algorithms

  • (^) An algorithm is a finite set of precise instructions

for performing a computation or for solving a

problem.

  • (^) What is the goal of analysis of algorithms?
    • (^) To compare algorithms mainly in terms of running time but also in terms of other factors (e.g., memory requirements, programmer's effort etc.)
  • (^) What do we mean by running time analysis?
    • (^) Determine how running time increases as the size of the problem increases.

Types of Analysis

  • (^) Worst case
    • (^) Provides an upper bound on running time
    • (^) An absolute guarantee that the algorithm would not run longer, no matter what the inputs are
  • (^) Best case
    • (^) Provides a lower bound on running time
    • (^) Input is the one for which the algorithm runs the fastest
  • (^) Average case
    • (^) Provides a prediction about the running time
    • (^) Assumes that the input is random

Lower Bound  Running Time  Upper Bound

How do we compare algorithms?

• We need to define a number of objective

measures.

(1) Compare execution times?

Not good : times are specific to a particular

computer !!

(2) Count the number of statements executed?

Not good : number of statements vary with

the programming language as well as the

style of the individual programmer.

Example

  • (^) Associate a "cost" with each statement.
  • (^) Find the "total cost“ by finding the total number of times each statement is executed. Algorithm 1 Algorithm 2 Cost Cost arr[0] = 0; c 1 for(i=0; i<N; i++) c 2 arr[1] = 0; c 1 arr[i] = 0; c 1 arr[2] = 0; c 1 ... ... arr[N-1] = 0; c 1

c 1 +c 1 +...+c 1 = c 1 x N (N+1) x c 2 + N x c 1 = (c 2 + c 1 ) x N + c 2

Another Example

  • (^) Algorithm 3 Cost

sum = 0; c

1

for(i=0; i<N; i++) c

2

for(j=0; j<N; j++) c

2

sum += arr[i][j]; c

3

c

1

+ c

2

x ( N +1) + c

2

x N x ( N +1) + c

3

x N

2

Rate of Growth

  • (^) Consider the example of buying elephants and

goldfish:

Cost : cost_of_elephants + cost_of_goldfish

Cost ~ cost_of_elephants (approximation)

  • (^) The low order terms in a function are relatively

insignificant for large n

n

4

+ 100 n

2

+ 10 n + 50 ~ n

4

i.e., we say that n

4

+ 100 n

2

+ 10 n + 50 and n

4

have

the same rate of growth

Asymptotic Notation

  • (^) O notation: asymptotic “less than”:
    • (^) f(n)=O(g(n)) implies: f(n) “≤” g(n)

  notation: asymptotic “greater than”:

  • (^) f(n)=  (g(n)) implies: f(n) “≥” g(n)

  notation: asymptotic “equality”:

  • (^) f(n)=  (g(n)) implies: f(n) “=” g(n)

Visualizing Orders of Growth

  • (^) On a graph, as

you go to the

right, a faster

growing

function

eventually

becomes

larger...

f A ( n )=30 n + Increasing nf B ( n )= n 2

Value of function

More Examples …

  • (^) n 4

+ 100 n

2

+ 10 n + 50 is O ( n

4

  • (^10) n 3

+ 2 n

2

is O ( n

3

  • (^) n 3 - n 2

is O ( n

3

  • (^) constants
    • (^) 10 is O (1)
    • (^) 1273 is O (1)

Example (cont’d)

Algorithm 3 Cost sum = 0; c 1 for(i=0; i<N; i++) c 2 for(j=0; j<N; j++) c 2 sum += arr[i][j]; c 3


c 1

  • c 2 x ( N +1) + c 2 x N x ( N +1) + c 3 x N 2 = O ( N 2 )

Asymptotic notations

  • (^) O-notation

Examples

- (^) 2n^2 = O(n^3 ): - n 2 = O(n 2 ): - (^) 1000n^2 +1000n = O(n^2 ): - (^) n = O(n^2 ): 2n 2 ≤ cn 32 ≤ cnc = 1 and n 0

n 2 ≤ cn 2c ≥ 1c = 1 and n 0

_1000n 2 +1000n ≤ 1000n 2

  • n 2 =1001n 2_  c=1001 and n 0

n ≤ cn

2

 cn ≥ 1  c = 1 and n

0

More Examples

  • (^) Show that 30 n +8 is O( n ).
    • Show^  c , n 0 : 30 n +8^ ^ cn,^  n >n 0.
      • Let c= 31, n 0 =8. Assume n > n 0 =8. Then cn = 31 n = 30 n + n > 30 n +8, so 30 n +8 < cn.