cs 340chapter 2: algorithm analysis1 time complexity the best, worst, and average-case complexities...

20
CS 340 Chapter 2: Algorithm Analysis 1 Time Complexity Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the size of the instances. It is difficult to work with these functions exactly because they are often very complicated, with many little up and down bumps. Thus it is usually cleaner and easier to talk about upper and lower bounds of such functions. This is where the big Oh notation comes into the picture.

Upload: samantha-barker

Post on 01-Jan-2016

217 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 1

Time ComplexityTime ComplexityTime ComplexityTime Complexity

The best, worst, and average-case complexities of a given algorithm are numerical functions of the size of the instances.

It is difficult to work with these functions exactly because they are often very complicated, with many little up and down bumps. Thus it is usually cleaner and easier to talk about upper and lower bounds of such functions.

This is where the big Oh notation comes into the picture.

Page 2: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 2

Time ComplexityTime ComplexityTime ComplexityTime Complexity

Upper and lower bounds smooth out the behavior of complex functions

Page 3: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 3

Time Complexity - Big-OTime Complexity - Big-OTime Complexity - Big-OTime Complexity - Big-O

T(n) = O(f(n))

means c.f(n) is an upper bound on T(n), where thereexists some constant c such that T(n) is always <= c.f(n) for large enough n.

Example: n3 + 3n2 + 6n + 5 is O(n3). (Use c = 15 and n0 = 1.)

Example: n2 + n logn is O(n2). (Use c = 2 and n0 = 1.)

Page 4: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 4

Each of the algorithms below has O(Each of the algorithms below has O(nn33) time complexity...) time complexity...Each of the algorithms below has O(Each of the algorithms below has O(nn33) time complexity...) time complexity...

(In fact, the execution time for Algorithm A is (In fact, the execution time for Algorithm A is nn33 + + nn22 + + nn, and the execution time for Algorithm B , and the execution time for Algorithm B

is is nn33 + 101 + 101nn22 + + nn.).)

(In fact, the execution time for Algorithm A is (In fact, the execution time for Algorithm A is nn33 + + nn22 + + nn, and the execution time for Algorithm B , and the execution time for Algorithm B

is is nn33 + 101 + 101nn22 + + nn.).)

Demonstrating The Big-O ConceptDemonstrating The Big-O ConceptDemonstrating The Big-O ConceptDemonstrating The Big-O Concept

1,1101,1101,1101,110 11,11011,11011,11011,110

1,010,1001,010,1001,010,1001,010,100 2,010,1002,010,1002,010,1002,010,100

1,001,001,0001,001,001,0001,001,001,0001,001,001,000 1,101,001,0001,101,001,0001,101,001,0001,101,001,000

1,000,100,010,0001,000,100,010,0001,000,100,010,0001,000,100,010,000 1,010,100,010,0001,010,100,010,0001,010,100,010,0001,010,100,010,000

1,000,010,000,100,0001,000,010,000,100,0001,000,010,000,100,0001,000,010,000,100,000 1,001,010,000,100,0001,001,010,000,100,0001,001,010,000,100,0001,001,010,000,100,0001,000,001,000,001,000,01,000,001,000,001,000,00000

1,000,001,000,001,000,01,000,001,000,001,000,00000

1,000,101,000,001,000,1,000,101,000,001,000,000000

1,000,101,000,001,000,1,000,101,000,001,000,000000

10101010

100100100100

1,0001,0001,0001,000

10,00010,00010,00010,000

100,000100,000100,000100,000

1,000,0001,000,0001,000,0001,000,000

AAAA BBBB

ALGORITHMALGORITHMALGORITHMALGORITHM

InputInputSizeSize

nn

InputInputSizeSize

nn

Page 5: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 5

(In fact, the execution time for Algorithm C is (In fact, the execution time for Algorithm C is nn22 + 2 + 2nn + 3, and the execution time for + 3, and the execution time for

Algorithm D is Algorithm D is nn22 + 1002 + 1002nn + 3.) + 3.)

(In fact, the execution time for Algorithm C is (In fact, the execution time for Algorithm C is nn22 + 2 + 2nn + 3, and the execution time for + 3, and the execution time for

Algorithm D is Algorithm D is nn22 + 1002 + 1002nn + 3.) + 3.)

A Second Big-O DemonstrationA Second Big-O DemonstrationA Second Big-O DemonstrationA Second Big-O DemonstrationEach of the algorithms below has O(Each of the algorithms below has O(nn22) time complexity...) time complexity...Each of the algorithms below has O(Each of the algorithms below has O(nn22) time complexity...) time complexity...

123123123123 10,12310,12310,12310,123

10,20310,20310,20310,203 110,203110,203110,203110,203

1,002,0031,002,0031,002,0031,002,003 2,002,0032,002,0032,002,0032,002,003

100,020,003100,020,003100,020,003100,020,003 110,020,003110,020,003110,020,003110,020,003

10,000,200,00310,000,200,00310,000,200,00310,000,200,003 10,100,200,00310,100,200,00310,100,200,00310,100,200,003

1,000,002,000,0031,000,002,000,0031,000,002,000,0031,000,002,000,003 1,001,002,000,0031,001,002,000,0031,001,002,000,0031,001,002,000,003

10101010

100100100100

1,0001,0001,0001,000

10,00010,00010,00010,000

100,000100,000100,000100,0001,000,001,000,0000

1,000,001,000,0000

CCCC DDDD

ALGORITHMALGORITHMALGORITHMALGORITHM

InputInputSizeSize

nn

InputInputSizeSize

nn

Page 6: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 6

(In fact, the execution time for Algorithm E is (In fact, the execution time for Algorithm E is nn loglognn + 5 + 5nn, and the execution time for Algorithm F , and the execution time for Algorithm F

is is nn log lognn + 105 + 105nn. Note that the linear term for . Note that the linear term for Algorithm F will dominate until Algorithm F will dominate until nn reaches 2 reaches 2105105.).)

(In fact, the execution time for Algorithm E is (In fact, the execution time for Algorithm E is nn loglognn + 5 + 5nn, and the execution time for Algorithm F , and the execution time for Algorithm F

is is nn log lognn + 105 + 105nn. Note that the linear term for . Note that the linear term for Algorithm F will dominate until Algorithm F will dominate until nn reaches 2 reaches 2105105.).)

One More Big-O DemonstrationOne More Big-O DemonstrationOne More Big-O DemonstrationOne More Big-O DemonstrationEach of the algorithms below has O(Each of the algorithms below has O(nlognnlogn) time complexity…) time complexity…Each of the algorithms below has O(Each of the algorithms below has O(nlognnlogn) time complexity…) time complexity…

83838383 1,0831,0831,0831,083

1,1641,1641,1641,164 11,16411,16411,16411,164

14,96614,96614,96614,966 114,966114,966114,966114,966

182,877182,877182,877182,877 1,182,8771,182,8771,182,8771,182,877

2,160,9642,160,9642,160,9642,160,964 12,160,96412,160,96412,160,96412,160,964

24,931,56924,931,56924,931,56924,931,569 124,931,569124,931,569124,931,569124,931,569

10101010

100100100100

1,0001,0001,0001,000

10,00010,00010,00010,000

100,000100,000100,000100,0001,000,001,000,0000

1,000,001,000,0000

EEEE FFFF

ALGORITHMALGORITHMALGORITHMALGORITHM

InputInputSizeSize

nn

InputInputSizeSize

nn

Page 7: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 7

Big-O Represents An Upper Big-O Represents An Upper BoundBound

Big-O Represents An Upper Big-O Represents An Upper BoundBound

If T(If T(nn) is O() is O(f(n)f(n)), then ), then f(n)f(n) is basically a is basically a capcap on how bad on how bad T(n)T(n) will behave when will behave when nn gets big. gets big.If T(If T(nn) is O() is O(f(n)f(n)), then ), then f(n)f(n) is basically a is basically a capcap on how bad on how bad T(n)T(n) will behave when will behave when nn gets big. gets big.

g(n)g(n)r(n)r(n)

b(n)b(n)

p(n)p(n)

y(n)y(n)

v(n)v(n)

Is g(n) Is g(n) O(r(n))?O(r(n))?

Is r(n) Is r(n) O(g(n))?O(g(n))?

Is v(n) Is v(n) O(y(n))?O(y(n))?

Is y(n) Is y(n) O(v(n))?O(v(n))?

Is b(n) Is b(n) O(p(n))?O(p(n))?

Is p(n) Is p(n) O(b(n))?O(b(n))?

Page 8: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 8

Function T(Function T(nn) is said to be ) is said to be ((gg((nn)) if there are positive )) if there are positive constants constants cc and and nn00 such that T( such that T(nn) ) c g c g ((nn) for any ) for any nn nn00 (i.e., (i.e., T(T(nn) is ultimately ) is ultimately bounded belowbounded below by by c g c g ((nn))))..– Example:Example: n n33 + 3 + 3nn22 + 6 + 6nn + 5 is + 5 is ((nn33). (Use ). (Use cc = 1 and = 1 and nn00 = 1.) = 1.)– Example:Example: n n22 + + nn log lognn is is ((nn22). (Use ). (Use cc = 1 and = 1 and nn00 = 1.) = 1.)

Function T(Function T(nn) is said to be ) is said to be ((gg((nn)) if there are positive )) if there are positive constants constants cc and and nn00 such that T( such that T(nn) ) c g c g ((nn) for any ) for any nn nn00 (i.e., (i.e., T(T(nn) is ultimately ) is ultimately bounded belowbounded below by by c g c g ((nn))))..– Example:Example: n n33 + 3 + 3nn22 + 6 + 6nn + 5 is + 5 is ((nn33). (Use ). (Use cc = 1 and = 1 and nn00 = 1.) = 1.)– Example:Example: n n22 + + nn log lognn is is ((nn22). (Use ). (Use cc = 1 and = 1 and nn00 = 1.) = 1.)

Time Complexity Terminology: Big-Time Complexity Terminology: Big-OmegaOmega

Time Complexity Terminology: Big-Time Complexity Terminology: Big-OmegaOmega

g(n)g(n)

r(n)r(n)

r(n) is not (g(n)) since for every positive constant c,

(c)g(n) ultimately gets bigger than r(n)

g(n) is (r(n)) since g(n)

exceeds (1)r(n) for all n-values

past nrnrnr

Page 9: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 9

Function T(Function T(nn) is said to be ) is said to be ((hh((nn)) if T()) if T(nn) is both O() is both O(hh((nn)) )) and and ((hh((nn))))..– Example:Example: n n33 + 3 + 3nn22 + 6 + 6nn + 5 is + 5 is ((nn33).).– Example:Example: n n22 + + nn log lognn is is ((nn22).).

Function T(Function T(nn) is said to be ) is said to be ((hh((nn)) if T()) if T(nn) is both O() is both O(hh((nn)) )) and and ((hh((nn))))..– Example:Example: n n33 + 3 + 3nn22 + 6 + 6nn + 5 is + 5 is ((nn33).).– Example:Example: n n22 + + nn log lognn is is ((nn22).).

Time Complexity Terminology: Big-Time Complexity Terminology: Big-ThetaTheta

Time Complexity Terminology: Big-Time Complexity Terminology: Big-ThetaTheta

g(n)g(n)

r(n)r(n)r(n) is (g(n)) since r(n) is squeezed between (1)g(n) and (2)g(n) once n exceeds

n0

g(n) is (r(n)) since

g(n) is squeezed between

(½)r(n) and (1)r(n) once n exceeds n0n0n0

Page 10: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 10

Function T(Function T(nn) is said to be o() is said to be o(p p ((nn)) if T()) if T(nn) is O() is O(p p ((nn)) )) but but notnot ((p p ((nn))))..– Example:Example: n n33 + 3 + 3nn22 + 6 + 6nn + 5 is O( + 5 is O(nn44). (Use ). (Use cc = 15 and = 15 and

nn00 = 1.) However, = 1.) However, nn33 + 3 + 3nn22 + 6 + 6nn + 5 is + 5 is notnot ((nn44).).

Proof (by contradiction):Proof (by contradiction):

Assume that there Assume that there areare positive constants positive constants cc and and nn00 such that such that nn33 + 3 + 3nn22 + 6 + 6nn + 5 + 5 c nc n44 for all for all nn nn00..

Then dividing by Then dividing by nn44 on both sides yields the fact on both sides yields the fact that (1/that (1/nn)+(3/)+(3/nn22)+(6/)+(6/nn33)+(5/)+(5/nn44) ) cc, for all , for all nn nn00..

Since limSince limnn((1/((1/nn)+(3/)+(3/nn22)+(6/)+(6/nn33)+(5/)+(5/nn44)) = 0, we )) = 0, we must conclude that 0 must conclude that 0 cc, which contradicts the , which contradicts the fact that fact that cc must be a must be a positivepositive constant. constant.

Function T(Function T(nn) is said to be o() is said to be o(p p ((nn)) if T()) if T(nn) is O() is O(p p ((nn)) )) but but notnot ((p p ((nn))))..– Example:Example: n n33 + 3 + 3nn22 + 6 + 6nn + 5 is O( + 5 is O(nn44). (Use ). (Use cc = 15 and = 15 and

nn00 = 1.) However, = 1.) However, nn33 + 3 + 3nn22 + 6 + 6nn + 5 is + 5 is notnot ((nn44).).

Proof (by contradiction):Proof (by contradiction):

Assume that there Assume that there areare positive constants positive constants cc and and nn00 such that such that nn33 + 3 + 3nn22 + 6 + 6nn + 5 + 5 c nc n44 for all for all nn nn00..

Then dividing by Then dividing by nn44 on both sides yields the fact on both sides yields the fact that (1/that (1/nn)+(3/)+(3/nn22)+(6/)+(6/nn33)+(5/)+(5/nn44) ) cc, for all , for all nn nn00..

Since limSince limnn((1/((1/nn)+(3/)+(3/nn22)+(6/)+(6/nn33)+(5/)+(5/nn44)) = 0, we )) = 0, we must conclude that 0 must conclude that 0 cc, which contradicts the , which contradicts the fact that fact that cc must be a must be a positivepositive constant. constant.

Time Complexity Terminology: Time Complexity Terminology: Little-OLittle-O

Time Complexity Terminology: Time Complexity Terminology: Little-OLittle-O

Page 11: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 11

To formally analyze the performance of algorithms, we will To formally analyze the performance of algorithms, we will use a computational model with a couple of simplifying use a computational model with a couple of simplifying assumptions:assumptions:

– Each simple instruction (assignment, comparison, Each simple instruction (assignment, comparison, addition, multiplication, memory access, etc.) is assumed addition, multiplication, memory access, etc.) is assumed to execute in a single time unit.to execute in a single time unit.

– Memory is assumed to be limitless, so there is always Memory is assumed to be limitless, so there is always room to store whatever data is needed.room to store whatever data is needed.

The size of the input, The size of the input, nn, will normally be used as our main , will normally be used as our main variable, and we’ll primarily be interested in “worst case” variable, and we’ll primarily be interested in “worst case” scenarios.scenarios.

To formally analyze the performance of algorithms, we will To formally analyze the performance of algorithms, we will use a computational model with a couple of simplifying use a computational model with a couple of simplifying assumptions:assumptions:

– Each simple instruction (assignment, comparison, Each simple instruction (assignment, comparison, addition, multiplication, memory access, etc.) is assumed addition, multiplication, memory access, etc.) is assumed to execute in a single time unit.to execute in a single time unit.

– Memory is assumed to be limitless, so there is always Memory is assumed to be limitless, so there is always room to store whatever data is needed.room to store whatever data is needed.

The size of the input, The size of the input, nn, will normally be used as our main , will normally be used as our main variable, and we’ll primarily be interested in “worst case” variable, and we’ll primarily be interested in “worst case” scenarios.scenarios.

Computational Model For Algorithm Computational Model For Algorithm AnalysisAnalysis

Computational Model For Algorithm Computational Model For Algorithm AnalysisAnalysis

Page 12: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 12

Rule One: LoopsRule One: LoopsThe running time of a loop is at most the running The running time of a loop is at most the running time of the statements inside the loop, multiplied time of the statements inside the loop, multiplied by the number of iterations.by the number of iterations.

Rule One: LoopsRule One: LoopsThe running time of a loop is at most the running The running time of a loop is at most the running time of the statements inside the loop, multiplied time of the statements inside the loop, multiplied by the number of iterations.by the number of iterations.

General Rules For Running Time General Rules For Running Time CalculationCalculation

General Rules For Running Time General Rules For Running Time CalculationCalculation

Example:Example:

for (i = 0; i < n; i++)for (i = 0; i < n; i++) // n iterations // n iterations A[i] = (1-t)*X[i] + t*Y[i]; // 12 time unitsA[i] = (1-t)*X[i] + t*Y[i]; // 12 time units // per iteration// per iteration

Example:Example:

for (i = 0; i < n; i++)for (i = 0; i < n; i++) // n iterations // n iterations A[i] = (1-t)*X[i] + t*Y[i]; // 12 time unitsA[i] = (1-t)*X[i] + t*Y[i]; // 12 time units // per iteration// per iteration

(Retrieving X[i] requires one addition and one memory (Retrieving X[i] requires one addition and one memory access, as does retrieving Y[i]; the calculation involves a access, as does retrieving Y[i]; the calculation involves a

subtraction, two multiplications, and an addition; subtraction, two multiplications, and an addition; assigning A[i] requires one addition and one memory assigning A[i] requires one addition and one memory

access; and each loop iteration requires a comparison and access; and each loop iteration requires a comparison and either an assignment or an increment, thus totals twelve either an assignment or an increment, thus totals twelve

primitive operations.)primitive operations.)

(Retrieving X[i] requires one addition and one memory (Retrieving X[i] requires one addition and one memory access, as does retrieving Y[i]; the calculation involves a access, as does retrieving Y[i]; the calculation involves a

subtraction, two multiplications, and an addition; subtraction, two multiplications, and an addition; assigning A[i] requires one addition and one memory assigning A[i] requires one addition and one memory

access; and each loop iteration requires a comparison and access; and each loop iteration requires a comparison and either an assignment or an increment, thus totals twelve either an assignment or an increment, thus totals twelve

primitive operations.)primitive operations.)Thus, the total running time is 12Thus, the total running time is 12nn time units, i.e., this part of the time units, i.e., this part of the

program is O(program is O(nn).).

Thus, the total running time is 12Thus, the total running time is 12nn time units, i.e., this part of the time units, i.e., this part of the

program is O(program is O(nn).).

Page 13: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 13

Rule Two: Nested LoopsRule Two: Nested LoopsThe running time of a nested loop is at most the The running time of a nested loop is at most the running time of the statements inside the running time of the statements inside the innermost loop, multiplied by the product of the innermost loop, multiplied by the product of the number of iterations of all of the loops.number of iterations of all of the loops.

Rule Two: Nested LoopsRule Two: Nested LoopsThe running time of a nested loop is at most the The running time of a nested loop is at most the running time of the statements inside the running time of the statements inside the innermost loop, multiplied by the product of the innermost loop, multiplied by the product of the number of iterations of all of the loops.number of iterations of all of the loops.Example:Example: for (i = 0; i < n; i++)for (i = 0; i < n; i++) // n iterations. 2 ops each// n iterations. 2 ops each for (j = 0; j < n; j++)for (j = 0; j < n; j++) // n iterations, 2 ops each// n iterations, 2 ops each C[i,j] = j*A[i] + i*B[j];C[i,j] = j*A[i] + i*B[j]; // 10 time units/iteration// 10 time units/iteration

(2 for retrieving A[i], 2 for retrieving B[j], 3 for the RHS arithmetic, 3 for (2 for retrieving A[i], 2 for retrieving B[j], 3 for the RHS arithmetic, 3 for assigning C[i,j].)assigning C[i,j].)

Total running time: ((10+2)n+2)n = 12Total running time: ((10+2)n+2)n = 12nn22+2+2nn time units, time units, which is O(which is O(nn22).).

Example:Example: for (i = 0; i < n; i++)for (i = 0; i < n; i++) // n iterations. 2 ops each// n iterations. 2 ops each for (j = 0; j < n; j++)for (j = 0; j < n; j++) // n iterations, 2 ops each// n iterations, 2 ops each C[i,j] = j*A[i] + i*B[j];C[i,j] = j*A[i] + i*B[j]; // 10 time units/iteration// 10 time units/iteration

(2 for retrieving A[i], 2 for retrieving B[j], 3 for the RHS arithmetic, 3 for (2 for retrieving A[i], 2 for retrieving B[j], 3 for the RHS arithmetic, 3 for assigning C[i,j].)assigning C[i,j].)

Total running time: ((10+2)n+2)n = 12Total running time: ((10+2)n+2)n = 12nn22+2+2nn time units, time units, which is O(which is O(nn22).).More complex example More complex example (ignoring (ignoring forfor loop time) loop time)::

for (i = 0; i < n; i++)for (i = 0; i < n; i++) // n iterations// n iterations for (j = i; j < n; j++)for (j = i; j < n; j++) // n-i iterations// n-i iterations C[j,i] = C[i,j] = j*A[i]+i*B[j];C[j,i] = C[i,j] = j*A[i]+i*B[j]; // 13 time units/iter// 13 time units/iter

Total running time: Total running time: ii=0,=0,nn-1-1(( jj==ii, , nn-1-113) = 13) = ii=0,=0,nn-1-1(13((13(nn--ii))))

= 13(= 13( ii=0,=0,nn-1-1n n - - ii=0,=0,nn-1-1ii) = 13() = 13(nn22 - ½ - ½nn((nn-1))-1))

= 6.5= 6.5nn22 + 6.5 + 6.5nn time units, which is also O( time units, which is also O(nn22).).

More complex example More complex example (ignoring (ignoring forfor loop time) loop time):: for (i = 0; i < n; i++)for (i = 0; i < n; i++) // n iterations// n iterations for (j = i; j < n; j++)for (j = i; j < n; j++) // n-i iterations// n-i iterations C[j,i] = C[i,j] = j*A[i]+i*B[j];C[j,i] = C[i,j] = j*A[i]+i*B[j]; // 13 time units/iter// 13 time units/iter

Total running time: Total running time: ii=0,=0,nn-1-1(( jj==ii, , nn-1-113) = 13) = ii=0,=0,nn-1-1(13((13(nn--ii))))

= 13(= 13( ii=0,=0,nn-1-1n n - - ii=0,=0,nn-1-1ii) = 13() = 13(nn22 - ½ - ½nn((nn-1))-1))

= 6.5= 6.5nn22 + 6.5 + 6.5nn time units, which is also O( time units, which is also O(nn22).).

Page 14: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 14

Rule Three: Consecutive StatementsRule Three: Consecutive StatementsThe running time of a sequence of statements The running time of a sequence of statements is merely the sum of the running times of the is merely the sum of the running times of the individual statements.individual statements.

Rule Three: Consecutive StatementsRule Three: Consecutive StatementsThe running time of a sequence of statements The running time of a sequence of statements is merely the sum of the running times of the is merely the sum of the running times of the individual statements.individual statements.Example:Example:

for (i = 0; i < n; i++)for (i = 0; i < n; i++){ // 22n time units{ // 22n time units

A[i] = (1-t)*X[i] + t*Y[i]; // for thisA[i] = (1-t)*X[i] + t*Y[i]; // for thisB[i] = (1-s)*X[i] + s*Y[i]; // entire loopB[i] = (1-s)*X[i] + s*Y[i]; // entire loop

} } for (i = 0; i < n; i++)for (i = 0; i < n; i++) // (12n+2)n time// (12n+2)n time

for (j = 0; j < n; j++)for (j = 0; j < n; j++) // units for // units for thisthis

C[i,j] = j*A[i] + i*B[j];C[i,j] = j*A[i] + i*B[j]; // nested loop// nested loop

Example:Example:for (i = 0; i < n; i++)for (i = 0; i < n; i++){ // 22n time units{ // 22n time units

A[i] = (1-t)*X[i] + t*Y[i]; // for thisA[i] = (1-t)*X[i] + t*Y[i]; // for thisB[i] = (1-s)*X[i] + s*Y[i]; // entire loopB[i] = (1-s)*X[i] + s*Y[i]; // entire loop

} } for (i = 0; i < n; i++)for (i = 0; i < n; i++) // (12n+2)n time// (12n+2)n time

for (j = 0; j < n; j++)for (j = 0; j < n; j++) // units for // units for thisthis

C[i,j] = j*A[i] + i*B[j];C[i,j] = j*A[i] + i*B[j]; // nested loop// nested loopTotal running time: 12Total running time: 12nn22+24+24nn time units, i.e., this time units, i.e., this

code is O(code is O(nn22).).Total running time: 12Total running time: 12nn22+24+24nn time units, i.e., this time units, i.e., this

code is O(code is O(nn22).).

Page 15: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 15

Rule Four: Conditional StatementsRule Four: Conditional StatementsThe running time of an The running time of an ifif--elseelse statement is at statement is at most the running time of the conditional test, most the running time of the conditional test, added to the maximum of the running times of added to the maximum of the running times of the the ifif and and elseelse blocks of statements. blocks of statements.

Rule Four: Conditional StatementsRule Four: Conditional StatementsThe running time of an The running time of an ifif--elseelse statement is at statement is at most the running time of the conditional test, most the running time of the conditional test, added to the maximum of the running times of added to the maximum of the running times of the the ifif and and elseelse blocks of statements. blocks of statements.Example:Example:

if (amt > cost + tax)if (amt > cost + tax) //2 time units//2 time units {{ count = 0;count = 0; //1 time unit//1 time unit while ((count<n) && (amt>cost+tax))while ((count<n) && (amt>cost+tax)) //4 TUs per iter//4 TUs per iter {{ //At most n iter//At most n iter amt -= (cost + tax);amt -= (cost + tax); //3 time units//3 time units count++;count++; //2 time units//2 time units }} cout << “CAPACITY:” << count;cout << “CAPACITY:” << count; //2 time units//2 time units } } elseelse cout << “INSUFFICIENT FUNDS”;cout << “INSUFFICIENT FUNDS”; //1 time unit//1 time unit

Example:Example: if (amt > cost + tax)if (amt > cost + tax) //2 time units//2 time units {{ count = 0;count = 0; //1 time unit//1 time unit while ((count<n) && (amt>cost+tax))while ((count<n) && (amt>cost+tax)) //4 TUs per iter//4 TUs per iter {{ //At most n iter//At most n iter amt -= (cost + tax);amt -= (cost + tax); //3 time units//3 time units count++;count++; //2 time units//2 time units }} cout << “CAPACITY:” << count;cout << “CAPACITY:” << count; //2 time units//2 time units } } elseelse cout << “INSUFFICIENT FUNDS”;cout << “INSUFFICIENT FUNDS”; //1 time unit//1 time unit

Total running time: 2 + max(1 + (4 + 3 + 2)Total running time: 2 + max(1 + (4 + 3 + 2)n n + + 22,, 1) = 9 1) = 9nn + 5 time units, i.e., this code is + 5 time units, i.e., this code is

O(O(nn).).

Total running time: 2 + max(1 + (4 + 3 + 2)Total running time: 2 + max(1 + (4 + 3 + 2)n n + + 22,, 1) = 9 1) = 9nn + 5 time units, i.e., this code is + 5 time units, i.e., this code is

O(O(nn).).

Page 16: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 16

int binsrch(const etype A[], const etype x, const int n)int binsrch(const etype A[], const etype x, const int n){{ int low = 0, high = n-1; // 3 time unitsint low = 0, high = n-1; // 3 time units int middle; // 0 time unitsint middle; // 0 time units while (low <= high) // 1 time unitwhile (low <= high) // 1 time unit {{ middle = (low + high)/2; // 3 time unitsmiddle = (low + high)/2; // 3 time units if (A[middle] < x) // 2 TU | <-- Worst Caseif (A[middle] < x) // 2 TU | <-- Worst Case low = middle + 1; // 2 TU |low = middle + 1; // 2 TU | else if (A[middle] > x) // 2 TU | <-- Worst Caseelse if (A[middle] > x) // 2 TU | <-- Worst Case high = middle - 1; // 2 TU | <-- Worst Casehigh = middle - 1; // 2 TU | <-- Worst Case else // 0 TU |else // 0 TU | return middle; // 1 TU |return middle; // 1 TU | }} return -1; // If search is unsuccessful; 1 time unit.return -1; // If search is unsuccessful; 1 time unit.}}

int binsrch(const etype A[], const etype x, const int n)int binsrch(const etype A[], const etype x, const int n){{ int low = 0, high = n-1; // 3 time unitsint low = 0, high = n-1; // 3 time units int middle; // 0 time unitsint middle; // 0 time units while (low <= high) // 1 time unitwhile (low <= high) // 1 time unit {{ middle = (low + high)/2; // 3 time unitsmiddle = (low + high)/2; // 3 time units if (A[middle] < x) // 2 TU | <-- Worst Caseif (A[middle] < x) // 2 TU | <-- Worst Case low = middle + 1; // 2 TU |low = middle + 1; // 2 TU | else if (A[middle] > x) // 2 TU | <-- Worst Caseelse if (A[middle] > x) // 2 TU | <-- Worst Case high = middle - 1; // 2 TU | <-- Worst Casehigh = middle - 1; // 2 TU | <-- Worst Case else // 0 TU |else // 0 TU | return middle; // 1 TU |return middle; // 1 TU | }} return -1; // If search is unsuccessful; 1 time unit.return -1; // If search is unsuccessful; 1 time unit.}}

Complete Analysis Of Binary Search Complete Analysis Of Binary Search FunctionFunction

Complete Analysis Of Binary Search Complete Analysis Of Binary Search FunctionFunction

In the worst case, the loop will keep dividing the In the worst case, the loop will keep dividing the distance between the low and high indices in half until distance between the low and high indices in half until they are equal, iterating at most logthey are equal, iterating at most lognn times. Thus, the times. Thus, the total running time is: 10logtotal running time is: 10lognn + 4 time units, which is + 4 time units, which is

O(logO(lognn).).

In the worst case, the loop will keep dividing the In the worst case, the loop will keep dividing the distance between the low and high indices in half until distance between the low and high indices in half until they are equal, iterating at most logthey are equal, iterating at most lognn times. Thus, the times. Thus, the total running time is: 10logtotal running time is: 10lognn + 4 time units, which is + 4 time units, which is

O(logO(lognn).).

Page 17: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 17

etype SuperFreq(const etype A[], const int n)etype SuperFreq(const etype A[], const int n){{ etype bestElement = A[0]; // 3 time unitsetype bestElement = A[0]; // 3 time units int bestFreq = 0;int bestFreq = 0; // 1 time unit // 1 time unit int currFreq; // 0 time unitsint currFreq; // 0 time units for (i = 0; i < n; i++) // n iterations; 2 TUs eachfor (i = 0; i < n; i++) // n iterations; 2 TUs each {{ currFreq = 0; // 1 time unitcurrFreq = 0; // 1 time unit for (j = i; j < n; j++) // n-i iterations; 2 TUs eachfor (j = i; j < n; j++) // n-i iterations; 2 TUs each if (A[i] == A[j]) // 3 time unitsif (A[i] == A[j]) // 3 time units currFreq++; // 2 time unitscurrFreq++; // 2 time units if (currFreq > bestFreq) // 1 time unitif (currFreq > bestFreq) // 1 time unit bestElement = A[i]; // 3 time unitsbestElement = A[i]; // 3 time units }} return bestElement; // 1 time unitreturn bestElement; // 1 time unit}}

etype SuperFreq(const etype A[], const int n)etype SuperFreq(const etype A[], const int n){{ etype bestElement = A[0]; // 3 time unitsetype bestElement = A[0]; // 3 time units int bestFreq = 0;int bestFreq = 0; // 1 time unit // 1 time unit int currFreq; // 0 time unitsint currFreq; // 0 time units for (i = 0; i < n; i++) // n iterations; 2 TUs eachfor (i = 0; i < n; i++) // n iterations; 2 TUs each {{ currFreq = 0; // 1 time unitcurrFreq = 0; // 1 time unit for (j = i; j < n; j++) // n-i iterations; 2 TUs eachfor (j = i; j < n; j++) // n-i iterations; 2 TUs each if (A[i] == A[j]) // 3 time unitsif (A[i] == A[j]) // 3 time units currFreq++; // 2 time unitscurrFreq++; // 2 time units if (currFreq > bestFreq) // 1 time unitif (currFreq > bestFreq) // 1 time unit bestElement = A[i]; // 3 time unitsbestElement = A[i]; // 3 time units }} return bestElement; // 1 time unitreturn bestElement; // 1 time unit}}

Analysis Of Another Function:Analysis Of Another Function: SuperFreqSuperFreq

Analysis Of Another Function:Analysis Of Another Function: SuperFreqSuperFreq

Note that the function is obviously O(Note that the function is obviously O(nn22) due to its ) due to its familiar nested loop structure. Specifically, its worst-case familiar nested loop structure. Specifically, its worst-case

running time is ½(7running time is ½(7nn22 + 21 + 21nn + 10). + 10).

Note that the function is obviously O(Note that the function is obviously O(nn22) due to its ) due to its familiar nested loop structure. Specifically, its worst-case familiar nested loop structure. Specifically, its worst-case

running time is ½(7running time is ½(7nn22 + 21 + 21nn + 10). + 10).

Page 18: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 18

humongInt pow(const humongInt &val, const humongInt &n)humongInt pow(const humongInt &val, const humongInt &n){{ if (n == 0)if (n == 0) return humongInt(0);return humongInt(0); if (n == 1)if (n == 1) return val;return val; if (n % 2 == 0)if (n % 2 == 0) return pow(val*val, n/2);return pow(val*val, n/2); return pow(val*val, n/2) * val;return pow(val*val, n/2) * val;}}

humongInt pow(const humongInt &val, const humongInt &n)humongInt pow(const humongInt &val, const humongInt &n){{ if (n == 0)if (n == 0) return humongInt(0);return humongInt(0); if (n == 1)if (n == 1) return val;return val; if (n % 2 == 0)if (n % 2 == 0) return pow(val*val, n/2);return pow(val*val, n/2); return pow(val*val, n/2) * val;return pow(val*val, n/2) * val;}}

What About Recursion?What About Recursion?What About Recursion?What About Recursion?

The worst-case running time would require all 3 conditions to be The worst-case running time would require all 3 conditions to be checked, and to fail (taking 4 time units).checked, and to fail (taking 4 time units).

The last return statement requires 3 time units each time it’s The last return statement requires 3 time units each time it’s executed, which happens logexecuted, which happens lognn times (since it halves times (since it halves nn with each with each

execution, until it reaches a value of 1).execution, until it reaches a value of 1).

When the parameterized When the parameterized nn-value finally reaches 1, two last -value finally reaches 1, two last operations are performed.operations are performed.

Thus, the worst-case running time is 7logThus, the worst-case running time is 7lognn + 2. + 2.

The worst-case running time would require all 3 conditions to be The worst-case running time would require all 3 conditions to be checked, and to fail (taking 4 time units).checked, and to fail (taking 4 time units).

The last return statement requires 3 time units each time it’s The last return statement requires 3 time units each time it’s executed, which happens logexecuted, which happens lognn times (since it halves times (since it halves nn with each with each

execution, until it reaches a value of 1).execution, until it reaches a value of 1).

When the parameterized When the parameterized nn-value finally reaches 1, two last -value finally reaches 1, two last operations are performed.operations are performed.

Thus, the worst-case running time is 7logThus, the worst-case running time is 7lognn + 2. + 2.

Page 19: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 19

int powerOf2(const int &n)int powerOf2(const int &n){{ if (n == 0)if (n == 0) return 1;return 1; return powerOf2(n-1) + powerOf2(n-1);return powerOf2(n-1) + powerOf2(n-1);}}

int powerOf2(const int &n)int powerOf2(const int &n){{ if (n == 0)if (n == 0) return 1;return 1; return powerOf2(n-1) + powerOf2(n-1);return powerOf2(n-1) + powerOf2(n-1);}}

Recurrence Relations To Evaluate Recurrence Relations To Evaluate RecursionRecursion

Recurrence Relations To Evaluate Recurrence Relations To Evaluate RecursionRecursion

Assume that there is a function Assume that there is a function T(n)T(n) such that it takes such that it takes T(k)T(k) time to executetime to execute powerOf2(k)powerOf2(k). Examining the code allows us . Examining the code allows us

to conclude the following:to conclude the following:

T(0) = 2T(0) = 2

T(k) = 5 + 2T(k-1) T(k) = 5 + 2T(k-1) for allfor all k > k > 00

Assume that there is a function Assume that there is a function T(n)T(n) such that it takes such that it takes T(k)T(k) time to executetime to execute powerOf2(k)powerOf2(k). Examining the code allows us . Examining the code allows us

to conclude the following:to conclude the following:

T(0) = 2T(0) = 2

T(k) = 5 + 2T(k-1) T(k) = 5 + 2T(k-1) for allfor all k > k > 00The second fact tells us that:The second fact tells us that:

T(n) = 5 + 2T(n-1) = 5 + 2(5 + 2T(n-2)) =T(n) = 5 + 2T(n-1) = 5 + 2(5 + 2T(n-2)) =5 + 2(5 + 2(5 + 2(T(n-3)))) = … 5 + 2(5 + 2(5 + 2(T(n-3)))) = …

= 5(1 + 2 + 2= 5(1 + 2 + 222 + 2 + 233 + … + 2 + … + 2n-1n-1) + 2) + 2nnT(0) =T(0) =5(25(2nn-1) + 2-1) + 2nn(2) = 7(2(2) = 7(2nn) - 5,) - 5, which is O(2 which is O(2nn).).

The second fact tells us that:The second fact tells us that:T(n) = 5 + 2T(n-1) = 5 + 2(5 + 2T(n-2)) =T(n) = 5 + 2T(n-1) = 5 + 2(5 + 2T(n-2)) =

5 + 2(5 + 2(5 + 2(T(n-3)))) = … 5 + 2(5 + 2(5 + 2(T(n-3)))) = … = 5(1 + 2 + 2= 5(1 + 2 + 222 + 2 + 233 + … + 2 + … + 2n-1n-1) + 2) + 2nnT(0) =T(0) =5(25(2nn-1) + 2-1) + 2nn(2) = 7(2(2) = 7(2nn) - 5,) - 5, which is O(2 which is O(2nn).).

Page 20: CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the

CS 340 Chapter 2: Algorithm Analysis 20

int alternatePowerOf2(const int &n)int alternatePowerOf2(const int &n){{ if (n == 0)if (n == 0) return 1;return 1; return 2*alternatePowerOf2(n-1);return 2*alternatePowerOf2(n-1);}}

int alternatePowerOf2(const int &n)int alternatePowerOf2(const int &n){{ if (n == 0)if (n == 0) return 1;return 1; return 2*alternatePowerOf2(n-1);return 2*alternatePowerOf2(n-1);}}

Another Recurrence Relation Another Recurrence Relation ExampleExample

Another Recurrence Relation Another Recurrence Relation ExampleExample

The second fact tells us that:The second fact tells us that:T(n) = 4 + T(n-1) = 4 + (4 + T(n-2)) = T(n) = 4 + T(n-1) = 4 + (4 + T(n-2)) =

+ (4 + (4 + (T(n-3)))) = … + (4 + (4 + (T(n-3)))) = … = 4n + T(0) = 4n + 2= 4n + T(0) = 4n + 2, which is O(, which is O(nn).).

The second fact tells us that:The second fact tells us that:T(n) = 4 + T(n-1) = 4 + (4 + T(n-2)) = T(n) = 4 + T(n-1) = 4 + (4 + T(n-2)) =

+ (4 + (4 + (T(n-3)))) = … + (4 + (4 + (T(n-3)))) = … = 4n + T(0) = 4n + 2= 4n + T(0) = 4n + 2, which is O(, which is O(nn).).

Assume that there is a function Assume that there is a function T(n)T(n) such that it such that it takes takes T(k)T(k) time to execute time to execute alternatePowerOf2alternatePowerOf2(k)(k). .

Examining the code allows us to conclude the Examining the code allows us to conclude the following:following:

T(T(00) = ) = 22

T(k) = T(k) = 44 + T(k- + T(k-11) ) for allfor all k > k > 00

Assume that there is a function Assume that there is a function T(n)T(n) such that it such that it takes takes T(k)T(k) time to execute time to execute alternatePowerOf2alternatePowerOf2(k)(k). .

Examining the code allows us to conclude the Examining the code allows us to conclude the following:following:

T(T(00) = ) = 22

T(k) = T(k) = 44 + T(k- + T(k-11) ) for allfor all k > k > 00