算法导论试卷

合集下载

中科大算法导论期末试卷及答案

中科大算法导论期末试卷及答案
2. (卷 1)已知 f1(n)=Θ(g1(n)),f2(n)=Θ(g2(n)),请问是否有 f1(n)+ f2(n)= Θ( max( g1(n),g2(n) ) )?
解:存在������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)

算法导论-9.红黑树习题

算法导论-9.红黑树习题

算法导论-9.红⿊树习题这⼀篇解决《算法导论》中红⿊树章节的部分习题,在上⼀篇⾃⼰亲⾃实现红⿊树后,解决这些题⽬就轻松多了。

练习13.1-6 在⼀棵⿊⾼度为 $k$ 的红⿊树中,内节点最多有多少个?最少有多少个?⿊⾼度为 $k$ 的⼆叉树,全⾼度最⼩为 $k+1$,最⼤为 $2k+2$ 。

内节点最多有 $2^{k+1}-1$ 个,这种情况下红⿊树中没有红节点,⿊节点全满(满⾜所有叶⼦节点⿊⾼度⼀致的条件);内节点最多有 $2^{2k+2}-1=4^{k+1}-1$ 个。

这种情况下红⿊树全满。

练习13.1-7 在 $n$ 个关键字上构造出来的⼆叉树,红节点与⿊节点个数的⽐值最⼤为多少?最⼩是多少?红节点最多时,⽐值为:$$\frac{n-2^{h-1}-2^{h-3}-...-(2)-1}{2^{h-1}+2^{h-3}+...+(2)+1},h=\lfloor\lg n\rfloor$$红节点最少时,⽐值时为:$$\frac{n-2^{\lfloor\lg n\rfloor}}{2^{\lfloor\lg n\rfloor}}$$练习13.2-2 证明,在⼀棵 $n$ 个节点的⼆叉查找树中,刚好有 $n-1$ 种可能的旋转。

思路:每⼀个可能的旋转对应于⼀条边,每⼀个条边也只能对应⼀个可能的旋转,因此可能的旋转数就是边的数⽬。

每⼀条边都对应⼀个⼉⼦节点,每⼀个节点(除了根节点)都对应⼀个边,所以边的数⽬为 $n-1$ ,也就是可能的旋转的数⽬。

练习13.2-4 证明任意⼀颗含有 $n$ 个节点的⼆叉查找树都能通过 $O(n)$ 次旋转,转变为另⼀颗含有同样节点(节点位置可以任意不⼀样,但仍然保持⼆叉查找树性质)的⼆叉查找树。

思路:考虑⼀颗⼆叉查找树的“右链”,即从根节点向具有最⼤节点值的节点的路径。

不断地右旋右链上具有左⼦树的节点,每⼀次旋转可以使右链上多⼀个节点,(⼜,右链上⾄少有⼀个节点,根节点),所以⾄多 $n-1$ 次右旋后,⼆叉树的右链上连接了所有的节点,这时⼆叉树实际上称了⼀个已排序(旋转保持⼆叉查找树的性质)的单链表。

算法导论习题答案

算法导论习题答案

Chapter2 Getting Start2.1 Insertion sort2.1.2 将Insertion-Sort 重写为按非递减顺序排序2.1.3 计算两个n 位的二进制数组之和2.2 Analyzing algorithms2.2.1将函数32/10001001003n n n --+用符号Θ表示2.2.2写出选择排序算法selection-sort当前n-1个元素排好序后,第n 个元素已经是最大的元素了.最好时间和最坏时间均为2()n Θ2.3 Designing algorithms2.3.3 计算递归方程的解22()2(/2)2,1k if n T n T n n if n for k =⎧=⎨+ = >⎩ (1) 当1k =时,2n =,显然有()lg T n n n =(2) 假设当k i =时公式成立,即()lg 2lg 22i i i T n n n i ===⋅,则当1k i =+,即12i n +=时,111111()(2)2(2)222(1)22lg(2)lg i i i i i i i i T n T T i i n n ++++++==+=⋅+=+== ()lg T n n n ∴ =2.3.4 给出insertion sort 的递归版本的递归式(1)1()(1)()1if n T n T n n if n Θ =⎧=⎨-+Θ >⎩2.3-6 使用二分查找来替代insertion-sort 中while 循环内的线性扫描,是否可以将算法的时间提高到(lg )n n Θ?虽然用二分查找法可以将查找正确位置的时间复杂度降下来,但是移位操作的复杂度并没有减少,所以最坏情况下该算法的时间复杂度依然是2()n Θ2.3-7 给出一个算法,使得其能在(lg )n n Θ的时间内找出在一个n 元素的整数数组内,是否存在两个元素之和为x首先利用快速排序将数组排序,时间(lg )n n Θ,然后再进行查找:Search(A,n,x)QuickSort(A,n);i←1; j←n ;while A[i]+A[j]≠x and i<jif A[i]+A[j]<xi←i+1elsej←j -1if A[i]+A[j]=xreturn trueelsereturn false时间复杂度为)(n Θ。

USC算法导论作业2

USC算法导论作业2
CSCI 303 Homework 2
Problem 1 (4.3-2): The recurrence T (n) = 7T (n/2) + n2 describes the running time of an algorithm A. A competing algorithm A has a running time of T (n) = aT (n/4) + n2 . What is the largest integer value for a such that A is asymptotically faster than A? Solution 1: By the master theorem, the worst-case asymptotic complexity of A is Θ(nlg 7 ) ≈ Θ(n2.80735 ), and the worst-case asymptotic complexity of A is Θ(nlog4 a ), if a > 16. For A to be asymptotically faster than A, log4 a < lg 7 = log4 49. Therefore, the largest integer value for a such that A is asymptotically faster than A is 48. Problem 2 (Derived from 4-1 and 4-4): Give asymptotic upper and lower bounds for T (n) in each of the following recurrences. Make your bounds as tight as possible, and justify your answers. a. T (n) = 2T (n/2) + n3 . b. T (n) = 16T (n/4) + n2 . c. T (n) = 7T (n/3) + n2 . d. T (n) = 7T (n/2) + n2 . √ e. T (n) = 2T (n/4) + n. f. T (n) = 3T (n/2) + n lg n. √ g. T (n) = 4T (n/2) + n2 n. h. T (n) = T (9n/10) + n. Solution 2: Use the master method to solve these recurrences. a. Case 3 of master theorem. T (n) = Θ(n3 ). b. Case 2 of master theorem. T (n) = Θ(n2 lg n). c. Case 3 of master theorem. T (n) = Θ(n2 ). d. Case 1 of master theorem. T (n) = Θ(nlg 7 ). √ e. Case 2 of master theorem. T (n) = Θ( n lg n). f. Case 1 of master theorem. T (n) = Θ(nlg 3 ). √ g. Case 3 of master theorem. T (n) = Θ(n2 n). h. Case 3 of master theorem. T (n) = Θ(n). Problem 3 (Derived from 4-1 and 4-4): Give asymptotic upper and lower bounds for T (n) in each of the following recurrences. Make your bounds as tight as possible, and justify your answers. You may assume that T (1) is a constant. a. T (n) = T (n − 1) + n.

算法导论题目

算法导论题目

• A map M = {r1 , r2 , . . . , rn } is a set of n roads. • A view rectangle V = �(Vx1 , Vy1 ), (Vx2 , Vy2 )� specifies the rectangular region that should be displayed by giving the coordinates of the rectangle’s lower-left and upper-right corners, (Vx1 , Vy1 ) and (Vx2 , Vy2 ) respectively. • A road r is visible in the view rectangle V if it intersects the interior of the rectangle V . There are two types of visible roads r: Type 1: One or both endpoints of the road r are inside the view rectangle V .
You will often be called upon to “give an algorithm” to solve a certain problem. Your write-up should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of your essay should provide the following: 1. A description of the algorithm in English and, if helpful, pseudocode. 2. At least one worked example or diagram to show more precisely how your algorithm works. 3. A proof (or indication) of the correctness of the algorithm. 4. An analysis of the running time of the algorithm. Remember, your goal is to communicate. Graders will be instructed to take off points for convo­ luted and obtuse descriptions.

算法导论课程考试

算法导论课程考试

Introduction to Algorithms December17,2001 Massachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine,Lee Wee Sun,and Charles E.Leiserson Final Exam ReviewFinal Exam ReviewTrue-false questions(1)T F The best case running time for I NSERTION S ORT to sort an n element array is O(n).(2)T F By the master theorem,the solution to the recurrence T(n)=3T(n/3)+log n isT(n)=Θ(n log n).(3)T F Given any binary tree,we can print its elements in sorted order in O(n)time by per-forming an inorder tree walk.(4)T F Computing the median of n elements takesΩ(n log n)time for any algorithm workingin the comparison-based model.(5)T F Every binary search tree on n nodes has height O(log n).(6)T F Given a graph G=(V,E)with cost on edges and a set S⊆V,let(u,v)be an edgesuch that(u,v)is the minimum cost edge between any vertex in S and any vertex in V−S.Then,the minimum spanning tree of G must include the edge(u,v).(You may assume the costs on all edges are distinct,if needed.)(7)T F Computing a b takes exponential time in n,for n-bit integers a and b.(8)T F There exists a data structure to maintain a dynamic set with operations Insert(x,S),Delete(x,S),and Member?(x,S)that has an expected running time of O(1)per operation. (9)T F The total amortized cost of a sequence of n operations(i.e.,the sum over all operations,of the amortized cost per operation)gives a lower bound on the total actual cost of the sequence.(10)T F Thefigure below describes aflow assignment in aflow network.The notation a/bdescribes a units offlow in an edge of capacity b.True or False:The followingflow is a maximalflow./- 36-Q Q Q Q Q Q Q Q Q s ?Q Q Q Q Q Q Q Q Q s 3t b a 3/36/63/70/21/44/44/65/52/4ds c (11)T F Let G =(V,E )be a weighted graph and let M be a minimum spanning tree of G .The path in M between any pair of vertices v 1and v 2must be a shortest path in G .(12)T F n lg n =O (n 2)(13)T F Let P be a shortest path from some vertex s to some other vertex t in a graph.If theweight of each edge in the graph is increased by one,P remains a shortest path from s to t .(14)T F Suppose we are given n intervals (l i ,u i )for i =1,···,n and we would like to find aset S of non-overlapping intervals maximizing i ∈S w i ,where w i represents the weight of interval (l i ,u i ).Consider the following greedy algorithm.Select (in the set S )the interval,say (l i ,u i )of maximum weight w i ,remove all intervals that overlap with (l i ,u i )and repeat.This algorithm always provides an optimum solution to this interval selection problem.(15)T F Given a set of n elements,one can output in sorted order the k elements followingthe median in sorted order in time O (n +k log k ).(16)T F Consider a graph G =(V,E )with a weight w e >0defined forevery edge e ∈E .If a spanning tree T minimizes e ∈T w e then it also minimizes e ∈E w 2e,and vice versa.(17)T F The breadth first search algorithm makes use of a stack.(18)T F In the worst case,merge sort runs in O (n 2)time.(19)T F A heap can be constructed from an unordered array of numbers in linear worst-casetime.(20)T F No adversary can elicit the Θ(n 2)worst-case running time of randomized quicksort.(21)T F Radix sort requires an “in place”auxiliary sort in order to work properly.(22)T F A longest path in a dag G =(V,E )can be found in O (V +E )time.(23)T F The Bellman-Ford algorithm is not suitable if the input graph has negative-weightedges.(24)T F Memoization is the basis for a top-down alternative to the usual bottom-up versionof dynamic programming.(25)T F Given a weighted,directed graph G=(V,E)with no negative-weight cycles,theshortest path between every pair of vertices u,v∈V can be determined in O(V3)worst-case time.(26)T F For hashing an item into a hash table in which collisions are resolved by chaining,the worst-case time is proportional to the load factor of the table.(27)T F A red-black tree on128keys must have at least1red node.(28)T F The move-to-front heuristic for self-organizing lists runs no more than a constantfactor slower than any other reorganization strategy.(29)T F Depth-first search of a graph is asymptotically faster than breadth-first search.(30)T F Dijkstra’s algorithm is an example of a greedy algorithm.(31)T F Fibonacci heaps can be used to make Dijkstra’s algorithm run in O(E+V lg V)timeon a graph G=(V,E).(32)T F The Floyd-Warshall algorithm solves the all-pairs shortest-paths problem using dy-namic programming.(33)T F A maximum matching in a bipartite graph can be found using a maximum-flow al-gorithm.(34)T F For any directed acyclic graph,there is only one topological ordering of the vertices.(35)T F If some of the edge weights in a graph are negative,the shortest path from s to t canbe obtained using Dijkstra’s algorithm byfirst adding a large constant C to each edge weight, where C is chosen large enough that every resulting edge weight will be nonnegative. (36)T F If all edge capacities in a graph are integer multiples of5then the maximumflowvalue is a multiple of5.(37)T F For any graph G with edge capacities and vertices s and t,there always exists anedge such that increasing the capacity on that edge will increase the maximumflow from s to t in G.(Assume that there is at least one path in the graph from s to t.)(38)T F Heapsort,quicksort,and mergesort are all asymmptotically optimal,stable comparison-based sort algorithms.(39)T F If each operation on a data structure runs in O(1)amortized time,then n consecutiveoperations run in O(n)time in the worst case.(40)T F A graph algorithm withΘ(E log V)running time is asymptotically better than analgorithm with aΘ(E log E)running time for a connected,undirected graph G(V,E). (41)T F In O(V+E)time a matching in a bipartite graph G=(V,E)can be tested todetermine if it is maximum.(42)T F n integers each of value less than n100can be sorted in linear time.(43)T F For any network and any maximalflow on this network there always exists an edgesuch that increasing the capacity on that edge will increase the network’s maximalflow.(44)T F If the depth-first search of a graph G yields no back edges,then the graph G is acyclic.(45)T F Insertion in a binary search tree is“commutative”.That is,inserting x and then yinto a binary search tree leaves the same tree as inserting y and then x.(46)T F A heap with n elements can be converted into a binary search tree in O(n)time.。

USC算法导论作业3

USC算法导论作业3

CSCI303Homework3Problem1(9-1):Given a set A of n numbers,we wish tofind the k largest in sorted order using a comparison-based algorithm.Find the algorithm that implements each of the following methods with the best asymptotic worst-case running time,and analyze the running time of the algorithms in terms of n and k.a.Sort the numbers,and list the k largest.b.Build a max-priority queue from the numbers,and call Extract-Max k times.e an order-statistic algorithm tofind the i th largest number,partition around that num-ber,and sort the k largest numbers.Solution1:a.Merge-Sort(A,1,n)return A[n−k...k]This algorithm takes only as long as it takes to Merge-Sort a list of n numbers,so its running time isΘ(n lg n).b.Build-Max-Heap(A)for i←1to kB[i]←Heap-Extract-Max(A)return BThis algorithmfirst calls Build-Max-Heap on A,which has worst-case asymptotic com-plexity O(n).Then it calls Heap-Extract-Max k times,each of which has worst-case asymptotic complexity O(lg n).So the worst-case asymptotic complexity for this algorithm is O(n+k lg n).c.i←Select(A,k)A[n]↔A[i]Partition(A)Merge-Sort(A,n−k,n)return A[n−k...k]This algorithmfirst calls Select,which has worst-case asymptotic complexity O(n), then calls Partition,which also has worst-case asymptotic complexity O(n),then calls Merge-Sort to sort just the last k elements,which has worst-case asymptotic complexity O(k lg k).So the worst-case asymptotic complexity for this algorithm is O(n+k lg k).Problem2(9.3-5):Suppose that you have a“black-box”worst-case linear-time median subroutine.Give a simple, linear-time algorithm that solves the selection problem for an arbitrary order statistic.Solution2:Simple-Select(A,p,r,i)if p=rreturn A[p]A[r]↔A[Index-Of-Median(A,p,r)] Partition(A,p,r)k← r−p+12if i=kreturn A[k]else if i<kreturn Simple-Select(A,1,k−1,i)elsereturn Simple-Select(A,k+1,r,i−k)Problem3(9.3-8):Let X[1,...,n]and Y[1,...,n]be two arrays,each containing n numbers already in sorted order. Give an O(lg n)-time algorithm tofind the median of all2n elements in arrays X and Y. Solution3:Two-List-Median(X,p,q,Y,s,t)mx←Index-Of-Median(X,p,q)my←Index-Of-Median(Y,s,t)X[mx]↔X[q]Y[my]↔Y[t]Partition(X,p,q)Partition(Y,s,t)if X[mx]=Y[my]return X[mx]else if X[mx]>Y[my]return Two-List-Median(X,p,mx−1,Y,my+1,t)elsereturn Two-List-Median(X,mx+1,q,Y,s,my−1)Problem4(8.1-1):What is the smallest possible depth of a leaf in a decision tree for a comparison sort?Solution4:Given an array A that contains n elements,the smallest possible depth of a leaf in a decision tree to sort A using a comparison-type sort is n−1.To verify that A is in sorted order takes n−1 comparisons,and if fewer comparisons are used then at least one element was not compared to any of the others,so that element might not be in the correct position.Problem 5(Derived from 8.1-4):You are given a sequence of n elements to sort and a number k such that k divides n .The input sequence consists of n/k subsequences,each containing k elements.The elements in a given subsequence are all smaller than the elements in the succeeding subsequence and larger than the elements in the preceding subsequence.Thus,all that is needed to sort the whole sequence of length n is to sort the k elements in each of the n/k subsequences.Show an Ω(n lg k )lower bound on the number of comparisons needed to solve this varient of the sorting problem using a comparison-type sorting algorithm.(Hint:It is not rigorous to simply combine the lower bounds for the individual subsequences.)Solution 5:We will construct a decision tree for this varient of the sorting problem and show that it has height at least n lg k .Each leaf of the decision tree corresponds to a permutation of the original sequence.How many permutations are there?Each subsequence has k !permutations,and there are n/k subsequences,so there are (k !)n/k permutations of the whole sequence.Thus the decision tree has (k !)n/k leaves.A binary tree with (k !)n/k leaves has height at least lg (k !)n/k .lg (k !)n/k =n/k lg(k !)=Θ(n/k ·k lg k )=Θ(n lg k )Therefore the lower bound on the number of comparisons needed to solve this varient of the sorting problem using a comparison-type sorting algorithm is Ω(n lg k ).。

算法基础试题及答案

算法基础试题及答案

算法基础试题及答案一、单项选择题(每题2分,共10分)1. 以下哪个选项是算法的基本特征之一?A. 有穷性B. 可行性C. 确定性D. 以上都是答案:D2. 在算法设计中,以下哪个步骤是不必要的?A. 问题定义B. 算法描述C. 算法实现D. 算法测试答案:D3. 算法的时间复杂度通常用来描述什么?A. 算法的运行时间B. 算法的空间需求C. 算法的执行步骤数量D. 算法的输入数据大小答案:A4. 以下哪个不是算法设计的基本方法?A. 递归B. 排序C. 搜索D. 迭代答案:B5. 在算法分析中,大O符号表示什么?A. 算法执行的时间B. 算法执行的空间C. 算法执行的最坏情况D. 算法执行的平均情况答案:C二、填空题(每题2分,共10分)1. 算法的输入输出定义了算法的______,算法的步骤定义了算法的______。

答案:功能;实现2. 算法的时间复杂度和空间复杂度是衡量算法______的两个重要指标。

答案:效率3. 在算法设计中,______是一种通过重复执行代码块来实现的算法结构。

答案:循环4. 递归算法通常包括两个基本部分:______和______。

答案:基本情况;递归情况5. 在算法分析中,______复杂度描述了算法执行过程中所需的存储空间。

答案:空间三、简答题(每题5分,共20分)1. 请简述算法的五个基本特征。

答案:算法的五个基本特征包括有穷性、确定性、可行性、输入和输出。

有穷性指算法必须在执行有限步骤后结束;确定性指算法的每一步都必须有明确的定义;可行性指算法的每一步都必须足够基本,以至于可以精确地执行;输入指算法有0个或多个输入,以描述运算的对象和初始条件;输出指算法至少有一个输出,输出表示算法运行的结果。

2. 算法的时间复杂度和空间复杂度有什么区别?答案:时间复杂度主要关注算法执行所需的时间,它通常与算法中操作的数量有关,而空间复杂度则关注算法执行过程中所需的存储空间。

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

共 2页 第 1 页
5、(15 分)在 0/1 背包问题中,设 f(i , y)表示剩余容量为 y,剩余可选物品为: i,i+1,…,n 时的最优解的值。 1)给出 f(i , y)的递推表达式; 2)请设计计算 f(i , y)的算法; 3)设 W=[10,20,15,30],P=[6,10,15,18], m=48,请用你的算法求解,要求写 出计算过程。
4、(15 分)[节点覆盖问题] 假设 G=(V,E)为一个无向图,G 的节点覆盖为节点 集合 V 的一个子集 U,满足 E 中每一条边至少与 U 中的一个顶点相关联。 最小节点覆盖是具有最少节点数量的一个覆盖。先制定贪婪准则为:每次 从未被覆盖的顶点中选择一个顶点,使得它所覆盖的未被覆盖的顶点数目 最多。 1)这种法总能产生一个最优解吗?如果能,请证明之;如果不能,请举 出一个反例。 2)用伪代码描述这种贪心算法。
西安交通大学考试题
成绩
课程
系别 专业班号 姓名
算法导论
ቤተ መጻሕፍቲ ባይዱ
考试日期
年 月日
学号
期中
期末 √
1、(10 分)证明:若 f(n)= amnm+…+a1n+a0 那么 f(n)= (nm),f(n)= (nm)。
(am > 0),
2、(20 分)到商场购买商品需要找零钱。假设有四种面值分别为 14 角、5 角、 2 角和 1 角的硬币可以找零,售货员希望找零的硬币数目最少。也就是, 优化目标是找零的硬币数目最少,限制条件是所选择的硬币的总面值等于 要找的零钱数。
1)假设要找的零钱数分别是 13 角,21 角和 41 角;(贪婪策略:每一次选 择应该使硬币的面值最大), 给出相应的解。
2)假设要找的零钱数是 n 角,请用 C 语言或伪代码描述算法。
3、(10 分)有 n(=2 k)个硬币,其中 1 个是假币,且假币和真币的重量不同。 请用分而治之方法设计一个找出假币的算法,并用伪代码描述你的算法。
共 2页 第 2 页
6、(15 分)请分别说明分治策略、动态规划、贪心算法、回溯法和分支限界法 的基本思想。
7、(15 分)旅行商问题:给出一个 n 个顶点网络,要求找出一个包含所有 n 个 顶点的具有最小耗费的环路。任何一个包含所有 n 个顶点的环路被称作一 个旅行。在旅行商问题中,要求设法找到一条最小耗费的旅行。 1)若图的邻接矩阵如下,画出旅行商问题的解空间树; 2)对该树运用回溯算法求解,并写出依回溯算法遍历节点的序列; 3) 用 C 语言或伪代码描述求解旅行商问题的回溯算法。 ∞ 20 30 10 11 15 ∞ 16 4 2 35 ∞ 2 4 19 6 18 ∞ 3 16 4 7 16 ∞
相关文档
最新文档