Theoretical analysis of time efficiency Time efficiency is

  • Slides: 24
Download presentation
Theoretical analysis of time efficiency Time efficiency is analyzed by determining the number of

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 input size T(n) ≈ cop. C(n) running time execution time for basic operation Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Number of times basic operation is executed A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -0

Input size and basic operation examples Problem Input size measure Basic operation Searching for

Input size and basic operation examples Problem Input size measure Basic operation Searching for key in a list of n items Number of list’s items, i. e. Key comparison n Multiplication of two matrices Matrix dimensions or total number of elements Multiplication of two numbers Checking primality of a n’size = number of digits Division given integer n (in binary representation) Typical graph problem #vertices and/or edges Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Visiting a vertex or traversing an edge A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -1

Empirical analysis of time efficiency b Select a specific (typical) sample of inputs b

Empirical analysis of time efficiency b Select a specific (typical) sample of inputs b Use physical unit of time (e. g. , milliseconds) or Count actual number of basic operation’s executions b Analyze the empirical data Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -2

Best-case, average-case, worst-case For some algorithms efficiency depends on form of input: b Worst

Best-case, average-case, worst-case For some algorithms efficiency depends on form of input: b Worst case: Cworst(n) – maximum over inputs of size n b Best case: b Average case: Cavg(n) – “average” over inputs of size n • • • Cbest(n) – minimum over inputs of size n Number of times the basic operation will be executed on typical input NOT the average of worst and best case Expected number of basic operations considered as a random variable under some assumption about the probability distribution of all possible inputs Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -3

Example: Sequential search b Worst case b Best case b Average case Copyright ©

Example: Sequential search b Worst case b Best case b Average case Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -4

Types of formulas for basic operation’s count b Exact formula e. g. , C(n)

Types of formulas for basic operation’s count b Exact formula 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 b Formula indicating order of growth with unknown multiplicative constant e. g. , C(n) ≈ cn 2 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -5

Order of growth b Most important: Order of growth within a constant multiple as

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 longer does it take to solve problem of double input size? Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -6

Values of some important functions as n Copyright © 2007 Pearson Addison-Wesley. All rights

Values of some important functions as n Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -7

Asymptotic order of growth A way of comparing functions that ignores constant factors and

Asymptotic order of growth 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) Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -8

Big-oh Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the

Big-oh Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -9

Big-omega Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the

Big-omega Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -10

Big-theta Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the

Big-theta Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -11

Establishing order of growth using the definition Definition: f(n) is in O(g(n)) if order

Establishing order of growth using the definition Definition: f(n) is in 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 and non-negative integer n 0 such that f(n) ≤ c g(n) for every n ≥ n 0 Examples: b 10 n is O(n 2) b 5 n+20 is O(n) Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -12

Basic asymptotic efficiency classes 1 constant log n logarithmic n linear n log n

Basic asymptotic efficiency classes 1 constant log n logarithmic n linear n log n n-log-n n 2 quadratic n 3 cubic 2 n exponential n! factorial Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -13

Time efficiency of nonrecursive algorithms General Plan for Analysis b Decide on parameter n

Time efficiency of nonrecursive algorithms General Plan for Analysis b Decide on parameter n indicating input size b Identify algorithm’s basic operation b Determine worst, average, and best cases for input of size n b Set up a sum for the number of times the basic operation is executed b Simplify the sum using standard formulas and rules (see Appendix A) Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -14

Useful summation formulas and rules l i u 1 = 1+1+…+1 = u -

Useful summation formulas and rules l i u 1 = 1+1+…+1 = u - l + 1 In particular, l i u 1 = n - 1 + 1 = n (n) 1 i n i = 1+2+…+n = n(n+1)/2 n 2/2 (n 2) 1 i n i 2 = 12+22+…+n 2 = n(n+1)(2 n+1)/6 n 3/3 (n 3) 0 i n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a 1 In particular, 0 i n 2 i = 20 + 21 +…+ 2 n = 2 n+1 - 1 (2 n ) (ai ± bi ) = ai ± bi cai = c ai Copyright © 2007 Pearson Addison-Wesley. All rights reserved. l i uai = l i mai + m+1 i uai A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -15

Example 1: Maximum element Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin

Example 1: Maximum element Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -16

Example 2: Element uniqueness problem Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A.

Example 2: Element uniqueness problem Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -17

Example 3: Matrix multiplication Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin

Example 3: Matrix multiplication Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -18

Example 5: Counting binary digits It cannot be investigated the way the previous examples

Example 5: Counting binary digits It cannot be investigated the way the previous examples are. Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -19

Plan for Analysis of Recursive Algorithms b Decide on a parameter indicating an input’s

Plan for Analysis of Recursive Algorithms b Decide on a parameter indicating an input’s size. b Identify the algorithm’s basic operation. b Check whether the number of times the basic op. is executed may vary on different inputs of the same size. (If it may, the worst, average, and best cases must be investigated separately. ) b Set up a recurrence relation with an appropriate initial condition expressing the number of times the basic op. is executed. b Solve the recurrence (or, at the very least, establish its solution’s order of growth) by backward substitutions or another method. Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -20

Example 1: Recursive evaluation of n! Definition: n ! = 1 2 … (n-1)

Example 1: Recursive evaluation of n! Definition: n ! = 1 2 … (n-1) n for n ≥ 1 and 0! = 1 Recursive definition of n!: F(n) = F(n-1) n for n ≥ 1 and F(0) = 1 Size: Basic operation: Recurrence relation: Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -21

Solving the recurrence for M(n) = M(n-1) + 1, M(0) = 0 Copyright ©

Solving the recurrence for M(n) = M(n-1) + 1, M(0) = 0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -22

Example 3: Counting #bits Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin

Example 3: Counting #bits Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 2 nd ed. , Ch. 2 2 -23