CSE 373: Asymptotic Analysis Michael Lee Wednesday, Jan 10, 2018 1 - - PowerPoint PPT Presentation

cse 373 asymptotic analysis
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

CSE 373: Asymptotic Analysis

Michael Lee Wednesday, Jan 10, 2018

1

slide-2
SLIDE 2

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 cq c cq cq

2

slide-3
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
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
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
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
SLIDE 7

Analysis: comparing functions

Question: Should we treat these two functions the same? n T(n)

4

slide-8
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
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
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
SLIDE 11

Analysis: comparing functions

Let’s zoom in... n T(n) Intuition: quadratic function eventually dominates the linear ones

6

slide-12
SLIDE 12

Analysis: comparing functions

Let’s zoom in... n T(n) Intuition: quadratic function eventually dominates the linear ones

6

slide-13
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
SLIDE 58

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 n0 and applying the correct defjnition

22