112 年 國立陽明交通大學資訊工程學系碩士班《資料結構與演算法》
For questions 1 to 16, if a question has multiple answers, then a correct response needs to contain all of them.
第 1 題3 分
Insert the following nodes in sequence into an AVL tree: . Which of the following statements is (are) true?
(A) Node 4 is the root node.
(B) Node 5 and Node 7 are at the same level.
(C) Node 6 is the ancestor of Node 7.
(D) Node 7 has no sibling.
登入後即可作答並保存紀錄。
核心觀念
AVL 樹是二元搜尋樹,且每個節點的左右子樹高度差至多為 。插入節點後,若某個祖先節點失衡,就依新節點相對於失衡節點的位置進行旋轉:
- LL 或 RR:單旋轉。
- LR 或 RL:雙旋轉。
本題依序插入節點,並在每次插入後維持 AVL 平衡。
解題方法
依二元搜尋樹規則插入,遇到失衡時立即旋轉。
- 插入 後,節點 出現 RR 失衡,對 左旋:
5
/ \
4 6
- 插入 ,再插入 。節點 出現 LR 失衡,先對 左旋,再對 右旋:
5
/ \
2 6
/ \
1 4
- 插入 ,它位於根節點 的左子樹中,並落在節點 的右子樹。此時節點 出現 LR 失衡:先對 左旋,再對 右旋:
第 2 題3 分
Given the resultant time complexity of in big-O notation, where in all cases , which of the following statements is (are) true?
(A) Let . Then .
(B) Let . Then .
(C) Let . Then .
(D) Let . Then .
登入後即可作答並保存紀錄。
核心觀念
本題考查遞迴式的漸近分析。式中的 、 表示額外成本的上界;可用反覆代入或遞迴樹估算總成本。若問題符合主定理,也可用主定理判斷。
解題方法與選項分析
(A) 正確
將遞迴式反覆展開:
每展開一層,前一項的係數就乘上 。展開到基底情況後,主要項為 ;各層累積的常數成本也是幾何級數,總量為 。因此 。
(B) 正確
每次遞迴都將問題規模縮小為一半,直到規模降至 ,遞迴深度為 。每層額外成本為 ,且只有一條遞迴分支,所以總成本為 。
(C) 正確
第 3 題3 分
Please refer to the following graph and solve the minimum spanning tree (MST) using Kruskal's algorithm. Which of the following statements is (are) true?
🖼️【此處有附圖,見下方】
(A) The sum of the weights of all edges in the MST is 110.
(B) In the MST, node connects to node through nodes and .
(C) The sum of the weights of all edges connected to node in the MST is 50.
(D) There are 4 edges in total in the MST.
登入後即可作答並保存紀錄。
核心觀念
Kruskal 演算法依邊權重由小到大考慮每條邊;若加入該邊不會形成環,就將它納入最小生成樹(MST)。連通且有 個頂點的生成樹恰有 條邊。
解題方法
圖中有 5 個頂點 ,邊與權重為:、、、、、、。依權重由小到大執行 Kruskal:
- 加入 。
- 加入 。
- 加入 。
- 會與已選的 路徑形成環,因此略過。
- 加入 ,此時 5 個頂點均連通,MST 完成。
第 4 題3 分
Which of the following statements about Kruskal's algorithm and Prim's algorithm is (are) correct?
(A) Kruskal's algorithm focuses on vertices.
(B) Prim's algorithm is better when there are many more edges than vertices.
(C) Kruskal's algorithm cannot be used on a negative-weight undirected graph.
(D) For Prim's algorithm, choosing different vertices as the starting vertex may result in different total weights for the minimum spanning tree.
登入後即可作答並保存紀錄。
核心觀念
Kruskal 與 Prim 都是求連通無向加權圖最小生成樹(MST)的貪婪演算法。MST 會連結所有頂點、沒有環,並使邊權重總和最小。
- Kruskal:依邊權重由小到大考慮各邊,若加入後不會形成環,就加入生成樹;通常搭配並查集判斷兩端點是否已連通。
- Prim:從一個頂點開始,反覆選擇連接「已納入頂點集合」與「尚未納入頂點集合」的最小權重邊,逐步擴張生成樹。
在常見實作下,Kruskal 的時間複雜度為 ;Prim 使用鄰接矩陣時為 ,使用二元堆與鄰接串列時為 。其中 是頂點數, 是邊數。
解題方法
逐項檢查兩種演算法的操作對象、適用權重,以及起點是否影響 MST 的總權重。判斷演算法效能時,依題目常見的比較方式,將稠密圖下 Prim 的 與 Kruskal 的排序成本比較。
選項分析
第 5 題3 分
Insert the following nodes, in order, into a binary search tree: . Which of the following is (are) correct?
(A) Pre-order:
(B) In-order:
(C) Finding node 8 will go through three edges.
(D) Nodes 4 and 9 are at the same level.
登入後即可作答並保存紀錄。
核心觀念
二元搜尋樹(BST)插入新節點時,從根節點開始比較:較小者往左,較大者往右。樹的走訪順序定義如下:
- 前序走訪:根、左子樹、右子樹。
- 中序走訪:左子樹、根、右子樹;BST 的中序結果會由小到大排列。
- 節點深度是從根到該節點所經過的邊數;位於同一層的節點深度相同。
解題方法
依序插入 :
- 插入 ,作為根節點。
- ,插入 的右側。
- ,插入 的左側。
- ,往右到 ;,插入 的左側。
- ,往左到 ;,插入 的右側。
- ,往右到 ;,往左到 ;,插入 的右側。
- ,往右到 ;,往左到 ;,插入 的左側。
因此樹形如下:
第 6 題3 分
The hash function maps an integer key to one of the slots indexed in a hash table of slots. Let . The table is initially empty. After inserting the keys into the table in the given order, which of the following is (are) correct?
(A) With linear probing to handle collisions, key 2 will be in the slot indexed 6.
(B) With quadratic probing to handle collisions, let the th probe position for a value be given by . Then key 2 will be in the slot indexed 2.
(C) With the function to handle collisions, key 2 will be in the slot indexed 2.
(D) With chaining to handle collisions, key 2 will be in the slot indexed 2.
登入後即可作答並保存紀錄。
核心觀念
雜湊函數 先決定鍵值的起始槽位。發生碰撞時,不同處理方式會用不同規則尋找位置:
- 線性探測:依序檢查下一個槽位,索引超過 時回到 。
- 平方探測:依題目給定公式計算後續探測位置。
- 鏈結法:每個槽位對應一條鏈結串列,同一槽位可存放多個鍵值。
依序插入,不能打亂 的順序。
解題方法
各鍵值的起始槽位為:
| 鍵值 | |
|---|---|
| 8 | 1 |
| 12 | 5 |
| 3 | 3 |
| 11 | 4 |
| 1 | 1 |
| 2 | 2 |
以下探測公式中的 從 開始;每個鍵值先檢查 ,若該槽位已被占用,再依公式計算後續位置。
選項分析
(A) 正確。 線性探測時, 分別放入槽位 。鍵值 從槽位 開始,依序檢查 ,最後放入槽位 。接著插入 :槽位 都已被占用,因此放入槽位 。故本選項正確。
(B) 錯誤。 平方探測公式為 ,探測位置以 取模。鍵值 的起始位置是 ;
第 7 題3 分
Which of the following statements is (are) true?
(A) A directed graph can be represented using either an adjacency matrix or an adjacency list.
(B) Breadth-first search (BFS) and depth-first search (DFS) are algorithms for traversing a graph or tree. BFS typically uses a queue to store the nodes that need to be visited, while DFS typically uses a stack.
(C) BFS can be used to find the shortest path between two nodes in a graph.
(D) DFS can be used to determine whether a graph is strongly connected and to check whether a graph contains a cycle.
登入後即可作答並保存紀錄。
核心觀念
本題考圖形的表示法,以及廣度優先搜尋(BFS)和深度優先搜尋(DFS)的用途。
圖形可用鄰接矩陣或鄰接串列表示。BFS 以佇列管理待訪問頂點,適合求無權圖的最短路徑;DFS 通常以堆疊或遞迴實作,可用於圖形遍歷、環路判定及連通性檢查。
解題方法
逐項檢查敘述是否符合圖形演算法的定義與適用條件。判斷最短路徑時,須留意邊是否有權重:BFS 求得的是最少邊數的路徑,適用於無權圖,或所有邊權重相同的情況。判斷強連通性時,則針對有向圖檢查頂點間是否能雙向互達。
選項分析
-
(A) 正確。 有向圖可用鄰接矩陣表示頂點間是否有邊,也可用鄰接串列記錄各頂點的出邊。矩陣適合快速查詢兩點間是否相鄰;串列通常較省空間,尤其適合稀疏圖。
-
(B) 正確。 BFS 與 DFS 都能遍歷圖形或樹。BFS 使用佇列,依層次擴展;DFS 使用堆疊或遞迴,沿一條路徑深入後再回溯。遍歷圖形時,還須記錄已訪問的頂點,避免重複處理。
第 8 題4 分
Refer to the directed weighted graph below, and use Bellman-Ford's algorithm to find the shortest paths from node 0 to any other nodes in the graph. Which of the following statements is (are) true?
🖼️【此處有附圖,見下方】
(A) The total weight of the minimal shortest path is .
(B) The total weight of the maximal shortest path is 10.
(C) The total weight of all shortest paths from node 0 to the other nodes is .
(D) The total weight of the second-to-last minimal shortest path is .
登入後即可作答並保存紀錄。
核心觀念
Bellman–Ford 演算法適用於含負權重邊的有向圖。令 表示起點 到節點 的最短距離,初始化為
對每條邊 ,以權重 進行鬆弛:
本圖共有 個節點,最多進行 輪完整鬆弛;若某輪沒有任何距離更新,即可提前結束。
解題方法
原圖的邊與權重如下;其中兩條斜邊分別是 、權重 ,以及 、權重 ,須注意箭頭方向。
為使計算不受邊的掃描順序影響,採用「第 輪只使用上一輪距離」的方式。此時第 輪表示最多使用 條邊的最短距離:
| 輪數 | ||||||||
|---|---|---|---|---|---|---|---|---|
第 9 題3 分
The following pseudocode sorts the elements of array in ascending order and stores the sorted elements in array . Array has numbers, where . Let . The array index is zero-based. The pseudocode is as follows:
Which instructions should be placed in (a) and (b), respectively?
(A) (a) ; (b)
(B) (a) ; (b)
(C) (a) ; (b)
(D) (a) ; (b)
登入後即可作答並保存紀錄。
核心觀念
本題考查合併排序中的「雙指標合併」。兩個已排序的區段分別由 、 指向目前尚未處理的元素;每次比較兩者,將較小者寫入 ,再移動相應指標。若兩者相等,選哪一邊都能維持非遞減順序,但必須確保所有元素都寫入結果。
解題方法
題目給定的兩段資料完全相同:
一開始有 。要讓左半部的元素依序寫入 ,判斷條件必須在兩者相等時選左邊,因此填入 ,並執行 。
這樣每次迴圈都會將左半部下一個元素寫入 ,直到 。此時主迴圈結束,後面的迴圈再將右半部剩餘元素依序寫入。最後得到:
這正是將 的 個元素依遞增順序排列的結果。此演算法時間複雜度為 ,輸出空間複雜度為 。
選項分析
第 10 題3 分
The following question is about a red-black tree with nodes, where . The node definition is used. For the nodes, the external and internal nodes are included. The function returns the smallest possible integer value that is greater than or equal to . Which of the following statements is (are) true?
(A) There are at least red nodes.
(B) There are at least black nodes.
(C) The height difference between the left and right subtrees of a given internal node is at most one.
(D) It is impossible to delete all the black nodes until the root is deleted.
登入後即可作答並保存紀錄。
核心觀念
依題意, 同時計入內部節點與外部節點(NIL 葉)。紅黑樹的基本性質包括:
- 根節點為黑色,所有外部 NIL 葉也為黑色。
- 紅色節點的子節點必為黑色。
- 從任一節點到其所有後代 NIL 葉的每條路徑,黑色節點數相同。
- 若有 個內部節點,二元樹共有 個外部葉,因此 。
解題方法
先利用外部 NIL 葉的數量判斷黑色節點下限;再用符合紅黑樹規則的反例檢驗紅色節點下限與子樹高度差。根節點必須為黑色,則可直接判斷選項 (D)。
選項分析
(A) 錯。 取最小情況:根為黑色,左右子節點都是黑色的 NIL 外部葉。此時 、紅色節點數為 ,但
因此紅色節點不一定至少有 個。
(B) 對。 設內部節點數為 。外部葉共有 個,而且每個外部葉都是黑色,因此黑色節點至少有 個。由 得
第 11 題3 分
There is a max binary heap with height , where . The height of the root is 0. The pop operation removes an element. After 15 elements are stored in the heap, the new height of the heap is . The function returns the smallest possible integer value that is greater than or equal to . Which of the following statements is (are) correct?
(A) It takes at most pop operations to remove all the nodes.
(B) Let . It takes at most pop operations to remove three levels of the heap.
(C) To remove the second smallest element, it takes pop operations, where is inside .
(D) is smaller than or equal to 2.
登入後即可作答並保存紀錄。
核心觀念
最大二元堆是完全二元樹,根節點高度為 。高度為 的堆有 層,節點數介於 與 之間:
pop 每次移除最大值,也就是根節點,並維持堆的完全二元樹形狀。
解題方法
先以完全二元樹的節點數界限判斷選項,再看 pop 的移除順序。特別要注意:最大堆的 pop 順序是由大到小,並不是依節點所在層數移除。
選項分析
(A) 正確。 高度為 的堆最多有 個節點。每次 pop 移除一個節點,因此移除所有節點所需的操作次數不會超過 。
(B) 錯誤。 若要使堆的高度降低三層,必須移除足夠多的節點,不能只由 次操作保證。以 為例,堆至少有 個節點;高度降至 時,最多只能剩下 個節點,因此至少要移除 個節點。
第 12 題3 分
A min binary heap initially contains one element, 10. The following elements are then stored in the heap in order: . After that, node 5 is deleted. Which of the following statements is (are) correct?
(A) The parent of node 9 is 7, and 8 is the child of 6.
(B) Node 4 is not the child of node 5, and 7 is not the parent of node 8.
(C) The children of node 3 are nodes 4 and 6.
(D) Node 2 does not have a parent, and node 8 does not have any children.
登入後即可作答並保存紀錄。
核心觀念
最小二元堆積是一棵完全二元樹,且每個父節點的鍵值都小於或等於子節點。用陣列由上到下、由左到右儲存時,索引從 開始,索引 的父節點為 ,左右子節點分別為 與 。
插入元素時,先放到陣列末端,再沿著父節點向上調整;刪除指定節點時,以最後一個元素補上空位,再向下調整以恢復堆積性質。
解題方法
依序插入各元素。每次插入都將新元素放在末端,並在它小於父節點時向上交換。插入完成後,陣列為:
刪除值為 的節點。它位於索引 ,以末端的 補上,再與較小的子節點 交換,得到:
因此最後的堆積結構為:
第 13 題3 分
is an ordered sequence of numbers formed by two ordered sequences: and . Let
where . The elements of are stored into an empty stack one by one in the given order. After the elements are popped from the stack one by one, at the th step, is popped from the stack, while is popped from the stack at the th step. Assume that and are two different positive elements of , and . Which of the following statements is (are) correct?
(A) or .
(B) .
(C) must be larger than for any positive integer .
(D) It takes at most pop operations to pop one of the two elements, and .
登入後即可作答並保存紀錄。
核心觀念
元素全部依序推入堆疊後,再逐一彈出,因此彈出順序與推入順序相反。若某元素在推入序列中的位置是第 個,則它在第
次彈出。
題目中的奇數依遞減順序出現,偶數依遞增順序出現。對元素 :
- 若 是奇數,它在奇數序列中的推入位置是 ,所以彈出步數為 。
- 若 是偶數,它在偶數序列中的推入位置是 ,所以彈出步數為 。
解題方法
依 的奇偶性,分別求出 與 的彈出步數。
若 為偶數,、 都是奇數,因此
若 為奇數,、 都是偶數,因此
所以不論 為奇數或偶數, 與 相差 ,且其中一個等於 。
選項分析
第 14 題3 分
The following question is about a binary tree. Denote as a node. The function returns the parent of . Furthermore, and return the left child and right child of node , respectively. and are nodes of the binary tree shown below. Given that
and
what are and ?
🖼️【此處有附圖,見下方】
(A) and .
(B) and .
(C) and .
(D) and .
登入後即可作答並保存紀錄。
核心觀念
本題考二元樹的父節點、左右子節點,以及巢狀函數的運算順序:
- :取得 的父節點。
- 、:取得 的左、右子節點。
- 巢狀函數必須由內向外計算;若途中所需的父節點或子節點不存在,該運算便無法成立。
解題方法
由圖可讀出:根節點為 ,左右子節點分別為 、; 的左右子節點為 、, 的左右子節點為 、。其中 是葉節點,沒有右子節點。
原卷第二式有兩層 ,因此應使用:
一、判斷
第一式要求:
圖中只有節點 的左子節點是 ,所以:
也就是 必須是根節點 的孫節點。圖中符合此條件的 為 、;選項中的候選值則只有 、。
代入 ,依序得到:
因此 符合。 則因沒有右子節點而不符合。
第 15 題3 分
This question considers binary trees with 5 nodes. The preorder and postorder traversal results are stored in the ordered lists and , respectively. Furthermore,
Which of the following binary trees satisfies the condition?
🖼️【此處有附圖,見下方】
登入後即可作答並保存紀錄。
核心觀念
前序走訪(preorder)依序為「根、左子樹、右子樹」;後序走訪(postorder)依序為「左子樹、右子樹、根」。依題目圖中的節點位置判定左右子樹,分別列出兩種走訪序列,再逐項計算同一位置元素的絕對差,確認總和是否為 。
解題方法
圖中四個選項都是含 個節點的二元樹,節點標籤可能重複。令前序序列為 、後序序列為 ,逐項計算 。
- (A) 前序為 ,後序為 。
符合條件。
第 16 題4 分
Initially, array , where . The array index is zero-based. Insertion sort is used to sort the elements of in descending order. Given the following pseudocode, how many times is Line L performed?
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
由於排序方向是遞減,內層條件 X[k] < b 會把比目前元素 小的元素向右移動。因此,Line L 的執行次數等於每次插入時,前面已排序部分中「比 小」的元素數量總和。
解題方法
初始陣列共有 個元素:
外層迴圈依序處理 。
- 當 ,。前面只有 ,不小於 ,所以 Line L 執行 次。
- 當 ,。前面已排序部分為 ,只有 ,Line L 執行 次。
- 當 ,。前面已排序部分為 ,有 小於 ,Line L 執行 次。
一般而言,處理位置 時,前面共有 個元素;其中第一個元素 不小於 ,其餘 個元素都小於 。因此 Line L 在這次插入中執行 次。
總執行次數為:
For questions 17 to 26, make sure your answer is written in a neat and tidy way. Otherwise, it will not be graded.
第 17 題2 分
Consider processing a set of items. The algorithm consists of two parts: the first part takes time, and the second part takes time. What is the total time for processing the set of items? Give your reasons. Use the most precise asymptotic notation to present the time required.
登入後即可作答並保存紀錄。
核心觀念
分段依序執行的總時間等於各部分時間相加。 表示時間上界; 表示時間同時具有 的漸近上界與下界。
解題方法
設第一部分的時間為 ,第二部分的時間為 。總時間為
由 ,存在常數 ,使得當 充分大時,。又因 ,存在常數 ,使得
第 18 題9 分
Linear-time algorithm for the selection problem (the computational problem of finding the th largest number among numbers):
(a) A typical implementation divides the numbers into groups. We then find the medians of the groups, and then find the median of those medians. After time, we can drop a set of numbers because does not contain the answer. Show that , where is the size of . (4%)
(b) Can we divide the numbers into groups instead of groups? Why or why not? (5%)
登入後即可作答並保存紀錄。
核心觀念
本題考的是 median of medians(中位數的中位數)選擇法:先用分組中位數找出一個樞紐,再利用樞紐證明每次分割都能丟掉固定比例的元素。關鍵是估計「有多少組的中位數位於樞紐同一側」,以及每一組因此至少能提供多少個可排除的元素。
以下以元素互異、每組取中位數為前提;最後不足一整組的邊界影響只會帶來常數項,小規模輸入可直接處理。
(a) 每次至少能丟掉 個數
把 個數分成約 組,每組最多 5 個,並取各組中位數。令這些中位數的中位數為樞紐 。
至少有一半的組,其中位數不小於 ;對每一個這樣的五元素組,至少有 3 個數不小於該組中位數,因此也不小於 。同理,至少有一半的組,其中位數不大於 ,這些組各至少提供 3 個不大於 的數。
因此,樞紐兩側各自都能得到約
個元素的保證;扣除樞紐本身與不完整分組造成的常數項後,仍有至少 個元素可排除(小規模情況直接處理)。
依 的目標排名,若答案在 的一側,就排除另一側至少 個元素。於是遞迴選擇的輸入規模至多為 。
(b) 改成每組 7 個是否可行?
第 19 題6 分
Given points on a line, the points are the users who need Wi-Fi signal. Suppose that we have only one kind of Wi-Fi router and we do not care about the number of Wi-Fi routers needed, but we have to minimize the power consumption of the routers. Power consumption is represented by the distance that the signal of a router can reach.
(a) Design a divide-and-conquer algorithm to determine the minimum power consumption. (4%)
(b) What is the time required for your divide-and-conquer algorithm? (2%)
登入後即可作答並保存紀錄。
核心觀念
題目沒有規定路由器必須設在特定位置,也沒有要求一台路由器必須服務多位使用者。依照題目給的模型,路由器的耗電量由訊號可到達的距離決定;因此,將路由器設在使用者所在位置,訊號距離可以是 。
解題方法
令使用者的位置為 。對每位使用者 ,在同一位置安裝一台路由器,並令其訊號半徑為 。此時使用者與路由器的距離為
所以每位使用者都能收到訊號,總耗電量為
訊號半徑不能小於 ,因此總耗電量不可能低於 ;上述配置已達到這個下界,故為最小值。
依照分治法,可將使用者分成兩半,遞迴處理每一半;當子問題只剩一位使用者時,就在該使用者的位置放置半徑為 的路由器。合併時合併兩邊的配置,無須額外調整。
時間複雜度
遞迴式為
共有 個單點子問題,因此建立所有路由器配置需 時間。若只需輸出最小耗電量 ,讀取全部位置也需 時間。
較可能的題意:訊號必須連成一片
字面答案 用不到任何分治,所以出題者較可能的意思是:所有路由器用同一種(同一個訊號距離 ),要讓這條線上的使用者連成一個網路——相鄰兩位使用者之間的空隙必須被訊號覆蓋。在每位使用者旁放一台路由器時,相鄰兩點距離 只要 就能接上,因此
第 20 題8 分
Stack operations are:
- : pushes item into stack .
- : removes the top of stack .
- : answers whether stack is empty.
The following questions are about implementing a FIFO queue by using two stacks and .
(a) (2%) Describe the algorithm (insert item into ) using pseudocode.
(b) (2%) Describe the algorithm (delete an item from ) using pseudocode.
(c) (4%) Show that takes amortized cost.
登入後即可作答並保存紀錄。
核心觀念
用兩個堆疊實作先進先出佇列:新項目先放入 ;需要取出時,若 為空,就把 的項目逐一彈出並推入 ,再從 彈出。這次轉移會反轉項目順序,使最早加入的項目位於 頂端。
解題方法
(a) enQueue(item)
enQueue(item):
S1.push(item)
(b) deQueue()
deQueue():
if S2.isEmpty():
while not S1.isEmpty():
S2.push(S1.pop())
if S2.isEmpty():
error "queue is empty"
return S2.pop()
只有在 為空時才轉移項目。若 尚有項目,直接彈出即可,因為這些項目比 裡的項目更早加入佇列。
(c) 攤銷時間分析
單次 deQueue 可能需要轉移 中的所有項目,因此最壞情況耗時為 。但轉移後,每個項目都留在 ,之後便能直接彈出,不會再次轉移。
用位能法分析。令目前位能為:
第 21 題4 分
Let be an undirected graph. A vertex cover is a vertex subset such that any edge in has at least one endpoint vertex in .
(a) Let be a vertex cover and be a matching for . Prove or disprove that . (2%)
(b) Consider the following graph. Identify a maximum-size matching and a minimum-size vertex cover for it. (2%)
🖼️【此處有附圖,見下方】
登入後即可作答並保存紀錄。
核心觀念
- **匹配(matching)**是兩兩不共用端點的邊集合。
- **頂點覆蓋(vertex cover)**是至少包含每條邊一個端點的頂點集合。
- 在二分圖中,最大匹配大小等於最小頂點覆蓋大小;此外,任何匹配都不可能大於任何頂點覆蓋。
解題方法
從圖中可讀出:左側頂點為 ,右側頂點為 ; 和 都只連到 。可見的邊包括 、、、、、、、。
(a) 命題成立。因為 是頂點覆蓋,每條匹配中的邊都至少有一個端點在 。匹配的邊彼此不共用端點,因此可為每條匹配邊各選一個位於 的端點,而且選到的端點彼此不同。故 至少要有 個頂點:
第 22 題4 分
Consider the residual flow network with a source vertex and a sink vertex given in the following figure. Apply the Ford-Fulkerson algorithm to it.
🖼️【此處有附圖,見下方】
(a) (2%) Write down the augmenting paths used by the Ford-Fulkerson algorithm.
(b) (2%) Identify a minimum - cut for the network.
登入後即可作答並保存紀錄。
核心觀念
Ford–Fulkerson 演算法在殘餘網路中尋找增廣路徑,每次增加的流量為路徑上的最小殘餘容量:
原邊 的容量為 、目前流量為 時,正向殘餘容量為 ,反向殘餘容量為 。走反向殘餘邊,代表減少原邊的流量。若兩節點間原本就有雙向邊,同方向的殘餘容量須合併計算。
當殘餘網路中不存在 路徑時,流量已達最大;由 仍可到達的節點集合可找出最小割。
解題方法
圖上的標記為「流量/容量」: 為 、 為 ;內部邊為 、、、、、。通往匯點的兩條邊為 與 ,其中 的殘餘容量只有 。
初始流量為
(a) 增廣路徑
增廣路徑的選擇不唯一,以下是一組完整的增廣順序:
| 次序 | 增廣路徑 | 當下各邊的殘餘容量 | 增廣量 |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 |
各步的流量更新如下:
-
沿 增廣 。
的殘餘容量包含原邊剩餘的 ,以及取消 流量所提供的 ;本步使用原邊剩餘的 。更新後: -
沿 增廣 。
瓶頸為 的剩餘容量 。更新後:
第 23 題6 分
Let be a sequence of integers, and assume that . For any with , we define to be a peak if .
(a) Identify all peaks in the given sequence . (2%)
(b) Write down a procedure that finds a peak for in time. (2%)
(c) Briefly justify your answer in (b). (2%)
登入後即可作答並保存紀錄。
核心觀念
峰值定義為 。由於題目令 ,序列兩端若不小於唯一的相鄰元素,也算峰值。
(b) 要求在 時間內找到一個峰值,可用二分搜尋的方向判斷:比較中點與右鄰元素,往較高的一側搜尋。
(a) 找出所有峰值
依序檢查各位置:
- ,右鄰為 ,所以不是峰值。
- ,且 、,所以是峰值。
- ,小於左鄰 ,不是峰值;、、 也都小於至少一個相鄰值。
- ,小於右鄰 ,不是峰值。
- ,且 、,所以是峰值。
因此,峰值為 與 。
(b) 找峰值程序
使用二分搜尋,以下索引採 起算:
第 24 題2 分
Let be a complete graph with edge length function . Prove or disprove the statement: The cost of any minimum spanning tree (MST) for is always no more than the cost of any TSP tour for .
登入後即可作答並保存紀錄。
核心觀念
MST 是成本最低的生成樹;TSP tour 是一條經過每個頂點恰好一次並回到起點的 Hamiltonian cycle。移除 tour 上任一條邊,就會得到一棵生成樹。
解題方法
設任一 TSP tour 的邊集合為 ,其成本為
由於 tour 是環,移除其中一條邊 後,剩下的邊仍連通所有頂點,且不含環,因此形成生成樹 。在邊長非負的定義下,,所以
MST 的成本不高於任何生成樹,故
第 25-(a) 題2 分
Consider the Knapsack problem with items and a knapsack size . Let be the sizes and profits of the items, respectively; that is, is the size of the th item and is its profit.
Consider the following simple greedy algorithm:
- , .
- Repeat until :
- Find with the maximum profit-cost ratio, that is,
- Add to if .
- Remove from .
Does this algorithm correctly compute an optimal solution for the Knapsack problem? If so, briefly justify its correctness. If not, provide a counterexample with the smallest number of items.
登入後即可作答並保存紀錄。
核心觀念
這題考的是 - 背包問題中,依「利潤/重量比」排序的貪婪法是否能保證最佳解。- 背包的每件物品只能整件選取或不選;即使某物品的利潤/重量比較高,先選它仍可能占用容量,讓總利潤更高的組合無法放入。
解題方法
給出物品數最少的反例。令背包容量 ,有兩件物品:
| 物品 | 尺寸 | 利潤 | 利潤/尺寸比 |
|---|---|---|---|
| 1 | 2 | 3 | |
| 2 | 3 | 4 |
第 25-(b) 題2 分
For any and with and , define to be the maximum profit obtainable using only the first items and a knapsack of size . Consider the following recurrence formula for :
Suppose that and we have 3 items with size and profit . Use the recurrence to calculate for all and in a table, as shown below. What is the optimal profit?
登入後即可作答並保存紀錄。
核心觀念
這題考的是 0/1 背包動態規劃。每件物品只能選一次,且容量限制為 。
表示只使用前 件物品、背包容量為 時可得到的最大利潤。對第 件物品有兩種選擇:
- 不選:利潤為 。
- 選:剩餘容量為 ,利潤為 。
因此,當 且 時,取兩者較大值。若 ,代表超出容量,設為 ,使超重的選法不會被選中。
解題方法
物品依序為 、、。先令 ,再逐列計算 :
- 只考慮第 1 件物品:容量至少為 才能放入,因此 ;容量 至 時利潤為 。
- 加入第 2 件物品:例如 ;
第 26-(a) 題2 分
Consider the following optimization problem. Alex is a wizard and wants to cast spells to grow the height of a tower. Alex has types of spells, denoted by for , where .
If Alex casts the th type of spell at the beginning of a round, the height of the tower grows by immediately. Then, when it comes to the end of each round for the next rounds, including the round in which Alex casts the spell, the tower height shrinks by .
The effects of the spells can stack. Thus, at the end of each round, the tower shrinks by the sum of for all spells that are still in effect.
Alex can cast at most one spell in each round, and each spell can be cast at most once. Alex can choose any subset of spells and cast them in any order he likes. The tower starts at height zero. The problem is to compute the maximum record height of the tower that Alex can make.
(a) Suppose that and the spells are , , , and . What is the maximum record height of the tower Alex can make?
登入後即可作答並保存紀錄。
核心觀念
本題要求的是全程曾達到的最高塔高。施法在回合開始時立即增高,縮減則發生在回合結束,因此必須記錄「施法後、縮減前」的高度。
若某法術已經過 次回合結束,其對目前塔高的淨貢獻為
因為每次回合結束只會降低塔高,最高紀錄必定出現在某次施法後的瞬間。
解題方法
原頁示意圖以向上箭頭表示立即增高、向下箭頭表示回合結束時縮減;印刷文字明定,縮減的 個回合包含施法當回合。本小題四種法術為 、、、。
一、構造達到 的施法順序
第一回合施放第 種法術,下一回合施放第 種法術:
| 回合 | 回合開始、施法前塔高 | 施放法術 | 施法後立即塔高 |
|---|---|---|---|
| 第 種 | |||
| 第 種 |
第 種法術的縮減量 在第二回合結束時才扣除,因此施法瞬間的 已經成為最高紀錄。
二、證明任何順序都無法超過
第 26-(b) 題3 分
Consider the following optimization problem. Alex is a wizard and wants to cast spells to grow the height of a tower. Alex has types of spells, denoted by for , where .
If Alex casts the th type of spell at the beginning of a round, the height of the tower grows by immediately. Then, when it comes to the end of each round for the next rounds, including the round in which Alex casts the spell, the tower height shrinks by .
The effects of the spells can stack. Thus, at the end of each round, the tower shrinks by the sum of for all spells that are still in effect.
Alex can cast at most one spell in each round, and each spell can be cast at most once. Alex can choose any subset of spells and cast them in any order he likes. The tower starts at height zero. The problem is to compute the maximum record height of the tower that Alex can make.
(b) For any , define the following:
- Let denote the maximum record height of the tower if (i) only the first spells are considered and (ii) spells are in effect when the value is attained.
- Let denote the maximum record height of the tower if (i) only the first spells are considered, (ii) spells, including the th spell, are in effect when the value is attained, and (iii) the th spell is cast in the first round.
and are defined to be if it is not possible. Based on the optimal substructure, write down the recurrence formulas for and , as in Question 25-(b). If needed, you may state explicitly and assume that are sorted in a particular order.
登入後即可作答並保存紀錄。
核心觀念
先把「一個法術對紀錄高度的貢獻」寫清楚。設紀錄高度在某回合 開頭施法後達到;法術 若在 回合施放(),到回合 開頭為止已經歷 次回合結束,它被縮減了 次:
- :法術仍生效中(in effect),貢獻 。
- :法術已失效,貢獻固定為 ,與何時施放無關。
排序(交換論證):把法術依 由大到小排序。生效中的法術應連續排在紀錄回合前的最後 回合(),而且 越大的越晚施放( 越小)。理由:若 而 比 早施放(),交換兩者後總縮減減少 ;就算 因此失效,它的貢獻 也只會更大。失效的法術則放在生效區段之前,必要時空出回合讓它完全失效即可。
因此依排序考慮第 個法術時,它的 是目前最小的:若它生效中,就應放在生效區段的最前面(最早施放,也就是 所說的「第一回合」),。
遞迴式
假設 。