Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace."— Presentation transcript:

1 Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace efficiency OptimalityOptimality b Approaches: Theoretical analysisTheoretical analysis Empirical analysisEmpirical analysis

2 Design and Analysis of Algorithms - Chapter 22 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 b Basic operation: the operation that contributes most towards the running time of the algorithm. T(n) ≈ c op C(n) T(n) ≈ c op C(n) running time execution time for basic operation Number of times basic operation is executed input size

3 Design and Analysis of Algorithms - Chapter 23 Input size and basic operation examples Problem Input size measure Basic operation Search for key in list of n items Number of items in list n Key comparison Multiply two matrices of floating point numbers Dimensions of matrices Floating point multiplication Compute a n n Floating point multiplication Graph problem #vertices and/or edges Visiting a vertex or traversing an edge

4 Design and Analysis of Algorithms - Chapter 24 Empirical analysis of time efficiency b Select a specific (typical) sample of inputs b Use physical unit of time (e.g., milliseconds) OR OR b Count actual number of basic operations b Analyze the empirical data

5 Design and Analysis of Algorithms - Chapter 25 Best-case, average-case, worst-case For some algorithms efficiency depends on type of input: b Worst case: W(n) – maximum over inputs of size n b Best case: B(n) – minimum over inputs of size n b Average case: A(n) – “average” over inputs of size n Number of times the basic operation will be executed on typical inputNumber of times the basic operation will be executed on typical input NOT the average of worst and best caseNOT the average of worst and best case Expected number of basic operations repetitions considered as a random variable under some assumption about the probability distribution of all possible inputs of size nExpected number of basic operations repetitions considered as a random variable under some assumption about the probability distribution of all possible inputs of size n

6 Design and Analysis of Algorithms - Chapter 26 Example: Sequential search b Problem: Given a list of n elements and a search key K, find an element equal to K, if any. b Algorithm: Scan the list and compare its successive elements with K until either a matching element is found (successful search) of the list is exhausted (unsuccessful search) b Worst case b Best case b Average case

7 Design and Analysis of Algorithms - Chapter 27 Types of formulas for basic operation count b Exact formula e.g., C(n) = n(n-1)/2 e.g., C(n) = n(n-1)/2 b Formula indicating order of growth with specific multiplicative constant e.g., C(n) ≈ 0.5 n 2 e.g., C(n) ≈ 0.5 n 2 b Formula indicating order of growth with unknown multiplicative constant e.g., C(n) ≈ cn 2 e.g., C(n) ≈ cn 2

8 Design and Analysis of Algorithms - Chapter 28 Order of growth b Most important: Order of growth within a constant multiple as n→∞ b Example: How much faster will algorithm run on computer that is twice as fast?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?How much longer does it take to solve problem of double input size? b See table 2.1

9 Design and Analysis of Algorithms - Chapter 29 Table 2.1

10 Design and Analysis of Algorithms - Chapter 210 Asymptotic growth rate b A way of comparing functions that ignores constant factors and small input sizes b O(g(n)): class of functions f(n) that grow no faster than g(n) b Θ (g(n)): class of functions f(n) that grow at same rate as g(n) b Ω(g(n)): class of functions f(n) that grow at least as fast as g(n) see figures 2.1, 2.2, 2.3

11 Design and Analysis of Algorithms - Chapter 211 Big-oh

12 Design and Analysis of Algorithms - Chapter 212 Big-omega

13 Design and Analysis of Algorithms - Chapter 213 Big-theta

14 Design and Analysis of Algorithms - Chapter 214 Establishing rate of growth: Method 1 – using limits lim n→∞ T(n)/g(n) = T(n)g(n) 0 order of growth of T(n) ___ order of growth of g(n) T(n)g(n) c>0 order of growth of T(n) ___ order of growth of g(n) T(n)g(n) ∞ order of growth of T(n) ___ order of growth of g(n) Examples: 10n vs. 2n 2 n(n+1)/2 vs. n 2 log b n vs. log c n

15 Design and Analysis of Algorithms - Chapter 215 L’Hôpital’s rule If b lim n→∞ f(n) = lim n→∞ g(n) = ∞ b The derivatives f´, g´ exist, Then f(n)f(n)g(n)g(n)f(n)f(n)g(n)g(n)lim n→∞ = f ´(n) g ´(n) lim n→∞ Example: logn vs. n Example: logn vs. n

16 Design and Analysis of Algorithms - Chapter 216 Establishing rate of growth: Method 2 – using definition b f(n) is O(g(n)) if order of growth of f(n) ≤ order of growth of g(n) (within constant multiple) b There exist positive constant c and non-negative integer n 0 such that f(n) ≤ c g(n) for every n ≥ n 0 f(n) ≤ c g(n) for every n ≥ n 0Examples: b 10n is O(2n 2 ) b 5n+20 is O(10n)

17 Design and Analysis of Algorithms - Chapter 217 Basic Asymptotic Efficiency classes 1constant log n logarithmic nlinear n log n n2n2n2n2quadratic n3n3n3n3cubic 2n2n2n2nexponential n!factorial

18 Design and Analysis of Algorithms - Chapter 218 Time efficiency of nonrecursive algorithms Steps in mathematical analysis of nonrecursive algorithms: b Decide on parameter n indicating input size b Identify algorithm’s basic operation b Determine worst, average, and best case for input of size n b Set up summation for C(n) reflecting algorithm’s loop structure b Simplify summation using standard formulas (see Appendix A)

19 Design and Analysis of Algorithms - Chapter 219 Examples: b Matrix multiplication b Selection sort b Insertion sort b Mystery Algorithm

20 Design and Analysis of Algorithms - Chapter 220 Matrix multipliacation

21 Design and Analysis of Algorithms - Chapter 221 Selection sort

22 Design and Analysis of Algorithms - Chapter 222 Insertion sort

23 Design and Analysis of Algorithms - Chapter 223 Mystery algorithm for i := 1 to n - 1 do max := i ; for j := i + 1 to n do if |A[ j, i ]| > |A[ max, i ]| then max := j ; for k := i to n + 1 do swap A[ i, k ] with A[ max, k ]; for j := i + 1 to n do for k := n + 1 downto i do A[ j, k ] := A[ j, k ] - A[ i, k ] * A[ j, i ] / A[ i, i ] ; A[ j, k ] := A[ j, k ] - A[ i, k ] * A[ j, i ] / A[ i, i ] ;

24 Design and Analysis of Algorithms - Chapter 224 Example Recursive evaluation of n ! b Definition: n ! = 1*2*…*(n-1)*n b Recursive definition of n!: b Algorithm: if n=0 then F(n) := 1 else F(n) := F(n-1) * n return F(n) b Recurrence for number of multiplications to compute n!:

25 Design and Analysis of Algorithms - Chapter 225 Time efficiency of recursive algorithms Steps in mathematical analysis of recursive algorithms: b Decide on parameter n indicating input size b Identify algorithm’s basic operation b Determine worst, average, and best case for input of size n b Set up a recurrence relation and initial condition(s) for C(n)-the number of times the basic operation will be executed for an input of size n (alternatively count recursive calls). b Solve the recurrence to obtain a closed form or estimate the order of magnitude of the solution (see Appendix B)

26 Design and Analysis of Algorithms - Chapter 226 Important recurrence types: b One (constant) operation reduces problem size by one. T(n) = T(n-1) + c T(1) = d Solution: T(n) = (n-1)c + d linear b A pass through input reduces problem size by one. T(n) = T(n-1) + cn T(1) = d Solution: T(n) = [n(n+1)/2 – 1] c + d quadratic b One (constant) operation reduces problem size by half. T(n) = T(n/2) + c T(1) = d Solution: T(n) = c lg n + d logarithmic b A pass through input reduces problem size by half. T(n) = 2T(n/2) + cn T(1) = d Solution: T(n) = cn lg n + d n n log n

27 Design and Analysis of Algorithms - Chapter 227 A general divide-and-conquer recurrence T(n) = aT(n/b) + f (n) where f (n) ∈ Θ(n k ) 1. a < b k T(n) ∈ Θ(n k ) 2. a = b k T(n) ∈ Θ(n k lg n ) 3. a > b k T(n) ∈ Θ( n log b a ) Note: the same results hold with O instead of Θ.

28 Design and Analysis of Algorithms - Chapter 228 Fibonacci numbers b The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … b Fibonacci recurrence: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 b Another example: A(n) = 3A(n-1) - 2(n-2) A(0) = 1 A(1) = 3 2nd order linear homogeneous recurrence relation with constant coefficients

29 Design and Analysis of Algorithms - Chapter 229 Solving linear homogeneous recurrence relations with constant coefficients b Easy first: 1 st order LHRRCCs: C(n) = a C(n -1) C(0) = t … Solution: C(n) = t a n b Extrapolate to 2 nd order L(n) = a L(n-1) + b L(n-2) … A solution?: L(n) = r n b Characteristic equation (quadratic) b Solve to obtain roots r 1 and r 2 —e.g.: A(n) = 3A(n-1) - 2(n-2) b General solution to RR: linear combination of r 1 n and r 2 n b Particular solution: use initial conditions—e.g.: A(0) = 1 A(1) = 3

30 Design and Analysis of Algorithms - Chapter 230 Computing Fibonacci numbers 1. Definition based recursive algorithm 2. Nonrecursive brute-force algorithm 3. Explicit formula algorithm 4. Logarithmic algorithm based on formula: F(n-1) F(n) F(n) F(n+1) 0 1 1 1 = n for n≥1, assuming an efficient way of computing matrix powers.


Download ppt "Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace."

Similar presentations


Ads by Google