Download presentation
Presentation is loading. Please wait.
Published byWilliam Hill Modified over 8 years ago
1
Big-O
2
Speed as function Function relating input size to execution time – f(n) = steps where n = length of array f(n) = 4(n-1) + 3 = 4n – 1
3
Big-O
4
Algebra of Big-O Constants do not matter – Show: 4n + 100 is O(n) Pick k = 100 and c = 5
5
Comparison Which algorithm is faster?
6
Comparison Which algorithm is faster? First will always be faster, but both are O(n)
7
Functions Different functions define "levels"
8
Asymptotic Analysis Which is bigger?
9
Asymptotic Analysis Asymptotic Analysis : focus on behavior for large values – Where k is sufficiently large… – Results may not hold for small problems
10
Functions
11
Dominant Term Largest term drives growth curve nn 2 + 20nn2n2 1211 10300100 12,00010,000 10001,020,0001,000,000 10000100,200,000100,000,000
12
Dominant Term Largest term drives growth curve – For Big-O just report largest term
13
Cases Complexity may differ in – Best case – Worst case – Average case When in doubt, assume worst
14
Other bounds Big Omega : – function that is a lower bound by a constant factor Big Theta : – function that can be either an upper bound or lower bound with right constant – Often mean Big-Theta when say Big-O
15
Big O Tips How many times does most common line get executed? – Normal line of code : 1 – Loop : function * contents of loop Standard counting loop = n – Function : based on contents Dominant term
16
Loops Loops driving factor of Big-O – Pattern of loop counter tells size LoopPatternSize for(int i = 0; i < n; i++)0 1 2 3 4 5 … n – 1n for(int i = 0; i <= n; i++)0 1 2 3 4 5 … n – 1 nn + 1 = n for(int i = 0; i < n; i += 2)0 2 4 6 … n – 1n / 2 = n for(int i = 1; i < 2*n; i++)1 2 3 4 5 … 2n2n = n for(int i = 1; i < n; i *= 2)1 2 4 8 16 …logn
17
Logs Log patterns – Loop counter multiplied 1 2 4 8 16 32 64… for(int i = 1; i < n; i = i * 2) – Remaining work divided at each step 100 50 25 12…counter = 100 while(counter > 0) { counter /= 2; }
18
Log Fun Logs of differing base differ only by constant factor: Base often unspecified in Big O – Generally 2 nlog 2 (n)log 10 (n) 103.3219281 1006.6438562 10009.9657843 1000013.287714
19
Nested Loops Nested loops get multiplied: LoopPatternSize for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) i = 0 j = 0 1 2 3 4 5 … n – 1 i = 1 j = 0 1 2 3 4 5 … n – 1 … i = n-1 j = 0 1 2 3 4 5 … n – 1 n * n n 2 for(int i = 0; i < n; i++) for(int j = 0; j <= i; j++) i = 0 j = 0 i = 1 j = 0 1 i = 2 j = 0 1 2 i = 3 j = 0 1 2 3 … i = n – 1 j = 0 1 2 3 4 5 … n – 1 n * n/2 n 2 /2 n 2
20
Increasing Work Last example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.