112 年 國立臺灣大學資訊工程學系碩士班《資料結構與演算法》
第 1 題5 分
Multiple Choice Problems (70 points, 5 points for each problem)
- Assuming we have n data points. Among the variant characteristics of the data points,
please select the correct descriptions.
(A) If the worst-case running time is the most important, merge sort can be a good
choice with time.
(B) If the input happens to be sorted already, bubble sort can be a best choice with
time.
(C) If the input array is in random order and the average sorting time is most
important, quick sort can be a good choice with time.
(D) If the exchanges of the items in the array are very expensive, selection sort will
incur the least "swaps" or "moves".
(E) If the input array consists of integers in the range , radix sort with radix n
with time.
登入後即可作答並保存紀錄。
第 1 題 詳解
核心觀念
本題考察各排序演算法在不同應用場景下的時間複雜度與行為特性,需掌握:
- Merge Sort:穩定排序,worst-case ,需 額外空間。
- Bubble Sort:最佳情況(已排序)可達 ,worst-case 。
- Quick Sort:平均 ,worst-case (例如 pivot 每次選到最大/最小值)。
- Selection Sort:無論輸入為何,交換次數最多 次(每輪至多一次 swap),比較次數永遠 。
- Radix Sort:非比較排序,time complexity 需由 digit 數與 radix 決定。
解題方法
逐一驗證每個選項所述的「場景」與「排序演算法特性」是否相符。關鍵是將場景條件(最壞情況 / 已排序輸入 / 隨機輸入 / 交換昂貴 / 整數範圍)對應到各演算法真正的行為,而非只背誦「平均複雜度」。
選項分析
(A) ✅ 正確
若最壞情況執行時間最重要,Merge Sort 是好選擇,worst-case 。
Merge Sort 的時間複雜度在任何輸入下均為 ,無論輸入已排序、逆序或隨機,遞迴分割與合併的步驟數不受輸入分佈影響。其遞迴關係為:
相較之下,Quick Sort 的 worst-case 為 ,Heap Sort 雖然 worst-case 同為 ,但 Merge Sort 仍是「保證 」的典型代表之一。(A) 敘述正確。
(B) ✅ 正確
若輸入已是排序狀態,Bubble Sort 是好選擇,best-case 。
標準的 Bubble Sort(附有「若本輪無交換則提前結束」的最佳化旗標)在輸入已排序時,只需掃描一次 對相鄰元素,確認無需交換即結束,時間複雜度為 。
此條件限定「輸入恰好已排序」,Bubble Sort 在此特例下確實表現最優。(B) 敘述正確。
(C) ✅ 正確
若輸入隨機且平均排序時間最重要,Quick Sort 是好選擇,average-case 。
Quick Sort 的平均時間複雜度分析:假設 pivot 每次將資料分成大致相等的兩半(在隨機輸入下此假設成立),遞迴關係同樣為:
更嚴格的平均分析(對所有 種排列取期望)可得平均比較次數約為 。Quick Sort 的常數因子小(cache-friendly、in-place),在隨機輸入下實務效能往往優於 Merge Sort 與 Heap Sort。(C) 敘述正確。
(D) ✅ 正確
若陣列中的交換(swap/move)代價極高,Selection Sort 的交換次數最少。
Selection Sort 的運作方式:每一輪在未排序部分找出最小值,僅做一次交換,將它放到正確位置。因此整個排序過程中,交換次數最多 次(精確地說是 次),與輸入順序無關。
- Bubble Sort:每次比較若逆序即交換,worst-case 交換次數 。
- Insertion Sort:每次插入需移動 個元素,worst-case 次移動。
- Quick Sort / Merge Sort:交換或搬移次數亦為 量級。
在「交換代價遠大於比較代價」的場景(例如大型資料結構的物理搬移),Selection Sort 的 次 swap 明顯優於其他演算法。(D) 敘述正確。
(E) ❌ 錯誤
第 2 題5 分
- B+ tree is an extension of B tree. The major differences from B tree are (1) all leaf
nodes are linked together in a doubly-linked list, and (2) data points are stored on
the leaf nodes only; internal nodes only hold keys and act as routers to the correct
leaf node; the left child is smaller than the key and the right child is larger or equal
than that. Please find any/all violations of a B+ tree structure in the following
diagram. Assume the tree node can at most contain 4 data points (keys).
🖼️【此處有附圖,請對照原卷】
(A) 10
(B) 13,20
(C) 6,8
(D) 1,2,3 6,7
(E) 20
(F) 22,27
(G) 30,40
(H) 33,36
(I) 47,52
(J) 8,9
(K) 10,11
(L) 13,14
(M) 20,21
(N) 22,23
(O) 24,25
(P) 27,28
(Q) 30,31
(R) 33,34
(S) 36,37
(T) 40,43
(U) 47,49
(V) 52,55
登入後即可作答並保存紀錄。
核心觀念
B+ 樹的內部節點只存放分隔鍵,實際資料存於葉節點。判斷圖中的違規,須同時檢查兩項規則:
-
節點的最低容量:內部節點有 個鍵,就有 個子節點。本題每個節點最多有 個鍵,因此內部節點最多有 個子節點;非根內部節點至少須有
個子節點,也就是至少 個鍵。根節點可例外,因此圖中的根只有一個鍵 ,仍屬合法。
-
分隔鍵的範圍限制:題目明定,分隔鍵左側的資料必須嚴格小於該鍵,右側則大於或等於該鍵。此限制適用於整個子樹,而且必須同時遵守所有祖先節點給定的範圍。
解題方法
原圖的根節點為 ,左子節點為 ,右子節點為 ;虛線框標示的五個選項依序是 (A) 、(B) 、(C) 、(D) 、(E) 。以下以原圖標示作答。
先由根節點往下傳遞範圍:
- 根的左子樹:所有資料必須小於 。
- 根的右子樹:所有資料必須大於或等於 。
- 右側節點 的三個子樹,範圍依序為 、、。
再逐一檢查虛線框中的節點是否符合容量與範圍限制。
選項分析
(A) :違規,應選。
這是非根內部節點,卻只有一個鍵 、兩個子節點,低於「至少兩個鍵、三個子節點」的要求。即使其分隔作用符合局部大小關係,仍因容量不足而違規。
(B) :違規,應選。
此節點位於根 的左子樹,應負責路由小於 的資料,但其中的分隔鍵 將右側子樹限定為大於或等於 ,與祖先要求矛盾:
兩者不能同時成立。原圖右側子葉確實存放 ,因此這個分隔配置違規。
(C) :違規,應選。
第 3 題5 分
- Please find the following table for the characters and their corresponding occurring
probabilities. Please design a Huffman encoding tree and select the correct
descriptions.
Symbol (X) | A | B | C | D | E | F | G
---|---|---|---|---|---|---|---
Prob (X) | 0.15 | 0.06 | 0.24 | 0.21 | 0.09 | 0.21 | 0.03
(A) The codeword length for symbol C is 3
(B) The codeword length for symbol G is 5
(C) The codeword length for symbol E is 5
(D) The codeword length for symbol A is 4
(E) none of above is correct
登入後即可作答並保存紀錄。
核心觀念
霍夫曼編碼每次選取目前權重最小的兩個節點合併,直到形成一棵二元樹。符號的碼字長度,就是從根節點走到該符號葉節點所經過的邊數;機率越小的符號通常會有越長的碼字。
解題方法
依機率由小到大,逐次合併兩個最小權重:
- 合併 與 ,得到權重 。
- 合併 與 ,得到權重 。
- 合併 與權重 的節點,得到權重 。
- 合併 與 ,得到權重 。
- 合併 與權重 的節點,得到權重 。
- 最後合併權重 與 ,形成根節點。
由合併結構可得各符號的碼字長度:
| 符號 | 碼字長度 |
|---|---|
| A | 3 |
| B | 5 |
| C | 2 |
| D | 2 |
| E | 4 |
| F | 2 |
| G | 5 |
例如, 先與 合併,再與 所在的節點合併,之後依序接到權重 的節點、權重 的節點及根節點,因此碼字長度為 。
第 4 題5 分
- (單選) We now use several algorithms to traverse a binary tree. Assuming there
are a total number of N nodes, how many of the following statements about the
worst-case space complexity are TRUE?
• Using DFS to traverse a balanced binary tree takes .
• Using DFS to traverse a binary tree takes .
• Using BFS to traverse a balanced binary tree takes .
• Using BFS to traverse a binary tree takes .
(A) 0
(B) 1
(C) 2
(D) 3
(E) 4
登入後即可作答並保存紀錄。
【答案】(E) 4
詳解
本題考查 DFS/BFS 遍歷二元樹的最差情況空間複雜度,以及 Big-O 的定義: 代表「 的成長不超過 」,是上界而非精確值。
核心觀念
- DFS 的額外空間來自遞迴堆疊(或顯式 stack),最多同時存放「一條根到葉的路徑」,空間為 , 為樹高。
- BFS 的額外空間來自佇列,最多同時存放「某一層(加上下一層一部分)」的節點,空間為 , 為最大層寬。
- 「一般二元樹(a binary tree)的最差情況」是對所有 個節點的二元樹取最糟的那一棵;平衡樹、鏈狀樹、完全二元樹都包含在內。
- 任何 個節點的樹,、,所以 DFS、BFS 的空間在任何情況下都是 。
逐一檢查四個敘述
敘述一:DFS 走平衡二元樹,
平衡樹高 ,DFS 空間為 。而 ,所以 的演算法也滿足 這個上界。→ TRUE
敘述二:DFS 走一般二元樹,
最差的一棵是退化成鏈狀的樹,,遞迴堆疊深度達 ,空間為 。→ TRUE
敘述三:BFS 走平衡二元樹,
完全(平衡)二元樹最底層約有 個節點,佇列會同時存放約 個節點,空間為 。→ TRUE
敘述四:BFS 走一般二元樹,
最差情況要看所有二元樹中最糟的那一棵。鏈狀樹每層只有 1 個節點,對 BFS 反而是最佳情況;完全二元樹也是一般二元樹的一種,佇列需要約 的空間,所以最差情況為 。
第 5 題5 分
- (單選) In a traditional merge sort, two sorted sub-arrays are combined to form a
single, fully sorted array. This is referred to as a 2-way merge. The problem at hand
is to extend this concept by merging N sorted arrays of integers, where N and M are
given integers representing the number of arrays and the number of integers in each
array, respectively. Please choose the correct worst-case time complexity of this
N-way merge.
(A)
(B)
(C)
(D)
(E) none of the above is correct.
登入後即可作答並保存紀錄。
核心觀念
本題考查的核心概念有兩個:
- N-way merge 的演算法設計:如何將 個已排序陣列合併為一個排序陣列,並選擇正確的資料結構。
- Min-Heap(最小堆積)的操作複雜度:Min-Heap 是實作 N-way merge 的標準工具,其
insert與extract-min均為 ,build-heap為 。
關鍵事實整理:
| 操作 | 時間複雜度 |
|---|---|
| Build-heap(對大小為 的堆積) | |
| Extract-min | |
| Insert |
解題方法
問題規模確認
- :陣列個數
- :每個陣列中的整數個數
- 總元素數:
標準演算法:Min-Heap 輔助的 N-way Merge
步驟一:初始化 Min-Heap
從每個陣列各取出第一個元素(共 個元素),連同「來自哪個陣列、目前指向哪個位置」的指標資訊,一起放入 Min-Heap。
步驟二:反覆執行 Extract-min + Insert
每一輪執行:
- 從 Min-Heap 取出最小元素,輸出至結果陣列。()
- 從該元素所在的原始陣列,取出下一個元素,插入 Min-Heap。()
此步驟重複執行,直到所有元素都被取出為止。
步驟三:計算總次數
- 總輸出元素數
- 每次輸出皆需一次 Extract-min 與(最多)一次 Insert,各耗
其中 的 Build-Heap 被 吸收,不影響漸近複雜度。
選項分析
(A)
錯誤。 此式完全忽略了每個陣列內的元素數 對「總輸出次數」的影響。若 極大,此式將嚴重低估複雜度。正確答案中 項(總元素數)是不可或缺的。
(B)
錯誤。 Heap 的大小始終維持為 (Heap 中每個陣列至多貢獻一個元素),因此每次 Extract-min 或 Insert 的複雜度是 ,而非 。將 誤寫為 是本題最常見的錯誤,通常是混淆了「Heap 大小」(由 決定)與「陣列長度」(由 決定)所致。
(C)
錯誤。 此式對應的直觀是「每次從 個陣列的頭端逐一比較取最小值,做 次,每次花 」這種暴力法的錯誤推算(暴力法應為 或類似形式),且 的出現更缺乏根據。此選項與標準 Heap-based 演算法毫無對應關係。
(D)
第 6 題5 分
- The following is a binary tree and the alphabet on the node is simply the "name"
(instead of value) of the node. Please select the correct statements from the following:
🖼️【此處有附圖,請對照原卷】
(A) The successor of node B is E.
(B) The successor of node A is C.
(C) The tree is not a AVL tree.
(D) If we remove node J, the result is an AVL tree.
(E) If we remove node H, the result is an AVL tree.
登入後即可作答並保存紀錄。
核心觀念
二元樹中節點的中序後繼,是中序走訪序列中緊接在該節點之後的節點。若節點有右子樹,後繼是右子樹中最左側的節點。
判斷 AVL 的高度平衡條件時,每個節點都必須滿足:
本題說明字母是節點名稱,因此不以字母順序判斷鍵值大小;以下依圖檢查高度平衡。
解題方法
依圖中的左右子樹做中序走訪,順序為:
因此, 的中序後繼是 ; 的中序後繼是 。
接著由葉節點往上計算高度。刪除前,、、、 的高度各為 , 的高度為 , 的高度為 , 的高度為 ,根節點 的高度為 。各節點左右子樹高度差皆不超過 。
- 刪除 : 變成葉節點,高度為 。此時 的左子樹高度為 、右子樹高度為 ,高度差為 ,不符合 AVL 平衡條件。
- 刪除 : 變成葉節點,高度為 。 的左右子樹高度分別為 、,高度差為 ;其餘節點也都仍符合平衡條件。
選項分析
第 7 題5 分
- (單選) Given a list of binary trees T = {t1, t2, ..., t7} where each node is 0 or 1
shown as below, we would like to insert these trees into a linear-probing hash
table of length N = 11. The hash function , where is the
binary sequence obtained from in-order traversal of tree t and converts a binary
sequence to a decimal number. For instance, . Here's the
question: how many collisions occur during the insertion process?
🖼️【此處有附圖,請對照原卷】
(A) 0
(B) 1
(C) 2
(D) 3
(E) 4
登入後即可作答並保存紀錄。
核心觀念
中序走訪的順序是「左子樹、根節點、右子樹」。將走訪得到的 0、1 序列視為二進位數,先轉成十進位,再對雜湊表長度 取餘數:
線性探測遇到已占用位置時,依序檢查下一格,直到找到空位。
解題方法
依圖中各樹的中序走訪結果計算起始位置:
| 樹 | 中序二進位序列 | 十進位值 | 起始位置 |
|---|---|---|---|
依 到 的順序插入:
- 放在位置 ; 放在位置 ,兩者沒有碰撞。
- 的起始位置 已被占用,往後探測後放在位置 。
- 放在位置 。
- 的起始位置 已被占用,往後探測後放在位置 。
- 的起始位置 已被占用,往後探測後放在位置 。
- 從位置 開始探測,位置 都已占用,最後放在位置 。
第 8 題5 分
- Postfix advantages: What is/are the advantage(s) of using postfix notation for math
expressions?
(A) No need to use parentheses
(B) No need to consider precedence of operators
(C) Easier for human to read
(D) Easier for computers to evaluate
(E) More concise when compared with prefix notation
登入後即可作答並保存紀錄。
第 8 題|Postfix notation 的優點(複選題)
核心觀念
本題考查三種數學運算式表示法(Notation)的基本性質與比較:
| 表示法 | 說明 | 範例() |
|---|---|---|
| Infix(中序) | 運算子在兩運算元之間 | |
| Prefix(前序/波蘭表示法) | 運算子在運算元之前 | |
| Postfix(後序/逆波蘭表示法,RPN) | 運算子在運算元之後 |
Postfix 的核心特性源自以下事實:運算子的「作用對象」已由其在字串中的相對位置完全決定,不需要任何額外的優先順序規則或括號,就能唯一地還原出運算樹(expression tree)。
解題方法
對於「優點比較」類型的選擇題,切入點是對每個選項進行反例法或原理驗證:
- 若能找到一個「使用 postfix 反而不符合該描述」的例子,則該選項錯誤。
- 若能從 postfix 的結構原理出發,說明該描述必然成立,則該選項正確。
Postfix 最關鍵的結構原理:運算子的計算順序由左到右、遇到運算子就取前兩個運算元計算,整個過程僅需一個 stack,不需回頭查詢優先順序,也不需括號。
選項分析
(A) No need to use parentheses ✅ 正確
Infix 在表達非預設優先順序的運算時必須使用括號。例如:
轉為 postfix 後:
括號資訊已被隱含於運算子的位置順序中,完全不需要括號符號。此特性是 postfix(及 prefix)相對於 infix 的最顯著優勢。
(B) No need to consider precedence of operators ✅ 正確
在 infix 表示法中,電腦(或人)讀到 時,必須先查詢「 優先於 」才能決定先計算 。
Postfix 將這個優先順序資訊在轉換階段一次性編碼進字串順序(例如 Dijkstra 的 shunting-yard 演算法),之後執行時只需:
- 從左到右掃描。
- 遇到運算元 → push 進 stack。
- 遇到運算子 → pop 兩個運算元計算,結果 push 回 stack。
整個執行迴圈無需任何優先順序表,因為順序已固定在字串本身。
(C) Easier for human to read ❌ 錯誤
人類從小學習 infix 表示法(),對中序的閱讀是直覺的。
Postfix 對多數人而言反直覺,例如:
讀者需要在腦中維護一個隱形的 stack 才能理解,認知負荷遠高於 infix。因此 postfix 對人類閱讀而言並非較易,反而更難。
(D) Easier for computers to evaluate ✅ 正確
這是 postfix 最廣為人知的優點。電腦只需一個 stack 與一次線性掃描( 時間、 空間)即可完成求值,不需要遞迴、不需要優先順序表、不需要回頭掃描。
第 9 題5 分
- DFS advantages: To find a feasible solution to the eight-queen problem, what is/are
the reason(s) that we prefer to use DFS (depth-first search) instead of BFS (breadth-
first search)?
(A) DFS is more memory efficient.
(B) DFS can be implemented using a stack.
(C) DFS usually reaches the terminal states of "good" or "bad" faster.
(D) DFS is conceptually simpler than BFS.
(E) BFS cannot be implemented using a stack.
登入後即可作答並保存紀錄。
核心觀念
本題考查兩個層面:
-
八皇后問題(Eight-Queen Problem)的本質:在 棋盤上放置 8 個皇后,使任意兩個皇后都不互相攻擊(不同行、不同列、不同對角線)。這是一個典型的約束滿足問題(Constraint Satisfaction Problem, CSP),通常以回溯法(Backtracking)搭配搜尋樹求解。
-
DFS 與 BFS 的特性比較:兩者皆可對搜尋樹進行遍歷,但空間複雜度、到達葉節點的速度,以及與回溯法的相容性截然不同。
搜尋樹的結構:每一層對應棋盤的一行(row),每個節點的子節點對應在該行可放置皇后的各欄(column)。深度為 8 的葉節點即為候選解,必須進一步驗證是否合法。
解題方法
對八皇后問題而言,我們關心的是「找到一個可行解(feasible solution)」,不需要找出所有解,也不需要找最短路徑。分別從記憶體用量、到達終止狀態的速度、實作方式三個角度分析 DFS 與 BFS 的差異:
記憶體比較
設搜尋樹的分支因子(branching factor)為 ,最大深度為 。
| 演算法 | 記憶體用量 |
|---|---|
| BFS | (需儲存當前層所有節點) |
| DFS | (每層只需儲存一條路徑上的節點及其兄弟節點) |
八皇后問題中 ,,BFS 最壞需儲存 個節點,DFS 只需儲存 個節點。
到達終止狀態的速度
DFS 一路向下推進,能迅速抵達深度為 的葉節點(終止狀態),並立即判斷是 "good"(可行解)或 "bad"(違反約束)。BFS 則必須展開整層所有節點後才能推進到下一層,到達葉節點前需處理大量中間節點。
與回溯法的相容性
八皇后的標準解法是 DFS + 回溯(Backtracking):發現某條路徑違反約束時,立即剪枝並退回上一層,大幅降低搜尋空間。BFS 的逐層展開策略與回溯法天然不相容,無法直接利用此剪枝機制。
選項分析
(A) DFS is more memory efficient. ✅ 正確
DFS 使用堆疊(Stack)儲存當前路徑,空間複雜度為 ;BFS 使用佇列(Queue)儲存當前層所有節點,空間複雜度為 。在樹深度固定的情況下,,DFS 的記憶體效率遠優於 BFS。此為選擇 DFS 的重要理由,選項正確。
(B) DFS can be implemented using a stack. ❌ 不構成選擇 DFS 的理由
此敘述本身為真——DFS 確實可以用明確的 Stack 資料結構實作(或利用系統呼叫堆疊的遞迴)。然而,BFS 同樣可以用 Queue 實作,這只是描述 DFS 的實作手段,並非說明為何在八皇后問題中 DFS 優於 BFS 的理由。「可以用 stack 實作」並不構成「應該偏好 DFS」的充分原因,故此選項不正確(不是偏好 DFS 的理由)。
(C) DFS usually reaches the terminal states of "good" or "bad" faster. ✅ 正確
DFS 從根節點一路沿著一條路徑深入,最快在 步內即可抵達葉節點(終止狀態)並判斷其為 "good"(找到可行解,立即回傳)或 "bad"(違反約束,執行回溯剪枝)。BFS 需先完整展開所有深度 的節點才能推進到深度 ,在到達葉節點之前必須處理 個中間節點。因此對於「找到第一個可行解」的目標,DFS 通常更快抵達終止狀態,選項正確。
第 10 題5 分
- Heap applications: Which one(s) of the following applications may involve the use
of heaps to increase time efficiency?
(A) Find the k largest numbers from a given stream of numbers.
(B) Given a stream of numbers coming one after another, calculate the median of
the currently received set of numbers.
(C) Compute the page rank of a given set of web pages
(D) Detect appropriate "buy" and "sell" requests to create transactions in stock
market
(E) Identify the next timing for collisions in event-driven simulation for molecular
dynamics
登入後即可作答並保存紀錄。
第 10 題 Heap 的應用場景
核心觀念
本題考察堆積(Heap)資料結構在各類實際問題中的適用性。需要掌握以下前提知識:
- Heap 的基本性質:完全二元樹,支援 的插入(insert)與刪除最大/最小值(extract-max / extract-min);peek 最大/最小值為 。
- Min-Heap / Max-Heap:分別讓根節點維持最小值或最大值。
- 判斷題目核心:「這個問題是否需要反覆找第 大/小的元素?」或「是否需要維護一個動態有序的優先序列?」若是,Heap 幾乎必然能大幅改善時間複雜度。
解題方法
對每個選項,先問自己兩個問題:
- 這個問題的瓶頸操作是什麼?
- Heap 能否把該操作從 或更差降至 ?
若答案是肯定的,該選項即為正確答案。
選項分析
(A) 從數字串流中找出前 大的數 ✅
演算法設計:
維護一個大小為 的 Min-Heap。對每個新進來的數字 :
- 若 Heap 元素數 ,直接插入。
- 若 Heap 的最小值(即根節點),彈出根節點,插入 。
- 否則捨棄 。
處理完所有串流後,Heap 中剩下的 個元素即為前 大。
時間複雜度分析:
| 方法 | 每個元素的處理成本 | 總時間複雜度 |
|---|---|---|
| 暴力排序 | (排完再取) | |
| Min-Heap(大小 ) |
當 時, 顯著優於 。Heap 有效提升效率,本選項正確。
(B) 動態計算數字串流的中位數 ✅
演算法設計:
使用雙堆法(Two-Heap Method):
- Max-Heap(下半部):存放較小的一半元素,堆頂為下半部最大值。
- Min-Heap(上半部):存放較大的一半元素,堆頂為上半部最小值。
維護不變量(invariant):兩個 Heap 的大小差 ,且 Max-Heap 的堆頂 Min-Heap 的堆頂。
每次插入新元素後,透過至多 2 次的 push/pop 操作恢復不變量;查詢中位數時:
時間複雜度:每次插入 ,每次查詢 。若用陣列排序則插入 ,Heap 顯著更優。本選項正確。
(C) 計算一組網頁的 PageRank ❌
PageRank 的核心演算法為冪次迭代法(Power Iteration):
其中 為轉移矩陣, 為阻尼係數(dampening factor,通常取 ), 為網頁總數。
計算瓶頸在於矩陣向量乘法與迭代收斂,本質是圖的隨機遊走問題。演算法沒有「反覆取最大/最小值」的需求,Heap 對此問題毫無幫助。本選項錯誤。
(D) 在股市中偵測合適的買賣請求以配對成交 ✅
演算法設計(訂單撮合引擎,Order Book Matching Engine):
股市的限價訂單簿(Limit Order Book)維護兩個佇列:
第 11 題5 分
- Properties of dynamic programming (DP): Which of the following statements
is/are correct about DP?
(A) Any DP problem can be visualized as the optimal path finding problem.
(B) Once a DP problem is solved, all the related sub-problems are also solved.
(C) Once a DP problem is solved, it is straightforward to obtain the second-best
solution.
(D) The optimal solution of a DP problem can be obtained using optimal solutions
of its sub-problems.
(E) A DP problem has overlapping sub-problems which are reused several times
when solving the original problem.
登入後即可作答並保存紀錄。
解析
- (A) 並非所有 DP 可化為最短/最優路徑問題;如 0/1 背包、序列比對等不直接是路徑找尋。 → 錯
- (B) DP 在求解最終目標時會自底而上或自頂而下計算所有子問題的最優解,子問題因此已被解出。 → 對
- (C) 取得次佳解通常需額外的狀態或回溯,並非「直接」可得。 → 錯
第 12 題5 分
- Properties of shortest path problem: Which of the following statements, is/are
correct?
(A) Dijkstra's algorithm can be applied to any directed graph with no negative cycle.
(B) The heap data structure is likely to be used in Dijkstra's algorithm.
(C) Floyd-Warshall algorithm can be applied to any directed graph with negative
weights.
(D) Floyd-Warshall algorithm is based on the concept of dynamic programming.
(E) Every shortest path in a weighted directed graph G will not change if an extra
weight is added on every edge of G
登入後即可作答並保存紀錄。
核心觀念
本題測驗最短路徑問題(Shortest Path Problem)中,Dijkstra 演算法 與 Floyd-Warshall 演算法 的適用條件、常用資料結構、設計方法,以及邊權重平移對最短路徑的影響。
各選項分析
-
(A) 錯誤
Dijkstra 演算法採貪婪策略(greedy),假設「一旦頂點被選取並確定最短距離,之後不會再出現更短的路徑」。因此它要求圖中所有邊的權重皆須非負()。即使圖中沒有負環,只要存在負權重邊,已確定的頂點距離仍可能透過後續的負權重邊被進一步縮短,導致演算法得到錯誤結果。 -
(B) 正確
Dijkstra 演算法在每一步都需要從尚未確定的頂點集合中,找出目前距離起點最近的頂點(extract-min),並在鬆弛(relaxation)鄰邊時更新頂點距離(decrease-key)。實務上常使用最小堆積(min-heap)(如二元堆積 binary heap 或費氏堆積 Fibonacci heap)來實作優先佇列,以提升時間效率。 -
(C) 錯誤
Floyd-Warshall 演算法可以處理含有負權重邊的有向圖,但前提是圖中不能存在負權重環(negative-weight cycle)。若圖中存在負環,經過該環的路徑權重可無限減小,最短路徑並不存在;因此「可應用於任何有負權重邊的有向圖」的敘述遺漏了無負環的前提條件,並不正確。 -
(D) 正確
Floyd-Warshall 演算法透過動態規劃(dynamic programming)逐步擴大允許使用的中繼點集合
第 13 題5 分
- Properties of min. spanning tree (MST): Let T'be a MST of a weighted graph G with
at least 3 vertices. Which of the following statements is/are correct?
(A) Given an edge e in G but not in T, we can form a cycle by putting e to T. Then e
has the largest weight among edges in C.
(B) If we partition G into two subsets, and let e be the smallest-weight edge across
the partition. Then e belongs to some MST in G.
(C) The edge with the smallest weight in G must belong to some MST of G.
(D) The edge with the second smallest weight in G must belong to some MST of G.
(E) The edge with the third smallest weight in G must belong to some MST of G.
登入後即可作答並保存紀錄。
核心觀念
本題考察最小生成樹(Minimum Spanning Tree, MST)的三大核心性質:
- Cycle Property(循環性質):對任意非樹邊 ,將 加入 MST 後形成唯一環 ,則 是 中權重最大的邊。
- Cut Property(割集性質):對圖 的任意割(partition),橫跨該割的最輕邊必屬於某棵 MST。
- Kruskal 貪心策略:依邊權由小到大排序,凡不形成環者一律加入。
解題方法
逐一對每個選項套用上述定理,輔以反例檢驗,確認各選項的真假。
選項分析
(A) ✅ 正確
陳述:對 中非樹邊 ,將 加入 後形成環 ,則 是 中權重最大的邊。
理由(反證法):設 , 中 的唯一路徑與 共同構成環 。假設路徑上存在某邊 使得 ,則令
仍是生成樹,且 ,與 是 MST 矛盾。
故對所有 ,皆有 ,即 是 中權重最大(含並列)的邊。(A) 正確。
(B) ✅ 正確
陳述:對 的任意割(partition into and ),橫跨該割的最輕邊 屬於某棵 MST。
理由(置換論證):設 為最輕橫跨邊,但假設某棵 MST 不含 。由於 是生成樹, 中存在一條 的路徑,該路徑必然經過割,故其中存在某邊 橫跨該割。令
仍是生成樹,且因 ,有 。若 , 不是 MST,矛盾;若 ,則 也是 MST 且包含 。
兩種情況下, 皆屬於某棵 MST。(B) 正確。
(C) ✅ 正確
陳述: 中權重最小的邊必屬於某棵 MST。
理由:設全域最輕邊為 。以 與 作割, 是橫跨此割的所有邊中權重最輕者(因為 是全圖最小邊)。由 (B) 的 Cut Property, 屬於某棵 MST。(C) 正確。
(D) ✅ 正確
陳述: 中權重第二小的邊必屬於某棵 MST。
理由(Kruskal 執行分析):設全域最輕邊為 ,第二輕邊為 ,且 (邊權相異情況下)。
執行 Kruskal:
- 加入 :目前生成森林僅含 ,將 連通為一個元件,其餘頂點各自獨立。
- 考慮 :問 是否已在同一元件?由於目前唯一的非平凡連通元件是 ,若 兩端點 也是 ,則 與 為同對端點的兩條邊,這在簡單圖(simple graph)中不可能發生。因此 必然位於不同元件, 不形成環,被 Kruskal 加入。
故 Kruskal 產生的 MST 同時包含 與 。(D) 正確。
第 14 題5 分
- Prefix, infix, and postfix: Which of the following statements is/are correct?
(A) We can use a stack to convert an infix to postfix expression.
(B) We can use a stack to convert a postfix to an infix expression.
(C) We can derive a binary tree from its infix and prefix notations.
(D) We can derive a binary tree from its infix and postfix notations.
(E) We can derive a binary tree from its prefix and postfix notations.
登入後即可作答並保存紀錄。
第 14 題 Prefix、Infix、Postfix 表示式與二元樹
核心觀念
本題同時測驗兩大主題:
- 堆疊(Stack)在表示式轉換中的應用:Infix → Postfix、Postfix → Infix 的演算法。
- 二元樹(Binary Tree)的唯一重建條件:給定不同組合的走訪序列(preorder、inorder、postorder),能否唯一確定一棵二元樹。
關鍵定理如下:
定理:給定一棵二元樹,若已知其 inorder(中序) 走訪序列,再搭配 preorder(前序) 或 postorder(後序) 其中之一,即可唯一重建該樹。
反之,僅知 preorder 與 postorder,無法唯一重建(一般二元樹的情況下)。
解題方法
逐一驗證五個選項即可。以下先整理兩大主題的演算法原理,再套用於各選項。
【主題一:表示式轉換與 Stack】
(Infix → Postfix)
標準的 Shunting-yard 演算法使用一個 Stack 存放運算子,依優先序(precedence)與結合性(associativity)決定何時將運算子從 Stack 彈出並輸出,最後將 Stack 中剩餘運算子全部彈出。全程只需一個 Stack,即可完成轉換。
輸入:a + b * c - d
Stack: [] Output: []
掃到 a → Output: [a]
掃到 + → Stack: [+]
掃到 b → Output: [a, b]
掃到 * → * > +,push。Stack: [+, *]
掃到 c → Output: [a, b, c]
掃到 - → - <= *,pop *;- <= +,pop +;push -。Stack: [-] Output: [a, b, c, *, +]
掃到 d → Output: [a, b, c, *, +, d]
結束,pop -。Output: [a, b, c, *, +, d, -]
結果為 a b c * + d -,正確。可以用 Stack 完成。
(Postfix → Infix)
掃描 Postfix 表示式,遇到運算元(operand)就 push 字串至 Stack;遇到運算子(operator)就 pop 兩個字串,組合成帶括號的 Infix 字串,再 push 回去。
輸入(Postfix):a b c * + d -
掃到 a → Stack: ["a"]
掃到 b → Stack: ["a", "b"]
掃到 c → Stack: ["a", "b", "c"]
掃到 * → pop c, b → push "(b*c)" → Stack: ["a", "(b*c)"]
掃到 + → pop (b*c), a → push "(a+(b*c))" → Stack: ["(a+(b*c))"]
掃到 d → Stack: ["(a+(b*c))", "d"]
掃到 - → pop d, (a+(b*c)) → push "((a+(b*c))-d)"
結果:"((a+(b*c))-d)"
全程使用一個 Stack 即可完成 Postfix → Infix 的轉換。可以用 Stack 完成。
【主題二:給定走訪序列重建二元樹】
(Infix + Preorder)
Preorder 的第一個節點必定是根(root)。找到根之後,在 Inorder 序列中定位根,左側為左子樹、右側為右子樹,遞迴處理即可唯一重建。可以唯一重建。
(Infix + Postorder)
Postorder 的最後一個節點必定是根。同樣在 Inorder 中定位根並切割左右子樹,遞迴重建。可以唯一重建。
(Preorder + Postorder,不含 Inorder)
考慮以下反例:
| 樹型 A | 樹型 B |
|---|---|
| 根為 1,左子為 2 | 根為 1,右子為 2 |
- 樹型 A:Preorder =
[1, 2],Postorder =[2, 1] - 樹型 B:Preorder =
[1, 2],Postorder =[2, 1]
兩棵不同的樹產生完全相同的 Preorder 與 Postorder,故無法唯一重建。不能唯一重建(一般情況)。
補充:僅在所有節點皆為滿二元樹(full binary tree,每個內部節點恰有兩個子節點) 的限制下,Preorder + Postorder 才能唯一確定二元樹。本題未有此假設,故 (E) 不正確。
選項分析
(A) We can use a stack to convert an infix to postfix expression.
✅ 正確。
Shunting-yard 演算法是教科書標準解,使用一個 Stack 存放運算子,線性掃描一遍即可完成轉換,時間複雜度 ,空間複雜度 。
第 15 題21 分
Short Answer Problems (30 points)
15. (21 points) Given k singly linked lists, each of which has n nodes. The numbers in the
nodes of the i-th list are given by , as shown in the figure below. Each
of the k lists has the numbers sorted in non-decreasing order, i.e.,
, where i is the index of the list. Professor Q asks the students to develop an
algorithm to merge these k lists into one singly linked list sorted in non-decreasing
order. Three students have come up with different algorithms, which are given below
in pseudo code.
🖼️【此處有附圖,請對照原卷】
Student A:
L1. Create an empty singly linked list S to collect the result.
L2. DO
L3. Iterate over the numbers in the head nodes of the k lists, and find
the node with the smallest number, denoted as node M.
L4. Remove node M from the original list, and insert it into the result
list S from the tail.
L5. UNTIL all original k lists are empty
L6. Return the result list S, which contains all nodes from the original k
lists and sorted in non-decreasing order.
Student B:
L1. Create an empty singly linked list S to collect the result.
L2. Create an empty min heap H.
L3. Remove the k head nodes from the k lists and insert them into H. Each
node in H also contains the index number i of the list where it is from.
L4. DO
L5. Remove the min node M from the heap. Insert this node into S from
the tail. Take note of the index number stored in M, denoted as m.
L6. If list m is not empty, remove the head node from list m and insert
it into H.
L7. WHILE H is not empty
L8. Return the result list S, which contains all nodes from the original k
lists and sorted in non-decreasing order.
Student C:
Call the recursive divide-and-conquer function MergeTwoGroupLists (k sorted linked
lists). The return value of the function is a list containing all nodes from the original k
lists and sorted in non-decreasing order.
L1. Function MergeTwoGroupLists (m sorted linked lists)
L2. If m==1 then return the only list from the input.
L3. Divide the input m lists into two groups of lists, with and
lists, respectively.
L4. Recursively call MergeTwoGroupLists for each of these two groups of
lists, merging the first group of the lists into one sorted list, S₁, and the
second group of the lists into one sorted list, S₂.
L5. Merge the two sorted lists S₁ and S₂ into one sorted list S.
L6. Return S.
For each of these three algorithms, analyze and give their asymptotic worst-case running
times with the big-O notation and in terms of k and n.
登入後即可作答並保存紀錄。
核心觀念
題目比較三種合併 條已排序鏈結串列的方法。每條串列有 個節點,因此總節點數為 。
分析時分別計算:
- 每個節點被處理幾次。
- 每次尋找或維護最小值需要多少時間。
- 分治合併在每一層需要處理多少資料。
解題方法與各演算法分析
Student A:每次掃描所有串列的表頭
每輪查看 條串列的表頭,找出最小節點,再將該節點移到結果串列尾端。找最小值需要 時間;移除表頭並接到結果串列尾端,使用表頭與尾端指標時各需 時間。
總共有 個節點要移動,因此總時間為:
Student B:使用最小堆積
先將每條串列的表頭放入最小堆積。之後每次取出最小節點,並從該節點所屬的串列補入下一個節點。
堆積最多有 個節點,因此每次取出或插入需要 。總共處理 個節點,時間為 。初始化依題目所述逐一插入 個表頭,需 ,不會改變整體漸近時間:
Student C:分治合併
第 16 題9 分
- (9 points) The pseudo code function to compute the prefix function in the Knuth-
Morris-Pratt (KMP) string matching algorithm is given below. Given a pattern string
P[1..m], the prefix function for this pattern P is the function
such that is the length of the longest prefix of P that is a proper
suffix of , where denotes the q-character prefix of the string P. For pattern string
P=ABACABACABACABAD, how many times is line 7 executed? (Note: only the final
answer will be graded and only fully correct answer will be given points)
COMPUTE-PREFIX-FUNCTION(P)
1 m = P.length
2 let be a new array
3
4 k = 0
5 for q=2 to m
6 while k > 0 and P[k+1] P[q]
7 k =
8 if P[k+1] == P[q]
9 k = k+1
10
11 return
登入後即可作答並保存紀錄。
核心觀念
本題考查 KMP 演算法中「前綴函數(Prefix Function)計算」的執行細節。
前綴函數定義:對於長度為 的模式字串 ,前綴函數 定義為:
即 ( 的前 個字元)的「最長真前綴=真後綴」的長度。
Line 7 的語意:k = π[k] 是在「目前已配對長度 的下一個字元仍然不匹配」時,向後退回(fall-back)到次長的可能配對長度。每執行一次 Line 7,代表發生一次「失敗跳轉」。
解題方法
完整逐步追蹤
模式字串 ,長度 ,以 1-indexed 標記:
| 位置 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| A | B | A | C | A | B | A | C | A | B | A | C | A | B | A | D |
初始化:,。
,
- while:,不進入,Line 7 執行 0 次
- , 不增
,
- while:,不進入,Line 7 執行 0 次
- ,
,,此時
- while:, → 執行 Line 7:,第 1 次
- while:,退出
- , 不增
,
- while:,不進入,Line 7 執行 0 次
- ,
,,此時
- while:,條件為假,不進入,Line 7 執行 0 次
- ,
,,此時
- while:,不進入,Line 7 執行 0 次
- ,
,,此時
- while:,不進入,Line 7 執行 0 次
- ,
,,此時
- while:,不進入,Line 7 執行 0 次
- ,
,,此時
- while:,不進入,Line 7 執行 0 次
- ,
,,此時
- while:,不進入,Line 7 執行 0 次
- ,
,,此時
- while:,不進入,Line 7 執行 0 次
- ,
,,此時
- while:,不進入,Line 7 執行 0 次
- ,
,,此時
- while:,不進入,Line 7 執行 0 次
- ,
,,此時
- while:,不進入,Line 7 執行 0 次