cs240 - james madison university · 2020-01-22 · cs240 nathan sprague january 22, 2020. big- /...
TRANSCRIPT
![Page 1: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/1.jpg)
CS240
Nathan Sprague
January 22, 2020
![Page 2: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/2.jpg)
Big-Θ / Order Notation
Informal description: Growth functions are categorizedaccording to their dominant (fastest growing) term
Constants and lower-order terms are discarded
Examples:
10n ∈ Θ(n)5n2 + 2n + 3 ∈ Θ(n2)nlogn + n ∈ Θ(nlogn)
We could read this as “10n is order n”
![Page 3: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/3.jpg)
Why Drop the Constants?
Example...
![Page 4: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/4.jpg)
Why Drop the Constants?
Despite constants, functions from slower growing classeswill always be faster eventually
![Page 5: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/5.jpg)
Why Drop Lower Order Terms
Contribution of lower-order terms becomes insignificantas input size increases
This difference looks important:
It looks less important now.
![Page 6: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/6.jpg)
Why Drop Lower Order Terms
Contribution of lower-order terms becomes insignificantas input size increases
This difference looks important:
It looks less important now.
![Page 7: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/7.jpg)
Are we SURE we want to drop the constants?
For two growth functions in the same complexity class,constant factors continue to have an impact, regardless ofinput size...
![Page 8: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/8.jpg)
Why Drop the Constants? (Again?)
Real goal is to understand the relative impact ofincreasing input size
Equivalently: allow us to predict the impact of using afaster computer
Constant factors are influenced by all the distractions wementioned before:
Choice of basic operationProgramming language...
That said... We DO care about constant factors.
![Page 9: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/9.jpg)
Why Drop the Constants? (Again?)
Real goal is to understand the relative impact ofincreasing input size
Equivalently: allow us to predict the impact of using afaster computer
Constant factors are influenced by all the distractions wementioned before:
Choice of basic operationProgramming language...
That said... We DO care about constant factors.
![Page 10: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/10.jpg)
Formal Definition of Big-O
Big O
For T (n) a non-negative function, T (n) ∈ O(f (n)) if and onlyif there exist positive constants c and n0 such that
T (n) ≤ cf (n) for all n > n0.
![Page 11: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/11.jpg)
Formal Definition of Big-O
Big O
For T (n) a non-negative function, T (n) ∈ O(f (n)) if and onlyif there exist positive constants c and n0 such that
T (n) ≤ cf (n) for all n > n0.
Informal rule of “dropping constants” followsimmediately:
50n?∈ O(n)
Yes! choose c = 50, n0 = 1, clearly50n ≤ 50n for all n > 1
![Page 12: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/12.jpg)
Formal Definition of Big-O
Big O
For T (n) a non-negative function, T (n) ∈ O(f (n)) if and onlyif there exist positive constants c and n0 such that
T (n) ≤ cf (n) for all n > n0.
Informal rule of “dropping constants” followsimmediately:
50n?∈ O(n)
Yes! choose c = 50, n0 = 1, clearly50n ≤ 50n for all n > 1
![Page 13: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/13.jpg)
Formal Definition of Big-O
Big O
For T (n) a non-negative function, T (n) ∈ O(f (n)) if and onlyif there exist positive constants c and n0 such that
T (n) ≤ cf (n) for all n > n0.
Informal rule of “dropping lower-order terms” alsofollows:
n2 + 40n?∈ O(n2)
Notice that:n2 + 40n ≤ n2 + 40n2 = 41n2
Choose c = 41, n0 = 1, clearlyn2 + 40n ≤ 41n2 for all n > 1
![Page 14: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/14.jpg)
Formal Definition of Big-O
Big O
For T (n) a non-negative function, T (n) ∈ O(f (n)) if and onlyif there exist positive constants c and n0 such that
T (n) ≤ cf (n) for all n > n0.
Informal rule of “dropping lower-order terms” alsofollows:
n2 + 40n?∈ O(n2)
Notice that:n2 + 40n ≤ n2 + 40n2 = 41n2
Choose c = 41, n0 = 1, clearlyn2 + 40n ≤ 41n2 for all n > 1
![Page 15: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/15.jpg)
Formal Definition of Big-O
Big O
For T (n) a non-negative function, T (n) ∈ O(f (n)) if and onlyif there exist positive constants c and n0 such that
T (n) ≤ cf (n) for all n > n0.
Informal rule of “dropping lower-order terms” alsofollows:
n2 + 40n?∈ O(n2)
Notice that:n2 + 40n ≤ n2 + 40n2 = 41n2
Choose c = 41, n0 = 1, clearlyn2 + 40n ≤ 41n2 for all n > 1
![Page 16: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/16.jpg)
Big O Describes an Upper Bound
Big O is loosely analogous to ≤All of these statements are true:n2 ∈ O(n2)n2 ∈ O(n4)n2 ∈ O(n!)...2n2 ∈ O(n2)
![Page 17: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/17.jpg)
Upper Bounds
Big-O descriptions are imprecise in two different ways:No constants or lower-order terms
GOOD: fewer distractions
Only provides an upper bound. Correct to say analgorithm requires O(n3) steps, even if it only requires nsteps.
UNFORTUNATE: conveys an incomplete analysis
![Page 18: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/18.jpg)
Upper Bounds
Big-O descriptions are imprecise in two different ways:No constants or lower-order terms
GOOD: fewer distractions
Only provides an upper bound. Correct to say analgorithm requires O(n3) steps, even if it only requires nsteps.
UNFORTUNATE: conveys an incomplete analysis
![Page 19: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/19.jpg)
Socrative Quiz!
Alyce is working on the analysis of a complex algorithm forfinding sequence matches in a DNA database. She can easilyshow that the algorithm requires no more than n2 + nbase-pair comparisons in the worst case. She hopes to showthat the algorithm requires at most n log n + n comparisons.How should Alyce describe the running time of the algorithmgiven the current state of her analysis?A) O(n3)B) O(n2 + n)C) O(n2)D) O(n log n + n)E) O(n)
![Page 20: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/20.jpg)
Big Omega
Big Ω
For T (n) a non-negative function, T (n) ∈ Ω(f (n)) if and onlyif there exist positive constants c and n0 such that
T (n) ≥ cf (n) for all n > n0.
Big Ω is loosely analogous to ≥All of these statements are true:n2 ∈ Ω(n2)n4 ∈ Ω(n2)n! ∈ Ω(n2)...n2 ∈ Ω(2n2)
![Page 21: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/21.jpg)
Big Theta
Big Θ
f (n) ∈ θ(g(n)) iff,
f (n) ∈ O(g(n)) and f (n) ∈ Ω(g(n))
Big Θ is loosely analogous to =
Which of these statements are true?
n2?∈ Θ(n2)
2n2?∈ Θ(n2)
n2?∈ Θ(n4)
5n2 + 2n?∈ Θ(4n3)
![Page 22: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/22.jpg)
Big Theta
Big Θ
f (n) ∈ θ(g(n)) iff,
f (n) ∈ O(g(n)) and f (n) ∈ Ω(g(n))
Big Θ is loosely analogous to =
Which of these statements are true?n2 ∈ Θ(n2)2n2 ∈ Θ(n2)n2 6∈ Θ(n4)5n2 + 2n 6∈ Θ(4n3)
![Page 23: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/23.jpg)
Socrative Quiz
What relationship(s) is(are) illustrated by the following figure?
A) f (n) ∈ O(g(n))B) f (n) ∈ Ω(g(n))C) f (n) ∈ Θ(g(n))D) g(n) ∈ O(f (n))E) g(n) ∈ Ω(f (n))F) g(n) ∈ Θ(f (n))G) A, B and C are all correctH) D, E and F are all correct
![Page 24: CS240 - James Madison University · 2020-01-22 · CS240 Nathan Sprague January 22, 2020. Big- / Order Notation Informal description: Growth functions are categorized according to](https://reader030.vdocuments.us/reader030/viewer/2022040323/5e67d0ae7bf39a6a43705e6f/html5/thumbnails/24.jpg)
Algorithm Analysis Algorithm
STEP 1: Select a measure of input size and a basicoperation
STEP 2: Develop a function T (n) that describes thenumber of times the basic operation occurs as a functionof input size
STEP 3: Describe T (n) using order notation
Big-O for an upper bound“The algorithm is at least this fast!”
Big-Ω for a lower bound“The algorithm is at least this slow!”
Big-Θ for both upper and lower bound