Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234...

51
Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design Jiraporn Pooksook Naresuan University

Transcript of Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234...

Page 1: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Ch7: Asymptotic Notations

305233, 305234

Algorithm Analysis and Design

Jiraporn PooksookNaresuan University

Page 2: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

What is asymptotic notation?

• We are concerned with how the running time of an algorithm increases with the size of the input in the limit, as the size of the input increases without bound.

• We use it to describe running time of an algorithm which are defined in terms of functions whose domain are N=0,1,2,3,…are N=0,1,2,3,…

• Five asymptotic notations:– Big theta

– Big O

– Big Omega

– Little O

– Little omega

Page 3: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Examples of the notationsΩΘ ,,O

Page 4: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

- Notations

• f(n): there exist positive

constants c1, c2, and n0 such that

0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0

• A function f(n) belongs to the set if there exist

Θ

=Θ ))(( ng

• A function f(n) belongs to the set if there exist

positive constants c1 and c2 such that it can be

“sandwiched” between c1 g(n) and c2 g(n), for

sufficiently large n.

We write to express ))(()( ngnf Θ= ))(()( ngnf Θ∈

Page 5: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ เราตองทาการหาคา c1 และ c2 และ n0 ท ทาให

สมการตอไปน #เปนจรง

Example: - NotationsΘ

nnnf 32

1)( 2

−=2)( nng =

))(()( ngnf Θ∈

Θสมการตอไปน #เปนจรง

• 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ ≤ ≤

• หารสมการท #งหมดดวย จะได

2

1nc nn 3

2

1 2−

2

2nc

2n 21

3

2

10 c

nc ≤−≤≤

ถา n > 6 เม อไหร จะทาให 3/n ลเขาส 0 ซ งจะทาให คาของ ½-3/n เกอบคงท ท ½ ดงน #นถาเราเลอกคา n ท >= 7

เราจะตองเลอกคา c1 <= 1/14

ถา n >=1 แต n < 6 จะทาใหคาของ ½-3/n ตดลบเรากตองเลอกคา c2 >= ½ (เพ อท จะไดทาใหสมการเปนบวก) พอ n=6 กจะได c2>=0 และเม อ n >=7 จะทา

ให 3/n ว งเขาส 0 ดงน #นคา c2 จะ >= ½

Page 6: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• เราไดวา ถา n = 6, c1 จะมคา <= 0 และถา n>=7, c1<=1/2

• ไดวา ถา n>=1 ถง n < 6, คา ½-3/n ตดลบ และถา n=6, c2 >=0 และเม อ n>=7, c2>=1/2

Example: - NotationsΘ

nnnf 32

1)( 2

−=2)( nng =

และเม อ n>=7, c2>=1/2

• คาถามคอ ถาเชนน #น เราทาไมไมเลอก c1 ท ½ และ n>=6 ลองมาแทนคา สมการกนด

• พจารณากรณ c1 กอน• จะเหนวา สมการไมเปนจรง• 36/2 ไมนอยกวา 0

360362

1

)6()6(3)6(2

1)6(

2

1

32

1

2

2

2

22

2

2

22

1

×≤≤×

≤−≤

≤−≤

c

c

ncnnnc

Page 7: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• เราไดวา ถา n = 6, c1 จะมคา <= 0 และถา n>=7, c1<=1/2

• ไดวา ถา n>=1 ถง n < 6, คา ½-3/n ตดลบ และถา n=6, c2

>=0 และเม อ n>=7, c >=1/2

Example: - NotationsΘ

nnnf 32

1)( 2

−=2)( nng =

>=0 และเม อ n>=7, c2>=1/2

• ถาหากเราเลอก n>=7 และ c1 ท 1/14 ลองมาแทนคา สมการกนด

• จะเหนวา สมการเปนจรง

36212

4949

14

1

)7()7(3)7(2

1)7(

14

1

32

1

2

2

2

22

2

2

22

1

×≤−≤×

≤−≤

≤−≤

c

c

ncnnnc

Page 8: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• เราไดวา ถา n = 6, c1 จะมคา <= 0 และถา n>=7, c1<=1/2

• ไดวา ถา n>=1 ถง n < 6, คา ½-3/n ตดลบ และถา n=6, c2

>=0 และเม อ n>=7, c >=1/2

Example: - NotationsΘ

nnnf 32

1)( 2

−=2)( nng =

>=0 และเม อ n>=7, c2>=1/2

• สมมตถาหากเราเลอก n>=7 และ c1 ท 1/2 ลองมาแทนคา สมการกนด

• จะเหนวา สมการไมเปนจรง

• 49/2 ไมนอยกวา 49/2-213621

2

4949

2

1

)7()7(3)7(2

1)7(

2

1

32

1

2

2

2

22

2

2

22

1

×≤−≤×

≤−≤

≤−≤

c

c

ncnnnc

Page 9: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• เราไดวา ถา n = 6, c1 จะมคา <= 0 และถา n>=7, c1<=1/2

• ไดวา ถา n>=1 ถง n < 6, คา ½-3/n ตดลบ และถา n=6, c2 >=0 และเม อ n>=7, c2>=1/2

• ทน #เรามาเลอก c บาง ท n>=1 ถง n<=5 จะแทนคาสมการแลวไดตด

Example: - NotationsΘ

nnnf 32

1)( 2

−=2)( nng =

2

1

2

51

)1(2

1)1(3)1(

2

1)1(

)1(2

13

2

1

1

222

1

222

1

≤−≤×

≤−≤

≤−≤

c

c

nnnc

• ทน #เรามาเลอก c2 บาง ท n>=1 ถง n<=5 จะแทนคาสมการแลวไดตดลบหมด แตวา เม อ n>=7 จะพบวาคาสมการลเขาส ½ ดงขอมลดานบนเน องจาก เราตองเลอก c2 ใหมากกวาคาคงท เรากจะหาคาคงท ท ทาให c2 มากกวาเสมอไมวา n จะเปนอะไร

• พจารณากรณ c2 >=1/2

• จะเหนวา สมการเปนจรง

Page 10: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ เราตองทาการหาคา c1 และ c2 และ n0 ท ทาให

สมการตอไปน #เปนจรง

Example: - NotationsΘ

nnnf 32

1)( 2

−=2)( nng =

))(()( ngnf Θ∈

Θ

จะเหนวา n>=1, C2 >=1/2

สวน n>=7, c1<=1/14

จะเหนวาเรามเง อนไขคา n สองคาสรป เรากเลอก n0 =7

เพ อใหท #งสมการเปนจรง ท ทาให สมการตอไปน #เปนจรง • 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ ≤ ≤

• หารสมการท #งหมดดวย จะได

2

1nc nn 3

2

1 2−

2

2nc

2n 21

3

2

10 c

nc ≤−≤≤

เพ อใหท #งสมการเปนจรง ท ทาให c1 <= 1/14, c2 >= ½

กจะทาใหพสจนไดวา

)(32

1 22nnn Θ=−

Page 11: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ เราตองทาการหาคา c1 และ c2 และ n0 ท ทาให

สมการตอไปน #เปนจรง

Example: - NotationsΘ

36)( nnf =2)( nng =

))(()( ngnf Θ∈

Θ

หรอมองอกมมคอ ถาพจารณา 6n <= c2

พสจนไดวาไมจรง

สมการตอไปน #เปนจรง • 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ ≤ ≤

• หารสมการท #งหมดดวย จะได

2

1nc

36n2

2nc

2n 21

60 cnc ≤≤≤

พจารณา 6n <= c2 ดงน #น n <= c2/6 ถาหากวา เราเลอกคา n เปนเลขจานวนมาก กจะไมสามารถมากกวา c2/6 ได ซ ง c2

เปนคาคงท กจะไมสามารถทาใหเรากาหนด n เปนเลขมากๆได

หรอมองอกมมคอ ถาพจารณา 6n <= c2 เม อเราแทน n=1 จะไดวา c2 มากกวา 6

พอแทน n=2 กตองได c2 มากกวา 12 ซ งจะเหนวาเราไมสามารถหาคาคงท สกคาหน งท มากกวาทกกรณของ n ใหกบ c2 ไดเลย (ตองระบคาคงท ไดวา c2 > คาคงท น #นๆ)

ทาการหา c1 เราลองแทน n>=1, c1<=6,

พอ n>=2 จะได c1<=12

ทาไปเร อยๆ เราจะยงคา c1 ท นอยกวา คาท เปล ยนไปได เพราะวา c1 จะนอยกวา 6(ต าสด)

Page 12: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ เราตองทาการหาคา c1 และ c2 และ n0 ท ทาให

สมการตอไปน #เปนจรง

Practice: - NotationsΘ

2)( nng =

))(()( ngnf Θ∈

Θ

nnnf 42)( 4−= พสจนไดวาไม

จรง

สมการตอไปน #เปนจรง • 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ ≤ ≤

• หารสมการท #งหมดดวย จะได

2

1nc

2

2nc

2n 2

2

1

420 c

nnc ≤−≤≤

nn 42 4−

Page 13: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• เราไดวา ถา n = 1, c1 จะมคา < 0 และถา n=2, c1<=6 ถา n=3, c1<=17 ไปเร อยๆ

• คาถามคอ เราจะเลอก c ท เทาไหร เพ อให สมการเปนจรง ลองมาแทน

Practice: - NotationsΘ

nnnf 42)( 4−=

2)( nng =

• คาถามคอ เราจะเลอก c1 ท เทาไหร เพ อให สมการเปนจรง ลองมาแทนคา สมการกนด ถาหากเลอก n>=1 แลว c1<=6

• พจารณากรณ c1 กอน

• จะเหนวา สมการไมเปนจรง

• 6 ไมนอยกวา -21216

)1()1(4)1(2)1(6

42

2

2

2

42

2

2

42

1

×≤−≤×

≤−≤

≤−≤

c

c

ncnnnc

Page 14: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• เราไดวา ถา n = 1, c1 จะมคา < 0 และถา n=2, c1<=6 ถา n=3, c1<=17 ไปเร อยๆ

• ถาเราเลอก n>=2 แลว c <=6

Practice: - NotationsΘ

nnnf 42)( 4−=

2)( nng =

• ถาเราเลอก n>=2 แลว c1<=6

• พจารณากรณ c1 กอน

• จะเหนวา สมการเปนจรง

• 24 เทากบ 24 22446

)2()2(4)2(2)2(6

42

2

2

2

42

2

2

42

1

×≤≤×

≤−≤

≤−≤

c

c

ncnnnc

Page 15: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• เราไดวา ถา n = 1, c1 จะมคา < 0 และถา n=2, c1<=6 ถา n=3, c1<=17 ไปเร อยๆ

• ถาเราเลอก n> 2 (n>=3)แลว c <=6

Practice: - NotationsΘ

nnnf 42)( 4−=

2)( nng =

• ถาเราเลอก n> 2 (n>=3)แลว c1<=6

• พจารณากรณ c1 กอน

• จะเหนวา สมการเปนจรง

• 54 นอยกวา 130 913096

)3()3(4)3(2)3(6

42

2

2

2

42

2

2

42

1

×≤≤×

≤−≤

≤−≤

c

c

ncnnnc

Page 16: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• ไดวา ถา n=1 , c2 >= -2 และเม อ n=2, c2=2 เม อ n>=3,

c2>=17 จะเหนวา เราสามารถเลอก เม อ n>=1 , c2 >= 2 ได

• เรามาลองทดสอบแทนคาสมการด หาก

Practice: - NotationsΘ

nnnf 42)( 4−=

2)( nng =

• เรามาลองทดสอบแทนคาสมการด หาก

• พจารณากรณ n>=1 , c2 >=2

• จะเหนวา สมการไมเปนจรง

• 12 ไมไดนอยกวา 2 2121

)1(2)1(4)1(2)1(

)1(242

1

242

1

242

1

≤≤×

≤−≤

≤−≤

c

c

nnnc

Page 17: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ไดวา ถา n=1 , c2 >= -2 และเม อ n=2, c2=2 เม อ n>=3,

c2>=17 จะเหนวา• สมมตวา พจารณากรณ n>=2 , c2 >=2

Practice: - NotationsΘ

nnnf 42)( 4−=

2)( nng =

จะเหนวา เราสามารถหาคาคงท ให c1 ไดแตหาให c2 ไมได

ดงน #น สรปไดวา g(n) ไมไดเปน big theta ของ f(n)

สมมตวา พจารณากรณ n>=2 , c2 >=2

• จะเหนวา สมการไมเปนจรง• 24 ไมไดนอยกวา 8• ตอใหเราเปล ยนคา c2 ใหมากข #นอก• แตวา คาของสมการตรงกลาง• กจะยงมากกวา คา c2 ท เราเลอกอยด

8241

)2(2)2(4)2(2)1(

)2(242

1

242

1

242

1

≤≤×

≤−≤

≤−≤

c

c

nnnc

big theta ของ f(n)

Page 18: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ เราตองทาการหาคา c1 และ c2 และ n0 ท ทาให

สมการตอไปน #เปนจรง

Practice: - NotationsΘ

2)( nng =

))(()( ngnf Θ∈

Θ

nnnf 42)( 2−=

สมการตอไปน #เปนจรง • 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ ≤ ≤

• หารสมการท #งหมดดวย จะได

2

1nc

2

2nc

2n 21

420 c

nc ≤−≤≤

หาคา c1 เราลองแทน n=1, c1=-2 ถา n=2, c1=0 ถา n>4, คาของ 4/n จะลเขาส 0 ดงน #นจะทาให c1<=2

เราจะสรปไดวา เม อ n>=5 เราจะเลอก c1 <=2

ถา n =1, c2 = -2 เรากตองเลอกคา c2 >= 2 (เพ อท จะไดทาใหสมการเปนบวก) ถา n=2, c2 = 0, ถา n =4 , c2=2 ดงน #นสรปไดวาเราจะเหนวา ถา n>=1

เราจะเลอก c2 >= 2

nn 42 2−

Page 19: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• เราไดวา ถา n=1 หรอ n=2, c1 จะมคา <= 0 และถา n>4, c1<=2

• ไดวา ถา n=1 คา c2 ตดลบ และถา n=2, c2 >=0 และเม อ n>4,

c2>=2

Practice: - NotationsΘ

nnnf 42)( 2−=

2)( nng =

c2>=2

• คาถามคอ ถาเชนน #น ลองเลอก c1 ท 2 และ n>=4 ลองมาแทนคา สมการกนด

• พจารณากรณ c1 กอน• จะเหนวา สมการไมเปนจรง• 36 ไมนอยกวา 16

161632

)4(16)16(2)4(2

42

2

2

2

2

2

2

22

1

×≤≤

≤−≤

≤−≤

c

c

ncnnnc

ถาเลอก c1=1,

n>=4 หรอถาเลอก c1=0,

n>=2

สมการจะเปนจรง

Page 20: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• เราไดวา ถา n=1 หรอ n=2, c1 จะมคา <= 0 และถา n>4, c1<=2

• ไดวา ถา n=1 คา c2 ตดลบ และถา n=2, c2 >=0 และเม อ n>4,

c2>=2

Practice: - NotationsΘ

nnnf 42)( 2−=

2)( nng =

c2>=2

• สมมต ถาเชนน #น ลองเลอก c1 ท 6/5 และ n>=5 ลองมาแทนคา สมการกนด

• พจารณากรณ c1 กอน• จะเหนวา สมการเปนจรง• 30 เทากบ 30

253030

)5(20)25(2)5(5

6

42

2

2

2

2

2

2

22

1

×≤≤

≤−≤

≤−≤

c

c

ncnnnc

Page 21: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• เราไดวา ถา n=1 หรอ n=2, c1 จะมคา <= 0 และถา n>4, c1<=2

• ไดวา ถา n=1 คา c2 ตดลบ และถา n=2, c2 >=0 และเม อ n>4,

c2>=2

Practice: - NotationsΘ

nnnf 42)( 2−=

2)( nng =

กจะพบวา c2>=2

• เพ อเปนการทดสอบ ลองเลอก c1 ท 6/5 และแทน n=6 บาง

• พจารณากรณ c1 กอน

• จะเหนวา สมการเปนจรง

• 23 นอยกวา 52

36525

216

)6(20)36(2)6(5

6

42

2

2

2

2

2

2

22

1

×≤≤

≤−≤

≤−≤

c

c

ncnnnc

กจะพบวา c1<=6/5 และ n>=5

เปนจรง

Page 22: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

O - Notations

• f(n): there exist positive

constants c and n0 such that

0 ≤ f(n) ≤ c g(n) for all n ≥ n0

• We use O-notation when we have only an

=))(( ngO

• We use O-notation when we have only an

asymptotic upper bound (the worst case

running time).

• Hence if we have

• then it follows that

))(()( ngnf Θ=

))(()( ngOnf =

Page 23: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ O เราตองทาการหาคา c และ n0 ท ทาใหสมการ

ตอไปน #เปนจรง

Example: O - Notations

23)( nnf =2)( nng =

))(()( ngOnf ∈

ตอไปน #เปนจรง • 0 ≤ f(n) ≤ c g(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ ≤

• หารสมการท #งหมดดวย จะได

23n2

cn

2n c≤≤ 30

ซ งเรากเลอกคา c ท >= 3 ได และ n >=1 เพราะคา n จะเปนอะไรกไดไมมผลอยแลว

Page 24: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ O เราตองทาการหาคา c และ n0 ท ทาใหสมการ

ตอไปน #เปนจรง

Example: O - Notations

53)( += nnf 2)( nng =

))(()( ngOnf ∈

ตอไปน #เปนจรง • 0 ≤ f(n) ≤ c g(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ ≤

• หารสมการท #งหมดดวย จะได

53 +n2

cn

n cnn

≤+≤5

30

สมมตถาใหคา n ท = 1 จะได c >= 8 , n ท = 2 จะได c >= 11/4

n=3, c >= 14/9, และ n=5,c>=1 และจะเหนวาเม อ n>=6, c >=

3/n ดงน #นแสดงวา c>0 แนๆ เม อ n>=6

Page 25: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• เราไดวา ถา c = 1, n>=3 และถา c=2, n >= 3/2 ถา c=3, n

>= 4/3 ไปเร อยๆ

• สมมตเราทดสอบสมการดวยการเลอก c >= 1, n>=3

Example: O - Notations

53)( += nnf 2)( nng =

• สมมตเราทดสอบสมการดวยการเลอก c >= 1, n>=3

• จะเหนวา สมการไมเปนจรง

• 14 ไมนอยกวา 9

9114

)3(15)3(3

53

2

2

×≤

≤+

≤+ ncn

Page 26: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• เราไดวา ถา c = 1, n>=3 และถา c=2, n >= 3/2 ไปเร อยๆ

• ทน #เราลองเลอก c = 1, n>3 (n>=4)

• จะเหนวา สมการไมเปนจรง

Example: O - Notations

53)( += nnf 2)( nng =

• จะเหนวา สมการไมเปนจรง

• 17 ไมนอยกวา 16

16117

)4(15)4(3

53

2

2

×≤

≤+

≤+ ncn

Page 27: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• เราไดวา ถา c = 1, n>=3 และถา c=2, n >= 3/2 ไปเร อยๆ

• ทน #เราลองเลอก c = 1, n>=5

• จะเหนวา สมการเปนจรง

Example: O - Notations

53)( += nnf 2)( nng =

• จะเหนวา สมการเปนจรง

• 20 นอยกวา 25

25120

)5(15)5(3

53

2

2

×≤

≤+

≤+ ncn

Page 28: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• เราไดวา ถา c = 1, n>=3 และถา c=2, n >= 3/2 ไปเร อยๆ • ทน #เราลองเลอก c >=0, n>=6 (จากแนวโนมท เราเหนวา c เขาใกล 0

เม อ n มากกวา 6)

• จะเหนวา สมการไมเปนจรง

Example: O - Notations

53)( += nnf 2)( nng =

53 2≤+ ncn• จะเหนวา สมการไมเปนจรง

• เราจะใช c=0 ไมไดเน องจาก• คาฝ งขวาจะกลายเปน 0 หมด• ซ งเรากาลงเทยบวาฝ งซายนอยกวา• ทาใหเราไมสามารถหาคาฝ งซายท <0 ได• เพราะฉะน #นตองเลอก c>=1, n>=5

023

)6(05)6(3

53

2

2

≤+

≤+ ncn

Page 29: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

o - Notations

• f(n): for any positive constant

c > 0, there exists a constant n0 > 0 such that

0 ≤ f(n) < c g(n) for all n ≥ n0

=))(( ngo

Page 30: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ little o เราตองทาการหาคา c และ n0 ท ทาให

สมการตอไปน #เปนจรง สาหรบทกๆคา c >0

Example: o - Notations

nnf 2)( = 2)( nng =

))(()( ngonf ∈

สมการตอไปน #เปนจรง สาหรบทกๆคา c >0

• 0 ≤ f(n) < c g(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ <

• หารสมการท #งหมดดวย จะได

n22

cn

n cn<≤ 20ซ งถาเราเลอกคา c ท =1 จะได n > 2 ดงน #น n0 >2

ถาเราเลอกคา c ท =2 จะได n > 1 ดงน #น n0 >1

เพราะฉะน #นเราสามารถหาคา c ไดเร อยๆถา c = k ใดๆ เรากจะหาคา n ไดท 2/k

Page 31: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• เราไดวา ถา c = 1, n> 2 และถา c=2, n > 1 ไปเร อยๆ คอ ถา c= k

, n> 2/k

• สมมตเราทดสอบสมการดวยการเลอก c = 1, n>=3

• จะเหนวา สมการเปนจรง

Example: o - Notations

nnf 2)( = 2)( nng =

• จะเหนวา สมการเปนจรง• 6 นอยกวา 9• แตหาก เลอก c=1, n>=2

• สมการจะไมเปนจรง• 4 ไมนอยกวา 4• เพราะฉะน #นตองเลอก c >=1, n>2

96

)3(1)3(2

2

2

2

≤ ncn

Page 32: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ little o เราตองทาการหาคา c และ n0 ท ทาให

สมการตอไปน #เปนจรง

Example: o - Notations

23)( nnf =2)( nng =

))(()( ngonf ∈

พสจนไดวาไมจรง

สมการตอไปน #เปนจรง • 0 ≤ f(n) < c g(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ <

• หารสมการท #งหมดดวย จะได

23n2

cn

2n c<≤ 30

ซ งเราจะไดคา c ท > 3 แตวาตามนยามของ little o บอกไววา สาหรบคา c ทกๆตวท > 0 (ซ งเราแทน c = 1 ไมไดในกรณน #) กจะ

พบวา เราไมสามารถพสจนได

Page 33: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

O-notations vs. o-notations

• จะเหนวา O –notations จะครอบคลมกรณมากกวา คอ ถาเรา วเคราะหสมการเวลาไว tight กได หรอจะไม tight bound กได

• แตวา o – notations จะครอบคลมเฉพาะกรณท ไม tight bound

• Tight bound คอกรณท สมการไมฟตกน อาจจะเพราะเรา over estimate เชน กรณ และTight bound คอกรณท สมการไมฟตกน อาจจะเพราะเรา over estimate เชน กรณ และ

• แลวเราจะหาวา • จะหาดวย little-o ไดเพราะวา ไม tight bound

• ดงน #น ถาสมการ ไม tight bound เราจะพบคา little o ของมน และ big o ของมนดวย

nnf 2)( = 2)( nng =

))(()( ngonf ∈

Page 34: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

- Notations

• f(n): there exist positive

constants c and n0 such that

0 ≤ c g(n) ≤ f(n) for all n ≥ n0

• We use -notation when we have only an

=Ω ))(( ng

Ω

Ω• We use -notation when we have only an

asymptotic lower bound (the best case

running time).

• Hence if we have

• then it follows that

))(()( ngnf Θ=

))(()( ngnf Ω=

Ω

Page 35: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ เราตองทาการหาคา c และ n0 ท ทาใหสมการ

ตอไปน #เปนจรง

Example: - NotationsΩ

23)( nnf = nng =)(

))(()( ngnf Ω∈

Ω

ตอไปน #เปนจรง • 0 ≤ c g(n) ≤ f(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ ≤

• หารสมการท #งหมดดวย จะได

cn 23n

2n 30 ≤≤

n

c

ถา n = 1 แลวให c <= 3 ถา n=2 แลว c <= 6, n=3, c<=9

ตอไปเร อยๆ จะเหนวา เราสามารถหาคาคงท ให cท ทาใหนอยกวา กจะไดวา n0>=1, c<=3

Page 36: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ

• เราไดวา ถา n = 1, c<=3 และถา n=2, c <= 6 ไปเร อยๆ

• ทน #เราลองเลอก c = 3, n =1

• จะเหนวา สมการเปนจรง

Example: - Notations

23)( nnf = nng =)(

3 2≤ nnc

Ω

• จะเหนวา สมการเปนจรง

• 3 เทากบ 3

• แตหากเลอก c>3, n=1 จะพบวาสมการไมเปนจรง33

)1(3)1(3

34

)1(3)1(4

3 2

≤ nnc

Page 37: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ เราตองทาการหาคา c และ n0 ท ทาใหสมการ

ตอไปน #เปนจรง

Example: - NotationsΩ

23)( nnf =2)( nng =

))(()( ngnf Ω∈

Ω

ตอไปน #เปนจรง • 0 ≤ c g(n) ≤ f(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ ≤

• หารสมการท #งหมดดวย จะได

2cn

23n

2n 30 ≤≤ c

เรากจะไดวา c <= 3 ดงน #น c เปนเลขอะไรกไดท นอยกวา 3สวน n จะเปนเทาไหรกได เพราะสมการท เหลอไมข #นกบ n

Page 38: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

- Notations

• f(n): for any positive constants

c > 0 and n0 such that

0 ≤ c g(n) < f(n) for all n ≥ n0

ω

=))(( ngω

Page 39: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ เราตองทาการหาคา c และ n0 ท ทาใหสมการ

ตอไปน #เปนจรง

Example: - Notationsω

23)( nnf = nng =)(

))(()( ngnf ω∈

ω

ตอไปน #เปนจรง • 0 ≤ c g(n) < f(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ <

• หารสมการท #งหมดดวย จะได

cn 23n

2n 30 <≤

n

c

ถาหาก c =3 เรากจะไดคา n > 1 เพ อให พจน c/n มนนอยกวา 3 ตามสมการสมมต c=1 เรากจะไดคา n > 1 เชนกน(หารแลวปดข #น) เรากสามารถหาคา c ไดทกตว ท ทาให หาคา n

ได

Page 40: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

• กาหนดให และ• ตองการพสจนวา • จากนยามของ เราตองทาการหาคา c และ n0 ท ทาใหสมการ

ตอไปน #เปนจรง

Example: - Notationsω

23)( nnf =2)( nng =

))(()( ngnf ω∈

ω

พสจนไดวาไมจรง

ตอไปน #เปนจรง • 0 ≤ c g(n) < f(n)

• เร มจากแทนคาฟงกชน f(n) และ g(n) ลงไป• จะได 0 ≤ <

• หารสมการท #งหมดดวย จะได

2cn

23n

2n 30 <≤ c

กจะไดคา c < 3 แตวาไมตรงกบนยาม เพราะนยามบอกวา สาหรบ c ทกๆตวท มากกวา 0 ดงน #นกรณน # เราจะแทนคา c=1 กไมได

Page 41: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Example of the notationsω,,,, ΩΘ oO

Upper boundLower bound

ถาไม tight

f(n)=o(g(n))

ถาไม tight

f(n) = (g(n))ω

Page 42: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Practice: Notations

• กาหนดให และnnnf 42)( 4−=

2)( nng =

ω,,,, ΩΘ oO

Page 43: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Practice: Notations

• กาหนดให และnnnf 42)( 2−=

2)( nng =

ω,,,, ΩΘ oO

Page 44: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Standard notations and common

functions

• Monotonicity

• A function f(n) is monotonically increasing if

m ≤ n implies f(m) ≤ f(n).

• A function f(n) is monotonically decreasing if • A function f(n) is monotonically decreasing if

m ≤ n implies f(m) ≥ f(n).

• A function f(n) is strictly increasing if m < n

implies f(m) < f(n) and strictly decreasing if m

< n implies f(m) > f(n).

Page 45: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Standard notations and common

functions

• Floor and ceilings

• For all real x, x-1 < ≤x ≤ < x+1

• For any integer n,

• For any real number n ≥ 0 and integers a,b > 0,

x x

nnn =+ 2/2/

• For any real number n ≥ 0 and integers a,b > 0,

• The floor function f(x) = is monotonically increasing, as is the ceiling function f(x) = .

x

x

bbaba

bbaba

abnban

abnban

/))1((/

/))1((/

///

///

−−≥

−+≤

=

=

Page 46: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Standard notations and common

functions

• Modular arithmetic :

a mod n = a –

• Polynomials:

nna /

∑=

di

nanp )(

• We say that a function f(n) is polynomially

bounded if f(n) = O(nk) for some constant k.

∑=

=i

i

inanp

0

)(

Page 47: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Standard notations and common

functions

• Exponentials : for all real a > 0, m and n, we have the following identities:

aa

aa

a

=

=

=

/1

1

1

1

0

• For all n and n>=1, the function an is monotonically increasing in n.

nmnm

mnnm

mnnm

aaa

aa

aa

+

=

=

=

)()(

)(

Page 48: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Standard notations and common

functions

• The rate of growth of polynomials and

exponentials for all real constants a, b such

that a>1, any exponential function with a

base strictly greater than 1 grows faster than base strictly greater than 1 grows faster than

any polynomial function.

• Using e to denote the base of the natural

logarithm function, we have for all real x:

• For all real x, we have :∑

=0 !i

i

i

x

xex

+≥ 1

211 xxexx

++≤≤+

Page 49: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Standard notations and common

functions

• Logarithms: we shall use the following notations:

)(lglg

logln

loglg2

nn

nn

nn

kk

e

=

=

= Binary logarithm

Natural logarithm

Exponentiation

Composition

• We say that a function f(n) is polylogarithmically

bounded if f(n) = O(lg n) for some constant k.

• Thus any positive polynomial function grows

faster than any polylogarithmic function.

)lg(lglglg

)(lglg

nn

nn

=

=Composition

Page 50: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Standard notations and common

functions

• Factorial: for integers n >=0

)lg()!lg(

)2(!

)(!

nnn

n

non

n

n

Θ=

=

=

ω

)lg()!lg( nnn Θ=

Page 51: Ch7: Asymptotic Notations - Naresuan University · Ch7: Asymptotic Notations 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is asymptotic notation?

Practice: asymptotic growth rates

• Rank the following functions by order of

growth.

2

n

n

1

n

3

2 1

2

1

lg

2

n

nn

n +

12

3

2

2

lg

+n

n

n

nn

n