中科大 算法导论作业答案
算法导论课程作业答案
算法导论课程作业答案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。
《算法导论》习题答案12、13、14章
第9章 中位数和顺序统计学
9.3-2
大于x的数至少有3n/10-6, n≥140时,易证3n/10-6 ≥n/4 小于x的数同理。
9.3-4
通过比较得到第i小元素,每次保留比较信息。 在比较过程中比这个元素小的元素构成的集合即为i – 1个 小数集合,而比较过程中比这个元素大的元素则构成了n – i 个大元素集合。不需要增加比较次数。
Preprocessing(A,k) for i←0 to k do C[i]←0 for j←1 to length[A] do C[A[j]] ←C[A[j]]+1 for i←1 to k do C[i] ←C[i]+C[i-1] Query(C,k,a,b) if b<a or b<1 or a>k return 0 if a<1 then a=1 if b>k then b=k if a≠1 then return C[b]-C[a-1] else return C[b]
0 +1
k +1
k +1
( k +1) +1
第6章 堆排序
6.4-3 不论递增还是递减,时间均为O(nlgn) 6.4-4 最坏情况下,n-1次调用MAX-HEAPIFY,运 行时间为O(nlgn)
第6章 堆排序
6.5-3
HEAP-MINIMUM(A) if heap-size[A]<1 then error”heap underflow” else return A[1] HEAP-EXTRACT-MIN(A) if heap-size[A]<1 then error”heap underflow” min<-A[1] A[1]<-A[heap-size[A]] heap-size[A]<-heap-size[A]-1 MIN-HEAPIFY(A,1) return min HEAP-DECREASE-KEY(A,i,key) if key>A[i] then error A[i]<-key while i>1 and A[PARENT(i)>A[i] do exchange A[i]<->A[PARENT(i)] i<-PARENT(i) MIN-HEAP-INSERT(A,key) heap-size[A]<-heap-size[A]+1 A[heap-size[A]]<-+∞ HEAP-DECREASE-KEY(A,heap-size[A],key)
中科大算法导论期末试卷及答案
解:存在������1(n) + ������2(������) = ������(max(������1(������), ������2(������) 证明: ������1(������) = ������(������1(n)) 则存在 a1>0,a2>0, n1>0 使得 n>n1 时有 ������1 ∗ ������1(n) < ������1(������) < ������2 ∗ ������1(n) ������2(������) = ������(������2(n)) 则存在 b1>0,b2>0, n2>0 使得 n>n2 时有 ������1 ∗ ������2(n) < ������2(������) < ������2 ∗ ������2(n) 取 c1=min(a1,b1) >0, c2=2*max(a2,b2) >0, n0=max(n1,n2)>0 当 n>n0 时,有 ������1(n) + ������2(������) > ������1 ∗ ������1(n) + ������1 ∗ ������2(n)
= ������2(������������ − 5 ������������������������) − 2������ > 2������3 − 2������ > 2������0(������02 − 1) =12 即当 c=7, n0=2 时,对 n>n0, 5������2������������������������ + 2������ < ������������3恒成立, 5������2������������������������ + 2������ = ������(������3)
中科大算法导论第一,二次和第四次作业答案
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++]
算法导论参考答案
算法导论参考答案算法导论参考答案算法导论是计算机科学领域中一本经典的教材,被广泛应用于计算机科学和工程的教学和研究中。
它涵盖了算法设计和分析的基本概念,以及各种常见算法的实现和应用。
本文将为读者提供一些算法导论中常见问题的参考答案,以帮助读者更好地理解和掌握这门课程。
1. 什么是算法?算法是一系列解决问题的步骤和规则。
它描述了如何将输入转换为输出,并在有限的时间内完成。
算法应具备正确性、可读性、健壮性和高效性等特点。
2. 如何分析算法的效率?算法的效率可以通过时间复杂度和空间复杂度来衡量。
时间复杂度表示算法执行所需的时间量级,常用的时间复杂度有O(1)、O(n)、O(logn)、O(nlogn)和O(n^2)等。
空间复杂度表示算法执行所需的额外空间量级,通常以字节为单位。
3. 什么是渐进符号?渐进符号用于表示算法的时间复杂度或空间复杂度的增长趋势。
常见的渐进符号有大O符号、Ω符号和Θ符号。
大O符号表示算法的上界,Ω符号表示算法的下界,Θ符号表示算法的平均情况。
4. 什么是分治法?分治法是一种算法设计策略,将问题分解为若干个子问题,并对子问题进行独立求解,最后将子问题的解合并得到原问题的解。
典型的分治算法有归并排序和快速排序。
5. 什么是动态规划?动态规划是一种通过将问题分解为相互重叠的子问题来求解的方法。
它通常用于求解具有重叠子问题和最优子结构性质的问题。
典型的动态规划算法有背包问题和最短路径问题。
6. 什么是贪心算法?贪心算法是一种通过每一步选择局部最优解来求解整体最优解的方法。
贪心算法通常不能保证得到全局最优解,但在某些问题上能够得到近似最优解。
典型的贪心算法有霍夫曼编码和最小生成树算法。
7. 什么是图算法?图算法是一类用于解决图结构相关问题的算法。
图由节点和边组成,节点表示对象,边表示对象之间的关系。
图算法包括图的遍历、最短路径、最小生成树和网络流等问题的求解。
8. 什么是NP完全问题?NP完全问题是一类在多项式时间内无法求解的问题。
中科大算法导论作业标准标准答案
第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。
算法导论参考 答案
第二章算法入门由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。
另,思考题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-14 while i ≥ 0 and A[i] > key5 do A[i+1]←A[i]6 i ←i − 17 A[i+1]←key循环丌变式(Loop Invariant)是证明算法正确性的一个重要工具。
算法导论答案 (4)
算法导论答案第一章:算法概述啊算法的定义算法是一系列解决问题的明确指令。
它是一个有穷步骤集,其中每个步骤或操作由确定性和可行性特征。
算法是通过将预期的输入转换为输出来解决问题的工具。
第二章:插入排序插入排序的思想插入排序是一种简单直观的排序算法,其基本思想是将待排序的序列分为已排序和未排序两部分,每次从未排序的部分中取出一个元素,并将其插入到已排序部分的正确位置,直到所有元素都被排序。
插入排序的算法实现以下是插入排序的伪代码:INSERTION-SORT(A)for j = 2 to A.lengthkey = A[j]// Insert A[j] into the sorted sequence A[1.. j-1].i = j - 1while i > 0 and A[i] > keyA[i + 1] = A[i]i = i - 1A[i + 1] = key插入排序的时间复杂度插入排序的时间复杂度为O(n^2),其中n是排序的元素个数。
虽然插入排序的最坏情况下的复杂度很高,但是对于小规模的数据集,插入排序是一种较快的排序算法。
第三章:分治策略分治策略的基本思想分治策略是一种解决问题的思想,它将问题的规模不断缩小,直到问题足够小而可以直接解决。
然后将子问题的解合并起来,得到原问题的解。
分治策略的应用实例一种经典的应用分治策略的算法是归并排序。
归并排序将待排序的序列划分为两个子序列,分别排序后再将两个有序子序列合并为一个有序序列。
以下是归并排序的伪代码:MERGE-SORT(A, p, r)if p < rq = floor((p + r) / 2)MERGE-SORT(A, p, q)MERGE-SORT(A, q + 1, r)MERGE(A, p, q, r)MERGE(A, p, q, r)n1 = q - p + 1n2 = r - qlet L[1..n1+1] and R[1..n2+1] be new arraysfor i = 1 to n1L[i] = A[p + i - 1]for j = 1 to n2R[j] = A[q + j]L[n1 + 1] = infinityR[n2 + 1] = infinityi = 1j = 1for k = p to rif L[i] <= R[j]A[k] = L[i]i = i + 1elseA[k] = R[j]j = j + 1分治策略的时间复杂度归并排序的时间复杂度为O(nlogn),其中n是待排序序列的长度。
算法导论参考答案
第二章算法入门由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。
另,思考题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-14 while i ≥ 0 and A[i] > key5 do A[i+1]←A[i]6 i ←i − 17 A[i+1]←key循环丌变式(Loop Invariant)是证明算法正确性的一个重要工具。
算法导论习题答案
算法导论习题答案算法导论习题答案算法导论是一本经典的计算机科学教材,讲述了算法设计与分析的基本原理。
在学习过程中,习题是不可或缺的一部分,通过解答习题可以帮助我们巩固所学的知识。
本文将针对算法导论中的一些习题进行解答,帮助读者更好地理解算法导论的内容。
习题1-1:证明对于任意两个实数a和b,有|a + b| ≤ |a| + |b|。
解答:根据绝对值的定义,我们可以将|a + b|、|a|和|b|分别表示为以下三种情况:1. 当a + b ≥ 0,a ≥ 0,b ≥ 0时,|a + b| = a + b,|a| = a,|b| = b。
此时,|a + b| ≤ |a| + |b| 成立。
2. 当a + b < 0,a < 0,b < 0时,|a + b| = -(a + b),|a| = -a,|b| = -b。
此时,|a + b| ≤ |a| + |b| 成立。
3. 当a + b ≥ 0,a < 0,b < 0时,|a + b| = a + b,|a| = -a,|b| = -b。
此时,|a + b| ≤ |a| + |b| 成立。
综上所述,无论a和b的取值如何,都有|a + b| ≤ |a| + |b| 成立。
习题2-1:证明插入排序的运行时间是O(n^2)。
解答:插入排序是一种简单直观的排序算法,它的基本思想是将待排序的元素一个个地插入到已排好序的序列中。
在最坏情况下,即待排序的序列是逆序排列时,插入排序的运行时间最长。
假设待排序的序列长度为n,那么第一次插入需要比较1次,第二次插入需要比较2次,依次类推,第n次插入需要比较n-1次。
总的比较次数为1 + 2 + 3+ ... + (n-1) = n(n-1)/2。
因此,插入排序的运行时间是O(n^2)。
习题3-1:证明选择排序的运行时间是O(n^2)。
解答:选择排序是一种简单直观的排序算法,它的基本思想是每次从待排序的序列中选择最小的元素,放到已排序序列的末尾。
算法导论第二次作业答案
Solution: First, let’s analyze the Activity Selection Problem again: This is a problem using greedy algorithm: Input: 1. n activities a1, a2, . . . , an; 2. for i = 1, 2, . . . , n, the activity ai has a start time si and a finish time fi: [si, fi) Output: Pick a maximum set of activities that are compatible in time.
Solution: First, let’s analyze the Huffman Code again:
This is a problem using greedy algorithm:
Input: 1. n symbols s1, s2, . . . , sn ; 2. every symbol si has a frequency fi
Assume the time period of each activity is shown as following table:
i
123
si
157
fi
469
duration 3 1 2
We will choose the a2 which has the least duration among all three activities. However, it is obvious that the best arrangement is to choose {a1, a3}. So the approach of selecting the activity of least duration from among those that are compatible with previously selected activities does not work.
算法导论习题答案 (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.
算法导论习题答案 (1)
Introduction to Algorithms September 24, 2004Massachusetts Institute of Technology 6.046J/18.410J Professors Piotr Indyk and Charles E. Leiserson Handout 7Problem Set 1 SolutionsExercise 1-1. Do Exercise 2.3-7 on page 37 in CLRS.Solution:The following algorithm solves the problem:1.Sort the elements in S using mergesort.2.Remove the last element from S. Let y be the value of the removed element.3.If S is nonempty, look for z=x−y in S using binary search.4.If S contains such an element z, then STOP, since we have found y and z such that x=y+z.Otherwise, repeat Step 2.5.If S is empty, then no two elements in S sum to x.Notice that when we consider an element y i of S during i th iteration, we don’t need to look at the elements that have already been considered in previous iterations. Suppose there exists y j∗S, such that x=y i+y j. If j<i, i.e. if y j has been reached prior to y i, then we would have found y i when we were searching for x−y j during j th iteration and the algorithm would have terminated then.Step 1 takes �(n lg n)time. Step 2 takes O(1)time. Step 3 requires at most lg n time. Steps 2–4 are repeated at most n times. Thus, the total running time of this algorithm is �(n lg n). We can do a more precise analysis if we notice that Step 3 actually requires �(lg(n−i))time at i th iteration.However, if we evaluate �n−1lg(n−i), we get lg(n−1)!, which is �(n lg n). So the total runningi=1time is still �(n lg n).Exercise 1-2. Do Exercise 3.1-3 on page 50 in CLRS.Exercise 1-3. Do Exercise 3.2-6 on page 57 in CLRS.Exercise 1-4. Do Problem 3-2 on page 58 of CLRS.Problem 1-1. Properties of Asymptotic NotationProve or disprove each of the following properties related to asymptotic notation. In each of the following assume that f, g, and h are asymptotically nonnegative functions.� (a) f (n ) = O (g (n )) and g (n ) = O (f (n )) implies that f (n ) = �(g (n )).Solution:This Statement is True.Since f (n ) = O (g (n )), then there exists an n 0 and a c such that for all n √ n 0, f (n ) ←Similarly, since g (n )= O (f (n )), there exists an n � 0 and a c such that for allcg (n ). �f (n ). Therefore, for all n √ max(n 0,n Hence, f (n ) = �(g (n )).�()g n ,0← �),0c 1 � g (n ) ← f (n ) ← cg (n ).n √ n c � 0 (b) f (n ) + g (n ) = �(max(f (n ),g (n ))).Solution:This Statement is True.For all n √ 1, f (n ) ← max(f (n ),g (n )) and g (n ) ← max(f (n ),g (n )). Therefore:f (n ) +g (n ) ← max(f (n ),g (n )) + max(f (n ),g (n )) ← 2 max(f (n ),g (n ))and so f (n ) + g (n )= O (max(f (n ),g (n ))). Additionally, for each n , either f (n ) √max(f (n ),g (n )) or else g (n ) √ max(f (n ),g (n )). Therefore, for all n √ 1, f (n ) + g (n ) √ max(f (n ),g (n )) and so f (n ) + g (n ) = �(max(f (n ),g (n ))). Thus, f (n ) + g (n ) = �(max(f (n ),g (n ))).(c) Transitivity: f (n ) = O (g (n )) and g (n ) = O (h (n )) implies that f (n ) = O (h (n )).Solution:This Statement is True.Since f (n )= O (g (n )), then there exists an n 0 and a c such that for all n √ n 0, �)f ()n ,0← �()g n ,0← f (n ) ← cg (n ). Similarly, since g (n ) = O (h (n )), there exists an n �h (n ). Therefore, for all n √ max(n 0,n and a c � such thatfor all n √ n Hence, f (n ) = O (h (n )).cc�h (n ).c (d) f (n ) = O (g (n )) implies that h (f (n )) = O (h (g (n )).Solution:This Statement is False.We disprove this statement by giving a counter-example. Let f (n ) = n and g (n ) = 3n and h (n )=2n . Then h (f (n )) = 2n and h (g (n )) = 8n . Since 2n is not O (8n ), this choice of f , g and h is a counter-example which disproves the theorem.(e) f(n)+o(f(n))=�(f(n)).Solution:This Statement is True.Let h(n)=o(f(n)). We prove that f(n)+o(f(n))=�(f(n)). Since for all n√1, f(n)+h(n)√f(n), then f(n)+h(n)=�(f(n)).Since h(n)=o(f(n)), then there exists an n0such that for all n>n0, h(n)←f(n).Therefore, for all n>n0, f(n)+h(n)←2f(n)and so f(n)+h(n)=O(f(n)).Thus, f(n)+h(n)=�(f(n)).(f) f(n)=o(g(n))and g(n)=o(f(n))implies f(n)=�(g(n)).Solution:This Statement is False.We disprove this statement by giving a counter-example. Consider f(n)=1+cos(�≈n)and g(n)=1−cos(�≈n).For all even values of n, f(n)=2and g(n)=0, and there does not exist a c1for which f(n)←c1g(n). Thus, f(n)is not o(g(n)), because if there does not exist a c1 for which f(n)←c1g(n), then it cannot be the case that for any c1>0and sufficiently large n, f(n)<c1g(n).For all odd values of n, f(n)=0and g(n)=2, and there does not exist a c for which g(n)←cf(n). By the above reasoning, it follows that g(n)is not o(f(n)). Also, there cannot exist c2>0for which c2g(n)←f(n), because we could set c=1/c2if sucha c2existed.We have shown that there do not exist constants c1>0and c2>0such that c2g(n)←f(n)←c1g(n). Thus, f(n)is not �(g(n)).Problem 1-2. Computing Fibonacci NumbersThe Fibonacci numbers are defined on page 56 of CLRS asF0=0,F1=1,F n=F n−1+F n−2for n√2.In Exercise 1-3, of this problem set, you showed that the n th Fibonacci number isF n=�n−� n,�5where �is the golden ratio and �is its conjugate.A fellow 6.046 student comes to you with the following simple recursive algorithm for computing the n th Fibonacci number.F IB(n)1 if n=02 then return 03 elseif n=14 then return 15 return F IB(n−1)+F IB(n−2)This algorithm is correct, since it directly implements the definition of the Fibonacci numbers. Let’s analyze its running time. Let T(n)be the worst-case running time of F IB(n).1(a) Give a recurrence for T(n), and use the substitution method to show that T(n)=O(F n).Solution: The recurrence is: T(n)=T(n−1)+T(n−2)+1.We use the substitution method, inducting on n. Our Induction Hypothesis is: T(n)←cF n−b.To prove the inductive step:T(n)←cF n−1+cF n−2−b−b+1← cF n−2b+1Therefore, T(n)←cF n−b+1provided that b√1. We choose b=2and c=10.∗{For the base case consider n0,1}and note the running time is no more than10−2=8.(b) Similarly, show that T(n)=�(F n), and hence, that T(n)=�(F n).Solution: Again the recurrence is: T(n)=T(n−1)+T(n−2)+1.We use the substitution method, inducting on n. Our Induction Hypothesis is: T(n)√F n.To prove the inductive step:T(n)√F n−1+F n−2+1√F n+1Therefore, T(n)←F n. For the base case consider n∗{0,1}and note the runningtime is no less than 1.1In this problem, please assume that all operations take unit time. In reality, the time it takes to add two numbers depends on the number of bits in the numbers being added (more precisely, on the number of memory words). However, for the purpose of this problem, the approximation of unit time addition will suffice.Professor Grigori Potemkin has recently published an improved algorithm for computing the n th Fibonacci number which uses a cleverly constructed loop to get rid of one of the recursive calls. Professor Potemkin has staked his reputation on this new algorithm, and his tenure committee has asked you to review his algorithm.F IB�(n)1 if n=02 then return 03 elseif n=14 then return 15 6 7 8 sum �1for k�1to n−2do sum �sum +F IB�(k) return sumSince it is not at all clear that this algorithm actually computes the n th Fibonacci number, let’s prove that the algorithm is correct. We’ll prove this by induction over n, using a loop invariant in the inductive step of the proof.(c) State the induction hypothesis and the base case of your correctness proof.Solution: To prove the algorithm is correct, we are inducting on n. Our inductionhypothesis is that for all n<m, Fib�(n)returns F n, the n th Fibonacci number.Our base case is m=2. We observe that the first four lines of Potemkin guaranteethat Fib�(n)returns the correct value when n<2.(d) State a loop invariant for the loop in lines 6-7. Prove, using induction over k, that your“invariant” is indeed invariant.Solution: Our loop invariant is that after the k=i iteration of the loop,sum=F i+2.We prove this induction using induction over k. We assume that after the k=(i−1)iteration of the loop, sum=F i+1. Our base case is i=1. We observe that after thefirst pass through the loop, sum=2which is the 3rd Fibonacci number.To complete the induction step we observe that if sum=F i+1after the k=(i−1)andif the call to F ib�(i)on Line 7 correctly returns F i(by the induction hypothesis of ourcorrectness proof in the previous part of the problem) then after the k=i iteration ofthe loop sum=F i+2. This follows immediately form the fact that F i+F i+1=F i+2.(e) Use your loop invariant to complete the inductive step of your correctness proof.Solution: To complete the inductive step of our correctness proof, we must show thatif F ib�(n)returns F n for all n<m then F ib�(m)returns m. From the previous partwe know that if F ib�(n)returns F n for all n<m, then at the end of the k=i iterationof the loop sum=F i+2. We can thus conclude that after the k=m−2iteration ofthe loop, sum=F m which completes our correctness proof.(f) What is the asymptotic running time, T�(n), of F IB�(n)? Would you recommendtenure for Professor Potemkin?Solution: We will argue that T�(n)=�(F n)and thus that Potemkin’s algorithm,F ib�does not improve upon the assymptotic performance of the simple recurrsivealgorithm, F ib. Therefore we would not recommend tenure for Professor Potemkin.One way to see that T�(n)=�(F n)is to observe that the only constant in the programis the 1 (in lines 5 and 4). That is, in order for the program to return F n lines 5 and 4must be executed a total of F n times.Another way to see that T�(n)=�(F n)is to use the substitution method with thehypothesis T�(n)√F n and the recurrence T�(n)=cn+�n−2T�(k).k=1Problem 1-3. Polynomial multiplicationOne can represent a polynomial, in a symbolic variable x, with degree-bound n as an array P[0..n] of coefficients. Consider two linear polynomials, A(x)=a1x+a0and B(x)=b1x+b0, where a1, a0, b1, and b0are numerical coefficients, which can be represented by the arrays [a0,a1]and [b0,b1], respectively. We can multiply A and B using the four coefficient multiplicationsm1=a1·b1,m2=a1·b0,m3=a0·b1,m4=a0·b0,as well as one numerical addition, to form the polynomialC(x)=m1x2+(m2+m3)x+m4,which can be represented by the array[c0,c1,c2]=[m4,m3+m2,m1].(a) Give a divide-and-conquer algorithm for multiplying two polynomials of degree-bound n,represented as coefficient arrays, based on this formula.Solution:We can use this idea to recursively multiply polynomials of degree n−1, where n isa power of 2, as follows:Let p(x)and q(x)be polynomials of degree n−1, and divide each into the upper n/2 and lower n/2terms:p(x)=a(x)x n/2+b(x),q(x)=c(x)x n/2+d(x),where a(x), b(x), c(x), and d(x)are polynomials of degree n/2−1. The polynomial product is thenp(x)q(x)=(a(x)x n/2+b(x))(c(x)x n/2+d(x))=a(x)c(x)x n+(a(x)d(x)+b(x)c(x))x n/2+b(x)d(x).The four polynomial products a(x)c(x), a(x)d(x), b(x)c(x), and b(x)d(x)are computed recursively.(b) Give and solve a recurrence for the worst-case running time of your algorithm.Solution:Since we can perform the dividing and combining of polynomials in time �(n), recursive polynomial multiplication gives us a running time ofT(n)=4T(n/2)+�(n)=�(n2).(c) Show how to multiply two linear polynomials A(x)=a1x+a0and B(x)=b1x+b0using only three coefficient multiplications.Solution:We can use the following 3 multiplications:m1=(a+b)(c+d)=ac+ad+bc+bd,m2=ac,m3=bd,so the polynomial product is(ax+b)(cx+d)=m2x2+(m1−m2−m3)x+m3.� (d) Give a divide-and-conquer algorithm for multiplying two polynomials of degree-bound nbased on your formula from part (c).Solution:The algorithm is the same as in part (a), except for the fact that we need only compute three products of polynomials of degree n/2 to get the polynomial product.(e) Give and solve a recurrence for the worst-case running time of your algorithm.Solution:Similar to part (b):T (n )=3T (n/2) + �(n )lg 3)= �(n �(n 1.585)Alternative solution Instead of breaking a polynomial p (x ) into two smaller polynomials a (x ) and b (x ) such that p (x )= a (x ) + x n/2b (x ), as we did above, we could do the following:Collect all the even powers of p (x ) and substitute y = x 2 to create the polynomial a (y ). Then collect all the odd powers of p (x ), factor out x and substitute y = x 2 to create the second polynomial b (y ). Then we can see thatp (x ) = a (y ) + x b (y )· Both a (y ) and b (y ) are polynomials of (roughly) half the original size and degree, and we can proceed with our multiplications in a way analogous to what was done above.Notice that, at each level k , we need to compute y k = y 2 (where y 0 = x ), whichk −1 takes time �(1) per level and does not affect the asymptotic running time.。
中科大软院算法导论算法作业讲解(1-3部分)
答:伪代码(略) 循环不变式:在每一轮开始的时候,子数 组A[1..J] 都是已排好序的(循环不变式是一种性 质,不是一个数组或式子) 最坏和最还情况运行时间都是O(n*n)
4.1-2 题目:证明T(n)=2T(n/2下取整)+n 的解为O(nlgn),证明他的解也是 得到解θ运行时间为
证明:因为 用代换法可证 故得证
7.4-4
题目:证明RANDOMIZED-QUICKSORT算法的期望的运 行时间为 证明: 随机快排每次最佳情况下的划分是把所有元素 尽量均分为两份,故此情况下的递归式为
题目:设f(n)与g(n)都是渐进非负函数。利用θ的基本定 义来证明 max(f(n),g(n))= θ(f(n)+g(n)). 答:因f(n)与g(n)都是渐进非负函数,所以存在Nf和Ng 当N>Nf时,f(n)>0;N>Ng时g(n)>0.因此当N>max(Nf,Ng) 有(f(n)+g(n))/2<=max(f(n),g(n))<= f(n)+g(n) (其中 c1=1/2,c2=1)故得证。
同7.4-2,用代换法可证期望运行时间为
2.3-4: 题目:对于插入排序的递归版本,为它的运行时 间写一个递归式。 答:递归式为: θ(1) 当n=1; T(n)= T(n-1)+ θ(n) 当n>1; 其中θ(n)为把A[n]插入已排好序的数组A[1...n-1] 的运行时间
4.1-1 题目:证明T(n)=T(n/2上取整)+1的解为O(lgn) 答:用代换法(猜测+数学归纳法)证明:
算法导论第十五章习题答案
算法导论第十五章习题答案算法导论第十五章习题答案算法导论是一本经典的计算机科学教材,其中第十五章涵盖了图算法的内容。
本文将针对该章节中的习题进行解答,并对其中一些问题进行深入探讨。
1. 习题15.1-1题目要求证明:对于任意的有向图G=(V,E),如果图中不存在从节点u到节点v的路径,则在每个强连通分量中,节点u和节点v都在同一个强连通分量中。
证明:假设存在一个强连通分量C,其中节点u在C中,节点v在C'中(C'为除了C之外的其他强连通分量)。
由于不存在从u到v的路径,所以在C中不存在从u到v的路径。
但是根据强连通分量的定义,C中的任意两个节点之间都存在路径。
所以存在一条从v到u的路径。
这与C'中的节点v不在C中矛盾,所以假设不成立,节点u和节点v必定在同一个强连通分量中。
2. 习题15.2-2题目要求证明:在一个有向无环图中,存在一个拓扑排序,使得任意两个非根节点u和v,u在v之前。
证明:假设存在一个有向无环图G=(V,E),不存在上述所要求的拓扑排序。
即对于任意的拓扑排序,存在两个非根节点u和v,u在v之后。
那么我们可以得到一条从u到v的路径。
由于图中不存在环,所以路径上的节点不会重复。
我们可以将路径上的节点按照拓扑排序的顺序排列,得到一个新的拓扑排序,使得u在v之前。
这与假设矛盾,所以原命题成立。
3. 习题15.3-3题目要求证明:在一个有向图G=(V,E)中,如果存在一条从节点u到节点v的路径,那么在图的转置G^T中,存在一条从节点v到节点u的路径。
证明:假设存在一条从节点u到节点v的路径。
那么在图的转置G^T中,边(u,v)变成了边(v,u)。
所以存在一条从节点v到节点u的路径。
因此,原命题成立。
4. 习题15.4-1题目要求:给出一个算法,判断一个有向图G=(V,E)是否是有向无环图。
算法思路:我们可以使用深度优先搜索(DFS)来判断是否存在环。
具体步骤如下:1. 对于图中的每个节点v,设置一个状态标记visited[v]为false。
《算法导论》习题答案
《算法导论》习题答案Chapter2 Getting Start2.1 Insertion sort2.1.2 将Insertion-Sort重写为按非递减顺序排序2.1.3 计算两个n位的二进制数组之和2.2 Analyzing algorithms2.2.1将函数用符号表示2.2.2写出选择排序算法selection-sort 当前n-1个元素排好序后,第n个元素已经是最大的元素了.最好时间和最坏时间均为2.3 Designing algorithms计算递归方程的解(1) 当时,,显然有T((2) 假设当时公式成立,即,则当,即时,2.3.4 给出insertion sort的递归版本的递归式2.3-6 使用二分查找来替代insertion-sort中while循环j?n;if A[i]+A[j]<xi?i+1elsej?j-1if A[i]+A[j]=xreturn trueelsereturn false时间复杂度为。
或者也可以先固定一个元素然后去二分查找x减去元素的差,复杂度为。
Chapter3 Growth of functions3.1Asymptotic notation3.1.2证明对于b时,对于,时,存在,当时,对于,3.1-4 判断与22n是否等于O(2n)3.1.6 证明如果算法的运行时间为,如果其最坏运行时间为O(g(n)),最佳运行时间为。
最坏时间O(g(n)),即;最佳时间,即3.1.7:证明定义3.2 Standard notation and common functions 3.2.2 证明证明当n>4时,,是否多项式有界~与设lgn=m,则?lgn~不是多项式有界的。
mememmmm2设,,是多项式有界的3.2.5比较lg(lg*n)与lg*(lgn)lg*(lgn)= lg*n-1设lg*n=x,lgx<x-1较大。
算法导论答案(经典)
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
16.3-1 16.3-2Leabharlann 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
24.1-3
24.1-4 24.1-5* 24.1-6 修改 Bellman-Ford 算法,先找到负环上的一个节点,再依次找到负环上的一个节点,再依 次找到负环上的其他节点。 24.2-1 见图 24-5 24.2-2 最后一次不影响结果
len=j;//更新 len
} 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 章
25.1-4 25.1-5
25.1-6 25.1-7 25.1-8
25.1-9 25.1-10 25.2-1 见图 25-4 25.2-2 25.2-3 25.2-4
算法导论习题解答2.3-7
算法导论习题解答2.3-7
•2.3-7 请给出⼀个运⾏为Θ(nlgn)的算法(伪码),使之能在给定⼀个由n个整数构成的集合S和另⼀个整数x时,判断出S中是否存在有两个其和等于x的元素。
解:解题思路:先对集合S进⾏归并排序,然后新建⼀个数组S1,使得S1[i] = x – S[i],再将两个数组并起来。
如果在并的过程中发现有两个元素相等且两个元素⼀个来⾃S,⼀个来⾃S1,则可以确定S中存在有两个其和等于x的元素。
Find whether x exits
1、Sort(S)
2、for i <- 0 to Length(S) – 1
3、 do S1[i] <- x - S[i]
4、for i <- 0 to Length(S) – 1
5、 do Merge( S,S1 )
6、 if S[p] > S1[q]
7、 S0[k] <- S[p] p++ k++
8、 if S[p] < S1[q]
9、 S0[k] <- S[q] q++ k++
10、 if S[p] == S1[q]
11、 return true
12、return false
在第⼀⾏进⾏排序时,时间代价为Θ(nlgn),后来的合并过程时间代价为Θ(n),总的时间代价为Θ(nlgn)。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
解法 2:设所有 n 个时间空位都是空的。然后按罚款的单调递减顺序对各个子任 务逐个作贪心选择。 在考虑任务 j 时,如果有一个恰处于或前于 dj 的时间空位仍 空着,则将任务 j 赋与最近的这样的空位,并填入; 如果不存在这样的空位,表 示放弃。 答案(a1,a2 是放弃的) :
<a5, a4, a6, a3, a7,a1, a2>or <a5, a4, a6, a3, a7,a2, a1> 划线部分按上表 di 递增的顺序排即可,答案不唯一
19.2.2
题目:
19.2.3
题目:
19.2.5
19.2.6
第 13 次作业参考答案
20.2-1
题目:
解答:
20.2-3
题目:
解答:
20.3-1
题目:
答案:
20.3-2
题目:
答案:
第 14 次作业参考答案4-1
证明中要三点:1.有穷非空集合 2.遗传性 3.交换性
第 10 次作业参考答案
16.5-1
题目表格:
ai di wi 1 4 10 2 2 20 3 4 30 4 3 40 5 1 50 6 4 60 7 6 70
解答:
解法 1: 使用引理 16.12 性质 (2) , 按 wi 单调递减顺序逐次将任务添加至 Nt (A) , 每次添加一个元素后,进行计算,{计算方法:Nt(A)中有 i 个任务时计算 N0 (A),…,Ni(A),其中如果存在 Nj (A)>j,则表示最近添加的元素是需要放弃的,从集合 中删除};最后将未放弃的元素按 di 递增排序,放弃的任务放在所有未放弃任务 后面,放弃任务集合内部排序可随意。
33 D:\ 编 程 开 发 \VS 20 10\ my Pro gra m\ 经 典 算 法 大 全\ 练 手 题 1_ 整 数 划 分 \In ter ger _P arti tio n\I nte rge r_P arti tio n
16.3-4
第 9 次参考答案
16.2-5
贪心算法实现,证明不能少, 参考答案:
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 独立。 伪代码:
第 8 次作业答案
16.1-1
16.1-2
16.2-2
16.2-4
16.3-2
54 D:\ 编 程 开 发 \VS 20 10\ my Pro gra m\ 经 典 算 法 大 全\ 练 手 题 1_ 整 数 划 分 \In ter ger _P arti tio n\I nte rge r_P arti tio n
^
ci ci ' ( Di ) ' ( Di 1 ) ci (( Di ) k ) (( Di 1) k ) ci ( Di ) ( Di 1)
'
17.3-2
题目:
答案:
第一步:此题关键是定义势能函数 ,不管定义成什么首先要满足两个条件 对所有操作 i, ( Di ) >=0 且 ( Di ) >= ( D0 ) 比如令 i 2 k ,j,k 均为整数且取尽可能大,设势能函数 ( Di ) =2k;
19.1.2
题目:
答案: (1) 如果 x 是 p[x]的最左子节点,则 p[x]为根的子树由两个相同的二项树合并而成,以 x 为 根的子树就是其中一个二项树,另一个以 p[x]为根,所以 degree[p[x]]=degree[x]+1; (2) 如果 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]
题目:
答案:
假设第 i 个操作是 TABLE_DELETE, 考虑装载因子 : i =(第 i 次循环之后的表中的 entry 数)/(第 i 次循环后的表的大小)= numi / sizei
第 12 次参考答案
19.1.1
题目:
答案: (1) 如果 x 不是根,则 degree[sibling[x]]=degree[child[x]]=degree[x]-1 (2) 如果 x 是根, 则 sibling 为二项堆中下一个二项树的根, 因为二项堆中根链是按根的度数 递增排序,因此 degree[sibling[x]]>degree[x]
int temp=0; for(i=0;i<n;i++) for(i=0;i<n;i++) for(i=0;i<n;i++) { temp+=a[i];//temp 就是 a[0]+a[1]+…+a[i] if(temp>i)//Ni(A)>i A 不独立; }
17.1-1(这题有歧义,不扣分) a) 如果 Stack Operations 包括 Push Pop MultiPush,答案是可以保持,解释和书上的 Push Pop MultiPop 差不多. b) 如果是 Stack Operations 包括 Push Pop MultiPush MultiPop,答案就是不可以保 持,因为 MultiPush,MultiPop 交替的话,平均就是 O(K).
j
第二步:求平摊代价,公式是 ci ci ( Di ) ( Di 1) 按上面设置的势函数示例:
^
^
当 k=0, ci =…=2
^
当 k!=0, ci =…=3 显然,平摊代价为 O(1)
17.3-4
题目:
答案:
结合课本 p249,p250 页对栈操作的分析很容易有下面结果
17.4-3
a[i]=0; a[di]++;
******O(n)= O(|A|) ******O(n)= O(|A|) ******O(n)= O(|A|)
17.1-2 本题目只要证明可能性,只要说明一种情况下结论成立即可 17.2-1
第 11 次作业参考答案
17.3-1
题目:
答案:
备注:最后一句话展开:采用新的势函数后对 i 个操作的平摊代价: