《算法导论》读书笔记 附录A习题解答
算法导论课程作业答案

算法导论课程作业答案Introduction to AlgorithmsMassachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine,Lee Wee Sun,and Charles E.Leiserson Handout10Diagnostic Test SolutionsProblem1Consider the following pseudocode:R OUTINE(n)1if n=12then return13else return n+R OUTINE(n?1)(a)Give a one-sentence description of what R OUTINE(n)does.(Remember,don’t guess.) Solution:The routine gives the sum from1to n.(b)Give a precondition for the routine to work correctly.Solution:The value n must be greater than0;otherwise,the routine loops forever.(c)Give a one-sentence description of a faster implementation of the same routine. Solution:Return the value n(n+1)/2.Problem2Give a short(1–2-sentence)description of each of the following data structures:(a)FIFO queueSolution:A dynamic set where the element removed is always the one that has been in the set for the longest time.(b)Priority queueSolution:A dynamic set where each element has anassociated priority value.The element removed is the element with the highest(or lowest)priority.(c)Hash tableSolution:A dynamic set where the location of an element is computed using a function of the ele ment’s key.Problem3UsingΘ-notation,describe the worst-case running time of the best algorithm that you know for each of the following:(a)Finding an element in a sorted array.Solution:Θ(log n)(b)Finding an element in a sorted linked-list.Solution:Θ(n)(c)Inserting an element in a sorted array,once the position is found.Solution:Θ(n)(d)Inserting an element in a sorted linked-list,once the position is found.Solution:Θ(1)Problem4Describe an algorithm that locates the?rst occurrence of the largest element in a?nite list of integers,where the integers are not necessarily distinct.What is the worst-case running time of your algorithm?Solution:Idea is as follows:go through list,keeping track of the largest element found so far and its index.Update whenever necessary.Running time isΘ(n).Problem5How does the height h of a balanced binary search tree relate to the number of nodes n in the tree? Solution:h=O(lg n) Problem 6Does an undirected graph with 5vertices,each of degree 3,exist?If so,draw such a graph.If not,explain why no such graph exists.Solution:No such graph exists by the Handshaking Lemma.Every edge adds 2to the sum of the degrees.Consequently,the sum of the degrees must be even.Problem 7It is known that if a solution to Problem A exists,then a solution to Problem B exists also.(a)Professor Goldbach has just produced a 1,000-page proof that Problem A is unsolvable.If his proof turns out to be valid,can we conclude that Problem B is also unsolvable?Answer yes or no (or don’t know).Solution:No(b)Professor Wiles has just produced a 10,000-page proof that Problem B is unsolvable.If the proof turns out to be valid,can we conclude that problem A is unsolvable as well?Answer yes or no (or don’t know).Solution:YesProblem 8Consider the following statement:If 5points are placed anywhere on or inside a unit square,then there must exist two that are no more than √2/2units apart.Here are two attempts to prove this statement.Proof (a):Place 4of the points on the vertices of the square;that way they are maximally sepa-rated from one another.The 5th point must then lie within √2/2units of one of the other points,since the furthest from the corners it can be is the center,which is exactly √2/2units fromeach of the four corners.Proof (b):Partition the square into 4squares,each with a side of 1/2unit.If any two points areon or inside one of these smaller squares,the distance between these two points will be at most √2/2units.Since there are 5points and only 4squares,at least two points must fall on or inside one of the smaller squares,giving a set of points that are no more than √2/2apart.Which of the proofs are correct:(a),(b),both,or neither (or don’t know)?Solution:(b)onlyProblem9Give an inductive proof of the following statement:For every natural number n>3,we have n!>2n.Solution:Base case:True for n=4.Inductive step:Assume n!>2n.Then,multiplying both sides by(n+1),we get(n+1)n!> (n+1)2n>2?2n=2n+1.Problem10We want to line up6out of10children.Which of the following expresses the number of possible line-ups?(Circle the right answer.)(a)10!/6!(b)10!/4!(c) 106(d) 104 ·6!(e)None of the above(f)Don’t knowSolution:(b),(d)are both correctProblem11A deck of52cards is shuf?ed thoroughly.What is the probability that the4aces are all next to each other?(Circle theright answer.)(a)4!49!/52!(b)1/52!(c)4!/52!(d)4!48!/52!(e)None of the above(f)Don’t knowSolution:(a)Problem12The weather forecaster says that the probability of rain on Saturday is25%and that the probability of rain on Sunday is25%.Consider the following statement:The probability of rain during the weekend is50%.Which of the following best describes the validity of this statement?(a)If the two events(rain on Sat/rain on Sun)are independent,then we can add up the twoprobabilities,and the statement is true.Without independence,we can’t tell.(b)True,whether the two events are independent or not.(c)If the events are independent,the statement is false,because the the probability of no rainduring the weekend is9/16.If they are not independent,we can’t tell.(d)False,no matter what.(e)None of the above.(f)Don’t know.Solution:(c)Problem13A player throws darts at a target.On each trial,independentlyof the other trials,he hits the bull’s-eye with probability1/4.How many times should he throw so that his probability is75%of hitting the bull’s-eye at least once?(a)3(b)4(c)5(d)75%can’t be achieved.(e)Don’t know.Solution:(c),assuming that we want the probability to be≥0.75,not necessarily exactly0.75.Problem14Let X be an indicator random variable.Which of the following statements are true?(Circle all that apply.)(a)Pr{X=0}=Pr{X=1}=1/2(b)Pr{X=1}=E[X](c)E[X]=E[X2](d)E[X]=(E[X])2Solution:(b)and(c)only。
《算法导论》读书笔记

哨兵(sentinel) 哨兵是一个哑对象,其作用是简化边界条件的处理。 类似于以前学习时讲到的头节点。加入哨兵将原来的双向链表转变成一个有哨兵的双向循环两表。L.nil代表哨兵。一图胜千言,如下:
如果有很多个很短的链表,慎用哨兵,因为哨兵所占用的额外存储空间会造成严重的存储浪费。哨兵并没有优化太多的渐进时间界,只是可 以使代码更紧凑简洁。 指针和对象的实现 对象的多数组表示: next和prev中的数字指的是对应数字的下标,有趣!
关于几个时间复杂度
通常情况下,当数据量足够大时,一般满足 θ(1)>θ(N)>θ(NlogN)>θ(N^2) (>代表优于)
1.算 法 基 础
1.1 插 入 排 序
时间复杂度:O(N^2) 思想:每次从右至左跟已排序数列进行对比,放入合适位置。两次遍历,一次相当于摸牌,另一次相当于具体的查找算法。
1.2 分 治 法
解决冲突的方法
链接法(chaining)
关于链接法采用双链的一些解释: 简单讲,删除就是从x对应的链表里删除x,双链表的删除加入哨兵只需两行,但是单链表的话只能指定x.next=null,但是在这之前需 要先将x.prev.next指向x.next,由于是单链,所以没有prev,只能一直next找下去,相比双链多了查找的时间耗费。
将问题分解为几个规模较小但类似于原问题的子问题,递归的求解这些子问题,然后再合并这些子问题的解来建立原问题的解。 (分解-解决-合并) 归并排序 时间复杂度O(NlogN)(O还是θ——theta,都差不多)
归并排序算法如下:
函数的增长
O渐进上界Ω渐进下界,o和ω 具体看数学定义最清楚。
分治策略
1. 维护堆的性质 通俗的讲,就是保持数组的最大堆性质。思路比较简单,比较parent节点和孩子节点,找到最大值,如果最大值是某个孩子节点,交 换,递归运行。对于树高为h的节点来说,该程序时间复杂度为O(h)
《算法导论》习题答案12、13、14章

第9章 中位数和顺序统计学
9.3-2
大于x的数至少有3n/10-6, n≥140时,易证3n/10-6 ≥n/4 小于x的数同理。
9.3-4
通过比较得到第i小元素,每次保留比较信息。 在比较过程中比这个元素小的元素构成的集合即为i – 1个 小数集合,而比较过程中比这个元素大的元素则构成了n – i 个大元素集合。不需要增加比较次数。
Preprocessing(A,k) for i←0 to k do C[i]←0 for j←1 to length[A] do C[A[j]] ←C[A[j]]+1 for i←1 to k do C[i] ←C[i]+C[i-1] Query(C,k,a,b) if b<a or b<1 or a>k return 0 if a<1 then a=1 if b>k then b=k if a≠1 then return C[b]-C[a-1] else return C[b]
0 +1
k +1
k +1
( k +1) +1
第6章 堆排序
6.4-3 不论递增还是递减,时间均为O(nlgn) 6.4-4 最坏情况下,n-1次调用MAX-HEAPIFY,运 行时间为O(nlgn)
第6章 堆排序
6.5-3
HEAP-MINIMUM(A) if heap-size[A]<1 then error”heap underflow” else return A[1] HEAP-EXTRACT-MIN(A) if heap-size[A]<1 then error”heap underflow” min<-A[1] A[1]<-A[heap-size[A]] heap-size[A]<-heap-size[A]-1 MIN-HEAPIFY(A,1) return min HEAP-DECREASE-KEY(A,i,key) if key>A[i] then error A[i]<-key while i>1 and A[PARENT(i)>A[i] do exchange A[i]<->A[PARENT(i)] i<-PARENT(i) MIN-HEAP-INSERT(A,key) heap-size[A]<-heap-size[A]+1 A[heap-size[A]]<-+∞ HEAP-DECREASE-KEY(A,heap-size[A],key)
算法导论 答案 (2)

算法导论答案算法导论概述《算法导论》是一本经典的计算机科学教材,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein合著。
这本书详细介绍了算法的设计、分析和实现,并涵盖了算法导论领域的许多重要概念和技术。
本文将为你提供一些关于《算法导论》中一些常见问题的答案。
1. 什么是算法?算法是一系列明确定义的步骤,用于解决特定问题或完成特定任务。
它可以是一个计算过程、一个程序或一个有限的操作序列。
算法通常用于计算和数据处理领域,是计算机科学的核心概念。
2. 为什么学习算法很重要?学习算法的重要性体现在以下几个方面:•提高问题解决能力:算法是解决问题的有效工具。
学习算法可以帮助我们思考和理解问题,并设计出相应的解决方案。
•优化计算性能:算法的设计和分析可以帮助我们提高计算的效率和性能。
合适的算法可以在短时间内处理大规模数据集和复杂计算任务。
•促进技术创新:算法是许多技术和应用的基石,包括人工智能、机器学习、数据挖掘等。
学习算法可以为我们打开更多的研究和创新机会。
3. 《算法导论》提供了哪些内容?《算法导论》这本书详细介绍了算法的基本概念和设计技巧,并提供了许多典型算法的实现和分析。
以下是该书的一些主要内容:•算法分析:对算法进行时间复杂度和空间复杂度的理论分析,帮助我们评估算法的效率和性能。
•排序和查找算法:介绍了各种排序算法(如插入排序、归并排序、快速排序)和查找算法(如二分查找、哈希表)。
•图算法:讨论了图的表示方法和图搜索算法(如深度优先搜索、广度优先搜索)以及最短路径算法(如Dijkstra算法)等。
•动态规划和贪心算法:介绍了动态规划和贪心算法的原理和应用,用于解决具有最优子结构性质的问题。
•分治算法和递归思想:讲解了分治算法的基本原理,并提供了许多使用递归思想解决问题的例子。
•NP完全问题:探讨了NP完全问题的性质和求解方法,引导了读者进入计算复杂性理论的领域。
算法导论读书笔记

算法导论读书笔记【篇一:《算法概论》读书笔记及读后感】《算法概论》读书笔记12计转1 12130907 李酉辰第0章本章较为简短,没有深入系统地涉及某些内容。
主要以fibonacci 数列的例子,让我体会了递归和递推思想的差别。
针对fibonacci数列例子直接递归解法中涉及的重复计算,优化出递推方式,展示了思考问题中自顶向下与自底向上的不同思考角度可能产生较大的算法效率差别,同时隐约体现记忆化搜索的思想。
另外本章较为详细介绍了大o复杂度度量标准。
第1章本章以rsa算法为例,细致深入讨论了rsa算法涉及的相关数论知识,诸如取模运算、模下的四则运算与逆元概念、取模幂运算、素性检测。
在素性检测部分有经典的欧几里德算法、扩展欧几里德算法,同时引入随机化算法概念,以极高的概率保证素性检测有效性。
通过本章的学习,我对过去不曾深入考虑或者说真正考虑的基础性运算有了更深的理解。
之前对乘除运算复杂度总是在以单元操作的概念下以o(1)带过,以后会更加细致地考虑乘除等基本运算的复杂度。
另外,本章以rsa为案例,系统地展示了针对某一问题,如何从基础性知识入手,一步一步学习案例所需基础知识,并将其整合从而解决案例。
素性检测与素因子分解,两个看似相去不远的问题,其复杂性天差地别的现实,从一般角度让人们想到的是类似问题的解决难度可能差别很大仅此而已,而rsa算法展示了如何深入的多想一步,利用这种情况设计出优雅的解决方案。
这思想很值得我借鉴与利用。
第2章本章介绍分治算法思想,提及分治,相信每一个学习算法的人都不会陌生,经典的《算法导论》中就已合并排序为例在开篇不久就引入分治概念。
本书介绍分治的角度与众不同,不似《导论》中总是介绍比较显而易见的可以分治的案例。
本书列举了矩阵相乘、快速傅立叶变换等数学领域分治的应用案例,在这些案例之中,分治的应用很多情况下隐藏的较为深,并非显而易见,加大了分析难度。
但是更能让我感受到分治应用之广泛,可能在学习本章之前,许多类型的题目我不会想到去向分治的角度思考,因为不易看出,但是本章给我的备忘录上加了一条:永远不要忽视分治,针对陌生题目,不要轻易就否决掉往分治角度思考的路线。
《算法导论》读书笔记 附录A习题解答

A.1-1求的简化公式。
利用等差级数求和公式和级数线性性质:
A.1-2利用调和级数性质证明。
利用调和级数性质:
A.1-3对,证明。
对无穷递减几何级数式两边求导,再乘以:
对该式再进行同上操作得到:
A.1-4 求。
A.1-5 求的值。
当时求得
当时:
计算得到:
A.1-6 利用求和公式的线性特征证明。
令,则下式显然成立:
再把函数代换回即可。
A.1-7 求的值。
A.1-8 求的值。
A.2-1 证明有常量上界。
A.2-2 求和的渐近上界。
故渐近上界是
A.2-3 通过分割求和证明第个调和数是。
故取得下界
A.2-4 通过积分求的近似值。
A.2-5 题略。
为了保证被积函数在积分域上都连续。
思考题
A-1 求和的界
求下列和式的渐近确界。
假设,都是常量。
a)
,得到确界为
b)
根据此式得到上界:
故得到下界:
故据此得到确界
c)
故得到上界:
故得到下界:
因此得到确界。
《算法导论(第二版)》(中文版)课后答案

17.3-1
11
《算法导论(第二版) 》参考答案
17.3-4
17.4-3 假设第 i 个操作是 TABLE_DELETE, 考虑装载因子 : i =(第 i 次循环之后的表 中的 entry 数)/(第 i 次循环后的表的大小)= numi / sizei
19.1-1. If x is not a root node, then Degree[x]=Degree[sibling[x]]+1 If x is a root node, then Degree[x]<Degree[sibling[x]] 19.1-2
13.1-5 prove:
3
《算法导论(第二版) 》参考答案 13.1-6 2k-1 22k-1 13.2-3 13.3-5
13.4-3
4
《算法导论(第二版) 》参考答案
14.1-4
14.2-2
14.2-3 不可以,性能改变 时间复杂度由 O( lgn ) -> O( nlgn )
14.3-2 Note: 注意 Overlap 的定义稍有不同,需要重新定义。 算法:只要将 P314 页第三行的 改成>就行。 14.3-3 INTERVAL-SEARCH-SUBTREE(x, i) 1 while x ≠ nil[T] and i does not overlap int[x] 2 do if left[x] ≠ nil[T] and max[left[x]] ≥ low[i] 3 then x ← left[x] 4 else x ← right[x] 5 return x INTERVAL-SEARCH-MIN(T, i) 2 y←INTERVAL-SEARCH-SUBTREE(root[T], i) 先找第一个重叠区间 3 z←y 4 while y≠ nil[T] and left[y] ≠ nil[T] 在它的左子树上查找
算法导论读书笔记

算法导论读书笔记第1章:算法在计算中的作用1.算法即是一系列的计算步骤,用来将一个有效的输入转换成一个有效的输出。
2.计算机的有限的资源必须被有效的利用,算法就是来解决这些问题的方法。
第2章:算法入门1、循环不变式的三个性质:(循环不变式通常用来证明递归的正确性)(1)初始化:它在循环的第一轮迭代开始之前,应该是正确的。
(2)保持:如果在循环的某一次迭代开始之前它是正确的,那么,在下一次迭代开始之前,它也应该保持正确。
(3)终止:当循环结束时,不变式给了我们一个有用的性质,它有助于表明算法是正确的。
2、分治策略:将原问题划分成n个规模较小而结构与原问题相似的子问题;递归地解决这些小问题,然后再合并其结果,就得到原问题的解。
3、分治模式在每一-层递归上都有三个步骤:(1)分解(Divde):将原问题分解成-系列子问题;(2)解决(Conquer):递归地解答各子问题。
若子问题足够小,则直接求解;(3)合并(Combine):将子问题的结果合并成原问题的解。
第3章:函数的增长1.对几个记号的大意:。
(非渐近紧确上界)≈<;0(渐近上界)≈≤;日(渐近紧界)≈=;2(渐近下界)≈≥;w(非渐近紧确下界)≈>;这里的<,s,=,2,>指的是规模上的比较,即o(n))的规模比g(n)小。
o(g())={f(n):对任意正常数C,存在常数n.>0,使对所有的n≥n,有0≤f(n)<cg(n)}O(g(n))={f(n):存在正常数c和n。
,使对所有n≥n。
,有0S(n)≤cg(n)}0(g(n)={f(n):存在正常数Cc.c和n。
,使对所有的n≥n,有0≤c,g(n)Sf(n)≤cg(n)}Q(g(n))={f(n):存在正常数c和n。
,使对所有n≥n,有0≤cg(n)Sf(n)}w(g())={f(n):对任意正常数c,存在常数n,>0.使对所有的n,有0≤cg(n)<f(n)}第4章:递归式1.递归式是一组等式或不等式,它所描述的函数是用在更小的输入下该函数的值来定义的。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
A.1-1求的简化公式。
利用等差级数求和公式和级数线性性质:
A.1-2利用调和级数性质证明。
利用调和级数性质:
A.1-3对,证明。
对无穷递减几何级数式两边求导,再乘以:
对该式再进行同上操作得到:
A.1-4 求。
A.1-5 求的值。
当时求得
当时:
计算得到:
A.1-6 利用求和公式的线性特征证明。
令,则下式显然成立:
再把函数代换回即可。
A.1-7 求的值。
A.1-8 求的值。
A.2-1 证明有常量上界。
A.2-2 求和的渐近上界。
故渐近上界是
A.2-3 通过分割求和证明第个调和数是。
故取得下界
A.2-4 通过积分求的近似值。
A.2-5 题略。
为了保证被积函数在积分域上都连续。
思考题
A-1 求和的界
求下列和式的渐近确界。
假设,都是常量。
a)
,得到确界为
b)
根据此式得到上界:
故得到下界:
故据此得到确界
c)
故得到上界:
故得到下界:
因此得到确界。