Download presentation
Presentation is loading. Please wait.
Published byAnn Cox Modified over 8 years ago
1
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation
2
Objectives for Today Big-O notation Reading - K+W Chap 4.1-4.5, 2.6
3
Big-O notation Describes the relationship between input size and execution time If we double the number of inputs, n, and the execution time is approximately doubled –Linear growth rate –Growth rate has an order of n –O(n)
4
Example 2.14/2.16 Analysis 2.14 –execution time proportional to x_length –O(n) 2.16 –execution time proportional to (x_length) 2 –O(n 2 )
5
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } Simple Statement 1 Simple Statement 2... Simple Statement 25
6
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } n 2 executions
7
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } n executions, 5 statements per = 5n
8
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } Simple Statement 1 Simple Statement 2... Simple Statement 25 1 execution, 25 statements = 25
9
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } Simple Statement 1 Simple Statement 2... Simple Statement 25 T(n) = total execution time as a function of n T(n) = n 2 + 5n + 25
10
Formal Big-O Definition T(n) = n 2 + 5n + 25 T(n) = O(f(n)) means that there exists a function, f(n), that for sufficiently large n and some constant c: cf(n) T(n)
11
n 2 + 25n + 25 vs. 3n 2
12
Common Big-O Runtimes Constant -- O(1) Logarithmic -- O(log n) Fractional -- O(sqrt n) Linear -- O(n) Log-Linear-- O(n log n) Quadratic -- O(n 2 ) Cubic -- O(n 3 ) Exponential -- O(2 n ) Factorial -- O(n!)
13
Various Runtimes
14
Powers of n (Order of the polynomial)
15
Multiplicative Constants
16
Multiplicative Constants (cont’)
17
Dominant Terms (1)
18
Dominant Terms (2)
19
Dominant Terms (3)
20
Dominant Terms Comparison
21
Dominant Terms Comparison (cont’)
22
Class Exercise Implement last on our Linked List
23
Group Exercise Implement last in O(1)
25
The big-O notation can be derived from T(n) using the following steps: 1) Set the coefficient of each term to 1 2) Keep the largest (least efficient) term in the function and discard the others.
26
Standard Measures of Efficiency To get a feel for how much of a difference an algorithm’s efficiency makes, check out the table on the next slide. The table assumes an instruction speed of 1 microsecond, 10 instructions per loop, and n=10,000
27
Standard Measures of Efficiency Efficiency Big-O IterationsEstimated Time Logarithmic O(log 2 n)14Microseconds Linear O(n)10,0000.1 seconds Linear Logarithmic O(nlog 2 n)140,0002 seconds Quadratic O(n 2 )10,000 2 ~17 minutes Polynomial O(n k )10,000 k Hours Exponential O(c n )2 10,000 Intractable Factorial O(n!)10,000!Intractable
28
Standard Measures of Efficiency
29
Calculating Big-O 1.T(n) = 6n 4 − 2n 3 + 5 2.T(n) = 9 log n + 5 (log n) 3 + 3n 2 + 2n 3 3.T(n) = 3n 2 + O(log n) + O(n) + O(n log n)
30
Calculating Big-O 4.Binary Search on an array: T(n) = T(n/2) + O(1) 5.T(1) = 2 T(n) = 2T(n - 1) + O(1) for n>1 6.T(1) = 1 T(n) = 2T(n/2) + O(1) for n>1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.