学霸总结的《算法设计》最全期末的题库(和解答)。
题目:A directed graph G is strongly connected if and only if G with its edge directions
reversed is strongly connected.答案: True, 强连通图(Strongly Connected Graph): 如果有向图 G 中的每一对节点 u 和 v 都存在互相可达的有向路径(即 u 到 v 和 v 到 u 都有路径),则称 G 是强连通图。
题目:If a weighted undirected graph has two MSTs, then its vertex set can be partitioned
into two, such that the minimum weight edge crossing the partition is not unique.答案: True,假设 T1 和 T2 是两个不同的最小生成树。设 e1 是在 T1 中但不在 T2 中的一条边。从 T1 中移除 e1 将图的顶点集划分为两个连通分量。存在一条唯一的边(称为 e2)在 T2 中跨越这个划分。由于 T1 和 T2 的最优性,e1 和 e2 应该具有相同的权重,并且进一步应该是所有跨越这个划分的边中权重最小的。
题目:If the vertex set of a weighted undirected graph can be partitioned into two, such that
the minimum weight edge crossing the partition is not unique, then the graph has atleast two MSTs.答案: False,考虑边集为 {(a, b), (b, c), (c, d)},其中所有边的权重相同的图。集合 ({a, d}, {b, c}) 是一个不具有唯一最小权重边的划分,然而这个图(作为一棵树)有一个唯一的最小生成树。
题目:The number of binomial trees in a binomial heap with n elements is at most O(log n).
答案:True。在二项堆中,每个元素都对应一个二项树,而二项树的数量受限于元素个数 n 的二进制表示中 1 的个数。由于 n 个元素的二进制表示中最多有 log n 个 1,所以二项树的数量至多为 O(log n)。
题目:A bipartite graph cannot contain a cycle of length 3 or greater.
答案:False,考虑一个图,它也是一个有 4 个顶点的环。它是二分图,与题中的断言相矛盾。
题目:Since Fibonacci heaps guarantee amortized cost and not worst case cost, the run time complexity of Dijkstra's algorithm using Fibonacci heaps may be worse than that of a
binary heap implementation.答案:False.对于包含 n 个条目的 Fibonacci 堆,删除最小元素的摊销运行时间为 O(log n),而减小关键字的摊销运行时间为 O(1)。因此,对于每个提取最小元素/减小关键字的序列(总共有 A 次提取最小元素和 B 次减小关键字),最坏情况下运行时间被界定为 O(B + A log n)。
当应用于 Dijkstra 算法中的优先队列实现时,A 是 O(B),即访问的顶点数在渐近意义上被边松弛的次数限制。而二叉堆的实现需要至少 Theta(B log n) 的时间,因为减小关键字操作需要 Theta(log n) 的时间。因此,即使在最坏情况下,Fibonacci 堆的实现速度也更快。
题目:Implementations of Dijkstra’s and Kruskal’s algorithms are identical except for the relaxation steps.
答案:False
迪杰斯特拉算法(Dijkstra's)和普里姆算法(Prim's)有相似之处,而不是迪杰斯特拉算法和克鲁斯卡尔算法(Kruskal's)。例如,在克鲁斯卡尔算法的通用阶段,我们可能有多个未连接的组件(一个森林),而迪杰斯特拉算法始终构建一棵树。
题目:The divide step in the divide and conquer algorithm always takes less time than the
combine step, therefore, the complexity of a divide and conquer algorithm is neverdependent on the divide step.答案:False,"Divide" 步骤可能比 "Combine" 步骤更慢。在分治算法中,一般有三个步骤:分解(Divide)、解决(Conquer)、合并(Combine)。"Divide" 步骤通常是将问题分解为子问题,而 "Combine" 步骤则是将子问题的解合并起来。有时候,问题的分解可能涉及到较为复杂的操作,导致 "Divide" 步骤相对耗时。而 "Combine" 步骤可能是将已经解决的子问题的结果简单地合并,因此较为高效。
题目:Suppose that in an instance of the original Stable Marriage problem with n couples,
there is a man M who is first on every woman's list and a woman W who is first onevery man's list. If the Gale-Shapley algorithm is run on this instance, then M and Wwill be paired with each other.答案:True,如果对于配对 (M, W) 来说它们不是相互匹配的,那么由于 (M, W) 相互偏好对方而不愿意与其他人配对,它们可以进行交换,从而导致匹配不稳定。
题目:For a search starting at node s in graph G, the DFS Tree is never as the same as the
BFS tree.答案:False. 当图 G 是一棵树时就是一个反例.
题目:Suppose you are choosing between the following three algorithms:
I) Algorithm A solves problems by dividing them in constant time into fivesubproblems of half the size, recursively solving each subproblem, and thencombining the solutions in linear time.II) Algorithm B solves problems of size n by dividing in constant time andrecursively solving two subproblems of size n-1 and then combining the solutions inconstant time.III) Algorithm C solves problems of size n by dividing them in constant time intonine subproblems of size n/3, recursively solving each subproblem, and thencombining the solutions in O(n 2 ) time.What are the running times of each of these algorithms (in big-O notation), and whichwould you choose?答案:I) 算法 A 的运行时间是 O( n^( log2(5)))。它的递归过程分割成五个子问题,每个子问题大小是原问题的一半,因此是典型的分治算法的运行时间。 II) 算法 B 的运行时间是 O( 2^n)。每次递归中,问题的大小减小了 1,导致总共需要 O(n) 次递归调用,每次调用的时间是常数。 III) 算法 C 的运行时间是 O( n^2 log n)。递归过程分割成九个子问题,每个子问题大小是原问题的 1/3,因此需要 O(log n) 层递归,每层递归的时间是 O(n^2)。
题目:Suppose a CS curriculum consists of n courses, all of them mandatory. The prerequisite graph G has a node for each course, and an edge from course v to course w if and only if v is a prerequisite for w. Find an algorithm that works directly with this graph
representation, and computes the minimum number of semesters necessary to completethe curriculum (assume that a student can take any number of courses in one semester).The running time of your algorithm should be linear.答案 : 首先,进行一次广度优先搜索(BFS)或深度优先搜索(DFS)来确保图是一个有向无环图(DAG)(否则,课程工作不能在有限学期内完成,因为会存在先修依赖的环路)。然后,问题转化为在DAG中寻找最长路径的长度,解决方法如下:
进行一次拓扑排序,得到顶点的线性排序 {v1, v2, v3, ..., vn},时间复杂度为 O(|V| + |E|)。回顾 HW 4,问题 5,拓扑排序保证了对于所有的 j>i,不存在从 vj 到 vi 的有向路径。
将所有边的长度设为 1。
然后,迭代地(类似于 Dijkstra 的修改在 HW 4,问题 5 中)计算到达每个顶点的最长路径的长度,直到 vn。
用 L(v) 表示以 v 结尾的最长路径的长度。从 v1 开始。由于 v1 没有入边,到达 v1 的最长路径的长度是 L(v1) = 0。到达 v2 的最长路径的长度是 L(v2) = 1(如果边 (v1, v2) 存在);否则为 0。更一般地,如果 vi 没有入边,则设 L(vi) = 0;如果 vi 有入边,则 L(vi) = max(L(vk) + 1),其中最大值取自所有满足 (vk, vi) 存在于图中的 vk。注意:排序意味着我们只需要考虑 k<i 的情况。
所需的学期数最终是 1 + max(L(v)),其中最大值取自所有顶点。
注意,一旦排序完成,每条边 (vk, vi) 最多只被考虑一次,因此在给定排序的情况下计算 L() 值的运行时间是 O(|V| + |E|)。
总的来说,该算法的运行时间为 O(|V| + |E|)。
问题:In Internet routing, there are delays on lines but also, more significantly, delays at
routers. Suppose that in addition to having edge lengths {l e