Docsity

Algorithm Analysis: Understanding Big-O Notation and Asymptotic Execution Time, Study notes of Computer Science

Partial preview of the text

Download Algorithm Analysis: Understanding Big-O Notation and Asymptotic Execution Time and more Study notes Computer Science in PDF only on Docsity!

1

CS162: Introduction to

Computer Science II

Algorithm Analysis

2

Algorithms

  • Definition of an algorithm: A set of

ordered steps for solving a problem

  • What do we typically analyze?
    • How long an algorithm takes
    • How much memory it uses

3

Algorithm analysis

Suppose that algorithm A processes n data

elements in time T and uses up space S

Algorithm analysis attempts to estimate how T

and/or S is affected by changes in n.

In other words, T and/or S is a function of n

when we use A.

4

Example (Find the sum of integers from 1 to

n ):

int n = 1000; long sum = 0; for(int k = 1; k <= n; k++) sum += k;

Suppose that this code executes in 2 ms.

What is the expected execution time if n is

changed to

500? 2 ms

2000? 5 ms 10,000? 21 ms

The relationship appears to be (n/500)+1 ms.

Note that the change in T is linear, i.e., T

approximately doubles when n doubles, etc.

7

Big-O Example 1

Print multiples of n from 1 to 500:

int n = 127; for(int k = 1; k <= 500; k++) System.out.print(k * n);

Suppose that this code executes in 2 ms.

What is the expected execution time if n is

changed to

8

Big-O Example 1

Print multiples of n from 1 to 500:

int n = 127; for(int k = 1; k <= 500; k++) System.out.print(k * n);

Suppose that this code executes in 2 ms. What

is the expected execution time if n is changed

to

254? 2 ms

1270? 2 ms

63? 2 ms

The "change" in T is constant, i.e., T does not depend on the size of n.

9

Big-O Example 1

Print multiples of n from 1 to 500:

int n = 127; for(int k = 1; k <= 500; k++) System.out.print(k * n);

We call this an O (1) algorithm.

  • Note that it doesn't matter if it's 1 ... 5 or 1 ... 500.
  • Yes, 1 … 500 takes longer than 1 … 5, but changing n doesn't change the time for either.
  • Even if you think of it as O (500), 500 = 500 x 1, so drop the constant to get O (1).

10

Big-O Example 2

Create a "multiplication table" with n rows and

n columns:

int n = 100; int table[][] = new int[n][n]; for(int row = 0; row < n; row++) for(int col = 0; col < n; col++) table[row][col] = (row+1) * (col+1); Suppose that this code executes in 10 ms. What is the expected execution time if n is changed to 200? 1000? 50?

13

Asymptotic Execution Time

  • Remember that the order of an algorithm is an approximate measure of the change in T as n changes (d T /d n ).
  • The order of an algorithm is called the asymptotic execution time (it only matters when n is a big value)
  • Big-O notation is not very useful for small values of n.

14

Big-O Example 3

Draw a triangle: int n = 40; for (int row = 1; row <= n; row++){ for (int col = 1; col <= row; col++) System.out.print(''); System.out.println(""); }* How many times is System.out.print('*'); executed? 1 + 2 + 3 + … + 40 = 820 = n(n+1)/2 = ½(n 2 + n) How does T change when n is doubled to 80? 1 + 2 + 3 + … + 80 = 3240 = approximately 4 x 820 (3280) What is the order of this algorithm? Drop the constant, and use the dominating term This algorithm is O (n^2 ) … not O (n^2 + n)

15

Big-O Example 4

How many times is System.out.println(n); executed?

This algorithm is O (log 2 n) = O (log n)

Find out how many times an integer can be divided by 2: int n = 1000; while (n > 0){ System.out.println(n); n /= 2; }

16

Big-O Example 4

Note:

When we say O (log n) the base of the logarithm isn’t important. Why?

Because, e.g., (log 2 n) = log (^) x n / (log (^) x 2). (log (^) x 2) is a constant which can be dropped.

19

How Big is N?

  • ...for all students in CS162? 60
  • ...for all students at OSU? ~20,
  • ...for all vehicles registered in Oregon?

    2,000,

  • ...for all web pages on the internet?

    10,000,000,000 (perhaps as many as 1 trillion)

20

Calculating Expected Execution

Times

  • Given an O (n log n) algorithm that processes 1024 data items in 3 ms. What is the expected time for this algorithm to process 4096 data items?
  • Set up the ratios: "order" of 2nd data set time for 2nd data set "order" of 1st data set time for 1st data set

4096 log 4096 x 1024 log 1024 3

  • Solve for x. (14.4 ms)

=

21

Formal Big-O

  • Algorithm A has a time requirement T

that is a function of n (the number of

elements).

  • f(n) = O(g(n)) if there exist positive integers c and N such that f(n) ≤ c*g(n) for all n ≥ N.
  • Big-O gives a maximum time

requirement, i.e., the “worst case” time

requirement.