algorithm analysis. algorithm def an algorithm is a step-by-step procedure

84
Algorit Algorit hm hm A A nalys nalys is is

Upload: prosper-bailey

Post on 26-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgoritAlgorithmhm AAnalysnalysisisAlgoritAlgorithmhm AAnalysnalysisis

Page 2: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmDef

An algorithm is a step-by-step procedure

Page 3: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmExample - Compute the sum 10+11+12+13+…+30

public int sum (int nStart, int nEnd) {int sum = 0;for (int i = nStart; i nEnd; i++) {

sum += i;}return sum;

}

Take the first number and continuing add next number until the last number

sum = 0FOR i = nStart TO nEnd

sum = sum + iENDFORRETURN sum

Page 4: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Algorithm AnalysisRequirements

SimplicityReadabilityVerifiabilityValidityRobustnessModifiabilityReuseabilityEfficiency

public int sum (int nStart, int nEnd) {int sum = 0;for (int i = nStart; i nEnd; i++) {

sum += i;}return sum;

}

sum = 0FOR i = nStart TO nEnd

sum = sum + iENDFORRETURN sum

Page 5: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmExample - Finding the largest value

5

8

2

7

Page 6: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmExample - selectionSort

5

8

2

7

Page 7: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmExample - Character permutations

A

B

C

B

A

C

A

C

B

C

A

B

B

C

A

C

B

Ann 321!

Page 8: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationNumber of statements - Mathematical relations

1 )1(

)1( -

1

1 1

1

6

)12)(1(

2

)1(

2

1

1

2

1

1

1

0

1

2

1

1

aa

aa

a

naai

aa

aa

nnni

nnn

ni

n

nnn

i

i

nn

i

i

n

i

n

i

n

i

Proof

n

1...111

naa

s nn 2

1 n

n ...321

n

n2222 ...321

1

210 ...

n

naaaa

n

nnaaaa ...321 321

Page 9: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationNumber of statements - Mathematical relationsProof by Induction

6

)672)(1(

6

)1(6

6

)12)(1()1(

6

)12)(1()1(

6

)672)(1(

6

)1)1(2)(1)1)((1( dvs,1for stemmer (*)at viseSkal

6

)12)(1( dvs ,for stemmer (*)at Anta

1for stemmer dvs 16

321

6

)112)(11(1 11:1

(*) 6

)12)(1(

2

222

1

21

1

2

2

1

1

2

1

2

21

1

2

1

2

kkk

kkkkk

kkkkii

kkk

kkkikn

kkkikn

nin

nnni

k

i

k

i

k

i

k

i

i

n

i

Page 10: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmBasic Timing Analysis - Consecutive statements

count algorithm step

1 a = 4;1 b = 8;1 c = a;

3111)( nT Run time

Page 11: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmBasic Timing Analysis - For loop

count algorithm step

1 for (i = 1;n+1 i <= n;n i++)n x = x + 1;

23)1(1)( nnnnnT

for (i = 1; i <= n; i++)x = x + 1;

Run time

Page 12: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmBasic Timing Analysis - For loop, if statement

count algorithm step

1 max = array[0];1 for ( i = 1;n i < n;n-1 i++)n-1 if (array[i] > max)p max = array[i]; 0 p n-11 return max;

nnT

pnpnnnnT

4)(

131)1()1(11)(

max

Run time

Page 13: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmBasic Timing Analysis

count algorithm step

1 sum = 0;1 + (n+1) + n for ( i = 1; i <= n; i++)1*n + (n+1)*n + n*n for ( j = 1; j <= n; j++)n*n sum = sum + 1;1 return sum;

342

*1111

**)1(*1)1(11)(

2

22

nn

nnnnnn

nnnnnnnnT

Run time

Page 14: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmBasic Timing Analysis - Simple For loop

count algorithm step

1+(n+1)+n for ( i = 1; i <= n; i++)kn Sk

)()(

2)2()1(1)(

nnT

nkknnnnT

Run time

ComplexityAsymptotic Notation

Page 15: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmBasic Timing Analysis - Simple For loop

count algorithm step

for ( i = 1; i < =n; i++)kn Sk

)()(

)(

nnT

knnT

Run time

ComplexityAsymptotic Notation

Page 16: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmBasic Timing Analysis - Double For loop

for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)

Sk

)()(

11)(

2

2

111 11 1

nnT

knknnknnkkknTn

i

n

i

n

i

n

j

n

i

n

j

Page 17: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmBasic Timing Analysis - Single For loop + Double For loop

for (i = 1; i <= n; i++)Sk

for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)

Sk

)()(

11)(

2

2

111 11 1

nnT

knknknnknknknnkknkknkknnTn

i

n

i

n

i

n

j

n

i

n

j

Page 18: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmBasic Timing Analysis - Double For loop

for (i = 1; i <= n; i++)for (j = i; j <= n; j++)

Sk

)()(

222

1)1(1)(

2

2

111

nnT

nk

nk

nn

kinkkknTn

i

n

i

n

ij

n

i

n

ij

Page 19: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmBasic Timing Analysis - Triple For loop

for (i = 1; i <= n; i++)for (j = i; j <= n; j++)

for (k=i; k <=j; k++) Sm

)()(

6

23

2

23

2

)1(

2

3

6

)12)(1(

2

1

1232

1

2

3

2

1

2

)2)(1(

)1(2

)1(1)1(...321

)1(1)(

3

23

2

1

2

11

2

1

11

111

nnT

mnnn

nnnnn

nnnn

m

nninim

ininm

inin

minm

ijmmmnT

n

i

n

i

n

i

n

i

n

i

n

i

n

i

n

ij

n

i

n

ij

j

ik

n

i

n

ij

j

ik

Page 20: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Different functions and their graphs

Expression Name--------------------------------------------1 constantlog n logarithmiclog2n log squaredn linearn log n n log nn2 quadraticn3 cubic2n exponential--------------------------------------------

Page 21: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmGrowth of Common Functions

Page 22: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

AlgorithmGeneral Rules

LoopsThe running time of a loop is at most the running time of the statementsinside the loop (including tests) times the number of iterations.

Nested LoopsAnalyze these inside out. The total running time of at statement insidea group of nested loops is the running time of the statement multiplied by the products of the sizes of all the loops.

Consecutive statementsThese just add.

if-then-elseThe running time of an if-then-else statement is never more thanthe running time of the test plus the larger of the running times of each part.

Page 23: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 01

A 01

The time required to fetch an operand from memory is a constant:

The time required to store an operand in memory is a constant:

fetch

store

Memoryfetchstore

Page 24: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsC 01

C 01

The time required to perform a simple assignment statementis the time required to fetch an operand plus the time required to store an operand: storefetchassignment

xfetch

store

xy

y

1y …

Page 25: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 02

A 02

The time required to perform elementary operations,such as addition, subtraction, multiplication, division, and comparison,are all contants.

/

*

Page 26: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 01 - A 02 - Example

Time required to perform the following statement:

storefetch 2

y

y

y

yy

1

1

Page 27: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 03

A 03

The time required to call a method is a contant,and the time required to return from a method is a constant.

return

call

Page 28: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 04

A 04

The time required to pass an argument to a methodis the same as the time required to store a value in memory:

storepass

Page 29: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 04 - Example

Time required to perform the following statement:

)(

)(

2

xfcallstorefetch

storexfstorecallfetch

T

T

)(xfy

fetch xcall f

passing x

running time of f

assignment to y

Page 30: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 01 - A 02 - A 03 - Example

n

i

i1

Running timeTime required to computethe following simple arithmetic series summation:

1 public class Example {2 public static int sum (int n) {3 int result = 0;4 for (int i = 0; i <= n; i++)5 result += i;6 return result;7 }8 }

resultreturn

iresultn

inc

ninb

ia

result

returnfetch

storefetch

storefetch

fetch

storefetch

storefetch

6

)2( 5

)2( 4

)1()2( 4

1 4

0 3

ττττ t

ττττt

nttT(n)

storefetch

returnstorefetch

226

25

2

1

21

Page 31: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 05

A 05

The time required for the address calculationimplied by an array subscripting operation, a[i], is a constant.This time does not include the time to compute the subscript expression,nor does it include the time to access (fetch or store) the array element.

][

Page 32: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 05 - Example

Time required to perform the following statement:

storefetch

storefetchfetchfetch

][

][

3

][iay

fetch afetch i

address subscripting calcutaion

fetch a[i]

assignment to y

Page 33: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 01 - A 02 - A 03 - A 04 - Example - Horner’s Rule

n

i

ii xa

0

1 public class Example {2 public static int horner (int[] a, int n, int x) {3 int result = a[n];4 for (int i = n-1; i >= 0; i--)5 result = result * x + a[i];6 return result;7 }8 }

resultreturn

iresultn

inc

inb

nia

naresult

returnfetch

storexfetch

storefetch

fetch

storefetch

storefetch

6

)5( 5

)2( 4

0 )1()2( 4

1 2 4

][ 3 3

][

][

xstorefetch

returnstorefetch

ττττ t

ττττt

nttT(n)

][2

][1

21

29

28

Page 34: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 01 - A 02 - A 03 - A 04 - Recursive Methods - Factorial [1/2]

0 1

0 1

0

0 1

!

1

n)!(n-n

n

ni

n

n n

i

1 public class Example {2 public static int factorial (int n) {3 if (n == 0)4 return 1;5 else6 return n * factorial(n-1);7 }8 }

)T(n-returncall

storefetch

returnfetch

fetchfetch

1

3 6

4

2 2 3

*

returncallxstorefetch

returnfetch

ττττ t

τττt

ntnT

ntnT

5

3

0 )1(

0 )(

2

1

2

1

Page 35: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 01 - A 02 - A 03 - A 04 - Recursive Methods - Factorial [2/2]

0 1

0 1!

n)!(n-n

nn

returncallxstorefetch

returnfetch

ττττ t

τττt

ntnT

ntnT

5

3

0 )1(

0 )(

2

1

2

1

21

2

2

222

222

2

)0(

)(

...

3)3(2)3(

2)2()2(

)1()(

ntt

ntT

ktknT

tnTttnT

tnTttnT

tnTnT

Page 36: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 01 … A 05 - Finding the Largest Element of an Array [1/2]

iani

max0

1 public class Example {2 public static int max (int[] a) {3 int result = a[0];4 for (int i = 1; i < a.length; i++)5 if (a[i] > result)6 result = a[i];7 return result;8 }9 }

storefetch

][storefetch

fetchstore

aa

n

in

τττt

τττττ t

ττττt

tnttaaanT

jiji

][3

2

1

)(max

1

1321110

3

28

2

),...,,,(

0

resultreturn

resultn

inc

lengthainb

ia

aresult

storefetch

storefetch

fetch

storefetch

fetch

storefetch

storefetch

3 7

a[i]result ?)(3 6

a[i] )1()4( 5

)1()2( 4

. )2( 4

1 4

]0[ 3 3

][

][

][

Page 37: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Basic AxiomsA 01 … A 05 - Finding the Largest Element of an Array [2/2]

iani

max0

)(max

1

1321110

0

),...,,,(

jiji aa

n

in tnttaaanT

ntntttnT

ni

HtHntt

ti

ntt

ti

ntt

iaaPptpnttnT

avg

n

inn

n

i

n

i

jij

ii

n

iiavg

ln)1()(

ln1

)1(

11

1

1

1

1)max( )(

3231

1321

31

21

3

1

121

0

1

1321

pi probabilitythat line 6 is executed

)ln()ln(11

1

1

1

11 nxdx

xiH n

nn

in

Page 38: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationDefinition 01 - An Asymtotic Upper Bound - Big Oh

00 )()(0 :0, |)())(( nnncgnfncnfngO

D 01

Consider a function f(n).We say that f(n) is big oh g(n), which we write f(n) O(g(n)),if there exists an integer n0 and a constant c > 0 such that for all integers n n0, 0 f(n) cg(n)

cg(n)

f(n)

n0

Page 39: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationDefinition 01 - An Asymtotic Upper Bound - Big OhAlternative definition

00 )()( :0,

))(()(

nnncgnfcn

ngOnf

D 01

Consider a function f(n) that is non-negative for all integers n >= 0.We say that f(n) is big oh g(n), which we write f(n) = O(g(n)),if there exists an integer n0 and a constant c > 0 such that for all integers n n0, f(n) cg(n)

cg(n)

f(n)

n0

Page 40: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationDefinition 01 - An Asymtotic Upper Bound - Big Oh

))(()(1 ngOnf

00 )()( :0,

))(()(

nnncgnfcn

ngOnf

00 )()(0 :, |)())(( nnncgnfncnfngO

cg(n)

f(n)

n0

))(()(1 ngOnf

Page 41: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationAn Asymtotic Upper Bound - Big Oh

)()(

50

501110

5050

5050

505)(

00

nOnf

nncn

cn

cn

cnn

nnf

f(n) = 5n + 50 We wish to show that f(n) O(n)

00 )()(0 :, |)())(( nnncgnfncnfngO

Page 42: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationAn Asymtotic Upper Bound - Big Oh 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

2

02

0

2

22

)(

)(10 ,1

0)5)(10(

0505

505)(

1cVelger

505)(

nOnf

nncnnfnc

nn

nn

nncnnf

nnf

f(n) = 5n + 50 We wish to show that f(n) = O(n2)

2

2

2

4)(3

2)(2

)(1

505 )(

nng

nng

nng

nnf

Page 43: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 01 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

T 01

If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n) + f2(n) = O(max(g1(n),g2(n))).

Proof

)))(),((max()()(

))()(max(

))()((2

)()()()(),max(

),max(2

2121

210

210

221121

0

210

210

ngngOnfnf

ngngc

ngngc

ngcngcnfnf

nn

nnn

ccc

Page 44: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationExamples 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

)( !

)!( 2

)2(

))((log

)( )(log

)( 3001004

)( 3001004

)1( 350

)( 201004

)( 20

10

210

32

22

323

22

n

n

nk

nOn

nO

On

nOn

nOnn

nOnn

nOnn

O

nOnn

nOn

Page 45: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationExample - 1+2+3+…+n 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

Outer BodyLevel Loop

1 12

1

10 or 10 or 10 or 1

)()( nOnT

Page 46: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationExample - Selection sort 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

Outer Body BodyLevel Outer Inner

Loop Loop

1+1 1+111+1 1+1

10 or 1

10 or 10 or 10 or 1

Page 47: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationNumber of statements 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

)1(41)1()1(441031

1

1

1

ininn

ij

n

ij

2

0

2

0

loopinner for count 9301 loopinner for count 5n

i

n

i

Outer loop

2

0

loopinner for count 92 loopouter for count 2n

i

Total number

Inner loop for a certain i

Page 48: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationNumber of statements 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

772

442244552

2

)1)(2(4)1()45(2

41)45(2

4452

1)-i-(n4 92

loopinner for count 92)(

2

22

2

0

2

0

2

0

2

0

2

0

nn

nnnnnn

nnnn

in

in

nTS

n

i

n

i

n

i

n

i

n

issort

)()( 2nOnTSssort

Page 49: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 01 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

T 02

If f(n) = f1(n) + f2(n) where f1(n) and f2(n) both are non-negative for all intergers n >= 0 such that limn-> f2(n)/f1(n) = L for some limit L >= 0,then f(n) = O(f1(n)).

Proof

))(()(

)( )(

)()()(

)()(

)()()(

)()()( )(

)(

)(

)(lim

1

021010

10211

2211

21

1020 01

2

1

2

nfOnf

Lcccnfc

nfLcnfc

nfcnfc

nfnfnf

nfLnfnnLnf

nfL

nf

nfn

Page 50: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 01 - Example 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

)(

)()()(

01

limlim)(

)(lim

)(

)(

)()()(

3

23

21

3

2

1

2

22

31

21

nO

nn

nfnfnf

nn

n

nf

nf

nnf

nnf

nfnfnf

nnn

f(n) = f1(n) + f2(n) = n3 + n2

Page 51: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 03 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

T 03

If f1(n) = O(g1(n)) and f2(n) = O(g2(n)) then f1(n) x f2(n) = O(g1(n) x g2(n)).

Proof

))()(()()(

))()(()()()()(

),,(

0 0)( 0 0)(

)()( )()(:,,,

2121

0210221121

210210

11

222211112121

ngngOnfnf

nnngngcngcngcnfnf

cccnnmaxn

nnfnnf

nnngcnfnnngcnfccnn

Page 52: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 03 - Example 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

f1(n) = n3 + n2 + n + 1 = O(n3)

f2(n) = n2 + n + 1 = O(n2)

f1(n) x f2(n) = O(n3 x n2) = O(n5)

Page 53: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 04 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

T 04

If f1(n) = O(g1(n)) and g2(n) is a function whose value is non-negativefor integers n >= 0, then f1(n) x g2(n) = O(g1(n) x g2(n)).

Proof

))()(()()(

)()()()(

0 0)(

)()(:,

2121

021021

2

010100

ngngOngnf

nnngngcngnf

nng

nnngcnfcn

Page 54: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 05 - Transitive Property 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

T 05

If f(n) = O(g(n)) and g(n) = O(h(n)) then f(n) = O(h(n)).

Proof

)(

)(

)()(

),,(

)()( )()(:,,,

0

021

11

210210

22112121

nhc

nnnhcc

nnngcnf

cccnnmaxn

nnnhcngnnngcnfccnn

Page 55: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 05 - Example 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

f1(n) = 7n3 = O(n3)

f2(n) = 2n2 = O(n2)

f1(n) + f2(n) = O(f1(n)) because lim f2(n)/ f1(n) = 0f1(n) + f2(n) = O(f1(n)) = O(n3)

Page 56: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 06 - Polynomials 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

T 06

Proof

m

mm

mm

m

m

i

ii

nOnf

aanananana

nanf

)(

0 ...

)(

012

21

1

0

m

n

c

m

ii

ng

mm

iimi

m

m

i

mii

mm

i

ii

m

i

ii

nOnf

nann

an

nnannananf

)(

1 1

1 )(

00)(0

000

Page 57: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationLogarithms 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

logn < n => logn = O(n)

Page 58: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 07 - Logarithms 00 )()( :0,

))(()(

nnncgnfcn

ngOnf

T 07

For every integer k >= 1, logkn = O(n).

Proof

0log

limln

1

ln1

log)1(lim

loglim :1

0log

lim:m1,2,3,...,kfor holdsit Assume

01

limln

1

1ln1

limlog

lim :1

:Induction

0log

lim :Hypothesis

''1

''

n

n

a

m

nan

nm

n

nmk

n

n

naan

n

nk

n

n

ma

n

ma

n

rulesHopitalLma

n

ma

n

nn

rulesHopitalLa

n

ka

n

Page 59: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Names of Common Big Oh Expressions

Expression Name--------------------------------------------O(1) constantO(log n) logarithmicO(log2n) log squaredO(n) linearO(n log n) n log nO(n2) quadraticO(n3) cubicO(2n) exponential--------------------------------------------

Page 60: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic Lower Bound - Omega

00 )()( :0,

))(()(

nnncgnfcn

ngnf

D 01

Consider a function f(n) that is non-negative for all integers n >= 0.We say that f(n) is omega g(n), which we write f(n) = (g(n)),if there exists an integer n0 and a constant c > 0 such that for all integers n >= n0, f(n) >= cg(n)

cg(n)

f(n)

n0

Page 61: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic Upper and Lower Bound - Theta

))(()( ))(()(

))(()(

ngnfngOnf

ngnf

D 01

Consider a function f(n) that is non-negative for all integers n >= 0.We say that f(n) is theta g(n), which we write f(n) = (g(n)),if and only if f(n) = O(g(n)) and f(n) = (g(n)).

c1g(n)

f(n)

n0

c2g(n)

Page 62: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic Lower Bound - Little Oh

))(()( ))(()(

))(()(

ngnfngOnf

ngonf

D 01

Consider a function f(n) that is non-negative for all integers n >= 0.We say that f(n) is little oh g(n), which we write f(n) = o(g(n)),if and only if f(n) = O(g(n)) and f(n) (g(n)).

cg(n)

f(n)

n0

Page 63: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationBig Oh - Omega - Theta - Little Oh

021021 )()()(0 :0,, |)())(( nnngcnfngcnccnfng

00 )()(0 :0, |)())(( nnnfncgncnfng

00 )()(0 :0,0 |)())(( nnncgnfncnfngO

0101

0202

)()(0 :0,

)()(0 :0, |)())((

nnnfngcnc

nnngcnfncnfngo

Page 64: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationExample An Asymtotic Upper and Lower Bound - Theta

)()(

)()()()(

3)(

nnf

nnfnOnf

nnf

f(n) = n + 3

Page 65: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationExample An Asymtotic Upper and Lower Bound - Little Oh

00 )()(0 :, |)())(( nnncgnfncnfngO

)()(

)()()()(

3)(

2

22

nonf

nnfnOnf

nnf

f(n) = n + 3

Page 66: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationT 06 - Polynomials 00 )()( :0,

))(()(

nnncgnfcn

ngnf

T 08

Proof

m

mm

mm

m

m

i

ii

nnf

aanananana

nanf

)(

0 ...

)(

012

21

1

0

1

0

1

0

1

1

0)1(

1

1

0

)1(1

1

0

1

00

1

1

1 1

1

)(

m

iim

m

m

ii

mmm

m

iimi

mmm

m

i

mii

mmm

m

i

ii

mm

m

i

ii

mm

m

i

ii

an

an

nanna

nn

anna

nnanna

nana

nanananf

m

c

m

iim

ng

m

m

ii

m

m

ii

m

m

iim

m

iim

m

iim

nnf

nnan

annf

aa

n

aa

n

an

a

an

aan

a

)(

1

)(

11

1

01

11

0

1

00)(

1

00

1

00

1

00

1

00

1

0

Page 67: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationExample An Asymtotic Upper and Lower Bound - Theta

00 )()(0 :, |)())(( nnncgnfncnfngO

)()(

)()()()(

...)( 011

1

m

mm

mm

mm

nnf

nnfnOnf

ananananf

f(n) = amnm + am-1nm-1 + … + a1n + a0

Page 68: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Asymtotic NotationRules for Big Oh Analysis

00 )()(0 :, |)())(( nnncgnfncnfngO

Sequence S1;S2;…Sm;

Iteration for (S1; S2; S3)S4;

Conditional if (S1)S2;

elseS3;

O(max ( T1(n), T2(n),…,Tm(n))

O(max ( T1(n), T2(n) x (I(n)+1),T3(n) x I(n),T4(n) x I(n)

)

O(max ( T1(n), T2(n),T3(n)

)

Page 69: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Actual Lower BoundsAssuming a 100 MHz Clock, c = 1 Cycle, n0 = 0

yearsyearssns

yearsssnsn

hourssnsnsn

mssnsnsnn

mssnsnsn

nsnsnsnsn

nsnsnsns

KnKnnn

n 510293

3

2

101056.210)2(

3657.1012.510)(

05.32.1064010)(

2102.1024010)log(

5.1002.18010)(

2001003010)(log

10101010)1(

1024181

Page 70: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

RecursionOne recursive call with half size - Binary Search

11

2

11)( n

nT

nnT

1)(log)( 2 nnT

))(log()( nnT

Page 71: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

RecursionOne recursive call with half size - Binary Search - Proof I

1)(log )(

...

1)16(log 5141)8( )16( 16

1)8(log 4131)4( )8( 8

1)4(log 3121)2( )4( 4

1)2(log 2111)1( )2( 2

1)1(log 1 )1( 1

2

2

2

2

2

2

nnTn

TT

TT

TT

TT

TProof

11

2

11)( n

nT

nnT

1)(log)( 2 nnT

))(log()( nnT

Page 72: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

RecursionOne recursive call with half size - Binary Search - Proof 2

1)(log

111)(log

11)2(log)(log

112

log

12

12

log2

Assume

1)1(log1)1(

2

2

22

2

2

2

k

k

k

k

kTkT

kkT

TProof

11

2

11)( n

nT

nnT

1)(log)( 2 nnT

))(log()( nnT

Page 73: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

RecursionTwo recursive calls with half size - Sum

11

22

11)( n

nT

nnT

12)( nnT

)()( nnT

Page 74: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

RecursionTwo recursive calls with half size - Sum - Proof I

12)(

...

1162 311301)8(2 )16( 16

182 151141)4(2 )8( 8

142 5161)2(2 )4( 4

122 3121)1(2 )2( 2

112 1 )1( 1

nnTn

TT

TT

TT

TT

TProof

11

22

11)( n

nT

nnT

12)( nnT

)()( nnT

Page 75: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

RecursionTwo recursive calls with size (about) n-1 - Fibonacci number - Recursive

11)2()1(

101)(

nnTnT

nnnT

)2()( nOnT

nnT 2)(

Page 76: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

RecursionTwo recursive calls with size (about) n-1 - Fibonacci number - Proof 1

k

k

kk

kk

kkk

kk

kk

kTkTkT

kTkT

T

T

2

22

22

222

222

122

1)2()1(

2)2( 2)1( Assume

21)1(

221)0(

1

11

21

221

21

21

1

00

Proof

11)2()1(

101)(

nnTnT

nnnT

)2()( nOnT

nnT 2)(

Page 77: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

RecursionFibonacci number - Iterative

)()( nOnT

Page 78: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

RecursionMore than two recursive calls in every level - Permutation

1)1(

11)(

nnnT

nnT

)!()( nnT

!)( nnT

Page 79: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

RecursionMore than two recursive calls in every level - Permutation - Proof I

!)(

...

!5 120245)4(5 )5( 5

!4 2464)3(4 )4( 4

!3 623)2(3 )3( 3

!2 212)1(2 )2( 2

!1 1 )1( 1

nnTn

TT

TT

TT

TT

T

Proof

1)1(

11)(

nnnT

nnT

)!()( nnT

!)( nnT

Page 80: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Recursive calls

abnnT

abnnnT

abnnT

kbancnb

naT

nnT

kk

kk

ka

k

b

)(

log)(

)(

011 1

11)(

log

Page 81: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Recursive callsExamples

loop simple size half with call recursive One )( )2/()( )(

loop simple size half with calls recursive Two log)( )2/(2)(

size half with call recursive One log)( )2/()( log)(

size half with calls recursive Two )( )2/(2)( )(

1

11)(

log

nnTcnnTnTabnnT

nnnTcnnTnT

nnTcnTnTabnnnT

nnTcnTnTabnnT

ncnb

naT

nnT

kk

kk

ka

k

b

Page 82: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Recursive callsProof

nnmmk

km

mkmk

mm

mm

i

i

kb

kmmmm

i

mmmk

m

ii

ikmmm

m

ii

ik

m

m

m

ii

ikm

ii

im

ii

i

m

mk

m

m

m

m

mkmkmm

m

k

bb

k

k

naaanTab

nba

banTab

x

xxx

x

x

x

xxxx

nnnTncncmbmcaacaabTab

a

bcaabT

a

bc

a

bT

a

bT

a

bc

a

bT

a

bT

a

bc

a

bT

a

bT

bcbaTbcb

baTbT

kbancnb

naT

nnT

loglog

1

1

1

10

0

111

1

1

1

1

1

1)(

)(

1 )1(

1 )()1(

11

1 1

)log()()log1()1(1)(

)()(

)()()(

)()()(

)()()(

)()()()(

011 1

11)(

Page 83: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

Complexity

Algorithm Running time------------------------------------------------------Linear search O(n)Binary search O(logn)Bubble sort O(n2)Quick sort O(nlogn)------------------------------------------------------

Page 84: Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure

ENDENDENDEND