115 年 國立成功大學人工智慧科技碩士學位學程《程式設計(含資料結構與演算法)》
第 1 題3 分
A campus system stores device IDs. Each ID is a 6-bit binary string. Given the following seven 6-bit keys: 000001, 000010, 000011, 001111, 100000, 101011, 111110. Please insert the 7 keys into an empty digital search tree in the order given above. When searching for key 001111, list the keys visited on the search path, in order.
Answer format: key1->key2->...
登入後即可作答並保存紀錄。
解法要點
- 數位搜尋樹(Digital Search Tree, DST)以關鍵字的從左到右位元做分支:第 i 位為 0 走左子樹,為 1 走右子樹。
- 插入時在比對節點的完整鍵後,若有不相同位元,依該位元值向左/右移動,空位即儲存新鍵。
- 搜尋時同樣依位元比較,沿途記錄每一次到達的節點鍵。
插入過程(依序)
| 插入鍵 | 走訪節點 | 新鍵儲存位置 |
|---|---|---|
| 000001 | - | 根節點 |
| 000010 | 000001 →左 | 左子節點 |
| 000011 | 000001 →右 | 右子節點 |
第 2 題4 分
(Continue Question 1) Build the compressed binary trie for the same seven 6-bit keys. For each branch node, define its bitNumber as the bit index used at that node to choose the left or right child. The leftmost bit is index 1 and the rightmost bit is index 6. Perform BFS starting from the root. When visiting a branch node, enqueue the left child before the right child. List the bitNumber of each visited branch node in BFS order.
Answer format: bitNum1, bitNum2, ...
登入後即可作答並保存紀錄。
缺少題目所給的七個 6‑bit 鍵資料,無法唯一確定壓縮二元 trie 的結構與各分支節點的 bitNumber。
第 3 題3 分
A binary tree stores distinct keys. Based on the following postorder and inorder sequences, please list the keys of nodes at level 3 of the tree from left to right. Note that the root is at level 1.
• Postorder sequence: ABCKDIGJHFE
• Inorder sequence: ACBDKEIGFHJ
Answer format: key1, key2, ...
登入後即可作答並保存紀錄。
解題步驟
- 從後序序列最後一個節點取得根:(層 1)。
- 在中序序列中找 ,將序列分成左子樹 與右子樹 。
- 左子樹:後序子序列 ,根為 (層 2)。
- 中序中 左側 ,右側 。
- 左側子樹後序 ,根為 (層 3),左子 、右子 。
第 4 題4 分
For the number of nodes n = 15, what is the maximum possible height of an AVL tree, and what is the maximum possible height of a red-black tree? Note that the root is at level 1.
Answer format: AVL max possible height=?, Red-black tree max possible height=?
登入後即可作答並保存紀錄。
核心觀念
本題的高度以「實際節點的 level 數」計算,根節點位於 level 1;紅黑樹的 NIL 哨兵葉不計入節點數與高度。
- AVL 樹:任一節點的左右子樹高度差不得超過 1。
- 紅黑樹:根為黑色、紅節點不可相鄰,且每條由根至
NIL的路徑具有相同黑高。
解題方法:AVL 樹
令 表示高度為 的 AVL 樹所需的最少節點數。
為了以最少節點達到高度 ,根的兩棵子樹高度必須為 與 ,因此:
逐步計算:
共有 個節點時:
- 高度 至少需要 個節點,不可能。
- 高度 只需至少 個節點,且可以構造出恰好 個節點的 AVL 樹。
例如,根的左右子樹分別取為高度 、含 個節點,以及高度 、含 個節點,則總節點數為:
且左右子樹高度差為 ,符合 AVL 條件。因此 AVL 樹的最大高度為 。
解題方法:紅黑樹
令黑高 表示由根到 NIL 路徑上的黑色實際節點數,不計入 NIL。
黑高為 的紅黑樹至少需要:
第 5 題3 分
Which description best matches what the program does to build the backbone?
(A) It starts from one vertex and repeatedly adds the cheapest edge that leaves the visited vertices.
(B) It sorts all edges by cost, scans them in that order, and adds an edge only if it connects two different groups.
(C) It visits vertices level by level using a queue and records a tree of visits.
(D) It repeatedly selects the closest vertex based on a distance array and updates distances.
(E) It computes shortest paths between every pair of vertices, then selects edges from those paths.
登入後即可作答並保存紀錄。
核心觀念
題目未附所指的程式碼;嚴格而言,僅憑這段題幹無法區分 A 與 B,因為兩者都能建構最小生成樹。以下依題意採用「先排序所有邊,再以群組判斷是否成環」的合理假設作答。
在帶權無向圖 中,生成樹必須:
- 包含所有頂點;
- 保持連通;
- 不含任何環;
- 若有 個頂點,恰有 條邊。
最小生成樹(Minimum Spanning Tree, MST)是在所有生成樹中,使總權重最小者:
本題描述的流程是 Kruskal 演算法。
解題方法
Kruskal 演算法的步驟如下:
- 將所有邊依成本由小到大排序。
- 一開始每個頂點各自形成一個群組。
- 依排序順序掃描每條邊:
- 若邊的兩端屬於不同群組,加入生成樹並合併兩個群組;
- 若兩端已屬於同一群組,加入後會形成環,因此捨棄。
- 加入 條邊後停止。
「不同群組」通常使用 Disjoint Set Union(DSU,並查集)判斷。其正確性來自最小生成樹的割性質:對任意割,跨越該割的最小權重邊都是安全邊,可加入某棵最小生成樹而不破壞最佳性。
關鍵流程可表示為:
T = empty set
for each vertex v:
MakeSet(v)
sort all edges by nondecreasing cost
for each edge (u, v):
if Find(u) != Find(v):
add (u, v) to T
Union(u, v)
if |T| == |V| - 1:
break
排序成本為 ;並查集操作的攤銷時間約為 ,因此總時間複雜度為:
空間複雜度為 ,主要用於儲存邊與並查集。
選項分析
(A) 錯誤
第 6 題3 分
What is the program output for the following input?
Input:
6 11
0 1 4
0 2 3
0 3 6
1 2 5
1 4 2
2 3 7
2 5 8
3 5 1
4 5 9
1 3 10
0 4 11
(A) 16 5
(B) 16 4
(C) 15 5
(D) 15 4
(E) 14 4
登入後即可作答並保存紀錄。
題目資訊說明
題幹未附程式碼,因此依輸入格式與選項設計,採定案假設:程式使用 Kruskal 演算法求無向加權圖的最小生成樹,輸出「最小總權重」與「選取邊數」。
核心觀念
最小生成樹(Minimum Spanning Tree, MST)必須符合:
- 連通所有 個頂點。
- 不含任何環。
- 恰有 條邊。
- 在所有生成樹中,總權重最小。
本題有 個頂點,因此生成樹必須選取
條邊。
Kruskal 演算法將所有邊依權重遞增排列,依序加入不會形成環的邊。
解題方法
邊依權重排序如下:
| 權重 | 邊 |
|---|---|
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 |
依序選取:
- 選 ,權重 。
- 選 ,權重 。
- 選 ,權重 。
- 選 ,權重 。
- 會形成環 ,跳過。
- 選 ,將兩個連通元件接起來,權重 。
此時已選取 條邊,所有頂點均連通:
透過邊 合併。
總權重為:
因此輸出為 。
關鍵程式邏輯
第 7 題3 分
The backbone is the graph formed by the k chosen edges. A vertex is an articulation point if removing it (and its incident backbone edges) disconnects the backbone. For the dataset in Question 6, which set of vertices are articulation points in the backbone?
(A) {0, 1, 4}
(B) {0, 2, 3}
(C) {1, 3, 5}
(D) {0, 1, 3}
(E) No articulation point
登入後即可作答並保存紀錄。
在判斷「骨幹」圖的關節點(articulation point)時,必須先知道第 6 題所選的 條邊形成的骨幹圖的完整結構(頂點與連線關係)。關節點的判定依賴於:若移除該頂點及其所有骨幹邊後,骨幹圖的連通元件數目增加,則該頂點為關節點。
第 8 題3 分
The operator wants one more feature: besides the current output, also print the number of connected components in the input graph (using all m edges). Print three integers in one line: total k c
where c is the number of connected components. Which modification is correct?
(A) Add the following code right before printf(...), and change the printf format:
int c = 0;
for(int i=0;i<n;i++) {
if(f1(i) == i) c++;
}
printf("%lld %d %d\n", total, k, c);
(B) Add the following code right before printf(...), and change the printf format:
int c = 0;
for(int i=0;i<n;i++) {
if(P[i] == -1) c++;
}
printf("%lld %d %d\n", total, k, c);
(C) Add the following code right before printf(...), and change the printf format:
int c = 0;
for(int i=0;i<n;i++){
if (S[i] == 1) c++;
}
printf("%lld %d %d\n", total, k, c);
(D) Add the following code right before printf(...), and change the printf format:
int c = 0;
for(int i=0;i<n;i++) {
c += (P[i] == 1);
}
printf("%lld %d %d\n", total, k, c);
(E) None of the above.
登入後即可作答並保存紀錄。
核心觀念
本題考查並查集(Disjoint Set Union, DSU)計算圖的連通分量數量。
將每個頂點視為一個集合,處理每條邊 時,合併 與 所屬的集合。完成全部 條邊後:
- 同一連通分量中的頂點具有相同代表元。
- 每個集合恰有一個根節點 ,滿足
f1(r) == r。 - 因此,連通分量數為根節點的數量:
其中 表示條件成立時取 ,否則取 。
解題方法
所有邊處理完畢後,逐一呼叫 f1(i) 找出頂點 的代表元。若代表元就是自己,表示 是一個集合的根,連通分量數加一。
正確程式碼為:
int c = 0;
for (int i = 0; i < n; i++) {
if (f1(i) == i)
c++;
}
printf("%lld %d %d\n", total, k, c);
f1 即為並查集的 find 操作。即使呼叫時觸發路徑壓縮,也只會改變樹的結構,不會改變連通分量本身。
選項分析
- (A) 正確。
f1(i) == i正確判定頂點 是否為並查集根節點。每個連通分量恰有一個根,因此計數結果就是 。
第 9 題4 分
You need operations: insert(x), delete(x), find(x), and also "find predecessor/successor of x". Please select all that are correct for the predecessor/successor queries.
(A) A red-black tree supports the queries in O(log n) worst case when the input is a key x.
(B) A binary heap supports the queries in O(log n) worst case when the input is a key x.
(C) A B+ tree supports the queries in O(1) average time when the input is a key x.
(D) An AVL tree supports the queries in O(log n) worst case when the input is a key x.
(E) Hashing supports the queries in O(1) average time when the input is a key x.
登入後即可作答並保存紀錄。
核心觀念
對一組鍵值集合 ,前驅與後繼定義為:
這類查詢不只需要判斷「是否存在鍵值 」,還需要比較所有鍵值的大小關係。因此,資料結構必須具備:
- 能有效找到 的位置;
- 能利用鍵值的順序,找出最接近 的較小值與較大值。
維持全序關係且高度為 的平衡搜尋樹,便能在 worst case 完成此查詢。
解題方法
判斷各選項時,依序檢查:
- 是否維持鍵值的全域排序;
- 找到輸入鍵值 是否需要走訪 的路徑;
- 所謂 是否只是在已知節點位置後成立,而非從鍵值 開始查詢。
在二元搜尋樹中,搜尋 時可以同時記錄候選答案:
- 往右子樹走時,目前節點可能是前驅;
- 往左子樹走時,目前節點可能是後繼;
- 若找到 ,前驅是左子樹中的最大值,後繼是右子樹中的最小值。
整個過程最多沿著樹高走一次,因此時間複雜度為 。
選項分析
(A) Red-Black Tree:正確
Red-Black Tree 是一種維持二元搜尋樹順序的平衡樹,其高度滿足:
更精確地,紅黑樹高度至多約為:
因此,從鍵值 開始搜尋時,可以在 worst case 找到 ,並沿搜尋路徑記錄前驅與後繼候選值。
所以 Red-Black Tree 支援:
故 (A) 正確。
(B) Binary Heap:錯誤
以 min-heap 為例,堆積只保證:
它只保證父子之間的局部關係,不保證整棵樹具有二元搜尋樹的全域排序。不同子樹之間的鍵值沒有完整順序可供搜尋。
因此,若輸入的是任意鍵值 ,要先在 heap 中找到 ,最壞情況可能必須檢查大量節點,時間為:
既然連任意鍵值的定位都無法保證 ,前驅與後繼查詢更無法達到 worst case。
第 10 題4 分
A system needs a priority queue with: frequent meld (A, B), frequent delete-min, and occasional find-max. Please select all that are correct.
(A) A Fibonacci heap is a good match for frequent meld and delete-min.
(B) A leftist heap is a good match for frequent meld and delete-min.
(C) An SMMH directly supports find-max and find-min efficiently.
(D) A Fibonacci heap supports find-max in O(1) without any extra structure.
(E) A leftist heap supports find-max in O(1) without extra structure.
登入後即可作答並保存紀錄。
核心觀念
本題考查不同優先佇列(priority queue)結構對下列操作的支援效率:
meld(A, B):合併兩個優先佇列。delete-min:刪除最小元素。find-min:尋找最小元素。find-max:尋找最大元素。
常見資料結構的操作複雜度如下:
| 資料結構 | meld | delete-min | find-min | find-max |
|---|---|---|---|---|
| Fibonacci heap(以最小堆為主) | 攤銷 | 攤銷 | 通常 | |
| Leftist heap(最小左偏堆) | 通常 | |||
| SMMH | 左右 |
其中,Fibonacci heap 的 meld 為 攤銷時間,原因是只需串接兩個根串列並更新最小值指標。delete-min 則需進行根節點整理與合併,為 攤銷時間。
解題方法
逐一檢查每個選項所描述的資料結構,並將題目要求的操作與該結構的標準複雜度比較:
- 是否適合頻繁
meld? - 是否適合頻繁
delete-min? - 是否原生維護最大值與最小值?
- 若沒有額外結構,是否能在 找到最大值?
只要操作複雜度與資料結構的設計目的相符,即可判定為正確。
選項分析
(A) A Fibonacci heap is a good match for frequent meld and delete-min.
正確。
Fibonacci heap 特別適合需要頻繁合併的情境:
它通常維護一個指向最小元素的指標,因此:
刪除最小元素時,需將其子樹加入根串列,並透過 consolidation 合併相同階數的樹。其攤銷時間為:
因此,Fibonacci heap 適合頻繁執行 meld 與 delete-min。
(B) A leftist heap is a good match for frequent meld and delete-min.
正確。
Leftist heap 的核心操作就是遞迴合併兩棵左偏堆。合併時沿著右鏈進行,而左偏性質保證右鏈長度為 ,因此:
刪除最小元素時,先移除根節點,再合併其左右子樹:
第 11 題4 分
A database index is stored on disk. Minimizing disk I/O is important. Fan-out means the maximum number of children of a node. Please select all that are correct.
(A) A B-tree is suitable for disk because a node can store many keys and child pointers in one disk page/block. This gives high fan-out and small height (few page reads).
(B) A red-black tree is suitable for disk because its fan-out is 2, which reduces the number of disk page reads.
(C) A B+ tree is suitable for disk indexes for the same reason as a B-tree (high fan-out, small height).
(D) A B+ tree is often used when range queries are common, because its leaves can be linked for fast sequential scan on disk.
(E) For disk-based indexes, a red-black tree usually needs more page reads than a B-tree/B+ tree when searching for a key, because the tree is taller.
登入後即可作答並保存紀錄。
核心觀念
本題考查磁碟索引的分支度(fan-out)、樹高與磁碟輸入/輸出(I/O)次數。Fan-out 是節點最多可有的子節點數;搜尋時,樹越矮,通常需要讀取的磁碟頁數越少。
B-tree 與 B+ tree 的節點可在一個磁碟頁中存放多個鍵值及子指標,因此分支度高、樹高低。紅黑樹是二元搜尋樹,分支度至多為 ,一般而言樹高較高。
解題方法
從原卷第 7 頁第 11 題可讀到,題目設定索引儲存在磁碟,重點是減少磁碟 I/O,並將 fan-out 定義為節點的最大子節點數。判斷各選項時,依序檢查資料結構能否提高分支度、降低樹高,以及是否支援快速範圍掃描。
若每個節點對應一次磁碟頁讀取,fan-out 為 的平衡樹搜尋所需讀取頁數約與樹高成正比,而樹高約為 。因此,較大的 fan-out 通常能減少搜尋時的頁面讀取次數。
選項分析
第 12 題2 分
Consider an algorithm with a running time function . Is it mathematically correct to claim that ?
(A) True
(B) False
登入後即可作答並保存紀錄。
核心觀念
大 表示漸近上界。若存在常數 與 ,使得對所有 都有
就能宣稱 。
判斷時,固定常數項不影響漸近成長階;若每一項都能被 的常數倍所界定,整個函數也能被 的常數倍所界定。
解題方法
取 。對常見的對數底數,,因此
此外, 時 ,所以
合併各項:
第 13 題2 分
Consider the famous "Halting Problem", which has been mathematically proven to be undecidable. Because the Halting Problem is strictly harder than any problem in NP, it satisfies the definition of being NP-Hard. Since the Halting Problem satisfies the NP-Hard property, it is therefore correctly classified as an NP-Complete (NPC) problem.
(A) True
(B) False
登入後即可作答並保存紀錄。
核心觀念
本題考查 NP-hard 與 NP-complete 的差別,以及「不可判定」對問題分類的影響。
- NP-hard(NP 困難):對每個 ,都存在多項式時間多對一歸約,將 歸約到問題 。也就是說, 至少和所有 NP 問題一樣困難。NP-hard 問題本身不必屬於 NP,也不必可判定。
- NP-complete(NP 完全):問題必須同時符合以下兩項:
- 是 NP-hard
因此,NP-hard 是 NP-complete 的必要條件,但單有 NP-hard 還不夠。
解題方法
判斷此題時,先看停機問題是否屬於 NP。停機問題問的是:給定程式 與輸入 , 是否會在 上停機。它已被證明為不可判定問題,也就是不存在一個演算法,能對所有輸入都在有限時間內正確回答。
第 14 題2 分
If a decision problem belongs to the class NP, it implies that given a specific candidate solution, there exists a deterministic algorithm that can verify whether this solution is correct in polynomial time.
(A) True
(B) False
登入後即可作答並保存紀錄。
核心觀念
本題考查複雜度類別 NP 的定義。對於一個判定問題,若輸入答案為「是」,存在一份長度受輸入大小多項式限制的證明(也稱候選解或憑證),且可由確定性演算法在多項式時間內驗證,就可用「多項式時間驗證器」描述該問題屬於 NP。
形式上,判定問題 屬於 NP,表示存在確定性多項式時間驗證器 ,使得對輸入 :
其中 是候選解, 表示驗證器確認候選解有效。
解題方法
第 15 題2 分
The 0/1 Knapsack Problem can be solved optimally by using a Greedy strategy that always selects the item with the highest value-to-weight ratio first.
(A) True
(B) False
登入後即可作答並保存紀錄。
核心觀念
0/1 背包問題中,每件物品只能「整件選取」或「完全不選」,不能拆分。若物品 的重量為 、價值為 ,背包容量為 ,目標是在總重量不超過 的條件下,最大化總價值。
依「價值重量比」 由高至低選取,是分數背包問題的最佳貪婪策略;在 0/1 背包中,物品不能拆分,這項策略不保證得到最佳解。
解題方法
用反例檢驗題目所稱的策略是否對所有輸入都能得到最佳解。設背包容量為 ,有以下三件物品:
| 物品 | 重量 | 價值 | 價值重量比 |
|---|---|---|---|
| 1 | 10 | 60 | 6 |
| 2 | 20 | 100 | 5 |
| 3 | 30 | 120 | 4 |
第 16 題2 分
Suppose a problem called "Robot-Path-Planning" is a known NP-Complete problem. To prove that a related new problem "3-SAT" is also NP-Complete, we must demonstrate that "3-SAT" is in NP, and we must construct a polynomial-time reduction from "3-SAT" to "Robot-Path-Planning".
(A) True
(B) False
登入後即可作答並保存紀錄。
核心觀念
判斷 NP 完全問題時,必須同時證明:
- 屬於 NP:對於問題的「是」實例,若有人提供一份解答證明,能在多項式時間內驗證。
- 具有 NP-hard 性:將一個已知的 NP 完全問題,在多項式時間內歸約到待證問題。
若已知問題 是 NP 完全,欲證明新問題 是 NP 完全,正確方向是構造:
也就是把 的任一實例轉換成 的實例,並保留答案。這能證明 至少和 一樣難,因此 是 NP-hard;再加上 ,即可得出 是 NP 完全。
解題方法
題目已知 Robot-Path-Planning 是 NP 完全,並要證明 3-SAT 也是 NP 完全。
因此要證明 3-SAT 的 NP-hard 性,應從已知的 NP 完全問題出發,建立:
題目卻要求建立相反方向:
第 Part II-1 題10 分
Assuming that you are a medical information analyst, currently analyzing an algorithm designed to process a 3D MRI volume. The input is a cubic volume of data with side length (total voxels ). Assume is a power of 2.
Pseudo-code:
Surface_Scan()
total_intensity = 0- For to :
- For to :
total_intensity = total_intensity + Read_Pixel(i, j)
- For to :
- Return
total_intensity
MRI_Voxel_Process()
- If , return.
- For to :
Surface_Scan(n)
- For to :
- For to :
- For to :
MRI_Voxel_Process(n/2)
- For to :
- For to :
Assume that the function Read_Pixel(i, j) takes constant time.
Questions:
(1) [3%] Analyze the Surface_Scan(n) function and determine its time complexity using -notation.
- Time complexity: ____
(2) [3%] Write down the recurrence relation for the running time of MRI_Voxel_Process(n).
- ____
(3) [4%] Analyze the asymptotic growth rate of the algorithm, and determine the overall asymptotic tight bound () for the running time of MRI_Voxel_Process(n). Please write down your calculation process.
- Calculation process: ____
- Overall asymptotic tight bound: ____
登入後即可作答並保存紀錄。
核心觀念
本題考查巢狀迴圈的時間複雜度、遞迴式建立,以及主定理與遞迴樹分析。
- 每次迴圈執行的工作量,等於執行次數乘上每次工作的時間。
- 遞迴函式的總時間,需加總目前層的非遞迴工作,以及所有子問題的遞迴時間。
- 主定理適用於 ,比較 與 的成長率。
解題方法與推導
(1)分析 Surface_Scan(n)
外層迴圈執行 次,內層迴圈執行 次。每次 Read_Pixel(i, j) 花費常數時間,因此總操作次數為:
是常數,依 記號可省略常數因子。
時間複雜度:
(2)建立 MRI_Voxel_Process(n) 的遞迴式
每次呼叫中,Surface_Scan(n) 被執行 次。由(1)可知,單次掃描花費 ,因此這部分總共花費:
三層迴圈分別讓 取 或 ,因此共呼叫 次 MRI_Voxel_Process(n/2)。故遞迴式為:
(3)求整體緊確界
第 Part II-2 題10 分
You are a process engineer managing a critical Lithography Machine. The machine has a strict thermal constraint: You cannot schedule production for two consecutive shifts. If Shift is active, Shifts and must be idle (cooling).
The estimated Production Yield Value for the upcoming 13 shifts is listed below.
| Shift ID | S1 | S2 | S3 | S4 | S5 | S6 | S7 | S8 | S9 | S10 | S11 | S12 | S13 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | 45 | 95 | 45 | 35 | 85 | 45 | 40 | 85 | 65 | 65 | 40 | 35 | 90 |
Questions:
(1) [4%] Identify the set of shifts that results in the maximum possible total value while satisfying the thermal constraint.
- Selected Shifts: ____ (e.g., S1, S2, …)
- Total Value: ____
(2) [4%] A very important client demands that Shift 7 (Value: 40) MUST be included in the schedule. Under this mandatory condition, what is the maximum total value you can achieve for the 13 shifts?
- Selected Shifts with S7: ____
- Total Value with S7: ____
(3) [2%] Process Upgrade: The High-Temperature Machine Imagine the factory installs a new, high-performance machine. This machine generates significantly more heat, so the safety constraint is updated: After any active shift, the machine must cool down for TWO consecutive shifts.
Let be the maximum value achievable considering shifts up to , and let be the value of Shift . Please write down the complete Recurrence Relation (Mathematical Equation) for:
- : ____
登入後即可作答並保存紀錄。
核心觀念
本題考察「相鄰班次不能同時選取」的最大權重獨立集,可用動態規劃求解。令 表示只考慮前 個班次時可得到的最大總值。對原本的冷卻限制,考慮第 班次時有兩種情況:不選它,或選它並跳過第 班次,因此
解題方法
圖中列出的 13 班產值依序為 ;原限制是任意兩個連續班次不可同時排產。升級題則改為每個排產班次之後須連續冷卻兩班。
(1) 不附加條件時的最大總值
令 ,依照上述遞迴式逐班計算:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 45 | 95 | 95 | 130 | 180 | 180 | 220 | 265 | 285 | 330 | 330 | 365 | 420 |
從 回溯:選 S13;S12 不選;選 S10;選 S8;S6 不選;選 S5;S3 不選;選 S2。因此排程為 S2、S5、S8、S10、S13。
(2) 強制包含 S7
選 S7 後,S6、S8 必須停機。剩下的班次分成 S1–S5 與 S9–S13 兩段,可分別求最大值:
第 Part II-3 題10 分
You are analyzing the routing latencies for a constellation of 4 satellites (Nodes A, B, C, D). Below is the adjacency list with the latency between specific nodes:
- A B: 10
- A C: -5
- C B: 4
- B D: 5
- D C: 2
- D A: 3
Requirement: You need to analyze the latency characteristics of the network so that shortest communication paths can be efficiently determined when needed.
Questions:
(1) [4%] Based on the specific properties of the edges in this graph and the problem requirement, identify the single most appropriate standard algorithm to use. Explain your choice by pointing out the specific graph feature that disqualifies other algorithms, and explicitly state why at least one other commonly used shortest-path algorithm would fail or be inappropriate in this case.
- Algorithm: ____
- Why choosing this algorithm? ____
(2) [2%] Let be the initial distance matrix used at the start of your chosen algorithm. Please construct and write down clearly.
(Note: Use for non-existing edges and for self-loops. Order: A, B, C, D.)
- ____
(3) [4%] After the algorithm completes execution:
(a) [2%] What is the final shortest path cost from Node D to Node B? Please trace the path logic (e.g., ).
- Final shortest path cost: ____
- Final path logic: ____
(b) [2%] State the asymptotic Time Complexity of this algorithm.
- Time Complexity: ____
登入後即可作答並保存紀錄。
核心觀念
本題考含負權重邊的全點對最短路徑。Floyd–Warshall 演算法逐一允許各節點作為中繼點,更新所有節點對的最短距離:
演算法可處理負權重邊,前提是圖中沒有負權重環。
解題方法
依原卷圖,圖是有向圖,邊為 、、、、、;其中 是負權重邊。題目要求需要時能查詢網路中的最短路徑,因此使用全點對最短路徑演算法。
- 演算法: Floyd–Warshall。
- 選擇理由: 負權重邊使 Dijkstra 演算法不適用;Dijkstra 仰賴已確定最短距離的節點不再被更短路徑改善,負權重邊可能破壞這項條件。Bellman–Ford 可處理負權重邊,但主要解單一起點的最短路徑;本題要分析整個網路各節點間的路徑,Floyd–Warshall 更直接。
- 初始距離矩陣: 對角線為 ,有邊處填入權重,沒有邊處填入 。
第 Part II-4 題10 分
You are optimizing the core engine of an image editing tool called "Smart Lasso". The tool helps users extract objects by automatically finding the best boundary between a Start Pixel () and a Target Pixel (). The image is represented as a grid of pixels. Each pixel has an associated "Energy Cost" (representing high-frequency noise or weak edges). Here are some rules:
- Movement Rule: From any pixel, you can move to adjacent pixels (Up, Down, Left, Right). Diagonal movement is not allowed.
- Path Cost: The sum of the Energy Costs of all pixels visited on the path (excluding , but including ).
The energy cost matrix is shown below (assume the cost of entering is 0):
In the following, we will use (row, col) as coordinate representation. Top-left is .
If two neighbors have the same cost, prioritize the direction in this order:
Right Down Up Left
Questions:
(1) [4%] A junior developer implemented a "High-Speed Mode" for the lasso. The logic is simple:
"At every step, the cursor simply snaps to the immediate neighbor with the lowest energy cost to get closer to the target. It does not plan ahead."
Please trace the path generated by this method from to and calculate the total cost.
- Path Sequence: ____ (e.g., )
- Total Path Cost: ____
(2) [4%] Users have complained that the "High-Speed Mode" (from Q. 4.1) often gets "trapped" in noisy areas or takes expensive detours, failing to find the true object boundary. Your manager asks you to rewrite the core engine. The new requirement is:
"The tool must guarantee finding the mathematically optimal boundary—the path with the absolute minimum Total Cumulative Cost from Start to Target—even if it takes a bit more computation time."
Please trace the path generated by this method from to and calculate the total cost.
- Path Sequence: ____ (e.g., )
- Total Path Cost: ____
(3) [2%] The solution in Q. 4.2 works great on small grids. However, when applied to a 4K Image (with pixels), the tool becomes laggy. Profiling reveals the bottleneck is in the Open Set (Priority Queue)—specifically, the operation of repeatedly finding and removing the pixel with the minimum cumulative cost. You are comparing two underlying data structures for this Priority Queue: a simple Unsorted Array vs. a Binary Min-Heap. Please analyze the Time Complexity (Big-O) for the "Extract-Min" operation in both cases:
- Case 1: You use a simple Unsorted Array to store the candidate pixels; the time complexity for Extract-Min is: ____
- Case 2: You switch to a Binary Min-Heap to store the candidate pixels; the time complexity for Extract-Min is: ____
登入後即可作答並保存紀錄。