SLIDE 1
CSE 373: Asymptotic Analysis Michael Lee Wednesday, Jan 10, 2018 1 - - PowerPoint PPT Presentation
CSE 373: Asymptotic Analysis Michael Lee Wednesday, Jan 10, 2018 1 - - PowerPoint PPT Presentation
CSE 373: Asymptotic Analysis Michael Lee Wednesday, Jan 10, 2018 1 Warmup Warmup: construct a mathematical function modeling the cq cq c q cq Answer: T q 2 in terms of q , the provided input integer. Assume each println takes some constant
SLIDE 2
SLIDE 3
Warmup
Warmup: construct a mathematical function modeling the worst-case runtime of this method. Your model should be written in terms of q, the provided input integer. Assume each println takes some constant c time to run.
public void mystery(int q) { for (int i = 0; i < q; i++) { for (int j = 0; j < q * q; j++) { System.out.println("Hello"); } for (int j = 0; j < 10; j++) { System.out.println("World"); } } }
Answer: T(q) = q(cq2 + 10c) = cq3 + 10cq
2
SLIDE 4
Last time
Two step process:
- 1. Model what we care about as a mathematical function
- 2. Analyze that function using asymptotic analysis
Specifjcally: have a way to compare two functions Even more specifjcally: defjne a “less then or equal to”
- perator for functions
3
SLIDE 5
Last time
Two step process:
- 1. Model what we care about as a mathematical function
- 2. Analyze that function using asymptotic analysis
Specifjcally: have a way to compare two functions Even more specifjcally: defjne a “less then or equal to”
- perator for functions
3
SLIDE 6
Last time
Two step process:
- 1. Model what we care about as a mathematical function
- 2. Analyze that function using asymptotic analysis
Specifjcally: have a way to compare two functions Even more specifjcally: defjne a “less then or equal to”
- perator for functions
3
SLIDE 7
Analysis: comparing functions
Question: Should we treat these two functions the same? n T(n)
4
SLIDE 8
Analysis: comparing functions
What about now? n T(n) Intuition: our quadratic function is dominating the linear ones Intuition: our linear functions (eventually) look the same
5
SLIDE 9
Analysis: comparing functions
What about now? n T(n) Intuition: our quadratic function is dominating the linear ones Intuition: our linear functions (eventually) look the same
5
SLIDE 10
Analysis: comparing functions
What about now? n T(n) Intuition: our quadratic function is dominating the linear ones Intuition: our linear functions (eventually) look the same
5
SLIDE 11
Analysis: comparing functions
Let’s zoom in... n T(n) Intuition: quadratic function eventually dominates the linear ones
6
SLIDE 12
Analysis: comparing functions
Let’s zoom in... n T(n) Intuition: quadratic function eventually dominates the linear ones
6
SLIDE 13
Analysis: comparing functions
Our goal: ◮ We want a way to say n2 eventually dominates n We want a way to treat n and n the same way
Intuition: Model made simplifying assumptions about constant factors Can usually improve constant-factor difgerences by being clever
We want a way to do this rigorously!
7
SLIDE 14
Analysis: comparing functions
Our goal: ◮ We want a way to say n2 eventually dominates n ◮ We want a way to treat n and 4n the same way
Intuition: ◮ Model made simplifying assumptions about constant factors ◮ Can usually improve constant-factor difgerences by being clever
We want a way to do this rigorously!
7
SLIDE 15
Analysis: comparing functions
Our goal: ◮ We want a way to say n2 eventually dominates n ◮ We want a way to treat n and 4n the same way
Intuition: ◮ Model made simplifying assumptions about constant factors ◮ Can usually improve constant-factor difgerences by being clever
◮ We want a way to do this rigorously!
7
SLIDE 16
Function comparison: exercise
True or false? ◮ Is n “less then or equal to” 5n + 3? True ◮ Is 5n + 3 “less then or equal to” n? True ◮ Is 5n + 3 “less then or equal to” 1? False ◮ Is 5n + 3 “less then or equal to” n2? True ◮ Is n2 + 3n + 2 “less then or equal to” n3? True ◮ Is n3 “less then or equal to” n2 + 3n + 2? False
8
SLIDE 17
Function comparison: exercise
True or false? ◮ Is n “less then or equal to” 5n + 3? True ◮ Is 5n + 3 “less then or equal to” n? True ◮ Is 5n + 3 “less then or equal to” 1? False ◮ Is 5n + 3 “less then or equal to” n2? True ◮ Is n2 + 3n + 2 “less then or equal to” n3? True ◮ Is n3 “less then or equal to” n2 + 3n + 2?False
8
SLIDE 18
Analysis: comparing functions
Our goal: ◮ We want a way to say n2 eventually dominates n ◮ We want a way to treat n and 4n the same way
9
SLIDE 19
Let’s formalize this...
Idea 1 A function f (n) is “less then or equal to” g(n) when f (n) ≤ g(n) is true for all values of n ≥ 0. Does this work? Remember this? n T n Problem: incorrectly handles the quadratic function!
10
SLIDE 20
Let’s formalize this...
Idea 1 A function f (n) is “less then or equal to” g(n) when f (n) ≤ g(n) is true for all values of n ≥ 0. Does this work? Remember this? n T(n) Problem: incorrectly handles the quadratic function!
10
SLIDE 21
Let’s formalize this...
Idea 1 A function f (n) is “less then or equal to” g(n) when f (n) ≤ g(n) is true for all values of n ≥ 0. Does this work? Remember this? n T(n) Problem: incorrectly handles the quadratic function!
10
SLIDE 22
Let’s formalize this...
Idea 2 A function f (n) is “less then or equal to” g(n) when f (n) ≤ g(n) is true for all values of n ≥ n0. ...where n0 > 0 is some constant value. Does it work now? We previously said we want to treat n and n as being the “same”. Do we? Problem: No, we don’t!
11
SLIDE 23
Let’s formalize this...
Idea 2 A function f (n) is “less then or equal to” g(n) when f (n) ≤ g(n) is true for all values of n ≥ n0. ...where n0 > 0 is some constant value. Does it work now? We previously said we want to treat n and 4n as being the “same”. Do we? Problem: No, we don’t!
11
SLIDE 24
Let’s formalize this...
Idea 2 A function f (n) is “less then or equal to” g(n) when f (n) ≤ g(n) is true for all values of n ≥ n0. ...where n0 > 0 is some constant value. Does it work now? We previously said we want to treat n and 4n as being the “same”. Do we? Problem: No, we don’t!
11
SLIDE 25
Let’s formalize this...
Idea 3 A function f (n) is “less then or equal to” g(n) when f (n) ≤ c · g(n) is true for all values of n ≥ n0. ...where n0 > 0 is some constant value. ...where c > 0 is some constant value. Does it work now? Yes!
12
SLIDE 26
Let’s formalize this...
Idea 3 A function f (n) is “less then or equal to” g(n) when f (n) ≤ c · g(n) is true for all values of n ≥ n0. ...where n0 > 0 is some constant value. ...where c > 0 is some constant value. Does it work now? Yes!
12
SLIDE 27
Defjnition: Dominated by
Defjnition: Dominated by A function f (n) is dominated by g(n) when... There exists two constants c and n ... Such that for all values of n n ... f n c g n is true The formal defjnition (not necessary to know this): Formal defjnition: Dominated by A function f n is dominated by g n when c n n n f n cg n ...is true.
13
SLIDE 28
Defjnition: Dominated by
Defjnition: Dominated by A function f (n) is dominated by g(n) when... ◮ There exists two constants c > 0 and n0 > 0... Such that for all values of n n ... f n c g n is true The formal defjnition (not necessary to know this): Formal defjnition: Dominated by A function f n is dominated by g n when c n n n f n cg n ...is true.
13
SLIDE 29
Defjnition: Dominated by
Defjnition: Dominated by A function f (n) is dominated by g(n) when... ◮ There exists two constants c > 0 and n0 > 0... ◮ Such that for all values of n ≥ n0... f n c g n is true The formal defjnition (not necessary to know this): Formal defjnition: Dominated by A function f n is dominated by g n when c n n n f n cg n ...is true.
13
SLIDE 30
Defjnition: Dominated by
Defjnition: Dominated by A function f (n) is dominated by g(n) when... ◮ There exists two constants c > 0 and n0 > 0... ◮ Such that for all values of n ≥ n0... ◮ f (n) ≤ c · g(n) is true The formal defjnition (not necessary to know this): Formal defjnition: Dominated by A function f n is dominated by g n when c n n n f n cg n ...is true.
13
SLIDE 31
Defjnition: Dominated by
Defjnition: Dominated by A function f (n) is dominated by g(n) when... ◮ There exists two constants c > 0 and n0 > 0... ◮ Such that for all values of n ≥ n0... ◮ f (n) ≤ c · g(n) is true The formal defjnition (not necessary to know this): Formal defjnition: Dominated by A function f (n) is dominated by g(n) when ∃(c > 0, n0 > 0). ∀(n ≥ n0). (f (n) ≤ cg(n)) ...is true.
13
SLIDE 32
Exercise
Demonstrate that 5n2 + 3n + 6 is dominated by n3 by fjnding a c and n0 that satisfy the above defjnition. Idea: pick c and n . (It probably works
)
Better idea: show that n n is dominated by an easier function to analyze. E.g. note that: n n n n n for all n n n So, what value of c makes n cn true (when n )? One possible choice: n and c . So, since we know n n n for n n and also know n cn , we conclude n n cn .
14
SLIDE 33
Exercise
Demonstrate that 5n2 + 3n + 6 is dominated by n3 by fjnding a c and n0 that satisfy the above defjnition. Idea: pick c = 10000 and n0 = 10000. (It probably works
)
Better idea: show that n n is dominated by an easier function to analyze. E.g. note that: n n n n n for all n n n So, what value of c makes n cn true (when n )? One possible choice: n and c . So, since we know n n n for n n and also know n cn , we conclude n n cn .
14
SLIDE 34
Exercise
Demonstrate that 5n2 + 3n + 6 is dominated by n3 by fjnding a c and n0 that satisfy the above defjnition. Idea: pick c = 10000 and n0 = 10000. (It probably works
)
Better idea: show that 5n2 + 3n + 6 is dominated by an easier function to analyze. E.g. note that: 5n2 + 3n + 6 ≤ 5n2 + 3n2 + 6n2 for all n ≥ 1 = 14n2 ≤ 14n3 So, what value of c makes n cn true (when n )? One possible choice: n and c . So, since we know n n n for n n and also know n cn , we conclude n n cn .
14
SLIDE 35
Exercise
Demonstrate that 5n2 + 3n + 6 is dominated by n3 by fjnding a c and n0 that satisfy the above defjnition. Idea: pick c = 10000 and n0 = 10000. (It probably works
)
Better idea: show that 5n2 + 3n + 6 is dominated by an easier function to analyze. E.g. note that: 5n2 + 3n + 6 ≤ 5n2 + 3n2 + 6n2 for all n ≥ 1 = 14n2 ≤ 14n3 So, what value of c makes 14n3 ≤ cn3 true (when n ≥ 1)? One possible choice: n0 = 1 and c = 14. So, since we know n n n for n n and also know n cn , we conclude n n cn .
14
SLIDE 36
Exercise
Demonstrate that 5n2 + 3n + 6 is dominated by n3 by fjnding a c and n0 that satisfy the above defjnition. Idea: pick c = 10000 and n0 = 10000. (It probably works
)
Better idea: show that 5n2 + 3n + 6 is dominated by an easier function to analyze. E.g. note that: 5n2 + 3n + 6 ≤ 5n2 + 3n2 + 6n2 for all n ≥ 1 = 14n2 ≤ 14n3 So, what value of c makes 14n3 ≤ cn3 true (when n ≥ 1)? One possible choice: n0 = 1 and c = 14. So, since we know 5n2 + 3n + 6 ≤ 14n3 for n ≥ n0 and also know 14n3 ≤ cn3, we conclude 5n2 + 3n + 6 ≤ cn3.
14
SLIDE 37
Exercise
Demonstrate that 2n3 − 3 + 9n2 + √n is dominated by n3 (again by fjnding a c and n0). Do the same thing. Note that: n n n n n n for all n n n n n So, one possible choice of n and c is n and c .
15
SLIDE 38
Exercise
Demonstrate that 2n3 − 3 + 9n2 + √n is dominated by n3 (again by fjnding a c and n0). Do the same thing. Note that: 2n3 − 3 + 9n2 + √n ≤ 2n3 + 9n2 + n for all n ≥ 1 ≤ 2n3 + 9n3 + n3 = 12n3 So, one possible choice of n0 and c is n0 = 1 and c = 12.
15
SLIDE 39
Families of functions
Observation: ◮ n, 5n + 3, 100n, etc... all dominate each other ◮ These three functions are the “same” Idea: can we give a name to this “family” of functions? Defjnition: Big- f n is the “family” or “set” of all functions that are dominated by f n Question: are n , n , and n all the same thing? Yes! By convention, we pick the “simplest” way of writing this and refer to this “family” as n .
16
SLIDE 40
Families of functions
Observation: ◮ n, 5n + 3, 100n, etc... all dominate each other ◮ These three functions are the “same” Idea: can we give a name to this “family” of functions? Defjnition: Big- f n is the “family” or “set” of all functions that are dominated by f n Question: are n , n , and n all the same thing? Yes! By convention, we pick the “simplest” way of writing this and refer to this “family” as n .
16
SLIDE 41
Families of functions
Observation: ◮ n, 5n + 3, 100n, etc... all dominate each other ◮ These three functions are the “same” Idea: can we give a name to this “family” of functions? Defjnition: Big-O O (f (n)) is the “family” or “set” of all functions that are dominated by f (n) Question: are n , n , and n all the same thing? Yes! By convention, we pick the “simplest” way of writing this and refer to this “family” as n .
16
SLIDE 42
Families of functions
Observation: ◮ n, 5n + 3, 100n, etc... all dominate each other ◮ These three functions are the “same” Idea: can we give a name to this “family” of functions? Defjnition: Big-O O (f (n)) is the “family” or “set” of all functions that are dominated by f (n) Question: are O (n), O (5n + 3), and O (100n) all the same thing? Yes! By convention, we pick the “simplest” way of writing this and refer to this “family” as n .
16
SLIDE 43
Families of functions
Observation: ◮ n, 5n + 3, 100n, etc... all dominate each other ◮ These three functions are the “same” Idea: can we give a name to this “family” of functions? Defjnition: Big-O O (f (n)) is the “family” or “set” of all functions that are dominated by f (n) Question: are O (n), O (5n + 3), and O (100n) all the same thing? Yes! By convention, we pick the “simplest” way of writing this and refer to this “family” as O (n).
16
SLIDE 44
Families of functions
A question: Do the following two sentences mean the same thing? ◮ f (n) is dominated by g(n) ◮ f (n) is contained inside O (g(n)) Yes! We can write this more concisely as f n g n . An aside: some people write this as f n g n This is wrong (but common, so we reluctantly accept this)
17
SLIDE 45
Families of functions
A question: Do the following two sentences mean the same thing? ◮ f (n) is dominated by g(n) ◮ f (n) is contained inside O (g(n)) Yes! We can write this more concisely as f (n) ∈ O (g(n)). An aside: some people write this as f n g n This is wrong (but common, so we reluctantly accept this)
17
SLIDE 46
Families of functions
A question: Do the following two sentences mean the same thing? ◮ f (n) is dominated by g(n) ◮ f (n) is contained inside O (g(n)) Yes! We can write this more concisely as f (n) ∈ O (g(n)). An aside: some people write this as f (n) = O (g(n)) This is wrong (but common, so we reluctantly accept this)
17
SLIDE 47
A few more questions
True or false: ◮ 5n + 3 ∈ O (n) True ◮ n ∈ O (5n + 3) True ◮ 5n + 3 = O (n) True (by convention) ◮ O (5n + 3) = O (n) True ◮ O
- n2
= O (n) False ◮ n2 ∈ O (1) False ◮ n2 ∈ O (n) False ◮ n2 ∈ O
- n2
True ◮ n2 ∈ O
- n3
True ◮ n2 ∈ O
- n100
True
18
SLIDE 48
A few more questions
True or false: ◮ 5n + 3 ∈ O (n) True ◮ n ∈ O (5n + 3) True ◮ 5n + 3 = O (n) True (by convention) ◮ O (5n + 3) = O (n) True ◮ O
- n2
= O (n) False ◮ n2 ∈ O (1) False ◮ n2 ∈ O (n) False ◮ n2 ∈ O
- n2
True ◮ n2 ∈ O
- n3
True ◮ n2 ∈ O
- n100
True
18
SLIDE 49
Defjnitions: Dominates
f (n) ∈ O (g(n)) is like saying “f (n) is less then or equal to g(n)”. Is there a way to say “greater then or equal to”? Yes! Defjnition: Dominates We say f n dominates g n when: There exists two constants c and n ... Such that for all values of n n ... f n c g n is true Defjnition: Big- f n is the family of all functions that dominates f n .
19
SLIDE 50
Defjnitions: Dominates
f (n) ∈ O (g(n)) is like saying “f (n) is less then or equal to g(n)”. Is there a way to say “greater then or equal to”? Yes! Defjnition: Dominates We say f (n) dominates g(n) when: ◮ There exists two constants c > 0 and n0 > 0... ◮ Such that for all values of n ≥ n0... ◮ f (n) ≥ c · g(n) is true Defjnition: Big- f n is the family of all functions that dominates f n .
19
SLIDE 51
Defjnitions: Dominates
f (n) ∈ O (g(n)) is like saying “f (n) is less then or equal to g(n)”. Is there a way to say “greater then or equal to”? Yes! Defjnition: Dominates We say f (n) dominates g(n) when: ◮ There exists two constants c > 0 and n0 > 0... ◮ Such that for all values of n ≥ n0... ◮ f (n) ≥ c · g(n) is true Defjnition: Big-Ω Ω (f (n)) is the family of all functions that dominates f (n).
19
SLIDE 52
A few more questions...
True or false? ◮ 4n2 ∈ Ω (1) True ◮ 4n2 ∈ Ω (n) True ◮ 4n2 ∈ Ω
- n2
True ◮ 4n2 ∈ Ω
- n3
False ◮ 4n2 ∈ Ω
- n4
False ◮ 4n2 ∈ O (1) False ◮ 4n2 ∈ O (n) False ◮ 4n2 ∈ O
- n2
True ◮ 4n2 ∈ O
- n3
True ◮ 4n2 ∈ O
- n4
True
20
SLIDE 53
A few more questions...
True or false? ◮ 4n2 ∈ Ω (1) True ◮ 4n2 ∈ Ω (n) True ◮ 4n2 ∈ Ω
- n2
True ◮ 4n2 ∈ Ω
- n3
False ◮ 4n2 ∈ Ω
- n4
False ◮ 4n2 ∈ O (1) False ◮ 4n2 ∈ O (n) False ◮ 4n2 ∈ O
- n2
True ◮ 4n2 ∈ O
- n3
True ◮ 4n2 ∈ O
- n4
True
20
SLIDE 54
Defjnition: Big-Θ
Defjnition: Big-Θ We say f (n) ∈ Θ (g(n)) when both: ◮ f (n) ∈ O (g(n)) and... ◮ f (n) ∈ Ω (g(n)) ...are true. Note: in industry, it’s common for many people to ask for the big- when they really want the big- !
21
SLIDE 55
Defjnition: Big-Θ
Defjnition: Big-Θ We say f (n) ∈ Θ (g(n)) when both: ◮ f (n) ∈ O (g(n)) and... ◮ f (n) ∈ Ω (g(n)) ...are true. Note: in industry, it’s common for many people to ask for the big-O when they really want the big-Θ!
21
SLIDE 56
Takeaways
Important things to know: ◮ Intuition behind the defjnitions of “dominated by” and big-O The precise defjnitions of:
“Dominated by” and big- “Dominates” and big- Big-
How to demonstrate that one function is dominated by another by fjnding c and n and applying the correct defjnition
22
SLIDE 57
Takeaways
Important things to know: ◮ Intuition behind the defjnitions of “dominated by” and big-O ◮ The precise defjnitions of:
◮ “Dominated by” and big-O ◮ “Dominates” and big-Ω ◮ Big-Θ
How to demonstrate that one function is dominated by another by fjnding c and n and applying the correct defjnition
22
SLIDE 58