Download presentation
Presentation is loading. Please wait.
1
Big-O & Asymptotic Analysis
2
Big-O Big – O : Implementation independent description of algorithm run time O for Omicron Technical: Function that by a constant factor, is an upper bound for steps in particular algorithm
3
Big-O Big – O Practical def: Function that best describes how work grows in relation to problem size. Example: n is a better match for the growth of 4n - 1 than n2
4
Functions
5
Big-O Big – O Practical def: Function that best describes how work grows in relation to problem size.
6
Other bounds Big Omega : Big Theta :
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
7
Big-O Mathematical Definition
Big – O : Function that by a constant factor, is an upper bound for steps in particular algorithm 𝑓 𝑛 𝑖𝑠 𝑂 𝑔 𝑛 if and only if There 𝑒𝑥𝑖𝑠𝑡 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡𝑠 𝑐 𝑎𝑛𝑑 𝑘 𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡 𝑓 𝑛 ≤𝑐∙𝑔 𝑛 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥𝑘 f(n) is work done by your code g(n) is the function category you want to call it
8
Algebra of Big-O Show: 4n − 1 is O(n) Must pick c and k so that:
Pick k = 0 and c = 5 𝑓 𝑛 ≤𝑐∙𝑔 𝑛 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥𝑘 or 4n − 1 ≤𝑐∙𝑛 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥𝑘 4𝑛−1≤5𝑛 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥0
9
Algebra of Big-O 4n − 1 is O(n) 4𝑛−1≤5𝑛 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥0
10
Important Idea 1 Constants don’t matter in Big-O Expression Category
Can treat all constants as 1 Expression Category 5n n : Linear 3n2 n2 : Quadratic n2/20 log2(n) + 6 log2(n) : Logarithmic 10 1 : Constant
11
Dominant Term Largest term drives growth curve n n2 + 20n n2 1 21 10
300 100 12,000 10,000 1000 1,020,000 1,000,000 10000 100,200,000 100,000,000
12
Example Show: 2n2 + 100n is O(n2) Must pick c and k so that:
Pick k = 100 and c = 5 𝑓 𝑛 ≤𝑐∙𝑔 𝑛 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥𝑘 or 2n n ≤𝑐∙𝑛2 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥𝑘 2n n≤5𝑛2𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥100
13
Example 2n n is O(n2) 2n n≤5𝑛2𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥100
14
Important Idea 2 For Big-O just report largest term
Examples: 8n2 + 3n O(8n2) O(n2) 3log2n + 4n O(4n) O(n) n O(3n) O(n)
15
BigO Limitations Asymptotic Analysis : focus on behavior for large values
16
BigO Limitations BigO focuses on categories at scale
Results may not hold for small problem sizes
17
BigO Limitations BigO focuses on categories at scale
Other terms and constants may matter for algorithms in the same category
18
Cases Algorithm may have different Big-O for
Best case Worst case Average case When in doubt, we describe the worst case
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.