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(log n), *: O(n log n)
(B) !: O(log n), *: O(n^2)
(C) !: O(n), *: O(n log n)
(D) !: O(n), *: O(n^2)
(E) !: O(n log n), *: O(n^2)

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

這一題的完整詳解

核心觀念

本題考查隨機演算法的複雜度與期望值分析(Analysis of Randomized Algorithms)、遞迴樹分析(Recursion Tree Analysis)以及類似於隨機快速排序(Randomized Quicksort)的遞迴關係式求解。


詳細推導

演算法輸入為大小為 nn 的陣列 AA。

1. 印出驚嘆號 ! 的期望次數

觀察基本終止條件(Base Case):

  • 當子陣列長度 m≤1m \le 1 時,演算法印出一個 ! 並直接返回。
  • 當子陣列長度 m≥2m \ge 2 時,選取 p∈{1,2,…,m−1}p \in \{1, 2, \dots, m-1\},將陣列切分為兩個長度分別為 p≥1p \ge 1 與 m−p≥1m-p \ge 1 的非空子陣列。

遞迴樹與葉節點分析:

  • 由於每次分割皆產生非空子陣列,且元素的總數守恆(p+(m−p)=mp + (m-p) = m),遞迴分割會持續進行直到每個子陣列的長度降為 11。
  • 這對應到一棵二元樹:每個大小 m≥2m \ge 2 的呼叫為內部節點,每個大小為 11 的呼叫為葉節點。
  • 初始陣列長度為 nn,最終必然被切分為恰好 nn 個長度為 11 的子陣列(即遞迴樹有 nn 個葉節點)。
  • 每個葉節點觸發一次 if n <= 1: 並印出一個 !。
  • 因此,不論隨機分割點 pp 如何選取,印出 ! 的總次數始終恆等於 nn。

遞迴關係式驗證:
設 E[!(n)]E[!(n)] 為大小為 nn 時印出 ! 的期望次數:

  • E[!(1)]=1E[!(1)] = 1
  • 對於 n≥2n \ge 2:
    E[!(n)]=1n−1∑p=1n−1(E[!(p)]+E[!(n−p)])=2n−1∑p=1n−1E[!(p)]E[!(n)] = \frac{1}{n-1} \sum_{p=1}^{n-1} \bigl(E[!(p)] + E[!(n-p)]\bigr) = \frac{2}{n-1} \sum_{p=1}^{n-1} E[!(p)]
  • 由數學歸納法,假設對所有 k<nk < n 均有 E[!(k)]=kE[!(k)] = k,則:
    E[!(n)]=2n−1∑p=1n−1p=2n−1⋅(n−1)n2=nE[!(n)] = \frac{2}{n-1} \sum_{p=1}^{n-1} p = \frac{2}{n-1} \cdot \frac{(n-1)n}{2} = n
  • 故 E[!(n)]=n=Θ(n)E[!(n)] = n = \Theta(n),其緊緻漸近上界為 O(n)O(n)。

2. 印出星號 * 的期望次數

設 T(n)T(n) 為大小為 nn 時印出 * 的期望次數:

  • 當 n≤1n \le 1 時,演算法在印出 ! 後直接返回,不印出任何 *,故 T(1)=0T(1) = 0。
  • 當 n≥2n \ge 2 時,迴圈 for i in {0, ..., n-1} 會印出 nn 個 *,接著均勻隨機在 {1,…,n−1}\{1, \dots, n-1\} 中挑選 pp,遞迴進入兩側子陣列。

因此遞迴關係式為:
T(n)=n+1n−1∑p=1n−1(T(p)+T(n−p))=n+2n−1∑p=1n−1T(p)T(n) = n + \frac{1}{n-1} \sum_{p=1}^{n-1} \bigl(T(p) + T(n-p)\bigr) = n + \frac{2}{n-1} \sum_{p=1}^{n-1} T(p)

此式與隨機快速排序(Randomized Quicksort)平均比較次數的遞迴關係式完全相同,求解如下:

  1. 兩邊同乘以 n−1n-1:
    (n−1)T(n)=n(n−1)+2∑p=1n−1T(p)(n-1) T(n) = n(n-1) + 2 \sum_{p=1}^{n-1} T(p)
  2. 將 nn 替換為 n−1n-1:
    (n−2)T(n−1)=(n−1)(n−2)+2∑p=1n−2T(p)(n-2) T(n-1) = (n-1)(n-2) + 2 \sum_{p=1}^{n-2} T(p)
  3. 兩式相減消除求和項:
🔒

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

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

免費註冊

第 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 是方程式的實數解,不要求為整數。

因此,符合定理條件的是 (B)。

選項分析

(A) 錯誤。 Akra–Bazzi 定理不要求所有子問題大小相同。例如 T(n)=T(n/2)+T(n/3)+g(n)T(n)=T(n/2)+T(n/3)+g(n) 中,兩個子問題大小不同,仍可使用該定理分析。

(B) 正確。 定理要求 g(n)g(n) 符合特定的正則性與多項式成長條件,才能套用其漸近估計公式。

🔒

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

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

免費註冊

第 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,因為只有最後一個字元不同:

dH(ATCGTA,ATCGTT)=1d_H(\text{ATCGTA},\text{ATCGTT})=1

距離近不等於必定分在同一群。群集歸屬還取決於其他序列、群集中心與初始化;演算法沒有「距離最近的兩個資料點必須同群」的保證。

(C) 錯誤。 kk-means 透過反覆分配資料點與更新中心來降低群內平方距離,通常收斂到局部最佳解。結果可能受初始中心影響,因此不保證得到全域最佳分群。

🔒

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

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

免費註冊

第 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) 正確:
S(i,j)=max⁡{S(i−1,j−1)+match(A[i],B[j]),S(i−1,j)−2,S(i,j−1)−2S(i,j)=\max\begin{cases} S(i-1,j-1)+\text{match}(A[i],B[j]),\\ S(i-1,j)-2,\\ S(i,j-1)-2 \end{cases}
🔒

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

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

免費註冊

第 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)

依數值遞減排序,並以較小標籤打破平手,順序為:

5, 10, 11, 3, 9, 4, 6, 7, 8, 12, 13, 1, 25,\ 10,\ 11,\ 3,\ 9,\ 4,\ 6,\ 7,\ 8,\ 12,\ 13,\ 1,\ 2

選項分析

  • (A) 錯。第二輪中頂點 66 的值是 1111,低於頂點 1010 的 1717,不應排在頂點 1010 前面。
  • (B) 錯。
🔒

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

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

免費註冊

第 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
最大價值 dp[w]dp[w]121415161820212224

容量 1717 時,取兩個 CC 和一個 AA:

7+7+3=17,10+10+4=247+7+3=17,\qquad 10+10+4=24
🔒

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

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

免費註冊

第 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

因此最大流至多為 12+13=2512+13=25。

此上限可以達成:將 D 參與的 12 場比賽全部分配給 D;其餘比賽可分配為 AB 對戰中 A 得 1 勝、B 得 4 勝,AC 對戰中 C 得 6 勝,BC 對戰中 B、C 各得 1 勝。這樣 A、B、C 分別流入 1、5、7 單位,總流量為

12+1+5+7=2512+1+5+7=25
🔒

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

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

免費註冊

第 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} 的成長快於任何固定次方的多項式乘對數因子。

選項分析

  • (A) (f1,f5)(f_1,f_5):正確。
    f5(n)f1(n)=n4(log⁡n)2→∞\dfrac{f_5(n)}{f_1(n)}=n^4(\log n)^2\to\infty,所以 f5f_5 成長快於 f1f_1。

  • (B) (f2,f5)(f_2,f_5):正確。
    在 log⁡2n\log_2 n 的慣例下,f2(n)=nf_2(n)=n。因此
    f5(n)f2(n)=n4(log⁡n)2→∞\dfrac{f_5(n)}{f_2(n)}=n^4(\log n)^2\to\infty,f5f_5 成長快於 f2f_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}。反覆展開遞迴式:

T(n)=2n2n−1⋯2c+1T(c)=2∑i=c+1niT(c).T(n)=2^n2^{n-1}\cdots 2^{c+1}T(c) =2^{\sum_{i=c+1}^{n}i}T(c).

因 cc 是固定常數,指數可寫成

∑i=c+1ni=n(n+1)−c(c+1)2.\sum_{i=c+1}^{n}i =\frac{n(n+1)-c(c+1)}{2}.

因此

T(n)=Θ(2n(n+1)/2)=Θ((2)n2+n).T(n)=\Theta\left(2^{n(n+1)/2}\right) =\Theta\left((\sqrt{2})^{n^2+n}\right).

選項正確。

(E) T(n)=T(n/6)+T(7n/9)+O(n)T(n)=T(n/6)+T(7n/9)+O(n)

每次分割後,兩個子問題的規模總和為

n6+7n9=17n18<n.\frac{n}{6}+\frac{7n}{9} =\frac{17n}{18} <n.

以遞迴樹觀察,第 jj 層所有子問題規模的總和至多為

🔒

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

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

免費註冊

第 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.

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

這一題的完整詳解

核心觀念

本題考查二元樹(Binary Tree)​與二元搜尋樹(Binary Search Tree, BST)​的基本定義、樹高性質、節點搜尋(前驅與後繼)、由排序陣列線性時間建構平衡 BST,以及在樹結構上執行廣度優先搜尋(Breadth-First Search, BFS)​的時間複雜度分析。

  1. 二元樹 vs. 二元搜尋樹:
    • 二元樹(Binary Tree)僅限制每個節點至多擁有兩個子節點(左子節點與右子節點)。
    • 二元搜尋樹(BST)在此基礎上需額外滿足「二元搜尋性質」(Binary-Search-Tree Property):對任意節點 xx,其左子樹所有節點之鍵值均小於 xx 之鍵值,其右子樹所有節點之鍵值均大於 xx 之鍵值(若鍵值皆相異)。
  2. BST 之樹高與搜尋代價:
    • 一般 BST 的樹高 hh 取決於元素的插入順序。在最壞情況下(如依排序序列連續插入),樹結構會退化成單一鏈狀結構,樹高可達 h=Θ(n)h = \Theta(n)。
    • 在一般 BST 中尋找節點的前驅(Predecessor)或後繼(Successor),需向下尋找子樹極值或沿父指標向上回溯,最差情況時間複雜度為 O(h)=O(n)O(h) = O(n),而非 O(1)O(1)。
  3. 由已排序序列建構平衡 BST:
    • 給定長度為 NN 的已排序陣列,可透過遞迴取中位數作為子樹根節點的分治法,在 O(N)O(N) 線性時間內建構出樹高為 O(log⁡N)O(\log N) 的高度平衡二元搜尋樹。
  4. 樹結構上的 BFS 複雜度:
    • 含有 nn 個節點的樹具備 n−1n-1 條邊。在無權重樹上執行 BFS,其走訪時間為 O(∣V∣+∣E∣)=O(n)O(|V| + |E|) = O(n);搭配父指標回溯從根節點到目標節點的路徑僅需外加樹高時間 O(log⁡n)O(\log n),總時間仍為 O(n)O(n),不可能需要 Ω(nlg⁡n)\Omega(n \lg n) 時間。

選項分析

  • (A) 錯誤。
    二元樹僅定義每個節點至多有兩個子節點,節點的鍵值並未限定任何大小順序關係;而二元搜尋樹必須符合二元搜尋性質(左子樹所有鍵值 << 根節點鍵值 << 右子樹所有鍵值)。例如:一個根節點值為 1010、其左子節點值為 2020 的二元樹,屬於合法的二元樹,但違反二元搜尋性質,因此不是二元搜尋樹。

  • (B) 錯誤。
    若依序將已排序序列(如 1,2,3,…,n1, 2, 3, \dots, n)插入一棵空的二元搜尋樹,樹會退化為單一直鏈(Skewed Tree),此時樹高為 h=n−1=Θ(n)h = n - 1 = \Theta(n)。只有在具備自我平衡機制的平衡二元搜尋樹(如 AVL 樹、紅黑樹)中,樹高才保證為 O(log⁡n)O(\log n)。

  • (C) 錯誤。
    在二元搜尋樹中尋找相鄰元素:

    • 若題意指「前驅節點」(In-order Predecessor,即小於給定元素的最大者):若該節點有左子樹,則前驅為左子樹的最右節點(最大值);若無左子樹,則需沿父指標向上回溯,直到找到第一個為其父節點之右小孩的祖先。
    • 若題意指「後繼節點」(In-order Successor,即大於給定元素的最小者):若該節點有右子樹,則後繼為右子樹的最左節點(最小值);若無右子樹,則需沿父指標向上回溯,直到找到第一個為其父節點之左小孩的祖先。
      無論是前驅或後繼,在一般未加入線索(Threaded)指標的 BST 中,走訪的最壞情況時間皆取決於樹高 hh。當樹退化時 h=Θ(n)h = \Theta(n),即使為平衡樹亦需 O(log⁡n)O(\log n),不可能在 O(1)O(1) 常數時間內完成。
  • (D) 正確。

    1. 多路合併:三個長度各為 nn 的已排序陣列 A1,A2,A3A_1, A_2, A_3 共有 3n3n 個相異實數。可透過兩兩二路合併(Two-Way Merge)或使用大小為 3 的最小堆積(Min-Heap)進行三路合併,在 O(3n)=O(n)O(3n) = O(n) 時間內將三者合併為單一已排序陣列 AA。
🔒

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

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

免費註冊

第 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

這些鍵互不相同,且最大鍵小於 k4=n2k^4=n^2,符合題目範圍。任取兩鍵,其差為 rk2rk^2,其中 r≠0r\ne 0。若隨機選到的 aa 是 kk 的倍數,則 a(rk2)a(rk^2) 可被 k3=mk^3=m 整除,該物件對必定碰撞。此事件的機率是 1/k1/k,因此

E[T]≥(n2)1k=Θ(k3)=Θ(n1.5)\mathbb{E}[T]\ge \binom{n}{2}\frac{1}{k} =\Theta(k^3) =\Theta(n^{1.5})

這不是 O(n)O(n),故選項錯誤。

(D) 錯誤。 取 n=k2n=k^2,其中 kk 是滿足 k≡1(mod4)k\equiv 1\pmod 4 的質數,則 m=k3m=k^3 且 m≡1(mod4)m\equiv 1\pmod 4。令

🔒

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

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

免費註冊

第 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。故 ee 不會只屬於 T1T_1 或只屬於 T2T_2,也就不會在 FF 中。

(D) 正確。 延用 (A) 的 K4K_4 範例,所有邊權皆為 11。此時 FF 有 44 條邊,所以 w(F)=4w(F)=4;而 T1T_1 有 33 條邊,所以 w(T1)=3w(T_1)=3。因此 w(F)>w(T1)w(F)>w(T_1)。

(E) 錯誤。 此問題可在多項式時間內求解,因此在假設 P≠NPP\ne NP 下,不是 NP-hard。

🔒

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

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

免費註冊

第 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] 原值取最小值,可能把較好的候選距離覆寫成較差的距離。

例如,邊權為 w(s,u)=1w(s,u)=1、w(u,y)=4w(u,y)=4、w(s,v)=2w(s,v)=2、w(v,y)=7w(v,y)=7。處理 uu 後會得到 A[y]=5A[y]=5;之後處理 vv,選項中的更新式會把 A[y]A[y] 覆寫成 99,但真正最短距離仍是 55。正確更新式必須包含 min⁡\min。

(B) 錯誤。
瓶頸路徑的正確更新應為

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

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

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

免費註冊

第 14 題5 分

  1. (5 points) Let GG be an n×nn \times n grid graph, with each undirected edge having a certain weight. Specifically, the vertices of GG can be labeled as (i,j)(i, j) where 1≤i,j≤n1 \le i, j \le n. There is an edge between (i,j)(i, j) and (k,ℓ)(k, \ell) if and only if ∣i−k∣+∣j−ℓ∣=1|i - k| + |j - \ell| = 1. Suppose we want to compute the value of a minimum cut that separates (1,1)(1, 1) and (n,n)(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 2n2n edges. Since there are 2n(n−1)≤2n22n(n - 1) \le 2n^2 edges in GG, we can design a brute force algorithm that enumerates at most ∑k=12n(2n2k)\sum_{k=1}^{2n} \binom{2n^2}{k} subsets of edges and check: if the removal of those edges separates (1,1)(1, 1) and (n,n)(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)(n, 1) to (1,n)(1, n). In other words, those edges form a path of exactly 2n−22n - 2 edges from (n,1)(n, 1) to (1,n)(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)(1, 1) be the source and (n,n)(n, n) be the sink. For each undirected edge connecting (i,j)(i, j) and (k,ℓ)(k, \ell), 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)(1, 1) to (n,n)(n, n) can be computed in O(n6)O(n^6). 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)(1, 1) and (n,n)(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}WS := \{(i, 1) \mid 1 < i \le n\} \cup \{(n, j) \mid 1 \le j < n\} to the north-east border NE:={(i,n)∣1≤i<n}∪{(1,j)∣1<j≤n}NE := \{(i, n) \mid 1 \le i < n\} \cup \{(1, j) \mid 1 < j \le n\}. Thus, we can run the Bellman-Ford algorithm that seeks for any shortest path from a vertex in WSWS to any vertex in NENE. Finally, the length of the shortest path is the value of the minimum cut.
(E) None of the above choices.

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

這一題的完整詳解

核心觀念

本題考查在 n×nn \times n 無向格狀圖(Grid Graph)上,求分離源點 (1,1)(1,1) 與匯點 (n,n)(n,n) 的最小 ss-tt 割(Minimum ss-tt Cut)​之幾何拓撲結構、最大流–最小割定理(Max-Flow Min-Cut Theorem)​與 Dinitz(Dinic)演算法之複雜度,以及平面圖最小割與對偶圖(Dual Graph)​最短路徑的對應關係。


選項詳細分析

(A) 錯誤

  • 命題分析:Student A 宣稱「必存在某個最小割其邊數至多為 2n2n」,並藉此枚舉所有大小至多為 2n2n 的邊子集來尋找最小割。此敘述在一般非負權重下不成立。
  • 反例說明:
    • 在 n×nn \times n 格狀圖中,最小割的邊數上限並不受 2n2n 所限制。
    • 我們可以設定圖中的邊權重,使得唯一的最小割必須橫截超過 2n2n 條邊:例如在格狀圖中設計一條深度蜿蜒折返的蛇形(snake-like)割線,該割跨越了 Θ(n2)\Theta(n^2) 條邊,並將這些橫截邊的權重皆設為 00;其餘所有原圖邊的權重皆設為極大值 M>0M > 0。
    • 由於該蛇形割的總權重為 00,其為全圖唯一的最小割。然而,受限於網格拓撲結構,任何邊數 ≤2n\le 2n 的割都無法完整沿著該蛇形路線穿過圖,必會切到至少一條權重為 MM 的邊,其總權重至少為 M>0M > 0。
    • 因此,唯一的最小割可能包含 Θ(n2)\Theta(n^2) 條邊。僅枚舉邊數至多為 2n2n 的邊子集無法保證涵蓋最小割。

(B) 錯誤

  • 命題分析:Student B 認為「最小割的跨越邊必須形成從 (n,1)(n, 1) 到 (1,n)(1, n) 且恰由 2n−22n-2 條邊組成的單調路徑」,並試圖以動態規劃求解。
  • 錯誤原因:
    1. 割邊不構成原圖頂點路徑:割邊(crossing edges)是從圖中移除後能分離 (1,1)(1,1) 與 (n,n)(n,n) 的邊集合。例如,僅切除與源點 (1,1)(1,1) 相鄰的兩條邊 {((1,1),(1,2)),((1,1),(2,1))}\{((1,1),(1,2)), ((1,1),(2,1))\} 即可分離 (1,1)(1,1) 與 (n,n)(n,n),這兩條邊根本未接觸 (n,1)(n,1) 或 (1,n)(1,n),在原圖中完全不構成連接 (n,1)(n,1) 到 (1,n)(1,n) 的路徑。
    2. 最小割走向非單調且邊數非固定:如 (A) 所述,在非均勻權重下,最小割所對應的幾何分割線可以折返蜿蜒,跨越邊數並非固定的 2n−22n-2 條,亦不具備單調性,無法利用沿單調路徑的動態規劃求解。

(C) 正確

  • 流網絡建構與等價性:
    • 設原無向圖為 G=(V,E)G=(V, E),邊權重 w(e)≥0w(e) \ge 0。建構有向流網絡 G′=(V,E′)G'=(V, E'):
      • 令 (1,1)(1,1) 為源點 ss、(n,n)(n,n) 為匯點 tt。
      • 對原圖中每條連接 (i,j)(i, j) 與 (k,ℓ)(k, \ell) 的無向邊,在 G′G' 中建立方向相反的兩條有向邊 ((i,j),(k,ℓ))((i, j), (k, \ell)) 與 ((k,ℓ),(i,j))((k, \ell), (i, j)),容量皆設為該邊的權重 ww。
    • 在有向網絡 G′G' 中,任一 ss-tt 點割 (S,T)(S, T)(其中 s∈S,t∈Ts \in S, t \in T)的容量定義為從 SS 指向 TT 的有向邊容量和:
      c(S,T)=∑u∈S,v∈T,(u,v)∈E′c(u,v)c(S, T) = \sum_{u \in S, v \in T, (u, v) \in E'} c(u, v)
      若原無向邊 {u,v}\{u, v\} 跨越割(即 u∈S,v∈Tu \in S, v \in T),則正向邊 (u,v)(u, v) 指向 TT 並貢獻容量 ww,而反向邊 (v,u)(v, u) 由 TT 指向 SS,不計入割容量中。
    • 因此,c(S,T)c(S, T) 嚴格等於原無向圖中跨越該割的無向邊權重總和。
🔒

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

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

免費註冊

第 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),因此公式不完整。

例如,根 xx 的左子節點 ll 與 xx 同色;ll 的左子節點 qq 與 xx 異色,而 qq 的左子節點 ss 與 qq 同色。另設根的右子樹非空且與根異色。令 w(x)=w(l)=100w(x)=w(l)=100、w(q)=w(s)=1w(q)=w(s)=1。T′T' 只含 x,lx,l,其最小權重頂點覆蓋值為 100100;但 qq 所在子樹仍有同色邊 q ⁣− ⁣sq\!-\!s,還需額外權重 11。題目寫的 Leaf⁡(T)\operatorname{Leaf}(T) 求和只會取原樹葉節點下方的空子樹,漏掉這個成本。

就算把求和改成 Leaf⁡(T′)\operatorname{Leaf}(T') 也不對:若根的右子節點與根異色、但與它自己的子節點同色,這棵右子樹接在 T′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.

取 (x1,x2,x3,x4)=(89,0,89,89)(x_1,x_2,x_3,x_4)=(\frac89,0,\frac89,\frac89) 時,三條限制式皆取等號,且目標值為 88。因此

OPTLP=8,γ=OPTLPOPTIP=87.\mathrm{OPT}_{LP}=8,\qquad \gamma=\frac{\mathrm{OPT}_{LP}}{\mathrm{OPT}_{IP}}=\frac87.

上述上界要取等號,必須 x2=0x_2=0,且三條限制式都取等號;聯立後唯一解為 (89,0,89,89)(\frac89,0,\frac89,\frac89)。所以鬆弛問題的最優解集合只有一個點,其三維體積為 00。

3. 判斷四維體積

🔒

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

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

免費註冊

第 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.

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

這一題的完整詳解

核心觀念

  1. 多項式時間歸約(Polynomial-time Many-one Reduction, Karp Reduction):

    • 符號 X≤pYX \le_p Y 表示決策問題 XX 可在多項式時間內歸約為決策問題 YY。即存在可在多項式時間內計算的函數 ff,使得對所有輸入字串 ww,均滿足 w∈X  ⟺  f(w)∈Yw \in X \iff f(w) \in Y。
    • 直觀意義:YY 的計算難度至少與 XX 相當(XX 不比 YY 難)。
    • 傳遞性(Transitivity):若 X≤pYX \le_p Y 且 Y≤pZY \le_p Z,則 X≤pZX \le_p Z。
  2. 複雜度類別在 ≤p\le_p 下的封閉性(Closure Properties):

    • P\text{P} 的封閉性:若 X≤pYX \le_p Y 且 Y∈PY \in \text{P},則 X∈PX \in \text{P}。
    • NP\text{NP} 的封閉性:若 X≤pYX \le_p Y 且 Y∈NPY \in \text{NP},則 X∈NPX \in \text{NP}。
      證明概要:因為 Y∈NPY \in \text{NP},存在非決定性多項式時間圖靈機 MYM_Y 可判定 YY。若要判定輸入 ww 是否屬於 XX,只需先以多項式時間計算 f(w)f(w),再將 f(w)f(w) 送入 MYM_Y 進行非決定性多項式時間驗證。複合演算法整體仍屬於非決定性多項式時間,故 X∈NPX \in \text{NP}。
  3. NP-hard 與 NP-complete 的定義:

    • NP-hard:決策問題 HH 滿足對所有 L∈NPL \in \text{NP},皆有 L≤pHL \le_p H。
    • NP-hardness 的傳播:若 HH 為 NP-hard 且 H≤pYH \le_p Y,則由傳遞性可知對任意 L∈NPL \in \text{NP} 均有 L≤pYL \le_p Y,故 YY 亦為 NP-hard。
    • NP-complete (NPC):決策問題 CC 同時滿足 (1) C∈NPC \in \text{NP} 且 (2) CC 為 NP-hard。
    • 複雜度階層坍縮定理:若存在任一問題 HH 滿足 HH 為 NP-hard 且 H∈PH \in \text{P},則所有 L∈NPL \in \text{NP} 均可藉由 L≤pHL \le_p H 於多項式時間內求解,因此 NP⊆P\text{NP} \subseteq \text{P},即導出 P=NP\text{P} = \text{NP}。

題目條件整理

  • 決策問題之間的歸約關係:
    1. A≤pBA \le_p B
    2. B≤pCB \le_p C
    3. C≤pDC \le_p D
    4. C≤pEC \le_p E
  • 預設複雜度類別隸屬性:
    • A∈PA \in \text{P}
    • B∈NPB \in \text{NP}

選項詳細分析

  • (A) 正確:
    若假設 AA 為 NP-hard:

    1. 已知題幹給定 A∈PA \in \text{P}。由 NP-hard 之定義,對所有 L∈NPL \in \text{NP} 均有 L≤pAL \le_p A。
    2. 因 A∈PA \in \text{P} 且 P\text{P} 在多項式時間歸約下具封閉性,所有 L∈NPL \in \text{NP} 均可在多項式時間內被判定,故 NP⊆P\text{NP} \subseteq \text{P},由此推得 P=NP\text{P} = \text{NP}。
    3. 題幹已知 B∈NPB \in \text{NP},既然 P=NP\text{P} = \text{NP},則所有屬於 NP\text{NP} 的問題皆屬於 P\text{P},故必然有 B∈PB \in \text{P}。
      因此,「若 AA 為 NP-hard,則 B∈PB \in \text{P}」為真命題。
  • (B) 正確:
    若假設 BB 為 NP-hard 且 E≤pBE \le_p B:

    1. 證明 CC 為 NP-complete:
      • CC 為 NP-hard:已知 BB 為 NP-hard,且題幹給定 B≤pCB \le_p C。由 NP-hardness 之傳播性,立即推得 CC 亦為 NP-hard。
      • C∈NPC \in \text{NP}:題幹給定 C≤pEC \le_p E,結合假設條件 E≤pBE \le_p B,由歸約的傳遞性可得 C≤pBC \le_p B。又已知 B∈NPB \in \text{NP},且 NP\text{NP} 在多項式時間歸約下具封閉性,故 C∈NPC \in \text{NP}。
      • 綜合上述兩點,C∈NPC \in \text{NP} 且 CC 為 NP-hard,故 CC 為 NP-complete。
    2. 證明 DD 為 NP-hard:
🔒

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

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

免費註冊

第 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].

也就是每一個值為 11 的位元貢獻 343\frac{34}{3} 單位勢能。此函數可直接由陣列中 11 的個數計算,且恆為非負;全零初始狀態的勢能為 00。

本次操作清除 tt 個 11,並新增一個 11,所以勢能變化為

ΔΦ=Φi−Φi−1=343(1−t).\Delta\Phi =\Phi_i-\Phi_{i-1} =\frac{34}{3}(1-t).

因此攤銷成本滿足

🔒

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

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

免費註冊

第 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。再除去這 nn 個單位項,得到

MA+D=Mt+C,MA+D=Mt+C,

其中 AA 是所選 xix_i 對應的 aia_i 之和,而 DD 是所選數字的 3i−13^{i-1} 編碼總和。編碼係數至多為 22,所以 0≤D≤2C<M0\le D\le 2C<M;又 C<MC<M。因此等式迫使 A=tA=t 且 D=CD=C。

在三進位表示下,CC 的每一位係數都是 11,而 D=CD=C 表示每一組 {xi,yi}\{x_i,y_i\} 恰好選一個。選到 xix_i 代表將 aia_i 納入原問題的子集合;故原問題有和為 tt 的子集合時,可選相應的 xix_i,其餘各組選 yiy_i,得到和為 k′k' 的子集合。

🔒

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

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

免費註冊

其他考古題