Presentation is loading. Please wait.

Presentation is loading. Please wait.

Time Complexity Lecture 15 Mon, Feb 27, 2006.

Similar presentations


Presentation on theme: "Time Complexity Lecture 15 Mon, Feb 27, 2006."— Presentation transcript:

1 Time Complexity Lecture 15 Mon, Feb 27, 2006

2 Topics Big-O notation Growth rates Estimating run times Feasibility
4/18/2019 Time Complexity

3 “Big-O” Notation: O(f)
Let f : R  R be a function. O(f) is the set of functions g : R  R such that there exist constants c  R, n0  N such that g(n)  cf(n) for all n  n0. In other words, g(n) is dominated by a constant multiple of f(n) from some point on. 4/18/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. Define the growth rate of f to be (f) = {g | O(g) = O(f)}. 4/18/2019 Time Complexity

5 Common Growth Rates Growth Rate Example (1) Access an array element
(log2 n) Binary search (n) Sequential search (n log2 n) Merge sort (n2) Bubble sort (2n) Factor an integer (n!) Traveling salesman problem 4/18/2019 Time Complexity

6 Estimating Run Times Suppose program has growth rate (f), for a specific function f(x). Then the run time of the program is of the form run time = c f(n) for some real number c. Suppose program runs in t0 seconds when the input size is n0. Then 4/18/2019 Time Complexity

7 Example: Estimating Run Times
Suppose a program has growth rate (n2). Suppose the program runs in t0 = 5 sec when the input size is n0 = 100. Then the run time is Thus, if the input size is 1000, then the run time is 4/18/2019 Time Complexity

8 Example: Estimating Run Times
Suppose a program has growth rate (n log n). Suppose the program runs in t0 = 5 sec when the input size is n0 = 100. Then the run time is Thus, if the input size is 1000, then the run time is 4/18/2019 Time Complexity

9 Example: FactoringTimes.cpp
4/18/2019 Time Complexity

10 Comparison of Growth Rates
Consider programs with growth rates of (1), (log n), (n), (n log n), and (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. 4/18/2019 Time Complexity

11 Comparison of Growth Rates
(1) (log n) (n) (n log n) (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 4/18/2019 Time Complexity

12 Comparison of (n2) and (2n)
Now consider a program with growth rate (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. 4/18/2019 Time Complexity

13 Comparison of (n2) and (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 4/18/2019 Time Complexity

14 Comparison of (2n) and (n!)
Now consider a program with growth rate (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. 4/18/2019 Time Complexity

15 Comparison of (2n) and (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 4/18/2019 Time Complexity

16 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). 4/18/2019 Time Complexity


Download ppt "Time Complexity Lecture 15 Mon, Feb 27, 2006."

Similar presentations


Ads by Google