Lecture 2-Analysis Framework - Efficiency Notation
Lecture 2-Analysis Framework - Efficiency Notation
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 2
Approaches:
T(n) ≈ copC(n)
• theoretical analysis
• empirical analysis running time Number of times
execution time
for basic operation basic operation is
executed
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 3 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 4
Example: Sequential Search Example: Sequential Search
cost of basic
operation
running time count of basic
operation
• What does this function do?
• We’d like to analyze its running time: T(n) ≈ copC(n)
• What are the valid inputs?
• What is the “input size” for this algorithm? n
• What is returned?
• What is the “basic operation”? key comparison
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 5 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 6
• What is the “input size” for this algorithm? n • What is the “input size” for this algorithm? n
• What is the “basic operation”? comparison • What is the “basic operation”? comparison
– Why not assignment?
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 7 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 8
Example: Insertion Sort Example: Matrix Multiplication
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 9 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 10
• What is the “input size” for this algorithm? n • What is the “input size” for this algorithm? n
• What is the “basic operation”? multiplication • What is the “basic operation”? comparison
– Why not addition? – or the operations inside the while loop
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 11 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 12
Example: Closest Pair of Points Example: String Matching
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 13 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 14
• If the key is not in the list, we have to compare the key to every item
in the list, so:
Ckey-not-in-list(n) = n
• If the key is in the list, let’s assume that it’s equally likely to be in any
Worst-case count: Cworst(n) = n position, so:
Best-case count: Cbest(n) = 1 Ckey-is-in-list(n) = (1 + 2 + 3 + ⋯ + i + ⋯ + n) / n
Average-case count: Cave(n) = ? Why? If the key is in the ith position, we have to make i comparisons.
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 17 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 18
p n (n+1)
Average-case running time:
C ave (n)= ⋅ +(1− p)⋅n • Tave(n) ≈ copCave(n) ≈ cop˙ n/2 (if the key is definitely in the list)
n 2
– running time for some “typical input”
p( n+1)
C ave (n)= +(1− p) n – not the average of the worst and best cases
2
– based on the “expected” number of basic operations
• Does this make sense?
– we had to make some assumptions
• How many key comparisons if the key is not in the list?
• About how many key comparisons, on average, if the key is
– probability of a successful search
definitely in the list? – probability of the location of the key if it’s in the list
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 19 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 20
How does the running time change … ? How does the running time change … ?
Consider the running time for some algorithm: Consider the worst-case running time for sequential search:
T (n)≈c op C (n) T (n)≈c op C worst (n)=c op⋅n
If we run the algorithm on a computer that’s 2x as fast, If input doubles to 2n, how does the running time change?
how does the running time change? T (2 n) c op C (2 n) c op⋅2 n
c op ≈ = =2
⋅C (n) T (n) c op C (n) c op⋅n
T fast (n) 2 1
T (n)
≈ =
c op⋅C (n) 2 If input doubles again, to 4n, how does the running time change?
If we run the algorithm on a computer that’s 4x as fast, T (4 n) c op C (4 n) c op⋅4 n
≈ = =4
how does the running time change? T (n) c op C (n) c op⋅n
c op
⋅C (n) If input doubles again, to 8n, how does the running time change?
T faster (n) 4 1
≈ = T (8 n) c op C (8 n) c op⋅8 n
T (n) c op⋅C (n) 4 ≈ = =8
T (n) c op C (n) c op⋅n
What happened to C(n)? Why is that significant?
What happened to c op? Why is that significant?
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 21 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 22
Suppose the running time of an algorithm is: We are primarily concerned about the running time of
T (n)≈c op C (n)=c op⋅n 2 algorithms.
If input doubles to 2n, how does the running time change? • To compare different algorithms that solve the same
T (2 n) c op C (2 n) c op⋅(2 n)
2 problem.
≈ = =4
T (n) c op C (n) c op⋅n 2 • To determine how the running time increases with the
If input doubles again, to 4n, how does the running time change? size of the problem.
T (4 n) c op C (4 n) c op⋅(4 n)
2
For some algorithms, the analysis may depend on the
≈ = =16
T (n) c op C (n) c op⋅n
2
input itself:
If input doubles again, to 8n, how does the running time change? • Usually interested in worst-case running time.
T (8 n) c op C (8 n) c op⋅(8 n)
2 • Sometimes interested in average-case running time
≈ = =64 (quicksort).
T (n) c op C (n) c op⋅n2
How does this algorithm compare with sequential search?
• Sometimes interested in best-case running time
(insertion sort).
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 23
Running Time Analysis Asymptotic Notation
Not concerned about:
• the execution time on a particular machine
• a specific count of operations
Better:
• compare the asymptotic running time as a function of
the input size
• the asymptotic rate of growth
• f(n) = Θ(g(n)): f(n) is asymptotically “=” g(n)
– g(n) is an asymptotically tight bound for f(n)
• f(n) = Ο(g(n)): f(n) is asymptotically “≤” g(n)
– g(n) is an asymptotic upper bound for f(n)
• f(n) = Ω(g(n)): f(n) is asymptotically “≥” g(n)
– g(n) is an asymptotic lower bound for f(n)
Image and ref: Cormen et al., Introduction to Algorithms, 3 rd Edition, page 45.
1 constant
log n logarithmic
n linear
n log n “n log n”
n2 quadratic
n3 cubic
2n exponential
n! factorial
Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Based on: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012
Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 27 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. (Modified by L. Markowsky) 28
For next time ...