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

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

第 1 題5 分

Consider the following variation of the Selection Problem: Given a sequence S=(a1,a2,…,an)S = (a_1, a_2, \dots, a_n) of nn distinct integers and two positive integers xx and yy, where each number in SS falls between 1 and yy, the goal is to report the smallest xx numbers in SS in ascending order. This scenario assumes the conventional single-processor, random access machine (RAM) model, with a machine word size of at least ⌈log⁡y⌉\lceil \log y \rceil bits. To tackle this problem, five algorithms have been proposed. Please select the correct description(s) below.

A. Algorithm A: The algorithm initializes an empty max-heap and sequentially inserts numbers from SS. During insertion, if the size of the max-heap exceeds xx, it removes the maximum element from the heap. Finally, the remaining numbers in the heap are sorted by heapsort and reported. If x≤n/log⁡nx \le n/\log n, Algorithm A runs in O(n)O(n) time in the worst case.
B. Algorithm B: The algorithm initially constructs a min-heap and proceeds to extract the minimum element xx times. If x≤n/log⁡nx \le n/\log n, Algorithm B runs in O(n)O(n) time in the worst case.
C. Algorithm C: The algorithm begins by performing a base-nn radix sort, followed by reporting the smallest xx values. If y≤n2y \le n^2, Algorithm C runs in O(n)O(n) time in the worst case.
D. Algorithm D: The algorithm performs a counting sort by allocating an array CC of length yy, traversing through SS and incrementing C[ai]C[a_i] for each aia_i. It finally reports the smallest xx values by scanning through the counter array. If y≤n2y \le n^2, Algorithm D runs in O(n)O(n) time in the worst case.
E. Algorithm E: The algorithm first applies quickselect, i.e., Randomized-Select, to determine the xx-th smallest number in SS. Subsequently, it traverses SS for extracting all numbers smaller than or equal to the xx-th smallest number. Finally, these xx numbers are sorted by quicksort and reported. The time complexity of Algorithm E is O(n+xlog⁡x)O(n + x \log x) in expectation.

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

這一題的完整詳解

核心觀念

本題探討在給定 nn 個介於 11 到 yy 之相異整數的序列 S=(a1,a2,…,an)S = (a_1, a_2, \dots, a_n) 中,依序找出並遞增輸出最小的 xx 個整數(x≤nx \le n)的變形選擇問題(Selection Problem)。在標準隨機存取機(RAM)模型下,機器字長(word size)至少為 ⌈log⁡y⌉\lceil \log y \rceil bits,確保數值範圍內的整數均可於單一 word 內儲存,且基礎算術與陣列索引操作均為 O(1)O(1)。
本題針對五種常見排序與選擇演算法,在不同的輸入參數(n,x,yn, x, y)邊界條件下分析其最壞情況或期望時間複雜度:

  1. Max-Heap:維持大小為 xx 的堆積以篩選前 xx 小元素。
  2. Min-Heap:建構包含全部 nn 個元素的堆積後,連續執行 xx 次 Extract-Min。
  3. Radix Sort(以 nn 為基底):數字範圍為 1∼y1 \sim y,在 base-nn 下進行非比較型排序,需執行 d=⌈log⁡ny⌉d = \lceil \log_n y \rceil 次穩定計數排序。
  4. Counting Sort:配置長度為 yy 的計數陣列,直接統計出現次數並循序掃描輸出。
  5. Randomized-Select + Quicksort:以期望線性時間找出第 xx 小元素後進行線性劃分,再以 Quicksort 對 xx 個元素排序。

選項分析

  • 選項 A:錯誤

    • 演算法流程:維護一個最多容納 xx 個元素的 Max-Heap。依序將序列 SS 中的元素插入堆積;每當堆積大小超過 xx 時,便移除最大元素(Extract-Max)。遍歷完全部 nn 個元素後,堆積內恰留存最小的 xx 個元素,最後再使用 Heapsort 將這 xx 個元素排序輸出。
    • 複雜度推導:
      • 插入與調整:對每個數字執行插入與可能的刪除最大值操作,堆積大小始終不超過 x+1x+1,單次操作耗時 O(log⁡x)O(\log x)。處理 nn 個元素共需 O(nlog⁡x)O(n \log x)。
      • 最終排序:對剩餘 xx 個元素進行 Heapsort 耗時 O(xlog⁡x)O(x \log x)。
      • 總最壞情況時間複雜度為 O(nlog⁡x+xlog⁡x)=O(nlog⁡x)O(n \log x + x \log x) = O(n \log x)(因 x≤nx \le n)。
    • 條件驗證:若 x≤n/log⁡nx \le n/\log n,代入總複雜度得: O(nlog⁡x)≤O(nlog⁡(nlog⁡n))=O(n(log⁡n−log⁡log⁡n))=Θ(nlog⁡n)O(n \log x) \le O\left(n \log\left(\frac{n}{\log n}\right)\right) = O(n(\log n - \log\log n)) = \Theta(n \log n) 當 x=Θ(n/log⁡n)x = \Theta(n/\log n) 時,執行時間為 Θ(nlog⁡n)\Theta(n \log n),並非 O(n)O(n)(欲達到 O(n)O(n) 需 x=O(1)x = O(1))。因此選項 A 錯誤。
  • 選項 B:正確

    • 演算法流程:利用由下而上的方式(Build-Min-Heap)將序列 SS 中的所有 nn 個數字建構成一個 Min-Heap,接著連續執行 xx 次 Extract-Min,依序取出並輸出前 xx 小的數值。
    • 複雜度推導:
      • 建構堆積:Build-Min-Heap 的最壞情況時間複雜度為 O(n)O(n)。
      • 提取元素:每次 Extract-Min 需維護堆積性質,耗時 O(log⁡n)O(\log n);連續提取 xx 次共耗時 O(xlog⁡n)O(x \log n)。提取出的數字自然呈遞增順序,可直接輸出。
      • 總最壞情況時間複雜度為 O(n+xlog⁡n)O(n + x \log n)。
    • 條件驗證:若 x≤n/log⁡nx \le n/\log n,代入得: O(n+xlog⁡n)≤O(n+nlog⁡n⋅log⁡n)=O(n+n)=O(n)O(n + x \log n) \le O\left(n + \frac{n}{\log n} \cdot \log n\right) = O(n + n) = O(n) 在該條件下最壞情況執行時間確實為 O(n)O(n)。因此選項 B 正確。
  • 選項 C:正確

    • 演算法流程:以 nn 為基底(base-nn)對序列 SS 執行 Radix Sort,排序完成後直接輸出前 xx 個最小值。
    • 複雜度推導:
      • 序列中的每個數值介於 11 到 yy 之間,以基底 nn 表示時,位數(digits)為 d=⌈log⁡ny⌉d = \lceil \log_n y \rceil。
      • 每一位數使用穩定計數排序(Counting Sort),基底為 nn 且元素個數為 nn,單次回合耗時 O(n+n)=O(n)O(n + n) = O(n)。
      • 整個 Radix Sort 共進行 dd 回合,耗時 O(d⋅n)=O(n⌈log⁡ny⌉)O(d \cdot n) = O(n \lceil \log_n y \rceil)。
🔒

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

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

免費註冊

第 2 題5 分

A Maximum Binary Tree (abbreviated as MaxBT) is defined as a binary tree that adheres to the max-heap property without the necessity of following the complete binary tree property. In a MaxBT, each node pp consists of a value (pp.data) and two links (pp.left and pp.right) pointing to the left and right children, respectively. A link to nil indicates the absence of further children.
The provided pseudocode, although incomplete, implements a recursive function for merging two MaxBTs into a single MaxBT. This function takes the root nodes of the two MaxBTs (root1 and root2) as input and returns the root node of the merged MaxBT.
1: function MergeMaxBinaryTrees(root1, root2)
2: if root1 = nil then return (a) end if
3: if root2 = nil then return (b) end if
4: if root1.data > root2.data then
5: (c) ← MergeMaxBinaryTrees( (d) , (e) )
6: return (f)
7: else
8: (g) ← MergeMaxBinaryTrees( (h) , (i) )
9: return (j)
10: end if
11: end function
Assuming all the blanks in the above function have been appropriately filled to ensure its proper functioning, please select the correct description(s) below.
A. (b) and (f) are the same.
B. (c), (d), and (e) are all distinct.
C. In the cases of (c) and (g), each has only one correct answer.
D. Exchanging the filled content of (h) and (i) leads to a correct implementation.
E. The function works correctly even when the two arguments passed into it refer to the same tree.

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

這一題的完整詳解

核心觀念

  1. 最大二元樹(Maximum Binary Tree, MaxBT)之定義:
    • 滿足最大堆積性質(max-heap property),即任一節點 pp 的鍵值均大於或等於其左右子節點的鍵值(p.data≥p.left.datap.\text{data} \ge p.\text{left.data} 且 p.data≥p.right.datap.\text{data} \ge p.\text{right.data})。
    • 不要求滿足完全二元樹(Complete Binary Tree)性質,因此節點排列無結構平衡或緊湊性要求。
  2. 遞迴合併演算法(MergeMaxBinaryTrees)之核心邏輯:
    • 終止條件(Base Cases):
      • 若其中一棵樹為空(nil),則合併結果直接為另一棵樹。
      • 若 root1 = nil,回傳 root2,即空格 (a) = root2。
      • 若 root2 = nil,回傳 root1,即空格 (b) = root1。
    • 依最大堆積性質進行遞迴合併:
      • 若 root1.data > root2.data:合併後的新樹根節點必為鍵值較大的 root1。將 root2 與 root1 的某一子樹(左或右子樹)遞迴合併,並將結果指派回該子節點欄位:
        (c)←MergeMaxBinaryTrees((d),(e))(c) \leftarrow \text{MergeMaxBinaryTrees}((d), (e))
        其中 (c) 為 root1.right(或 root1.left),傳入引數 (d) 與 (e) 為原該子樹與 root2。最後回傳合併後的根節點 root1,即空格 (f) = root1。
      • 若 root1.data <= root2.data:由 root2 作為新根節點,將 root1 與 root2 的某一子樹遞迴合併:
        (g)←MergeMaxBinaryTrees((h),(i))(g) \leftarrow \text{MergeMaxBinaryTrees}((h), (i))
        其中 (g) 為 root2.right(或 root2.left),最後回傳新根節點 root2,即空格 (j) = root2。

選項詳細分析

  • A. (b) and (f) are the same.(正確)

    • 空格 (b) 是當 root2 = nil 時的回傳值,應填入 root1。
    • 空格 (f) 是當 root1.data > root2.data 時回傳的新樹根節點,應填入 root1。
    • 兩者皆為 root1,完全相同,故選項 A 為真。
  • B. (c), (d), and (e) are all distinct.(錯誤)

    • 第 5 行為 (c) ← MergeMaxBinaryTrees( (d) , (e) )。
    • (c) 是被賦值的子節點指標(例如 root1.right 或 root1.left),而傳入遞迴函數的引數 (d) 與 (e) 分別為原該子樹(root1.right 或 root1.left)以及另一棵樹 root2。
    • 因此,(d) 或 (e) 必有一者與 (c) 相同(皆為相同的子樹成員表示式),三者並非全部相異(not all distinct),故選項 B 為偽。
  • C. In the cases of (c) and (g), each has only one correct answer.(錯誤)

    • 根據題目定義,MaxBT 僅需遵循 max-heap property,並無完全二元樹限制,亦無二元搜尋樹(BST)之左右鍵值順序限制。
    • 合併時無論將另一棵樹遞迴併入左子樹(即 (c) = root1.left, (g) = root2.left)或右子樹(即 (c) = root1.right, (g) = root2.right),合併後產生的二元樹皆嚴格維持父節點大於等於子節點的性質,皆為合法正確的實作方式。
    • 故 (c) 與 (g) 各自不只一種正確填法,並非「各自只有唯一一個正確答案」,故選項 C 為偽。
🔒

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

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

免費註冊

第 3 題5 分

A Dynamic Array (abbreviated as DArray) is a type of array that permits the insertion and deletion of elements at the end to dynamically adjust its size. DArray D maintains three crucial attributes, including the underlying array D.data, the logical data size D.size, and the actual array capacity D.capacity. The invariant D.size ≤ D.capacity holds at any given moment. The Insert(D, x) operation appends the element x to the end of D.data. In case D.size = D.capacity, it triggers a call to Resize(D) before appending x, where Resize(D) allocates a new and larger array, moves all the data to it, increases the value of D.capacity, sets up D.data, and finally frees the old array. Two implementations of Resize(D) are provided: ResizeA(D) increases D.capacity by a constant (e.g., 10) while ResizeB(D) doubles D.capacity each time.
To initialize DArray D, we set D.size to 0, D.capacity to 1, and allocate a size 1 array to D.data. Assuming that both allocating and freeing arrays of length n take O(n) time, and moving a single element takes O(1) time, please select the correct description(s) below.
A. DArray is not suitable for random access because its memory may be relocated.
B. DArray can only be allocated in the heap memory due to its dynamic memory nature.
C. If ResizeA(D) is considered, the amortized time complexity of Insert(D, x) is O(1).
D. If ResizeB(D) is considered, the amortized time complexity of Insert(D, x) is O(1).
E. In the Delete(D) operation, if D.size > 0, D.size is decreased by 1. If, at this stage, D.size < [D.capacity/2], we create a new array d' with a size of [D.capacity/2], transfer the data to d', set up d' as the new data array for D.data, and subsequently free the old array. Then, the amortized time complexity of mixed Insert and Delete operations can be O(1), where Resize(D) can be either ResizeA(D) or ResizeB(D).

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

這一題的完整詳解

核心觀念

本題考查動態陣列(Dynamic Array)的內部運作機制、記憶體配置方式,以及在不同擴容(Resize)與縮容策略下的攤還時間複雜度(Amortized Time Complexity)。


選項逐一分析

  • A. DArray is not suitable for random access because its memory may be relocated.

    • 分析:錯誤。
    • 動態陣列在擴容搬移資料後,其底層記憶體空間依然是一段連續的記憶體區塊。任何合法索引 ii 的元素位址皆可藉由基底位址加上偏移量在 O(1)O(1) 時間內直接存取,故動態陣列完全支援且適合隨機存取。
  • B. DArray can only be allocated in the heap memory due to its dynamic memory nature.

    • 分析:錯誤。
    • 動態陣列的控制結構物件本身(包含資料指標 D.data、邏輯大小 D.size 與容量 D.capacity)完全可以配置在堆疊(Stack)或靜態全域記憶體中;此外,底層資料緩衝區亦可經由自訂記憶體配置器(Custom Allocator)取自自訂記憶體池或共享記憶體,並非「只能(can only)」配置在堆積(Heap)記憶體。
  • C. If ResizeA(D) is considered, the amortized time complexity of Insert(D, x) is O(1).

    • 分析:錯誤。
    • ResizeA 策略每次僅將容量增加固定常數 CC。從空陣列連續執行 nn 次插入操作時,擴容會發生在大小達到 C,2C,3C,…C, 2C, 3C, \dots 時,複製元素的總開銷為:
∑i=1n/CiC≈C⋅(n/C)22=O(n2)\sum_{i=1}^{n/C} iC \approx C \cdot \frac{(n/C)^2}{2} = O(n^2)

nn 次插入的總時間為 O(n2)O(n^2),因此單次插入的攤還時間複雜度為 O(n2)/n=O(n)O(n^2) / n = O(n),而非 O(1)O(1)。

  • D. If ResizeB(D) is considered, the amortized time complexity of Insert(D, x) is O(1).
    • 分析:正確。
    • ResizeB 策略在容量不足時將容量加倍。連續執行 nn 次插入操作時,擴容複製元素的總成本為幾何級數求和:
1+2+4+⋯+2⌈log⁡2n⌉≤2n=O(n)1 + 2 + 4 + \dots + 2^{\lceil \log_2 n \rceil} \le 2n = O(n)
🔒

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

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

免費註冊

第 4 題10 分

Consider a hash function h that maps numerous keys into an array A, indexed from 0 to n-1. The concern is that excessive collisions arising from the hashed keys can potentially cause inefficiency within the hash table. The following pseudocode, while not complete, implements a function that utilizes the divide-and-conquer strategy to identify the case where strictly more than half of the keys share the same hash value. The function takes the hash h and the subarray A with the index range from low to high as input and returns the majority hash value shared by over ⌊(high - low + 1)/2⌋ keys. If the keys stored in the subarray A[low...high] do not have such a majority hash value, the function returns -1.
1: function FindMajorityHashValue(h, A, low, high)
2: if low = high then return h(A[low]) end if
3: mid ← ⌊(low + high)/2⌋
4: leftMajority ← FindMajorityHashValue(h, A, low, mid)
5: rightMajority ← FindMajorityHashValue(h, A, mid + 1, high)
6: leftCount ← CountElement(A, low, high, leftMajority)
7: rightCount ← CountElement(A, low, high, rightMajority)
8: [BLANK BOX]
9: end function
In the pseudocode, CountElement(A, low, high, val) calculates and returns the count of keys in the subarray A[low...high] whose hashed values are equal to the given val. Please select the correct description(s) below.
A. (b) and (f) are the same.
B. (c), (d), and (e) are all distinct.

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

這一題的完整詳解

核心觀念

本題考分治法(divide-and-conquer)找「多數雜湊值」(majority element 的變形):

  • 若某個雜湊值在 A[low…high]A[low \dots high] 中出現次數嚴格超過一半,則它至少是左半段或右半段其中之一的多數值。
    理由:若它在兩半都沒有超過各自的一半,則總次數 ≤⌊nL/2⌋+⌊nR/2⌋≤n/2\le \lfloor n_L/2 \rfloor + \lfloor n_R/2 \rfloor \le n/2,不可能超過整段的一半。
  • 所以只要把左右兩半遞迴找出的候選值 leftMajority、rightMajority 拿回整段重新計數,就能判定整段是否有多數值。

解題方法

第 8 行 [BLANK BOX] 應該填入的邏輯(令 n=high−low+1n = high - low + 1):

if leftCount > ⌊n/2⌋ then return leftMajority
else if rightCount > ⌊n/2⌋ then return rightMajority
else return -1

補充兩個細節:

  • 子段沒有多數值時回傳 −1-1。對 −1-1 呼叫 CountElement 會得到 0(不會有雜湊值等於 −1-1),不會誤判,所以第 6、7 行可以直接計數。
  • 時間複雜度:每層合併做兩次線性計數,遞迴式為 T(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + \Theta(n) 由主定理 Case 2 得 T(n)=Θ(nlog⁡n)T(n) = \Theta(n \log n)。

選項分析

題目的兩個選項是在比較程式碼片段 (b)~(f):

🔒

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

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

免費註冊

第 5 題10 分

A Binary Search Tree (abbreviated as BST) is a tree structure comprised of nodes that maintain a specific order among their keys. Given a single node x and two BSTs L and R, in which l.key < x.key < r.key for all nodes l ∈ L and r ∈ R, the objective is to design a Join(L, x, R) function that integrates the given input into a unified BST T with the nodes L ∪ {x} ∪ R. Be aware that AVL and Red-Black trees are specific types of BSTs. Given the constraint that T, L, and R must share the same type, they are required to be either all BSTs, all AVL trees, or all Red-Black trees.
The Join(L, x, R) function is implemented with the steps: (1) selecting a subtree T' from either L or R, where a subtree is defined as potentially being empty, a portion, or the entire tree, (2) detaching T', (3) attaching the tree rooted at x to the former parent of T'. The modified tree is then returned as T. If T' has no former parent, the tree rooted at x is simply returned as T. The following figure demonstrates an example result of Join(L1, x1, R1), where the value represents the key of the node. Inside a circle, the value represents the key of the node.
Please note that almost all steps, except for (1), have been finalized — there could be multiple ways to choose T' in (1). The tree returned from Join(L, x, R) may not be unique. Additionally, unlike AVL or Red-Black trees, Join does not perform re-coloring and re-balancing. Consider the following following arguments for brevity. All nil nodes (considered in red-black trees) are omitted for brevity.
A. If no additional balance is required for L2 and R2, i.e., both L2 and R2 are BSTs, there are 6 possible tree shapes that could be returned from Join(L2, x2, R2).

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 3 頁

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

這一題的完整詳解

核心觀念

Join(L,x,R)Join(L,x,R) 必須維持 BST 的大小順序,因此從 LL 選 T′T' 時,只能沿 LL 的最右路徑選取;從 RR 選時,只能沿 RR 的最左路徑選取,路徑末端的空子樹也可選。AVL 樹要求每個節點左右子樹高度差至多為 11;紅黑樹則要求根與 NIL 葉為黑色、沒有紅紅相鄰,且每條根至 NIL 的路徑黑高相同。

解題方法

圖中 L2L_2 是根節點 22、左右子節點 1、31、3;x2=4x_2=4。R2R_2 以 1010 為根,左子樹根為 77(其左右子樹根為 5、95、9,且 55 的右子為 66、99 的左子為 8);右子樹根為8);右子樹根為 13(其左右子樹根為(其左右子樹根為 11、15,,11的右子為的右子為12,,15的左右子為的左右子為14、16,而,而 16的右子為的右子為17$)。

合法的 T′T' 選擇共有七個:在 L2L_2 可選根 22、根 33,或 33 的空右子樹;在 R2R_2 可選根 1010、根 77、根 55,或 55 的空左子樹。選根 22 與選根 1010 都會得到以 44 為根、L2L_2 與 R2R_2 分居左右的相同形狀,因此七種選擇只形成六種樹形。

選項分析

A. 正確。 七種合法選擇中,選整棵 L2L_2 或整棵 R2R_2 的結果相同,其餘選擇各形成不同樹形,故共有六種。

B. 錯誤。 L2、R2L_2、R_2 都是 AVL 樹,但至少有兩種 Join 結果仍為 AVL 樹。以葉節點高度 00 計,L2L_2 高度為 11,根為 77 的子樹高度為 22,根為 55 的子樹高度為 11,根為 1313 的子樹高度為 33。

  • 選根 77 為 T′T':44 的左右子樹高度為 1、21、2,故 44 的高度為 33;根 1010 的左右子樹高度為 3、33、3。
  • 選根 55 為 T′T':44 的左右子樹高度為 1、11、1,故 44 的高度為 22;根 77 的左右子樹高度為 2、12、1,根 1010 的左右子樹高度則為 3、33、3。

兩種結果都符合 AVL 平衡條件,因此不是唯一一棵。

C. 錯誤。 R2R_2 的紅黑著色由黑高條件唯一決定。節點 6、8、12、176、8、12、17 必須為紅色,其父節點 5、9、11、165、9、11、16 必須為黑色;節點 1414 必須為黑色,節點 1515 必須為紅色,節點 13、7、1013、7、10 必須為黑色。故沒有多種合法著色。

D. 錯誤。 將 NIL 葉的黑高計為 11,R2R_2 的黑高為 44,根為 13、7、5、913、7、5、9 的子樹黑高分別為 3、3、2、23、3、2、2。L2L_2 的根黑高則為 22(兩個葉節點紅色)或 33(兩個葉節點黑色)。

若選根 77 為 T′T',L2L_2 必須取黑高 33 才能和根為 $7

🔒

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

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

免費註冊

第 6 題5 分

We want to multiply a sequence of matrices M1,…,MnM_1, \dots, M_n with the minimum number of multiplications. Since the matrix multiplication is associative, we can parenthesize the matrix multiplication in all possible orders. Let mi,jm_{i,j} be the minimum number of multiplications to multiply Mi,…,MjM_i, \dots, M_j. We can derive a recursion of mi,jm_{i,j}, where rir_i and cic_i are the numbers of rows and columns of the matrix MiM_i respectively.
mi,j=min⁡i≤k<j(mi,k+mk+1,j+rickcj)(1)m_{i,j} = \min_{i \le k < j} (m_{i,k} + m_{k+1,j} + r_i c_k c_j) \qquad (1)
Now consider ms,s+km_{s,s+k}, 1<s<s+k<n1 < s < s+k < n. For given ss and kk, let xx be the number of mi,jm_{i,j}'s that have ms,s+km_{s,s+k} at the right hand side of Equation 1, and let yy be the number of different mm's that are at the right-hand side of Equation 1 when i=si = s and j=s+kj = s+k. What is the approximate value of x+yx + y?
A. s(n−k2)s \binom{n-k}{2}
B. n+kn + k
C. (n−s)(n−s−k)(n-s)(n-s-k)
D. (k2)+(n2)\binom{k}{2} + \binom{n}{2}
E. (n−k−s2)+(k2)\binom{n-k-s}{2} + \binom{k}{2}

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

這一題的完整詳解

核心觀念

本題考查動態規劃(Dynamic Programming)中經典的矩陣鏈乘積問題(Matrix Chain Multiplication, MCM)之子問題相依關係(Subproblem Dependency):

  1. 矩陣鏈乘積之遞迴關係式:
    考慮矩陣序列 M1,M2,…,MnM_1, M_2, \dots, M_n,其中矩陣 MiM_i 維度為 ri×cir_i \times c_i(且 ci=ri+1c_i = r_{i+1})。
    定義 mi,jm_{i,j} 為計算矩陣鏈乘積 Mi…MjM_i \dots M_j 所需之最少純量乘法次數。為避免遞迴式中的分割點變數與題幹給定的常數區間長度 kk 混淆,我們將分割點標記為 pp:

    mi,j=min⁡i≤p<j(mi,p+mp+1,j+ricpcj)m_{i,j} = \min_{i \le p < j} (m_{i,p} + m_{p+1,j} + r_i c_p c_j)

    其中基礎條件為 mi,i=0m_{i,i} = 0。

  2. 子問題 ms,s+km_{s,s+k} 的相依性分析(矩陣鏈跨度為 kk,包含 k+1k+1 個矩陣):

    • xx(入度/被引用的母問題數量):有多少個較大的母問題 mi,jm_{i,j} 之遞迴展開式右側會用到 ms,s+km_{s,s+k}?
      • 作為左子鏈 mi,pm_{i,p}:需滿足 i=si = s 且分割點 p=s+kp = s+k。因分割點滿足 p<j≤np < j \le n,故 j∈{s+k+1,s+k+2,…,n}j \in \{s+k+1, s+k+2, \dots, n\},共有 n−(s+k)n - (s+k) 個可能的 jj。
      • 作為右子鏈 mp+1,jm_{p+1,j}:需滿足 p+1=sp+1 = s(即 p=s−1p = s-1)且 j=s+kj = s+k。因分割點滿足 1≤i≤p=s−11 \le i \le p = s-1,故 i∈{1,2,…,s−1}i \in \{1, 2, \dots, s-1\},共有 s−1s - 1 個可能的 ii。
      • 兩者互斥,故總計: x=[n−(s+k)]+(s−1)=n−k−1x = [n - (s+k)] + (s - 1) = n - k - 1
    • yy(出度/計算該問題所依賴之相異子問題數量):計算特定的 ms,s+km_{s,s+k} 時,其遞迴式右端共包含多少個相異的 mm 項?
      • 分割點 pp 的範圍為 s≤p<s+ks \le p < s+k,共有 (s+k−1)−s+1=k(s+k-1) - s + 1 = k 個不同的分割點。
      • 產生的左子項為 ms,pm_{s,p}(p=s,…,s+k−1p = s, \dots, s+k-1),共 kk 項;
      • 產生的右子項為 mp+1,s+km_{p+1,s+k}(p=s,…,s+k−1p = s, \dots, s+k-1),即 ms+1,s+k,…,ms+k,s+km_{s+1,s+k}, \dots, m_{s+k,s+k},共 kk 項。
      • 左子項起始指標恆為 ss,右子項起始指標恆大於等於 s+1s+1,兩者無交集,故相異項總數為: y=k+k=2ky = k + k = 2k
  3. 求 x+yx + y 的近似值:

    x+y=(n−k−1)+2k=n+k−1≈n+kx + y = (n - k - 1) + 2k = n + k - 1 \approx n + k

詳細推導過程

1. 計算 xx(母問題數量)

在遞迴式 (1) 中,任意母問題 mi,jm_{i,j} 的右側項由兩類子問題構成:mi,pm_{i,p} 與 mp+1,jm_{p+1,j},其中分割點滿足 i≤p<ji \le p < j。
欲使 ms,s+km_{s,s+k} 出現在右側,存在以下兩種情況:

  • 情況一(ms,s+km_{s,s+k} 為左子問題 mi,pm_{i,p}):
    此時指標必須對應:i=si = s 且 p=s+kp = s+k。
    由分割點條件 p<j≤np < j \le n,可知 jj 必須滿足 s+k<j≤ns+k < j \le n。
    因此,jj 的可取值為 s+k+1,s+k+2,…,ns+k+1, s+k+2, \dots, n,共有:

    x1=n−(s+k)x_1 = n - (s + k)

    個母問題 ms,jm_{s,j}。

  • 情況二(ms,s+km_{s,s+k} 為右子問題 mp+1,jm_{p+1,j}):
    此時指標必須對應:p+1=s  ⟹  p=s−1p+1 = s \implies p = s-1 且 j=s+kj = s+k。
    由分割點條件 1≤i≤p1 \le i \le p,可知 ii 必須滿足 1≤i≤s−11 \le i \le s-1。
    因此,ii 的可取值為 1,2,…,s−11, 2, \dots, s-1,共有:

    x2=s−1x_2 = s - 1

    個母問題 mi,s+km_{i,s+k}。

由於情況一的母問題滿足 j>s+kj > s+k 且起始為 ss,情況二的母問題滿足 j=s+kj = s+k 且起始 i<si < s,兩集合之 (i,j)(i,j) 索引完全不重疊。
故右側包含 ms,s+km_{s,s+k} 的母問題總數為:

🔒

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

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

免費註冊

第 7 題5 分

When we compute all m_ij's with Equation 1, we must follow an order of i and j so that the m's on the right-hand side are all known when we compute m_ij. Please select all the correct orders in the following choices.
A. decreasing order of i + j
B. increasing order of i + j
C. increasing order of (i - j)^2
D. decreasing order of (i - j)^3
E. increasing order of |(i + j)(i - j)|

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

這一題的完整詳解

核心觀念

本題考查動態規劃計算表格填表順序(Evaluation Order of DP Table):

  • 矩陣鏈乘法的遞迴式為: mi,j=min⁡i≤k<j(mi,k+mk+1,j+rickcj)(1≤i<j≤n)m_{i,j} = \min_{i \le k < j} (m_{i,k} + m_{k+1,j} + r_i c_k c_j) \quad (1 \le i < j \le n)
  • 正確計算順序之充要條件:在計算 mi,jm_{i,j} 時,其右側所需依賴的所有子問題 mi,km_{i,k} 及 mk+1,jm_{k+1,j}(其中 i≤k<ji \le k < j)必須已經在先前的步驟中被計算完成。
  • 各子問題與母問題的特徵指標比較:
    • 子鏈長度:mi,km_{i,k} 的長度為 k−i+1<j−i+1k - i + 1 < j - i + 1;mk+1,jm_{k+1,j} 的長度為 j−(k+1)+1=j−k<j−i+1j - (k+1) + 1 = j - k < j - i + 1。因此,任何嚴格依「子鏈長度 j−ij - i 單調遞增」的順序皆為合法順序。
    • 題目檢驗的各項指標函數值:
      • A/B:i+ji + j 之遞增或遞減。
      • C:(i−j)2=(j−i)2(i - j)^2 = (j - i)^2。
      • D:(i−j)3=−(j−i)3(i - j)^3 = -(j - i)^3。
      • E:∣(i+j)(i−j)∣=∣i2−j2∣=j2−i2|(i + j)(i - j)| = |i^2 - j^2| = j^2 - i^2(因 j≥ij \ge i)。

解題方法

各選項之嚴格數學檢驗

  • 選項 A:decreasing order of i+ji + j(依 i+ji + j 遞減順序):
    • 不正確。子問題 mi,km_{i,k} 的指標和為 i+ki + k。因 k<jk < j,故 i+k<i+ji + k < i + j。若依 i+ji + j 遞減順序計算,則較大的 i+ji + j 會先被計算,此時較小指標和的子問題 i+ki + k 尚未被計算,違反相依性。
  • 選項 B:increasing order of i+ji + j(依 i+ji + j 遞增順序):
    • 不正確。考慮右側另一個子問題 mk+1,jm_{k+1,j},其指標和為 (k+1)+j(k+1) + j。因 k≥ik \ge i,故 (k+1)+j>i+j(k+1) + j > i + j。這意味著子問題的指標和可能大於母問題,若按 i+ji + j 遞增計算,在算 mi,jm_{i,j} 時 mk+1,jm_{k+1,j} 尚未算好,無法執行。
  • 選項 C:increasing order of (i−j)2(i - j)^2(依 (i−j)2(i - j)^2 遞增順序):
    • 正確。
    • 由於 j≥ij \ge i,定義子鏈長度差 L=j−i≥1L = j - i \ge 1,則 (i−j)2=(j−i)2=L2(i - j)^2 = (j - i)^2 = L^2。
    • 對於任意分割點 kk(i≤k<ji \le k < j):
      • 左子問題 mi,km_{i,k} 的跨度為 L1=k−iL_1 = k - i。因 k<jk < j,故 0≤L1<L0 \le L_1 < L,因此 L12<L2L_1^2 < L^2。
      • 右子問題 mk+1,jm_{k+1,j} 的跨度為 L2=j−(k+1)L_2 = j - (k + 1)。因 k≥ik \ge i,故 0≤L2<L0 \le L_2 < L,因此 L22<L2L_2^2 < L^2。
    • 所有子問題的 (i−j)2(i - j)^2 均嚴格小於母問題的 (i−j)2(i - j)^2!若按 (i−j)2(i - j)^2 遞增順序計算,計算 mi,jm_{i,j} 時其所需的所有子問題必定早已計算完成。
  • 選項 D:decreasing order of (i−j)3(i - j)^3(依 (i−j)3(i - j)^3 遞減順序):
    • 正確。
    • 由於 i≤ji \le j,令 L=j−i≥0L = j - i \ge 0,則 i−j=−L≤0i - j = -L \le 0。
    • 其三次方為 (i−j)3=(−L)3=−L3(i - j)^3 = (-L)^3 = -L^3。
    • 若依 −L3-L^3 的「遞減順序(decreasing order)」計算,即表示 −L3-L^3 從 0 開始變小(0,−1,−8,−27,…0, -1, -8, -27, \dots),這完全等價於鏈長 LL 按照遞增順序(increasing order of LL)計算!
    • 由於所有子問題的長度 L1,L2<LL_1, L_2 < L,其 −L13,−L23>−L3-L_1^3, -L_2^3 > -L^3。因此在遞減順序中,子問題必然先於母問題被處理,該順序完全正確。
🔒

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

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

免費註冊

第 8 題5 分

We define the height of a tree to be the maximum number of edges from the root to a leaf. We also define the level of a node vv, denoted as l(v)l(v) to be the height of the subtree rooted at vv. Which of the following descriptions is correct?
A. If vv is a leaf, then l(v)=1l(v) = 1.
B. If vv is an internal node, l(v)=max⁡s∈Sl(s)l(v) = \max_{s \in S} l(s), where SS is the set of children of vv.
C. We can compute l(v)l(v) by starting a breadth-first-search (BFS) from vv, traversing all nodes of the subtree rooted at vv, and computing the maximum distance to a leaf as l(v)l(v). Since there are no more than nn vv's and each BFS takes O(n)O(n) time, the total time complexity is O(n2)O(n^2).
D. We can compute the levels for all nodes by a depth-first-search (DFS) starting from the root of TT. That is, we can compute the level of an internal node vv after we compute the ll values of all children of vv.
E. Since there are nn tree nodes to visit, the DFS from the root takes O(n)O(n) time.

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

這一題的完整詳解

觀念說明

本題依題幹所給定之特定定義進行推導與判斷:

  1. 樹高(Height of a tree):定義為從根節點(root)到任一葉節點(leaf)的最大邊數(maximum number of edges)。依此定義,僅包含單一節點的樹,其高度為 00。
  2. 節點的 Level(l(v)l(v)):定義為以節點 vv 為根的子樹之高度(height of the subtree rooted at vv)。

選項分析

  • 選項 A 錯誤:
    若 vv 為葉節點(leaf),以 vv 為根的子樹僅包含 vv 本身,從根節點 vv 到葉節點 vv 的邊數為 00。因此 l(v)=0l(v) = 0 而非 11。

  • 選項 B 錯誤:
    若 vv 為內部節點(internal node),以 vv 為根的子樹到葉節點的最長路徑,必然經過 vv 的某個子節點 s∈Ss \in S。從 vv 到子節點 ss 有 11 條邊,而從 ss 到其子樹之葉節點的最長路徑邊數為 l(s)l(s)。因此正確的遞迴關係式應為:
    l(v)=1+max⁡s∈Sl(s)l(v) = 1 + \max_{s \in S} l(s)
    選項中漏掉了從 vv 連接至子節點 ss 的邊數 11。

  • 選項 C 正確:
    對任意節點 vv,從 vv 出發對其子樹執行廣度優先搜尋(BFS),可依邊數由近至遠逐層遍歷子樹中的所有節點。在遍歷過程中記錄從 vv 到各葉節點的距離,取其最大值即為子樹高度 l(v)l(v)。
    子樹節點數至多為 nn,單次 BFS 的時間複雜度為 O(n)O(n)。對樹中至多 nn 個節點各執行一次 BFS,總時間複雜度為 n×O(n)=O(n2)n \times O(n) = O(n^2)。

🔒

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

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

免費註冊

第 9 題5 分

We have a binary tree TT and for every node v∈Tv \in T, we want to compute the new height if we select vv as the new root. We consider the effect of making vv the new root. First, the height of the subtree of vv is still l(v)l(v). Second, since vv is now the new root, it could go up to its parent and then to the farthest leaf. We define this distance as r(v)r(v).
A. The new tree after selecting a new root is always a binary tree.
B. The height of the tree after making vv the new root is max⁡(l(v),r(v))\max(l(v), r(v)).
C. We can compute r(v)r(v) for all tree node vv's in a top-down manner. We assume that the rr value of vv's parent in TT is known, so vv only needs to consider the rr value of its parent and the ll value of its sibling in TT, in order to compute its own rr.
D. The rr value of the root of TT is 11.
E. We can compute the rr values for all nodes in O(n)O(n) time.

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

這一題的完整詳解

核心觀念

本題考查二元樹的性質與「換根動態規劃(Tree Re-rooting DP)」技術。

題目針對二元樹 TT 定義:

  • l(v)l(v):節點 vv 在原樹 TT 中其子樹的高度(即以 vv 為根時向下到其子樹最遠葉節點的距離)。
  • r(v)r(v):節點 vv 往其原父節點方向向上走,在原樹不屬於 vv 之子樹部分所能達到的最遠距離。
  • 將 vv 選為新根時,新樹的高度為 max⁡(l(v),r(v))\max(l(v), r(v))。

各選項詳細分析

  • (A) The new tree after selecting a new root is always a binary tree.

    • 分析:錯誤。
    • 在原樹中,若節點 vv 同時具有左子節點、右子節點與父節點 pp,當將 vv 設為新樹之根時,原本的左子節點、右子節點以及原父節點 pp 都將成為 vv 的子節點,使得 vv 擁有 3 個子節點,違反二元樹每個節點至多 2 個子節點的定義。因此換根後的樹不一定為二元樹。
  • (B) The height of the tree after making v the new root is max(l(v), r(v)).

    • 分析:正確。
    • 將 vv 設為新根後,從 vv 出發到達最遠節點的路徑僅有兩大方向:進入 vv 原本的子樹(最長距離為 l(v)l(v)),或向上經過原父節點進入樹的其他部分(最長距離為 r(v)r(v))。故新樹高度確為 max⁡(l(v),r(v))\max(l(v), r(v))。
  • (C) We can compute r(v) for all tree node v's in a top-down manner. We assume that the r value of v's parent in T is known, so v only needs to consider the r value of its parent and the l value of its sibling in T, in order to compute its own r.

    • 分析:正確。
    • 設節點 vv 的父節點為 pp,兄弟節點為 ss。從 vv 向上走到 pp 距離為 1;
🔒

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

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

免費註冊

第 10 題5 分

Consider a sequence of numbers (v1, ..., vn). Now we remove k numbers from the sequence so that we have k+1 non-empty segments of numbers. For example, consider (2, 3, 8, 1, 4). If we remove 8 then we have two segments (2, 3) and (1, 4). Now we want to minimize the maximum sum of numbers of a segment. Let m(1, n, k) be the answer, then what is the correct recursion for m when 1 ≤ i < j ≤ n and 0 < k ≤ j - i - 1?
A. m(i, j, k) = min_{x=i}^{j-1} max(∑{y=i}^{x} v_y, m(i, x - k - 1))
B. m(i, j, k) = min
{x=i+1}^{j-1} max(∑{y=i}^{x-1} v_y, m(i, x - 1, k - 1))
C. m(i, j, k) = min
{x=i+1}^{j-1} max(m(x + 1, j, k - 1), ∑{y=i}^{x-1} v_y)
D. m(i, j, k) = min
{v+w=k-1, v,w≥0} min_{x=i+1}^{j-1} max(m(x - 1, v), m(x + 1, j, w))
E. m(i, j, k) = min_{v+w=k-1, v,w≥0} min_{x=i}^{j} max(m(i, x, v), m(x, j, w))

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

這一題的完整詳解

核心觀念

本題考查序列分割與動態規劃(Dynamic Programming)遞迴關係式的建構:

  • 給定數列 (v1,…,vn)(v_1, \dots, v_n),從中移除 kk 個數字,將剩餘數列切分為 k+1k+1 個非空區段(Non-empty segments)。
  • 目標:最小化所有區段和的最大值(Min-Max 問題),令 m(i,j,k)m(i, j, k) 表示對子序列 (vi,…,vj)(v_i, \dots, v_j) 移除 kk 個數字所得的最小最大區段和。
  • 動態規劃狀態轉移設計:
    • 考慮第一個(最左邊)被移除的數字,設其在原序列中的下標為 xx(其中 i<x<ji < x < j)。
    • 當 vxv_x 被選為第一個移除的元素時:
      1. 其左側形成的區段為 (vi,…,vx−1)(v_i, \dots, v_{x-1}),該區段內不再移除任何數字,其區段和為 ∑y=ix−1vy\sum_{y=i}^{x-1} v_y;
      2. 其右側剩餘的子序列為 (vx+1,…,vj)(v_{x+1}, \dots, v_j),此右側部分必須繼續移除剩餘的 k−1k-1 個數字,其最佳解即為子問題 m(x+1,j,k−1)m(x+1, j, k-1)。
    • 對於選定的分割點 xx,兩部分之最大區段和為 max⁡(∑y=ix−1vy,m(x+1,j,k−1))\max\left( \sum_{y=i}^{x-1} v_y, m(x+1, j, k-1) \right)。
    • 為了求最佳解,窮舉所有合法的第一個移除點 xx(xx 的取值範圍為 i+1≤x≤j−1i+1 \le x \le j-1),取其最小值。此即精確對應選項 C。

解題方法

各選項遞迴式逐一審視與分析

  • 選項 C 分析: m(i,j,k)=min⁡x=i+1j−1max⁡(m(x+1,j,k−1),∑y=ix−1vy)m(i, j, k) = \min_{x=i+1}^{j-1} \max\left( m(x + 1, j, k - 1), \sum_{y=i}^{x-1} v_y \right)
    • 合法性與正確性證明:
      • 移除 kk 個元素將序列分成 k+1k+1 個非空區段,必定存在一個「最左邊被移除的元素」。設該元素為 vxv_x。
      • 由於產生的每個區段必須為「非空(Non-empty)」,最左邊區段 (vi,…,vx−1)(v_i, \dots, v_{x-1}) 至少需包含一個元素,故 x−1≥i  ⟹  x≥i+1x - 1 \ge i \implies x \ge i + 1。
      • 同理,最右邊區段至少需包含一個元素,故 x≤j−1x \le j - 1。因此 xx 的範圍嚴格為 i+1≤x≤j−1i+1 \le x \le j-1。
      • 既然 vxv_x 是第一個被移除的元素,則左側區段 (vi,…,vx−1)(v_i, \dots, v_{x-1}) 是一個連續完整的區段,其總和固定為 ∑y=ix−1vy\sum_{y=i}^{x-1} v_y。
🔒

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

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

免費註冊

第 11 題5 分

Which of the following descriptions is true for the previous problem?
A. We can always find an optimal solution that removes one of the largest v.
B. We can always find an optimal solution that does not remove one of the smallest v.
C. The initial value for m(i, i, 0) is v_i.
D. The initial value for m(i, j, k) when k > (j - i)/2 is 0.
E. The initial value for m(i, i + 2, 1), 1 ≤ i ≤ n - 2 is max(v_i, v_{i+2}).

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

這一題的完整詳解

核心觀念

本題考查序列分割動態規劃(Dynamic Programming)之邊界條件(Base Cases)與貪婪策略(Greedy Strategy)反例分析。

  • 問題情境:
    給定數列 (v1,…,vn)(v_1, \dots, v_n),欲移除 kk 個元素,將數列分割為 k+1k + 1 個非空區段(non-empty segments),目標是使各區段總和的最大值最小化。令 m(i,j,k)m(i, j, k) 表示子區間 (vi,…,vj)(v_i, \dots, v_j) 移除 kk 個數後的最小最大區段和。
  • 合法狀態的長度限制:
    若要移除 kk 個數並分割出 k+1k + 1 個非空區段,任何被移除的數不能位於端點,且任意兩個被移除的數不能相鄰(每個被移除數的前後都至少要有一個保留的元素)。
    因此,留下的 k+1k+1 個區段各至少含 1 個元素,子區間長度 j−i+1j - i + 1 必須滿足: (j−i+1)≥(k+1)×1+k=2k+1  ⟺  k≤j−i2(j - i + 1) \ge (k + 1) \times 1 + k = 2k + 1 \iff k \le \frac{j - i}{2} 若 k>j−i2k > \frac{j - i}{2},則無法分割出 k+1k+1 個非空區段,該狀態為非法(無解)。

選項詳細分析

  • A. We can always find an optimal solution that removes one of the largest vv:錯誤。
    貪婪策略不保證得到最佳解。

    • 反例 1(最大值位於端點):
      考慮數列 (100,1,2,1,1)(100, 1, 2, 1, 1),k=1k = 1。
      最大值為 v1=100v_1 = 100。由於必須留下 k+1=2k + 1 = 2 個非空區段,合法的移除位置只能是內部元素 v2,v3,v4v_2, v_3, v_4(移除端點 v1v_1 或 v5v_5 只會剩下 1 個非空區段,為非法操作)。因此任何合法解(包含最佳解)都不可能移除最大值 100100。
    • 反例 2(最大值位於內部):
      考慮數列 (10,10,1,10,10)(10, 10, 1, 10, 10),k=1k = 1。
      內部最大值為 1010。
      • 若移除內部的最大值之一(如 v2=10v_2 = 10),留下兩段 (10)(10) 與 (1,10,10)(1, 10, 10),區段和為 1010 與 2121,最大區段和為 2121。
      • 若移除 v3=1v_3 = 1(非最大值),留下兩段 (10,10)(10, 10) 與 (10,10)(10, 10),區段和皆為 2020,最大區段和為 2020。
        顯然移除最小值 11 能達到嚴格更小的最大區段和(20<2120 < 21),最佳解不移除任何最大值。
  • B. We can always find an optimal solution that does not remove one of the smallest vv:錯誤。

    • 反例:
      考慮數列 (10,1,10)(10, 1, 10),k=1k = 1。
      數列中唯一的最小值為中間的 v2=1v_2 = 1。
      欲移除 11 個數且留下 22 個非空區段,唯一合法的移除選擇就是中間的 v2=1v_2 = 1(若移除 v1v_1 或 v3v_3 均無法留下 2 個非空區段)。
      因此,該問題唯一的合法解(即最佳解)必然移除 v2=1v_2 = 1,不存在「不移除最小值」的最佳解。
  • C. The initial value for m(i,i,0)m(i, i, 0) is viv_i:正確。

🔒

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

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

免費註冊

第 12 題5 分

Consider a directed acylic graph G=(V,E)G = (V, E) of nn tasks where every node is a task and every edge is a dependency. If there is an edge from a task vv to another task ww, that means we need to finish vv before starting ww. We have pp processors of the same capability and every processor can finish any task in one unit of time.
We define a schedule function ss to map a task vv to a positive integer time step s(v)s(v). Since we have only pp processors, so the system can only process at most pp tasks in one time step. Also, a schedule needs to respect the dependency of edges.
A. For every positive integer ii, ∣{v∣s(v)≤i}∣≤p|\{v \mid s(v) \le i\}| \le p.
B. If there is a path (vi1,…,vik)(v_{i_1}, \dots, v_{i_k}) in GG, then s(vij)<s(vik)s(v_{i_j}) < s(v_{i_k}) for 1≤j≤k1 \le j \le k.
C. The number of time steps required is at least min⁡(⌈n/p⌉,L)\min(\lceil n/p \rceil, L), where LL is the number of tasks along the longest path in GG.
D. Without loss of generality, we can always assume s(v)=1s(v) = 1 for all vv's without incoming edges.
E. If the number of processors is infinite, then we can finish all tasks in LL time steps, where LL is the number of tasks along the longest path in GG.

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

這一題的完整詳解

核心觀念

本題考查有向無環圖(DAG)任務排程理論(Task Scheduling on DAGs)、處理器容量限制(Processor Capacity Constraint)、相依性約束(Precedence Constraint)與關鍵路徑(Critical Path / Span):

  1. 排程設定與約束:
    • 排程函數 s:V→Z+s: V \to \mathbb{Z}^+ 將每個任務映射至其執行的正整數時間步。
    • 容量限制:系統具備 pp 台能力相同的處理器,每台處理器每時間步執行一個任務,故在任一時間步 tt 中,執行的任務數量最多為 pp,即 ∣{v∈V∣s(v)=t}∣≤p|\{v \in V \mid s(v) = t\}| \le p。
    • 相依性限制:若存在邊 (v,w)∈E(v, w) \in E,代表任務 vv 必須先於 ww 完成,因每個任務需 1 個時間步,故必須滿足 s(v)<s(w)s(v) < s(w)(即 s(w)≥s(v)+1s(w) \ge s(v) + 1)。

各選項詳細解析

  • A 選項(錯誤):
    集合 {v∣s(v)≤i}\{v \mid s(v) \le i\} 表示在時間步 1,2,…,i1, 2, \dots, i 期間所有已被處理執行的任務聯集。
    由於每個時間步最多能處理 pp 個任務,在 ii 個時間步內系統累積可完成的任務總數上限為:

    ∣{v∣s(v)≤i}∣=∑t=1i∣{v∣s(v)=t}∣≤∑t=1ip=p⋅i|\{v \mid s(v) \le i\}| = \sum_{t=1}^i |\{v \mid s(v) = t\}| \le \sum_{t=1}^i p = p \cdot i

    選項敘述漏乘了時間步數 ii。例如當 i=2,p=2i = 2, p = 2 時,最多可完成 2×2=42 \times 2 = 4 個任務,若任務數充足且排入前兩步,則 ∣{v∣s(v)≤2}∣=4>2|\{v \mid s(v) \le 2\}| = 4 > 2,因此命題為偽。

  • B 選項(錯誤):
    在 DAG 的有向路徑 (vi1,vi2,…,vik)(v_{i_1}, v_{i_2}, \dots, v_{i_k}) 中,由邊的相依約束可知,沿著路徑的任務執行時間步必定嚴格遞增:

    s(vi1)<s(vi2)<⋯<s(vik)s(v_{i_1}) < s(v_{i_2}) < \dots < s(v_{i_k})

    對於路徑上的前置節點 vijv_{i_j}(1≤j<k1 \le j < k),確實滿足 s(vij)<s(vik)s(v_{i_j}) < s(v_{i_k})。然而選項給定的指標範圍為 1≤j≤k1 \le j \le k,當取端點 j=kj = k 時,式子變成:

    s(vik)<s(vik)s(v_{i_k}) < s(v_{i_k})

    任何數不可能嚴格小於自身,出現邏輯矛盾(若路徑長度為單一節點 k=1k = 1 時亦出現相同矛盾)。因此該敘述為偽。

  • C 選項(錯誤):
    完成所有 nn 個任務所需的時間步總數(Makespan T=max⁡v∈Vs(v)T = \max_{v \in V} s(v))受到兩個基本物理下界的限制:

    1. 工作量限制(Work Bound):共有 nn 個任務,每步最多處理 pp 個,故至少需要 ⌈np⌉\left\lceil \frac{n}{p} \right\rceil 步。
    2. 關鍵路徑限制(Span Bound):最長路徑上包含 LL 個相依任務,必須依序串行執行,故至少需要 LL 步。
🔒

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

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

免費註冊

第 13 題30 分

A vertex cover of an undirected graph G = (V, E) is a subset V' ⊆ V such that if (u, v) ∈ E, then either u ∈ V' or v ∈ V' (or both). The size of a vertex cover is defined as the number of vertices in it. The vertex-cover problem is to find a vertex cover of minimum size in a given undirected graph. It has been shown to be NP-hard, thus we cannot find a polynomial-time algorithm for solving it exactly unless NP = P. Fortunately, some polynomial-time approximation algorithms have been proposed for solving the vertex-cover problem.
The following two approximation algorithms take as input an undirected graph and compute a vertex cover for it. Initially, all edges in the graph are uncovered for both Algorithm VC-Arbitrary and Algorithm VC-Max-Degree. Once a vertex v is put into the cover, all edges incident on v are covered and removed.
Algorithm VC-Arbitrary picks an arbitrary uncovered edge (u, v), puts both u and v into the cover, throws out all edges incident on either u or v, and repeats the process until there are no uncovered edges left.
Algorithm VC-Max-Degree picks a vertex v that covers the most of the uncovered edges, puts v into the cover, throws out all edges incident on v, and repeats the process until there are no uncovered edges left.
(a) (10 points) Give an example for which Algorithm VC-Arbitrary yields a solution that is at least double the size of a minimum-size vertex cover. Give another example for which Algorithm VC-Max-Degree will definitely yield a solution with the size more than that of a minimum-size vertex cover no matter how it breaks the tie. Each example should contain no more than ten vertices. You have to justify your solution by showing the vertex cover found by the algorithm and its corresponding minimum-size vertex cover.
(b) (10 points) Prove or disprove that Algorithm VC-Arbitrary has a smaller ratio bound than Algorithm VC-Max-Degree. The ratio bounds in your proof or disproof should be as tight as possible.
(c) (10 points) In the weighted vertex-cover problem, you are given an undirected graph G = (V, E) and a weight w(v) for each vertex v ∈ V, and the objective is to find a vertex cover (covering all edges in E) of minimum total weight. In other words, the goal is to find a vertex cover C of G such that Σ_{v∈C} w(v) is minimized among all possible vertex covers of G. You are required to formulate the weighted vertex cover problem as an integer linear program.

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

這一題的完整詳解

核心觀念

本題評量頂點覆蓋問題(Vertex Cover Problem)的兩種多項式時間近似演算法(VC-Arbitrary 與 VC-Max-Degree)之近似比界限分析與反例構造,以及加權頂點覆蓋問題的整數線性規劃(ILP)模型建構。


(a) 反例構造與分析

1. Algorithm VC-Arbitrary 之反例(解的大小達最小覆蓋之 2 倍)

  • 圖形構造:考慮 5 個頂點的路徑圖 P5P_5:
    • 頂點集合 V={v1,v2,v3,v4,v5}V = \{v_1, v_2, v_3, v_4, v_5\}
    • 邊集合 E={(v1,v2),(v2,v3),(v3,v4),(v4,v5)}E = \{(v_1, v_2), (v_2, v_3), (v_3, v_4), (v_4, v_5)\}
  • 最小頂點覆蓋 C∗C^*:
    選取頂點集合 C∗={v2,v4}C^* = \{v_2, v_4\}。v2v_2 覆蓋邊 (v1,v2)(v_1, v_2) 與 (v2,v3)(v_2, v_3);v4v_4 覆蓋邊 (v3,v4)(v_3, v_4) 與 (v4,v5)(v_4, v_5),覆蓋所有邊,大小為 ∣C∗∣=2|C^*| = 2。
  • VC-Arbitrary 執行過程:
    1. 演算法任意挑選未覆蓋邊 (v1,v2)(v_1, v_2),將兩端點 v1,v2v_1, v_2 放入覆蓋集合,移除相連邊 (v1,v2)(v_1, v_2) 與 (v2,v3)(v_2, v_3)。
    2. 剩餘未覆蓋邊為 (v3,v4)(v_3, v_4) 與 (v4,v5)(v_4, v_5)。演算法挑選邊 (v3,v4)(v_3, v_4),將兩端點 v3,v4v_3, v_4 放入覆蓋集合,移除相連邊。
    3. 圖中已無未覆蓋邊,演算法終止。
      演算法求得之頂點覆蓋為 Calg={v1,v2,v3,v4}C_{alg} = \{v_1, v_2, v_3, v_4\},大小為 ∣Calg∣=4|C_{alg}| = 4。
  • 結果:
∣Calg∣∣C∗∣=42=2\frac{|C_{alg}|}{|C^*|} = \frac{4}{2} = 2

所得覆蓋大小恰為最小頂點覆蓋的 2 倍。

2. Algorithm VC-Max-Degree 之反例(無論如何打破平手皆大於最小覆蓋)

  • 圖形構造:考慮 7 個頂點的圖形:
    • 頂點集合 V={c,v1,v2,v3,v4,v5,v6}V = \{c, v_1, v_2, v_3, v_4, v_5, v_6\}
    • 邊集合 E={(c,v1),(c,v3),(c,v5),(v1,v2),(v3,v4),(v5,v6)}E = \{(c, v_1), (c, v_3), (c, v_5), (v_1, v_2), (v_3, v_4), (v_5, v_6)\}
  • 最小頂點覆蓋 C∗C^*:
    選取 C∗={v1,v3,v5}C^* = \{v_1, v_3, v_5\}。此三頂點覆蓋了三條獨立邊以及所有與中心點 cc 相連的邊,大小為 ∣C∗∣=3|C^*| = 3。
  • VC-Max-Degree 執行過程:
    1. 初始各點度數:deg⁡(c)=3\deg(c) = 3,deg⁡(v1)=deg⁡(v3)=deg⁡(v5)=2\deg(v_1) = \deg(v_3) = \deg(v_5) = 2,deg⁡(v2)=deg⁡(v4)=deg⁡(v6)=1\deg(v_2) = \deg(v_4) = \deg(v_6) = 1。
    2. 頂點 cc 具有唯一最大度數 3,演算法必先挑選 cc 加入覆蓋,移除與 cc 相連的 3 條邊。
    3. 剩餘未覆蓋邊為 3 條互不相交的獨立邊 {(v1,v2),(v3,v4),(v5,v6)}\{(v_1, v_2), (v_3, v_4), (v_5, v_6)\},此時各頂點度數均為 1。
    4. 無論如何打破平手,演算法每挑選一個頂點只能再覆蓋 1 條邊,因此必定需要再挑選 3 個頂點才能完全覆蓋剩餘邊。
    5. 最終演算法求得之覆蓋大小必定為 1+3=41 + 3 = 4。
  • 結果:演算法解大小為 4,嚴格大於最小頂點覆蓋大小 3(4>34 > 3)。

(b) 近似比界限證明

結論:陳述為真(True)。Algorithm VC-Arbitrary 具有比 Algorithm VC-Max-Degree 更小的近似比界限。

證明:

  1. VC-Arbitrary 的近似比為 2:
    演算法每次挑選一條邊 (u,v)(u, v) 並將兩端點加入覆蓋,隨後移除所有與 u,vu, v 相連的邊。演算法所選出的邊集合 M={e1,e2,…,ek}M = \{e_1, e_2, \dots, e_k\} 互不共用頂點,構成原圖的一個極大匹配(Maximal Matching)。
    任何合法的頂點覆蓋 C∗C^* 為了覆蓋 MM 中的每一條獨立邊,對每條邊至少必須選取其一端點,故 ∣C∗∣≥∣M∣=k|C^*| \ge |M| = k。
    而演算法將 MM 中每條邊的兩端點皆加入覆蓋,故覆蓋大小為 ∣Carb∣=2k|C_{arb}| = 2k。由此可得:
∣Carb∣=2k≤2∣C∗∣|C_{arb}| = 2k \le 2|C^*|

因此 VC-Arbitrary 的最差近似比界限為常數 2(且由 (a) 小題之 P5P_5 範例可知該界限為緊的 Tight Bound)。

  1. VC-Max-Degree 的近似比界限為 Θ(log⁡∣V∣)\Theta(\log |V|):
    VC-Max-Degree 屬於貪婪集合覆蓋(Greedy Set Cover)策略,其近似比界限為 H(Δ)=∑i=1Δ1iH(\Delta) = \sum_{i=1}^\Delta \frac{1}{i},其中 Δ\Delta 為圖中最大度數。
🔒

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

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

免費註冊

其他考古題

113 年臺灣大學的其他科目

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