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)

Upload: others

Post on 23-Sep-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

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