算法导论第二十二章答案
算法导论 答案 (2)
算法导论答案算法导论概述《算法导论》是一本经典的计算机科学教材,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein合著。
这本书详细介绍了算法的设计、分析和实现,并涵盖了算法导论领域的许多重要概念和技术。
本文将为你提供一些关于《算法导论》中一些常见问题的答案。
1. 什么是算法?算法是一系列明确定义的步骤,用于解决特定问题或完成特定任务。
它可以是一个计算过程、一个程序或一个有限的操作序列。
算法通常用于计算和数据处理领域,是计算机科学的核心概念。
2. 为什么学习算法很重要?学习算法的重要性体现在以下几个方面:•提高问题解决能力:算法是解决问题的有效工具。
学习算法可以帮助我们思考和理解问题,并设计出相应的解决方案。
•优化计算性能:算法的设计和分析可以帮助我们提高计算的效率和性能。
合适的算法可以在短时间内处理大规模数据集和复杂计算任务。
•促进技术创新:算法是许多技术和应用的基石,包括人工智能、机器学习、数据挖掘等。
学习算法可以为我们打开更多的研究和创新机会。
3. 《算法导论》提供了哪些内容?《算法导论》这本书详细介绍了算法的基本概念和设计技巧,并提供了许多典型算法的实现和分析。
以下是该书的一些主要内容:•算法分析:对算法进行时间复杂度和空间复杂度的理论分析,帮助我们评估算法的效率和性能。
•排序和查找算法:介绍了各种排序算法(如插入排序、归并排序、快速排序)和查找算法(如二分查找、哈希表)。
•图算法:讨论了图的表示方法和图搜索算法(如深度优先搜索、广度优先搜索)以及最短路径算法(如Dijkstra算法)等。
•动态规划和贪心算法:介绍了动态规划和贪心算法的原理和应用,用于解决具有最优子结构性质的问题。
•分治算法和递归思想:讲解了分治算法的基本原理,并提供了许多使用递归思想解决问题的例子。
•NP完全问题:探讨了NP完全问题的性质和求解方法,引导了读者进入计算复杂性理论的领域。
算法导论 第三版 第22章 答案 英
Michelle Bodnar, Andrew Lohr April 12, 2016
Exercise 22.1-1 Since it seems as though the list for the neighbors of each vertex v is just an undecorated list, to find the length of each would take time O(out − degree(v )). So, the total cost will be v∈V O(outdegree(v )) = O(|E | + |V |). Note that the |V | showing up in the asymptotics is necessary, because it still takes a constant amount of time to know that a list is empty. This time could be reduced to O(|V |) if for each list in the adjacency list representation, we just also stored its length. To compute the in degree of each vertex, we will have to scan through all of the adjacency lists and keep counters for how many times each vertex has appeared. As in the previous case, the time to scan through all of the adjacency lists takes time O(|E | + |V |). Exercise 22.1-2 The adjacency list representation: 1 : 2, 3 2 : 1, 4, 5 3 : 1, 6, 7 4:2 5:5 6:3 7 : 3.
算法导论(第三版)-复习-第六部分图论22-26[转]
算法导论(第三版)-复习-第六部分图论22-26[转]22习题22.1-5 有向图G(V, E)的平⽅图。
链表表⽰时,对每结点u的Adj[u]中所有v加⼊队列,后边出队边将Adj[v]加⼊Adj[u]中。
矩阵表⽰时,若w[i, j]、w[j, k]同时为1则将w[i, k]置1.习题22.1-6 O(V)的时间寻找通⽤汇点。
汇点的特征是邻接矩阵的第j列除[j, j]外所有元素为1. 可将每⾏调整[j ,j]后作为⼀个整数,所有整数与运算,为1的位是汇点。
习题22.1-7 有向⽆环图的关联矩阵B,BB’每个元素C[i, j]=∑B[i, k]*B’[k, j]=∑B[i, k]*B[j, k],即同时进i, j两结点与同时出i, j的结点总数-⼀进⼀出i, j两结点的结点总数。
习题22.2-7 类似BFS,d mod2为0则标为B(娃娃脸),d mod2为1则标为H(⾼跟鞋)。
但若有边连接相同类的结点,则⽆法划分。
wrestler(G){for each u in G{(u,v)=Adj[u];if(v.mark==u.mark){throw error;}if(v.d==NIL) {v.d=u.d+1; v.mark=v.d mod 2;}}}习题22.2-8 任意点之间的最短路径。
重复的Dijktra算法或Floyd-Warshall算法习题22.2-9 ⽆向图扩展为有向图。
问题变成要遍历所有边⼀次。
访问结点u时,将u的⼦结点v的其他边都可视为⼦集v,问题等价于u到v,访问v的集合,v到u。
u标为visiting⼊列,然后访问v,v标为visiting⼊列,然后访问v的后继结点,访问过的边标为visited,返回到visiting的点时,如果该点所有连接的边都标为visited只剩⼀条返回上级的边,则返回上级结点并将点标为visited,v出列,访问u的其他⼦结点,最终u出列。
全部结点出列后达到遍历所有边⼀次。
算法导论课程作业答案
算法导论课程作业答案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。
藏书阁-《算法导论》常见算法总结
常见算法总结分治法分治策略的思想:顾名思义,分治是将一个原始问题分解成多个子问题,而子问题的形式和原问题一样,只是规模更小而已,通过子问题的求解,原问题也就自然出来了。
总结一下,大致可以分为这样的三步:分解:将原问题划分成形式相同的子问题,规模可以不等,对半或2/3对1/3的划分。
解决:对于子问题的解决,很明显,采用的是递归求解的方式,如果子问题足够小了,就停止递归,直接求解。
合并:将子问题的解合并成原问题的解。
这里引出了一个如何求解子问题的问题,显然是采用递归调用栈的方式。
因此,递归式与分治法是紧密相连的,使用递归式可以很自然地刻画分治法的运行时间。
所以,如果你要问我分治与递归的关系,我会这样回答:分治依托于递归,分治是一种思想,而递归是一种手段,递归式可以刻画分治算法的时间复杂度。
所以就引入本章的重点:如何解递归式?分治法适用的情况分治法所能解决的问题一般具有以下几个特征:1. 该问题的规模缩小到一定的程度就可以容易地解决2. 该问题可以分解为若干个规模较小的相同问题,即该问题具有最优子结构性质。
3. 利用该问题分解出的子问题的解可以合并为该问题的解;4. 该问题所分解出的各个子问题是相互独立的,即子问题之间不包含公共的子子问题。
第一条特征是绝大多数问题都可以满足的,因为问题的计算复杂性一般是随着问题规模的增加而增加;第二条特征是应用分治法的前提它也是大多数问题可以满足的,此特征反映了递归思想的应用;、第三条特征是关键,能否利用分治法完全取决于问题是否具有第三条特征,如果具备了第一条和第二条特征,而不具备第三条特征,则可以考虑用贪心法或动态规划法。
第四条特征涉及到分治法的效率,如果各子问题是不独立的则分治法要做许多不必要的工作,重复地解公共的子问题,此时虽然可用分治法,但一般用动态规划法较好。
——————————————————————————————最大堆最小堆1、堆堆给人的感觉是一个二叉树,但是其本质是一种数组对象,因为对堆进行操作的时候将堆视为一颗完全二叉树,树种每个节点与数组中的存放该节点值的那个元素对应。
《算法导论(第二版)》(中文版)课后答案
5
《算法导论(第二版) 》参考答案 do z←y 调用之前保存结果 y←INTERVAL-SEARCH-SUBTREE(y, i) 如果循环是由于y没有左子树,那我们返回y 否则我们返回z,这时意味着没有在z的左子树找到重叠区间 7 if y≠ nil[T] and i overlap int[y] 8 then return y 9 else return z 5 6 15.1-5 由 FASTEST-WAY 算法知:
15
lg n
2 lg n1 1 2cn 2 cn (n 2 ) 2 1
4.3-1 a) n2 b) n2lgn c) n3 4.3-4
2
《算法导论(第二版) 》参考答案 n2lg2n 7.1-2 (1)使用 P146 的 PARTION 函数可以得到 q=r 注意每循环一次 i 加 1,i 的初始值为 p 1 ,循环总共运行 (r 1) p 1次,最 终返回的 i 1 p 1 (r 1) p 1 1 r (2)由题目要求 q=(p+r)/2 可知,PARTITION 函数中的 i,j 变量应该在循环中同 时变化。 Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; 7.3-2 (1)由 QuickSort 算法最坏情况分析得知:n 个元素每次都划 n-1 和 1 个,因 为是 p<r 的时候才调用,所以为Θ (n) (2)最好情况是每次都在最中间的位置分,所以递推式是: N(n)= 1+ 2*N(n/2) 不难得到:N(n) =Θ (n) 7.4-2 T(n)=2*T(n/2)+ Θ (n) 可以得到 T(n) =Θ (n lgn) 由 P46 Theorem3.1 可得:Ω (n lgn)
中科大算法导论第一,二次和第四次作业答案
2.2-3 再次考虑线性查找问题 (见练习2.1-3)。在平均情况 下,需要检查输入序列中的多 少个元素?假定待查找的元素 是数组中任何一个元素的可能 性是相等的。在最坏情况下有 怎样呢?用Θ形式表示的话,线 性查找的平均情况和最坏情况 运行时间怎样?对你的答案加 以说明。 • 线性查找问题 • 输入:一列数A=<a1,a2,…,an>和一 个值v。 • 输出:下标i,使得v=A[i],或者当 v不在A中出现时为NIL。 • 平均情况下需要查找 (1+2+…+n)/n=(n+1)/2 • 最坏情况下即最后一个元素为待 查找元素,需要查找n个。 • 故平均情况和最坏情况的运行时 间都为Θ(n)。
• 2.3-2改写MERGE过程,使之不使 用哨兵元素,而是在一旦数组L或R 中的所有元素都被复制回数组A后, 就立即停止,再将另一个数组中 余下的元素复制回数组A中。 • MERGE(A,p,q,r) 1. n1←q-p+1 2. n2 ←r-q 3. create arrays L[1..n1] and R[1..n2] 4. for i ←1 to n1 5. do L*i+ ←A*p+i-1] 6. for j ←1 to n2 7. do R*j+ ←A*q+j+ 8. i ←1 9. j ←1
10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.
k ←p while((i<=n1) and (j<=n2)) do if L[i]<=R[j] do A[k]=L[i] i++ else do A[k]=R[j] j++ k++ while(i<=n1) do A[k++]=L[i++] while(j<=n2) do A[k++]=R[j++]
中科大算法导论作业标准标准答案
第8次作业答案16.1-116.1-2543316.3-416.2-5参考答案:16.4-1证明中要三点:1.有穷非空集合2.遗传性3.交换性第10次作业参考答案16.5-1题目表格:解法1:使用引理16.12性质(2),按wi单调递减顺序逐次将任务添加至Nt(A),每次添加一个元素后,进行计算,{计算方法:Nt(A)中有i个任务时计算N0 (A),…,Ni(A),其中如果存在Nj (A)>j,则表示最近添加地元素是需要放弃地,从集合中删除};最后将未放弃地元素按di递增排序,放弃地任务放在所有未放弃任务后面,放弃任务集合内部排序可随意.解法2:设所有n个时间空位都是空地.然后按罚款地单调递减顺序对各个子任务逐个作贪心选择.在考虑任务j时,如果有一个恰处于或前于dj地时间空位仍空着,则将任务j赋与最近地这样地空位,并填入; 如果不存在这样地空位,表示放弃.答案(a1,a2是放弃地):<a5, a4, a6, a3, a7,a1, a2>or <a5, a4, a6, a3, a7,a2, a1>划线部分按上表di递增地顺序排即可,答案不唯一16.5-2(直接给个计算例子说地不清不楚地请扣分)题目:本题地意思是在O(|A|)时间内确定性质2(性质2:对t=0,1,2,…,n,有Nt(A)<=t,Nt(A)表示A中期限不超过t地任务个数)是否成立.解答示例:思想:建立数组a[n],a[i]表示截至时间为i地任务个数,对0=<i<n,如果出现a[0]+a[1]+…+a[i]>i,则说明A不独立,否则A独立.伪代码:int temp=0;for(i=0;i<n;i++) a[i]=0; ******O(n)=O(|A|)for(i=0;i<n;i++) a[di]++; ******O(n)=O(|A|)for(i=0;i<n;i++) ******O(n)=O(|A|) {temp+=a[i];//temp就是a[0]+a[1]+…+a[i]if(temp>i)//Ni(A)>iA不独立;}17.1-1(这题有歧义,不扣分)a) 如果Stack Operations包括Push Pop MultiPush,答案是可以保持,解释和书上地Push Pop MultiPop差不多.b) 如果是Stack Operations包括Push Pop MultiPush MultiPop,答案就是不可以保持,因为MultiPush,MultiPop交替地话,平均就是O(K).17.1-2本题目只要证明可能性,只要说明一种情况下结论成立即可17.2-1第11次作业参考答案17.3-1题目:答案:备注:最后一句话展开:采用新地势函数后对i 个操作地平摊代价:)1()())1(())(()()(1''^'-Φ-Φ+=--Φ--Φ+=Φ-Φ+=-Di Di c k Di k Di c D D c c i i i i i i17.3-2题目:答案:第一步:此题关键是定义势能函数Φ,不管定义成什么首先要满足两个条件 对所有操作i ,)(Di Φ>=0且)(Di Φ>=)(0D Φ比如令k j+=2i ,j,k 均为整数且取尽可能大,设势能函数)(Di Φ=2k;第二步:求平摊代价,公式是)1()(^-Φ-Φ+=Di Di c c i i 按上面设置地势函数示例:当k=0,^i c =…=2当k !=0,^i c =…=3 显然,平摊代价为O(1)17.3-4题目:答案:结合课本p249,p250页对栈操作地分析很容易有下面结果17.4-3题目:答案:αα=(第i次循环之后地表中地entry 假设第i个操作是TABLE_DELETE, 考虑装载因子:inum size数)/(第i次循环后地表地大小)=/i i第12 次参考答案19.1.1题目:答案:如果x不是根,则degree[sibling[x]]=degree[child[x]]=degree[x]-1如果x是根,则sibling为二项堆中下一个二项树地根,因为二项堆中根链是按根地度数递增排序,因此degree[sibling[x]]>degree[x]19.1.2题目:答案:如果x是p[x]地最左子节点,则p[x]为根地子树由两个相同地二项树合并而成,以x为根地子树就是其中一个二项树,另一个以p[x]为根,所以degree[p[x]]=degree[x]+1;如果x不是p[x]地最左子节点,假设x是p[x]地子节点中自左至右地第i个孩子,则去掉p[x]前i-1个孩子,恰好转换成第一种情况,因而degree[p[x]]=degree[x]+1+(i-1)=degree[x]+i;综上,degree[p[x]]>degree[x]19.2.2题目:题目:19.2.519.2.6第13次作业参考答案20.2-1题目:解答:20.2-3 题目:解答:20.3-1 题目:答案:20.3-2 题目:答案:第14次作业参考答案这一次请大家自己看书处理版权申明本文部分内容,包括文字、图片、以及设计等在网上搜集整理.版权为个人所有This article includes some parts, including text, pictures, and design. Copyright is personal ownership.6ewMy。
算法导论9.1-1习题解答(二叉树)
算法导论9.1-1习题解答(⼆叉树)CLRS 9.1-1 :证明:在最坏情况下,利⽤n + [lgn] - 2此⽐较,即可找到n个元素中的第2⼩元素。
(提⽰:同时找最⼩元素)算法思想:1.将数组中的元素分组,每组两个元素,然后⽐较每组中的两个元素得到最⼩值,重新得到包含原来⼀半元素的数组,继续重复上述过程,那么最后⼀个元素必然为最⼩值。
如图所⽰,数组为{2, 1, 4, 3, 5}2.上述过程形成的是⼀个⼆叉树,其中叶⼦节点都为数组元素,⾮叶⼦节点刚好4个,这是⼆叉树的性质。
3.然后我们来找第⼆⼩元素,第⼆⼩元素必然跟着1,⾸先赋值为5,然后再赋值为3,然后赋值为2,即为所求。
PS:本章讨论的元素都互异,不存在相同值(见原书)#include <iostream>using namespace std;class Node{public:Node* left;Node* right;int data;Node();Node(int d);};class BinaryTree{public:Node* root;//创建树void create_tree(Node** node, int len);//求最⼩值int min(int a, int b);//寻找第⼆⼩值int search_second_small();BinaryTree();};int main(){int arr[10] = {89, 123, 7, 9, 2, 5, 25, 8, 43, 23};//叶⼦节点Node** node =new Node*[10];for(int i =0; i <10; i++)node[i] =new Node(arr[i]);BinaryTree* bi_tree =new BinaryTree();bi_tree->create_tree(node, 10);cout<<bi_tree->root->data<<endl;cout<<bi_tree->search_second_small()<<endl;return 0;}Node::Node(){left = right = NULL;}Node::Node(int d){data = d;left = right = NULL;}void BinaryTree::create_tree(Node** node, int len){//len == 2时,就剩下两个元素进⾏⽐较了,得到最后⼀个元素为root节点,即最⼩值节点 if(len ==2){root->left = node[0];root->right = node[1];root->data = min(node[0]->data, node[1]->data);}else{int new_len = (len%2) ? (len/2+1) : len/2;Node** new_node =new Node*[new_len];//new_node元素个数为奇数if(len%2){for(int i =0; i < new_len -1; i++){//构建⽗亲节点new_node[i] =new Node(min(node[2*i]->data, node[2*i+1]->data));new_node[i]->left = node[2*i];new_node[i]->right = node[2*i+1];}new_node[new_len -1] = node[len -1];}//new_node元素个数为偶数else{for(int i =0; i < new_len; i++){//构建⽗亲节点new_node[i] =new Node(min(node[2*i]->data, node[2*i+1]->data));new_node[i]->left = node[2*i];new_node[i]->right = node[2*i+1];}}create_tree(new_node, new_len);delete[] new_node;}}int BinaryTree::min(int a, int b){return a < b ? a : b;}int BinaryTree::search_second_small(){int second =1000000;Node* p = root;while(p->left != NULL && p->right != NULL){if(p->data == p->left->data && second > p->right->data){second = p->right->data;p = p->left;}else if(p->data == p->right->data && second > p->left->data){second = p->left->data;p = p->right;}elsereturn second;}return second;}BinaryTree::BinaryTree() {root = new Node();}。
算法导论习题答案 (4)
The father now has to decide how to procure skis for his daughter for every weekly session (until she quits). One can buy skis at a one-time cost of B dollars, or rent skis at a weekly cost of R dollars. (Note that one can buy skis at any time—e.g., rent for two weeks, then buy.)
(a) Suppose that (starting from an empty queue) we do 3 insertions, then 2 removals,
then 3 more insertions, and then 2 more removals. What is the total cost of these 10
Problem Set 4 Solutions
Reading: Chapters 17, 21.1–21.3 Both exercises and problems should be solved, but only the problems should be turned in.
麻省理工大学算法导论lecture22
Equivalence of definitions
Theorem. The two definitions are equivalent.
Proof. (⇒) Let f (u, v) = p(u, v) – p(v, u). • Capacity constraint: Since p(u, v) ≤ c(u, v) and p(v, u) ≥ 0, we have f (u, v) ≤ c(u, v). • Flow conservation: ( p(u, v) − p(v, u ) ) f (u , v) =
Day 38
L22.13
Another characterization of flow value
Lemma. For any flow f and any cut (S, T), we have | f | = f (S, T).
Proof. f (S, T) = f (S, V) – f (S, S) = f (S, V) = f (s, V) + f (S–s, V) = f (s, V) = | f |.
∑ p(u, v) − ∑ p(v, u) = ∑ f (u, v) .
v∈V v∈V v∈V
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 38 L22.9
Notation
Definition. The value of a flow f, denoted by | f |, is given by f = ∑ f ( s, v )
算法导论标准答案
算法导论标准答案————————————————————————————————作者:————————————————————————————————日期:2第二章算法入门由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。
另,思考题2-3 关于霍纳规则,有些部分没有完成,故没把解答写上去,我对其 c 问题有疑问,请有解答方法者提供个意见。
给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。
插入排序算法伪代码INSERTION-SORT(A)1 for j ←2 to length[A]2 do key ←A[j]3 Insert A[j] into the sorted sequence A[1..j-1]4 i ←j-15 while i > 0 and A[i] > key6 do A[i+1]←A[i]7 i ←i − 18 A[i+1]←keyC#对揑入排序算法的实现:public static void InsertionSort<T>(T[] Input) where T:IComparable<T>{T key;int i;for (int j = 1; j < Input.Length; j++){key = Input[j];i = j - 1;for (; i >= 0 && Input[i].CompareTo(key)>0;i-- )Input[i + 1] = Input[i];Input[i+1]=key;}}揑入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[ j]揑入,形成排好序的子数组A[1..j]这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个不伪代码认为的数组的数是第1个有所丌同,一般要注意有几个关键值要比伪代码的小1.如果按照大部分计算机编程语言的思路,修改为:INSERTION-SORT(A)1 for j ← 1 to length[A]2 do key ←A[j]3 i ←j-112 31 41 59 26 41 584 while i ≥ 0 and A[i] > key5 do A[i+1]←A[i]6 i ← i − 17A[i+1]←key循环丌变式(Loop Invariant)是证明算法正确性的一个重要工具。
算法导论(第二版)课后习题解答
Θ
i=1
i
= Θ(n2 )
This holds for both the best- and worst-case running time. 2.2 − 3 Given that each element is equally likely to be the one searched for and the element searched for is present in the array, a linear search will on the average have to search through half the elements. This is because half the time the wanted element will be in the first half and half the time it will be in the second half. Both the worst-case and average-case of L INEAR -S EARCH is Θ(n). 3
Solutions for Introduction to algorithms second edition
Philip Bille
The author of this document takes absolutely no responsibility for the contents. This is merely a vague suggestion to a solution to some of the exercises posed in the book Introduction to algorithms by Cormen, Leiserson and Rivest. It is very likely that there are many errors and that the solutions are wrong. If you have found an error, have a better solution or wish to contribute in some constructive way please send a message to beetle@it.dk. It is important that you try hard to solve the exercises on your own. Use this document only as a last resort or to check if your instructor got it all wrong. Please note that the document is under construction and is updated only sporadically. Have fun with your algorithms. Best regards, Philip Bille
算法导论中文版答案
24.2-3
24.2-4
24.3-1 见图 24-6 24.3-2
24.3-3
24.3-4 24.3-5 24.3-6
24.3-7
24.3-8 这种情况下不会破坏已经更新的点的距离。 24.4**** 24.5****
25.1-1 见图 25-1 25.1-2 为了保证递归定义式 25.2 的正确性 25.1-3
8.3-3 8.3-4
8.3-5(*) 8.4-1 见图 8-4 8.4-2
8.4-3 3/2,1/2 8.4-4(*) 8.4-5(*)
9.1-1
9.1-2 9.2-1 9.3-1
第九章
9.3-2 9.3-3
9.3-4 9.3-5
9.3-6 9.3-7
9.3-8
9.3-9
15.1-1
6.4-4
6.4-5
6.5-1 据图 6-5 6.5-2
6.5-3 6.5-4 6.5-5
6.5-6 6.5-7
6.5-8
7.1-1 见图 7-1 7.1-2
7.1-3 7.1-4 7.2-1 7.2-2
7.2-3 7.2-4 7.2-5
第七章
7.2-6 7.3-1
7.3-2
7.4-1 7.4-2
5.3-6
6.1-1 6.1-2 6.1-3 6.1-4 6.1-5 6.1-6
第6章
6.1-7
6.2-1 见图 6-2 6.2-2
6.2-3
6.2-4
6.2-5 对以 i 为根结点的子树上每个点用循环语句实现 6.2-6
6.3-1
见图 6-3 6.3-2
6.3-3
6.4-1 见图 6-4 6.4-2 HEAPSORT 仍然正确,因为每次循环的过程中还是会运行 MAX-HEAP 的过程。 6.4-3
算法导论Exercises22.5(转载)
算法导论Exercises22.5(转载)Exercises 22.5 - 导论.英⽂第3版最近看书的同时, 感觉⼀些练习缺少参考, 所以按部分总结了⾃⼰的解答, 也能够强化学习过程.如有不⾜或疑问, 欢迎指正.Exercises 22.5-1How can the number of strongly connected components of a graph change if a newedge is added?可以将每个强连通组件当作⼀个顶点, 组成强连通图, 图内顶点数量即强连通组件数量.如果新增加的边在顶点内部(即指向⾃⼰), 或者重复已有顶点间的边, 则数量没变化.如果新增加的边使图形成环, 这样环内顶点组成⼀个新的强连通组件, 所以数量减少N, N = (环内顶点数量 - 1). Exercises 22.5-2Show how the procedure STRONGLY-CONNECTED-COMPONENTS works on thegraph of Figure 22.6. Specifically, show the finishing times computed in line 1 andthe forest produced in line 3. Assume that the loop of lines 5–7 of DFS considersvertices in alphabetical order and that the adjacency lists are in alphabetical order.按照字母排序的循环和邻接表, 可得出第⼀次DFS访问顶点顺序, 如下(q (s (v (w, w) v) s) (t (x (z, z) x) (y, y) t) q) (r (u, u) r)finishing time: f(r) 20, f(u) 19, r(q) 16, f(t) 15, f(y) 14,f(x) 12, f(z) 11, f(s) 7, f(v) 6, f(w) 5.根据 finishing time 降序排列, 并倒转上图中的边, 第⼆次DFS访问顶点顺序, 如下(r, r) * (u, u) * (q (y (t, t) y) q) * (x (z, z) x) * (s (w (v, v) w) s)每个不相关括号(⽤*间隔)内的顶点, 代表 line 3 ⽣成的树(即强连通组件)ANSWER:DFS(G)森林:(其中红⾊为树边)强连通分量结果:Exercises 22.5-3Professor Bacon claims that the algorithm for strongly connected componentswould be simpler if it used the original (instead of the transpose) graph in thesecond depth-first search and scanned the vertices in order of increasing finishingtimes. Does this simpler algorithm always produce correct results?设想对上图应⽤指定的升序式算法, 图中有A, B, C 三个顶点, A->B, B->A, B->C 三条边.如果⾸次 DFS 从 B 开始, 先向 A 遍历, 则访问顶点顺序为 (B (A, A) (C, C) B)升序排列 finishing time 是 A, C, B. 这样第⼆次进⾏ DFS 则应从 A 开始, 该算法结果显⽰ A, B, C 是⼀个强连通组件, ⽽实际情况不是如此.该算法不能保证通⽤性, 只有在选择特殊顶点作为起始点, 以及规定邻接表顺序的情况下,结果才有可能是正确的.Exercises 22.5-4Prove that for any directed graph G, we have (( GT)SCC)T = (G)SCC. That is, thetranspose of the component graph of GT is the same as the component graph of G.在图 G 中, 同⼀组件中的顶点可以互相连通. ⽽图 GT 中, 组件中的边虽然反转,但其顶点仍可以互相连通, 这⽅⾯是不受影响的, 组件之间的边全部反转, 同样不会形成新的组件. 所以 ((GT)SCC)T = (G)SCC, 即 GT 和 G 的强连通组件相同.Exercises 22.5-5Give an O (V + E)-time algorithm to compute the component graph of a directedgraph G = (V, E). Make sure that there is at most one edge between two verticesin the component graph your algorithm produces.(1) first DFS (G)if v finishorderList.push_front(v) // orderList 是 finishing time 降序队列时间 O(V + E)(2) for each v in Vfor each x in v.adjx.adj.push_back(v)即求 GT 时间 O(V + E)(3) second DFS (GT)for v in order of orderListmap v to scc // scc 是强连通组件for each x in v.adjif x.color = COLOR_BLACK // 代表这条边(v, x)是强连通组件间的边if x non-exist in scc.adj // 在DFS(GT)期间总搜索时间 O(E)scc.adj.push_back(x)即求 scc 和 scc.adj 时间 O(V + E), 所以算法时间复杂度 O(V + E).即:(1)利⽤STRONGLY-CONNECTED-COMPONENTS(G)将结点分类,给每个分量中的结点赋予⼀个相同的标号,时间为O(V+E)。
算法导论-复习笔记
《算法导论》复习笔记Chapter 22 基本图算法22.1—1 有向图邻接链表,计算节点出度和入度的时间复杂度?O(V+E)开一个degree[]数组,大小为结点个数,复杂度O(V);遍历邻接链表,经过边uv时,计算出度degree[u]+=1,计算入度degree[v]+=1,复杂度O(E)22。
1—4 将一个多图变成等价无向图,用邻接链表表示,时间复杂度O(V+E)多图是允许重复边和自循环边的图。
开一个bool数组mark[],大小为节点个数,初始化为false.复杂度O(V)。
对每个顶点u的邻接链表,遍历,令v为u的边所指向的顶点;如果mark[v]=false,将uv加入新图,并将mark[v]设置为true;否则就跳过。
复杂度O(E)再次遍历u的连边,将mark[v]初始化整体复杂度O(V+E)伪代码:SOLVE(G,G’)1 for each vetex u∈G2 for each v∈ G。
Adj[u]3 if mark[v]==false4 mark[v]==true5 Addedge(G’,u,v)6 for each v∈G。
Adj[u]7 mark[v]=false22.1—6 图G的邻接矩阵表示,给出一个O(V)的算法来判断有向图G中是否存在一个通用汇点。
通用汇点指的是入度|V|-1,但出度为0。
等价问题:给定有向图G的V×V邻接矩阵G,在O(V)时间内判断是否存在一个数k,使得对所有的i有A[i][k]=1,对所有的j有A[k][j]=0,(i≠k,j≠k)令i和j初值为1,若G[i][j]=0,说明i到j无边,j不可能是通用汇点,令j=j+1;若G[i][j]=1,说明i到j有边,i不可能是通用汇点,令i=i+1,循环直到i〉|V|或者j>|V|;若i>|V|,则不存在通用汇点,若j>|V|,则检查顶点i是否满足要求。
伪代码:判断是否存在通用汇点 O(V)HAS_UNIVERSL_SINK(G)1 i=j=12 while i≤V and j≤V3 if G[i][j]==14 i=i+15 else j=j+16 if i>V7 return false8 else return CHECK(G,i)CHECK(G,u)1 for each vertex v∈G.V2 if G[u][v]=13 return false4 for each vertex v ∈ G.V5 if G[v][u]==0& u!=v6 return false7 return true检查点u是否是通用汇点【宽度优先搜索】22。
算法导论中文版答案
} cout<<len<<endl; } return 0; } 15.5-1
15.5-2 15.5-3
15.5-4
16.1-1
第 16 章
16.1-2 16.1-3
16.1-4 16.2-1 16.2-2
16.2-3
16.2-4
16.2-5 16.2-6
16.2-7
25.3-6
5.3-6
6.1-1 6.1-2 6.1-3 6.1-4 6.1-5 6.1-6
第6章
6.1-7
6.2-1 见图 6-2 6.2-2
6.2-3
6.2-4
6.2-5 对以 i 为根结点的子树上每个点用循环语句实现 6.2-6
6.3-1
见图 6-3 6.3-2
6.3-3
6.4-1 见图 6-4 6.4-2 HEAPSORT 仍然正确,因为每次循环的过程中还是会运行 MAX-HEAP 的过程。 6.4-3
6.4-4
6.4-5
6.5-1 据图 6-5 6.5-2
6.5-3 6.5-4 6.5-5
6.5-6 6.5-7
6.5-8
7.1-1 见图 7-1 7.1-2
7.1-3 7.1-4 7.2-1 7.2-2
7.2-3 7.2-4 7.2-5
第七章
7.2-6 7.3-1
7.3-2
7.4-1 7.4-2
16.3-1 16.3-2
16.3-3 16.3-4
16.3-5
16.3-6 那就推广到树的结点有三个孩子结点,证明过程同引理 16.3 的证明。 16.3-7 16.3-8
第 24 章
24.1-1 同源顶点 s 的运行过程,见图 24-4 24.1-2