114 年 國立臺灣大學資料科學碩士學位學程《資料結構與演算法》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題5 分

  1. (5 points) Consider the following randomized algorithm, which prints out some number of exclamation
    marks (!) and some number of asterisks (*). Assume that the size of A is n initially. What are the
    expected numbers of !'s and 's that the algorithm prints out? Choose the tightest possible answers.
    RandomizedPrint (A):
    n = length(A)
    if n < 1:
    print "!"
    return
    for i in {0,...,n-1}:
    print "
    "
    Choose a uniformly random integer p in {1,..., n-1}
    RandomizedPrint(A[:p])
    RandomizedPrint(A[p:])
    (A) !: O(logn), *: O(nlogn)
    (B) !: O(logn), *: O(n²)
    (C) !: O(n), *: O(nlogn)
    (D) !: O(n), *: O(n²)
    (E) !: O(nlogn), *: O(n²)

登入後即可作答並保存紀錄。

這一題的完整詳解

分析
設 T∗(n)T_*(n) 為印出 * 的期望次數,T!(n)T_!(n) 為印出 ! 的期望次數。

  • 若 n=0n=0,只印 !,故 T!(0)=1,  T∗(0)=0T_!(0)=1,\;T_*(0)=0。
  • 若 n≥1n\ge1,演算法先印出 nn 個 *,再以均勻隨機 p∈{1,…,n−1}p\in\{1,\dots,n-1\} 分割成兩子陣列,遞迴處理。

因此

T∗(n)=n+1n−1∑p=1n−1(T∗(p)+T∗(n−p))(1)T_*(n)=n+\frac{1}{n-1}\sum_{p=1}^{n-1}\bigl(T_*(p)+T_*(n-p)\bigr) \tag{1}
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題5 分

  1. (5 points) Which of the following statements about the Akra-Bazzi theorem is correct?
    (A) It can only be applied to recurrences where all subproblems have equal size.
    (B) It requires that the function g(n) in the recurrence satisfies a polynomial growth condition.
    (C) It always produces a closed-form solution for the recurrence.
    (D) It is less general than the Master Theorem for solving recurrences.
    (E) The value of p in the theorem must always be an integer.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Akra–Bazzi 定理用來分析分治遞迴式,尤其適合各子問題規模不相同的情況。常見形式為

T(x)=∑i=1kaiT(bix+hi(x))+g(x),T(x)=\sum_{i=1}^{k} a_iT(b_ix+h_i(x))+g(x),

其中 ai>0a_i>0、0<bi<10<b_i<1,而 hi(x)h_i(x) 是相對於 xx 足夠小的規模擾動項。函數 g(x)g(x) 需符合定理要求的正則性與多項式成長條件。

先找出滿足下式的實數 pp:

∑i=1kaibip=1.\sum_{i=1}^{k}a_ib_i^p=1.

接著,遞迴式的漸近解由下式決定:

T(x)=Θ(xp(1+∫1xg(u)up+1 du)).T(x)=\Theta\left(x^p\left(1+\int_1^x\frac{g(u)}{u^{p+1}}\,du\right)\right).

解題方法

逐一檢查選項是否符合定理的適用範圍與結論:

  • 定理可處理不同大小的子問題。
  • 定理對非遞迴項 g(n)g(n) 有成長條件。
  • 定理給出漸近複雜度,不保證總能得到封閉形式。
  • Akra–Bazzi 的適用範圍比 Master 定理廣。
  • 指數 pp 是方程式的實數解,不要求為整數。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題5 分

  1. (5 points) Consider the following five DNA sequences:
    a) ATCGTA
    b) ATCGTT
    c) GCATAG
    d) GCATAC
    e) TTAGCG
    Which of the following statements is correct regarding the application of a c-means clustering algorithm
    to these sequences (c = 3)?
    (A) The algorithm will always halt with exactly three clusters regardless of the chosen distance metric.
    (B) Using Hamming distance as the similarity measure, sequences a and b will always be in the same
    cluster.
    (C) The algorithm guarantees finding the globally optimal clustering solution for these sequences.
    (D) Using k-mer frequency vectors (k = 2) as features, sequences c and d will never be in the same
    cluster.
    (E) The time complexity of the k-means algorithm for clustering these sequences is O(n), where n is
    the number of sequences.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查 cc-means/kk-means 的分群數、距離與分群結果的關係,以及演算法的收斂與時間複雜度。

設定 c=3c=3 表示演算法以三個群集為目標,但不代表相近的兩筆資料必定同群,也不代表演算法找到全域最佳解。標準 kk-means 每次迭代的時間複雜度為 O(nkd)O(nkd);若迭代 II 次,總時間複雜度為 O(nkdI)O(nkdI),其中 nn 是資料筆數、kk 是群集數、dd 是特徵維度。

解題方法

逐項檢查敘述是否能由演算法定義保證:

  1. 分群數設為三,是否代表任何距離度量下都必定收斂並產生三群?
  2. 兩序列的 Hamming 距離較小,是否足以保證它們同群?
  3. 演算法是否保證找到全域最佳解?
  4. 特徵向量不同,是否代表兩序列不能同群?
  5. 時間複雜度是否能忽略群數、特徵維度與迭代次數?

選項分析

(A) 錯誤。 設定 c=3c=3 是指定目標群數,不代表「不論距離度量都必定停止並產生三個非空群集」。標準 kk-means 的中心更新使用特徵平均值,演算法本身也不是對任意距離度量都能直接套用。題目敘述的「總是」與「不論距離度量」過於絕對。

(B) 錯誤。 序列 a 與 b 的 Hamming 距離為 11,因為只有最後一個字元不同:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題5 分

  1. (5 points) Consider two protein sequences A and B of lengths m and n respectively. You are tasked
    with implementing a dynamic programming algorithm to find the optimal global alignment between
    these sequences. Given the following definitions:
    • A match score of +2 for identical amino acids.
    • A mismatch penalty of -1 for different amino acids.
    • A gap penalty of -2 for each gap introduced.
    Which of the following statements is correct regarding the dynamic programming solution for this
    alignment problem?
    (A) The time complexity of the algorithm is O(m+n).
    (B) The space complexity of the algorithm is O(mn).
    (C) The recurrence relation for the optimal alignment score S(i, j) at position (i,j) in the dynamic
    programming matrix is:
    S(i, j) = max{S(i-1,j-1)+match(A[i], B[j]), S(i-1,j) -2, S(i, j -1)-2}.
    (D) The traceback procedure to reconstruct the optimal alignment always starts from the top-left
    corner of the dynamic programming matrix.
    (E) The algorithm guarantees finding all possible optimal alignments between the two sequences.

登入後即可作答並保存紀錄。

這一題的完整詳解

關鍵概念

  • 全局比對使用 Needleman‑Wunsch DP。
  • DP 表格大小為 (m+1)×(n+1)(m+1)\times(n+1),故時間 O(mn)O(mn),空間亦 O(mn)O(mn)(未做空間優化)。
  • 轉移式必須同時考慮匹配/錯配、插入與刪除三種情形。

選項判斷

  • (A) O(m+n)O(m+n) 為線性時間,與 DP 需要遍歷整個表格不符 → 錯。
  • (B) 空間 O(mn)O(mn) 正確,但題目要求「關於 DP 解法的正確敘述」中,較核心的是遞迴關係式 → 次要。
  • (C) 正確:
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題5 分

  1. (5 points) Many graph theory can be used to find the number of a chemical structure; a possible one
    can be used according to the following steps:
    a) Represent the molecule as an undirected graph G = (V, E), where V is the set of vertices and E is
    the set of edges (bonds).
    b) Initialize each vertex v ∈ V with a value equal to its degree (number of incident edges).
    c) For a fixed number of iterations or until convergence:
    a. For each vertex v, compute a new value as the sum of the current values of its neighbors (not
    including itself).
    b. Update all vertex values simultaneously.
    d) After the iterations, sort the vertices based on their final values in non-increasing order, tie breaking
    with smaller labels.
    Consider the following 2-Carboxyoxybenzoic acid structure represented as a graph (Numbers in paren-
    theses are temporary labels for reference, not the final numbering.):
    🖼️【此處有附圖,請對照原卷】
    Apply the above algorithm for two iterations to this graph. What is the correct ordering of the vertices
    after these iterations?
    (A) 5, 6, 10, 11, 9, 4, 3, 7, 8, 12, 13, 1, 2
    (B) 5, 10, 11, 9, 3, 6, 4, 7, 8, 12, 13, 1, 2
    (C) 5, 6, 10, 11, 9, 3, 4, 7, 8, 12, 13, 1, 2
    (D) 5, 10, 11, 9, 6, 3, 4, 7, 8, 12, 13, 1, 2
    (E) None of the above choices.
🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 2 頁原卷第 3 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

每個頂點的初始值是其度數,也就是相鄰頂點的數量。每次迭代時,頂點的新值等於上一輪所有鄰點的值總和;所有頂點都使用同一輪的舊值計算,再同時更新。完成兩輪後,依數值由大到小排序;數值相同時,標籤較小者排前面。

圖中的鍵結視為無向圖的邊,每條鍵結只計為一條邊。

解題方法

依圖可列出各頂點的鄰點:

頂點鄰點
1、23
31、2、4
43、5
54、6、10
65、7
76、8
87、9
98、10
105、9、11
1110、12、13
12、1311

依標籤 11 至 1313 排列,初始度數為

(1,1,3,2,3,2,2,2,2,3,3,1,1)(1,1,3,2,3,2,2,2,2,3,3,1,1)

第一輪以初始度數加總鄰點的值,得到

(3,3,4,6,7,5,4,4,5,8,5,3,3)(3,3,4,6,7,5,4,4,5,8,5,3,3)

第二輪改以第一輪的值加總鄰點的值,得到

(4,4,12,11,19,11,9,9,12,17,14,5,5)(4,4,12,11,19,11,9,9,12,17,14,5,5)
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6 題5 分

  1. (5 points) Consider a knapsack with capacity 17 and five types of items, each with its size and value
    listed below. All items are available in unlimited quantities and each item can be selected more than
    once. The goal is to maximize the total value of the items placed in the knapsack. If the maximum
    value of the knapsack is 10a+b where a and b are integers in {0, 1, 2,..., 9}, what is the value of a +b?
    item A B C D E
    size 3 4 7 8 9
    value 4 5 10 11 12
    (A) 3
    (B) 4
    (C) 5
    (D) 6
    (E) 7
🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 3 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考無限背包問題:每種物品都能重複選取,總尺寸不得超過容量 1717,並使總價值最大。

令 dp[w]dp[w] 表示容量為 ww 時可取得的最大價值,則遞迴式為:

dp[w]=max⁡i∈{A,B,C,D,E}si≤w(dp[w−si]+vi)dp[w]=\max_{\substack{i\in\{A,B,C,D,E\}\\s_i\le w}}\bigl(dp[w-s_i]+v_i\bigr)

其中 sis_i、viv_i 分別是物品 ii 的尺寸與價值。由於物品可重複選取,每次計算都能再次使用任一種物品。

解題方法

依照尺寸由小到大計算 dp[w]dp[w],得到:

容量 ww012345678
最大價值 dp[w]dp[w]00045581011
容量 ww91011121314151617
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7 題5 分

  1. (5 points) Max-flow algorithms can be applied to determine the elimination of sports teams in a group.
    Consider the following record for a group of five teams. Note that the number of remaining games for
    a team does not necessarily equal the number of remaining games against the group's rivals, as teams
    may play opponents outside their own group. Only the team with the highest number of wins will
    advance to the next round. Our goal is to determine whether team E has already been eliminated.
    The intuition behind constructing the graph is to assume that team E wins all of its remaining 28 (TE)
    games, and then attempt to ensure that the number of wins for each competing team does not exceed
    the total possible wins of team E (WE + TE = 76). The constructed graph consists of 12 nodes: in
    addition to the source (s) and sink (t) nodes, there are six nodes representing the head-to-head games
    between teams A-D and four nodes representing teams A-D. What is the maximum flow value of the
    constructed graph?
    Games against each other
    Wins (w:) Losses (li) To play (ri) team A team B team C team D team E
    Team
    team A
    75
    59
    28
    5
    7
    4
    3
    team B
    71
    63
    28
    5
    2
    4
    4
    team C
    69
    65
    28
    7
    2
    4
    0
    team D
    63
    71
    28
    4
    4
    4
    0
    team E
    48
    86
    28
    3
    4
    0
    0
    (A) 22
    (B) 23
    (C) 24
    (D) 25
    (E) 26
🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 3 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考「棒球淘汰問題」中的最大流建模。假設 E 贏下剩餘 28 場比賽後,E 最多有

48+28=7648+28=76

勝。因此 A、B、C、D 各自還能增加的勝場上限分別為:

A:76−75=1,B:76−71=5,C:76−69=7,D:76−63=13A:76-75=1,\quad B:76-71=5,\quad C:76-69=7,\quad D:76-63=13

建圖時,源點連到每一組對戰的比賽節點,容量為該組剩餘比賽數;比賽節點連到參賽隊伍節點,容量視為無限;各隊伍節點再連到終點,容量為該隊可增加的勝場上限。

解題方法

A、B、C、D 之間共有六組對戰,場數為:

AB=5,AC=7,AD=4,BC=2,BD=4,CD=4AB=5,\quad AC=7,\quad AD=4,\quad BC=2,\quad BD=4,\quad CD=4

源點到六個比賽節點的總容量為

5+7+4+2+4+4=265+7+4+2+4+4=26

隊伍 D 雖然可增加至多 13 勝,但與 A、B、C 之間只有

4+4+4=124+4+4=12

場比賽,所以最多只能從這些對戰流入 12 單位。A、B、C 的勝場容量合計為

1+5+7=131+5+7=13
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 8 題5 分

  1. (5 points) Select all pairs of functions in the increasing order of growth. That is, select g1, g2 if g2 grows
    faster than g1.
    f1(n) = n
    f2(n) = 2log n, f3(n) = n/(n-4)
    f4(n) = √2n, f5(n) = n5(logn)²
    (A) f1(n), f5(n)
    (B) f2(n), f5(n)
    (C) f2(n), f4(n)
    (D) f4(n), f5(n)
    (E) f1(n), fs(n)

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考漸近成長率的比較。若

lim⁡n→∞g2(n)g1(n)=∞\lim_{n\to\infty}\frac{g_2(n)}{g_1(n)}=\infty

則 g2g_2 的成長速度快於 g1g_1,應選 (g1,g2)(g_1,g_2)。

依題目常見排版還原,函數為 f2(n)=2log⁡nf_2(n)=2^{\log n}、f4(n)=2nf_4(n)=\sqrt{2^n}、f5(n)=n5(log⁡n)2f_5(n)=n^5(\log n)^2;採演算法常用的 log⁡2n\log_2 n。選項 E 的「fsf_s」依函數編號判讀為 f3f_3。

解題方法

先判斷各函數的漸近成長率:

  • f1(n)=nf_1(n)=n,是線性成長。

  • f2(n)=2log⁡2n=nf_2(n)=2^{\log_2 n}=n,與 f1f_1 同階。

  • f3(n)=nn−4f_3(n)=\dfrac{n}{n-4}。當 n→∞n\to\infty 時,

    lim⁡n→∞nn−4=1\lim_{n\to\infty}\frac{n}{n-4}=1

    因此 f3(n)=Θ(1)f_3(n)=\Theta(1)。

  • f4(n)=2n=2n/2f_4(n)=\sqrt{2^n}=2^{n/2},是指數成長。

  • f5(n)=n5(log⁡n)2f_5(n)=n^5(\log n)^2,是多項式乘上對數因子。

由此得到成長順序:

f3≺f1∼f2≺f5≺f4f_3 \prec f_1\sim f_2 \prec f_5 \prec f_4

其中 ≺\prec 表示右側函數成長較快,∼\sim 表示兩者同階。特別是,指數函數 2n/22^{n/2} 的成長快於任何固定次方的多項式乘對數因子。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 9 題5 分

  1. (5 points) Select all the correct statement(s) from the following statements regarding recurrence.
    (A) The solution to the recurrence T(n) = 2T(n/2) + Θ(n) is Θ(n²).
    (B) The solution to the recurrence T(n) = 7T(n/2) + Θ(n²) is Θ(n^log27).
    (C) The solution to the recurrence T(n) = logn+T(√n) is Θ(logn).
    (D) The solution to the recurrence T(n) = 2"T(n-1) is ((√2)n²+n). (Assume T(n) = 1 for n smaller
    than some constant c).
    (E) The solution to the recurrence T(n) = T(n/6) +T(7n/9) + O(n) is O(n). (Assume T(n) = 1 for
    n smaller than some constant c).

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考遞迴式的漸近解,主要工具包括:

  • 主定理(Master Theorem):處理 T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n),比較 f(n)f(n) 與 nlog⁡ban^{\log_b a} 的成長速度。
  • 遞迴展開與遞迴樹:將每層的工作量加總,適合分析縮小幅度不相同的子問題。
  • 直接代入與幾何級數:可用來推導單一路徑遞迴,或驗證線性時間上界。

解題方法

逐一將各選項的遞迴式展開,並比較其實際成長速度與選項所聲稱的漸近界。

(A) T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)

由主定理,a=2a=2、b=2b=2,因此

nlog⁡ba=nlog⁡22=n.n^{\log_b a}=n^{\log_2 2}=n.

而 f(n)=Θ(n)f(n)=\Theta(n),與 nlog⁡ban^{\log_b a} 同階,故

T(n)=Θ(nlog⁡n).T(n)=\Theta(n\log n).

選項聲稱 Θ(n2)\Theta(n^2),不正確。

(B) T(n)=7T(n/2)+Θ(n2)T(n)=7T(n/2)+\Theta(n^2)

由主定理,a=7a=7、b=2b=2,所以遞迴子問題的基準項為

nlog⁡ba=nlog⁡27.n^{\log_b a}=n^{\log_2 7}.

因為 log⁡27>2\log_2 7>2,n2n^2 比 nlog⁡27n^{\log_2 7} 成長得慢,符合主定理第一種情形。因此

T(n)=Θ(nlog⁡27).T(n)=\Theta(n^{\log_2 7}).

選項正確。

(C) T(n)=log⁡n+T(n)T(n)=\log n+T(\sqrt n)

每次遞迴都將問題規模開平方。令 n0=nn_0=n,經過 ii 次遞迴後,問題規模為

ni=n1/2i.n_i=n^{1/2^i}.

遞迴停止時,nin_i 小於固定常數 cc。取對數後可知,遞迴層數為 Θ(log⁡log⁡n)\Theta(\log\log n)。

第 ii 層的非遞迴工作量為

log⁡(ni)=log⁡(n1/2i)=log⁡n2i.\log(n_i)=\log\left(n^{1/2^i}\right)=\frac{\log n}{2^i}.

將各層工作量加總:

∑i=0Θ(log⁡log⁡n)log⁡n2i=Θ(log⁡n),\sum_{i=0}^{\Theta(\log\log n)}\frac{\log n}{2^i} =\Theta(\log n),

因為這是首項為 log⁡n\log n 的幾何級數。因此選項正確。

(D) T(n)=2nT(n−1)T(n)=2^nT(n-1)

依題目排版,選項聲稱解為 (2)n2+n(\sqrt{2})^{n^2+n}。反覆展開遞迴式:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 10 題5 分

  1. (5 points) Select all the correct statement(s) from the following statements regarding binary trees and
    binary search trees.
    (A) Every binary tree is a binary search tree.
    (B) Every binary search tree on n nodes has height O(logn).
    (C) In a binary search tree, we can find the next smallest element to a given element in O(1) time.
    (D) Let A1, A2, and A3 be three sorted arrays of n distinct real numbers. Constructing a balanced
    binary search tree of the set A₁ U A₂ U A3 requires O(n) time.
    (E) Let T be a complete binary tree with n nodes. Finding a path from the root of T to a given vertex
    v∈T using breadth-first search takes Ω(n log n) time.

登入後即可作答並保存紀錄。

這一題的完整詳解

A:錯。二元樹僅是每個節點至多有左、右子樹;二元搜尋樹必須滿足「左子樹所有鍵 < 根鍵 < 右子樹所有鍵」的排序性質,並非所有二元樹皆符合。

B:錯。若插入順序為已排序序列,BST 會退化成鏈狀,最高可達 h=n−1h = n-1,高度上界僅為 O(n)O(n),非必然 O(log⁡n)O(\log n)。

C:錯。尋找給定節點的後繼(次小元素)需沿父指標上移或向右子樹的最左葉子,最壞情況需走過樹高 hh,時間 O(h)O(h),在一般 BST 中 hh 可能為 Θ(n)\Theta(n),不保證 O(1)O(1)。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 11 題5 分

  1. (5 points) Let S be a collection of n objects, each with a unique integer key a¡ ∈ {0,1,2,..., n² - 1}.
    Consider hashing all the objects in S into an array A[0..m-1] of m = [n^1.5] buckets. Each bucket
    stores a (singly) linked list of all objects hashed into the same bucket. The hashing algorithm works as
    follows. The algorithm first picks a hash function h uniformly at random from a hash family H. Then,
    for each object i ∈ S, the algorithm computes h(a) and inserts the object into the bucket A[h(a)].
    The number of collisions T is defined to be the total number of (unordered) object pairs hashed into
    the same bucket. We focus on the choice of the hash family H and the worst-case expected number
    of collisions E[T]. Note that the term worst-case refers to the choice of the input set S. Select all
    statement(s) that are correct for every sufficiently large n.
    (A) If H is a uniform hash family, then E[T] = O(n).
    (B) If H is a universal hash family, then E[T] = O(n).
    (C) If H = {h(x) = (ax + b) mod m | a, b ∈ Z and 0 ≤ a, b ≤ m - 1}, then E[T] = O(n).
    (D) If H = {h(x) = (x²+x+c) mod m | c∈ Z and 0 ≤ c≤m-1}, then E[T] = O(n).
    (E) The hash family described in (C) is a universal hash family.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

碰撞總數可寫成每一對不同物件是否落入同一桶的指示變數總和:

T=∑{i,j}⊆S1[h(ai)=h(aj)]T=\sum_{\{i,j\}\subseteq S}\mathbf{1}[h(a_i)=h(a_j)]

因此,期望值為:

E[T]=∑{i,j}⊆SPr⁡[h(ai)=h(aj)]\mathbb{E}[T] =\sum_{\{i,j\}\subseteq S}\Pr[h(a_i)=h(a_j)]

共有 (n2)\binom{n}{2} 對物件。均勻雜湊下,每對碰撞機率是 1/m1/m;通用雜湊則保證每對碰撞機率至多為 1/m1/m。題目中 m=⌊n1.5⌋=Θ(n1.5)m=\lfloor n^{1.5}\rfloor=\Theta(n^{1.5}),所以這兩種情況的期望碰撞數都是 O(n0.5)O(n^{0.5}),自然也是 O(n)O(n)。

解題方法

對選項 (C) 的仿射雜湊函數,令兩個鍵相差 dd。因為 bb 會在比較時抵消,碰撞條件是:

ad≡0(modm)ad\equiv 0\pmod m

當 mm 是合數時,這個條件可能對多個 aa 成立,碰撞機率可能大於 1/m1/m。要判斷最壞情況期望是否仍為 O(n)O(n),可取一組讓許多物件對都在同一類碰撞條件下的鍵。

對選項 (D),隨機的 cc 只是對所有雜湊值加上相同常數,不會改變物件彼此是否碰撞。因此可直接尋找許多鍵,使 x2+xx^2+x 在模 mm 下相同。

選項分析

(A) 正確。 均勻雜湊下,每一對不同鍵的碰撞機率為 1/m1/m,故

E[T]=(n2)1m=Θ(n2n1.5)=Θ(n0.5)=O(n)\mathbb{E}[T]=\binom{n}{2}\frac{1}{m} =\Theta\left(\frac{n^2}{n^{1.5}}\right) =\Theta(n^{0.5})=O(n)

(B) 正確。 通用雜湊保證任意一對不同鍵的碰撞機率至多為 1/m1/m,所以

E[T]≤(n2)1m=O(n0.5)=O(n)\mathbb{E}[T]\le \binom{n}{2}\frac{1}{m} =O(n^{0.5})=O(n)

(C) 錯誤。 取 n=k2n=k^2,其中 kk 為質數,則 m=k3m=k^3。令輸入鍵為

0, k2, 2k2, …, (k2−1)k20,\ k^2,\ 2k^2,\ \ldots,\ (k^2-1)k^2
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 12 題5 分

  1. (5 points) Let G = (V, E, w) be an undirected, weighted, simple, and connected graph. Let T₁ and T₂
    be two minimum spanning trees of G. Define the set of edges F ⊆ E as all edges that are in either T₁
    or T₂ but not in both. Suppose that F ≠ ∅. Select all correct statement(s) below.
    (A) The subgraph H = (V, F) is a disjoint union of cycles and paths.
    (B) All edges in F have the same weight.
    (C) There is no edge in F that has a unique weight.
    (D) There exists a graph G such that w(F) > w(T₁).
    (E) Consider the problem MOST-DISTANT-MSTS, which seeks two minimum spanning trees T₁ and
    T₂ in order to maximize w(F) on G. Assume P≠ NP. Then MOST-DISTANT-MSTS is NP-hard.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

最小生成樹(MST)是總權重最小的生成樹。兩棵 MST 的共同邊權重相同,因此若它們的對稱差集為 FF,則 FF 中來自兩棵樹的邊,按每個權重值分別計算時,數量相等。

本題也考查割性質:若某條邊是其跨越割中唯一最輕的邊,則它會出現在每一棵 MST 中。

解題方法

逐項檢查 FF 的結構、邊權與總權重,再判斷「最遠 MST」問題是否能以多項式時間求解。

選項分析

(A) 錯誤。 H=(V,F)H=(V,F) 不一定是由路徑與環組成的互不相交聯集,因為頂點在 FF 中的度數可以大於 22。

例如取所有邊權皆為 11 的完全圖 K4K_4,頂點為 a,b,c,da,b,c,d。令

T1={ab,ac,ad},T2={ab,bc,cd}.T_1=\{ab,ac,ad\},\qquad T_2=\{ab,bc,cd\}.

兩者都是 MST,而

F={ac,ad,bc,cd}.F=\{ac,ad,bc,cd\}.

在 HH 中,頂點 cc 與 a,b,da,b,d 相連,度數為 33,故 HH 不是路徑與環的互不相交聯集。

(B) 錯誤。 FF 中可以有不同權重的邊。

例如以一條權重為 00 的橋連接兩個三角形;其中一個三角形的三條邊權重皆為 11,另一個三角形的三條邊權重皆為 55。兩棵 MST 可以在兩個三角形中分別選擇不同的邊。此時 FF 同時含有權重為 11 與權重為 55 的邊。

(C) 正確。 假設邊 ee 的權重在整張圖中只出現一次。若 ee 出現在某棵 MST T1T_1,刪除 ee 會將 T1T_1 分成兩個連通部分,形成一個割。依 MST 的割性質,ee 必為跨越此割的唯一最輕邊,因此必出現在每一棵 MST。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 13 題5 分

  1. (5 points) Consider the (incomplete) pseudocode snippet SOLVE().
    1: function SOLVE(G = (V, E, w), s, t)
    2: P=V
    3: Set A[s] = 0 and A[x] = ∞ for all x ≠ s.
    4: while P do
    5: x = argmin_{p∈P}{A[x]}
    6: P=P-{x}
    7: for each y ∈ P such that (x,y) ∈ E, do
    8: ()
    9: return (**)
    ▷ Initializes a set with all vertices.
    ▷ Initializes an array A.
    ▷ Chooses a vertex.
    ▷ Removes the vertex.
    You have realized that this code snippet can be used to solve different problems by filling the blanks
    in different ways. Here are three problems we consider:
    • In the ST-DISTANCE problem, you are given a graph G with non-negative weights, and two vertices
    s, t ∈ V. The goal is to compute the shortest distance between s and t.
    • In the ST-BOTTLENECK problem, you are given a graph G with non-negative weights, and two
    vertices s, t ∈ V. The goal is to compute the smallest possible weight wgt such that there exists
    at least one path from s to t using only edges whose weights are less than or equal to wgt.
    • In the MST problem, you are given an undirected, weighted, and connected graph G. The goal
    is to compute the total weight of a minimum spanning tree.
    Select all correct statement(s) below.
    (A) To solve ST-DISTANCE, we fill (
    ) with A[y] = A[x] + w(x, y), and we fill () with A[t].
    (B) To solve ST-BOTTLENECK, we fill (*) with A[y] = max{A[y], min{A[x], w(x, y)}}, and we fill (
    )
    with A[t].
    (C) To solve MST, we fill (*) with A[y] = min{A[y], w(x, y)}, and we fill (**) with A[t].
    (D) The code snippet can be implemented with Fibonacci heaps, so there exist algorithms with running
    time O(|E|+|V|log|V|) for all three problems stated above.
    (E) The value A[t] will always be non-negative in any correct implementation for solving any of the
    problems stated above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這段程式的架構是「每次從尚未處理的頂點中,取出目前標記值最小的頂點,再更新它的鄰點」。三個問題都能使用這種逐步選取頂點的架構,但標記值的意義與更新方式不同:

  • 最短距離:A[v]A[v] 表示從 ss 到 vv 的目前最短距離估計。
  • 瓶頸路徑:A[v]A[v] 表示從 ss 到 vv 的路徑中,最小可能的最大邊權。
  • 最小生成樹:A[v]A[v] 表示將 vv 接到目前生成樹所需的最小邊權。

前兩者是 Dijkstra 類型的標記更新;最小生成樹則是 Prim 演算法。三者的「選最小標記頂點」相似,但更新公式與最後輸出不可混為一談。

解題方法

要判斷選項,分別檢查三件事:更新式是否維持正確的標記意義、回傳值是否符合問題目標,以及是否能用 Fibonacci heap 支援必要操作。

三個問題的正確更新方式如下。

ST-DISTANCE

A[y]←min⁡{A[y], A[x]+w(x,y)}A[y]\gets \min\{A[y],\,A[x]+w(x,y)\}

回傳 A[t]A[t]。更新時必須與原標記比較,保留較小的距離候選值。

ST-BOTTLENECK

A[y]←min⁡{A[y], max⁡{A[x],w(x,y)}}A[y]\gets \min\{A[y],\,\max\{A[x],w(x,y)\}\}

延伸一條邊後,路徑的瓶頸值是原瓶頸與新邊權的較大者;再從所有候選路徑中取最小值。回傳 A[t]A[t]。

MST

使用 Prim 演算法時,更新式為

A[y]←min⁡{A[y], w(x,y)}A[y]\gets \min\{A[y],\,w(x,y)\}

此處 A[y]A[y] 是將 yy 接入目前生成樹所需的最小邊權。最小生成樹總權重應回傳所有頂點鍵值的總和,而非單一頂點的 A[t]A[t]:

∑v∈VA[v]\sum_{v\in V} A[v]

根頂點的 A[s]=0A[s]=0,因此不影響總和。

選項分析

(A) 錯誤。
選項給的更新式是 A[y]=A[x]+w(x,y)A[y]=A[x]+w(x,y),沒有與 A[y]A[y] 原值取最小值,可能把較好的候選距離覆寫成較差的距離。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 14 題5 分

  1. (5 points) Let G be an n×n grid graph, with each undirected edge having a certain weight. Specifically,
    the vertices of G can be labeled as (i, j) where 1 ≤ i, j ≤ n. There is an edge between (i, j) and (k, l) if
    and only if |i-k|+|j-l|=1. Suppose we want to compute the value of a minimum cut that separates
    (1,1) and (n,n). The value of a cut is defined as the sum of weights to all edges crossing the cut.
    Four students proposed different ideas for solving this task. Let us examine their approaches and select
    all correct statement(s) below.
    (A) Student A: Some minimum cut must cut through at most 2n edges. Since there are 2n(n-1) ≤ 2n²
    edges in G, we can design a brute force algorithm that enumerates at most 22, (22) subsets of
    edges and check: if the removal of those edges separates (1,1) and (n,n). Any such subset leads
    to a candidate for the minimum cut.
    (B) Student B: For some minimum cut, the crossing edges must form a monotonic path from (n, 1) to
    (1,n). In other words, those edges form a path of exactly 2n-2 edges from (n, 1) to (1,n). Thus,
    we can solve this problem via dynamic programming.
    (C) Student C: Why don't we simply computes a maximum flow and then turning it to a minimum
    cut? First, construct a flow network by letting (1, 1) be the source and (n, n) be the sink. For each
    undirected edge connecting (i, j) and (k, l), create two directed edges in opposite directions, both
    with capacities equal to that edge's weight. By using Dinitz's (Dinic's) algorithm, the maximum
    flow from (1, 1) to (n,n) can be computed in O(n). Using the Max-Flow Min-Cut Theorem, the
    returned maximum flow value equals the value of the minimum cut.
    (D) Student D: To separate (1, 1) and (n, n), for some minimum cut, the crossing edges must form a
    shortest path from the south-west border WS := {(i, 1) | 1 < i < n} ∪ {(n, j) | 1 ≤ j < n} to
    the north-east border NE := {(i,n) | 1 ≤ i < n} ∪ {(1,j) | 1 < j ≤ n}. Thus, we can run the
    Bellman-Ford algorithm that seeks for any shortest path from a vertex in WS to any vertex in
    NE. Finally, the length of the shortest path is the value of the minimum cut.
    (E) None of the above choices.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心概念
此題的重點在於了解 平面格狀圖的最小 ss-tt 切割 與 對偶圖的最短路徑 之間的關係,以及 最大流–最小割定理 的正確應用與演算法時間複雜度。


(A) 逐一列舉至多 22n2^{2n} 個邊集合即可得到最小割

  • 錯誤原因

    • 最小割的 邊數 未必受 2n2n 的上界限制。雖然任一 單調路徑 長度為 2n−22n-2,但最小割的邊集合不一定形成單一路徑;它可以是一條在對偶圖中的曲線,跨過多於 2n2n 條原圖邊。例如,若所有邊權重相同,最小割會是 寬度為 nn 的切線,其跨過的邊數為 nn,已超過 2n2n 的上界並不保證。
    • 更嚴重的是,若某些邊的權重為 00,最小割可能會包含大量 00 權重的邊,而仍保持同樣的最小總權值。此時最小割的邊數可高達 Θ(n2)\Theta(n^2)。因此僅列舉「至多 22n2^{2n}」的子集合 不一定涵蓋所有可能的最小割,會遺漏正確答案。
  • 結論:此敘述 不正確。


(B) 最小割的跨邊必形成從 (n,1)(n,1) 到 (1,n)(1,n) 的單調路徑

  • 錯誤原因

    • 最小割在原圖中對應的是 對偶圖的一條最短路徑。對偶圖的頂點是原圖的面,而對偶圖的邊對應原圖的邊。此最短路徑的起點與終點分別位於 西南邊界 與 東北邊界 的 面,而非原圖的頂點 (n,1)(n,1)、(1,n)(1,n)。
    • 因此跨過的邊集合 不一定 按照原圖頂點的單調路徑排列;它可能在格子中「蜿蜒」而形成曲線。
  • 結論:此敘述 不正確。


(C) 用 Dinic 計算最大流,時間 O(n)O(n),即得最小割值

  • 正確的部分

    • 以每條無向邊建立兩條相反方向的有向邊,容量等於該邊的權重,確實可以把問題轉成 最大流,再利用 最大流–最小割定理 取得最小割值。
  • 錯誤的部分

    • 圖 GG 有 n2n^2 個頂點與約 2n(n−1)=Θ(n2)2n(n-1)=\Theta(n^2) 條邊。Dinic 演算法在一般圖上的時間複雜度為 O(EV)O(E\sqrt V)(在單位容量圖上為 O(Elog⁡V)O(E\log V)),在此情況下至少是 O(n2n2)=O(n3)O(n^2\sqrt{n^2})=O(n^3),遠大於 O(n)O(n)。
    • 敘述中將時間誤寫為 O(n)O(n),顯然不符合實際分析。
  • 結論:此敘述 不正確(雖然概念正確,但時間複雜度錯誤,使整體敘述錯誤)。


(D) 最小割的跨邊即為從 WS 到 NE 的最短路徑,使用 Bellman‑Ford

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 15 題5 分

  1. (5 points) In this problem we use the following notations. For any binary tree T, we define T.x to be
    its root node, T.l (resp. T.r) to be the left (resp. right) child of T.r, and T.L (resp. T.R) to be the
    left (resp. right) subtree of T.x. For any vertex z on a binary tree, we define x.l, x.L, x.r, and x.R
    similarly. These values are defined as ⊥ (empty) if they do not exist. The set of leaves of T are denoted
    as Leaf(T). For any statement P we define [P] to be 1 if P is true and 0 if P is false.
    Let T be a binary tree of n nodes. Each node v on T has a weight w(v) and a color c(v). We want to
    make the tree colorful in the sense that every non-root tree node has a color different from its parent.
    To do so, some re-coloring to the tree nodes are necessary, and we would like to minimize the total
    weights of the tree nodes that are re-colored. Notice that there are infinitely many colors to choose
    so once a node is re-colored it gets a unique color. Let f(T) be the answer. Suppose that both T.L
    and T.R are non-empty. What is the correct recursive formulations for f(T)? Select all choice(s) that
    apply.
    (A) Let T' ⊆ T be the (partial) binary tree consisting of all tree node v who has the same color as
    the tree root T.x and all v's ancestors have the same color c(T.x) too. Then, f(T) = g(T') +
    Σa∈Leaf(T) (f(a.L) + f(a.R)), here g(T') is the weight of the maximum weighted independent set
    on T'.
    (B) With the same notion T' defined above, f(T) = h(T') + Σa∈Leaf(T) (f(a.L) + f(a.R)), here h(T')
    is the weight of the minimum weighted vertex cover on T'.
    (C) With the same notion T' defined above, f(T) = h(T') + Σa∈Leaf(T) (f(a.L) + f(a.R)), here h(T)
    is the weight of the minimum weighted dominating set on T'.
    (D) f(T) = min{f(T.L) + f(T.R) + w(T.x), [c(T.l)==c(T.x)] · (w(T.l) + f(T.L)) + [c(T.l) ≠ c(T.x)] ·
    f(T.L) + [c(T.r)==c(T.x)] · (w(T.r) +f(T.R)) + [c(T.r) ≠ c(T.x)] · f(T.R)}
    (E) None of the above choices apply.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

只要一條樹邊兩端的節點原本同色,就至少要重新著色其中一端。把「需要重新著色的節點」視為頂點集合,這個集合必須涵蓋所有同色邊,因此是同色邊所形成圖上的頂點覆蓋。

重新著色的總權重要最小,所以目標是求最小權重頂點覆蓋。由於新顏色彼此唯一,重新著色的節點不會再與相鄰節點同色。

解題方法

令 GTG_T 是由樹 TT 的節點及「兩端原本同色」的邊所構成的圖,則

f(T)=GT 的最小權重頂點覆蓋值.f(T)=\text{$G_T$ 的最小權重頂點覆蓋值}.

若 T′T' 是由根節點開始、沿著原色與根相同的節點所形成的連通子樹,T′T' 內的同色邊正是需要處理的邊。離開 T′T' 後,每個邊界子樹可獨立處理。因此,正確的分解是 T′T' 的最小權重頂點覆蓋值,加上所有「接在 T′T' 節點底下、但不屬於 T′T'」的子樹答案:

f(T)=MVC⁡(T′)+∑a∈T′ ∑b∈{a.l,a.r}b≠⊥, b∉T′f(Tb)f(T)=\operatorname{MVC}(T')+\sum_{a\in T'}\ \sum_{\substack{b\in\{a.l,\,a.r\}\\ b\neq\perp,\ b\notin T'}} f(T_b)

其中 TbT_b 是以 bb 為根的子樹,MVC⁡(T′)\operatorname{MVC}(T') 表示 T′T' 上同色邊的最小權重頂點覆蓋值。題目選項的求和寫成 Leaf⁡(T)\operatorname{Leaf}(T),也就是原樹的葉節點;葉節點沒有子樹,這一項恆為 0,等於完全沒計入 T′T' 外面那些子樹的成本。即使改成 Leaf⁡(T′)\operatorname{Leaf}(T') 也不夠,因為異色子樹也可能接在 T′T' 的內部節點下。

選項分析

(A) 錯誤。
重新著色的節點須構成頂點覆蓋,不是最大權重獨立集。兩者雖有補集關係,但最大權重獨立集的權重本身不等於最小權重頂點覆蓋的權重。以根權重為 11、兩個葉節點權重各為 1010,且三者同色為例,最小權重頂點覆蓋只需重新著色根,權重為 11;最大權重獨立集則選兩個葉節點,權重為 2020。此外,求和索引也有上述葉節點記號問題。

(B) 依題目字面寫法錯誤。
最小權重頂點覆蓋是正確的核心模型,但求和範圍寫為 Leaf⁡(T)\operatorname{Leaf}(T),因此公式不完整。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 16 題5 分

  1. (5 points) Consider the following 0-1 integer linear program P:
    maximize 2x1+0x2+2x3+5x4
    subject to 2x1+x2+3x3+4x4 ≤8
    4x1+x2+2x3+3x4 ≤8
    x1+8x2+x3+7x4 ≤8
    X1, X2, X3, X4 ∈ {0,1}
    Let α be the number of feasible solutions to P. Let β be the number of optimal solutions to P. Let γ be
    the integrality gap. Let P' be the relaxed linear program of P. Let v4 be the four-dimensional volume of
    the convex polytope that consists of all feasible solutions to P'. Let v3 be the three-dimensional volume
    of the convex polyhedron that consists of all optimal solutions to P'. Select all correct statement(s)
    below.
    (A) α = 8
    (B) β≠ 1
    (C) γ = 1
    (D) v4 <1
    (E) v3 = 0

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

將 xi∈{0,1}x_i\in\{0,1\} 放寬為 0≤xi≤10\le x_i\le1,即可得到線性規劃鬆弛 P′P'。本題要分別計算:

  • α\alpha:原 0-1 整數規劃的可行解數。
  • β\beta:原問題的最優整數解數。
  • γ\gamma:整數間隙。以下採最大化問題常用的乘法定義:γ=OPTLP/OPTIP\gamma=\mathrm{OPT}_{LP}/\mathrm{OPT}_{IP}。
  • v4v_4:鬆弛可行區域在四維空間中的體積。
  • v3v_3:鬆弛最優解集合的三維體積。

解題方法

1. 計算整數可行解與最優解

若 x2=1x_2=1,第三條限制式變成

x1+x3+8+7x4≤8,x_1+x_3+8+7x_4\le8,

因此只能有 (x1,x2,x3,x4)=(0,1,0,0)(x_1,x_2,x_3,x_4)=(0,1,0,0),這一解的目標值為 00。

若 x2=0x_2=0,只需檢查 (x1,x3,x4)(x_1,x_3,x_4) 的 8 種組合。當 (x1,x3,x4)=(1,1,1)(x_1,x_3,x_4)=(1,1,1) 時,三條限制式左側皆為 99,不符合上限 88;其餘 7 種組合皆可行。因此

α=7+1=8.\alpha=7+1=8.

在可行整數解中,目標值最大的組合為 (1,0,0,1)(1,0,0,1) 與 (0,0,1,1)(0,0,1,1),兩者目標值皆為 77。所以

OPTIP=7,β=2.\mathrm{OPT}_{IP}=7,\qquad \beta=2.

2. 求線性規劃鬆弛的最優值與最優解集合

將三條限制式分別乘上 25,15,25\frac25,\frac15,\frac25 後相加,左側得到

2x1+2x3+5x4+195x2≤8.2x_1+2x_3+5x_4+\frac{19}{5}x_2\le 8.

因為 x2≥0x_2\ge0,所以 P′P' 的目標值滿足

2x1+2x3+5x4≤8.2x_1+2x_3+5x_4\le8.
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 17 題5 分

  1. (5 points) Let A, B, C, D, E be decision problems. We denote a polynomial time reduction with the
    symbol "≤p". Suppose that A ≤p B, B ≤p C, C≤p D, and C≤p E. We also assume A∈ P and
    B∈ NP. Select all correct statement(s) below.
    (A) If A is NP-hard, then B ∈ P.
    (B) If B is NP-hard and E≤p B, then C is NP-complete and D is NP-hard.
    (C) If C is NP-complete, then both D and E are NP-complete.
    (D) If D ∈ NP, then C ∈ NP.
    (E) If E is NP-hard, then C is NP-hard.

登入後即可作答並保存紀錄。

這一題的完整詳解

關鍵概念

  • X≤pYX\le _p Y 表示在多項式時間內把 XX 轉換為 YY。
  • 若 X≤pYX\le _p Y 且 Y∈PY\in\mathsf P,則 X∈PX\in\mathsf P。
  • 若 X≤pYX\le _p Y 且 XX 為 NP‑hard,則 YY 也是 NP‑hard(或更難)。
  • 若 X≤pYX\le _p Y 且 Y≤pXY\le _p X,則 XX 與 YY 多項式等價,擁有相同的複雜度等級。

逐項判斷

(A) 錯
AA NP‑hard 且 A≤pBA\le _p B ⇒ BB 至少同樣 NP‑hard,不能保證 B∈PB\in\mathsf P。

(B) 正
假設 BB NP‑hard 且 E≤pBE\le _p B。已知 B≤pCB\le _p C 與 C≤pEC\le _p E,得到

B≤pC≤pE≤pB,B\le _p C\le _p E\le _p B,

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 18 題5 分

  1. (5 points) Consider the task of incrementing a k-bit binary counter from all zeroes to all ones. Let A
    be an array of length k that simulates the counter, where A[0] represents the least significant bit and
    A[k-1] represents the most significant bit. We implement the INCREMENT() operation as follows:
    function INCREMENT()
    i=0
    while A[i] == 1 do
    A[i] = 0
    i=i+1
    A[i] = 1
    Unfortunately, this counter is implemented on a deficient random access machine (RAM). On this
    machine, each read-access to a single array entry takes 1 unit of time. However, a write to an entry
    may take different amount of time. In particular, for any index i (0 ≤ i <k), if i is a multiple of 3
    but not a multiple of 5, the memory stick will fizz and it will take exactly 31/3 units of time to write
    the data into A[i]. If i is a multiple of 5 but not a multiple of 3, the memory stick will buzz and it will
    take exactly 51/5 units of time to write the data into A[i]. Writing into all other entries not mentioned
    above takes 1 unit of time as usual.
    Please design a potential function Φ(A) such that the amortized time (using Φ(A)) per INCREMENT()
    operation is still O(1) when incrementing from zero. Make sure your potential function is rigorously
    defined, can be evaluated into an actual number by human beings, and as clean and clear without
    ambiguity. For example, if you left any undetermined variable (such as "for some constant c") in your
    expression, you must state an actual number for that variable (e.g., "c = 1"). You do not need to
    verify your answer in the answer sheet. This problem has no partial score.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

使用**勢能法(potential method)**分析攤銷成本。若一次操作的實際成本為 cic_i,勢能函數為 Φ\Phi,則該操作的攤銷成本定義為

c^i=ci+Φi−Φi−1.\hat{c}_i = c_i + \Phi_i - \Phi_{i-1}.

只要每次操作的攤銷成本都有固定上界,且勢能非負、初始勢能為 00,就能推出從全零開始執行 nn 次操作的總實際成本為 O(n)O(n)。

解題方法

設 tt 為此次遞增時,最低位起連續的 11 的個數。也就是說,A[0],…,A[t−1]A[0],\ldots,A[t-1] 都是 11,而 A[t]=0A[t]=0。這次操作會:

  • 讀取 A[0]A[0] 到 A[t]A[t],共 t+1t+1 次讀取。
  • 將 A[0],…,A[t−1]A[0],\ldots,A[t-1] 設為 00,並將 A[t]A[t] 設為 11,共 t+1t+1 次寫入。

每次寫入成本至多為

max⁡(313,515,1)=313.\max\left(\frac{31}{3},\frac{51}{5},1\right)=\frac{31}{3}.

因此,這次操作的實際成本至多為

ci≤(t+1)(1+313)=343(t+1).c_i \le (t+1)\left(1+\frac{31}{3}\right) = \frac{34}{3}(t+1).

定義勢能函數為

Φ(A)=343∑r=0k−1A[r].\Phi(A)=\frac{34}{3}\sum_{r=0}^{k-1} A[r].
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 19 題10 分

  1. (10 points) Consider the following decision problem ODDNUMBERSUM:
    ODDNUMBERSUM
    Input: A set of positive odd numbers S. A target odd number k.
    Question: Is there an odd number of elements in S whose sum is exactly k?
    (a) (5 points) Prove that ODDNUMBERSUM is NP-complete.
    (b) (5 points) For any set of numbers T we define sum(T) to be the sum of all numbers in T. Let S
    and k be the input from ODDNUMBERSUM. Define k* = max{sum(T) | T ⊆ S and sum(T) ≤ k}
    be the largest possible subset sum among all subsets whose sum does not exceed k.
    Let ε > 0 be any sufficiently small constant. Give an algorithm that computes an (1 – ε)-
    approximate solution to k*. Specifically, your algorithm should output a value kALG such that
    (1-ε)k* ≤ kALG≤ k*. The running time of your algorithm should be a polynomial of |S| and
    1/ε. State the algorithm as concisely and percisely as possible. You should not write anything
    besides the algorithm.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考兩件事:

  1. NP 完全性證明:證明問題屬於 NP,並從已知的 NP 完全問題做多項式時間歸約。
  2. 子集合和的近似演算法:以動態規劃列出可達子集合和,定期刪除彼此太接近的值,將清單大小控制在多項式範圍內。

由於 SS 中每個數都是奇數,任一子集合的和,其奇偶性等於該子集合的元素個數奇偶性。因此,和為奇數的子集合必定含有奇數個元素。

(a) NP 完全性證明

屬於 NP

證明者提供一個子集合 T⊆ST\subseteq S 作為憑證。可在多項式時間內計算 sum⁡(T)\operatorname{sum}(T)、確認它等於 kk,並確認 ∣T∣|T| 為奇數。因此 ODDNUMBERSUM 屬於 NP。

NP 困難

由 SUBSET SUM 歸約。給定一個正整數集合 {a1,…,an}\{a_1,\ldots,a_n\} 與目標 t>0t>0,問題是是否存在子集合,其和為 tt。

若 nn 為偶數,加入一個數 t+1t+1;這個數大於目標,不能出現在任何解中。如此可令 nn 為奇數而不改變答案。以下仍以 nn 表示調整後的元素數量,並令

R=2n+1,M=3n,C=∑i=0n−13i.R=2n+1,\qquad M=3^n,\qquad C=\sum_{i=0}^{n-1}3^i.

對每個 aia_i 建立兩個數:

xi=1+2R(Mai+3i−1),yi=1+2R⋅3i−1.x_i=1+2R(Ma_i+3^{i-1}),\qquad y_i=1+2R\cdot 3^{i-1}.

令新集合與目標為

S′={xi,yi:1≤i≤n},k′=n+2R(Mt+C).S'=\{x_i,y_i:1\le i\le n\},\qquad k'=n+2R(Mt+C).

每個 S′S' 中的數都是正奇數,且因 nn 為奇數,k′k' 也是奇數。這些數彼此不同:同一組中的 xi≠yix_i\ne y_i;不同索引的 3i−13^{i-1} 編碼可區分同類數字,而 MM 大於所有編碼項之和,也能區分 xix_i 的原始數值部分。

若 S′S' 中某子集合的和為 k′k',設它選了 qq 個數。每個數都可寫成 11 加上 2R2R 的倍數;又 q≤2n<2Rq\le 2n<2R 且 n<2Rn<2R,比較兩邊除以 2R2R 的餘數可得 q=nq=n。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題

114 年臺灣大學的其他科目

臺灣大學《資料結構與演算法》其他年度