110 年 國立陽明交通大學資訊工程學系碩士班《資料結構與演算法》
第 1 題4 分
Suppose problem is polynomial-time reducible to problem . Which of the following statement(s) is(are) correct?
(A) If is NP-hard, then is NP-hard.
(B) If is NP-hard, then is NP-hard.
(C) If is in P, then is in P.
(D) If is in P, then is in P.
(E) If a problem is in NP, then there does not exist any polynomial-time algorithm to solve it.
登入後即可作答並保存紀錄。
核心觀念
本題考的是多項式時間歸約的方向,以及它和 、、-hard 的關係。
若 ,表示存在一個可在多項式時間內計算的轉換 ,使得對每個輸入 :
因此,若能在多項式時間內解決 ,就能先把 的輸入轉成 的輸入,再呼叫 的解法。也就是說,歸約可以把「解 」轉成「解 」。
是 -hard,表示每個 中的問題都能在多項式時間內歸約到 。此外,:任何可在多項式時間內解決的問題,也都屬於 。
解題方法
先記住歸約的方向:
也就是說, 的多項式時間解法可以透過歸約帶回 ;反方向則不能直接推論。
再看 -hard:若每個 問題都能歸約到 ,而 ,歸約的傳遞性表示每個 問題也都能歸約到 。因此, 也是 -hard。
選項分析
(A) 正確。 若 是 -hard,則每個 問題 都有 。又已知 ,利用多項式時間歸約的傳遞性可得:
因此,每個 問題都能歸約到 ,所以 是 -hard。
第 2 題4 分
Assume that . Which of the following problem(s) is(are) NP-hard?
(A) Finding a longest simple path between two nodes in a directed graph where the edge weight is one for every edge.
(B) Finding a smallest cycle in a graph where the edge weight is one for every edge.
(C) Finding a largest cycle in a graph where the edge weight is one for every edge.
(D) Finding a negative-weight cycle with any kind (positive or negative) of edge weights.
(E) Finding a shortest simple path between two nodes in a directed graph with negative-weight cycles.
登入後即可作答並保存紀錄。
核心觀念
本題考查「NP-hard」與多項式時間演算法的區別,以及簡單路徑、環與負權環的性質。
在假設 下,若一個問題有多項式時間演算法,就不會是 NP-hard。判斷最佳化問題是否 NP-hard,常將它改寫成判定版本:例如「是否存在長度至少為 的路徑?」或「是否存在權重至多為 的路徑?」再由已知的 NP-hard 問題做多項式時間歸約。
解題方法
逐項判斷哪些問題能用多項式時間演算法求解,哪些可由 Hamiltonian Path 或 Hamiltonian Cycle 歸約。
特別要注意:題目中的「simple path」要求路徑不重複經過頂點。即使圖含負權環,簡單路徑的候選數仍有限;若允許重複經過頂點,負權環則會使路徑權重可以無限下降。
選項分析
(A) 正確。
有向圖中的最長簡單路徑是 NP-hard。以指定起點 、終點 的 Hamiltonian Path 判定問題為例:若存在一條從 到 、經過所有 個頂點的簡單路徑,它的邊數就是 。因此,判斷最長簡單路徑是否至少有 條邊,便可判斷是否存在 Hamiltonian Path。每條邊權重為 ,所以最大化路徑權重等同於最大化路徑邊數。
(B) 錯誤。
單位邊權圖中的最小環可在多項式時間內求出。例如,對每個頂點執行 BFS,計算到其他頂點的最短距離,再檢查回到起點的邊,取其中最短的環。整體時間複雜度可為 。因此此問題屬於 ;在 的假設下,它不是 NP-hard。
第 3 題4 分
Huffman codes: Given characters, each with a given frequency, find Huffman binary codes for these characters. Suppose that there are six characters to be encoded. For the following five binary coding trees, select the ones that cannot possibly represent Huffman binary codes.
🖼️【此處有附圖,見下方】
登入後即可作答並保存紀錄。
核心觀念
Huffman 編碼每次取出頻率最小的兩個節點,合併成一個新節點,新節點的頻率等於兩者之和。因此:
- 小方形是葉節點,代表待編碼的字元;圓形是內部節點,代表合併結果。
- 每個內部節點都必須恰有兩個子節點,不能只有一個子節點。
- 六個字元對應六個葉節點,經過五次合併形成整棵樹,所以內部節點數必須為 。
Huffman 樹不必平衡;只要頻率差異適當,長短不一的編碼也能是最佳編碼。
解題方法
依原圖,五個選項各有六個小方形葉節點;但 (B) 左子樹中靠右的圓形、(D) 根節點右側的第一個圓形,都只有一個子節點。(A)、(C)、(E) 的每個圓形則都有兩個子節點。
先用「每個內部節點都有兩個子節點」排除不合法的結構,再為其餘選項構造一組頻率,確認確實能由 Huffman 合併程序產生。以下頻率是用來證明可行性的例子,並非題目給定的數據;遇到相同頻率時,可以選擇其中任意兩個最小節點合併。
選項分析
(A) 可以形成 Huffman 編碼樹,不選。
圖中共有六個葉節點,其深度依序為:
令最深的兩個葉節點頻率皆為 ,其餘葉節點由深到淺配置頻率 。Huffman 合併順序為:
每一步都是把前一步形成的子樹,與同頻率的另一個字元合併,便得到圖中的階梯狀結構。樹很不平衡並不代表它不合法。
(B) 不可能形成 Huffman 編碼樹,要選。
左子樹中靠右的圓形,只有一條向下連線,連到一個小方形葉節點。
這表示該內部節點只有一個子節點,違反 Huffman 每次合併兩個節點的規則。即使共有六個葉節點,也不能成為 Huffman 樹。
第 4 題4 分
Which of the following statement(s) is(are) true about red-black trees?
(A) Red-black trees are not search trees.
(B) An external node must be black.
(C) It is possible that the path from the root to an external node has two consecutive black nodes.
(D) Every path from the root to an external node has the same number of red nodes.
(E) It takes time to insert a key to the red-black tree.
登入後即可作答並保存紀錄。
核心觀念
紅黑樹是附有顏色規則的二元搜尋樹。每個節點為紅色或黑色,空子樹以黑色外部節點(NIL)表示。判斷紅黑樹性質時,需掌握以下規則:
- 每個節點是紅色或黑色,根節點為黑色,外部節點
NIL為黑色。 - 紅色節點的子節點必須是黑色,因此不能有連續的紅色節點。
- 從同一節點出發,到其後代外部節點的每條路徑,都有相同數量的黑色節點;此數量稱為黑高度。
- 紅黑樹的高度為 ,因此搜尋與插入的最壞時間皆為 。
解題方法
逐項對照紅黑樹的定義與性質。特別要區分「黑色節點數相同」和「紅色節點數相同」:紅黑樹保證前者,不保證後者;它也禁止相鄰紅色節點,並未禁止相鄰黑色節點。
選項分析
- (A) 錯誤。 紅黑樹仍然是二元搜尋樹,只是額外為節點標記顏色並維持平衡規則。對每個節點而言,左子樹的鍵值小於節點鍵值,右子樹的鍵值大於節點鍵值。
第 5 題4 分
Assume that we have a data structure that contains elements with . Which of the following statement(s) is(are) true?
(A) Let be a linked list with its elements required to be ordered in ascending order. It takes time to insert a new element into .
(B) Let be a queue. It takes time to remove all the elements one by one from .
(C) Let be a double-ended queue implemented as a doubly linked list and supporting insertion and deletion from both ends. It takes time to add an element to the back of and time to find an element of .
(D) Let be a doubly linked list. Given a node of , it takes time to delete the node from .
登入後即可作答並保存紀錄。
核心觀念
本題考查資料結構操作的時間複雜度,尤其要區分「已知節點位置」與「必須先搜尋位置」的情況。
- 搜尋:在未排序的鏈結串列中尋找指定元素,最壞情況須逐一檢查 個節點,時間為 。
- 已知節點的刪除:雙向鏈結串列可直接透過前後指標調整連結,時間為 。
- 逐一移除所有元素:若每次移除為 ,移除 個元素的總時間為 。
解題方法
逐項判斷操作是否需要遍歷串列,再計算單次操作的複雜度與重複執行的總複雜度。特別注意,題目若已給定節點,就不需再計入尋找該節點的時間。
選項分析
(A) 錯誤。
升冪排序的單向鏈結串列插入新元素前,通常須從串列開頭尋找正確插入位置,最壞情況要走訪 個節點,因此時間為 。找到位置後進行連結指標調整才是 ;整體插入時間不能因此視為 。
第 6 題4 分
We say that if there exists a function in that is greater than all the functions in . For being positive integers, select the correct statement(s) in the following:
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
本題比較函數的漸近成長率。依演算法題目的慣例, 表示代表函數 相較於 成長更快;可用下列極限判斷:
比較 Big-O 的成長階時,通常比較代表函數的成長率;Big-O 函數族包含任意常數倍,不作逐點的字面比較。
解題方法
逐項比較兩邊代表函數的比值。若比值趨近無限大,左側成長較快;若比值趨近零,左側成長較慢。階乘與冪函數的比較可使用 Stirling 公式:
選項分析
(A) 正確
比較 與 :
任何正次方 最終都比任意固定次方的對數函數成長快,因此 成長快於 。
(B) 正確
令 。由 Stirling 公式, 的對數成長量約為 ,而 的對數成長量約為 。由於 ,兩者相減有:
第 7 題4 分
Which of the following graph algorithm(s) utilize dynamic programming?
(A) Kruskal's algorithm
(B) Dijkstra's algorithm
(C) Finding longest common sequence in a pair of strings
(D) The Floyd-Warshall algorithm for computing the all-pairs shortest path of a graph.
登入後即可作答並保存紀錄。
核心觀念
動態規劃會把問題拆成較小的子問題,利用子問題的最佳解或計數結果推導較大問題的解,並保存已算過的結果,避免重複計算。常見特徵是具有最佳子結構,且不同子問題會重複出現。
本題要判斷各演算法是否以這種方式逐步建立答案。選項中的「longest common sequence」依常見考題用語,指兩字串的最長共同子序列(Longest Common Subsequence, LCS)。
解題方法
逐一觀察演算法的核心步驟:
- 若演算法從較小的子問題答案,按規則推導較大的子問題答案,屬於動態規劃。
- 若演算法主要依局部最佳選擇逐步前進,則屬於貪婪法;即使問題本身具有最佳子結構,也不代表該演算法使用動態規劃。
選項分析
(A) Kruskal's algorithm:錯誤。
Kruskal 演算法求最小生成樹。它將邊依權重由小到大排序,再依序選取不會形成環的邊,直到選出 條邊。這是貪婪選擇;它使用並查集判斷兩端點是否已連通,但並查集是資料結構,不是動態規劃。
(B) Dijkstra's algorithm:錯誤。
Dijkstra 演算法在邊權重非負時,反覆選取目前距離起點最近、尚未確定的頂點,並更新鄰點距離。核心是逐步確定最近頂點的貪婪策略,並非透過保存多個較小子問題的解來遞推答案,因此一般分類為貪婪演算法。
(C) Finding longest common sequence in a pair of strings:正確。
第 8 題4 分
The preorder tree walk of a certain binary search tree generates the following sequence of node keys: . Which of the following statement(s) is(are) correct?
(A) is an ancestor of .
(B) has no sibling.
(C) appears after during the postorder tree walk.
(D) appears after during the postorder tree walk.
(E) The two subtrees of the root have the same heights.
登入後即可作答並保存紀錄。
核心觀念
二元搜尋樹(BST)中,每個節點的左子樹鍵值小於該節點,右子樹鍵值大於該節點。前序走訪順序為「根、左、右」,後序走訪順序為「左、右、根」。
本題可利用前序序列依 BST 的大小規則還原樹,再由樹的結構判斷祖先、兄弟、後序順序與子樹高度。
解題方法
前序序列的第一個鍵值 是根。後續小於 的鍵值屬於左子樹,大於 的鍵值屬於右子樹:
- 左子樹前序序列:。根為 ,其右子節點為 ; 的左子節點為 ,右子節點為 。
- 右子樹前序序列:。根為 ,其左子節點為 ; 的右子節點為 。
還原出的樹如下:
6
/ \
2 9
\ /
4 7
/ \ \
3 5 8
依「左、右、根」進行後序走訪,左子樹得到 ,右子樹得到 ,最後訪問根 。因此完整後序序列為:
第 9 題3 分
Consider radix sort of a list of positive integers, with each decimal digit as a key. Assume that we proceed in a least-significant-digit-first manner, and we want to sort the list from small to large. Let the initial list of integers be:
(A) After the first pass, only one of the first four numbers in the list is less than .
(B) The last number in the list after the second pass is .
(C) is among the last four numbers of the list after the second pass.
(D) It is possible to have radix sort run with linear time complexity with respect to the length of the list to be sorted.
登入後即可作答並保存紀錄。
核心觀念
本題考 最低有效位數優先基數排序(LSD radix sort)。每一趟依序處理個位、十位、百位等位數,並且每趟都必須使用穩定排序:同一位數相同的元素,維持前一趟的相對順序。
不足位數的整數以 補齊,例如 視為 。
解題方法
初始序列為:
第一趟:依個位數排序
個位數分別為 。依個位數由小到大排列,個位數相同時保留原順序:
前四個數字是 ,其中只有 。
第二趟:依十位數排序
在第一趟結果上查看十位數:
| 數字 | 十位數 |
|---|---|
第 10 題3 分
Which of the following sorting algorithm(s), using typical implementation, apply the divide-and-conquer approach?
(A) Quick Sort
(B) Heap Sort
(C) Selection Sort
(D) Insertion Sort
(E) Merge Sort
登入後即可作答並保存紀錄。
核心觀念
分治法(divide and conquer)的典型流程是:
- 分解:把原問題切成較小的子問題。
- 征服:遞迴解決各子問題。
- 合併:把子問題的解組合成原問題的解。
判斷排序法是否採用分治法,重點在於它是否把待排序序列拆成較小的排序問題,再以遞迴方式處理。演算法內部使用樹狀結構或遞迴操作,本身不足以判定它是分治法。
解題方法
逐一檢查各演算法的主要流程:是否將原序列分成子序列、遞迴排序,再合成完整排序結果。符合這種結構的演算法就是分治法。
選項分析
(A) Quick Sort:正確。
快速排序先選擇樞紐值,將元素分割為小於樞紐與大於樞紐的兩側子序列,再遞迴排序兩側。分割完成後,樞紐已在最終位置,左右兩側不需額外合併即可形成完整排序結果,因此屬於分治法。
第 11 題3 分
Consider the maximum flow problem. Assume that all the edge capacities are positive integers. Which of the following statement(s) is(are) correct?
(A) There is a polynomial-time algorithm to find a maximum cut.
(B) There is a polynomial-time algorithm to find a minimum cut.
(C) There are possibly many distinct flows having maximum flow value with different edge flows.
(D) There are possibly many distinct minimum cuts with different total capacities.
(E) The sum of the capacity of a minimum cut and the value of a maximum flow must be an even number.
登入後即可作答並保存紀錄。
核心觀念
令網路的頂點集合為 ,源點為 ,匯點為 。一個 - 割將頂點分成兩部分 ,其中 、。其容量是所有由 指向 的邊容量總和:
最大流最小割定理指出:
此外,若邊容量皆為整數,則存在一個每條邊流量皆為整數的最大流。
解題方法
逐一檢查選項中的演算法複雜度、流量是否可有多種配置,以及最大流最小割定理與整數容量性質。
選項分析
(A) 錯誤。 一般圖上的最大割問題要求找出切割容量最大的頂點分割,屬於 NP 困難問題;最大流最小割定理只保證尋找最小割,不代表最大割也能在多項式時間內求得。
第 12 題3 分
Which of the following statement(s) is(are) correct? Assume that is a constant and .
(A) If , then .
(B) If , then .
(C) If , then .
(D) If , then .
(E) If , then .
登入後即可作答並保存紀錄。
核心觀念
本題考查遞迴式的漸近分析,主要工具是主定理與遞迴樹。遞迴樹中,每一層的總成本要乘上該層的子問題數量,再把各層成本加總;葉節點的成本也要納入。
以下的 可取任意固定底數。對含 的遞迴式,漸近分析以足夠大的 為準,底部以常數規模作為終止條件。
解題方法
先判斷各選項是否可直接套用主定理;若額外成本 不屬於主定理的常見情況,就用遞迴樹逐層加總。對子問題大小不固定為 的選項,則檢查規模縮小速度,以及每層或所有層的總成本。
選項分析
(A) 錯誤
遞迴式為
每層子問題的總規模都是 ,因此在規模為 的那一層,總成本為
樹高為 。將各層成本加總,得到
所以 ,不是 。
(B) 錯誤
遞迴式為
在規模為 的那一層,所有子問題的非遞迴成本合計為
因此各層成本總和具有下列量級:
葉節點成本為 ,不會改變此量級。因此 ,不是 。
(C) 正確
遞迴式為
規模為 的那一層,非遞迴成本合計為
第 13 題3 分
Let be an undirected connected graph, where each edge has a given length . Let be a fixed vertex of this graph and let be the length of a shortest path from to . Which of the following statement(s) is(are) correct?
(A) For each vertex , there is an incident edge such that .
(B) For each vertex , choose one incident edge such that ; the collection of these edges forms a spanning tree of .
(C) For each vertex , choose one incident edge such that ; the collection of these edges forms a minimum-weight spanning tree of .
(D) for some edge .
(E) for every edge .
登入後即可作答並保存紀錄。
核心觀念
本題考查單源最短路徑的距離函數 ,以及最短路徑上的前驅邊。
因為 連通且邊長皆為正,每個頂點 都有從 到達的最短路徑。若最短路徑最後一條邊是 ,則
另一方面,對任意邊 ,先走一條從 到 的最短路徑,再走邊 ,便得到一條從 到 的路徑。因此
這是最短距離必須滿足的三角不等式。
解題方法
先由最短路徑的定義判斷每條邊是否都滿足三角不等式,再利用最短路徑的最後一條邊判斷是否存在等式成立的前驅邊。接著檢查選出的前驅邊是否形成樹,以及是否必然是最小生成樹。
對任意 ,取一條從 到 的最短路徑。設最後一條邊為 ,則該路徑到達 的部分也是從 到 的最短路徑,否則可替換成更短的路徑,使到達 的路徑更短。因此 。
若每個 都選一條滿足等式的邊作為前驅邊,由於 ,沿著前驅邊回溯時,距離會嚴格遞減:
回溯不可能形成環,且有限次後必定到達距離為 的 。所以這些邊將所有頂點連通,並且沒有環,構成一棵生成樹。
選項分析
第 14 題3 分
Consider a B-tree with minimum degree . Keys in the same node are stored in ascending order. The median key is used to split a full node. Initially the B-tree is empty. We then build the B-tree by inserting the keys and one by one in the given order. The following statements are independent from each other and all start from the same tree. Which of the following statement(s) is(are) true?
(A) If is inserted into the B-tree, the height of the B-tree is changed.
(B) If is inserted into the B-tree, the height of the B-tree is not changed.
(C) The keys and are not in the same node.
(D) The keys and are not in the same node.
登入後即可作答並保存紀錄。
核心觀念
B-tree 的最小度數為 ,因此:
- 非根節點至少有 個鍵,最多有 個鍵。
- 插入時若遇到已滿的節點,取中位數鍵升到父節點;中位數左、右兩側的鍵分別成為兩個子節點。
- 樹高只有在根節點分裂、產生新的根節點時才會增加。葉節點分裂本身不會增加樹高。
解題方法
依序插入 後,根節點有五個鍵,已滿:
接著插入 時,先將滿根分裂。中位數 升為新根,左右兩側各形成一個子節點:
再將 依序插入右側子節點。題目給定以中位數分裂,因此最後的樹為:
第 15 題3 分
For the given weighted graph to the right, let and be the total weights of its minimum spanning tree and its second-best minimum spanning tree, respectively. Which of the following statement(s) are true?
🖼️【此處有附圖,見下方】
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
最小生成樹(MST)是連接所有頂點且不含環的生成樹中,總邊權重最小者。用 Kruskal 演算法依邊權重由小到大選邊;求第二小生成樹時,可將一條未選邊加入 MST,形成一個環,再移除環上權重最大的邊,得到替換後的生成樹。
解題方法
圖中共有 9 個頂點;依權重由小到大讀到的邊為 。Kruskal 選取權重 的邊後,所有頂點連通且沒有環,因此:
要找第二小生成樹,逐一檢查未選邊加入 MST 所形成的環:
| 加入的邊 | MST 中對應路徑上的最大權重 | 替換後總權重 |
|---|---|---|
| (連接中央兩個頂點) |
第 16 題3 分
This question is about binary search trees. All the keys are unique. Let be a tree node, be the parent of , be the left child of , be the right child of , and be the key of . Assume that the root of a binary search tree has left and right children. The key of the root is greater than the key of the left child but is smaller than the key of the right child. Which of the following statement(s) is(are) true for binary search trees?
(A) If and are non-empty, then is greater than all the keys in the subtree rooted at .
(B) If is non-empty, then we must have .
(C) If and are non-empty, then must be larger than .
(D) If , , and are non-empty, then must be less than all the keys in the subtree rooted at .
登入後即可作答並保存紀錄。
核心觀念
二元搜尋樹(BST)的關鍵性質是:對任一節點 ,
- 的左子樹中,所有鍵值都小於 。
- 的右子樹中,所有鍵值都大於 。
鍵值互異,因此可使用嚴格不等式。判斷選項時,要看節點位於哪個子樹;僅知道節點是父節點、左子節點或右子節點,不能忽略祖先所設定的鍵值範圍。
解題方法
逐項將節點位置轉成大小關係。對於「必須」成立的敘述,只要能構造出符合 BST 規則、但不符合敘述的例子,就能判定為錯。
選項分析
(A) 正確。
是 的左子節點,因此它整棵子樹中的鍵值都小於 :
題目多給的條件 非空,不會改變這項 BST 性質。
(B) 錯誤。
父節點可能是 的左子節點,也可能是右子節點。若 是父節點的右子節點,則 ,不符合選項所稱的「必定大於」。
第 17 題3 分
This question is about a directed flow network. The flow and capacity are denoted as on an edge, where is the flow amount and is the capacity. The following figure shows a flow in a directed flow network. and are the source and the sink, respectively.
🖼️【此處有附圖,見下方】
Which of the following sets of , , , and are valid for the flow?
(A) , , , and
(B) , , , and
(C) , , , and
(D) , , , and
登入後即可作答並保存紀錄。
核心觀念
合法流量必須同時符合兩個條件:
- 流量守恆:除源點 與匯點 外,每個節點的流入量等於流出量。
- 容量限制:每條邊的流量不得超過容量,且流量不得為負。
圖中標示的 表示流量為 、容量為 。
解題方法
圖中左側兩條邊的流量分別為 ( 流向左上節點)與 ( 流向左下節點);中間兩條反向邊分別是 (左上流向左下)與 (左下流向左上)。左上到右上流量為 ,左下到右下流量為 。右側垂直邊中, 的方向是由右上流向右下, 的方向是由右下流向右上;右上流向 的流量為 ,右下流向 的流量為 。
套用右上節點的流量守恆:
套用右下節點的流量守恆:
接著逐一代入選項,並確認每條邊的流量不超過其容量。
選項分析
第 18 題3 分
Divide and conquer for sorting: Given a set of integers, where is an exact power of .
(A) If I divide the integers into equal parts, conquer the parts recursively, and merge the parts into a sorted sequence in linear time, I get the sorted sequence in time.
(B) If I divide the integers into equal parts, sort the parts using selection sort, I get the sorted sequence in time.
(C) If I divide the integers into equal parts, sort the parts using heap-sort, I get the sorted sequence in time.
(D) Let represent base- logarithm. If I divide the integers into parts, with each part except the last part having integers (), sort each part using heap-sort, and merge the parts, I get the sorted sequence in time.
登入後即可作答並保存紀錄。
核心觀念
本題考查分治排序的時間複雜度,以及「合併多個已排序序列」所需的成本。
分治法常用遞迴式表示:
其中 是子問題數量、每個子問題的規模為 ,而 是切分與合併的成本。合併兩個已排序序列可在線性時間 完成;合併 個已排序序列,常用最小堆維護各序列目前最小元素,成本為 。
解題方法
逐項計算排序子問題的成本,再加上合併成本。注意:把資料分成多個區塊後,區塊內排序的成本並非全部成本;還必須計入合併所有區塊的時間。
選項分析
(A) 正確。
分成 個大小為 的子問題,每個子問題遞迴排序,最後以線性時間合併。遞迴式為:
每層所有子問題的合併成本合計為 ,遞迴深度為 ,因此:
(B) 正確。
兩個大小為 的部分各自使用選擇排序,排序成本合計為:
第 19 題3 分
A maximum binary heap (Max-Heap for short) is implemented in an array, starting with index . (In many programming languages, the array index starts at . In this case, we skip the first cell which has index . For example, represents a Max-Heap where is the root, is the left child of , and is the right child of $3. Since we don’t use the first cell, it is marked as “-”.) Which of the following statement(s) is(are) true?
(A) Given the input , the Max-Heap built by the best (linear-time) algorithm from is .
(B) is a Max-Heap. By deleting the maximum, we get .
(C) is a Max-Heap. By inserting , we get .
(D) Binary Max-Heap supports the merging of two Max-Heaps in linear time.
登入後即可作答並保存紀錄。
核心觀念
以陣列表示的二元最大堆,從索引 開始時,索引 的左子節點與右子節點分別位於 、;父節點位於 。最大堆要求每個父節點的值都不小於子節點,因此根節點是全堆最大值。
建堆、刪除最大值與插入的操作如下:
- 線性時間建堆:從最後一個非葉節點 開始,依序向前執行向下調整,時間複雜度為 。
- 刪除最大值:移除根節點,以最後一個元素補到根,再向下調整。
- 插入:新元素放在陣列末端,再與父節點比較並向上調整。
- 合併兩堆:將兩個堆的元素放進同一陣列,再執行線性時間建堆。
解題方法
逐一依照操作規則模擬即可。對於建堆與刪除,檢查被調整節點是否要與較大的子節點交換;對於插入,從新增元素的位置開始向上比較。
選項分析
(A) 錯誤。 對 執行自底向上的線性建堆:
- 從索引 開始, 與子節點 比較,交換 。
- 索引 的 與子節點 比較,交換 。
- 索引 的 先與較大的子節點 交換,再與子節點 交換。
- 索引 的 先與 交換,再依序與 、 交換。
第 20 題3 分
This question considers “vector” in C++ STL. The usage of vector is very similar to a regular array in C/C++ in that it supports direct access. In addition, it also supports the operation \texttt{push_back}, which adds an element at the end of a vector. As a result, we do not need to declare the size for a vector.
(A) Elements in a vector are stored in consecutive memory spaces so that it supports direct access.
(B) Let be a vector of size . The statement \texttt{V.push_back(a)} adds at the end of . However, the memory allocated for might be too small to hold the added element . This is handled by allocating a new block of memory of size , copying all the elements of to the newly allocated space, and then inserting at the end of .
(C) If we make a sequence of insertions, there is a way that we can achieve amortized cost for the memory space management.
(D) A single \texttt{push_back} operation could take time, where is the number of elements in the vector.
登入後即可作答並保存紀錄。
核心觀念
本題考查 C++ STL vector 的連續儲存特性,以及動態陣列在容量不足時的擴充策略。
vector 的元素存放在連續記憶體中,因此可以用索引直接存取,時間為 。vector 的「大小」是目前元素數量,「容量」則是已配置的空間可容納的元素數量。當容量已滿時,push_back 必須配置較大的記憶體區塊、搬移既有元素,再加入新元素。
常見的策略是按固定倍數擴充容量,例如每次加倍。這使得單次 push_back 的最壞時間仍可能是 ,但連續執行 次插入的總搬移成本為 ,所以每次插入的攤銷成本為 。
解題方法
逐項檢查敘述:先判斷 vector 是否連續儲存,再看選項描述的擴充策略是否能支援攤銷常數時間,最後區分「單次最壞成本」與「一連串操作的攤銷成本」。
若容量每次只增加 ,插入 個元素時,搬移次數約為:
若容量採倍數成長,搬移次數則形成幾何級數:
因此,倍數擴充能讓 次插入的總搬移成本維持線性。
選項分析
第 21 題2 分
We generalize Huffman’s algorithm to ternary codes (i.e., codes using three symbols, , , and ). Suppose that there are six characters to be encoded. For the following five ternary coding trees, select the ones that cannot possibly represent Huffman ternary codes.
🖼️【此處有附圖,見下方】
登入後即可作答並保存紀錄。
核心觀念
三元 Huffman 演算法每次合併目前權重最小的三個節點。六個字元的數量是偶數,因此可先合併兩個字元;此後每次合併三個節點。等價地說,六個字元的樹中應有一個由兩個原始字元組成的二元內部節點,其餘內部節點皆有三個子節點,且根節點有三個子節點。
解題方法
圖中每個方框代表一個字元。逐一檢查樹的分支數與位置:A、E 各有一個由兩個字元組成的二元節點;B 的二元節點位在樹的中間;C 的根節點只有兩個子節點;D 則有三個二元節點。
判斷時要特別檢查二元節點是否能對應第一次合併。第一次合併發生在尚未建立任何子樹時,因此二元節點必須直接連接兩個原始字元。最後一次合併會建立根節點,所以根必須有三個子節點。
選項分析
- (A) 可以代表。 樹中有一個二元節點,且它的兩個子節點都是原始字元;根與另一個內部節點則各有三個子節點。可令二元節點的兩個字元權重為 ,三元子樹的三個字元權重為 ,剩餘字元權重為 。先合併 ,接著合併 ,最後合併剩下的三個節點,能形成圖示結構。
第 22 題2 分
Let and be asymptotically positive functions, where and for all sufficiently large . Which of the following statement(s) is(are) correct?
(A) implies .
(B) implies .
(C) implies .
(D) .
(E) .
登入後即可作答並保存紀錄。
核心觀念
本題考查漸近上界 、對數與指數的成長關係,以及階乘和指數函數的比較。
若 ,表示存在常數 與 ,使得對所有 ,
對數會把乘法常數轉成加法常數;但指數函數會放大指數中的比例差異。因此,取對數與取指數時,不能直接把 關係照搬。
解題方法
逐項檢查各選項。對含有 、 的敘述,可用簡單函數檢驗是否必然成立;對含有階乘或指數的敘述,則比較兩側的對數成長率。以下假設 使用固定底數 。
選項分析
(A) 正確。
由 ,存在常數 ,使得充分大的 滿足 。取對數得
題目給定充分大的 有 ,因此常數項 可由 的常數倍控制。再加上 ,可知
(B) 錯誤。
令 、。則
但 。所以對數的 關係不足以推出原函數的 關係。
(C) 錯誤。
令 、,則 。然而
第 23 題2 分
For the given directed weighted graph below:
🖼️【此處有附圖,見下方】
(A) In every topological sort of the graph, has to appear after .
(B) The path from to with the most total weight includes four edges.
(C) The path from to found by Dijkstra’s algorithm contains three edges.
(D) If we add an edge from to with weight , we can still obtain a valid topological sort.
(E) If we add an edge from to with weight , Dijkstra’s algorithm will still find the same path from to as before the edge is added.
登入後即可作答並保存紀錄。
核心觀念
拓樸排序只適用於有向無環圖(DAG),且只看邊的方向,不看權重。Dijkstra 演算法則在邊權重非負時,逐步確定從起點到各頂點的最短距離。
解題方法
圖中的有向邊與權重為:、、、、、、、、。
先依邊的方向檢查拓樸排序,再列出從 到 的路徑比較總權重。用 Dijkstra 演算法計算最短路徑時,從 出發,初始距離為 、。依序處理 、、、 後,得到 、,最短路徑為 。
選項分析
- (A) 錯誤。 不必排在 之後。排序 符合所有邊的方向,因此 可以在 之前。
- **(B) 錯誤。
第 24 題2 分
The hash function is used to map any integer key to one of the slots, indexed , of a hash table of slots.
(A) If is , there is just one occurrence of collision among these keys: .
(B) Collision does not happen as long as the load factor is less than .
(C) Consider a hash table where is and uses chaining to handle collisions. The table is initially empty. After we insert the keys into the table in the given order, key is in the slot indexed .
(D) Consider a hash table where is and uses open addressing with linear probing to handle collisions. The table is initially empty. After we insert the keys into the table in the given order, key is in the slot indexed .
登入後即可作答並保存紀錄。
核心觀念
雜湊函數 以除以 的餘數決定鍵值所屬的槽位,槽位編號為 到 。不同鍵值若映射到同一槽位,就發生碰撞。
負載因子定義為
其中 是已存入的鍵值數, 是雜湊表的槽位數。負載因子描述表格的平均使用程度,但不能單獨保證不同鍵值會映射到不同槽位。
解題方法
先計算每個鍵值的雜湊位置,再依題目指定的碰撞處理方法模擬插入。
對 ,鍵值 的雜湊位置如下:
| 鍵值 | 雜湊位置 | |
|---|---|---|
| 1 | 1 | |
| 2 | 2 | |
| 4 | 4 | |
| 6 | 1 | |
| 8 | 3 |
只有 和 映射到同一個槽位,形成一組碰撞。
選項分析
(A) 正確。
鍵值 與 都映射至槽位 ;其他鍵值分別映射至槽位 、、,沒有其他碰撞。因此這組鍵值只有一組發生碰撞。
第 25 題2 分
A red-black tree is initially empty. Now four keys are inserted in the order of . Which of the following statement(s) is(are) true about the resulting red-black tree?
(A) There are no more than external nodes.
(B) There are two red nodes.
(C) The node is black.
(D) Exactly one rotation operation occurs during the insertion of the four nodes.
(E) Nodes and have different colors.
登入後即可作答並保存紀錄。
核心觀念
紅黑樹插入新節點時,新節點先著成紅色,再依紅黑樹規則修正:
- 根節點必須是黑色。
- 紅色節點不能有紅色子節點。
- 每條從節點到後代空節點的路徑,黑色節點數必須相同。
- 若新節點的父節點與叔叔節點皆為紅色,將父節點與叔叔節點改為黑色,祖父節點改為紅色;若祖父是根,再將根改回黑色。此情形只需重新著色,不需旋轉。
外部節點指紅黑樹中的空子節點(NIL)。含有 個實際節點的二元樹有 個外部節點。
解題方法
依序插入 ,每次插入後檢查紅黑樹條件:
- 插入 : 成為根,著為黑色。
- 插入 : 是紅色,成為 的左子節點。
- 插入 : 是紅色,成為 的右子節點。
- 插入 : 是紅色,成為 的左子節點。此時父節點 與叔叔節點 都是紅色,故將 、 改為黑色,並將祖父節點 改為紅色。由於 是根,再將 改回黑色。
結果如下:
5(B)
/ \
3(B) 8(B)
/
2(R)
整個插入過程只有重新著色,沒有旋轉。
第 26 題2 分
Consider the following sequence of operations for disjoint sets (there is no set initially):
\texttt{Make-Set('a'), Make-Set('b'), Make-Set('c'), Union('b','c'), Make-Set('d'), Make-Set('e'), Make-Set('f'), Union('e','f'), Union('a','b'), Union('a','f')}
Which of the following statement(s) is(are) true after this sequence of operations?
(A) There are two sets.
(B) Find-Set('b') and Find-Set('f') return the same node.
(C) Find-Set('c') and Find-Set('d') return the same node.
(D) Each union operation takes constant time.
登入後即可作答並保存紀錄。
核心觀念
不相交集合(Disjoint Sets)以 Make-Set(x) 建立只含 的集合;Union(x,y) 將含 與含 的兩個集合合併。Find-Set(x) 回傳 所屬集合的代表元。
每次合併只會減少一個集合,因此可由集合的合併過程追蹤最後的分組。
解題方法
依序整理各元素所屬的集合:
| 操作 | 操作後的集合 |
|---|---|
Make-Set('a'), Make-Set('b'), Make-Set('c') | |
Union('b','c') | |
Make-Set('d'), Make-Set('e'), Make-Set('f') | |
Union('e','f') | |
Union('a','b') |
第 27 題2 分
Activity-selection problem. Suppose we have a set of proposed activities. Each activity has a start time and a finish time , where . If selected, activity takes place during the half-open time interval . Activities and are compatible if the intervals and do not overlap. That is, and are compatible if or . In the activity-selection problem, we wish to select a maximum-size subset of mutually compatible activities. For the following greedy ideas, select the one(s) that yield an optimal solution.
(A) Selecting the first activity to start (that is compatible with all previously selected activities).
(B) Selecting the first activity to finish (that is compatible with all previously selected activities).
(C) Selecting the last activity to start (that is compatible with all previously selected activities).
(D) Selecting the last activity to finish (that is compatible with all previously selected activities).
(E) Selecting the compatible activity that overlaps the fewest other remaining activities.
登入後即可作答並保存紀錄。
核心觀念
這題考活動選擇問題(activity-selection problem):從多個活動中挑出最多個互不重疊的活動。活動 佔用半開區間 ;兩活動相容的條件是 或 。因此,一個活動恰好在另一個活動結束時開始,兩者仍相容。
標準的貪婪策略是:每次選擇最早結束的相容活動,接著只考慮它結束之後的活動。反向看,從時間軸尾端開始,每次選擇最晚開始的相容活動,也會得到最佳解。
解題方法
先確認各策略的選擇依據是否能保留最多後續安排空間。最早結束的活動會盡早釋出時段,因此可用交換論證證明它不會使最大可選數量變少。
設 是所有活動中最早結束者, 是任一最佳解,而 是 中最先結束的活動。因為 最早結束,所以 。將 中的 換成 ,原本排在 之後的活動仍不會與 重疊;替換後的解仍是最佳解。故最早結束的活動可以安全地納入最佳解。選定後,對剩下與它相容的活動重複同一論證,即可得到最佳解。
從尾端反向套用同一論證,選擇最晚開始的相容活動也能得到最佳解。至於只看開始時間、結束時間,或剩餘重疊數的策略,則可能作出讓後續可選活動變少的局部選擇。
選項分析
第 28 題2 分
This question is about the Ford-Fulkerson method. The flow network is shown below to the left, and its corresponding residual network is shown below to the right. The flow and capacity are denoted as on an edge, where is the amount of flow and is the capacity.
🖼️【此處有附圖,見下方】
Which of the following statement(s) are true?
(A) ,
(B) ,
(C)
(D) ,
登入後即可作答並保存紀錄。
核心觀念
Ford–Fulkerson 方法利用殘餘網路表示「還能增加多少流量」與「能撤回多少既有流量」。原網路的一條邊 ,若流量為 、容量為 ,會產生:
- 正向殘餘邊 :殘餘容量為 。
- 反向殘餘邊 :殘餘容量為 ,代表可撤回已送出的流量。
因此,必須先辨認箭頭方向,再決定使用 或 。
解題方法
依原卷左圖,相關邊為 的 、 的 、 的 ,以及 的 。右圖中, 標在 , 標在 , 標在 , 標在 。
逐一對照原網路與殘餘網路:
| 標記 | 殘餘邊方向 | 對應計算 | 殘餘容量 |
|---|---|---|---|
| 的反向邊,取原流量 | |||
| 的反向邊,取原流量 | |||
| 的反向邊,取原流量 |
第 29 題2 分
Function iteration: The notation (NOT ) denotes the function iteratively applied times to an initial value of . For non-negative integers , if , and if . Now let represent base- logarithm. is defined as above with ; note that is defined only if . Let denote the iterated logarithm, given by . Which of the following statement(s) is(are) correct?
(A) If , then .
(B) If , then .
(C) .
(D) .
登入後即可作答並保存紀錄。
核心觀念
本題考兩種容易混淆的「反覆套用」:
- 表示把函數 連續套用 次,初值為 ;當 時,結果是 。
- 表示連續取以 為底的對數,直到結果小於或等於 所需的最少次數。
因此, 是「取對數的次數」,不是再對 取兩次對數。
解題方法
先依定義推導 的反覆套用結果。每套用一次,數值乘以 :
一般而言,套用 次會乘上 ,所以:
再計算 :依序取對數,直到結果不大於 :
共取了 次對數,因此 。
選項分析
第 30 題2 分
Given integers, the problem of searching for the th largest integer among the integers is called the “selection problem”. Selection problems can be solved in linear time. A typical implementation starts by partitioning the integers into groups. Then we solve a sequence of median finding (selection problem again) from integers of smaller problem size. Finally, we can drop out more than a quarter of the integers. The next iteration is to deal with a selection problem of size less than . Thus linear time is achieved. Which of the following statement(s) is(are) correct?
(A) In the linear-time algorithm stated above, if we partition the set of integers into groups, we still can achieve linear-time performance.
(B) In the linear-time algorithm stated above, if we partition the set of integers into groups, we still can achieve linear-time performance.
(C) This problem can be solved in time using heap sort.
(D) If heap sort is applied, this problem has lower bound because is the lower bound to heap sort.
登入後即可作答並保存紀錄。
核心觀念
本題考的是 BFPRT 中位數的中位數選擇法,以及漸近下界的判讀。
將元素分成每組 個( 為奇數),找出各組中位數,再遞迴選出這些中位數的中位數作為樞紐。若樞紐能保證丟棄足夠多元素,選擇問題的遞迴式便可在每層只花線性時間,總時間也能維持線性。
解題方法
若共有約 組,至少約一半的組中位數不小於樞紐。每個這樣的完整組,至少有 個元素不小於樞紐。因此,可保證至少約
個元素不小於樞紐;另一側也有相同的保證。忽略分組與重複值造成的常數項,遞迴式可寫成
比較兩個遞迴子問題的比例總和:
- :比例總和為 ,此遞迴式無法推出線性時間。
- :比例總和為 ,因此遞迴式可解得 。
選項分析
(A) 錯誤。
每組 3 個元素時,樞紐兩側各只能保證排除約 個元素,較大的遞迴子問題仍約有 個元素。遞迴式為