算法分析(MIT教材) 英文版

合集下载

Algorithms Chapter 1 绪论

Algorithms Chapter 1 绪论

怎么处理?
30
The Design and Analysis of Algorithms
Chapter 1 Introduction to Algorithms
What’s an Algorithm?
算法是一系列解决问题的清晰指令,也就是说,能够对 一定规范的输入,在有限时间内获得所要求的输出。
16
The Design and Analysis of Algorithms
算法可以解决哪些问题
找出人类DNA中所有100000种基因,确定构成人类DNA的30亿种化学基 17 对的各种序列。
The Design and Analysis of Algorithms
算法可以解决哪些问题
快速访问和检索互联网数据
The Design and Analysis of Algorithms
例子
• “贝格尔”编排法(Beiger Arrangement) 把参赛队数分一半(参赛队为单数时,最后以“0” 表示形成双数),前一半由1号开始,自上而下写在 左边;后一半的数自下而上写在右边,然后用横线 把相对的号数连接起来。这即是第一轮的比赛。 第二轮将第一轮右上角的编号(“0”或最大的一个代 号数)移到左角上,第三轮又移到右角上,以此类推。 即单数轮次时“0”或最大的一个代号在右上角,双 数轮次时则在左上角。
//使用欧几里得算法计算gcd(m,n) //输入:两个不全为0的非负整数m,n //输出:m,n的最大公约数
28
The Design and Analysis of Algorithms
例子
• “贝格尔”编排法(Beiger Arrangement)
这种编排方法是否完美?

Introduction_to_Algorithms

Introduction_to_Algorithms

和式的界
思考题 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
第一部分 基础知识
2 在学习本篇的内容时,我们建议读者不必一次将这些数学内容全部消化。先浏览 一下这部分的各章,看看它们包含哪些内容,然后直接去读集中谈算法的章节。在阅 读这些章节时,如果需要对算法分析中所用到的数学工具有个更好的理解的话,再回 过头来看这部分。当然,读者也可顺序地学习这几章,以便很好地掌握有关的数学技 巧。 本篇各章的内容安排如下: • 第一章介绍本书将用到的算法分析和设计的框架。 • 第二章精确定义了几种渐近记号,其目的是使读者采用的记号与本书中的一致, 而不在于向读者介绍新的数学概念。 • 第三章给出了对和式求值和限界的方法,这在算法分析中是常常会遇到的。 • 第四章将给出求解递归式的几种方法。我们已在第一章中用这些方法分析了合并 排序,后面还将多次用到它们。一种有效的技术是“主方法”,它可被用来解决 分治算法中出现的递归式。第四章的大部分内容花在证明主方法的正确性,读者 若不感兴趣可以略过。 • 第五章包含了有关集合、关系、函数、图和树的基本定义和记号。这一章还给出 了这些数学对象的一些基本性质。如果读者已学过离散数学课程,则可以略过这 部分内容。 • 第六章首先介绍计数的基本原则,即排列和组合等内容。这一章的其余部分包含 基本概率的定义和性质。
求和公式和性质 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 线性性质 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 算术级数 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 几何级数 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 调和级数 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 积分级数与微分级数 . . . . . . . . . . . . . . . . . . . . . . . . . . 39 套迭级数 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 积 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 数学归纳法 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 对项的限界 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 分解和式 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 积分近似公式 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

机器学习经典书目汇总

机器学习经典书目汇总

机器学习经典书目汇总本文总结了机器学习的经典书籍,包括数学基础和算法理论的书籍。

入门书单《数学之美》作者吴军大家都很熟悉。

以极为通俗的语言讲述了数学在机器学习和自然语言处理等领域的应用。

《Programming Collective Intelligence》(《集体智慧编程》)作者Toby Segaran也是《BeautifulData : The Stories Behind Elegant Data Solutions》(《数据之美:解密优雅数据解决方案背后的故事》)的作者。

这本书最大的优势就是里面没有理论推导和复杂的数学公式,是很不错的入门书。

目前中文版已经脱销,对于有志于这个领域的人来说,英文的pdf是个不错的选择,因为后面有很多经典书的翻译都较差,只能看英文版,不如从这个入手。

还有,这本书适合于快速看完,因为据评论,看完一些经典的带有数学推导的书后会发现这本书什么都没讲,只是举了很多例子而已。

《Algorithms of the Intelligent Web》(《智能web算法》)作者Haralambos Marmanis、Dmitry Babenko。

这本书中的公式比《集体智慧编程》要略多一点,里面的例子多是互联网上的应用,看名字就知道。

不足的地方在于里面的配套代码是BeanShell而不是python或其他。

总起来说,这本书还是适合初学者,与上一本一样需要快速读完,如果读完上一本的话,这一本可以不必细看代码,了解算法主要思想就行了。

《统计学习方法》作者李航,是国内机器学习领域的几个大家之一,曾在MSRA 任高级研究员,现在华为诺亚方舟实验室。

书中写了十个算法,每个算法的介绍都很干脆,直接上公式,是彻头彻尾的“干货书”。

每章末尾的参考文献也方便了想深入理解算法的童鞋直接查到经典论文;本书可以与上面两本书互为辅助阅读。

《Machine Learning》(《机器学习》)作者Tom Mitchell是CMU的大师,有机器学习和半监督学习的网络课程视频。

算法分析与设计

算法分析与设计

算法与数据结构
描述算法可以有多种方式
自然语言方式、表格方式、图示形式等 本书采用C++语言描述算法
算法与数据结构的关系
不了解施加于数据上的算法就无法决定如何构造 数据,可以说算法是数据结构的灵魂;
反之算法的结构和选择又常常在很大程度上依赖 于数据结构,数据结构则是算法的基础。
算法+数据结构=程序
无序表的数组占用的空间记在上层程序 的帐上.
例题2.3
实例特征:n,S(n)=0
Program Program 1.8 Add a[0:n-1]
例题2.4
实例特征:n ,递归栈需6 bytes S(n)=6(n+1)bytes
Program 1.9 Recursive code to add a[0:n-1]
例题2.2
实例特征:n,S(n)=0
Program 2.1 Sequential search
例题2.2(续)
T a[] 和T& x需2 bytes 指针(假定T为整 型)
形参 n 需2 bytes i 需2 bytes 以上均为实例特征独立的空间需求量
所以s(n)=0 注:上述分析是从程序调用的角度看,存放
Sartaj Sahni著,汪诗林等译,《数据结构、算法与应用--C++ 语言描述》,机械工业出版社,2003 (翻译版)
T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms (the second edition),The MIT Press,2001《算法导论(第二版)》(影印版,中文本),高 等教育出版社,2003

MIT麻省理工学院 算法导论公开课 Problem Set 4 solution

MIT麻省理工学院 算法导论公开课 Problem Set 4 solution

Introduction to Algorithms October 29, 2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik D. Demaine and Charles E. Leiserson Handout 18Problem Set 4 SolutionsProblem 4-1. TreapsIf we insert a set of n items into a binary search tree using T REE-I NSERT, the resulting tree may be horribly unbalanced. As we saw in class, however, we expect randomly built binary search trees to be balanced. (Precisely, a randomly built binary search tree has expected height O(lg n).) Therefore, if we want to build an expected balanced tree for a fixed set of items, we could randomly permute the items and then insert them in that order into the tree.What if we do not have all the items at once? If we receive the items one at a time, can we still randomly build a binary search tree out of them?We will examine a data structure that answers this question in the affirmative. A treap is a binary search tree with a modified way of ordering the nodes. Figure 1 shows an example of a treap. As usual, each item x in the tree has a key key[x]. In addition, we assign priority[x], which is a random number chosen independently for each x. We assume that all priorities are distinct and also that all keys are distinct. The nodes of the treap are ordered so that (1) the keys obey the binary-search-tree property and (2) the priorities obey the min-heap order property. In other words,•if v is a left child of u, then key[v]<key[u];•if v is a right child of u, then key[v]>key[u]; and•if v is a child of u, then priority(v)>priority(u).(This combination of properties is why the tree is called a “treap”: it has features of both a binary search tree and a heap.)Figure 1: A treap. Each node x is labeled with key[x]:p riority[x]. For example, the root has key G and priority 4.It helps to think of treaps in the following way. Suppose that we insert nodes x1,x2,...,x n, each with an associated key, into a treap in arbitrary order. Then the resulting treap is the tree that wouldhave been formed if the nodes had been inserted into a normal binary search tree in the order given by their (randomly chosen) priorities. In other words, priority[x i]<priority[x j]means that x i is effectively inserted before x j.(a) Given a set of nodes x1,x2,...,x n with keys and priorities all distinct, show that thereis a unique treap with these nodes.Solution:Prove by induction on the number of nodes in the tree. The base case is a tree withzero nodes, which is trivially unique. Assume for induction that treaps with k−1orfewer nodes are unique. We prove that a treap with k nodes is unique. In this treap, theitem x with minimum priority must be at the root. The left subtree has items with keys<key[x]and the right subtree has items with keys >key[x]. This uniquely defines theroot and both subtrees of the root. Each subtree is a treap of size ≤k−1, so they areunique by induction.Alternatively, one can also prove this by considering a treap in which nodes are in­serted in order of their priority. Assume for induction that the treap with the k−1nodes with smallest priority is unique. For k=0t his is trivially true. Now considerthe treap with the k nodes with smallest priority. Since we know that the structureof a treap is the same as the structure of a binary search tree in which the keys areinserted in increasing priority order, the treap with the k nodes with smallest priorityis the same as the treap with the k−1nodes with smallest priority after inserting thek-th node. Since BST insert is a deterministic algorithm, there is only one place thek-th node could be inserted. Therefore the treap with k nodes is also unique, provingthe inductive hypothesis.(b) Show that the expected height of a treap is O(lg n), and hence the expected time tosearch for a value in the treap is O(lg n).Solution: The idea is to realize that a treap on n nodes is equivalent to a randomlybuilt binary search tree on n nodes. Recall that assigning priorities to nodes as theyare inserted into the treap is the same as inserting the n nodes in the increasing orderdefined by their priorities. So if we assign the priorities randomly, we get a randomorder of n priorities, which is the same as a random permutation of the n inputs, sowe can view this as inserting the n items in random order.The time to search for an item is O(h)where h is the height of the tree. As we saw inlecture, E[h]=O(lg n). (The expectation is taken over permutations of the n nodes,i.e., the random choices of the priorities.)Let us see how to insert a new node x into an existing treap. The first thing we do is assign x a random priority priority[x]. Then we call the insertion algorithm, which we call T REAP-I NSERT, whose operation is illustrated in Figure 2.(e) (f)Figure 2: Operation of T REAP-I NSERT. As in Figure 1, each node x is labeled with key[x]: priority[x]. (a) Original treap prior to insertion. (b) The treap after inserting a node with key C and priority 25. (c)–(d) Intermediate stages when inserting a node with key D and priority 9.(e) The treap after insertion of parts (c) and (d) is done. (f) The treap after inserting a node with key F and priority 2.(c) Explain how T REAP-I NSERT works. Explain the idea in English and give pseudocode.(Hint: Execute the usual binary search tree insert and then perform rotations to restorethe min-heap order property.)Solution: The hint gives the idea: do the usual binary search tree insert and thenperform rotations to restore the min-heap order property.T REAP-I NSERT (T,x)inserts x into the treap T(by modifying T). It requires that xhas defined key and priority values. We have used the subroutines T REE-I NSERT,R IGHT-R OTATE, and R IGHT-R OTATE as defined in CLRS.T REAP-I NSERT (T,x)1T REE-I NSERT (T,x)2 while x =root[T]and priority[x]<priority[p[x]]3 do if x=left[p[x]]4 then R IGHT-R OTATE (T,p[x])5 else L EFT-R OTATE (T,p[x])Note that parent pointers simplify the code but are not necessary. Since we only needto know the parent of each node on the path from the root to x(after the call toT REE-I NSERT), we can keep track of these ourselves.(d) Show that the expected running time of T REAP-I NSERT is O(lg n). Solution:T REAP-I NSERT first inserts an item in the tree using the normal binary search treeinsert and then performs a number of rotations to restore the min-heap property.The normal binary-search-tree insertion algorithm T REE-I NSERT always places thenew item at a new leaf of tree. Therefore the time to insert an item into a treap isproportional to the height of a randomly built binary search tree, which as we saw inlecture is O(lg n)in expectation.The maximum number of rotations occurs when the new item receives a priority lessthan all priorities in the tree. In this case it needs to be rotated from a leaf to the root.An upper bound on the number of rotations is therefore the height of a randomly builtbinary search tree, which is O(lg n)in expectation. (We will see that this is a fairlyloose bound.) Because each rotation take constant time, the expected time to rotate isO(lg n).Thus the expected running time of T REAP-I NSERT is O(lg n+lg n)=O(lg n).T REAP-I NSERT performs a search and then a sequence of rotations. Although searching and ro­tating have the same asymptotic running time, they have different costs in practice. A search reads information from the treap without modifying it, while a rotation changes parent and child pointers within the treap. On most computers, read operations are much faster than write operations. Thus we would like T REAP-I NSERT to perform few rotations. We will show that the expected number of rotations performed is bounded by a constant (in fact, less than 2)!(a) (b)Figure 3: Spines of a binary search tree. The left spine is shaded in (a), and the right spine is shaded in (b).In order to show this property, we need some definitions, illustrated in Figure 3. The left spine of a binary search tree T is the path which runs from the root to the item with the smallest key. In other words, the left spine is the maximal path from the root that consists only of left edges. Symmetrically, the right spine of T is the maximal path from the root consisting only of right edges. The length of a spine is the number of nodes it contains.(e) Consider the treap T immediately after x is inserted using T REAP-I NSERT. Let Cbe the length of the right spine of the left subtree of x. Let D be the length of theleft spine of the right subtree of x. Prove that the total number of rotations that wereperformed during the insertion of x is equal to C+D.Solution: Prove the claim by induction on the number of rotations performed. Thebase case is when x is the parent of y. Performing the rotation so that y is the new rootgives y exactly one child, so C+D=1.Assume for induction that the number of rotations k performed during the insertionof x equals C+D. The base case is when 0 rotations are necessary and x is insertedas a leaf. Then C+D=0. To prove the inductive step, we show that if after k−1rotations C+D=k−1, then after k rotations C+D=k. Draw a picture of aleft and right rotation and observe that C+D increases by 1 in each case. Let y bethe parent of x, and suppose x is a left child of y. After performing a right rotation, ybecomes the right child of x, and the previous right child of x becomes the left childof y. That is, the left spine of the right subtree of x before the rotation is tacked onto y, so the length of that spine increases by one. The left subtree of x is unaffectedby a right rotation. The case of a left rotation is symmetric. Therefore after one morerotation C+D increases by one and k=C+D, proving the inductive hypothesis.We will now calculate the expected values of C and D. For simplicity, we assume that the keys are 1,2,...,n. This assumption is without loss of generality because we only compare keys.�For two distinct nodes x and y , let k = key [x ]and i = key [y ], and define the indicator random variableX 1 if y is a node on the right spine of the left subtree of x (in T ),i,k =0 otherwise .(f) Show that X i,k =1if and only if (1) priority [y ]> priority [x ], (2) key [y ]< key [x ], and(3) for every z such that key [y ]< key [z ]< key [x ],we have p riority [y ]< priority [z ].Solution:To prove this statement, we must prove both directions of the “if and only if”. Firstwe prove the “if” direction. We prove that if (1) priority [y ]> priority [x ], (2) key [y ]<key [x ], and (3) for every z such that key [y ]< key [z ]< key [x ]are true, priority [y ]< priority [z ], then X i,k =1. We prove this by contradiction. Assume that X i,k =0. That is, assume that y is not on the right spine of the left subtree of x . We show thatthis leads to a contradiction. If y is not on the right spine of the left subtree of x ,it could be in one of three places:1. Suppose y is in the right subtree of x . This contradicts condition (2) becausekey [y ]< key [x ].2. Suppose y is not in one of the subtrees of x . Then x and y must share somecommon ancestor z . Since key [y ]< key [x ], we know that y is in the left subtreeof z and x is in the right subtree of z and key [y ]< key [z ] < key [x ]. Since y isbelow z in the tree, priority [z ]< priority [x ]and priority [z ]< priority [y ]. Thiscontradicts condition (3).3. Suppose that y is in the left subtree of x but not on the right spine of the leftsubtree of x . This implies that there exists some ancestor z of y in the left subtreeof x such that y is in the left subtree of z . Hence key [y ]< key [z ]< key [x ]. Sincez is an ancestor of y , priority [z ]< priority [y ], which contradicts condition (3).All possible cases lead to contradictions, and so X i,k =1.Now for the “only if” part. We prove that if X i,k =1, then statements (1), (2), and (3) are true. If X i,k =1, then y is in the right spine of the left subtree of x . Since y is ina subtree of x , y must have been inserted after x ,so p riority [y ]> priority [x ], proving(1). Since y is in the left subtree of x , key [y ]< key [x ], proving (2). We prove (3) by contradiction: suppose that X i,k =1and there exists a z such that key [y ]< key [z ]< key [x ]and priority [z ]< priority [y ]. In other words, z was inserted before y . There arethree possible cases that satisfy the condition key [z ]< key [x ]:1. Suppose z is in the right spine of the left subtree of x .For y to be inserted into theright spine of the left subtree of x , it will have to be inserted into the right subtreeof z . Since key [y ]< key [z ], this leads to a contradiction.2. Suppose z is in the left subtree of x but not in the right spine. This implies thatz is in the left subtree of some node z in the right spine of x . Therefore for y tobe inserted into the right spine of the left subtree of x , it must be inserted into theright subtree of z . This leads to a contradiction by reasoning similar to case 1.3. Suppose that z is not in one of the subtrees of x . Then z and x have a commonancestor z such that z is in the left subtree of z and x is in the right subtree of x .This implies key [z ] < key [z ] < key [x ]. Since key [y ]< key [z ]< key [z ], y cannotbe inserted into the right subtree of z . Therefore it cannot be inserted in a subtreeof x , which is a contradiction.Therefore there can be no z such that key [y ] < key [z ] < key [x ] and priority [z ] < priority [y ]. This proves statement (3). We have proven both the “if” and “only if” directions, proving the claim.(g) Show that(k − i − 1)! 1 Pr {X i,k =1} == . (k − i +1)! (k − i +1)(k − i )Solution: We showed in the previous part that X i,k =1if and only if the priorities of the items between y and x are ordered in a certain way. Since all orderings are equally likely, to calculate the probability we count the number of permutations of priorities that obey this order and divide by the number of total number of priority permutations. We proved in (e) that whether or not X i,k =1depends only on the relative ordering of the priorities of y , x , and all z such that key [y ] < key [z ] < key [x ]. Since we assumed that the keys of the items come from {1,...,n }, the keys of the items in question are i,i +1,i +2,...,k − 1,k . There are (k − i +1)!permutations of the priorities of these items. Of these permutations, the ones for which X i,k =1are those where i has minimum priority, k has the second smallest priority, and the priorities of the remaining k − i − 1 items follow in any order. There are (k − i − 1)! of these permutations. Thus the probability that we get a “bad” order is (k −i −1)!/(k −i +1)!= 1/(k − i )(k − i +1).(h) Show thatk −1 � 1 1 E [C ]= =1− . j (j +1)k j =1 Solution:X For a node x with key k , E [C ]is the expected number of nodes in the right spine of the left subtree of x . This equals the sum of the expected value of the random variables i,k for all i in the tree. Since X i,k =0for all nodes i ≥ k , we only need to consider i <k .� � � k −1 �k −1 � E [C ]=E [X i,k ]=E X i,k i =1 i =1 k −1 =Pr {X i,k =1}i =1 k −1 � 1 =(k − i )(k − i +1) i =1 k −1 � 1= j (j +1)j =1 1To simplify this sum, observe that j (j 1+1) = j +1−j = − 1 . Therefore the sumj (j +1) j j +1 telescopes and we have 1 E [C ]=1− . kIf you didn’t see this, you could have proven that the equationk −1 � 1 1 =1− j (j +1)k j =1 holds by induction on k . In the proving the inductive hypothesis you might have 11discovered 1 − = .j j +1 j (j +1)(i) Use a symmetry argument to show that1 E [D ]=1− . n − k +1Solution: The idea is to consider the treap produced if the ordering relationship amongthe keys is reversed. That is, for all items x , leave priority [x ]unchanged but replacekey [x ]with n − key [x ]+1.Let T be the binary tree we get when inserting the items (in increasing order of pri­ority) using the original keys. Once we remap the keys and insert them into a newbinary search tree, we get a tree T whose shape is the mirror image of the shape ofT . (reflected left to right). Consider the item x with key k in T and therefore has key n − k +1 i n T . The length of the left spine of x ’s right subtree in T has becomethe length of the right spine of x ’s left subtree in T . We know by part (g) that the expected length of the right spine of a left subtree of a node y is 1− 1/idkey [y ],so the expected length of the right spine of the left subtree of x in T is 1− 1/(n − k +1), which means that 1 E [D ]=1− . n − k +1(j) Conclude that the expected number of rotations performed when inserting a node into a treap is less than 2.Solution:11 E [number of rotations ]= E [C +D ]=E [C ]+E [D ]=1− +1− k n − k +1 ≤ 1+1=2. Problem 4-2. Being balancedCall a family of trees balanced if every tree in the family has height O (lg n ), where n is the number of nodes in the tree. (Recall that the height of a tree is the maximum number of edges along any path from the root of the tree to a leaf of the tree. In particular, the height of a tree with just one node is 0.)For each property below, determine whether the family of binary trees satisfying that property is balanced. If you answer is “no”, provide a counterexample. If your answer is “yes”, give a proof (hint: it should be a proof by induction). Remember that being balanced is an asymptotic property, so your counterexamples must specify an infinite set of trees in the family, not just one tree. (a) Every node of the tree is either a leaf or it has two children.Solution: No. Counterexample is a right chain, with each node having a leaf hanging off to the left(b) The size of each subtree can be written as 2k − 1, where k is an integer (k is not the same for each subtree).Solution: Yes.Consider any subtree with root r . We know from the condition that the size of this subtree is 2k 1 − 1. We also know that the size of the subtree rooted at the left child of r is 2k 2 − 1, and the size of the subtree rooted at the right child of r is 2k 3 − 1. But the size of the subtree at r is simply the node r together with the nodes in the left and right subtrees. This leads to the equation 2k 1 − 1=1+(2k 2 − 1)+(2k 3 − 1),or 2k 1 =2k 2 +2k 3. Now we show that k 2 =k 3. This is easy to see if you consider the binary representations of k 1, k 2, and k 3.Otherwise, if we assume WLOG that k 2 ≤ k 3, then we have 2k 1−k 2 =1+2k 3−k 2. 2Now, the only pair of integer powers of 2 that could satisfy this equation are 21 and 0 . Thus k 3 − k 2 =0,or k 2 =k 3, and the left and right subtrees of r have an equal number of nodes. It follows that the tree is perfectly balanced.(c) There is a constant c>0such that, for each node of the tree, the size of the smallerchild subtree of this node is at least c times the size of the larger child subtree.Solution:Yes1. The proof is by induction. Assume that the two subtrees of x with n nodes in itssubtree has two children y and z with subtree sizes n1 and n2. By inductive hypothesis,the height of y’s subtree is at most d lg n1 and the height of z’s subtree is at most d lg n2for some constant d. We now prove that the height of x’s subtree is at most d lg n.Assume wlog that n1 ≥n2. Therefore, by the problem statement, we have n2 ≥cn1.Therefore, we have n=n1 +n2 +1≥(1+c)n1 +1≥(1+c)n1 and the height hof x’s subtree is d lg n1 +1≤d lg(n/(c+1))+1≤d lg n−d lg(1+c)+1≤d lg nif d lg(1+c)≥1. Therefore, for sufficiently large d, the height of a tree with n nodesis at most d lg n.(d) There is a constant c such that, for each node of the tree, the heights of its childrensubtrees differ by at most c.Solution: Yes1. Let n(h)be the minimum number of nodes that a tree of height h thatsatisfies the stated property can have. We show by induction that n(h)≥(1+α)h−1,for some constant 0<α≤1. We can then conclude that for a tree with n nodes,h≤log1+α(n+1)=O(lg n).For the base case, a subtree of height 0has a single node, and 1≥(1+α)0 −1forany constant α≤1.In the inductive step, assume for all trees of height k<h, that the n(k)≥(1+α)k−1.Now consider a tree of height h, and look at its two subtrees. We know one subtreemust have height h−1, and the other must have height at least h−1−c. Therefore,we known(h)≥n(h−1)+n(h−1−c)+1.Using the inductive hypothesis, we getn(h) ≥(1+α)h−1 −1+(1+α)h−1−c−1+1≥2(1+α)h−1−c−1.Suppose we picked αsmall enough so that (1+α)<21/(c+1). Then (1+α)c+1 <2.Therefore, we getn(h)≥2(1+α)h−1−c−1≥(1+α)h−1.1Note that in this problem we assume that a nil pointer is a subtree of size 0, and so a node with only one child has two subtrees, one of which has size 0. If you assume that a node with only one child has only one subtree, then the answer to this problem part is “no”.�11Handout18: Problem Set4SolutionsTherefore, we satisfy the inductive hypothesis.Note that if we plug this value for αback into h≤log1+α(n+1),we getlg(n+1)h≤≤(c+1)l g(n+1).lg(1+2c+1)(e) The average depth of a node is O(lg n). (Recall that the depth of a node x is thenumber of edges along the path from the root of the tree to x.)Solution: No.√Consider a tree with n−n nodes organized as a complete binary tree, and the other √ √n nodes sticking out as a chain of length n from the balanced tree. The height of√√√the tree is lg(n−n)+n=Ω(n), while the average depth of a node is at most�√√ √(1/n)n·(n+lg n)+(n−n)·lg n√√=(1/n)(n+n lg n+n lg n−nlgn)=(1/n)(n+n lg n)=O(lg n)√Thus, we have a tree with average node depth O(lg n), but height Ω(n).。

算法分析与设计 第二版 英文版 (潘彦 著) 清华大学出版社 课后答案--solu9

算法分析与设计 第二版 英文版 (潘彦 著) 清华大学出版社 课后答案--solu9

This file contains the exercises,hints,and solutions for Chapter 9of the book ”Introduction to the Design and Analysis of Algorithms,”2nd edition,byA.Levitin.The problems that might be challenging for at least some students are marked by ;those that might be difficult for a majority of students are marked by .Exercises 9.11.Give an instance of the change-making problem for which the greedy al-gorithm does not yield an optimal solution.2.Write a pseudocode of the greedy algorithm for the change-making prob-lem,with an amount n and coin denominations d 1>d 2>...>d m as its input.What is the time efficiency class of your algorithm?3.Consider the problem of scheduling n jobs of known durations t 1,...,t n for execution by a single processor.The jobs can be executed in any order,one job at a time.You want to find a schedule that minimizes the total time spent by all the jobs in the system.(The time spent by one job in the system is the sum of the time spent by this job in waiting plus the time spent on its execution.)Design a greedy algorithm for this problem. Does the greedy algo-rithm always yield an optimal solution?4.Design a greedy algorithm for the assignment problem (see Section 3.4).Does your greedy algorithm always yield an optimal solution?5.Bridge crossing revisited Consider the generalization of the bridge cross-ing puzzle (Problem 2in Exercises 1.2)in which we have n >1people whose bridge crossing times are t 1,t 2,...,t n .All the other conditions of the problem remain the same:at most two people at the time can cross the bridge (and they move with the speed of the slower of the two)and they must carry with them the only flashlight the group has.Design a greedy algorithm for this problem and find how long it willtake to cross the bridge by using this algorithm.Does your algorithm yields a minimum crossing time for every instance of the problem?If it does–prove it,if it does not–find an instance with the smallest number of people for which this happens.6.Bachet-Fibonacci weighing problem Find an optimal set of n weights {w 1,w 2,...,w n }so that it would be possible to weigh on a balance scale any integer load in the largest possible range from 1to W ,provided a. weights can be put only on the free cup of the scale.b. weights can be put on both cups of the scale.1课后答案网 w w w .k h d a w .c o m7.a.Apply Prim’s algorithm to the following graph.Include in the priority queue all the vertices not already in the tree.b.Apply Prim’s algorithm to the following graph.Include in the priority queue only the fringe vertices (the vertices not in the current tree which are adjacent to at least one tree vertex).8.The notion of a minimum spanning tree is applicable to a connected weighted graph.Do we have to check a graph’s connectivity before ap-plying Prim’s algorithm or can the algorithm do it by itself?9.a.How can we use Prim’s algorithm to find a spanning tree of a connected graph with no weights on its edges?b.Is it a good algorithm for this problem?10. Prove that any weighted connected graph with distinct weights hasexactly one minimum spanning tree.11.Outline an efficient algorithm for changing an element’s value in a min-heap.What is the time efficiency of your algorithm?2课后答案网 w h d a w .c o mHints to Exercises 9.11.As coin denominations for your counterexample,you may use,among a multitude of other possibilities,the ones mentioned in the text:d 1=7,d 2=5,d 3=1.2.You may use integer divisions in your algorithm.3.Considering the case of two jobs might help.Of course,after forming a hypothesis,you will have to either prove the algorithm’s optimality for an arbitrary input or find a specific counterexample showing that it is not the case.4.You can apply the greedy approach either to the entire cost matrix or to each of its rows (or columns).5.Simply apply the greedy approach to the situation at hand.You may assume that t 1≤t 2≤...≤t n .6.For both versions of the problem,it is not difficult to get to a hypothesis about the solution’s form after considering the cases of n =1,2,and 3.It is proving the solutions’optimality that is at the heart of this problem.7.a.Trace the algorithm for the graph given.An example can be found in the text of the section.b.After the next fringe vertex is added to the tree,add all the unseen vertices adjacent to it to the priority queue of fringe vertices.8.Applying Prim’s algorithm to a weighted graph that is not connected should help in answering this question.9.a.Since Prim’s algorithm needs weights on a graph’s edges,some weights have to be assigned.b.Do you know other algorithms that can solve this problem?10.Strictly speaking,the wording of the question asks you to prove two things:the fact that at least one minimum spanning tree exists for any weighted connected graph and the fact that a minimum spanning tree is unique if all the weights are distinct numbers.The proof of the former stems from the obvious observation about finiteness of the number of spanning trees for a weighted connected graph.The proof of the latter can be obtained by repeating the correctness proof of Prim’s algorithm with a minor adjustment at the end.11.Consider two cases:the key’s value was decreased (this is the case needed for Prim’s algorithm)and the key’s value was increased.3课后答案网 w w w .k h d a w .c o mSolutions to Exercises 9.11.Here is one of many such instances:For the coin denominations d 1=7,d 2=5,d 3=1and the amount n =10,the greedy algorithm yields one coin of denomination 7and three coins of denomination 1.The actual optimal solution is two coins of denomination 5.2.Algorithm Change (n,D [1..m ])//Implements the greedy algorithm for the change-making problem //Input:A nonnegative integer amount n and//a decreasing array of coin denominations D//Output:Array C [1..m ]of the number of coins of each denomination //in the change or the ”no solution”messagefor i ←1to m doC [i ]← n/D [i ]n ←n mod D [i ]if n =0return Celse return ”no solution”The algorithm’s time efficiency is in Θ(m ).(We assume that integer di-visions take a constant time no matter how big dividends are.)Note also that if we stop the algorithm as soon as the remaining amount becomes 0,the time efficiency will be in O (m ).3.a.Sort the jobs in nondecreasing order of their execution times and exe-cute them in that order.b.Yes,this greedy algorithm always yields an optimal solution.Indeed,for any ordering (i.e.,permutation)of the jobs i 1,i 2,...,i n ,the total time in the system is given by the formula t i 1+(t i 1+t i 2)+...+(t i 1+t i 2+...+t i n )=nt i 1+(n −1)t i 2+...+t i n .Thus,we have a sum of numbers n,n −1,...,1multiplied by “weights”t 1,t 2,...t n assigned to the numbers in some order.To minimize such a sum,we have to assign smaller t ’s to larger numbers.In other words,the jobs should be executed in nondecreasing order of their execution times.Here is a more formal proof of this fact.We will show that if jobs are ex-ecuted in some order i 1,i 2,...,i n ,in which t i k >t i k +1for some k,then the total time in the system for such an ordering can be decreased.(Hence,no such ordering can be an optimal solution.)Let us consider the other job ordering,which is obtained by swapping the jobs k and k +1.Obvi-ously,the time in the systems will remain the same for all but these two 4课后答案网 w w w .k h d a w .c o mjobs.Therefore,the difference between the total time in the system for the new ordering and the one before the swap will be[(k −1j =1t i j +t i k +1)+(k −1j =1t i j +t i k +1+t i k )]−[(k −1j =1t i j +t i k )+(k −1j =1t i j +t i k +t i k +1)]=t i k +1−t i k <0.4.a.The all-matrix version:Repeat the following operation n times.Select the smallest element in the unmarked rows and columns of the cost matrix and then mark its row and column.The row-by-row version:Starting with the first row and ending with the last row of the cost matrix,select the smallest element in that row which is not in a previously marked column.After such an element is selected,mark its column to prevent selecting another element from the same col-umn.b.Neither of the versions always yields an optimal solution.Here isa simple counterexample:C = 122100 5.Repeat the following step n −2times:Send to the other side the pair of two fastest remaining persons and then return the flashlight with the fastest person.Finally,send the remaining two people together.Assuming that t 1≤t 2≤...≤t n ,the total crossing time will be equal to (t 2+t 1)+(t 3+t 1)+...+(t n −1+t 1)+t n =ni =2t i +(n −2)t 1=n i =1t i +(n −3)t 1.Note:For an algorithm that always yields a minimal crossing time,seeGünter Rote,“Crossing the Bridge at Night,”EATCS Bulletin,vol.78(October 2002),241—246.The solution to the instance of Problem 2in Exercises 1.2shows that the greedy algorithm doesn’t always yield the minimal crossing time for n >3.No smaller counterexample can be given as a simple exhaustive check for n =3demonstrates.(The obvious solution for n =2is the one generated by the greedy algorithm as well.)5课后答案网 w w w .kh d a w .c o m6.a.Let’s apply the greedy approach to the first few instances of the problem in question.For n =1,we have to use w 1=1to balance weight 1.For n =2,we simply add w 2=2to balance the first previously unattainable weight of 2.The weights {1,2}can balance every integral weights up to their sum 3.For n =3,in the spirit of greedy thinking,we take the next previously unattainable weight:w 3=4.The three weights {1,2,4}allow to weigh any integral load l between 1and their sum 7,with l ’s binary expansion indicating the weights needed for load l :Generalizing these observations,we should hypothesize that for any posi-tive integer n the set of consecutive powers of 2{w i =2i −1,i =1,2,...n }makes it possible to balance every integral load in the largest possible range,which is up to and including n i =12i −1=2n −1.The fact that every integral weight l in the range 1≤l ≤2n −1can be balanced with this set of weights follows immediately from the binary expansion of l,which yields the weights needed for weighing l.(Note that we can obtain the weights needed for a given load l by applying to it the greedy algorithm for the change-making problem with denominations d i =2i −1,i =1,2,...n.)In order to prove that no set of n weights can cover a larger range of consecutive integral loads,it will suffice to note that there are just 2n −1nonempty selections of n weights and,hence,no more than 2n −1sums they yield.Therefore,the largest range of consecutive integral loads they can cover cannot exceed 2n −1.[Alternatively,to prove that no set of n weights can cover a larger range of consecutive integral loads,we can prove by induction on i that if any mul-tiset of n weights {w i ,i =1,...,n }–which we can assume without loss of generality to be sorted in nondecreasing order–can balance every integral load starting with 1,then w i ≤2i −1for i =1,2,...,n.The basis checks out immediately:w 1must be 1,which is equal to 21−1.For the general case,assume that w k ≤2k −1for every 1≤k <i.The largest weight the first i −1weights can balance is i −1k =1w k ≤ i −1k =12k −1=2i −1−1.If w i were larger than 2i ,then this load could have been balanced neither with the first i −1weights (which are too light even taken together)nor with the weights w i ≤...≤w n (which are heavier than 2i even individ-ually).Hence,w i ≤2i −1,which completes the proof by induction.This immediately implies that no n weights can balance every integral load up to the upper limit larger than n i =1w i ≤ n i =12i −1=2n −1,the limit attainable with the consecutive powers of 2weights.]b.If weights can be put on both cups of the scale,then a larger range can 6课后答案网 w w w .k h d a w .be reached with n weights for n >1.(For n =1,the single weight still needs to be 1,of course.)The weights {1,3}enable weighing of every integral load up to 4;the weights {1,3,9}enable weighing of every inte-gral load up to 13,and,in general,the weights {w i =3i −1,i =1,2,...,n }enable weighing of every integral load up to and including their sum of n i =13i −1=(3n −1)/2.A load’s expansion in the ternary system indicates the weights needed.If the ternary expansion contains only 0’s and 1’s,the load requires putting the weights corresponding to the 1’s on the opposite cup of the balance.If the ternary expansion of load l,l ≤(3n −1)/2,contains one or more 2’s,we can replace each 2by (3-1)to represent it in the form l =n i =1βi 3i −1,where βi ∈{0,1,−1},n = log 3(l +1) .In fact,every positive integer can be uniquely represented in this form,obtained from its ternary expansion as described above.For example,5=123=1·31+2·30=1·31+(3−1)·30=2·31−1·30=(3−1)·31−1·30=1·32−1·31−1·30.(Note that if we start with the rightmost 2,after a simplification,the new rightmost 2,if any,will be at some position to the left of the starting one.This proves that after a finite number of such replacements,we will be able to eliminate all the 2’s.)Using the representation l = n i =1βi 3i −1,we can weigh load l by placing all the weights w i =3i −1for negative βi ’s along with the load on one cup of the scale and all the weights w i =3i −1for positive βi ’s on the opposite cup.Now we’ll prove that no set of n weights can cover a larger range of con-secutive integral loads than (3n −1)/2.Each of the n weights can be either put on the left cup of the scale,or put on the right cup,or not to be used at all.Hence,there are 3n −1possible arrangements of the weights on the scale,with each of them having its mirror image (where all the weights are switched to the opposite pan of the scale).Eliminating this symmetry,leaves us withjust (3n −1)/2arrangements,which can weight at most (3n −1)/2different integral loads.Therefore,the largest range of consecutive integral loads they can cover cannot exceed (3n −1)/2.7.a.Apply Prim’s algorithm to the following graph:7课后答案网 w w w.k h d a w .c o mthe edges ae,eb,ec,and cd.b.Apply Prim’s algorithm to the following graph:the edges ab,be,ed,dc,ef,ei,ij,cg,gh,il,gk.8.There is no need to check the graph’s connectivity because Prim’s algo-rithm can do it itself.If the algorithm reaches all the graph’s vertices (via edges of finite lengths),the graph is connected,otherwise,it is not.9.a.The simplest and most logical solution is to assign all the edge weights to 1.8课a w .c o mb.Applying a depth-first search (or breadth-first search)traversal to get a depth-first search tree (or a breadth-first search tree),is conceptually simpler and for sparse graphs represented by their adjacency lists faster.10.The number of spanning trees for any weighted connected graph is a pos-itive finite number.(At least one spanning tree exists,e.g.,the one obtained by a depth-first search traversal of the graph.And the number of spanning trees must be finite because any such tree comprises a subset of edges of the finite set of edges of the given graph.)Hence,one can always find a spanning tree with the smallest total weight among the finite number of the candidates.Let’s prove now that the minimum spanning tree is unique if all the weights are distinct.We’ll do this by contradiction,i.e.,by assuming that there exists a graph G =(V,E )with all distinct weights but with more than one minimum spanning tree.Let e 1,...,e |V |−1be the list of edges com-posing the minimum spanning tree T P obtained by Prim’s algorithm with some specific vertex as the algorithm’s starting point and let T be an-other minimum spanning tree.Let e i =(v,u )be the first edge in the list e 1,...,e |V |−1of the edges of T P which is not in T (if T P =T ,such edge must exist)and let (v,u )be an edge of T connecting v with a vertex not in the subtree T i −1formed by {e 1,...,e i −1}(if i =1,T i −1consists of vertex v only).Similarly to the proof of Prim’s algorithms correctness,let us replace (v,u )by e i =(v,u )in T .It will create another spanning tree,whose weight is smaller than the weight of T because the weight of e i =(v,u )is smaller than the weight of (v,u ).(Since e i was chosen by Prim’s algorithm,its weight is the smallest among all the weights on the edges connecting the tree vertices of the subtree T i −1and the vertices adjacent to it.And since all the weights are distinct,the weight of (v,u )must be strictly greater than the weight of e i =(v,u ).)This contradicts the assumption that T was a minimum spanning tree.11.If a key’s value in a min-heap was decreased,it may need to be pushedup (via swaps)along the chain of its ancestors until it is smaller than or equal to its parent or reaches the root.If a key’s value in a min-heap was increased,it may need to be pushed down by swaps with the smaller of its current children until it is smaller than or equal to its children or reaches a leaf.Since the height of a min-heap with n nodes is equal to log 2n (by the same reason the height of a max-heap is given by this formula–see Section 6.4),the operation’s efficiency is in O (log n ).(Note:The old value of the key in question need not be known,of paring the new value with that of the parent and,if the min-heap condition holds,with the smaller of the two children,will suffice.)9课后答案网 w w w.k h d a w .c o mExercises 9.21.Apply Kruskal’s algorithm to find a minimum spanning tree of the follow-ing graphs.a.b.2.Indicate whether the following statements are true or false:a.If e is a minimum-weight edge in a connected weighted graph,it must be among edges of at least one minimum spanning tree of the graph.b.If e is a minimum-weight edge in a connected weighted graph,it must be among edges of each minimum spanning tree of the graph.c.If edge weights of a connected weighted graph are all distinct,the graph must have exactly one minimum spanning tree.d.If edge weights of a connected weighted graph are not all distinct,the graph must have more than one minimum spanning tree.3.What changes,if any,need to be made in algorithm Kruskal to make it find a minimum spanning forest for an arbitrary graph?(A minimum spanning forest is a forest whose trees are minimum spanning trees of the graph’s connected components.)10课后答案网h d a w .c o m4.Will either Kruskal’s or Prim’s algorithm work correctly on graphs that have negative edge weights?5.Design an algorithm for finding a maximum spanning tree –a spanning tree with the largest possible edge weight–of a weighted connected graph.6.Rewrite the pseudocode of Kruskal’s algorithm in terms of the operations of the disjoint subsets’ADT.7. Prove the correctness of Kruskal’s algorithm.8.Prove that the time efficiency of find (x )is in O (log n )for the union-by-size version of quick union.9.Find at least two Web sites with animations of Kruskal’s and Prim’s al-gorithms.Discuss their merits and demerits..10.Design and conduct an experiment to empirically compare the efficienciesof Prim’s and Kruskal’s algorithms on random graphs of different sizes and densities.11. Steiner tree Four villages are located at the vertices of a unit squarein the Euclidean plane.You are asked to connect them by the shortest network of roads so that there is a path between every pair of the villages along those roads.Find such a network.11课后答案网ww w.kh d aw .c omHints to Exercises 9.21.Trace the algorithm for the given graphs the same way it is done for another input in the section.2.Two of the four assertions are true,the other two are false.3.Applying Kruskal’s algorithm to a disconnected graph should help to an-swer the question.4.The answer is the same for both algorithms.If you believe that the algorithms work correctly on graphs with negative weights,prove this assertion;it you believe this is not to be the case,give a counterexample for each algorithm.5.Is the general trick of transforming maximization problems to their mini-mization counterparts (see Section6.6)applicable here?6.Substitute the three operations of the disjoint subsets’ADT–makeset (x ),find (x ),and union (x,y )–in the appropriate places of the pseudocode given in the section.7.Follow the plan used in Section 9.1to prove the correctness of Prim’s algorithm.8.The argument is very similar to the one made in the section for the union-by-size version of quick find.9.You may want to take advantage of the list of desirable characteristics in algorithm visualizations,which is given in Section 2.7.10.n/a11.The question is not trivial because introducing extra points (called Steinerpoints )may make the total length of the network smaller than that of a minimum spanning tree of the square.Solving first the problem for three equidistant points might give you an indication how a solution to the problem in question could look like.12课后答案网ww w.kh d aw .c omSolutions to Exercises9.21.a.后课13b.⇒⇒⇒⇒⇒⇒14课c⇒⇒⇒⇒⇒课2.a.True.(Otherwise,Kruskal’s algorithm would be invalid.)b.False.As a simple counterexample,consider a complete graph withthree vertices and the same weight on its three edgesc.True(Problem10in Exercises9.1).15d.False (see,for example,the graph of Problem 1a).3.Since the number of edges in a minimum spanning forest of a graph with |V |vertices and |C |connected components is equal to |V |−|C |(this for-mula is a simple generalization of |E |=|V |−1for connected graphs),Kruskal (G )will never get to |V |−1tree edges unless the graph is con-nected.A simple remedy is to replace the loop while ecounter <|V |−1with while k <|E |to make the algorithm stop after exhausting the sorted list of its edges.4.Both algorithms work correctly for graphs with negative edge weights.One way of showing this is to add to all the weights of a graph with negative weights some large positive number.This makes all the new weights positive,and one can “translate”the algorithms’actions on the new graph to the corresponding actions on the old one.Alternatively,you can check that the proofs justifying the algorithms’correctness do not depend on the edge weights being nonnegative.5.Replace each weight w (u,v )by −w (u,v )and apply any minimum spanning tree algorithm that works on graphs with arbitrary weights (e.g.,Prim’s or Kruskal’s algorithm)to the graph with the new weights.6.Algorithm Kruskal (G )//Kruskal’s algorithm with explicit disjoint-subsets operations //Input:A weighted connected graph G = V,E//Output:E T ,the set of edges composing a minimum spanning tree of G sort E in nondecreasing order of the edge weights w (e i 1)≤...≤w (e i |E |)for each vertex v ∈V make (v )E T ←∅;ecounter ←0//initialize the set of tree edges and its size k ←0//the number of processed edges while ecounter <|V |−1k ←k +1if find (u )=find (v )//u,v are the endpoints of edge e i kE T ←E T ∪{e i k };ecounter ←ecounter +1union (u,v )return E T 7.Let us prove by induction that each of the forests F i ,i =0,...,|V |−1,of Kruskal’s algorithm is a part (i.e.,a subgraph)of some minimum span-ning tree.(This immediately implies,of course,that the last forest in the sequence,F |V |−1,is a minimum spanning tree itself.Indeed,it contains all vertices of the graph,and it is connected because it is both acyclic and has |V |−1edges.)The basis of the induction is trivial,since F 0is16课后答案网ww w.kh d aw .c ommade up of |V |single-vertex trees and therefore must be a subgraph of any spanning tree of the graph.For the inductive step,let us assume that F i −1is a subgraph of some minimum spanning tree T .We need to prove that F i ,generated from F i −1by Kruskal’s algorithm,is also a part of a minimum spanning tree.We prove this by contradiction by assuming that no minimum spanning tree of the graph can contain F i .Let e i =(v,u )be the minimum weight edge added by Kruskal’s algorithm to forest F i −1to obtain forest F i .(Note that vertices v and u must belong to different trees of F i −1–otherwise,edge (v,u )would’ve created a cycle.)By our assumption,e i cannot belong to T .Therefore,if we add e i to T ,a cycle must be formed (see the figure below).In addition to edge e i =(v,u ),this cycle must contain another edge (v ,u )connecting a vertex v in the same tree of F i −1as v to a vertex u not in that tree.(It is possible that v coincides with v or u coincides with u but not both.)If we now delete the edge (v ,u )from this cycle,we will obtain another spanning tree of the entire graph whose weight is less than or equal to the weight of T since the weight of e i is less than or equal to the weight of (v ,u ).Hence,this spanning tree is a minimum spanning tree,which contradicts the assumption that no minimum spanning tree contains F i .This com-pletes the correctness proof of Kruskal’s algorithm.8.In the union-by-size version of quick-union ,each vertex starts at depth 0of its own tree.The depth of a vertex increases by 1when the tree it is in is attached to a tree with at least as many nodes during a union operation.Since the total number of nodes in the new tree containing the node is at least twice as much as in the old one,the number of such increases cannot exceed log 2n.Therefore the height of any tree (which is the largest depth of the tree’s nodes)generated by a legitimate sequence of unions will not exceed log 2n.Hence,the efficiency of find (x )is in O (log n )because find (x )traverses the pointer chain from the x ’s node to the tree’s root.9.n/a10.n/a17课后答案.kh d aw .c om11.The minimum Steiner tree that solves the problem is shown below.(Theother solution can be obtained by rotating the figure 90◦.)A popular discussion of Steiner trees can be found in “Last Recreations:Hydras,Eggs,and Other Mathematical Mystifications”by Martin Gard-ner.In general,no polynomial time algorithm is known for finding a minimum Steiner tree;moreover,the problem is known to be NP -hard (see Section 11.3).For the state-of-the-art information,see,e.g.,The Steiner Tree Page at /steiner/.18课后答案网ww w.kc omExercises 9.31.Explain what adjustments if any need to be made in Dijkstra’s algorithm and/or in an underlying graph to solve the following problems.a.Solve the single-source shortest-paths problem for directed weighted graphs.b.Find a shortest path between two given vertices of a weighted graph or digraph.(This variation is called the single-pair shortest-path prob-lem .)c.Find the shortest paths to a given vertex from each other vertex of a weighted graph or digraph.(This variation is called the single-destination shortest-paths problem .)d.Solve the single-source shortest-path problem in a graph with nonneg-ative numbers assigned to its vertices (and the length of a path defined as the sum of the vertex numbers on the path).2.Solve the following instances of the single-source shortest-paths problem with vertex a as the source:a.b.3.Give a counterexample that shows that Dijkstra’s algorithm may not work for a weighted connected graph with negative weights.19课案w w.kh d aw .c om4.Let T be a tree constructed by Dijkstra’s algorithm in the process of solving the single-source shortest-path problem for a weighted connected graph G .a.True or false:T is a spanning tree of G ?b.True or false:T is a minimum spanning tree of G ?5.Write a pseudocode of a simpler version of Dijkstra’s algorithm that finds only the distances (i.e.,the lengths of shortest paths but not shortest paths themselves)from a given vertex to all other vertices of a graph represented by its weight matrix.6. Prove the correctness of Dijkstra’s algorithm for graphs with positive weights.7.Design a linear-time algorithm for solving the single-source shortest-paths problem for dags (directed acyclic graphs)represented by their adjacency lists.8.Design an efficient algorithm for finding the length of a longest path in a dag.(This problem is important because it determines a lower bound on the total time needed for completing a project composed of precedence-constrained tasks.)9.Shortest-path modeling Assume you have a model of a weighted con-nected graph made of balls (representing the vertices)connected by strings of appropriate lengths (representing the edges).a.Describe how you can solve the single-pair shortest-path problem with this model .b.Describe how you can solve the single-source shortest-paths problem with this model .10.Revisit Problem 6in Exercises 1.3about determining the best route fora subway passenger to take from one designated station to another in a well-developed subway system like those in Washington,DC and London,UK.Write a program for this task.20课后答案网ww w.kh d aw .c om。

算法设计技巧与分析英文版课后练习题含答案

算法设计技巧与分析英文版课后练习题含答案

Algorithm Design Techniques and Analysis: English VersionExercise with AnswersIntroductionAlgorithms are an essential aspect of computer science. As such, students who are part of this field must master the art of algorithm design and analysis. Algorithm design refers to the process of creating algorithms that solve computational problems. Algorithm analysis, on the other hand, focuses on evaluating the resources required to execute those algorithms. This includes computational time and memory consumption.This document provides students with helpful algorithm design and analysis exercises. The exercises are in the formof questions with step-by-step solutions. The document is suitable for students who have completed the English versionof the Algorithm Design Techniques and Analysis textbook. The exercises cover various algorithm design techniques, such as divide-and-conquer, dynamic programming, and greedy approaches.InstructionEach exercise comes with a question and its solution. Read the question carefully and try to find a solution withoutlooking at the answer first. If you get stuck, look at the solution. Lastly, try the exercise agn without referring to the answer.Exercise 1: Divide and ConquerQuestion:Given an array of integers, find the maximum possible sum of a contiguous subarray.Example:Input: [-2, -3, 4, -1, -2, 1, 5, -3]Output: 7 (the contiguous subarray [4, -1, -2, 1, 5]) Solution:def max_subarray_sum(arr):if len(arr) ==1:return arr[0]mid =len(arr) //2left_arr = arr[:mid]right_arr = arr[mid:]max_left_sum = max_subarray_sum(left_arr)max_right_sum = max_subarray_sum(right_arr)max_left_border_sum =0left_border_sum =0for i in range(mid-1, -1, -1):left_border_sum += arr[i]max_left_border_sum =max(max_left_border_sum, left_b order_sum)max_right_border_sum =0right_border_sum =0for i in range(mid, len(arr)):right_border_sum += arr[i]max_right_border_sum =max(max_right_border_sum, righ t_border_sum)return max(max_left_sum, max_right_sum, max_left_border_s um+max_right_border_sum)Exercise 2: Dynamic ProgrammingQuestion:Given a list of lengths of steel rods and a corresponding list of prices, determine the maximum revenue you can get by cutting these rods into smaller pieces and selling them. Assume the cost of each cut is 0.Lengths: [1, 2, 3, 4, 5, 6, 7, 8]Prices: [1, 5, 8, 9, 10, 17, 17, 20]If the rod length is 4, the maximum revenue is 10.Solution:def max_revenue(lengths, prices, n):if n ==0:return0max_val =float('-inf')for i in range(n):max_val =max(max_val, prices[i] + max_revenue(length s, prices, n-i-1))return max_valExercise 3: Greedy AlgorithmQuestion:Given a set of jobs with start times and end times, find the maximum number of non-overlapping jobs that can be scheduled.Start times: [1, 3, 0, 5, 8, 5]End times: [2, 4, 6, 7, 9, 9]Output: 4Solution:def maximum_jobs(start_times, end_times):job_list =sorted(zip(end_times, start_times))count =0end_time =float('-inf')for e, s in job_list:if s >= end_time:count +=1end_time = ereturn countConclusionThe exercises presented in this document provide a practical way to master essential algorithm design and analysis techniques. Solving the problems without looking at the answers will expose students to the type of problems they might encounter in real life. The document’s solutionsprovide step-by-step instructions to ensure that students can approach the problems with confidence.。

MIT牛人解说数学体系(增加部分英文翻译和备注)

MIT牛人解说数学体系(增加部分英文翻译和备注)

在过去的一年中,我一直在数学的海洋中游荡,research进展不多,对于数学世界的阅历算是有了一些长进。

为什么要深入数学的世界作为计算机的学生,我没有任何企图要成为一个数学家。

我学习数学的目的,是要想爬上巨人的肩膀,希望站在更高的高度,能把我自己研究的东西看得更深广一些。

说起来,我在刚来这个学校的时候,并没有预料到我将会有一个深入数学的旅程。

我的导师最初希望我去做的题目,是对appearance 和motion建立一个unified的model。

这个题目在当今Computer Vision中百花齐放的世界中并没有任何特别的地方。

事实上,使用各种Graphical Model把各种东西联合在一起framework,在近年的论文中并不少见。

我不否认现在广泛流行的Graphical Model是对复杂现象建模的有力工具,但是,我认为它不是panacea(万应灵药),并不能取代对于所研究的问题的深入的钻研。

如果统计学习包治百病,那么很多“下游”的学科也就没有存在的必要了。

事实上,开始的时候,我也是和Vision中很多人一样,想着去做一个Graphical Model——我的导师指出,这样的做法只是重复一些标准的流程,并没有很大的价值。

经过很长时间的反复,另外一个路径慢慢被确立下来——我们相信,一个图像是通过大量“原子”的某种空间分布构成的,原子群的运动形成了动态的可视过程。

微观意义下的单个原子运动,和宏观意义下的整体分布的变换存在着深刻的联系——这需要我们去发掘。

在深入探索这个题目的过程中,遇到了很多很多的问题,如何描述一个一般的运动过程,如何建立一个稳定并且广泛适用的原子表达,如何刻画微观运动和宏观分布变换的联系,还有很多。

在这个过程中,我发现了两个事情:我原有的数学基础已经远远不能适应我对这些问题的深入研究。

在数学中,有很多思想和工具,是非常适合解决这些问题的,只是没有被很多的应用科学的研究者重视。

各种算法英文表示

各种算法英文表示

各种算法英文表示array algorithm数组算法bounded variable algorithm边界变量算法dynamic programming algorithm 动态规划算法enumerative algorithm枚举算法Euclid's algorithm欧几里得算法FFT algorithm快速傅里叶算法fuzzy algorithm模糊算法game playing algorithm博奕[对策]算法Gauss-Newton algorithm高斯-牛顿算法iterative algorithm迭代算法knapsack algorithm渐缩算法Markov algorithm马尔可夫算法minimization algorithm极小化算法optimal estimation algorithm最佳估计算法optimization algorithm最优化算法partan algorithm平行切线(算)法recursive algorithm递归算法statistcal algorithm统计算法steepest descent algorithm最速下降算法steplength algorithms步长算法universal algorithm通用算法algorithm for coding images编码图象的算法Hew to 服从,遵守各种分布的表示:泊松分布:Poisson distribution指数分布:Exponential distribution高斯分布:Gaussian distribution几何分布:Geometric distribution全球性分布:Global distribution均匀分布:Homogeneous distribution 对数分布:Logarithmic distribution。

算法设计与分析双语课Chapter 3 Divide and conquer

算法设计与分析双语课Chapter 3 Divide and  conquer
1 C ( n) = 2C (n / 2) + 2
C (n) = 2C (n / 2) + 2 = 2(2 C ( n / 4 ) + 2 ) + 2 = 4C (n / 4) + 4 + 2 = ... = 2 k 1 C ( n / 2 k 1 ) + 2 k 1 + 2 k 2 + ... 2 2 + 2 = 2 k 1 C ( 2 ) + ∑ 2
North China Electric Power University
Notation:
1. The combine step is very crucial to the performance of virtually all divide-and-conquer algorithms, as the efficiency of the algorithm is largely dependent on how judiciously this step is implemented. 2. On the other hand,the divide step is invariant in almost all divide-and-conquer algorithms: Partition the input data into p parts, and proceed to the conquer step. In many divide-andconquer algorithms, it takes O(n) time or even only O(1) time. In general, a divide-and-conquer algorithm has the following format. 1) If the size of the instance I is “small”, then solve the problem using a straightforward method and return the answer. Otherwise, Continue to the next step.

data structures and algorithm analysi英文原版 pdf (2)

data structures and algorithm analysi英文原版 pdf (2)

data structures and algorithm analysi英文原版 pdfTitle: Data Structures and Algorithm Analysis: A Comprehensive ReviewIntroduction:Data structures and algorithm analysis are fundamental concepts in computer science. They form the backbone of efficient and optimized software development. This article aims to provide a comprehensive review of the book "Data Structures and Algorithm Analysis" in its English original version PDF format. The review will cover the key points, structure, and significance of the book.I. Overview of the Book:1.1 Importance of Data Structures:- Discuss the significance of data structures in organizing and manipulating data efficiently.- Explain how data structures enhance the performance and scalability of software applications.1.2 Algorithm Analysis:- Describe the role of algorithm analysis in evaluating the efficiency and performance of algorithms.- Highlight the importance of selecting appropriate algorithms for different problem-solving scenarios.1.3 Book Structure:- Outline the organization of the book, including chapters, sections, and topics covered.- Emphasize the logical progression of concepts, starting from basic data structures to advanced algorithm analysis.II. Data Structures:2.1 Arrays and Linked Lists:- Explain the characteristics, advantages, and disadvantages of arrays and linked lists.- Discuss the implementation details, operations, and time complexities of these data structures.2.2 Stacks and Queues:- Define stacks and queues and their applications in various scenarios.- Elaborate on the implementation, operations, and time complexities of stacks and queues.2.3 Trees and Graphs:- Introduce the concepts of trees and graphs and their real-world applications.- Discuss different types of trees (binary, AVL, B-trees) and graphs (directed, undirected, weighted).III. Algorithm Analysis:3.1 Asymptotic Notation:- Explain the significance of asymptotic notation in analyzing the efficiency of algorithms.- Discuss the Big-O, Omega, and Theta notations and their usage in algorithm analysis.3.2 Sorting and Searching Algorithms:- Describe various sorting algorithms such as bubble sort, insertion sort, merge sort, and quicksort.- Discuss searching algorithms like linear search, binary search, and hash-based searching.3.3 Dynamic Programming and Greedy Algorithms:- Define dynamic programming and greedy algorithms and their applications.- Provide examples of problems that can be solved using these approaches.IV. Advanced Topics:4.1 Hashing and Hash Tables:- Explain the concept of hashing and its applications in efficient data retrieval.- Discuss hash functions, collision handling, and the implementation of hash tables.4.2 Graph Algorithms:- Explore advanced graph algorithms such as Dijkstra's algorithm, breadth-first search, and depth-first search.- Discuss their applications in solving complex problems like shortest path finding and network analysis.4.3 Advanced Data Structures:- Introduce advanced data structures like heaps, priority queues, and self-balancing binary search trees.- Explain their advantages, implementation details, and usage in various scenarios.V. Summary:5.1 Key Takeaways:- Summarize the main points covered in the book, emphasizing the importance of data structures and algorithm analysis.- Highlight the significance of selecting appropriate data structures and algorithms for efficient software development.5.2 Practical Applications:- Discuss real-world scenarios where the concepts from the book can be applied.- Illustrate how understanding data structures and algorithm analysis can lead to optimized software solutions.5.3 Conclusion:- Conclude the review by emphasizing the relevance and usefulness of the book "Data Structures and Algorithm Analysis."- Encourage readers to explore the book further for a deeper understanding of the subject.In conclusion, "Data Structures and Algorithm Analysis" is a comprehensive guide that covers essential concepts in data structures and algorithm analysis. The book's structure, detailed explanations, and practical examples make it a valuable resource for computer science students, software developers, and anyone interested in optimizing their software solutions. Understanding these fundamental concepts is crucial for building efficient and scalable software applications.。

designandanalysisofalgorithms算法设计与分析

designandanalysisofalgorithms算法设计与分析

Design and Analysis of Algorithms–CSE101Basic Information:Spring,2011Instructor:Russell ImpagliazzoClass:TT,11:00-12:20,HSS1330,Mandatory discussion section:Wed.1-1:50,Center105101Professor Office Hours:Wed.,Friday,2:30-4,start in CSE4248 (may move to bigger room).email:***************.eduwebpage:/classes/sp09/cse101TA:Qian PengTA office Hours:Thu5-7PM at EBU3B room B250aPrerequisites:CSE21,CSE100Text Books:Johnsonbaugh and Schaefer,Algorithms.OREdmonds,How to Think About Algorithms.You need at least one of these two textbooks.Preferably,each study group will have both available.I mark the reading JS for Johnson-baugh and Schaefer or JE for JeffEdmonds.Assignments There will be a calibration homework(not for credit),four homework assignments,a mid-term exam,and afinal exam. Evaluation:Homework will account for30%of the grade,the mid-term, 30%,and thefinal will account for the remaining40%of the grade.The calibration homework does not count for credit.The best3out of 4homework assignments will be counted,so each homework is worth 10%of grade.There will be a practice mid-term;the mid-term grade will be the better of the practice and real mid-term grades.Sorry,no practicefinal.Ethics and Academic Dishonesty In the past,there has been epidemic cheating in this class.For example,dishonesty caused25%of the1class to fail in1997.For this reason,some rather intrusive rules have been instituted.Students will be allowed to solve and write up all homework assign-ments in groups of size up to4.All names should appear on the assignment.Members of a group are responsible for all parts of any assignment with their names on it.Problems should be solved by the group,not divided up between group members.Each member of a group should participate in discussions about each problem.The front page should be signed by each member of a group;this is interpretted as the state-ment:”I participated in discussion for each problem,and have read and understood the answers here,which are summaries of our discus-sion.”If this statement is true,just sign your name.If you wish to modify this statement,write and sign the modified statement instead. If the statement is not true of some of the problems,add”except for problems...”.You will not receive credit for these problems,but you also will not bear responsibility for them.Students should not look for answers to homework problems in other (i.e.,other than the course texts and class notes)texts or other sources (e.g.Internet discussion groups or newsgroups).However,students may use other texts as a general study tool,and may accidentally see solutions to homework problems.In this case,the student should write up thefinal solution without consulting this text or source,and should give an acknowledgement of the text or source on thefirst page of their solutions.Such a solution may be given partial or no credit if it too closely follows the source.Not giving an acknowledement is academic dishonesty,and will be treated as such.This rule applies to any material found on the internet,and to conversations with or written material from other people, whether or not they are students in the class.However,it does not apply to material handed out in class or on the class web-page for this year,or to conversations with the instructor or teaching assistants.Be sure to follow the following guidelines:1.Do not discuss problems with people outside your group(exceptduring office hours,or with the TAs or myself).22.Do not share written solutions or partial solutions with othergroups.3.Prepare yourfinal written solution without consulting any writ-ten material except class notes and the class text.4.Acknowledge all supplementary texts or sources that had solu-tions to homework problems.5.All problems should be discussed by the entire group. Standards for assignments Most assignments and exam problems will be mathematical or theoretical in nature,and will require you to prove your answer correct.Grading of all such problems(homework and exam)will be both on the basis of correctness and on logical consistency and completeness,i.e.,“mathematical style”.It is your obligation to provide a compelling argument that forces the reader to believe the result,not just notes from which an argument could be constructed.In particular,correct formulas or pseudo-code are nota complete solution by themselves;their significance and the logic oftheir application need to be explained.A typical assignment is to design an efficient algorithm for a givenproblem.When giving an algorithm,the following two things should always be included,unless the problem explicitly says not to:a cor-rectness argument,showing why the algorithm solves the problem;and a time analysis,giving the order of the worst-case runtime(in O-notation).One problem on each homework assignment will involve implementing an algorithm,and reporting time usage data on a variety of inputs (which will be either completely specified or specified as a distribution on random instances).This implementation may be done in any lan-guage,and be run on any machine.Your solution should only includea brief description of your program;in particular,we will not readactual code,so you needn’t hand it in.You should hand in only a description of your program,specifying the basic algorithm used,any modifications that you made to this algorithm,the language used,the performance characteristics of the machine used,and timing informa-tion for the various inputs you ran the program on.Discuss whether the timing results seemed consistent with the asymptotic analysis;if not,what in your opinion is the reason?The TA or I may ask to seea demonstration of your program on other instances.3Lateness Policy Late homework will be accepted until I give out an answer key and no later.So you have to be no later than me.I will also not accept homework after thefirst10minutes of the class it is due.Working on the homework is no excuse for missing class or not paying attention.Reading Schedule We will not be able to cover every example on each topic in the text in class.(JS=Johnsonbaugh and Shaeffer,required text;JE=JeffEdmonds’How to Think About Algorithms.)You are expected to read the other sub-sections independently.In particular, we will only quickly review the material in JE Chapter1=JS Ch.1,2.1-4and the basic data structures(JE Chapter2&3=JS Ch. 2.5,2.6,3).These should be familiar from CSE100and21,but will beused heavily in this class.Reading this material in advance is a good plan.To help you plan your reading,here is a tentative schedule of topics to be covered in class,and the corresponding sections of the text to be read.I reserve the option to change the schedule at a later point.You shouldfind most of the material in EITHER of:JS=Johnsonbaugh and Schaefer,Algorithms OR JE=JeffEdmonds,How To Think about Algorithms.(You don’t need to read both textbooks,but sometimes it may be helpful to see things explained in different ways.)1.Background:This is material we are only quickly reviewing.Youshould be familiar with this material from previous classes.Ifyou are having trouble with this material,you will need to workmuch harder throughout the course to keep up.Order notation,time analysis,recurrence relations:(JE Chapter1,JS Chapter2.3,2.4)Basic data structures:lists,arrays,graphs,trees stacks,heaps(JS:Chapters2.5,2.6,3JE:Chapters2and5.1).This ma-terial should be covered in CSE100and21.Read it and trysome exercises.If you have any problems,go back and read thechapter thoroughly.2.Basic iterative algorithms.Loop invariants and correctness proofs.Time analysis.(JE,Chapter3;JS pp37-38,section2.3).Exam-ples:Largest sum consecutive subarray;depth-first and breadth-first search(JS Ch4.2, 4.3,JE8.2,8.4),topological sort(JS:Chapter4.4,JE8.5).(2lectures).43.Maximizing Efficiency in Iterative ing restructur-ing,pre-processing and data structures to get the most efficientversions of algorithms.Graph and integer ingdata structures such as lists,arrays,heaps and balanced searchtrees.Sorting(JS,Chapter6),skylines,auction problem,maxi-mum min-degree subgraph.(2lectures).4.Greedy Algorithms(JE,Chapter10;JS,Chapter7)When do greedy algorithmswork?Proof techniques for optimality of greedy algorithms.Ex-amples:scheduling(JE,10.2.1);Minimum spanning trees(JS,7.2,7.3;JE,10.2.3);Dijkstra’s Algorithm(JS7.4;JE8.3);Inde-pendent set of a tree;others to be added.3lectures.5.Recursive algorithms and their analysis.Correctness proofs bystrong induction.Recurrence relations.(JE,Chapter5,6,JS2.4).Euclid’s GCD algorithm(JE,Chapter4.3),(1lectures.)6.Arithmetic and numerical algorithms.Euclid’s GCD algorithmrevisited.Amortized analysis of GCD.(1lecture)7.Divide-and-Conquer.(JS,Chapter5.)Examples:Mergesort(JS,5.2);Multiplicationof large integers;all distances in balanced binary tree;closestpair of points(JS,5.3);Quicksort(JS,6.2);The analysis of somedivide-and-conquer algorithms will require Lemma2.4.15of JS,which is also in Chapter1.6of JE.(3lectures.)8.Backtracking(JS,Chapter4.5,JE Chapter11)I spend more time on thisbecause Dynamic Programming can be viewed as a modificationof Backtracking.Examples:Independent set;n queens((JS4.5,JE11.2.3),graphcoloring,Hamiltonian Cycle(JS,4.5),and addition chains.2lectures.9.Dynamic Programming,(JS,Chapter8;JE Chapter12)Examples:Fibonnacci numbers(JS,8.1);Longest increasing sub-sequence;Shortest paths(JE12.2.7,JS8.5);Matrix Multiplication(JE,12.2.5,JS8.3);Editdistance=Longest Common subsequence(JE,12.2.2,JS,8.4);schedul-ing.3lectures.10.Reductions and NP-completeness5(JS,Ch10,11;JE,Ch.13)When can one type of problem“code”another;NP,a format for search problems;universal(NP-complete)search problems.Coping with intractibility.2lectures Assignment and Exam schedule To help you plan,here is the tentative assignment and exam schedule:1.April5:Calibration homework(order,recurrences,simple algo-rithm analysis and correctness)due2.April19:Homework1(efficient versions of algorithms)due3.April27:Discussion section:practice mid-term(order,solvingrecurrences,algorithm analysis and correctness,data structures,greedy algorithms)4.May3:homework2(greedy algorithms)due5.May11:discussion section:midterm(order,solving recurrences,algorithm analysis and correctness,data structures,greedy algo-rithms)6.May17,homework3(divide-and-conquer and back-tracking)due.7.June2,homework4(dynamic programming)due8.June7,Final exam.6。

英语 算法 -回复

英语 算法 -回复

英语算法-回复The Basics of Algorithm: Understanding the Core Principles [英语算法]Introduction:In the world of computer science, algorithms are the building blocks of software development. They are step-by-step instructions that guide computers to perform specific tasks efficiently and effectively. Understanding algorithms is crucial for programmers and developers, as it helps enhance the performance and functionality of their programs. This article will delve into the basics of algorithms, exploring their core principles, and providingstep-by-step explanations.1. What is an Algorithm?An algorithm is a set of well-defined instructions that solve a specific problem or perform a specific task. It can be thought of as a recipe that defines a series of steps to achieve a desired outcome. Algorithms are at the heart of software development, enabling computers to process and manipulate data in a logical and structured manner.2. Understanding Algorithm Complexity:Algorithm complexity refers to the resources (time and space) required by an algorithm to solve a problem. It is crucial to analyze the complexity of an algorithm to determine its efficiency and scalability. Two main factors are considered when analyzing algorithm complexity: time complexity and space complexity.a. Time Complexity:Time complexity measures the amount of time an algorithm takes to solve a problem. It focuses on how the algorithm's performance changes with the input size. Common notations used to describe time complexity include O(1), O(n), O(log n), O(n^2), etc. These notations give an idea of how the algorithm's performance scales as the input size grows.b. Space Complexity:Space complexity measures the amount of memory an algorithm requires to solve a problem. It determines how much additional memory the algorithm consumes as the input size increases. Common notations used for space complexity include O(1), O(n), O(n^2), etc. These notations provide insights into the memory usage of the algorithm.3. Algorithm Design Techniques:Several algorithm design techniques are employed to create efficient and optimized algorithms. Here are a few commonly used techniques:a. Divide and Conquer:This technique involves breaking down a large problem into smaller subproblems, solving them independently, and then combining the results to obtain the final solution. Merge Sort and Quick Sort are examples of divide and conquer algorithms.b. Dynamic Programming:Dynamic programming breaks down a problem into smaller overlapping subproblems and solves them in a bottom-up manner. It stores the results of subproblems to avoid redundant computations. The Fibonacci series is often used to demonstrate dynamic programming.c. Greedy Algorithm:Greedy algorithms make locally optimal choices at each step, with the hope that these choices will lead to a globally optimalsolution. They do not consider the future consequences of their choices. Dijkstra's algorithm for finding the shortest path is a classic example.4. Analyzing and Comparing Algorithms:When designing algorithms, it is essential to analyze and compare them to determine their effectiveness and efficiency. Some key factors to consider when analyzing and comparing algorithms include:a. Worst-case, Average-case, and Best-case Analysis:Algorithms may perform differently based on various input scenarios. Analyzing their performance in worst-case, average-case, and best-case scenarios helps understand their strengths and weaknesses.b. Big O Notation:The Big O notation provides an upper bound on the time or space complexity of an algorithm. By comparing algorithms' Big O notations, one can determine which algorithm is more efficient for a given problem.c. Benchmarking:Benchmarking involves running algorithms on various inputs and measuring their performance. This helps understand the practical impact of an algorithm and allows for real-world comparisons.Conclusion:Algorithms are the fundamental building blocks of software development. Understanding their core principles, analyzing their complexity, and employing appropriate design techniques are essential for any programmer or developer. By continuously improving algorithms, we can create more efficient and powerful software that can tackle complex problems with ease.。

算法设计与分析(DesignandAnalysisofAlgorithms

算法设计与分析(DesignandAnalysisofAlgorithms

算法设计与分析(Design and Analysis of Algorithms)主讲:冼楚华Email: ****************.cnHomepage:QQ:89071086 (可QQ答疑)办公室:(TBD)助教:曹旭(QQ:948623560, Email: ****************, Office: B3-440)参考教材:算法设计技巧与分析(Algorithms Design Techniques and Analysis)。

(沙特)阿苏外耶著。

电子工业出版社。

定价:36.0 RMB课时:1-11周,14-18周,64课时(含16课时实验课,时间及地点另定)考核方式:平时成绩20% + 实验成绩20% + 期末考试60%,平时成绩包含课上测试、上课表现等;实验成绩包含课后上机作业及模拟竞赛等课程网站:/algorithms/或者访问:,然后点击Teaching --> Design and Analysis of Algorithms (Spring 2016)网站提供每次讲课的PPT课件要点、作业答案以及与本课程相关的资源下载。

在线做题网站:/oj/ (华南理工大学)/onlinejudge/ (浙江大学ACM网站) (北京大学ACM网站)注意:如有问题,请发送邮件,收到邮件后,将会在两个工作日内回复。

发送邮件请注明学号及姓名,无学号及姓名的邮件恕不回复。

Design and Analysis of AlgorithmsInstructor:Prof. Chuhua Xian (冼楚华)Email: ****************.cnHomepage:QQ:89071086Office Room:(TBD)Teaching Assistants:CAO Xu (QQ:948623560, Email: ****************, Office: B3-440)Textbook:Algorithms Design Techniques and Analysis. (Saudi Arabia) M. H. Alsuwaiyel. Publishing House of Electronic Industry (电子工业出版社). Price:36.0 RMBCouse Time:Week 1rd-11th, 14th-18th, 64 lessons (including 16 lessons for experiment; Times and Room: to be announced)Final Grade:Performance in class (20%) + homework and experiments (20%) + final examination (60%)Website:/algorithms/Or via my homepage:, click ‘Teaching --> Design and Analysis of Algorithms (Spring 2015)’The slides, some answers of the homework and links of the resources will be put on this website.Online Judge:/oj/ South China University of Technology)/onlinejudge/ (Zhejiang University) (Peking University)If you have any questions, please feel free to contact me by email. I will reply within two working days. Please list your name or student ID when you send me an email. Thank you.。

算法经典书籍合集全10本

算法经典书籍合集全10本

如果计算机系只开三门课,那么这三门课就一定是:离散数学,数据结构与算法,编译原理。

如果只开一门课,那剩下的就一定是:数据结构与算法。

Niklaus Wirth说:算法+数据结构=程序,不说废话了,下面列出一份数据结构算法书目,先从最著名的说起1.原书名:The Art of Computer Programming作者:Donald E.Knuth难度:*****个人评价:*******推荐程度:****本书是算法分析的经典名作(用经典不太恰当,应该是圣经或史诗),被科学美国人列为20世纪12大科学名著之一(和Dirac的量子力学,Einstein 的广义相对论,von Neumann 的博弈论的著作等齐名)。

其亮点在于其超乎寻常的数学技巧,要求读者拥有极高的数学修养,只要你坚持忍耐,一旦读懂了,你的算法和程序设计水平也会达到更高的档次,你会对程序设计有一种截然不同的体会和领悟,就是“道”(Tao)。

书的排版很漂亮(得益于作者的Tex系统),看起来很舒服。

作者的文笔很好,写得生动活泼,读起来荡气回肠(英文版)。

习题多且精华,触及算法和程序本质,书后有几乎所有习题的答案(占了整全书篇幅的1/4),书中的分析方法体现了作者严谨的风格。

不过本书的程序不是用我们熟悉的高级语言描述的,而是作者设计的MIX语言。

整套书原计划出七卷,现在出了三卷:基本算法,半数值算法,排序和搜索,第四卷组合算法跳票了20年,Knuth称在2008年推出。

本书有中文版,不过建议读者选用英文版,因为都学到这个程度了,英语应该不会有大困难了。

引用一句话“在我们的有生之年,可能会看到C++的消亡,但Knuth和他的程序设计艺术,将永远留在我们的心里。

”2原书名:Introduction to Algorithms作者:Thomas H.Cormen,Charles E.Leiserson,Ronald L.Rivest,Clifford Stein难度:***个人评价:*****推荐程度:*****本书俗称CLRS(作者名字的简写),算法的经典教材,堪称算法分析著作中的“独孤九剑”。

算法分析与设计 第二版 英文版 (潘彦 著) 清华大学出版社 课后答案--solu7

算法分析与设计 第二版 英文版 (潘彦 著) 清华大学出版社 课后答案--solu7
This file contains the exercises, hints, and solutions for Chapter 7 of the book ”Introduction to the Design and Analysis of Algorithms,” 2nd edition, by A. Levitin. The problems that might be challenging for at least some students are marked by ; those that might be difficult for a majority of students are marked by .
1
8. a. Write a program for multiplying two sparse matrices, a p-by-q matrix A and a q-by-r matrix B.
b. Write a program for multiplying two sparse polynomials p(x) and q(x) of degrees m and n, respectively.
tree’s vertices in constant time.
7. The following technique, known as virtual initialization, provides a
网 time-efficient way to initialize just some elements of a given array A[0..n −Fra 网 案 答 后 课2
Hints to Exercises 7.1
1. Yes, it is possible. How? 2. Check the algorithm’s pseudocode to see what it does upon encountering

算法分析算法复习题中英文

算法分析算法复习题中英文

〔有翻译〕1.The O-notation provides an asymptotic upper bound. The -notationprovides an asymptotic lower bound. The Θ-notation asymptoticallya function form above and below.2.To represent a heap as an array,the root of tree is A[1], and giventhe index i of a node, the indices of its parent Parent(i) { return ëi/2û; },left child,Left(i) { return 2*i; },right child, right(i) { return 2*i + 1; }.代表一个堆中的一个数组,树的根节点是A[1],并且给出一个节点i,那么该节点的父节点是左孩子右孩子3.Because the heap of n elements is a binary tree, the height of anynode is at most Q(lg n).因为n个元素的堆是一个二叉树,任意节点的树高最多是4.In optimization problems, there can be many possible solutions. Eachsolution has a value, and we wish to find a solution with the optimal (minimum or maximum) value. We call such a solution an optimal solution to the problem.在最优化问题中,有很多可能的解,每个解都有一个值,我们希望找到一个最优解〔最大或最小〕,我们称这个解为最优解问题。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

参考教材
1、算法设计与分析 王晓东 清华大学出版社 2、算法分析与设计 (美)Michael T. Goodrich Roberto Tamassia 著人民邮电出版 社 3、算法设计技巧与分析(沙特) M.H. Alsuwaiyel 著 电子工业出版社 4、算法设计与分析 郑宗汉 清华大学出版社 5、算法导论, Thomas H.Cormen Charles E.Leiserson Ronald L.Rivest Clifford Stein 著, 潘金贵等译, 机械工业出版社
Knigsberg桥对应的图 Knigsberg桥对应的图
欧拉图
定理
G是无向连通图,则G是欧拉图G中所有顶点度 数都是偶数。 定义 如果无向连通图G的每条边一次且仅一次的通路称为图 G的欧拉通路。 定理 具有一条连接顶点vi和vJ的欧拉通路的充分条件是vi和vJ 是G中仅有的具有奇数度的顶点。
货郎担问题
Introduction to Algorithms
授课教师:宋玲
E-Mail: song_ling@
山东建筑大学计算机学院(2009年9月)
Lecture 1
What Do You Want?
a good score to be more intelligent the beauty of math the way to research If you listen to me carefully from now and finish every homework,I promise that you will get all of them.
棋盘覆盖 在一个2k×2k 个方格组成的棋盘中,恰有一个方格与其它方格 不同,称该方格为一特殊方格,且称该棋盘为一特殊棋盘。在 棋盘覆盖问题中,要用图示的4种不同形态的L型骨牌覆盖给定 的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌 不得重叠覆盖。
Longest Common Subsequence (LCS)
本课程的难点和学习方法
双语学习 较多的数学知识和推倒(第一部分) 预习-上课认真听讲-复习(重点词汇) 预备的数学知识(p51-57) 本次课和下节课所讲重点内容在教材上划 出。
教辅用书
教材
Introduction to Algorithms(Second Edition),(美) Thomas H.Cormen Charles E.Leiserson Ronald L.Rivest Clifford Stein,高等教育出版社
本课程的教学目的及要求 (2/2)
解释各主要的排序算法, 解释各主要的排序算法,练习这些算法 的分析及它们所包含的设计策略, 的分析及它们所包含的设计策略,实现 将排序作为子程序的算法, 将排序作为子程序的算法,推算比较排 序法执行时间的下限, 序法执行时间的下限,和解释怎样可以 克服这些界限; 克服这些界限; 实现图论算法和使用图论计算为关键的 算法,分析它们, 算法,分析它们,以及如何使用图来模 拟工程问题; 拟工程问题;
一个货郎要去若干城镇卖货,然后回到 出发地,给定各城镇之间所需的旅行时 间后,应怎样计划他的路线,使他能去 每个城镇恰好一次而且总时间最短? 实质:无向加权图,寻找最短的H回路 的问题。
货郎担问题
用图论的术语说,就是在一个赋权完全 图中,找出一个具有最小权的Hamilton 圈(包含图G的每个顶点的圈)。 这个问题目前还没有有效的算法。 30!=265,252, 859,812,191,058,636 308,480,000,000 有兴趣的同学编程序实现,看你能解决 多大规模的问题。
七桥问题
18世纪的七桥问题—穿过Knigsberg城的七 座桥,要求每座桥通过一次且仅通过一次。 Euler1736年证明了不可能存在这样的路线。
Euler 定理
定义(欧拉图) 通过无向连通图G的每条边一次且仅有一次 的回路称为欧拉回路。具有欧拉回路的图为 欧拉图。 定义包含多重图在内,即欧拉回路中允许顶 点重复出现。
注: (1)欧拉道路未必是哈密顿道路,因为 欧拉道路可以经过同一顶点多次。 哈密顿道路未必是欧拉道路,因为哈密顿道路不 一定要经过E中所有的边。 (2)基本道路必然是简单道路。
四色问题
著名的世界难题“四色猜想” :一张地图, 用一种颜色对一个地区着色,那么一共只需 要四种颜色就能保证每两个相邻的地区颜色 不同。
课前讨论
在学期中将会有大约4次的讨论课,共四组(大约20 个人为1个小组),15分钟问题的描述和讲解。题目可以 自己拟定,也可以由教师指定。 目的是让同学们及时复习,培养团队合作以及主动 学习精神,记入平时成绩。
作业以及实验报告中 算法描述要求
当被指定“用一个算法”来解决某个问题。应该 提供以下部分: 1. 算法的描述:伪代码(pseudocode)。 2. 最少以一个工作例子或图表来更明确的显示你 的算法是怎样工作的。 3. 算法正确性的一个证明(或表示)(*)。 4. 算法执行时间的分析。
你能解决以下问题吗?
古城哥尼斯堡,景致迷人,碧波荡漾的普瑞格尔河横贯 其境。普瑞格尔河的两岸及河中的两个美丽的小岛,由七座 桥连接组成了这座秀色怡人的城市(如图)。市民们喜欢四 处散步,于是便产生这样的问题:是否可以设计一种方案, 使得人们从自己家里出发,经过每座桥恰好一次,最后回到 家里。这便是著名的“哥尼斯堡七桥问题”。热衷于这个有 趣的问题的人们试图解决它,但一段时间内竟然没有人能给 出答案。后来,问题传到了著名数学家欧拉那里,居然也激 起了他的兴趣。他从人们寻求路线屡遭失败的教训中敏锐地 领悟到,也许这样的方案根本就不存在。欧拉经过悉心的研 究,1736年,年方29岁的欧拉终于解决了这个问题,并向圣 彼得堡科学院递交了一份题为《哥尼斯堡的七座桥》的论文。 论文不仅仅是解决了这一难题,而且引发了一门新的数学分 支——图论的诞生。
第三部分(Part III) 数据结构(Data Structures) 第十章 基本的数据结构(Elementary Data Structures) 第十一章 散列表(Hash Tables) 第十二章 二叉查找树(Binary Search Trees) 第十三章 红-黑树(Red-Black Trees) 第十四章 扩充的数据结构(Augmenting Data Structures) 第四部分(Part IV) 高级的设计与分析技术(Advanced Design and Analysis Techniques) 第十五章 动态规划(Dynamic Programming) 第十六章 贪婪算法(Greedy Algorithms) 第十七章 分摊分析(Amortized Analysis)
哈密顿图
一. 哈密顿道路问题: 1859年发明的一种游戏。 在一个实心的正十二面体,20个顶点标上 世界著名大城市的名字,要求游戏者从某一城 市出发,遍历各城市一次,最后回到原地。 这就是“绕行世界”问题。即找一条经过 所有顶点(城市)的基本道路(回路)。
哈密顿图
定义 通过图G的每个顶点一次且仅一次的回路称为 哈密顿回路。具有哈密顿回路的图称为哈密顿 图。哈密顿通路是通过图G的每个顶点一次且 仅一次的通路。
作业要求
在学期中将会指定 多次作业。要求同学上交并给出 成绩,作为部分期末成绩。 作业的目的是让同学有练习掌握课堂内容的机会。 因此,鼓励同学们合作解题。虽然鼓励合作解答题目, 但是,要求独立写下答案,并要求在习题上写下合作者 , 们的名字。如果没有跟任何人合作,应该写下“合作者: 无”。如果答案是由研究而来(例如:互联网),注明你 的资料来源,但依你自己的方法写下答案。
相关事项
教学方式:理论(48学时),实践(16ห้องสมุดไป่ตู้时) 最终的评分会基于作业、平时表现、实 验报告和期末考 先修课程:《离散数学》《数据结构》 《数值分析》《C语言程序设计》 作业:每个部分交一次 答疑时间:周四下午2:30 答疑地点:XX305
Grading policy:
Homework: 8% Experiment Run Results: 8% Experiment Paper: 8% Arrival: 6% Final Exam: 70%
Application: comparison of two DNA strings Ex: X= {A B C B D A B }, Y= {B D C A B A} Longest Common Subsequence: X= AB C BDAB Y= B D CAB A Brute force algorithm would compare each subsequence of X with the symbols in Y
第五部分(Part V) 高级的数据结构(Advanced Data Structures) 第十八章 B-树(B-Trees) 第十九章 二项式堆(Binomial Heaps) 第二十章 斐波纳契堆(Fibonacci Heaps) 第二十一章 不相交集的数据结构(Data Structures for Disjoint Sets) 第六部分(Part VI) 图算法(Graph Algorithms) Part VI Graph Algorithms 第二十二章 基本的图算法(Elementary Graph Algorithms) 第二十三章 最小生成树(Minimum Spanning Trees) 第二十四章 单源最短路径(Single-Source Shortest Paths) 第二十五章 全对的最短路径(All-Pairs Shortest Paths) 第二十六章 最大流(Maximum Flow) 第七部分(Part VII) 精选的主题(Selected Topics) 第二十七章 排序网络(Sorting Networks)
本课程的教学目的及要求 (1/2)
分析算法的渐进效率;掌握最坏, 分析算法的渐进效率;掌握最坏,平均及最好情 况下复杂性的分析; 况下复杂性的分析; 叙述分治法的模式和解释当什么情况算法设计会 需要它,练习使用此模式的算法, 需要它,练习使用此模式的算法,实现并推导出 分治法的递归描述; 分治法的递归描述; 叙述动态规划的模式和解释当什么情况算法设计 会需要它,练习使用此模式的算, 会需要它,练习使用此模式的算,实现并分析动 态规划算法。 态规划算法。 叙述贪心算法的模式和解释当什么情况算法设计 会需要它,练习使用此模式的算法, 会需要它,练习使用此模式的算法,实现并分析 贪心算法; 贪心算法;
相关文档
最新文档