ADS Correction

Ch01 AVL

After merging two Leftist Heaps H1 and H2 of different NPL's, the NPL of the resulted Leftist Heap will be no more than max(NPL of H1, NPL of H2)

Answer

F。一个左偏树差一个点满,另一个左偏树只有一个节点,则插入后增加。实际上应有 NPL(H)=max{NPL(H1),NPL(H2)}+1NPL(H)=\max\{NPL(H_1),NPL(H_2)\}+1

There exists an AVL tree of depth 6 (the depth of the root is 0) containing 31 nodes.

Answer

F。设 fnf_n 为深度为 nn 的 AVL 树的最小节点数,则:a0=1a_0=1a1=2a_1=2an=an1+an2+1a_n=a_{n-1}+a_{n-2}+1

课件也有提到:

Ch02 Red Black Tree & B+ Tree

Insert (7,15,9,11,5,8,19,18)(7, 15, 9, 11, 5, 8, 19, 18) into an initially empty 2-3 tree (with splitting), and then delete 7. Which one of the following statements is FALSE about the resulting tree?

  • A. there are 3 leaf nodes
  • B. 13 and 15 are in the same node
  • C. the parent of the node containing 18 has 3 children
  • D. the first key stored in the root is 11

Answer

B。如果叶子数量小于 2,需要一直向上寻求合并。

In a red-black tree, the number of internal nodes in the subtree rooted at x is no more than 2bh(x)12^{bh(x)} - 1 where bh(x)bh(x) is the black height of x. (T or F)

Answer

F。应为 22bh(x)12^{2bh(x)}-1。因为 2bh(x)h(x)2bh(x)\ge h(x) 对红黑树成立。

Ch04 Leftist Heap & Skew Heap

By definition, for a light node pp in a skew heap, the number of descendants of pp's right subtree is no more than 1/2 of the number of descendants of pp.

Answer

F。如果刚好等于的话根据定义应该归为重节点。考虑左子树为空右子树大小为 1 的情况。

Ch05 Binomial Queue

For a binomial queue, ___ takes a constant time on average.

  • A. merging
  • B. find-max
  • C. delete-min
  • D. insertion

Answer

D。因为是均摊复杂度,考虑进位次数的均摊,可以证明插入操作是均摊 O(1)O(1) 的。

Ch06 Backtracking

For the Turnpike reconstruction algorithm of NN points, assuming that the distance set DD is maintained as an AVL tree, the running time is O(N2logn)O(N^{2} \log n) if no backtracking happens.

Answer

T。每一层都需要 O(n)O(n)(其实总共都有 Θ(n2)\Theta(n^2) 对距离要删除)。

Given the following game tree, the red node will be pruned with α\alpha-β\beta pruning algorithm if and only if ___.

  • A. 6x136\leq x\leq13
  • B. x13x\geq13
  • C. 6x96\leq x\leq9
  • D. x9x\ge 9

Answer

D。其实这里可以剪枝的理由就是,无论其值多小,只能让父亲从 9 变成更小的值。但是如果这里整棵树的根节点已经大于等于 9,那么即使让他变的比 9 更小也已经没有意义,所以可以删除。

Ch07 Divide and Conquer

Recall that in the merge sort, we divide the input list into two groups of equal size, sort them recursively, and merge them into one sorted list. Now consider a variant of the merge sort. In this variant, we divide the input list into n\sqrt{n} groups of equal size, where nn is the input size. What is the worst case running time of this variant? (You may use the fact that merging kk sorted lists takes O(mlogk)O(m \log k) where mm is the total number of elements in these lists.)

  • A. O(nlog2n)O(n \log^2 n)
  • B. O(nlogn)O(n \log n)
  • C. None of the other options is correct
  • D. O(nlognloglogn)O(n \log n \log \log n)

Answer

B。

T(n)=nT(n)+nlogn=12nlogn+14nlogn+18nlogn+=nlognT(n) = \sqrt{n} T(\sqrt{n}) + n \log \sqrt{n} = \dfrac{1}{2} n \log n + \dfrac{1}{4} n \log n + \dfrac{1}{8} n \log n + \cdots = n\log n

Recall that, to solve the cloest pair problem, the first step of the divide-and-conquer algorithm divides the point set into LL and RR according to hte x-coordinate. Is the following statement true or false?

  • In the combine step of this algorithm, it is always to find the closest pair with one point in LL and the other in RR.

Answer

F。只检查了是否会小于 δ\delta,有可能最小的一对比这个大,直接没检查过。

Consider the following pseudo-code.

strang(a1,,ana_{1},\ldots,a_{n}):

  1. if n2022n\le 2022 then return
  2. strange(a1,,an/2a_{1},\ldots,a_{\lfloor n/2 \rfloor})
  3. strange(an/4,,a3n/4a_{\lfloor n/4 \rfloor},\ldots,a_{\lfloor 3n/4 \rfloor})
  4. for i=1i=1 to nn:
  5. \quadfor j=1j=1 to n\lfloor \sqrt n \rfloor:
  6. \quad\quadprint(ai+aja_{i}+a_{j})
  7. strange(an/4+1,,a3n/4a_{\lfloor n/4 + 1 \rfloor},\ldots,a_{\lfloor 3n/4 \rfloor})
  8. strange(an/2+1,,ana_{\lfloor n/2 + 1\rfloor},\ldots,a_{n})

What is the running time of this pseudo-code? Your answer should be as tight as possible. (You may assume that nn is a power of 2.)

  • A. None of the order options is correct.
  • B. O(n1.5)O(n^{1.5})
  • C. O(n1.5)lognO(n^{1.5}) \log n
  • D. O(n2)O(n^{2})

Answer

D。

T(n)=4T(n2)+nn=k=1logn4k(n2k)32=k=1logn22k1.5kn1.5=n1.5k=1logn20.5kT(n)=4T(\dfrac{n}{2})+n\sqrt{n}=\sum_{k=1}^{\log n} 4^k (\dfrac{n}{2^k})^{\frac{3}{2}}=\sum_{k=1}^{\log n} 2^{2k-1.5k} n^{1.5}=n^{1.5}\sum_{k=1}^{\log n} 2^{0.5k} 应用等比数列求和公式可证 T(n)=O(n2)T(n)=O(n^2)

Consider the following pseudo-code.

strange(a1,,ana_{1},\ldots,a_{n})

  1. if n2022n\leq2022 then return
  2. strange(a1,,an/2a_{1},\ldots,a_{\lfloor n/2 \rfloor})
  3. for i=1i=1 to nn:
  4. \quadfor j=1j=1 to n\lfloor \sqrt n \rfloor:
  5. \quad\quadprint(ai+aja_{i}+a_{j})
  6. strange(an/2+1,,ana_{\lfloor n/2 +1\rfloor}, \ldots, a_{n})

What is the running time of this pseudo-code? Your answer should be as tight as possible. (You may assume that nn is a power of 2.)

  • A. None of the order options is correct.
  • B. O(n1.5)O(n^{1.5})
  • C. O(n1.5)lognO(n^{1.5}) \log n
  • D. O(n2)O(n^{2})

Answer

B。可以直接应用主定理。

When solving a problem with input size NN by divide and conquer, if at each stage the problem is divided into 77 sub-problems of equal size N/3N/3, and the conquer step takes O(logN)O(\log N) to form the solution from the sub-solutions, then the overall time complexity is ___.

  • A. O(Nlog7/log3)O(N^{\log 7 / \log 3})
  • B. O(log2N)O(\log^{2} N)
  • C. O(logN)O(\log N)
  • D. O(N)O(N)

Answer

A。是直接应用主定理的结果,a=7a=7b=3b=3

Is this asymptotic upper bound for the following recursive T(n)T(n) is correct?

  • T(n)=T(n1/3)+T(n2/3)+lognT(n) = T(n^{1/3}) + T(n^{2/3}) + \log n. Then T(n)=O(lognloglogn)T(n) = O(\log n \log \log n).

Answer

T。

可以直接做:

  • k=lognk=\log n,则 T(2k)=T(2k/3)+T(22k/3)+kT(2^{k}) = T(2^{k / 3}) + T(2^{2k / 3}) + k
  • t(k)=T(2k)t(k)=T(2^{k}),则 t(k)=t(k/3)+t(2k/3)+k=O(klogk)t(k) = t(k / 3) + t(2k / 3) + k = O(k \log k)
  • T(n)=t(k)=lognloglognT(n)=t(k)=\log n \log \log n

也可以归纳法:

nlognloglogn=13logn(log13+loglogn)+23logn(log23+loglogn)+ϵlognn\log n \log \log n = \dfrac{1}{3} \log n (\log \dfrac{1}{3} + \log \log n)+\dfrac{2}{3}\log n (\log \dfrac{2}{3} + \log \log n) + \epsilon \log n

注意 log13\log \dfrac{1}{3}log23\log \dfrac{2}{3} 是小于零的常数。

Ch10 NP

If L1pL2L_{1} \leq_{p} L_{2} and L2NPL_2 \in NP, then L1NPL_1 \in NP. (T/F)

Answer

T。L1L_1 可能还是 PP 问题,但注意 PP 问题都是 NPNP 问题。

Suppose QQ is a problem in NPNP, but not necessarily NP-complete. Which of the following is FALSE?

  • A. A polynomial-time algorithm for SAT would sufficiently imply a polynomial-time algorithm for QQ.
  • B. A polynomial-time algorithm for QQ would sufficiently imply a polynomial-time algorithm for SAT.
  • C. If QPQ \notin P, then PNPP \neq NP.
  • D. If QQ is NP-hard, then QQ is NP-complete.

Answer

B。A 选项就是说能在多项式时间内解决 SAT 问题,即 P=NPP=NP,这样任何 NP 问题都能在多项式时间复杂度内解决,故自然存在对 QQ 的多项式复杂度解。但是 B 选项对于问题 QQ 就没有这一性质。

The following problem is in co-NP. (T/F)

  • Given a positive integer kk, is kk a prime number?

Answer

T。P=co-P\text{P} = \text{co-P}Pco-NP\text{P}\subseteq \text{co-NP}

Ch11 Approximation

Assume that you are a real world Chinese postman, which have learned an awesome course "Advanced Data Structures and Algorithm Analysis" (ADS). Given a 2-dimensional map indicating NN positions pi(xi,yi)p_i(x_i,y_i) of your post office and all the addresses you must visit, you'd like to find a shortest path starting and finishing both at your post office, and visit all the addresses at least once in the circuit. Fortunately, you have a magic item "Bamboo copter & Hopter" from "Doraemon", which makes sure that you can fly between two positions using the directed distance (or displacement).

However, reviewing the knowledge in the ADS course, it is an NPCNPC problem! Wasting too much time in finding the shortest path is unwise, so you decided to design a 2approximation2-approximation algorithm as follows, to achieve an acceptable solution.

Compute a minimum spanning tree T connecting all the addresses.
Regard the post office as the root of T.
Start at the post office.
Visit the addresses in order of a _____ of T.
Finish at the post office.

There are serveral methods of traversal which can be filled in the blank of the above algorithm. Assume that PNPP\neq NP, how many methods of traversal listed below can fulfill the requirement?

  • Level-Order Traversal
  • Pre-Order Traversal
  • Post-Order Traversal

Answer

2。pre-order 和 post-order 都是可行的。因为最小生成树的边权和一定小于 OPT,而这两种 travelsal 每条边最多走两次,所以是一个 2-approximation。

As we know there is a 2-approximation algorithm for the Vertex Cover problem. Then we must be able to obtain a 2-approximation algortithm for the Clique problem, since the Clique problem can be polynomially reduced to the Vertex Cover problem. (T/F)

Answer

F。虽然这两个问题是可以互相规约的,但是计算近视率时除的最优解不同,稍微构造一下即可推翻:

Ch12 Local Search

Max-cut problem: Given an undirected graph G=(V,E)G = (V, E) with positive integer edge weights wew_{e}, find a node partition (A,B)(A, B) such that w(A,B)w(A, B), the total weight of edges crossing the cut, is maximized. Let us define SS' to be the neighbor of SS that can be obtained from SS by moving one node from AA to BB, or one from BB to AA. We only choose a node which, when flipped, increases the cut value by at least w(A,B)/Vw(A, B) / |V|. Then which of the following is true?

  • A. Upon the termination of the algorithm, the algorithm returns a cut (A,B)(A, B) so that 2.5w(A,B)w(A,B)2.5 w(A, B) \geq w(A^*, B^*), where (A,B)(A^*, B^*) is an optimal partition.
  • B. The algorithm terminates after at most O(logVlogW)O(\log |V| \log W) flips, where WW is the total weight of edges.
  • C. Upon the termination of the algorithm, the algorithm returns a cut (A,B)(A, B) so that 2w(A,B)w(A,B)2w(A, B) \geq w(A^*, B^*).
  • D. The algorithm terminates after at most O(V2)O(|V|^2) flips.

Answer

A。直接代入公式:

Ch14 Parallel

Which one of the following statements about the Maximum Finding is False?

  • A. Parallel random sampling algorithm can run with O(logn)O(\log n) work load.
  • B. Common CRCW can be used to solve the access conflicts in this problem.
  • C. It can be solved by summation algorithm with time complexity being O(logn)O(\log n).
  • D. There exists parallel algorithm solving the problem in constant time.

Answer

A。

  • A 选项 workload 无论怎么说至少得有 O(n)O(n)
  • B 选项会遇到要同时写的问题,但写的数是同一个,所以实际上不会冲突(?)

When we solve the summation problem via designing the parallel algorithms, we shorten the aasymptotic time complexity but take more asymptotic work loads comparing with the sequential algorithms.

Answer

F。不一定是 more。

There is an array AA, A[i]=i (1i1024)A[i]=i\space (1\leq i\leq1024). If we use Balanced Binary Trees Parallel Algorithm to calculate the Prefix-Sum of AA. Define C(h,i)=k=1αA(k)C(h,i)=\sum_{k=1}^{\alpha} A(k), where (0,α)(0,\alpha) is the rightmost descendant leaf of node (h,i)(h,i). Node (h,i)(h,i) indicates the ii-th node in height hh. For example, the root is node (10,1)(10,1) and leaves are node (0,i) 1i1024(0,i)\space 1\leq i\leq1024. The value of C(2,223)C(2,223) is

  • A. 398278398278
  • B. 3070830708
  • C. 17011701
  • D. 67966796

Answer

A。相当于 C(0,932)C(0,932)B(i,j)B(i,j) 是对应子树和,C(i,j)C(i,j) 是子树最右端节点前缀和。B(i,j)B(i,j) 的过程从下到上并行合并,C(i,j)C(i,j) 的过程从上到下。

Ch15 External Sorting

If only one tape drive is available to perform the external sorting, then the tape access time for any algorithm will be Ω(N2)\Omega(N^2) (T/F)

Answer

T。考虑寻道时间从 O(1)O(1) 变成 O(n)O(n)

Given 100,000,000 records, each 256 bytes, and an internal memory size of 128MB, if a simple 2-way merge is used, how many passes do we have to do?

  • A. 10
  • B. 9
  • C. 8
  • D. 7

Answer

B。pass 数量是 log (runs 数量)+1(根据课件公式)

We have 4 tapes for 3-way external merge sorting. How shall we distribute 31 runs into 3 tapes, such that the total number of passes is minimized?

  • A. 15 runs on Tape 1, 9 runs on Tape 2, and 7 runs on Tape 3.
  • B. 13 runs on Tape 1, 11 runs on Tape 2, and 7 runs on Tape 3.
  • C. 13 runs on Tape 1, 10 runs on Tape 2, and 8 runs on Tape 3.
  • D. 11 runs on Tape 1, 10 runs on Tape 2, and 10 runs on Tape 3.

Answer

B。三阶斐波那契数列为:0,0,1,1,2,4,7,13,24,我们选择 FnF_nFn1+Fn2F_{n-1}+F_{n-2}Fn1F_{n-1},可以得到一组划分为 7+11+13=31,刚好就是我们想要的总数。如果不能恰好对上的话,应选第一组大于等于的。

Suppose we have the internal memory that can handle 12 numbers at a time, and the following two runs on the tapes:

Run 1: 1, 3, 5, 7, 8, 9, 10, 12 Run 2: 2, 4, 6, 15, 20, 25, 30, 32

Use 2-way merge with 4 input buffers and 2 output buffers for parallel operations. Which of the following three operations are NOT done in parallel?

  • A. 1 and 2 are written onto the third tape; 3 and 4 are merged into an output buffer; 6 and 15 are read into an input buffer
  • B. 3 and 4 are written onto the third tape; 5 and 6 are merged into an output buffer; 8 and 9 are read into an input buffer
  • C. 5 and 6 are written onto the third tape; 7 and 8 are merged into an output buffer; 20 and 25 are read into an input buffer
  • D. 7 and 8 are written onto the third tape; 9 and 15 are merged into an output buffer; 10 and 12 are read into an input buffer

Answer

D。大概是懂了,好像又没懂,求大哥评论区教教。

https://blog.csdn.net/HGGshiwo/article/details/118362764

Ex01 Amortized Analysis

A nn-nodes AVL tree TT performs insertion or deletion in the worst case that costs a0lgn+b0a_0 \lg n + b_0, a1lgn+b1a_1 \lg n + b_1, respectively, where a0,a1,b0,b1a_0, a_1, b_0, b_1 are constants. Now, we start from an empty tree, perform a sequence of n insertions or deletions in the AVL tree. To analysis the amortized cost per insertion or deletion, we define a potential function Φ(T)=a1i=1nlgi\Phi(T) = a_1 \sum_{i=1}^{n} \lg i. What is the amortized cost per insertion or deletion?

  • A. O(lgn)O(\lg n) for insertion, O(1)O(1) for deletion
  • B. O(1)O(1) for insertion, O(lgn)O(\lg n) for deletion
  • C. O(lgn)O(\lg n) for insertion, O(lgn)O(\lg n) for deletion
  • D. O(1)O(1) for insertion, O(1)O(1) for deletion

Answer

A。简单版本的理解是如果一次操作的时间贡献为 α\alpha,势能贡献为 β\betaβ\beta 可能为负数),那么这次的 amortized cost 为 α+β\alpha+\beta,或者直接用 potential method 的柿子:

(insertion)a0lgn+b0+(a1i=1nlgi)(a1i=1n1lgi)=(a0+a1)lgn+b0=O(lgn)(deletion)a1lgn+b1+(a1i=1n1lgi)(a1i=1nlgi)=b1=O(1)\begin{aligned} \text{(insertion)}\quad& a_0 \lg n + b_0 +(a_1 \sum_{i=1}^n \lg i) - (a_1 \sum_{i=1}^{n-1} \lg i) = (a_0 + a_1) \lg n + b_0 = O(\lg n) \\ \text{(deletion)}\quad& a_1 \lg n + b_1 + (a_1 \sum_{i=1}^{n-1} \lg i) - (a_1 \sum_{i=1}^n \lg i) = b_1 = O(1) \end{aligned}

We have a binary counter of k bits. Each time we conduct an increment on the counter: xx+1(mod2k)x \equiv x + 1 \pmod{2^k} and the cost of the increment is the number of bits we need to flip. For example, when k=3k = 3, currently we have x=010x = 010, after increment we have x=011x = 011. Then this increment costs 1 because only 1 bit flips after increment. If we conduct the increment again, x=100x = 100. Then this increment costs 3 because we flip 3 bits. Now we conduct n consecutive increments and estimate the total cost. Which of the following statements are TRUE?

  1. If the initial value of the counter is 0, the total cost is O(n)O(n)
  2. If the initial value of the counter is 0, the total cost is O(nlogk)O(n \log k)
  3. If n = Ω(k)\Omega(k), the total cost is O(n)O(n)
  4. If n = Ω(k)\Omega(k), the total cost is O(nlogk)O(n \log k)
  • A. 2 and 4
  • B. 2 and 3
  • C. 1 and 3
  • D. 1 and 4

Answer

C。对于 1,可以直接考虑每一位的反转次数。对于 3,考虑如果不从 11 开始可以用 O(k)O(k) 的代价消除影响,然而有 n=Ω(k)n=\Omega(k),所以有 O(n+k)=O(n)O(n+k)=O(n)

In this problem, we would like to find the amortized cost of insertion in a dynamic table TT. Initially the size of the table TT is 1. The cost of insertion is 11 if the table is not full. When an item is inserted into a full table, the table TT is expanded as a new table of size 55 times larger. Then, we copy all the elements of the old table into this new table, and insert the item in the new table.

Let num(T)num(T) be the number of elements in the table TT, and size(T)size(T) be the total number of slots of the table. Let DiD_i denote the table after applying the ii-th operation on Di1D_{i-1}.

Which of the following potential function Φ(Di)\Phi(D_i) can help us achieve O(1)O(1) amortized cost per insertion?

  • A. Φ(Di)=num(T)+size(T)5\Phi(D_{i}) = num(T) + \dfrac{size(T)}{5}
  • B. Φ(Di)=54(num(T)+size(T)5)\Phi(D_{i}) = \dfrac{5}{4} \left( num(T) + \dfrac{size(T)}{5} \right)
  • C. Φ(Di)=54(num(T)size(T)5)\Phi(D_{i})=\dfrac{5}{4}(num(T) - \dfrac{size(T)}{5})
  • D. Φ(Di)=num(T)size(T)5\Phi(D_{i}) = num(T) - \dfrac{size(T)}{5}

Answer

C。可以这样分析,设 Φ(Di)=αnum(T)+βsize(T)\Phi(D_i)= \alpha \cdot num(T)+\beta \cdot size(T)

  • 简单加入一个数,ci=1c_i = 1Φ(Di)Φ(Di1)=α\Phi(D_i)-\Phi(D_{i-1}) = \alpha
  • 加入一个数并拓展,ci=5n+1c_i=5n+1Φ(Di)Φ(Di1)=α+β4n\Phi(D_i)-\Phi(D_{i-1}) = \alpha + \beta \cdot 4 n

现在让 O(1)=1+α=α+β4nO(1)=1+\alpha=\alpha+\beta\cdot 4n,所以有 β=14\beta = -\dfrac{1}{4}。再由于 Φ(Di)0\Phi(D_i)\ge 0,可得 α54\alpha\ge \dfrac{5}{4}。所以选 C。

Other Problems

Consider a bipartite graph G(A,B,E)G(A,B,E). (Recall that a graph GG is bipartite if the vertex set of GG can be partitioned into AA and BB such that all the edges of GG have one endpoint in AA and the other in BB.) A matching in GG is a subset MM of EE such that no two edges in MM have a common vertex. A vertex cover is a subset CC of VV, where every edge in EE must have at least one endpoint in CC. Denote by S|S| the cardinality of a set SS. Which of the following statements is true?

  • A. For a minimum vertex cover CC, C=min{A,B}|C| = \min\{|A|,|B|\}.
  • B. Exactly one of the other options is wrong.
  • C. For any vertex cover CC and any matching MM, CM|C| \ge |M|.
  • D. For a minimum vertex cover CC and a maximum matching MM, C=M|C| = |M|.

Answer

A。对于 B,考虑一个不与其他点连边的孤立点,只会贡献 A,B|A|,|B| 而不会贡献 C|C|。对于 C,D,可以利用二分图最大匹配=最小点覆盖分析。

Consider two disjoint sorted arrays A[1n]A[1\ldots n] and B[1m]B[1\ldots m], we would like to compute the kk-th smallest element in the union of the two arrays, where kminm,nk\le \min{m,n}. Please choose the smallest possible running time among the following options:

  • A. O(logn)O(\log n)
  • B. min{O(logm),O(logn)}\min\{O(\log m), O(\log n)\}
  • C. O(logm)O(\log m)
  • D. O(logk)O(\log k)

Answer

D。脑筋急转弯。注意到 A,BA,B 都是 sorted,所以最后的复杂度肯定和 n,mn,m 无关。

评论

TABLE OF CONTENTS

Ch01 AVL
Ch02 Red Black Tree & B+ Tree
Ch04 Leftist Heap & Skew Heap
Ch05 Binomial Queue
Ch06 Backtracking
Ch07 Divide and Conquer
Ch10 NP
Ch11 Approximation
Ch12 Local Search
Ch14 Parallel
Ch15 External Sorting
Ex01 Amortized Analysis
Other Problems