Big-O
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
Big-O
Algebra of Big-O Constants do not matter – Show: 4n is O(n) Pick k = 100 and c = 5
Comparison Which algorithm is faster?
Comparison Which algorithm is faster? First will always be faster, but both are O(n)
Functions Different functions define "levels"
Asymptotic Analysis Which is bigger?
Asymptotic Analysis Asymptotic Analysis : focus on behavior for large values – Where k is sufficiently large… – Results may not hold for small problems
Functions
Dominant Term Largest term drives growth curve nn nn2n ,00010, ,020,0001,000, ,200,000100,000,000
Dominant Term Largest term drives growth curve – For Big-O just report largest term
Cases Complexity may differ in – Best case – Worst case – Average case When in doubt, assume worst
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
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
Loops Loops driving factor of Big-O – Pattern of loop counter tells size LoopPatternSize for(int i = 0; i < n; i++) … n – 1n for(int i = 0; i <= n; i++) … n – 1 nn + 1 = n for(int i = 0; i < n; i += 2) … n – 1n / 2 = n for(int i = 1; i < 2*n; i++) … 2n2n = n for(int i = 1; i < n; i *= 2) …logn
Logs Log patterns – Loop counter multiplied … for(int i = 1; i < n; i = i * 2) – Remaining work divided at each step …counter = 100 while(counter > 0) { counter /= 2; }
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)
Nested Loops Nested loops get multiplied: LoopPatternSize for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) i = 0 j = … n – 1 i = 1 j = … n – 1 … i = n-1 j = … 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 = i = 3 j = … i = n – 1 j = … n – 1 n * n/2 n 2 /2 n 2
Increasing Work Last example