Download presentation
Presentation is loading. Please wait.
1
CSC 380: Design and Analysis of Algorithms
Dr. Curry Guinn
2
Quick Info Dr. Curry Guinn CIS 2015 guinnc@uncw.edu
Office Hours: MTF: 10:00am-11:00m and by appointment
3
Today Determining O(n) using common code examples
Some examples of mathematical analysis Blackboard Quiz 3 due Tuesday, Jan 29, 11:59pm On Chapter 2, Appendix A, Appendix B Homework due Sunday, Feb 03
4
Program loop runtimes for (int i = 0; i < n; i += c) // O(n) statement(s); Adding to the loop counter means that the loop runtime grows linearly when compared to its maximum value n. Loop executes its body exactly n / c times. for j in range(0, n, c): // O(n) statement(s) Or if myList contains n elements for item in myList: // O(n)
5
More loop runtimes Nesting loops multiplies their runtimes.
for (int i = 0; i < n; i += c) { //O(n2) for (int j = 0; j < n; i += c) { statement; } } for j in range(0, n, c): // O(n2) for k in range(0, n, c): statement(s) Or if myList contains n elements for item in myList: // O(n2) for item in myList:
6
The loop maximum is n2, so the runtime is quadratic.
for (int i = 0; i < n * n; i += c) // O(n2) statement(s); The loop maximum is n2, so the runtime is quadratic. Loop executes its body exactly (n2 / c) times. for j in range(0, n*n, c): // O(n2) statement(s)
7
for (int i = 1; i <= n; i *= c) // O(log n)
statement(s); Multiplying the loop counter means that the maximum value n must grow exponentially to linearly increase the loop runtime; therefore, it is logarithmic. Loop executes its body exactly logc n times. j = 1 while j <= n: // O(log n) statement(s) j *= c
8
for (int i = n; i >= 1; i /= c) // O(log n)
statement(s); Multiplying the loop counter means that the maximum value n must grow exponentially to linearly increase the loop runtime; therefore, it is logarithmic. Loop executes its body exactly logc n times. while n >= 1: // O(log n) statement(s) n /= c
9
Loops in sequence add together their runtimes, which means the loop set with the larger runtime dominates. for (int i = 0; i < n; i += c) { // O(n) statement; } // O(nlog n) for (int i = 0; i < n; i += c) { for (int j = 0; j < n; i *= c) { } }
10
Types of runtime analysis
Express the running time as f(N), where N is the size of the input worst case: your enemy gets to pick the input average case: need to assume a probability distribution on the inputs However, even with input size N, cost of an algorithm could vary on different input.
11
Time efficiency of nonrecursive algorithms
General Plan for Analysis Decide on parameter n indicating input size Identify algorithm’s basic operation Determine worst, average, and best cases for input of size n Set up a sum for the number of times the basic operation is executed Simplify the sum using standard formulas and rules (see Appendix A) A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
12
Useful summation formulas and rules
liu1 = 1+1+ ⋯ +1 = u - l + 1 In particular, 1in1 = n = n (n) 1in i = 1+2+ ⋯ +n = n(n+1)/2 n2/2 (n2) 1in i2 = ⋯ +n2 = n(n+1)(2n+1)/6 n3/3 (n3) 0in ai = 1 + a + ⋯ + an = (an+1 - 1)/(a - 1) for any a 1 In particular, 0in 2i = ⋯ + 2n = 2n (2n ) (ai ± bi ) = ai ± bi cai = cai liuai = limai + m+1iuai A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
13
Example 1: Sequential search
Worst case Best case Average case A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
14
Example 2: Maximum element
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
15
Example 3: Element uniqueness problem
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
16
Example 4: Matrix multiplication
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
17
Example 5: Gaussian elimination
Algorithm GaussianElimination(A[0..n-1,0..n]) //Implements Gaussian elimination of an n-by-(n+1) matrix A for i 0 to n - 2 do for j i + 1 to n - 1 do for k i to n do A[j,k] A[j,k] - A[i,k] A[j,i] / A[i,i] Find the efficiency class and a constant factor improvement. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
18
Example 6: Counting binary digits
It cannot be investigated the way the previous examples are. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
19
For Next Class, Wednesday
Blackboard Quiz 3 due Tuesday, Jan 29, 11:59pm On Chapter 2, Appendix A, Appendix B Homework due Sunday, Feb 03
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.