Download presentation
Presentation is loading. Please wait.
Published byFrancine Warner Modified over 6 years ago
1
Time Complexity Lecture 14 Sec 10.4 Thu, Feb 22, 2007
2
“Big-O” Notation: O(f)
Let f : R R be a function. A function g : R R is “big-O” of f if there exist constants c R, n0 N such that g(n) cf(n) for all n n0. In other words, g(n) is bounded above by a constant multiple of f(n) from some point on. c R, n0 N, n N, n n0 g(n) cf(n). 2/24/2019 Time Complexity
3
Examples Show that 5n + 8 is O(n).
Show that an + b is O(n) for all a, b R. Show that n2 + 10n + 5 is O(n2). Show that n2 + 10n + 5 is not O(n). 2/24/2019 Time Complexity
4
Growth Rates If g O(f), then the growth rate of g(n) is no greater than the growth rate of f(n). If g O(f) and f O(g), then O(f) = O(g) and f(n) and g(n) have the same growth rate. 2/24/2019 Time Complexity
5
Example Show that n and 5n + 8 have the same growth rate. 2/24/2019
Time Complexity
6
Common Growth Rates Growth Rate Example O(1) Access an array element
O(log2 n) Binary search O(n) Sequential search O(n log2 n) Merge sort O(n2) Bubble sort O(2n) Factor an integer O(n!) Traveling salesman problem 2/24/2019 Time Complexity
7
Estimating Run Times Suppose the run time of a program is O(f), for a specific function f(x). Then the run time T(n) is of the form for some real number c. Suppose program runs in t0 seconds when the input size is n0. Then 2/24/2019 Time Complexity
8
Estimating Run Times Thus, 2/24/2019 Time Complexity
9
Example: Estimating Run Times
Suppose the run time of a program is O(n2). Suppose the program runs in t0 = 5 sec when the input size is n0 = 100. Then Thus, if the input size is 1000, then the run time is 2/24/2019 Time Complexity
10
Example: Estimating Run Times
Suppose the run time of a program is O(n log n). Suppose the program runs in t0 = 5 sec when the input size is n0 = 100. Then Thus, if the input size is 1000, then the run time is 2/24/2019 Time Complexity
11
Example: FactoringTimes.cpp
2/24/2019 Time Complexity
12
Comparison of Growth Rates
Consider programs with run times that are O(1), O(log n), O(n), O(n log n), and O(n2). Assume that each program runs in 1 sec when the input size is 100. Calculate the run times for input sizes 102, 103, 104, 105, 106, and 107. 2/24/2019 Time Complexity
13
Comparison of Growth Rates
O(log n) O(n) O(n log n) O(n2) 102 1 sec 103 1.5 sec 10 sec 15 sec 100 sec 104 2 sec 200 sec 10 msec 105 2.5 sec 1 msec 2.5 msec 1 sec 106 3 sec 30 msec 1.7 min 107 3.5 sec 100 msec 350 msec 2.8 hrs 2/24/2019 Time Complexity
14
Comparison of O(n2) and O(2n)
Now consider a program with growth rate O(2n). Assume that the program runs in 1 sec when the input size is 100. Calculate the run times for input sizes 100, 110, 120, 130, 140, and 150. 2/24/2019 Time Complexity
15
Comparison of O(n2) and O(2n)
100 1 sec 110 1.2 sec 1 msec 120 1.4 sec 1 sec 130 1.7 sec 18 min 140 2.0 sec 13 days 150 2.3 sec 37 yrs 2/24/2019 Time Complexity
16
Comparison of O(2n) and O(n!)
Now consider a program with growth rate O(n!). Assume that the program runs in 1 sec when the input size is 100. Calculate the run times for input sizes 100, 101, 102, 103, 104, and 105. 2/24/2019 Time Complexity
17
Comparison of O(2n) and O(n!)
100 1 sec 101 2 sec 100 sec 102 4 sec 10 msec 103 8 sec 1.1 sec 104 16 sec 1.8 min 105 32 sec 3.2 hrs 106 64 sec 14 days 107 128 sec 4.2 yrs 108 256 sec 450 yrs 2/24/2019 Time Complexity
18
Feasible vs. Infeasible
Algorithms with polynomial growth rates or less are considered feasible. Algorithms with growth rates greater than polynomial are considered infeasible. Of course, they are all feasible for small input sizes. It is feasible to factor small integers (< 50 digits). It is infeasible to factor large integers (> 200 digits). 2/24/2019 Time Complexity
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.