PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10...

36
CMU 15-251 Time complexity Teachers: Anil Ada Ariel Procaccia (this time)

Transcript of PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10...

Page 1: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

CMU 15-251

Time complexity

Teachers:

Anil Ada

Ariel Procaccia (this time)

Page 2: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Course overview

2

Page 3: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

The big O

3

Page 4: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Adding two ๐‘›-bit numbers

4

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*+

Page 5: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Adding two ๐‘›-bit numbers

5

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

+

Page 6: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Adding two ๐‘›-bit numbers

6

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

+

Page 7: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

Adding two ๐‘›-bit numbers

7

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

+

Page 8: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Time complexity

โ€ข ๐‘‡ ๐‘› =๐‘›

โ€ข

โ€ข

8

Page 9: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

A great idea

โ€ข

โ€ข ๐‘€ ๐‘๐‘€

โ€ข ๐‘›๐‘€ ๐‘ โ‹… ๐‘›

โ€ข ๐‘€โ€ฒ ๐‘โ€ฒ

โ€ข ๐‘€โ€ฒ ๐‘โ€ฒ๐‘›

9

Page 10: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

A great idea

โ€ข

โ€ข

10

๐‘›

Page 11: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

A great idea

โ€ข

11

๐‘›

Page 12: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Multiplying two ๐‘›-bit numbers

12

ร—

๐‘›2

* * * * * * * ** * * * * * * *

* * * * * * * ** * * * * * * *

* * * * * * * ** * * * * * * *

* * * * * * * ** * * * * * * *

* * * * * * * ** * * * * * * *

* * * * * * * ** * * * * * * *

Page 13: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Linear vs. quadratic

โ€ข ๐‘n2

โ€ข

โ€ข

13

1

10๐‘›2

10๐‘›

0 2000

4000

100

1000

2000

3000

Page 14: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Nursery school addition

โ€ข ๐‘› ๐‘Ž ๐‘ ๐‘Ž๐‘

โ€ข ๐‘‡ ๐‘›

โ€ข ๐‘ = 00โ‹ฏ0

โ€ข ๐‘ = 11โ‹ฏ1

1. ๐‘ log ๐‘› 2

2. ๐‘๐‘› log ๐‘›

3. ๐‘๐‘›2

4. ๐‘๐‘›2๐‘›

14

Page 15: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Worst case time

15

๐‘‡(๐‘›) ๐ด =

๐‘ฅ๐‘›

๐ด ๐‘ฅ

Page 16: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Kindergarten multiplication

โ€ข ๐‘› ๐‘Ž ๐‘๐‘Ž ๐‘Ž ๐‘ โˆ’ 1

โ€ข ๐‘‡ ๐‘› = ๐‘๐‘›2๐‘›

โ€ข

โ€ข

16

Page 17: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

More formally: big ๐‘‚โ€ข ๐‘“: โ„• โ†’ โ„•๐‘“(๐‘›) = ๐‘‚ ๐‘›

๐‘

๐‘› ๐‘“ ๐‘› โ‰ค ๐‘๐‘›

โ€ข

๐‘“

17

๐‘›

Page 18: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

More formally: ฮฉ

โ€ข ๐‘“: โ„• โ†’ โ„•๐‘“(๐‘›) = ฮฉ ๐‘›

๐‘

๐‘› ๐‘“ ๐‘› โ‰ฅ ๐‘๐‘›

โ€ข

๐‘“

18

๐‘›

Page 19: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

More formally: ฮ˜

โ€ข ๐‘“: โ„• โ†’โ„• ๐‘“ ๐‘› = ฮ˜ ๐‘›๐‘“ ๐‘› = ๐‘‚(๐‘›)๐‘“ ๐‘› = ฮฉ(๐‘›)

โ€ข ๐‘“

19

๐‘›

Page 20: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

More formally and generally

โ€ข ๐‘“ ๐‘› = ๐‘‚ ๐‘” ๐‘› ๐‘

๐‘›๐‘“ ๐‘› โ‰ค ๐‘ โ‹… ๐‘”(๐‘›)

โ€ข ๐‘“ ๐‘› = ฮฉ ๐‘” ๐‘› ๐‘

๐‘›๐‘“ ๐‘› โ‰ฅ ๐‘ โ‹… ๐‘”(๐‘›)

โ€ข ๐‘“ ๐‘› = ฮ˜(๐‘›) ๐‘“ ๐‘› = ๐‘‚ ๐‘” ๐‘› ๐‘“ ๐‘› =

ฮฉ ๐‘” ๐‘›

20

Page 21: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Exercises

โ€ข ๐‘›4 + 3๐‘› + 22 = O(๐‘›4)

โ€ข ๐‘›4 + 3๐‘› + 22 = ฮฉ(๐‘›4 log ๐‘›)

โ€ข

1. ln ๐‘› = ๐‘‚ log ๐‘›

2. ln ๐‘› = ฮฉ log ๐‘›

21

Page 22: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Exercises

โ€ข log ๐‘›! = ?

1. ฮ˜ ๐‘›

2. ฮ˜(๐‘› log ๐‘›)

3. ฮ˜ ๐‘›2

4. ฮ˜ 2n

โ€ข

1. ๐‘“ = ๐‘‚(๐‘”) ๐‘” = ๐‘‚ โ„Ž โ‡’ ๐‘“ = ๐‘‚(โ„Ž)

2. ๐‘“ = ๐‘‚(โ„Ž) ๐‘” = ๐‘‚ โ„Ž โ‡’ ๐‘“ = ๐‘‚(๐‘”)

22

Page 23: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Names for growth rates

โ€ข ๐‘‡ ๐‘› = ๐‘‚ ๐‘›

โ€ข ๐‘‡ ๐‘› = ๐‘‚ ๐‘›2

โ€ข ๐‘˜ โˆˆ โ„•

๐‘‡ ๐‘› = ๐‘‚ ๐‘›๐‘˜

o 13๐‘›28 + 11๐‘›17 + 2

23

Page 24: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Names of growth rates

โ€ข ๐‘˜ โˆˆ โ„•๐‘‡ ๐‘› = ๐‘‚ ๐‘˜๐‘›

o ๐‘‡ ๐‘› = ๐‘›2๐‘› = ๐‘‚(3๐‘›)

โ€ข ๐‘‡ ๐‘› = ๐‘‚(log ๐‘›)

o

o

24

Page 25: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Limits of the possible

25

104 108

108

1016

๐‘›

๐‘›2

๐‘›32๐‘›

Page 26: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Two similar problems

โ€ข

o

o

o

โ€ข

o ๐‘›!

o 2๐‘›

o 1.657๐‘›

26

Page 27: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Two similar problems

โ€ข

o

o

o

โ€ข

๐‘‚ ๐‘›2

โ€ข

27

Page 28: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Polynomial time

28

Page 29: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

29

Dragon Age: Inquisition

Page 30: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Representation

โ€ข

โ€ข

o ๐‘› 1,โ€ฆ , ๐‘› ๐‘ฃ1, โ€ฆ , ๐‘ฃ๐‘›๐‘ค1, โ€ฆ , ๐‘ค๐‘› ๐ต ๐‘‰

o

o ๐‘† ๐‘–โˆˆ๐‘†๐‘ค๐‘– โ‰ค ๐ต ๐‘–โˆˆ๐‘† ๐‘ฃ๐‘– โ‰ฅ ๐‘‰

30

Page 31: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Representation*

โ€ข

o ๐‘› ร— ๐ต ๐ด

o ๐ด(๐‘–, ๐‘—) = max{๐ด ๐‘– โˆ’ 1, ๐‘— , ๐ด ๐‘– โˆ’ 1, ๐‘— โˆ’ ๐‘ค๐‘– + ๐‘ฃ๐‘–}

31

1 4 52 3 23 5 2

๐ต = 5

1 2 3 4 5123

0 0 0 0 40 3 3 3 40 5 5 8 8

Page 32: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Representation

โ€ข

๐‘‚(๐‘›๐ต)

โ€ข

o 2๐‘› โ‹… max{log ๐ต, log ๐‘‰}

o

โ€ข

o 2๐‘› โ‹… max{๐ต, ๐‘‰}

o

32

Page 33: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Cool growth rates: 2stack

โ€ข 2STACK 0 = 1

โ€ข 2STACK ๐‘› = 2

โ€ข

o 2STACK 1 = 2

o 2STACK 2 = 4

o 2STACK 3 = 16

o 2STACK 4 = 65536

o 2STACK 5 = yikes!

33

2222222

2

2STACK(๐‘› โˆ’ 1)2

Page 34: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Cool growth rates: logโˆ—

โ€ข logโˆ— ๐‘› =๐‘› โ‰ค 1

โ€ข

o 2STACK 1 = 2 logโˆ— 2 = 1

o 2STACK 2 = 4 logโˆ— 4 = 2

o 2STACK 3 = 16 logโˆ— 16 = 3

o 2STACK 4 = 65536 logโˆ— 65536 = 4

o 2STACK 5 = yikes! logโˆ— yikes! = 5

34

Page 35: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

Cool growth rates: logโˆ—

โ€ข logโˆ—

โ€ข ๐‘‚(๐‘› log ๐‘› 2logโˆ— ๐‘›)

35

Page 36: PowerPoint Presentationarielpro/15251f15/slides/lec07.pdfย ยท Linear vs. quadratic โ€ข n2 13 1 10 ๐‘›2 10๐‘› 0 200 0 4000 100 1000 2000 3000

What we have learned

โ€ข

o

o

โ€ข

o

o

36