主讲人: 吕敏 email: { lvmin05@ustc } spring 2012 , ustc

32
niversity of Science and Technology of Chi 主主主 主主 Email: { [email protected] } Spring 2012 USTC 主主主主

Upload: winola

Post on 12-Jan-2016

152 views

Category:

Documents


1 download

DESCRIPTION

算法基础. 主讲人: 吕敏 Email: { [email protected] } Spring 2012 , USTC. 第二讲 函数增长. 内容提要: 渐进记号 常用函数 级数求和. n →∞ , 归并排序 Θ( n lg n ), 算法性能优于 插入排序 Θ( n 2 ) 利用函数增长率来描述算法效率,并可以用来比较各种算法的相对性能; 算法的渐近效率, 即极限情况下的函数行为 通常忽略低阶项和常数因子 如何描述算法的运算时间 比较函数大小的方法. 第二讲 函数增长. 内容提要: 渐进记号 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China

主讲人: 吕敏Email: { [email protected] }

Spring 2012 , USTC

算法基础

Page 2: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China23/4/21 2

第二讲 函数增长内容提要: 渐进记号 常用函数级数求和

Page 3: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China23/4/21 3

n →∞, 归并排序 Θ(nlgn), 算法性能优于 插入排序 Θ(n2) 利用函数增长率来描述算法效率,并可以用来比较各种算法的相

对性能; 算法的渐近效率,即极限情况下的函数行为 通常忽略低阶项和常数因子 如何描述算法的运算时间 比较函数大小的方法

< ; ; = ; ; >o O

Page 4: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China23/4/21 4

第二讲 函数增长内容提要: 渐进记号

定义: O, Ω, Θ, o, ω

证明例子 常用函数 级数求和

Page 5: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China

Asymptotic notation (渐近记号)

23/4/21 5

表示渐近运行时间的记号用定义域为自然数集的函数定义。

为了方便,可以将定义域扩充至实数域或缩小到自然数集 的某个受限子集上。

注意:要清楚这些表示法的准确含义,以保证越规使用但 不误用。

Page 6: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China

Θ-notation: asymptotically tight bound (渐近紧界)

23/4/21 6

What this notation T(n)=Θ(n2) means

For a given function g(n), we denote by Θ(g(n)) the set of functions

Θ(g(n)) = {f(n): there exist positive constants c1, c2 , and n0 such that

0≤c1g(n)≤f(n)≤c2g(n) for all n≥n0}.

we could write “f(n) Θ(∈ g(n))” to indicate that f(n) is a member of Θ(g(n)).

Instead, we will usually write “f(n)=Θ(g(n))” to express the same notion. The abuse may at first appear confusing, but it has advantages.

Page 7: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China

Θ-notation: asymptotically tight bound (渐近紧界)

23/4/21 7

T(n)=Θ(n2) For a given function g(n), we denote by Θ(g(n)) the set of functions

Θ(g(n)) = {f(n): there exist positive constants c1, c2 , and n0 such that

0≤c1g(n)≤f(n)≤c2g(n) for all n≥n0}.

We say that g(n) is an asymptotically tight bound for f(n).

Page 8: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China

Θ-notation: asymptotically tight bound (渐近紧界)

23/4/21 8

Θ(g(n)) = { f(n): there exist positive constants c1, c2 , and n0 such that

0≤c1g(n)≤f(n)≤c2g(n) for all n≥n0 }. In this chapter, assume that every asymptotic notations are

asymptotically nonnegative. (设所有的渐近符号为渐近非负) Example: How to show that n2/2-3n=Θ(n2) ?

We must determine positive constants c1, c2, and n0 such that

by choosing c1 =1/14, c2=1/2, and n0=7, we can verify that n2/2-3n=Θ(n2)

Other choices for the constants may exist. The key is some choice exists. (可能有多个值,只要存在某个值就可以了)

2 2 21 2

1 2

/ 2 3

1/ 2 3/

c n n n c n

c n c

Page 9: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China

Θ-notation: asymptotically tight bound (渐近紧界)

23/4/21 9

Θ(g(n)) = {f(n): there exist positive constants c1, c2 , and n0 such that

0≤c1g(n)≤f(n)≤c2g(n) for all n≥n0 }.

How verify that ?

Suppose for the purpose of contradiction that c2 and n0 exist such that for all . But then , which cannot possibly hold for arbitrarily large n, since c2 is constant.

3 26 ( )n n

3 226n c n 0n n 2 / 6n c

Page 10: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China

Θ-notation: asymptotically tight bound (渐近紧界)

23/4/21 10

Θ(g(n)) = { f(n): there exist positive constants c1, c2 , and n0 such that

0≤c1g(n)≤f(n)≤c2g(n) for all n≥n0 }.

The lower-order terms, the coefficient of the highest-order term can be ignored.

Example: , where a>0, b, c are constants.

Throwing away the lower-order terms and ignoring the constant yields . (Page44)

In general, for any polynomial , where the ai are constants and ad >0, we have .

We can express any constant function as or .

often mean either a constant or a constant function.

2( )f n an bn c

2( ) ( )f n n

0( )

d iii

p n a n

( ) ( )dp n n

0( )n (1)(1)

Page 11: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China11

O – notation: For a given function g(n), we denote by O(g(n)) the set of functions O(g(n)) = {f(n): there exist positive constants c and n0 such that 0≤f(n)≤c g(n) for all n≥n0}.

" ( ) ( ( ))" indicates " ( ) ( ( ))"

( ) ( ( )) ( ) ( ( ))

( ( )) ( ( ))

f n O g n f n O g n

f n g n f n O g n

g n O g n

O-notation: asymptotic upper bound (渐近上界)

Page 12: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China12

O – notation: For a given function g(n), we denote by O(g(n)) the set of functions O(g(n)) = {f(n): there exist positive constants c and n0 such that 0≤f(n)≤c g(n) for all n≥n0}.

Example: 2n2 = O(n3), with c=1 and n0=2 Example of functions in O(n2)2

2

2

2

2000

500 1000

n

n n

n n

n n

O-notation: asymptotic upper bound

1.99999

2

/ 3000

/ lg lg lg

n

n

n

n n

Page 13: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China13

Ω-notation: asymptotic lower bound (渐近下界)

Ω – notation: For a given function g(n), we denote by Ω(g(n)) the set of functions Ω(g(n)) = {f(n): there exist positive constants c and n0 such that

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

Page 14: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China14

Ω-notation: 渐近下界

Ω – notation: For a given function g(n), we denote by Ω(g(n)) the set of functions Ω(g(n)) = {f(n): there exist positive constants c and n0 such that

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

Example of functions in Ω(n2)

2

2

2

2

2000

500 1000

n

n n

n n

n n

3

2.0000001

2 lg lg lg

n

n

n n

Page 15: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China15

Theorem 3.1

For any two functions f(n) and g(n), we have f(n)=Θ(g(n)) if and only if f(n)=O(g(n)) and f(n) =Ω(g(n)).

Prove:

1 2 0

0 1 2

0 2

0 1

2 20

: ( ) ( ( )), then 0, 0, >0,

s.t. , 0 ( ) ( ) ( )

then , 0 ( ) ( ) ( ) ( ( ))

then , 0 ( ) ( ) ( ) ( ( ))

: ( ) ( ( )), then 0, >

f n g n c c n

n n c g n f n c g n

n n f n c g n f n O g n

n n c g n f n f n g n

f n O g n c n

20 20

10 10

10 10

0 10 20 0

10 20

0,

s.t. , 0 ( ) ( )

( ) ( ( )), then 0, >0,

s.t. , 0 ( ) ( )

let max{ , }, then ,

0 ( ) ( ) ( ), that is ( ) ( ( )).

n n f n c g n

f n g n c n

n n c g n f n

n n n n n

c g n f n c g n f n g n

Ω-notation: 渐近下界

Page 16: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China16

Theorem 3.1

For any two functions f(n) and g(n), we have f(n)=Θ(g(n)) if and only if f(n)=O(g(n)) and f(n) =Ω(g(n)).

In practice, rather than using the theorem to obtain asymptotic upper and lower bounds from asymptotically tight bounds, we usually use it to prove asymptotically tight bounds from asymptotic upper and tower bounds.

(定理作用:实际中,通常根据渐近上界和渐近下界来证明渐近紧界,而不是根据渐近紧界来得到渐近上界和渐近下界。)

Ω-notation: 渐近下界

Page 17: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China17

• 因为 Ω 记号描述了渐近下界,当它用来对一个算法最佳情况运行时间限界时,也隐含给出了在任意输入下运行时间的界。

• 插入排序的最佳情况运行时间是 Ω(n) ,这隐含着该算法的运行时间是 Ω(n) 。

• 我们不能说插入排序算法的运行时间为 Ω( ) 。这是因为存在一个输入(也就是已经排好序情况下),插入排序时间为 Θ(n) 。但是,可以说该算法的最坏运行时间为 Ω( ) 。

• 当我们说一个”算法的运行时间为 Ω(g(n))” 时,我们是指对足够大的 n 值,对输入规模为 n 的任意输入,其运行时间至少是g(n) 的一个常数倍。

Ω-notation: 渐近下界

2n

2n

Page 18: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China18

我们已经见过了渐近记号在数学公式中的应用。如 n=O( ) , 2 +3n+1= 2 +Θ(n) 等,如何来解释这些公式呢?

1 )当渐近记号出现在等式的右边时,则等号表示左边的函数属于右边函数集合中的元素,即等号表示集合的成员关系。

2 )一般来说,当渐近记号出现在某个公式中时,我们将其解释为一个不在乎其名称的匿名函数。如公式“ 2 +3n+1=2 +Θ(n)” 即表示“ 2 + 3n+1=2 +f(n)”, 其中 f(n) 是某个属于集合 Θ(n )的函数。这里 3n+1也确实在 Θ(n )中。

3 )当渐近记号出现在等式左边的时候,这时候我们用如下规则来解释:无论等号左边的匿名函数如何选择,总有办法选取等号右边的匿名函数使等式成立。这样,对于任意函数 f(n) Θ(∈ n) ,存在函数 g(n) Θ( ∈ ) ,使得对所有的 n ,有 2 +f(n)=g(n) 成立。换而言之,等式右边提供了较左边更少的细节。

4 )利用 2 ) 3 )我们就可以得到这么一个等式“ 2 + 3n+1=2 +Θ(n)=Θ( )”. 该等式的解释是:第一个等式说明存在函数 f(n) Θ(∈ n), 使对所有的n 有 2 + 3n+1 = 2 +f(n) 。第二个等式说明,对任意函数 g(n) Θ(∈ n ) ,存在某个函数 h(n) Θ(∈ ) , 使对所有的 n 有 2 +g(n)= h(n) 。当然了,这个解释也蕴含着 2 + 3n+1=Θ( ).

等式和不等式中的渐近记号2n 2n 2n

2n 2n 2n2n

2n2n

2n2n 2n

2n2n

2n 2n

2n

2n

Page 19: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China19

大 O 记号所提供的渐近上界可能是、也可能不是渐近紧确的。 例如, 2n2=O(n2) 是渐近紧确的 , 但 2n=O(n2) 却不是 . 我们利用小 o 记号来表示非渐近紧确的上界,其定义如下

(渐近非紧上界)

o(g(n)) = {f(n): for any positive constants c>0, there exits a constant n0>0 such that 0≤f(n)<c g(n) for all n≥n0}.

For example, 2n=o(n2), but 2n2≠o(n2).

o-notation: ( 非渐近紧确上界)

Page 20: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China20

ω-notation is to Ω-notation as o-notation is to O-notation. Theω-notation denotes an lower bound that is not asymptotically

tight. Formally, define ω(g(n)) as the set

ω(g(n)) = { f(n): for any positive constants c>0, there exits a constant n0>0 such that 0≤c g(n)<f(n) for all n≥n0}.

One way to define it is by f(n)∈ω(g(n)) if and only if g(n) o(∈ f(n))

For example, n2/2=ω(n), but n2/2≠ω(n2).

The relation f(n)=ω(g(n)) implies that

ω-notation — 非渐近紧确下界

( )lim , if the limit exists.

( )n

f n

g n

Page 21: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China21

Many of the relational properties of real number apply to asymptotic comparisons.

For the following, Assume that f(n) and g(n) are asymptotically positive.

Transitivity (传递性)

Comparison of functions (函数比较)

( ) ( ( )) and ( ) ( ( )) imply ( ) ( ( )),

( ) ( ( )) and ( ) ( ( )) imply ( ) ( ( )),

( ) ( ( )) and ( ) ( ( )) imply ( ) ( ( )),

( ) ( ( )) and ( ) ( ( )) imply ( ) ( ( )),

( )

f n g n g n h n f n h n

f n O g n g n O h n f n O h n

f n g n g n h n f n h n

f n o g n g n o h n f n o h n

f n

( ( )) and ( ) ( ( )) imply ( ) ( ( )).g n g n h n f n h n

Page 22: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China22

Reflexivity (自反性)

Symmetry (对称性)

Transpose symmetry (反对称性)

( ) ( ( )),

( ) ( ( )),

( ) ( ( )).

f n f n

f n O f n

f n f n

( ) ( ( )) if and only if ( ) ( ( )).f n g n g n f n

( ) ( ( )) if and only if ( ) ( ( )),

( ) ( ( )) if and only if ( ) ( ( )).

f n O g n g n f n

f n o g n g n f n

Comparison of functions (函数比较)

Page 23: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China23

An analogy between the asymptotic comparison of two functions and the comparison of two real numbers

(函数渐近性比较与实数比较的类比)

( ) ( ( )) ,

( ) ( ( )) ,

( ) ( ( )) ,

( ) ( ( )) ,

( ) ( ( )) .

f n o g n a b

f n O g n a b

f n g n a b

f n g n a b

f n g n a b

Comparison of functions (函数比较)

Page 24: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China24

One property of real numbers, does not carry over to asymptotic notation

Trichotomy (三分法) : any two real numbers a and b, one of the following must holds: a<b, a=b, or a>b.

Not all functions are asymptotically comparable. That is, for two functions f(n) and g(n), it may be the case that neither f(n)=O(g(n)) nor f(n)=Ω(g(n)) holds.

For example, the functions n and cannot be compared using asymptotic notation.

1 sin nn

0 1 sin 2

1 sin 1 sin

1 sin 1

???

n

n n

n n n n

n n n

Comparison of functions (函数比较)

Page 25: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China23/4/21 25

第二讲 函数增长内容提要: 渐进记号 常用函数级数求和

Page 26: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China26

Monotonicity (单调性) Floors and ceilings

Modular arithmetic (remainder or residue) (模运算)

Polynomials (多项式)

Exponentials (指数) Logarithms (对数) Factorials (阶乘)

1 1x x x x x

Standard notation and common function

mod /a n a a n n

0

( )d

ii

i

p n a n

)lg()!lg(),2(!),(!

))1

(1(2!

nnnnnon

ne

nnn

nn

n

斯特林近似公式

Page 27: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China27

Functional iteration (迭代函数)We use the notation f(i)(n) to denote the function f(n) iteratively applied i times to an initial value of n. For non-negative integers i, we recursively define

For example, if f(n)=2n, then

( )

( 1)

if =0( )

( ( )) if >0i

i

n if n

f f n i

(2) 2

( )

( ) ( ( )) (2 ) 2(2 ) 2

( ) 2i i

f n f f n f n n n

f n n

Standard notation and common function

Page 28: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China28

The iterated logarithm function (多重对数函数)We use the notation lg*n to denote the iterated logarithm. Let lg(i)n be iterated function, with f(n)=lgn, that is

lg(i)(n)=lg(lg(i-1)(n)) . lg(i)n is defined only if lg(i-1)n>0. Be sure to distinguish lg(i)n from lgin. lg*n is defined as

The iterated logarithms is a very slowly growing function:

265536 >>1080. Rarely encounter an input size n such that lg*n>5.

Fibonacci numbers

* ( )lg min{ 0 : lg 1}in i n

* * *

* * 65536

lg 2 1, lg 4 2, lg 16 3,

lg 65536 4, lg 2 5.

Standard notation and common function

Page 29: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China23/4/21 29

第二讲 函数增长内容提要: 渐进记号 常用函数级数 求和

Page 30: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China

级数求和

定义:有限和、无限和、级数收敛、级数发散的、绝对收敛级数

等差级数:

平方和与立方和:

几何级数:

调和级数:

级数的积分和微分:

23/4/21 30

)()1(2

1...21 2

1

nnnnkn

k

4

)1(,

6

)12)(1( 22

0 0

32

nnk

nnnk

n

k

n

k

n

k

nnk

x

xxxxx

0

12

1

1...1

)1(ln...11

1131

21 OnH

n

kknn

20 )1( x

xkx

k

k

Page 31: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China

确定求和时间的界

数学归纳法

1)计算级数的准确值

2)计算和式的界,如:证明几何级数 的界是

3)一个容易犯的错误,如:证明

确定级数各项的界

1)一个级数的理想上界可以通过对级数中的每个项求界来获得;

2)一个级数实际上以一个几何级数为界时,可以选择级数的最大项作为每项的界(注意防止犯错!)

分割求和:可以将级数表示为两个或多个级数,按下标的范围进行划分

,然后再对每个级数分别求界。

23/4/21 31

n

k

nOk1

)(

n

k

k

0

3 )3( nO

Page 32: 主讲人:   吕敏 Email: {  lvmin05@ustc  } Spring  2012 , USTC

University of Science and Technology of China

谢谢!

23/4/21 32

Q & A

作业:3.2-3

第三章思考题 3-2 , 3-3