Docsity

Lecture Notes on Analysis of Algorithm Efficiency | COMP 157, Study notes of Algorithms and Programming

Partial preview of the text

Download Lecture Notes on Analysis of Algorithm Efficiency | COMP 157 and more Study notes Algorithms and Programming in PDF only on Docsity!

Analysis

of

Algorithm

Efficiency

COMP

Sept

Time

and

Space

Our

principle

interest

in

analyzing

algorithms

is

to

determine

time

and

space

efficiency

We

measure

these

relative

to

the

problem

size,

which

we

usually

denote

by

n

Time

how

fast

an

algorithm

runs

Space

amount

of

memory

an

algorithm

uses

Time

Estimates

c

op

execution

time

of

basic

operation

(machine/language/system

dependent)

C(n)

number

of

basic

operations

required

for

a

problem

of

size

n

T(n)

estimated

running

time

n

C

c

n

T

op

our

real

interest

Growth

Functions

Growth

Functions

< day

< month

year

Worst/Best/Average

For

a

particular

problem

size,

we

may

be

interested

in:

Worst

case

efficiency:

Longest

running

time

for

any

input

of

size

n

Best

case

efficiency:

Shortest

running

time

for

any

input

of

size

n

Average

case

efficiency:

Average

running

time

for

all

inputs

of

size

n

Example

Algorithm:

Selection

Sort

Best

case:

Worst

case:

Average

case:

∑

− =

(^11) n i

i

∑

− =

(^11) n i

i

∑

− =

(^11) n i

i

Amortized

Efficiency

This

measure

spreads

cost

over

multiple

runs

of

an

algorithm

Useful

in

cases

when

results

of

a

high

cost

portion

of

an

algorithm

can

be

reused.

Example:

search

sort

binary

search

If

the

data

rarely

changes,

we

can

avoid

the

sort

portion

of

the

algorithm

for

most

runs.

Exercise

Suggest

a

modification

to

a

sorting

algorithm

that

results

in

best

case

comparison

count

of

n

Would

it

be

a

good

idea

to

make

this

modification

to

all

sorting

algorithms?

Asymptotic

Notations

O(g(n))

big

Oh

all

functions

which

grow

slower,

or

the

same,

as

g(n)

(g(n))

big

Omega

all

function

that

grow

faster,

or

the

same,

as

g(n)

(g(n))

big

Theta

all

functions

with

same

order

of

growth

as

g(n)

Big

Omega

Big

Theta

Asymptotic

Properties

Reflexivity:

f(n)

є

O(f(n))

f(n)

є Θ

(f(n))

f(n)

є Ω

(f(n))

Transitivity:

f(n)

є

O(g(n))

and

g(n)

є

O(h(n))

Æ

f(n)

є

O(h(n))

f(n)

є Θ

(g(n))

and

g(n)

є Θ

(h(n))

Æ

f(n)

є Θ

(h(n))

f(n)

є Ω

(g(n))

and

g(n)

є Ω

(h(n))

Æ

f(n)

є Ω

(h(n))