Docsity

Algorithm Efficiency: Benchmarking and Asymptotic Analysis for Complexity - Prof. Nelson P, Study notes of Computer Science

Partial preview of the text

Download Algorithm Efficiency: Benchmarking and Asymptotic Analysis for Complexity - Prof. Nelson P and more Study notes Computer Science in PDF only on Docsity!

Algorithmic Complexity

CMSC 132

Department of Computer Science

University of Maryland, College Park

Algorithm Efficiency

Efficiency

Amount of resources used by algorithm

Time, space

Measuring efficiency

Benchmarking Asymptotic analysis

Benchmarking

Advantages

Precise information for given configuration

Implementation, hardware, inputs

Disadvantages

Affected by configuration

Data sets (usually too small) Hardware Software

Affected by special cases (biased inputs) Does not measure

intrinsic

efficiency

Asymptotic Analysis

Approach

Mathematically analyze efficiency Calculate time as function of input size

n

T

O[ f(

n

) ]

T is on the order of f(

n

“Big O” notation

Advantages

Measures intrinsic efficiency Dominates efficiency for large input sizes

Linear Search Algorithm

Algorithm

Guess number = 1

If incorrect, increment guess by 1

Repeat until correct

Example

Given number between 1…100 Pick 20 Guess sequence = 1, 2, 3, 4 … 20 Required 20 guesses

Linear Search Algorithm

Analysis of # of guesses needed for 1…n

If number = 1, requires 1 guess If number = n, requires n guesses On average, needs n/2 guesses Time = O( n ) =

Linear

time

Binary Search Algorithm

Example

Given number between 1…100 Pick 20 Guesses =

= 25, Answer = too large, subtract

= 12 , Answer = too large, subtract

Answer = too small, add

Answer = too small, add

Answer = too large, subtract

Answer = too large, subtract

Required 7 guesses

Binary Search Algorithm

Analysis of # of guesses needed for 1…n

If number = n/2, requires 1 guess If number = 1, requires log

2

( n ) guesses

If number = n, requires log

2

( n ) guesses

On average, needs log

2

( n ) guesses

Time = O( log

2

( n ) ) =

Log

time

Asymptotic Complexity

Comparing two linear functions

4n+

n/

Running Time

Size

Asymptotic Complexity

Comparing two functions

n/2 and 4n+3 behave similarly Run time roughly doubles as input size doubles Run time increases

linearly

with input size

For large values of n

Time(2n) / Time(n) approaches exactly 2

Both are O(

n

) programs

Asymptotic Complexity

Comparing two functions

log

2

( n )

and

5 * log

2

( n ) + 3

behave similarly

Run time roughly increases by constant as input sizedoubles Run time increases

logarithmically

with input size

For large values of n

Time(2n) – Time(n) approaches constant Base of logarithm does not matter

Simply a multiplicative factor

log

a

N = (log

b

N) / (log

b

a)

Both are O(

log(n)

) programs

Asymptotic Complexity

Comparing two quadratic functions

2 n

2

n

2

Running Time

Size

Big-O Notation

Represents

Upper bound on number of steps in algorithm Intrinsic efficiency of algorithm for large inputs

f(n)

O(…)

input size

# steps

Formal Definition of Big-O

Function f(

n

) is

( g(

n

) ) if

For some positive constants M, N

0

M

×

×

×

×

g(n)

f(n), for all n

N

0

Intuitively

For some coefficient M & all data sizes

N

0

M

××××

g(n) is always greater than f(n)