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 c time to run. worst-case runtime of this method. Your model should be written 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"); } } }
Warmup Assume each println takes some constant c time to run. Warmup: construct a mathematical function modeling the 2 in terms of q , the provided input integer. worst-case runtime of this method. Your model should be written 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 ( cq 2 + 10 c ) = cq 3 + 10 cq
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” operator for functions 3
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” operator for functions 3
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” operator for functions 3
Analysis: comparing functions Question: Should we treat these two functions the same? n 4 T ( n )
Analysis: comparing functions What about now? n Intuition: our quadratic function is dominating the linear ones Intuition: our linear functions (eventually) look the same 5 T ( n )
Analysis: comparing functions What about now? n Intuition: our quadratic function is dominating the linear ones Intuition: our linear functions (eventually) look the same 5 T ( n )
Analysis: comparing functions What about now? n Intuition: our quadratic function is dominating the linear ones Intuition: our linear functions (eventually) look the same 5 T ( n )
Analysis: comparing functions Let’s zoom in... n Intuition: quadratic function eventually dominates the linear ones 6 T ( n )
Analysis: comparing functions Let’s zoom in... n Intuition: quadratic function eventually dominates the linear ones 6 T ( n )
Analysis: comparing functions Our goal: 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 ◮ We want a way to say n 2 eventually dominates n
Analysis: comparing functions Our goal: Intuition: We want a way to do this rigorously! 7 ◮ We want a way to say n 2 eventually dominates n ◮ We want a way to treat n and 4 n the same way ◮ Model made simplifying assumptions about constant factors ◮ Can usually improve constant-factor difgerences by being clever
Analysis: comparing functions Our goal: Intuition: 7 ◮ We want a way to say n 2 eventually dominates n ◮ We want a way to treat n and 4 n the same way ◮ Model made simplifying assumptions about constant factors ◮ Can usually improve constant-factor difgerences by being clever ◮ We want a way to do this rigorously!
Function comparison: exercise True False True True True or false? False True “less then or equal to” n ? 8 “less then or equal to” 5 n + 3 ? ◮ Is n ◮ Is 5 n + 3 ◮ Is 5 n + 3 “less then or equal to” 1 ? “less then or equal to” n 2 ? ◮ Is 5 n + 3 ◮ Is n 2 + 3 n + 2 “less then or equal to” n 3 ? “less then or equal to” n 2 + 3 n + 2 ? ◮ Is n 3
Function comparison: exercise True True True False True or false? 8 “less then or equal to” n ? True “less then or equal to” 5 n + 3 ? ◮ Is n ◮ Is 5 n + 3 ◮ Is 5 n + 3 “less then or equal to” 1 ? “less then or equal to” n 2 ? ◮ Is 5 n + 3 ◮ Is n 2 + 3 n + 2 “less then or equal to” n 3 ? “less then or equal to” n 2 + 3 n + 2 ?False ◮ Is n 3
Analysis: comparing functions Our goal: 9 ◮ We want a way to say n 2 eventually dominates n ◮ We want a way to treat n and 4 n the same way
Let’s formalize this... Idea 1 Does this work? Remember this? n T n Problem: incorrectly handles the quadratic function! 10 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 .
Let’s formalize this... Idea 1 Does this work? Remember this? n Problem: incorrectly handles the quadratic function! 10 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 . T ( n )
Let’s formalize this... Idea 1 Does this work? Remember this? n Problem: incorrectly handles the quadratic function! 10 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 . T ( n )
Let’s formalize this... Idea 2 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 A function f ( n ) is “less then or equal to” g ( n ) when f ( n ) ≤ g ( n ) is true for all values of n ≥ n 0 . ...where n 0 > 0 is some constant value.
Let’s formalize this... Idea 2 Does it work now? Do we? Problem: No, we don’t! 11 A function f ( n ) is “less then or equal to” g ( n ) when f ( n ) ≤ g ( n ) is true for all values of n ≥ n 0 . ...where n 0 > 0 is some constant value. We previously said we want to treat n and 4 n as being the “same”.
Let’s formalize this... Idea 2 Does it work now? Do we? Problem: No, we don’t! 11 A function f ( n ) is “less then or equal to” g ( n ) when f ( n ) ≤ g ( n ) is true for all values of n ≥ n 0 . ...where n 0 > 0 is some constant value. We previously said we want to treat n and 4 n as being the “same”.
Let’s formalize this... Idea 3 Does it work now? Yes! 12 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 ≥ n 0 . ...where n 0 > 0 is some constant value. ...where c > 0 is some constant value.
Let’s formalize this... Idea 3 Does it work now? Yes! 12 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 ≥ n 0 . ...where n 0 > 0 is some constant value. ...where c > 0 is some constant value.
Defjnition: Dominated by Formal defjnition: Dominated by ...is true. cg n f n n n n c A function f n is dominated by g n when The formal defjnition (not necessary to know this): Defjnition: Dominated by c g n is true f n n ... Such that for all values of n ... and n There exists two constants c 13 A function f ( n ) is dominated by g ( n ) when...
Defjnition: Dominated by A function f n is dominated by g n when ...is true. cg n f n n n n c Formal defjnition: Dominated by Defjnition: Dominated by The formal defjnition (not necessary to know this): c g n is true f n n ... Such that for all values of n 13 A function f ( n ) is dominated by g ( n ) when... ◮ There exists two constants c > 0 and n 0 > 0 ...
Defjnition: Dominated by c ...is true. cg n f n n n n A function f n is dominated by g n when Defjnition: Dominated by Formal defjnition: Dominated by The formal defjnition (not necessary to know this): c g n is true f n 13 A function f ( n ) is dominated by g ( n ) when... ◮ There exists two constants c > 0 and n 0 > 0 ... ◮ Such that for all values of n ≥ n 0 ...
Defjnition: Dominated by c ...is true. cg n f n n n n A function f n is dominated by g n when Defjnition: Dominated by Formal defjnition: Dominated by The formal defjnition (not necessary to know this): 13 A function f ( n ) is dominated by g ( n ) when... ◮ There exists two constants c > 0 and n 0 > 0 ... ◮ Such that for all values of n ≥ n 0 ... ◮ f ( n ) ≤ c · g ( n ) is true
Defjnition: Dominated by Defjnition: Dominated by The formal defjnition (not necessary to know this): Formal defjnition: Dominated by ...is true. 13 A function f ( n ) is dominated by g ( n ) when... ◮ There exists two constants c > 0 and n 0 > 0 ... ◮ Such that for all values of n ≥ n 0 ... ◮ f ( n ) ≤ c · g ( n ) is true A function f ( n ) is dominated by g ( n ) when ∃ ( c > 0 , n 0 > 0) . ∀ ( n ≥ n 0 ) . ( f ( n ) ≤ cg ( n ))
Exercise n cn true (when n )? One possible choice: n and c . So, since we know n So, what value of c makes n for n n and also know n cn , we conclude n n cn . n n 14 n Idea: pick c and n . (It probably works ) Better idea: show that n is dominated by an easier n function to analyze. E.g. note that: n n n n n for all n Demonstrate that 5 n 2 + 3 n + 6 is dominated by n 3 by fjnding a c and n 0 that satisfy the above defjnition.
Recommend
More recommend