# Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007

## Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007

Spann, James, Meteorologist has reference to this Academic Journal, PHwiki organized this Journal Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Analysis of algorithms Issues: correctness time efficiency space efficiency optimality Approaches: theoretical analysis empirical analysis Theoretical analysis of time efficiency Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size Basic operation: the operation that contributes the most towards the running time of the algorithm T(n) copC(n) Note: Different basic operations may cost differently!

This Particular University is Related to this Particular Journal

Input size in addition to basic operation examples Empirical analysis of time efficiency Select a specific (typical) sample of inputs Use physical unit of time (e.g., milliseconds) or Count actual number of basic operations executions Analyze the empirical data Best-case, average-case, worst-case For some algorithms, efficiency depends on as long as m of input: Worst case: Cworst(n)  maximum over inputs of size n Best case: Cbest(n)  minimum over inputs of size n Average case: Cavg(n)  average over inputs of size n Number of times the basic operation will be executed on typical input NOT the average of worst in addition to best case Expected number of basic operations considered as a r in addition to om variable under some assumption about the probability distribution of all possible inputs. So, avg = expected under uni as long as m distribution.

Example: Sequential search Worst case Best case Average case n key comparisons 1 comparisons (n+1)/2, assuming K is in A Types of as long as mulas as long as basic operations count Exact as long as mula e.g., C(n) = n(n-1)/2 Formula indicating order of growth with specific multiplicative constant e.g., C(n) 0.5 n2 Formula indicating order of growth with unknown multiplicative constant e.g., C(n) cn2 Order of growth Most important: Order of growth within a constant multiple as n Example: How much faster will algorithm run on computer that is twice as fast How much longer does it take to solve problem of double input size

Values of some important functions as n Asymptotic order of growth A way of comparing functions that ignores constant factors in addition to small input sizes (because) O(g(n)): class of functions f(n) that grow no faster than g(n) (g(n)): class of functions f(n) that grow at same rate as g(n) (g(n)): class of functions f(n) that grow at least as fast as g(n) Big-oh

Big-omega Big-theta Establishing order of growth using the definition Definition: f(n) is in O(g(n)), denoted f(n) O(g(n)), if order of growth of f(n) order of growth of g(n) (within constant multiple), i.e., there exist positive constant c in addition to non-negative integer n0 such that f(n) c g(n) as long as every n n0 Examples: 10n is in O(n2) 5n+20 is in O(n)

-notation Formal definition A function t(n) is said to be in (g(n)), denoted t(n) (g(n)), if t(n) is bounded below by some constant multiple of g(n) as long as all large n, i.e., if there exist some positive constant c in addition to some nonnegative int

Theorem If t1(n) O(g1(n)) in addition to t2(n) O(g2(n)), then t1(n) + t2(n) O(max{g1(n), g2(n)}). The analogous assertions are true as long as the -notation in addition to -notation. Implication: The algorithms overall efficiency will be determined by the part with a larger order of growth, i.e., its least efficient part. For example, 5n2 + 3nlogn O(n2) Proof. There exist constants c1, c2, n1, n2 such that t1(n) c1g1(n), as long as all n n1 t2(n) c2g2(n), as long as all n n2 Define c3 = c1 + c2 in addition to n3 = max{n1,n2}. Then t1(n) + t2(n) c3max{g1(n), g2(n)}, as long as all n n3 Some properties of asymptotic order of growth f(n) O(f(n)) f(n) O(g(n)) iff g(n) (f(n)) If f (n) O(g (n)) in addition to g(n) O(h(n)) , then f(n) O(h(n)) Note similarity with a b If f1(n) O(g1(n)) in addition to f2(n) O(g2(n)) , then f1(n) + f2(n) O(max{g1(n), g2(n)}) Also, 1in (f(i)) = (1in f(i)) Exercise: Can you prove these properties Establishing order of growth using limits lim T(n)/g(n) = Examples: 10n vs. n2 n(n+1)/2 vs. n2 n

LHôpitals rule in addition to Stirlings as long as mula LHôpitals rule: If limn f(n) = limn g(n) = in addition to the derivatives f´, g´ exist, then Stirlings as long as mula: n! (2n)1/2 (n/e)n Example: log n vs. n Example: 2n vs. n! Orders of growth of some important functions All logarithmic functions loga n belong to the same class (log n) no matter what the logarithms base a > 1 is because All polynomials of the same degree k belong to the same class: aknk + ak-1nk-1 + + a0 (nk) Exponential functions an have different orders of growth as long as different as order log n < order n (>0) < order an < order n! < order nn Basic asymptotic efficiency classes

Time efficiency of nonrecursive algorithms General Plan as long as Analysis Decide on parameter n indicating input size Identify algorithms basic operation Determine worst, average, in addition to best cases as long as input of size n Set up a sum as long as the number of times the basic operation is executed Simplify the sum using st in addition to ard as long as mulas in addition to rules (see Appendix A) Useful summation as long as mulas in addition to rules lin1 = 1+1+ +1 = n – l + 1 In particular, lin1 = n – 1 + 1 = n (n) 1in i = 1+2+ +n = n(n+1)/2 n2/2 (n2) 1in i2 = 12+22+ +n2 = n(n+1)(2n+1)/6 n3/3 (n3) 0in ai = 1 + a + + an = (an+1 – 1)/(a – 1) as long as any a 1 In particular, 0in 2i = 20 + 21 + + 2n = 2n+1 – 1 (2n ) (ai ± bi ) = ai ± bi cai = cai liuai = limai + m+1iuai Example 1: Maximum element T(n) = 1in-1 1 = n-1 = (n) comparisons

Example 2: Element uniqueness problem Example 3: Matrix multiplication Example 4: Gaussian elimination Algorithm GaussianElimination(A[0 n-1,0 n]) //Implements Gaussian elimination on an n-by-(n+1) matrix A as long as i 0 to n – 2 do as long as j i + 1 to n – 1 do as long as k i to n do A[j,k] A[j,k] – A[i,k] A[j,i] / A[i,i] Find the efficiency class in addition to a constant factor improvement. as long as i 0 to n – 2 do as long as j i + 1 to n – 1 do B A[j,i] / A[i,i] as long as k i to n do A[j,k] A[j,k]  A[i,k] B

Decrease-by-a-constant-factor recurrences  The Master Theorem T(n) = aT(n/b) + f (n), where f (n) (nk) , k>=0 a < bk T(n) (nk) a = bk T(n) (nk log n ) a > bk T(n) (nlog a) Examples: T(n) = T(n/2) + 1 T(n) = 2T(n/2) + n T(n) = 3T(n/2) + n T(n) = T(n/2) + n (nlog23) (log n) (nlog n) b (n)

## Spann, James Meteorologist

Spann, James is from United States and they belong to ABC 33/40 News at 10 PM -WJSU-TV and they are from  Birmingham, United States got related to this Particular Journal. and Spann, James deal with the subjects like Meteorology

## Journal Ratings by Institut d’Etudes Politiques de Bordeaux

This Particular Journal got reviewed and rated by Institut d’Etudes Politiques de Bordeaux and short form of this particular Institution is FR and gave this Journal an Excellent Rating.