算法导论第二十六章答案
算法导论文档
第一课课程细节;绪论:算法分析,插入排序法(Insertion Sort),归并排序(Merge Sort) 阅读:1-2章发测验02 演示课1 算法的正确性发《作业1》3 第二课渐进记号(Asymptotic Notation)。
递归公式(Recurrences):置换法,迭代法,主方法阅读:3-4 章,除了§4.44 第三课分治法:Strassen 算法,费氏数列,多项式乘法。
阅读:28 章第2 节,30章第1节5 演示课2 递归公式,松散性阅读:Akra-Bazzi 的讲义6 第四课快速排序法,随机化算法阅读:5 章1 到 3 节,7 章收《作业1》发《作业2》7 演示课3 排序法:堆排序,动态集合,优先队列阅读:6 章8 第五课线性时间的排序法:时间下界,计数排序法,基数排序法阅读:8 章第1 到3 节收《作业2》发《作业3》9 第六课顺序统计学,中位数阅读:9 章10 演示课4 中位数的应用,桶排序阅读:8 章第 4 节11 第七课散列,全域散列阅读:11 章1 到3 节收《作业3》发《作业4》12 第八课散列函数,完美散列阅读:11 章第5 节13 演示课5 测验1 复习收《作业4》14 评分后的作业4可以在中午拿到15 测验116 演示课6 二叉搜索树,树的遍历阅读:12 章1 到 3 节17 第九课二叉搜索树和快速排序法之间的关系;随机二叉搜索树的分析阅读:12 章4 节发《作业5》18 第十课红黑树,旋转,插入,删除阅读:13 章19 演示课7 2-3树,B-树阅读:18 章1 到 2 节20 第十一课高级数据结构,动态顺序统计,线段树(区间树)阅读:14 章收《作业5》发《作业6》21 第十二课计算几何,区间查询阅读:33 章1 到 2 节22 演示课8 凸多边形阅读:33 章3 节23 第十三课van Emde Boas树,优先队列阅读:van Emde Boas 的讲义收《作业6》发《作业7》24 第十四课平摊分析,表的复制,可能法阅读:17 章25 演示课9 竞争分析,自我排序列26 第十五课动态规划,最长公共子序列,最优二叉搜索树阅读:15 章收《作业7》发《作业8》27 第十六课贪婪算法,最小生成树阅读:16 章1 到 3 节,23 章28 演示课10 贪婪算法和动态规划的范例29 第十七课最短路径1,Dijkstra算法,广度优先搜索阅读:22 章1, 2 节;第580 - 587 页,24章 3 节收《作业8》发《作业9》30 演示课11 深度优先搜索,拓扑排序阅读:22 章3 到 5 节31 第十八课最短路径2,Bellman-Ford算法,DAG最短路径,差分约束阅读:24 章1, 2, 4, 5 节32 第十九课所有点对最短路径,Floyd-Warshall,Johnson 的算法阅读:25 章收《作业9》33 第二十课不相交集合的数据结构阅读:21 章34 评分后的作业9可以在中午拿到35 第二十一课带回家发下测验2 ; 道德,解决问题(强制参加)发测验236 没有演示课- 解答测验2!37 没有课算法程序比赛开始(非强制参加)收测验238 第二十二课网络流,最大流最小割切定理阅读:26 章1 - 2 节发《作业10》(选答)39 演示课12 图的匹配算法(注:最大二分匹配)阅读:26 章3 节40 第二十三课网络流,Edmonds-Karp 算法参赛答案截止41 第二十四课随堂测验;比赛颁奖;后续课程的讨论《作业10》解答。
算法导论(第三版)-复习-第六部分图论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出列。
全部结点出列后达到遍历所有边⼀次。
《算法导论(第二版)》(中文版)课后答案
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)
(完整版)算法导论复习要点
一、基础知识部分1. 算法的复杂性有时间复杂性和空间复杂性之分。
2. 算法是由若干条指令组成的有穷序列,且要满足输入、输出、确定性和有限性四条性质。
3. 快速排序算法是基于分治策略的一种排序算法。
4. 矩阵连乘问题的算法可由动态规划设计实现。
5、算法是指解决问题的一种方法或一个过程。
6、从分治法的一般设计模式可以看出,用它设计出的程序一般是递归算法。
7、问题的最优子结构性质是该问题可用动态规划算法或贪心算法求解的关键特征。
8、贪心选择性质是贪心算法可行的第一个基本要素,也是贪心算法与动态规划算法的主要区别。
9. 贪心算法的基本要素是贪心选择质和最优子结构性质。
10. 动态规划算法的两个基本要素是最优子结构性质和重叠子问题性质。
11、合并排序算法是利用(A )实现的算法A、分治策略B、动态规划法C、贪心法D、回溯法12、下列不是动态规划算法基本步骤的是( A )。
A、找出最优解的性质B、构造最优解C、算出最优解D、定义最优解13.下列算法中通常以自底向上的方式求解最优解的是( B )。
A、备忘录法B动态规划法C、贪心法D回溯法14. 备忘录方法是那种算法的变形。
(B )A、分治法B动态规划法C、贪心法D回溯法15.最长公共子序列算法利用的算法是( B )。
A、分支界限法B动态规划法C贪心法D回溯法16.贪心算法与动态规划算法的主要区别是( B )。
A、最优子结构B、贪心选择性质C、构造最优解D、定义最优解17. (D )是贪心算法与动态规划算法的共同点。
A、重叠子问题B、构造最优解C、贪心选择性质D最优子结构性质18. 矩阵连乘问题的算法可由(B )设计实现。
A、分支界限算法 B 、动态规划算法C、贪心算法D、回溯算法19、使用分治法求解不需要满足的条件是( A )。
A 子问题必须是一样的B 子问题不能够重复C 子问题的解可以合并D 原问题和子问题使用相同的方法解20.下列是动态规划算法基本要素的是( D )。
中科大算法导论作业标准标准答案
第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。
算法导论(第二版)习题答案(英文版)
Last update: December 9, 2002
1.2 − 2 Insertion sort beats merge sort when 8n2 < 64n lg n, ⇒ n < 8 lg n, ⇒ 2n/8 < n. This is true for 2 n 43 (found by using a calculator). Rewrite merge sort to use insertion sort for input of size 43 or less in order to improve the running time. 1−1 We assume that all months are 30 days and all years are 365.
n
Θ
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
算法导论习题答案26章
Solution to Exercise 26.2-11
For any two vertices u and in G , we can define a flow network Gu consisting of the directed version of G with s D u, t D , and all edge capacities set to 1. (The flow network Gu has V vertices and 2 jE j edges, so that it has O.V / vertices and O.E/ edges, as required. We want all capacities to be 1 so that the number of edges of G crossing a cut equals the capacity of the cut in Gu .) Let fu denote a maximum flow in Gu . We claim that for any u 2 V , the edge connectivity k equals min fjfu jg. We’ll
2V fug
show below that this claim holds. Assuming that it holds, we can find k as follows: E DGE -C ONNECTIVITY.G/ k D1 select any vertex u 2 G: V for each vertex 2 G: V fug set up the flow network Gu as described above find the maximum flow fu on Gu k D min.k; jfu j/ return k The claim follows from the max-flow min-cut theorem and how we chose பைடு நூலகம்apacities so that the capacity of a cut is the number of edges crossing it. We prove that k D min fjfu jg, for any u 2 V by showing separately that k is at least this
《算法导论》读书笔记(一)
《算法导论》读书笔记(⼀) 本章是本书的开篇,介绍了什么是算法,为什么要学习算法,算法在计算机中的地位及作⽤。
算法(algorithm)简单来说就是定义良好的计算机过程,它取⼀个或⼀组值作为输⼊,并产⽣出⼀个或⼀组值作为输出。
即算法就是⼀系列的计算步骤,⽤来将输⼊数据转换成输出数据。
书中有⼀句话⾮常好: Having a solid base of algorithm knowledge and technique is one characteristic that separates the truly skilled programmers from the novices. 是否具有扎实的算法知识和技术基础,是区分真正熟练的程序员与新⼿的⼀项重要特征。
以这句话激励⾃⼰要努⼒学习算法,夯实基础,成为真正熟练的程序员。
本章通过介绍插⼊排序和归并排序两种常见的排序算法来说明算法的过程及算法分析,在介绍归并排序算法过程中引⼊了分治(divide-and-conquer)算法策略。
1、插⼊排序 输⼊:n个数(a1,a2,a3,...,an) 输出:输⼊序列的⼀个排列(a1',a2',a3',...an')使得(a1'≤a2'≤a3'≤...≤an')。
插⼊排序的基本思想是:将第i个元素插⼊到前⾯i-1个已经有序的元素中。
具体实现是从第2个元素开始(因为1个元素是有序的),将第2个元素插⼊到前⾯的1个元素中,构成两个有序的序列,然后从第3个元素开始,循环操作,直到把第n元素插⼊到前⾯n-1个元素中,最终使得n个元素是有序的。
该算法设计的⽅法是增量⽅法。
书中给出了插⼊排序的为代码,并采⽤循环不变式证明算法的正确性。
我采⽤C 语⾔实插⼊排序,完整程序如下:1 void insert_sort(int *datas,int length)2 {3 int i,j;4 int key,tmp;5 //判断参数是否合法6 if(NULL == datas || 0==length)7 {8 printf("Check datas or length.\n");9 exit(1);10 }11 //数组下标是从0开始的,从第⼆个元素(对应下标1)开始向前插⼊12 for(j=1;j<length;j++)13 {14 key = datas[j]; //记录当前要插⼊的元素15 i = j-1; //前⾯已经有序的元素16 //寻找待插⼊元素的位置,从⼩到到排序,如果是从⼤到⼩改为datas[i]<key17 while(i>=0 && datas[i] > key)18 {19 /×tmp = datas[i+1];20 datas[i+1] = datas[i];21 datas[i] = tmp;×/ 这个过程不需要进⾏交换,因为要插⼊的值保存在key中,没有被覆盖掉,在此感谢”两⽣花“指出问题所在datas[i+1] = datas[i];22 i--; //向前移动23 }24 datas[i+1] = key; //最终确定待插⼊元素的位置25 }26 }插⼊排序算法的分析 算法分析是对⼀个算法所需的资源进⾏预测,资源是指希望测度的计算时间。
算法导论标准答案
算法导论标准答案————————————————————————————————作者:————————————————————————————————日期: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)是证明算法正确性的一个重要工具。
《算法导论》摘记经典算法题集锦
《算法导论》摘记经典算法题集锦分治策略Diogenes教授有n个被认为是完全相同的VLSI芯⽚,原则上它们是可以互相测试的。
教授的测试装置⼀次可测⼆⽚,当该装置中放有两⽚芯⽚时,每⼀⽚就对另⼀⽚作测试并报告其好坏。
⼀个好的芯⽚总是能够报告另⼀⽚的好坏,但⼀个坏的芯⽚的结果是不可靠的。
这样,每次测试的四种可能结果如下: A芯⽚报告 B芯⽚报告 结论 B是好的 A是好的 都是好的,或都是坏的 B是好的 A是坏的 ⾄少⼀⽚是坏的 B是坏的 A是好的 ⾄少⼀⽚是坏的 B是坏的 A是坏的 ⾄少⼀⽚是坏的Q:1.证明如果超过n/2个芯⽚是坏的,使⽤任何基于这种逐对检测操作的策略,教授都不能确定哪些芯⽚是好的。
2.如果超过n/2个芯⽚是好的,如何O(n)找出所有好的芯⽚?解:对于问题2,只需找出1个好的芯⽚,就能确定所有的芯⽚的好坏。
粗暴的⽅法是拿⼀个芯⽚与其他所有芯⽚配对,因为好的⽐坏的多,就可以根据多的确定该芯⽚的好坏,直到找到⼀个好芯⽚为⽌。
复杂度是O(n2)的。
同时也说明了Q1(否则⽆法确定好坏) O(n)怎么做?递归缩⼩问题规模。
假设有偶数个芯⽚。
两两配对,有a对检测结果为都是好的,b对检测结果为⼀好⼀坏,c对检测结果为都是坏的。
取a对中每对的任意⼀个芯⽚,组成a个芯⽚的⼦问题。
易证明该⼦问题依然满⾜好的芯⽚数量多于坏的芯⽚。
(因为b+c对中坏的 >= 好的)如果有奇数个芯⽚。
两两配对,有a对检测结果为都是好的,b对检测结果为⼀好⼀坏,c对检测结果为都是坏的。
还有⼀个零头。
1.如果零头是坏的,则a对中好的芯⽚ > 坏的芯⽚,⽤a对去检测零头,发现有⼀半以上的检测结果是坏的,反⾔之⼩于⼀半的检测结果是好的。
2.如果零头是好的,则a对中好的芯⽚ >= 坏的芯⽚,⽤a对去检测零头,发现⼤于等于⼀半的检测结果是好的。
根据结果判断零头是好是坏。
如果是1,把零头弃掉;如果是2,把零头加进来,这样该⼦问题依然满⾜好的芯⽚数量多于坏的芯⽚。
算法导论(第二版)课后习题解答
Θ
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
算法导论 第三版 第26章 答案 英
Chapter 26Michelle Bodnar,Andrew LohrApril 12,2016Exercise 26.1-1To see that the networks have the same maximum flow,we will show that every flow through one of the networks corresponds to a flow through the other.First,suppose that we have some flow through the network before applying the splitting procedure to the anti-symmetric edges.Since we are only changing one of any pair of anti-symmetric edges,for any edge that is unchanged by the splitting,we just have an identical flow going through those edges.Suppose that there was some edge (u,v )that was split because it had an anti-symmetric edge,and we had some flow,f (u,v )in the original graph.Since the capacity of both of the two edges that are introduced by the splitting of that edge have the same capacity,we can set f (u,v )=f (u,x )=f (x,v ).By constructing the new flow in this manor,we have an identical total flow,and we also still have a valid flow.Similarly,suppose that we had some flow f on the graph with split edges,then,for any triple of vertices u,x,v that correspond to a split edge,we must have that f (u,x )=f (x,v )because the only edge into x is (u,x )and the only edge out of x is (x,v ),and the net flow into and out of each vertex must be zero.We can then just set the flow on the unsplit edge equal to the common value that the flows on (u,x )and (x,v )have.Again,since we handle this on an edge by edge basis,and each substitution of edges maintains the fact that it is a flow of the same total,we have that the end result is also a valid flow of the same total value as the original.Since we have shown that any flow in one digraph can be translated into a flow of the same value in the other,we can translate the maximum value flow for one of them to get that it’s max value flow is ≤to that of the other,and do it in the reverse direction as well to achieve equality.Exercise 26.1-2The capacity constraint remains the same.We modify flow conservation so that each s i and t i must satisfy the “flow in equals flow out”constraint,and we only exempt s and t .We define the value of a flow in the multiple-source,multiple-sink problem to be m i =1 v ∈V f (s i ,v )− v ∈V f (v,s i ) .Let f i = v ∈V f (s i ,v )− v ∈V f (v,s i ).In the single-source flow network,set1f (s,s i )=f i .This satisfies the capacity constraint and flow conservation,so it is a valid assignment.The flow for the multiple-source network in this case is |f 1+f 2+···+f m |.In the single-source case,since there are no edges coming into s ,the flow is m i =1f (s,s i ).Since f (s,s i )is positive and equal to f i ,they are equivalent.Exercise 26.1-3Suppose that we are in the situation posed by the question,that is,that there is some vertex u that lies on no path from s to t .Then,suppose that we have for some vertex v ,either f (v,u )or f (u,v )is nonzero.Since flow must be conserved at u ,having any positive flow either leaving or entering u ,there is both flow leaving and entering.Since u doesn’t lie on a path from s to t ,we have that there are two cases,either there is no path from s to u or(possibly and)there is no path from u to t .If we are in the second case,we construct a path with c 0=u ,and c i +1is an successor of c i that has f (c i ,c i +1)being positive.Since the only vertex that is allowed to have a larger flow in than flow out is t ,we have that this path could only ever terminate if it were to reach t ,since each vertex in the path has some positive flow in.However,we could never reach t because we are in the case that there is no path from u to t .If we are in the former case that there is no path from s to u ,then we similarly define c 0=u ,however,we let c i +1be any vertex so that f (c i +1,c i )is nonzero.Again,this sequence of vertices cannot terminate since we could never arrive at having s as one of the vertices in the sequence.Since in both cases,we an always keep extending the sequence of vertices,we have that it must repeat itself at some point.Once we have some cycle of vertices,we can decrease the total flow around the cycle by an amount equal to the minimum amount of flow that is going along it without changing the value of the flow from s to t since neither of those two vertices show up in the cycle.However,by decreasing the flow like this,we decrease the total number of edges that have a positive flow.If there is still any flow passing though u ,we can continue to repeat this procedure,decreasing the number of edges with a positive flow by at least one.Since there are only finitely many vertices,at some point we need to have that there is no flow passing through u .The flow obtained after all of these steps is the desired maximum flow that the problem asks for.Exercise 26.1-4Since f 1and f 2are flows,they satisfy the capacity constraint,so we have 0≤αf 1(u,v )+(1−α)f 2(u,v )≤αc (u,v )+(1−α)c (u,v )=c (u,v ),so the new flow satisfies the capacity constraint.Further,f 1and f 2satisfy flow conservation,2so for all u ∈V −{s,t }we havev ∈V αf 1(v,u )+(1−α)f 2(v,u )=α v ∈Vf 1(v,u )+(1−α) v ∈V f 2(v,u )=αv ∈V f 1(u,v )+(1−α) v ∈V f 2(u,v )=v ∈V αf 1(u,v )+(1−α)f 2(u,v ).Therefore the flows form a convex set.Exercise 26.1-5A linear programming problem consists of a set of variables,a linear func-tion of those variables that needs to be maximized,and a a set of constraints.Our variables x e will be the amount of flow across each edge e .The function to maximize is e leaving s x e − e entering s x e .The sum of these flows is exactly equal to the value of the flow from s to t .Now,we consider constraints.There are two types of constraints,capacity constraints and flow constraints.The capacity constraints are just x e ≤c (e )where c e is the capacity of edge e .The flow constraints are that e leaving v x e − e entering v x e =0for all vertices v =s,t .Since this linear program captures all the same constraints,and wants to maximize the same thing,it is equivalent to the max flow problem.Exercise 26.1-6Use the map to create a graph where vertices represent street intersections and edges represent streets.Define c (u,v )=1for all edges (u,v ).Since a street can be traversed,start offby creating a directed edge in each direction,then make the transformation to a flow problem with no antiparallel edges as described in the section.Make the home the source and the school the sink.If there exist at least two distinct paths from source to sink then the flow will be at least 2because we could assign f (u,v )=1for each of those edges.However,if there is at most one distinct path from source to sink then there must exist a bridge edge (u,v )whose removal would disconnect s from t .Since c (u,v )=1,the flow into u is at most 1.We may assume there are no edges into s or out from t ,since it doesn’t make sense to return home or leave school.By flow conservation,this implies that f = v ∈V f (s,v )≤1.Thus,determining the maximum flow tells the Professor whether or not his children can go to the same school.Exercise 26.1-7We can capture the vertex constraints by splitting out each vertex into two,where the edge between the two vertices is the vertex capacity.More formally,3our newflow network will have vertices{0,1}×V.It has an edge between1×v and0×u if there is an edge(v,u)in the original graph,the capacity of such an edge is just c(v,u).The edges of the second kind that the newflow network will have are from0×v to1×v for every v with capacity l(v).This newflow network will have2|V|vertices and have|V|+|E|stly,we can see that this network does capture the idea that the vertices have capacities l(v).This is because anyflow that goes through v in the original graph must go through the edge(0×v,1×v)in the new graph,in order to get from the edges going into v to the edges going out of v.Exercise26.2-1To see that equation(26.6)equals(26.7),we will show that the terms that we are throwing into the sums are all zero.That is,we will show that if v∈V\(V1∪V2),then f (s,v)=f (v,s)=0.Since v∈V1,then there is no edge from s to v,similarly,since v∈V2,there is no edge from v to s.This means that there is no edge connecting s and v in any way.Sinceflow can only pass along edges,we know that there can be noflow passing directly between s and v.Exercise26.2-2Theflow across the cut is11+1+7+4−4=19.The capacity of the cut is16+4+7+4=31.Exercise26.2-3If we perform a breadthfirst search where we consider the neighbors of a vertex as they appear in the ordering{s,v1,v2,v3,v4,t},thefirst path that we will find is s,v1,v3,t.The min capacity of this augmenting path is12,so we send12 units along it.We perform a BFS on the resulting residual network.This gets us the path s,v2,v4,t.The min capacity along this path is4,so we send4units along it.Then,the only path remaining in the residual network is{s,v2,v4,v3} which has a min capacity of7,since that’s all that’s left,wefind it in our BFS. Putting it all together,the totalflow that we have found has a value of23. Exercise26.2-4A minimum cut corresponding to the maximumflow is S={s,v1,v2,v4} and T={v3,t}.The augmenting path in part(c)cancelsflow on edge(v3,v2). Exercise26.2-5Since the only edges that have infinite value are those going from the super-source or to the supersink,as long as we pick a cut that has the supersource and all the original sources on one side,and the other side has the supersink as well as all the original sinks,then it will only cut through edges offinite capacity. Then,by Corollary26.5,we have that the value of theflow is bounded above4by the value of any of these types of cuts,which is finite.Exercise 26.2-6Begin by making the modification from multi-source to single-source as done in section 26.1.Next,create an extra vertex ˆs i for each i and place it between s and s i .Explicitly,remove the edge from s to s i and add edges (s,ˆs i )and (ˆs i ,s i ).Similarly,create an extra vertex ˆt i for each vertex t i and place it between t and t i .Remove the edges (t i ,t )and add edges (t i ,ˆt i )and (ˆt i ,t ).Assign c (ˆs i ,s i )=p i and c (t i ,ˆt i )=q i .If a flow which satisfies the constraints exists,it will assign f (ˆs i ,s i )=p i .By flow conservation,this implies that v ∈V f (s i ,v )=p i .Sim-ilarly,we must have f (t i ,ˆt i )=q i ,so by flow conservation this implies that v ∈V f (v,t i )=q i .Exercise 26.2-7To check that f p is a flow,we make sure that it satisfies both the capacity constraints and the flow constraints.First,the capacity constraints.To see this,we recall our definition of c f (p ),that is,it is the smallest residual capacity of any of the edges along the path p .Since we have that the residual capacity is always less than or equal to the initial capacity,we have that each value of the flow is less than the capacity.Second,we check the flow constraints,Since the only edges that are given any flow are along a path,we have that at each vertex interior to the path,the flow in from one edge is immediately canceled by the flow out to the next vertex in the stly,we can check that its value is equal to c f (p )because,while s may show up at spots later on in the path,it will be canceled out as it leaves to go to the next vertex.So,the only net flow from s is the initial edge along the path,since it (along with all the other edges)is given flow c f (p ),that is the value of the flow f p .Exercise 26.2-8Paths chosen by the while loop of line 3go from s to t and are simple because capacities are always nonnegative.Thus,no edge into s will ever appear on an augmenting path,so such edges may as well never have existed.Exercise 26.2-9The augmented flow does satisfy the flow conservation property,since the sum of flow into a vertex and out of a vertex can be split into two sums each,one running over flow in f and the other running over flow in f ,since we have the parts are equal separately,their sums are also equal.The capacity constraint is not satisfied by this arbitrary augmentation of flows.To see this,suppose we only have the vertices s and t ,and have a single edge from s to t of capacity 1.Then we could have a flow of value 1from s to5t,however,augmenting thisflow with itself ends up putting two units along the edge from s to t,which is greater than the capacity we can send.Exercise26.2-10Suppose we already have a maximumflow f.Consider a new graph G where we set the capacity of edge(u,v)to f(u,v).Run Ford-Fulkerson,with the mod-ification that we remove an edge if itsflow reaches its capacity.In other words, if f(u,v)=c(u,v)then there should be no reverse edge appearing in residual network.This will still produce correct output in our case because we never exceed the actual maximumflow through an edge,so it is never advantageous to cancelflow.The augmenting paths chosen in this modified version of Ford-Fulkerson are precisely the ones we want.There are at most|E|because every augmenting path produces at least one edge whoseflow is equal to its capacity, which we set to be the actualflow for the edge in a maximumflow,and our modification prevents us from ever destroying this progress.Exercise26.2-11To test edge connectivity,we will take our graph as is,pick an arbitrary s to be our source for theflow network,and then,we will consider every possible other selection of our sink t.For each of theseflow networks,we will replace each(undirected)edge in the original graph with a pair of anti-symmetric edges, each of capacity1.We claim that the minimum value of all of these different consideredflow networks’maximumflows is indeed the edge connectivity of the original graph. Consider one particularflow network,that is,a particular choice for t.Then, the value of the maximumflow is the same as the value of the minimum cut separating s and t.Since each of the edges have a unit capacity,the value of any cut is the same as the number of edges in the original graph that are cut by that particular cut.So,for this particular choice of t,we have that the maximumflow was the number of edges needed to be removed so that s and t are in different components.Since our end goal is to remove edges so that the graph becomes disconnected,this is why we need to consider all n−1flow networks.That is,it may be much harder for some choices of t than others to make s and t end up in different components.However,we know that there is some vertex that has to be in a different component than s after removing the smallest number of edges required to disconnect the graph.So,this value for the number of edges is considered when we have t be that vertex.Exercise26.2-12Since every vertex lies on some path starting from s there must exist a cycle which contains the edge(v,s).Use a depthfirst search tofind such a cycle with no edges of zeroflow.Such a cycle must exist since f satisfies conservation of flow.Since the graph is connected this takes O(E).Then decrement theflow6of every edge on the cycle by1.This preserves the value of theflow so it is still maximal.It won’t violate the capacity constraint because f>0on every edge of the cycle prior to decrementing.Finally,flow conservation isn’t violated because we decrement both an incoming and outgoing edge for each vertex on the cycle by the same amount.Exercise26.2-13Suppose that your givenflow network contains|E|edges,then,we were to modify all of the capacities of the edges by taking any edge that has a postivecapacity and increasing its capacity by1|E|+1.Doing this modification can’t getus a set of edges for a min cut that isn’t also a min cut for the unmodified graph because the difference between the value of the min cut and the next lowest cut vale was at least one because all edge weights were integers.This means thatthe new min cut value is going to be at most the original plus|E||E|+1.Since thisvalue is more than the second smallest valued cut in the originalflow network, we know that the choice of cuts we make in the newflow network is also a minimum cut in the stly,since we added a small constant amount to the value of each edge,our minimum cut would have the smallest possible number of edges,otherwise one with fewer would have a smaller value. Exercise26.3-1First,we pick an augmenting path that passes through vertices1and6.Then, we pick the path going through2and8.Then,we pick the path going through 3and7.Then,the resulting residual graph has no path from s to t.So,we know that we are done,and that we are pairing up vertices(1,6),(2,8),and (3,7).This number of unit augmenting paths agrees with the value of the cut where you cut the edges(s,3),(6,t),and(7,t).Exercise26.3-2We proceed by induction on the number of iterations of the while loop of Ford-Fulkerson.After thefirst iteration,since c only takes on integer values and(u,v).f is set to0,c f only takes on integer values.Thus,lines7and8of Ford-Fulkerson only assign integer values to(u,v).f.Assume that(u,v).f∈Z for all(u,v)after the n th iteration.On the(n+1)st iteration c f(p)is set to the minimum of c f(u,v)which is an integer by the induction hypothesis.Lines7 and8compute(u,v).f or(v,u).f.Either way,these the the sum or difference of integers by assumption,so after the(n+1)st iteration we have that(u,v).f is an integer for all(u,v)∈E.Since the value of theflow is a sum offlows of edges,we must have|f|∈Z as well.Exercise26.3-3The length of an augmenting path can be at most2min{|L|,|R|}+1.To7see that this is the case,we can construct an example which has an augmenting path of that length.Suppose that the vertices of L are{ 1, 2,... |L|},and of R are{r1,r2,...,r|R|}. For convenience,we will call m=min{|L|,|R|}.Then,we will place the edges{( m,r m−1),( 1,r1),( 1,r m)}∪∪i=m−1i=2{( i,r i),( i,r i−1)}Then,after augmenting with the shortest length path min{|L|,|R|}−1times,we could end up having sent a unitflow along{( i,r i)}i=1,...,m−1.At this point,there is only a single augmenting path,namely,{s, m,r m−1, m−1,r m−2,..., 2,r1, 1,r m,t}. This path has the length2m+1.It is clear that any simple path must have length at most2m+1,since thepath must start at s,then alternate back and forth between L and R,and thenend at t.Since augmenting paths must be simple,it is clear that our boundgiven for the longest augmenting path is tight.Exercise26.3-4First suppose there exists a perfect matching in G.Then for any subsetA⊆L,each vertex of A is matched with a neighbor in R,and since it is a matching,no two such vertices are matched with the same vertex in R.Thus,there are at least|A|vertices in the neighborhood of A.Now suppose that|A|≤|N(A)|for all A⊆L.Run Ford-Fulkerson on the correspondingflownetwork.Theflow is increased by1each time an augmenting path is found,soit will suffice to show that this happens|L|times.Suppose the while loop hasrun fewer than L times,but there is no augmenting path.Then fewer than Ledges from L to R haveflow1.Let v1∈L be such that no edge from v1toa vertex in R has nonzeroflow.By assumption,v1has at least one neighborv 1∈R.If any of v1’s neighbors are connected to t in G f then there is a path,so assume this is not the case.Thus,there must be some edge(v2,v1)withflow1.By assumption,N({v1,v2})≥2,so there must exist v 2=v 1such thatv 2∈N({v1,v2}).If(v 2,t)is an edge in the residual network we’re done sincev 2must be a neighbor of v2,so s,v1,v 1,v2,v 2,t is a path in G f.Otherwise v 2must have a neighbor v3∈L such that(v3,v 2)is in G f.Specifically,v3=v1since(v3,v 2)hasflow1,and v3=v2since(v2,v 1)hasflow1,so no moreflowcan leave v2without violating conservation offlow.Again by our hypothesis,N({v1,v2,v3})≥3,so there is another neighbor v 3∈R.Continuing in this fashion,we keep building up the neighborhood v i,expand-ing each time wefind that(v i,t)is not an edge in G f.This cannot happen Ltimes,since we have run the Ford-Fulkerson while-loop fewer than|L|times,sothere still exist edges into t in G f.Thus,the process must stop at some vertexv k ,and we obtain an augmenting path s,v1,v 1,v2,v 2,v3,...,v k,vk,t,contra-dicting our assumption that there was no such path.Therefore the while loop runs at least|L|times.By Corollary26.3theflow strictly increases each time by f p.By Theorem26.10f p is an integer.In particular,it is equal to1.This implies that|f|≥|L|.It is clear that|f|≤|L|,so we must have|f|=|L|.By8Corollary26.11this is the cardinality of a maximum matching.Since|L|=|R|, any maximum matching must be a perfect matching.Exercise26.3-5We convert the bipartite graph into aflow problem by making a new vertex for the source which has an edge of unit capacity going to each of the vertices in L,and a new vertex for the sink that has an edge from each of the vertices in R,each with unit capacity.We want to show that the number of edge between the two parts of the cut is at least L,this would get us by the max-flow-min-cut theorem that there is aflow of value at least|L|.The,we can apply the integrality theorem that all of theflow values are integers,meaning that we are selecting|L|disjoint edges between L and R.To see that every cut must have capacity at lest|L|,let S1be the side of the cut containing the source and let S2be the side of the cut containing the sink. Then,look at L∩S1.The source has an edge going to each of L∩(S1)c,and there is an edge from R∩S1to the sink that will be cut.This means that we need that there are at least|L∩S1|−|R∩S1|many edges going from L∩S1|to R∩S2.If we look at the set of all neighbors of L∩S1,we get that there must be at least the same number of neighbors in R,because otherwise we could sum up the degrees going from L∩S1to R on both sides,and get that some of the vertices in R would need to have a degree higher than d.This means that the number of neighbors of L∩S1is at least L∩S1,but we have that they are in S1,but there are only|R∩S1|of those,so,we have that the size of the set of neighbors of L∩S1that are in S2is at least|L∩S1|−|R∩S1|.Since each of these neighbors has an edge crossing the cut,we have that the total number of edges that the cut breaks is at least(|L|−|L∩S1|)+(|L∩S1|−|R∩S1|)+|R∩S1|=|L|. Since each of these edges is unit valued,the value of the cut is at least|L|. Exercise26.4-1When we run INITIALIZE-PREFLOW(G,s),s.e is zero prior to the for loop on line7.Then,for each of the vertices that s has an edge to,we decrease the value of s.e by the capacity of that edge.This means that at the end,s.e is equal to the negative of the sum of all the capacities coming out of s.This is then equal to the negative of the cut value of the cut that puts s on one side, and all the other vertices on the other.The negative of the value of the min cut is larger or equal to the negative of the value of this cut.Since the value of the maxflow is the value of the min cut,we have that the negative of the value of the maxflow is larger or equal to s.e.Exercise26.4-2We must select an appropriate data structure to store all the information which will allow us to select a valid operation in constant time.To do this,we will need to maintain a list of overflowing vertices.By Lemma26.14,a push or9a relabel operation always applies to an overflowing vertex.To determine which operation to perform,we need to determine whether u.h =v.h +1for some v ∈N (u ).We’ll do this by maintaining a list u.high of all neighbors of u in G f which have height greater than or equal to u .We’ll update these attributes in the PUSH and RELABEL functions.It is clear from the pseudocode given for PUSH that we can execute it in constant time,provided we have maintain the attributes δf (u,v ),u.e ,c f (u,v ),(u,v ).f ,and u.h .Each time we call PUSH(u,v )the result is that u is no longer overflowing,so we must remove it from the list.Maintain a pointer u.overflow to u ’s position in the overflow list.If a vertex u is not overflowing,set u.overflow =NIL .Next,check if v became overflowing.If so,set v.overflow equal to the head of the overflow list.Since we can update the pointer in constant time and delete from a linked list given a pointer to the element to be deleted in constant time,we can maintain the list in O (1).The RELABEL operation takes O (V )because we need to compute the minimum v.h from among all (u,v )∈E f ,and there could be |V |−1many such v .We will also need to update u.high during RELABEL.When RELABEL(u )is called,set u.high equal to the empty list and for each vertex v which is adjacent to u ,if v.h =u.h +1,add u to the list v.high .Since this takes constant time per adjacent vertex we can maintain the attribute in O (V )per call to relabel.Exercise 26.4-3To run RELABEL(u),we need to take the min a number of things equal to the out degree of u (and so taking this min will take time proportional to the out degree).This means that since each vertex will only be relabeled at most O (|V |)many times,the total amount of work is on the order of |V | v ∈V outdeg (v ).But the sum of all the out degrees is equal to the number of edges,so,we have the previous expression is on the order of |V ||E |.Exercise 26.4-4In the proof of 2=⇒3in Theorem 26.6we obtain a minimum cut by letting S ={v ∈V |there exists a path from s to v in G f }and T =V −S .Given a flow,we can form the residual graph in O (E ).Then we just need to perform a depth first search to find the vertices reachable from s .This can be done in O (V +E ),and since |E |≥|V |−1the whole procedure can be carried out in O (E ).Exercise 26.4-5First,construct the flow network for the bipartite graph as in the previous section.Then,we relabel everything in L .Then,we push from every vertex in L to a vertex in R ,so long as it is possible.keeping track of those that vertices of L that are still overflowing can be done by a simple bit vector.Then,we relabel everything in R and push to the last vertex.Once these operations have been done,The only possible valid operations are to relabel the vertices of L10that weren’t able tofind an edge that they could push theirflow along,so could possibly have to get a push back from R to L.This continues until there are no more operations to do.This takes time of O(V(E+V)).Exercise26.4-6The number of relabel operations and saturating pushes is the same as before. An edge can handle at most k nonsaturating pushes before it becomes saturated, so the number of nonsaturating pushes is at most2k|V||E|.Thus,the total num-ber of basic operations is at most2|V|2+2|V||E|+2k|V||E|=O(kV E).Exercise26.4-7This won’t affect the asymptotic performance,in fact it will improve the bound obtained in lemma16.20to be that no vertex will ever have a height more than2|V|−3.Since this lemma was the source of all the bounds later, they carry through,and are actually a little bit(not asymptotically)better (lower).To see that it won’t affect correctness of the algorithm.We notice that the reason that we needed the height to be as high as it was was so that we could consider all the simple paths from s to t.However,when we are done initializ-ing,we have that the only overflowing vertices are the ones for which there is an edge to them from s.Then,we only need to consider all the simple paths from them to t,the longest such one involves|V|−1vertices,and,so,only |V|−2different edges,and so it only requires that there are|V|−2differences in heights,since the set{0,1,...,|V|−3}has|V|−2different values,this is possible.Exercise26.4-8We’ll prove the claim by induction on the number of push and relabel op-erations.Initially,we have u.h=|V|if u=s and0otherwise.We have s.h−|V|=0≤δf(s,s)=0and u.h=0≤δf(u,t)for all u=s,so the claim holds prior to thefirst iteration of the while loop on line2of the GENERIC-PUSH-RELABEL algorithm.Suppose that the properties have been maintained thus far.If the next iteration is a nonsaturating push then the properties are maintained because the heights and existence of edges in the residual network are preserved.If it is a saturating push then edge(u,v)is removed from the residual network,which increases bothδf(u,t)andδf(u,s),so the properties are main-tained regardless of the height of u.Now suppose that the next iteration causes a relabel of vertex u.For all v such that(u,v)∈E f we must have u.h≤v.h. Let v =min{v.h|(u,v)∈E f}.There are two cases to consider.First,sup-pose that v .h<|V|.Then after the relabeling we have u.h=1+v .h≤1+min(u,v)∈Ef δf(v,t)=δf(u,t).Second,suppose that v .h≥|V|.Then afterrelabeling we have u.h=1+v .h≤1+|V|+min(u,v)∈Ef δf(v,s)=δf(u,s)+|V|which implies that u.h−|V|≤δ)f(u,s).Therefore the GENERIC-PUSH-11。
算法导论中文版答案
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
算法导论中文版答案
} 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
leetcode第26题
leetcode第26题
题目编号26的LeetCode问题要求我们从已排序的数组中删除
重复项,并返回新的数组长度。
同时,要求在不使用额外的数组空
间的情况下完成。
在解决这个问题时,我们需要考虑如何在原地修
改数组,以及如何处理重复的元素。
首先,我们可以使用双指针的方法来解决这个问题。
我们定义
两个指针,一个用于遍历数组(称为“遍历指针”),另一个用于
指向新数组的末尾(称为“新数组指针”)。
初始时,遍历指针和
新数组指针都指向数组的第一个元素。
然后,我们开始遍历数组。
当遍历指针指向的元素与新数组指
针指向的元素不同时,我们将新数组指针指向的下一个位置的元素
设置为遍历指针指向的元素,并将新数组指针向后移动一位。
这样,我们就可以不断地将不重复的元素放置在新数组的前面。
最后,遍历完成后,新数组指针所指的位置就是新数组的长度,我们将其加一并返回即可。
需要注意的是,这个方法只是修改了数组的前几个元素,而并
未真正删除重复的元素。
但由于题目要求返回新数组的长度,因此这个方法是符合要求的。
综上所述,我们可以通过双指针的方法,在原地修改数组,去除重复元素,并返回新数组的长度。
这样就完成了LeetCode第26题的要求。
算法导论-平摊分析习题解
算法导论-平摊分析习题解17.2-3 假设我们希望不仅能使⼀个计数器增值,也能使之复位⾄零(即,使其中所有的位都为0)。
请说明如何将⼀个计数器实现为⼀个位数组,使得对⼀个初始为零的计数器,任⼀个包含n个INCREMENT和RESET操作的序列的时间都为O(n)。
(提⽰:保持⼀个指针指向⾼位1) 解:如提⽰所⾔,保存⼀个指针指向⾼位1。
规定把某⼀位设为1的操作收取4元的平摊费⽤,其中1元⽤来⽀付置位操作的实际代价,剩余3元作为存款。
维护指针花费1元,当将该位翻转为0时,花费其中1元,置位花费1元。
所以n个操作的序列时间⾄多为4n = O(n)。
17.3-7 设计⼀个数据结构来⽀持整数动态多重集合S上的下列两个操作:INSERT(S, x) 将x插⼊到S中。
DELETE-LARGER-HALF(S) 删除S中最⼤的ceil(|S|/2)个元素。
解释如何实现这个数据结构,使得任意m个操作的序列在O(m)时间内运⾏,⽽且要在O(|S|)时间内输出S的元素。
解:⽤⽆序数组即可实现该数据结构。
在⽆序数组中INSERT(S,x)的时间为O(1),DELETE-LARGER-HALF(S),先⽤划分的⽅法找到S 的中位数,然后删除最⼤的|S|/2个元素,时间为O(n)。
可⽤记账法分析这个过程,插⼊⼀个元素收取3元费⽤,其中1元付给插⼊操作。
删除操作每个元素付1元,然后将删除部分元素的余款分配到剩余的元素上。
这样数组中每个元素始终有2元存款。
所以任意m个操作序列可以在O(m)时间内运⾏。
17-1 使⼆叉查找动态化 解:1) 最坏情况下需要在每个数组中寻找相应的元素,因此时间复杂度是O(klgn) = O(lg2n)。
2) 最坏情况下n = 2i - 1,这时候插⼊⼀个元素需要合并所有k个数组中的元素,总的时间复杂度为O(n)。
平摊分析:可使⽤记账法,每插⼊⼀个元素付k元,其中1元作为插⼊费⽤,剩余k-1元作为合并费⽤,平摊运⾏时间为O(klgn) = O(lgn)。