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) ✅ 正確
第 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) :違規,應選。
第 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
登入後即可作答並保存紀錄。
核心觀念
霍夫曼編碼每次選取目前權重最小的兩個節點合併,直到形成一棵二元樹。符號的碼字長度,就是從根節點走到該符號葉節點所經過的邊數;機率越小的符號通常會有越長的碼字。
解題方法
依機率由小到大,逐次合併兩個最小權重:
- 合併 與 ,得到權重 。
- 合併 與 ,得到權重 。
- 合併 與權重 的節點,得到權重 。
- 合併 與 ,得到權重 。
- 合併 與權重 的節點,得到權重 。
- 最後合併權重 與 ,形成根節點。
由合併結構可得各符號的碼字長度:
第 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
登入後即可作答並保存紀錄。
核心觀念
本題考查 DFS(深度優先搜尋) 與 BFS(廣度優先搜尋) 在遍歷二元樹時的最差情況空間複雜度(worst-case space complexity),以及平衡二元樹(balanced binary tree) 與一般二元樹(general binary tree) 在結構上的本質差異。
關鍵定義與事實:
- DFS 的空間來源:遞迴呼叫堆疊(call stack),其深度等於樹的高度 。空間複雜度為 。
- BFS 的空間來源:輔助佇列(queue),其最大容量等於樹在某一層的最大節點數(即最大寬度 )。空間複雜度為 。
- 平衡二元樹的高度:,因此最大寬度 (最底層可達 個節點)。
- 一般二元樹的高度:最差情況為退化成鏈狀(linked list),,此時每層只有一個節點,寬度 。
解題方法
對每個敘述,分別套用「DFS → 分析高度 」或「BFS → 分析最大寬度 」的框架,再依據樹的類型(平衡 vs. 一般)代入對應的上界。
選項分析
敘述一:Using DFS to traverse a balanced binary tree takes
DFS 的空間 。平衡二元樹的高度 ,因此空間複雜度為 ,不是 。
→ 此敘述為「FALSE」。
敘述二:Using DFS to traverse a binary tree (一般二元樹) takes
DFS 的空間 。一般二元樹在最差情況下退化為一條鏈(每個節點只有一個子節點),高度 ,呼叫堆疊最深達 。因此最差情況空間複雜度確實為 。
→ 此敘述為「TRUE」。
敘述三:Using BFS to traverse a balanced binary tree takes
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)
第 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 的高度平衡條件時,每個節點都必須滿足:
本題說明字母是節點名稱,因此不以字母順序判斷鍵值大小;以下依圖檢查高度平衡。
解題方法
依圖中的左右子樹做中序走訪,順序為:
因此, 的中序後繼是 ; 的中序後繼是 。
接著由葉節點往上計算高度。刪除前,、、、 的高度各為 , 的高度為 , 的高度為 , 的高度為 ,根節點 的高度為 。各節點左右子樹高度差皆不超過 。
第 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 對多數人而言反直覺,例如:
第 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 的理由
第 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 ❌
第 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 在求解最終目標時會自底而上或自頂而下計算所有子問題的最優解,子問題因此已被解出。 → 對
第 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
登入後即可作答並保存紀錄。
核心觀念
本題測驗的是 最短路徑問題的概念與常用演算法的適用條件,包括 Dijkstra、Floyd‑Warshall 兩個演算法的前提、資料結構的使用,以及圖的權重變化對最短路徑的影響。要判斷每個敘述是否正確,需要回顧以下事實:
- Dijkstra 演算法在 沒有負權重的有向圖(或無負環)上才能正確工作。
- Dijkstra 內部常以 優先佇列(heap / binary heap、Fibonacci heap) 來取得目前未確定的最近節點。
- Floyd‑Warshall 演算法是 動態規劃(DP)解法,適用於 任意有向圖,只要 沒有負環(即使有負權重亦可)。
- 若在圖的每條邊上同時增加同一常數 ,所有路徑的長度皆增加 ,最短路徑結構不會改變;但若增量僅加在 每條邊上相同的常數,最短路徑仍保持不變。
(a) (A) Dijkstra's algorithm can be applied to any directed graph with no negative cycle.
說明
Dijkstra 演算法的正確性依賴於「已確定的最短路徑長度永遠不會因為後續的松弛而變小」這一性質。若圖中存在 負權重邊(即使沒有負環),從起點到某些節點的暫時距離可能在之後被更短的路徑抵消,導致已確定的距離被錯誤地固定。因此 Dijkstra 只能在 所有邊重量皆非負 的圖上保證正確;僅保證「沒有負環」不足以排除負權重邊的情況。
結論
此敘述 錯誤。
(b) (B) The heap data structure is likely to be used in Dijkstra's algorithm.
說明
Dijkstra 需要在每一步從「尚未確定的節點集合」中挑選距離最小的節點。最直接的實作是使用 最小堆(min‑heap) 作為優先佇列,使得 extract‑min 與 decrease‑key 操作皆可在 (或 、 於 Fibonacci heap)時間內完成。實務上,大多數教科書與程式庫皆採用二元堆或二叉堆實作此資料結構。
結論
此敘述 正確。
(c) (C) Floyd-Warshall algorithm can be applied to any directed graph with negative weights.
第 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。
第 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 中定位根並切割左右子樹,遞迴重建。可以唯一重建。
第 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:使用最小堆積
先將每條串列的表頭放入最小堆積。之後每次取出最小節點,並從該節點所屬的串列補入下一個節點。
第 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 次
- ,