《算法导论》

算法整理:

1)  插入排序

INSERTION-SORT(A)

1  for j ← 2 to length[A]

2       do key ← A[j]

3          Insert A[j] into the sorted sequence A[1 ‥ j - 1].

4          i ← j - 1

5          while i > 0 and A[i] > key

6              do A[i + 1] ← A[i]

7                 i ← i - 1

8          A[i + 1] ← key

 

2)  合并排序

MERGE(A, p, q, r)

 1  n1 ← q - p + 1

 2  n2 ← r - q

 3  create arrays L[1 ‥ n1 + 1] and R[1 ‥ n2 + 1]

 4  for i ← 1 to n1

 5       do L[i] ← A[p + i - 1]

 6  for j ← 1 to n2

 7       do R[j] ← A[q + j]

 8  L[n1 + 1] ← ∞

 9  R[n2 + 1] ← ∞

10  i ← 1

11  j ← 1

12  for k ← p to r

13       do if L[i] ≤ R[j]

14             then A[k] ← L[i]

15                  i ← i + 1

16             else A[k] ← R[j]

17                  j ← j + 1

 

MERGE-SORT(A, p, r)

1 if p < r

2   then q ← (p + r)/2

3        MERGE-SORT(A, p, q)

4        MERGE-SORT(A, q + 1, r)

5        MERGE(A, p, q, r)

 

3)  inversions计数(Let A[1 n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A.),用修改的merge排序算法:

 

COUNT-INVERSIONS(A, p, r )

inversions 0

if p < r

then q        (p + r)/2 inversions inversions +COUNT-INVERSIONS(A, p, q)

inversions inversions +COUNT-INVERSIONS(A, q + 1, r )

inversions inversions +MERGE-INVERSIONS(A, p, q, r )

return inversions

 

MERGE-INVERSIONS(A, p, q, r )

n1 q p + 1

n2 r q

create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]

for i 1 to n1

do L[i ] A[p + i 1]

for j 1 to n2

do R[ j ] A[q + j ]

L[n1 + 1]←∞

R[n2 + 1]←∞

i 1

j 1

inversions 0

counted FALSE

for k p to r

do if counted = FALSE and R[ j ] < L[i ]

then inversions inversions +n1 i + 1

counted TRUE

if L[i ] R[ j ]

then A[k] L[i ]

i i + 1

else A[k] R[ j ]

j j + 1

counted FALSE

return inversions

第三章 函数的增长

A way to compare .sizes. of functions:

O ≈ ≤

Ω ≈ ≥

Θ ≈ =

o <

ω >

 

O-notation

O(g(n)) = {f (n) : there exist positive constants c and n0 such that

0 f (n) cg(n) for all n n0} .

 

Ω-notation

_Ω(g(n)) = { f (n) : there exist positive constants c and n0 such that

0 cg(n) f (n) for all n n0} .

 

Θ-notation

Θ(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} .

 

Theorem

f (n) =Θ(g(n)) if and only if f = O(g(n)) and f =Ω(g(n)) .

 

o-notation

o(g(n)) = { f (n) : for all constants c > 0, there exists a constant

n0 > 0 such that 0 f (n) < cg(n) for all n n0}

 

n1.9999 = o(n2)

n2/ lg n = o(n2)

n2 o(n2) (just like 2 _< 2)

n2/1000 o(n2)

 

ω-notation

ω(g(n)) = { f (n) : for all constants c > 0, there exists a constant

n0 > 0 such that 0 cg(n) < f (n) for all n n0} .

 

n2.0001 = ω(n2)

n2 lg n = ω(n2)

n2  ω(n2)

 

关系属性:

Transitivity:

f (n) =Θ(g(n)) and g(n) =Θ(h(n)) f (n) =Θ(h(n)).

Same for O, Ω, o, and ω.

 

Reßexivity:

f (n) =Θ( f (n)).

Same for O andΩ.

 

Symmetry:

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

 

Transpose symmetry:

f (n) = O(g(n)) if and only if g(n) =Ω( f (n)).

f (n) = o(g(n)) if and only if g(n) = ω( f (n)).

 

Comparisons:

f (n) is asymptotically smaller than g(n) if f (n) = o(g(n)).

f (n) is asymptotically larger than g(n) if f (n) = ω(g(n)).

n! = o(nn)
n! =
ω(2n
)
lg(
n!) =Θ(nlgn)

 

第四章 递归

主要讲了三种解递归式的方法,for obtaining asymptotic "Θ" or "O" bounds on the solution

1.         substitution method:

a)         Guess the solution.

b)        Use induction to find the constants and show that the solution works.

 

2.         recursion-tree method:

a)         Use to generate a guess.

b)        Then verify by substitution method.

 

3.         master method
主要是针对解这样的式子:

T
(n) = aT (n/b) + f (n) , where a 1, b > 1, and f (n) > 0.
Compare nlogb a vs. f
(n)

对于不同的f(n),分三种情况讨论:

Case 1: f (n) = O(nlogb a−c) for some constant c > 0.

( f (n) is polynomially smaller than nlogb a.)

Solution: T (n) = _(nlogb a).

(Intuitively: cost is dominated by leaves.)

EgT (n) = 5T (n/2) + _(n2)

nlog2 5 vs. n2

Since log2 5 c = 2 for some constant c > 0, use Case 1=> T (n) =Θ(nlg 5)

 

Case 2: f (n) =Θ(nlogb a lgk n), where k 0.(注意,k一定要≥0)

 ( f (n) is within a polylog factor of nlogb a, but not smaller.)

Solution: T (n) = _(nlogb a lgk+1 n).

(Intuitively: cost is nlogb a lgk n at each level, and there are Θ(lg n) levels.)

Simple case: k = 0 => f (n) =Θ(nlogb a) => T (n) =Θ(nlogb a lg n).

Eg: T (n) = 27T (n/3) +Θ(n3 lg n)

nlog3 27 = n3 vs. n3 lg n

Use Case 2 with k = 1=> T (n) =Θ(n3 lg2 n)

 

Case 3: f (n) =Ω(nlogb a+e) for some constant e > 0 and f (n) satisfies the regularity condition af (n/b) c f (n) for some constant c < 1 and all sufficiently large n.

( f (n) is polynomially greater than nlogb a.)

Solution: T (n) = Θ( f (n)).

(Intuitively: cost is dominated by root.)
Eg
T (n) = 5T (n/2) +Θ(n3)

nlog2 5 vs. n3

Now lg 5 + e = 3 for some constant e > 0

Check regularity condition (don’t really need to since f (n) is a polynomial):

a f (n/b) = 5(n/2)3 = 5n3/8 cn3 for c = 5/8 < 1

Use Case 3=> T (n) =Θ(n3)

 

      

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值