29
CSE 373: Asymptotic Analysis Michael Lee Monday Jan 8, 2017 1

CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

CSE 373: Asymptotic Analysis

Michael LeeMonday Jan 8, 2017

1

Page 2: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Warmup

WarmupRemind your neighbor: what fields did our array list iterator need?

2

Page 3: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Comparing algorithms

Goal: compare algorithms

What are we comparing? Lots of metrics we could pick!

I Time needed to runI Memory usedI Number of network calls madeI Amount of data we save to the diskI etc...

(Some metrics are intangible: clarity, security... Hard to measure those.)

Today: focus on comparing algorithms based on how long it takesthem to run in the worst case.

3

Page 4: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Comparing algorithms

Goal: compare algorithmsWhat are we comparing? Lots of metrics we could pick!

I Time needed to runI Memory usedI Number of network calls madeI Amount of data we save to the diskI etc...

(Some metrics are intangible: clarity, security... Hard to measure those.)

Today: focus on comparing algorithms based on how long it takesthem to run in the worst case.

3

Page 5: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Comparing algorithms

Goal: compare algorithmsWhat are we comparing? Lots of metrics we could pick!

I Time needed to runI Memory usedI Number of network calls madeI Amount of data we save to the diskI etc...

(Some metrics are intangible: clarity, security... Hard to measure those.)

Today: focus on comparing algorithms based on how long it takesthem to run in the worst case.

3

Page 6: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Comparing algorithms

Goal: compare algorithmsWhat are we comparing? Lots of metrics we could pick!

I Time needed to runI Memory usedI Number of network calls madeI Amount of data we save to the diskI etc...

(Some metrics are intangible: clarity, security... Hard to measure those.)

Today: focus on comparing algorithms based on how long it takesthem to run in the worst case.

3

Page 7: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

An idea: let’s time our algorithms!

Goal: find the number of primes below n

Time taken for n = 18000

Algorithm Time (in ms)

Algo 1 0.0018Algo 2 35.58Algo 3 100.75

Which algorithm is better?

This is a trick question. Why isn’t this table enough to let usdecide which algorithm is better?

4

Page 8: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

An idea: let’s time our algorithms!

Goal: find the number of primes below n

Time taken for n = 18000

Algorithm Time (in ms)

Algo 1 0.0018Algo 2 35.58Algo 3 100.75

Which algorithm is better?

This is a trick question. Why isn’t this table enough to let usdecide which algorithm is better?

4

Page 9: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

An idea: let’s time our algorithms!

Goal: find the number of primes below n

Time taken for n = 18000

Algorithm Time (in ms)

Algo 1 0.0018Algo 2 35.58Algo 3 100.75

Which algorithm is better?

Which algorithm is better?

This is a trick question. Why isn’t this table enough to let usdecide which algorithm is better?

4

Page 10: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

An idea: let’s time our algorithms!

Goal: find the number of primes below n

Time taken for n = 18000

Algorithm Time (in ms)

Algo 1 0.0018Algo 2 35.58Algo 3 100.75

Which algorithm is better?

This is a trick question. Why isn’t this table enough to let usdecide which algorithm is better?

4

Page 11: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

An idea: let’s time our algorithms!

Which algorithm is better?

5

Page 12: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Our goal

What we want:

I To see overall trends as input increases(considering a single data point isn’t useful)

I Final result is independent of incidental factors(CPU speed, other programs that may be running, battery life,programming language, coding tricks...)

I Rigorously discover overall trends without resorting to testing(what if we miss worst-case input? best-case input?)

I A way to analyze before coding!

6

Page 13: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Our goal

What we want:

I To see overall trends as input increases(considering a single data point isn’t useful)

I Final result is independent of incidental factors(CPU speed, other programs that may be running, battery life,programming language, coding tricks...)

I Rigorously discover overall trends without resorting to testing(what if we miss worst-case input? best-case input?)

I A way to analyze before coding!

6

Page 14: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Our goal

What we want:

I To see overall trends as input increases(considering a single data point isn’t useful)

I Final result is independent of incidental factors(CPU speed, other programs that may be running, battery life,programming language, coding tricks...)

I Rigorously discover overall trends without resorting to testing(what if we miss worst-case input? best-case input?)

I A way to analyze before coding!

6

Page 15: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Our goal

What we want:

I To see overall trends as input increases(considering a single data point isn’t useful)

I Final result is independent of incidental factors(CPU speed, other programs that may be running, battery life,programming language, coding tricks...)

I Rigorously discover overall trends without resorting to testing(what if we miss worst-case input? best-case input?)

I A way to analyze before coding!

6

Page 16: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Our process

Two step process:

1. Model what we care about as a mathematical function2. Analyze that function using asymptotic analysis

7

Page 17: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Modeling: Assumptions

Assumption: basic operations take “constant” time

I Arithmetic (for fixed-width numbers)I Variable assignmentI Accessing a field or array indexI Printing something outI etc...

Warning: These assumptions are over-simplifications.

But they’re very useful approximations!

8

Page 18: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Modeling: Assumptions

Assumption: basic operations take “constant” time

I Arithmetic (for fixed-width numbers)I Variable assignmentI Accessing a field or array indexI Printing something outI etc...

Warning: These assumptions are over-simplifications.

But they’re very useful approximations!

8

Page 19: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Modeling: Complex statements

I Consecutive statementsSum of time of each statement

I Function callsTime of function’s body

I ConditionalsTime of condition + max(if branch, else branch)

I LoopsNumber of iterations × time for loop body

9

Page 20: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Modeling: Complex statements

I Consecutive statementsSum of time of each statement

I Function callsTime of function’s body

I ConditionalsTime of condition + max(if branch, else branch)

I LoopsNumber of iterations × time for loop body

9

Page 21: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Modeling: Complex statements

I Consecutive statementsSum of time of each statement

I Function callsTime of function’s body

I ConditionalsTime of condition + max(if branch, else branch)

I LoopsNumber of iterations × time for loop body

9

Page 22: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Modeling: Complex statements

I Consecutive statementsSum of time of each statement

I Function callsTime of function’s body

I ConditionalsTime of condition + max(if branch, else branch)

I LoopsNumber of iterations × time for loop body

9

Page 23: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Modeling: exercise

Goal: return ’true’ if a sorted array of ints contains duplicates

Algorithm 1: compare each pair of elementspublic boolean hasDuplicate1(int[] array) {

for (int i = 0; i < array.length; i++)

for (int j = 0; j < array.length; j++)

if (i != j && array[i] == array[j])

return true;

return false;

}

Algorithm 2: compare each consecutive pairs of elementspublic boolean hasDuplicate2(int[] array) {

for (int i = 0; i < array.length - 1; i++)

if (array[i] == array[i + 1])

return true;

return false;

}

Exercise: create a mathematical function modeling the amountof time taken in the worst case

10

Page 24: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Modeling: exercise

Goal: return ’true’ if a sorted array of ints contains duplicates

Algorithm 1: compare each pair of elementspublic boolean hasDuplicate1(int[] array) {

for (int i = 0; i < array.length; i++)

for (int j = 0; j < array.length; j++)

if (i != j && array[i] == array[j])

return true;

return false;

}

Algorithm 2: compare each consecutive pairs of elementspublic boolean hasDuplicate2(int[] array) {

for (int i = 0; i < array.length - 1; i++)

if (array[i] == array[i + 1])

return true;

return false;

}

Exercise: create a mathematical function modeling the amountof time taken in the worst case

10

Page 25: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Modeling: exercise

Goal: return ’true’ if a sorted array of ints contains duplicates

Algorithm 1: compare each pair of elementspublic boolean hasDuplicate1(int[] array) {

for (int i = 0; i < array.length; i++)

for (int j = 0; j < array.length; j++)

if (i != j && array[i] == array[j])

return true;

return false;

}

Algorithm 2: compare each consecutive pairs of elementspublic boolean hasDuplicate2(int[] array) {

for (int i = 0; i < array.length - 1; i++)

if (array[i] == array[i + 1])

return true;

return false;

}

Exercise: create a mathematical function modeling the amountof time taken in the worst case

10

Page 26: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Modeling: exercise

Goal: return ’true’ if a sorted array of ints contains duplicates

Algorithm 1: compare each pair of elementspublic boolean hasDuplicate1(int[] array) {

for (int i = 0; i < array.length; i++)

for (int j = 0; j < array.length; j++)

if (i != j && array[i] == array[j])

return true;

return false;

}

Algorithm 2: compare each consecutive pairs of elementspublic boolean hasDuplicate2(int[] array) {

for (int i = 0; i < array.length - 1; i++)

if (array[i] == array[i + 1])

return true;

return false;

}

Exercise: create a mathematical function modeling the amountof time taken in the worst case 10

Page 27: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Our process

Two step process:

1. Model what we care about as a mathematical function2. Analyze that function using asymptotic analysis

Specifically: have a way to compare two functions

11

Page 28: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Our process

Two step process:

1. Model what we care about as a mathematical function2. Analyze that function using asymptotic analysis

Specifically: have a way to compare two functions

11

Page 29: CSE 373: Asymptotic Analysis · 2018. 3. 11. · Algo1 0.0018 Algo2 35.58 Algo3 100.75 Whichalgorithmisbetter? Thisisatrickquestion.Whyisn’tthistableenoughtoletus decidewhichalgorithmisbetter?

Next time

Next time: how do we compare functions?

12