Docsity

Algorithm Analysis: Measuring Algorithm Efficiency with Big O Notation, Slides of Computer Science

Partial preview of the text

Download Algorithm Analysis: Measuring Algorithm Efficiency with Big O Notation and more Slides Computer Science in PDF only on Docsity!

Algorithm Analysis andBig Oh Notation

Docsity.com

Measuring the Efficiency of Algorithms

^

Analysis of algorithms^ ^

Area of computer science ^

Provides tools for

determining efficiency

of different

methods of solving a problem^ 

E.g., the sorting problem - which sorting method is more efficient ^

Comparing the

efficiency

of different methods of solution.

^ Concerned with

significant

differences

^ E.g.:

^ n

  • the number of items to be sorted ^ Is the running time proportional to

n^ or proportional to

(^2) n?

^ Big difference: e.g., for

n^ =100 it results in 100-fold difference; for

n=

1000 it results in 1000-fold difference; for

n^ = …

Docsity.com

The Execution Time of Algorithms

^

Count the number of basic operations ofan algorithm^ 

Read, write, compare, assign, jump,arithmetic operations (increment, decrement,^ add, subtract, multiply, divide),

open, close,

add, subtract, multiply, divide),

open, close,

logical operations (not/complement, AND, OR,XOR), …

Docsity.com

The Execution Time of Algorithms

^

Counting

an algorithm’s

operations

^

Example: calculating a sum of array elements^ int sum = item[0];int j = 1;while (j <

n

)

{

<- 1 assignment <- 1 assignment <-

n

comparisons

{

sum += item[j];++j; }

<-

n

-1 plus/assignments <-

n

-1 plus/assignments Total: 3n operations

^

Notice: Problem size

n

= number of elements in an array

This problem of size n requires solution with 3n operations

Docsity.com

Common Growth-Rate Functions - I

Docsity.com

Common Growth-Rate Functions - II

^ Differences

among the growth-rate functions

grow with

n

^ See the

differences

growing on the diagram on the previous page

^ The bigger n, the bigger differences -- that’s why algorithm efficiency is “

concern for large problems only”

Docsity.com

Big-Oh Notation

^ Algorithm A is order f(n) —denoted O(f(n))—

if there exist

constants k and n

such that A requires <= k*f(n) time units 0

to solve a problem of size n >= n

0

^ Examples

^ n

^ n

2 /5^ ^

O(n

2 ): k=1/5, n

^ 5*n

^ O(n): k=5, n

Docsity.com

More Examples

^

How about

n

2 -3n+10?

^

It is

O(n

2 ) if

there exist k and n

such that 0

kn

2 ≥ n

2 -3n+10 for all n ≥

n

0

^ We see (fig.) that:

3 n

2 ≥ n

2 -3n+10 for all n ≥

^ So

k=3, n

^ More k

  • n^0

pairs could be found, but finding just one is

More k

  • n^0

pairs could be found, but finding just one is

enough to prove that

(^2) n -3n+10 is O(n

2 )

Docsity.com

Worst-case vs. Average-case Analyses

^

An algorithm can require different times tosolve different problems of the same size.

^

Worst-case analysis

= find the maximum

number of operations an algorithm canexecute in all situations^ ^

Worst

  • case analysis is

easier to calculate

^

Worst

  • case analysis is

easier to calculate

^

More common ^

Average-case analysis

= enumerate all

possible situations, find the time of each ofthe m possible cases, total and divide by m^ ^

Average-case analysis is

harder to compute

^

Yields a

more realistic

expected behavior

Docsity.com

Bigger Example: Analysis of Selection Sort

Divides the array into twoparts: already sorted, and notyet sorted.

values [ 0 ]

[ 1 ]

On each pass, finds thesmallest of the unsortedelements, and swap it into itscorrect place,

thereby

increasing the number ofsorted elements by one.

[ 2 ] [ 3 ] [ 4 ]

Docsity.com

Selection Sort: End of Pass One

values [ 0 ]

[ 1 ]

U

SORTED

[ 2 ] [ 3 ] [ 4 ]

U N S O R T E D

Docsity.com

Selection Sort: Pass Two

SORTED

values [ 0 ]

[ 1 ] [ 2 ] [ 3 ]

U N S O R

[ 3 ] [ 4 ]

R T E D

To find the smallest in UNSORTED:indexMin =

1

comp. 1:

check if values[2] = 10 < values[indexMin] = 24 - yes => indexMin =

2

comp. 2:

check if values[3] = 36 < values[indexMin] = 10 - NO

comp. 3:

check if values[4] = 12 < values[indexMin] = 10 - NO

Thus indexMin =

2;^

swap values[1] = 24 with values[indexMin] = 10 – see next slide

Docsity.com

Selection Sort: Pass Three

values [ 0 ]

[ 1 ] [ 2 ]

U N S

SORTED

[ 3 ] [ 4 ]

S O R T E D

To find the smallest in UNSORTED:indexMin =

2

comp. 1:

check if values[3] = 36 < values[indexMin] = 24 - NO

comp. 2:

check if values[4] = 12 < values[indexMin] = 24 -

yes => indexMin =

4

Thus indexMin =

4;^

swap values[2] = 24 with values[indexMin] = 12 – see next slide

Docsity.com

Selection Sort: End of Pass Three

values [ 0 ]

[ 1 ]

S O R

[ 2 ] [ 3 ] [ 4 ]

T E D

UNSORTED

Docsity.com