《算法导论》

本文介绍了插入排序和合并排序两种基本排序算法,并提供详细的伪代码。此外,还讲解了如何通过修改合并排序来计算数组中逆序对的数量。文章进一步探讨了函数增长的比较方法及递归式的解决策略。

算法整理:

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)

 

      

 

 

 

<think>好的,我需要处理用户关于《算法导论》的查询。首先,用户希望查找与这本书相关的资料或内容,包括书籍本身、相关资料、计算机科学背景以及算法实现。根据提供的引用内容,用户已经提到了两篇引用,其中引用[1]和[2]都介绍了《算法导论》的基本信息和资源。 首先,我应该确认用户的主要需求。他们可能需要了解如何获取这本书,或者需要学习资源、相关课程、算法实现示例等。根据用户的问题,他们明确提到了“算法导论 书籍 资料 计算机科学 算法实现”,所以需要涵盖这些方面。 接下来,我需要整理现有的引用信息。引用[1]提到了《算法导论》的作者和内容概览,引用[2]则提供了PDF下载的信息,并强调其作为经典教材的地位。此外,用户提供的系统级指令要求生成相关问题,并确保回答结构清晰,帮助用户逐步解决问题。 在回答结构上,应该分点列出书籍简介、内容概述、学习资源、算法实现资源以及相关课程推荐。每个部分都需要结合引用内容,并在末尾添加引用标识。同时,需要确保使用正确的中文表达,避免使用Markdown格式,但根据用户提供的示例,可能需要使用代码块来展示算法实现示例,比如Python代码。 需要注意用户提到的所有行内数学表达式必须用$...$,独立公式用$$...$$。例如,在讨论算法时间复杂度时,可能需要提到$O(n \log n)$这样的表达式。但根据当前用户的问题,可能数学公式较少,但如果有相关部分需要涉及,必须正确应用格式。 另外,用户要求生成相关问题,我需要根据《算法导论》的内容,设计几个相关问题,如学习方法、核心章节、算法实现注意事项等。 最后,确保回答真实可靠,引用提供的资料,并正确标注引用标识。例如,在提到书籍内容时,引用[^1]和[^2]。 现在开始组织回答内容: 1. 书籍简介:根据引用[1]和[2],介绍《算法导论》的基本信息,作者、版本、内容范围等。 2. 内容概述:分点列出主要章节内容,如基础数据结构、算法设计技巧、高级主题等,结合引用中的信息。 3. 学习资源:包括PDF下载(引用[2])、在线课程、开源项目等,可能需要补充一些常见的资源,如MIT公开课,GitHub上的算法实现仓库。 4. 算法实现资源:提供具体的代码示例,如Python实现快速排序,并解释其时间复杂度$O(n \log n)$,同时引用相关章节。 5. 相关课程推荐:如Coursera的算法专项课程,edX的相关课程,引用来源可能需要假设或根据现有引用调整。 生成的问题需要围绕学习《算法导论》的难点、核心章节、与其他书籍的比较、算法实现中的常见错误等。 检查是否有遗漏,确保每个部分都引用了正确的来源,并且格式符合要求,没有使用Markdown,但代码块使用三个反引号包裹。数学表达式正确应用$符号,例如时间复杂度$O(n^2)$或$O(n \log n)$。 最后,验证所有引用标识是否正确添加,例如在提到PDF下载时引用,书籍简介引用[^1]和。</think>### 《算法导论》相关资料与内容指南 #### 1. 书籍简介 《算法导论》(*Introduction to Algorithms*)是计算机科学领域的经典教材,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein合著(即CLRS组合)[^1]。第四版系统性地介绍了算法设计与分析的核心内容,涵盖基础数据结构(如栈、队列、树)、算法设计技巧(分治、动态规划、贪心算法)以及高级主题(NP完全性、并行算法等)[^2]。 #### 2. 内容概述 - **基础章节**:算法分析(时间复杂度$O(n)$、$O(n^2)$等)、排序算法(快速排序、归并排序)、数据结构(红黑树、哈希表)。 - **进阶章节**:动态规划(如背包问题)、图算法(Dijkstra最短路径、最小生成树)、字符串匹配(KMP算法)。 - **理论扩展**:NP完全性理论、概率算法、线性规划。 #### 3. 学习资源 - **PDF下载**:可通过学术平台或图书馆获取《算法导论 第四版》的电子版[^2]。 - **配套代码**:GitHub上有开源项目提供书中算法的代码实现(如Python、Java)。 - **在线课程**:MIT OpenCourseWare提供配套课程视频(如“算法导论”公开课)。 #### 4. 算法实现示例 以下为快速排序的Python实现(基于分治思想,平均时间复杂度$O(n \log n)$): ```python def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[len(arr)//2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right) ``` #### 5. 相关课程推荐 - **Coursera**:普林斯顿大学《算法(Part I & II)》专项课程。 - **edX**:MIT《算法设计与分析》系列课程。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值