113 年 國立成功大學人工智慧科技碩士學位學程《程式設計(含資料結構與演算法)》
第 1 題3 分
- [3%, 2%] You are given an empty binary search tree (BST).
i) [Step 1] Please successively insert the data pairs containing the following keys 15, 8, 13, 18, 17, 6, 11, 14, 5 into the tree. What is the level of the node containing 5 in the resultant BST?
Note: In a tree, each step from top to bottom is called as level of a tree. The level count starts with 1 and increments by 1 at each level or step.
(A) 1
(B) 2
(C) 3
(D) 4
(E) None of the above
ii) [Step 2] After Step 1 is executed, please delete the node containing 15 from the BST. To delete a nonleaf node, the pair to be deleted should be replaced by the largest pair in its left subtree or the smallest one in the right subtree. Which of the following keys is a suitable key to replace 15?
(A) 8
(B) 13
(C) 17
(D) 18
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
本題考查二元搜尋樹(Binary Search Tree, BST)的:
- 插入規則:左子樹所有鍵值小於節點,右子樹所有鍵值大於節點。
- 節點層級:根節點為第 1 層,往下一層增加 1。
- 非葉節點刪除:以左子樹的最大鍵值,或右子樹的最小鍵值取代被刪節點。
i) 插入後,鍵值 5 的層級
依序插入:
- :成為根節點。
- :插入 的左子樹。
- 且 :插入 的右子樹。
- :插入 的右子樹。
- 且 :插入 的左子樹。
- 且 :插入 的左子樹。
- :插入 的左子樹。
- :插入 的右子樹。
- :插入 的左子樹。
形成的 BST 為:
15 第 1 層
/ \
8 18 第 2 層
/ \ /
6 13 17 第 3 層
/ / \
5 11 14 第 4 層
從根節點到鍵值 的路徑為:
共有四個節點,因此鍵值 位於第 層。
選項分析
- (A) 1:錯。第 1 層只有根節點 。
- (B) 2:錯。鍵值 不在 的直接子節點位置。
- (C) 3:錯。鍵值 的父節點是 ,而 位於第 3 層,因此 位於第 4 層。
- (D) 4:對。
第 2 題1 分
- [1%, 1%, 3%] Considering the data pairs with keys in the given order: 20, 5, 10, 18, 4, 22, 11, 32, 21, as inputs to create a tree.
i) If you create a max heap for them, what is the level of the node containing 5?
(A) 1
(B) 2
(C) 3
(D) 4
(E) None of the above
ii) If you create a min heap for them, which of the following statements is true?
(A) The node containing 11 is in the left subtree of the root.
(B) The node containing 18 is in the right subtree of the root.
(C) The node containing 20 is the root.
(D) The node containing 21 is at level 3.
(E) None of the above.
iii) If you create a symmetric min-max heap for them, which of the following statements is true?
(A) To delete the node containing 5, the node containing 21 is used to replace the deleted node.
(B) There are two nodes at level 4.
(C) The leftmost grandchild of the root is 5.
(D) The keys of the nodes in the right subtree of the root are 32, 10, and 20.
(E) None of the above.
登入後即可作答並保存紀錄。
i) 最大堆
依序以「插入 → 上浮」方式建立:
| 插入次序 | 堆陣列 (1‑基) |
|---|---|
| 20 | [20] |
| 5 | [20,5] |
| 10 | [20,5,10] |
| 18 | [20,18,10,5] |
| 4 | [20,18,10,5,4] |
| 22 | [22,18,20,5,4,10] |
| 11 | [22,18,20,5,4,10,11] |
| 32 | [32,22,20,18,4,10,11,5] |
| 21 | [32,22,20,21,4,10,11,5,18] |
5 位於索引 ,層級 。
【答案】D
ii) 最小堆
同樣以「插入 → 上浮」建立,最終陣列:
第 3 題2 分
- [2%, 3%] Given a binary tree with the following postfix and infix patterns.
postfix: Z, P, C, L, Y, K, S, W, R, Q
infix: Z, Y, P, L, C, Q, K, R, W, S
i) What is the result of level-order traversal for the tree?
(A) C, S, R, Z, L, K, W, Q, Y, P
(B) Q, Y, R, Z, L, K, W, P, C, S
(C) Q, R, Y, W, K, L, Z, S, C, P
(D) Y, K, R, Z, Q, L, W, C, P, S
(E) None of the above
ii) What is the result of preorder traversal for the tree?
(A) Q, Y, Z, L, P, C, R, K, W, S
(B) P, Z, C, Y, L, Q, K, R, W, S
(C) Q, Y, Z, L, P, C, R, K, S, W
(D) P, Z, C, Y, L, Q, K, R, S, W
(E) None of the above
登入後即可作答並保存紀錄。
i) 層序遍歷結果為
Q, Y, R, Z, L, K, W, P, C, S → 選項 (B)
第 4 題2 分
- [2%, 3%] Given the following weighted undirected graph.
🖼️【此處有附圖,請對照原卷】
i) Which of the following statements is correct?
(A) When performing a depth first search of this graph, the next node to visit can be D after visiting A, C, I, N, M, and H.
(B) When performing a breath first search of this graph, the next node to visit can be D after visiting A, C, I, N, M, and H.
(C) When performing a depth first search of this graph, the next node to visit can be K after visiting B, E, and F.
(D) When performing a breath first search of this graph, the next node to visit can be K after visiting B, E, and F.
(E) None of the above
ii) After generating a maximum cost spanning tree from this graph, which of the following statements is correct?
(A) The degree of the node F in the resultant maximum cost spanning tree is 3.
(B) The cost of the resultant maximum cost spanning tree is 118.
(C) (O, P) is an edge of the resultant maximum cost spanning tree.
(D) In the resultant maximum cost spanning tree, the number of edges on the path from A to O is 6.
(E) None of the above
登入後即可作答並保存紀錄。
=== 第 4 題 ===
i) 觀念與推導
- 選項 (A) 與 (B):若從 開始進行 DFS 或 BFS,因 為 的鄰接節點(第一層),在 BFS 中勢必在造訪第二層的 之前即先被造訪;而在 DFS 中,若走 後回溯至 ,此時 與 已無未造訪鄰點,回溯至 時,其未造訪鄰點還有 ,故必須先造訪 才能回溯到 進而造訪 。因此下一節點皆不可能為 。
- 選項 (C):若 DFS 起點為 ,走訪路徑可以為 。此時在 節點上,其未造訪的鄰接節點有 ,因此下一個造訪的節點可以選擇 。此敘述正確。
- 選項 (D):若為 BFS,走訪完 (第 0 層)與 (第 1 層)後,必須先將 的所有鄰接節點( 與 )皆加入佇列並造訪,不可能跳過 直接造訪第 3 層的 。
【答案】:(C)
ii) 觀念與推導
使用 Kruskal 演算法由大到小選取邊以建構最大生成樹(MCST):
- 排序所有邊的權重:, , , , , , , , , , , , , , , , ,
第 5 題3 分
- [3%, 2%] Given the following bloom filter.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
1 1 0 1 1 1 1 1 0 1 0 0 0 1 0
i) Assume that the bloom filter has 3 hash functions fi(k), f2(k), and f3(k):
fi(k) = (3k) mod m,
f2(k) = (2k+1) mod m,
f3(k) = k mod m,
where k represents the key and m is the size of bit array for the bloom filter. Which of the following statements is true?
(A) When inserting k=12, the bits in the bloom filter are not changed.
(B) To delete k=2, the bits at indices 4, 5, and 6 are changed to 0.
(C) When querying the bloom filter for k=15, the answer is "yes."
(D) When querying the bloom filter for k=8, the answer is "yes."
(E) None of the above.
ii) Considering that the elements in the bloom filter is 5. To minimize the false positive probability, what is the ideal optimum number of hash functions?
(A) 15
(B) 5
(C) 3ln(2)
(D) 5ln(2)
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
Bloom filter 使用 個位元與多個雜湊函數:
- 插入元素:將所有雜湊位置的位元設為 。
- 查詢元素:只要其中一個對應位元為 ,必定不存在;全部為 時回答「是」,但可能是假陽性。
- 一般 Bloom filter 不支援刪除:直接將位元設為 ,可能同時影響其他元素。
本題位元陣列大小為:
目前各索引的位元為:
i) 各選項判斷
選項(A)
插入 時:
對應位元為:
- 索引 :原本為
- 索引 :原本為
- 索引 :原本為
因此索引 、 會由 變成 ,位元陣列會改變。
所以(A)錯誤。
選項(B)
對 計算雜湊位置:
正確位置為 、、,不是選項所說的 、、。
此外,一般 Bloom filter 不可直接刪除元素,因為索引 、 的位元可能也是其他元素所設為 。任意清除位元會造成假陰性。
所以(B)錯誤。
選項(C)
對 計算:
需要檢查索引 與 :
所有對應位元皆為 ,因此查詢結果為「是」。
所以(C)正確。
選項(D)
對 計算:
第 6 題5 分
- [5%] Given the following height-based leftist tree.
🖼️【此處有附圖,請對照原卷】
After deleting the minimum element from the leftist tree, which of the following statement is true?
(A) The number of nodes in the right subtree of the root is 7.
(B) The node containing 15 is at level 3.
(C) The shortest path from the root to the external node contains 2 edges.
(D) The node containing 16 is in the right subtree of the root.
(E) None of the above
登入後即可作答並保存紀錄。
根據 leftist tree 的合併與刪除最小元素(Delete-Min)步驟:
- 刪除根節點:刪除根節點 後,需合併左子樹 (以 為根)與右子樹 (以 為根)。
- 合併過程:
- 比較 與 ,因 ,以 為新根,遞迴合併其右子樹(以 為根)與樹 (以 為根)。
- 比較 與 ,因 ,以 為根,遞迴合併其右子樹(以 為根)與樹(以 為根)。
- 比較 與 ,因 ,以 為根,遞迴合併其右子樹(空節點 )與樹(以 為根),結果為 成為 的右子樹。
- 更新 -value(最短外路徑長度)與調整:
- 節點 14:左子樹為 (),右子樹為 ()。不需交換,更新 。
- 節點 9:左子樹為 (),右子樹為 ()。
第 7 題5 分
- [5%] Given an initial empty Fibonacci heap (F heap). After a sequence of insertion operations and delete-min operations, the Fibonacci heap becomes as follows:
🖼️【此處有附圖,請對照原卷】
Then, please proceed to perform operations in the following order: decrease the key 14 by 5, decrease the key 21 by 14, delete the key 12, insert the key 14, delete the minimum key. Considering these operations, which of the following statements is true?
(A) After performing "delete the key 12", the F heap has two min trees.
(B) A cascading cut occurs when performing "decrease the key 14 by 5."
(C) The resultant F heap has 4 min trees.
(D) The resultant F heap has 2 min trees.
(E) None of the above.
登入後即可作答並保存紀錄。
-
初始狀態:
- 根節點為 。其子節點為 、、。
- 的子節點為 、,其中 的子節點為 。
- 的子節點為 。
- 無子節點。
- 此時無任何節點被標記(marked)。
-
操作一:decrease the key 14 by 5
- 減少 變為 。
- 因 (其父節點),將 自 切斷(cut)並移至根 list。
- 失去第一個子節點,標記 ( 變為 marked)。無 cascading cut 發生(選項 (B) 錯誤)。
- 根 list:
[8, 9]
-
操作二:decrease the key 21 by 14
- 減少 變為 。
- 因 (其父節點),將 自 切斷並移至根 list。
- 根 list:
[8, 9, 7]
-
操作三:delete the key 12
- 將 的鍵值降為 。
- 將 自其父節點 切斷並移至根 list。
- 因 先前已被標記,觸發 cascading cut:將 自其父節點 切斷並移至根 list,並將 取消標記(unmarked)。因 為根節點,級聯剪下停止。
第 8 題5 分
- [5%] Given the following red-black tree, where the dark color indicates a black node and white color indicates a red node.
🖼️【此處有附圖,請對照原卷】
After inserting 62, which of the followings is true?
(A)
(B)
(C)
(D)
(E) None of the above.
登入後即可作答並保存紀錄。
在紅黑樹中插入節點 的步驟如下:
-
搜尋與插入:
根據二元搜尋樹的性質,將新節點 插入為 的右子節點,且新插入節點 初始顏色為紅色。 -
第一次修正(Recoloring):
- 此時 的雙親節點 為紅色,發生雙紅衝突(Double Red violation)。
- 的叔叔節點 為紅色。
- 根據紅黑樹規則(叔叔節點為紅色),進行重新著色(Recoloring):
- 將雙親節點 與叔叔節點 改為黑色。
- 將祖父節點 改為紅色。
- 此步驟無須旋轉。
第 9 題5 分
- [5%] Given the following B⁺-tree of order 4 (2-3-4 tree). The capacity of a data node is 3 and a data node has at least two elements.
(A) After inserting the element whose key is 49 into the B⁺-tree, 45 becomes in the root.
(B) After deleting 72 from the B⁺-tree, the number of keys in node E is 2.
(C) After inserting the element whose key is 28 into the B⁺-tree, the key in node C is 16.
(D) After deleting 45 from the B+-tree, the keys in node D are 33, 41, and 45.
(E) None of the above
登入後即可作答並保存紀錄。
B⁺‑Tree 基本規則
- Order = 4 → 每個內部節點最多 3 個鍵,最少
⌈4/2⌉‑1 = 1個鍵。 - 資料節點(葉節點)容量 = 3 → 每個葉節點最多 3 個鍵,最少 2 個鍵。
判斷各選項
| 選項 | 推理 | 結論 |
|---|---|---|
| (A) 插入鍵 49 後,45 成為根節點 | 根節點僅在原根節點 滿(已有 3 個鍵)且 分裂 時才會改變。分裂時提升的是 中間鍵,若提升的是 45,根節點仍會保留原有其他鍵,根不會只剩 45。故 45 不可能單獨成為根。 | 錯誤 |
第 10 題5 分
- [5%] A binary trie can be transformed to Patricia. Considering the following binary trie, which of the Patricia is a correct transformation result?
🖼️【此處有附圖,請對照原卷】
(A) 🖼️【此處有附圖,請對照原卷】
(B) 🖼️【此處有附圖,請對照原卷】
(C) 🖼️【此處有附圖,請對照原卷】
(D) 🖼️【此處有附圖,請對照原卷】
(E) None of the above.
登入後即可作答並保存紀錄。
核心觀念
Patricia 樹會把二元 trie 中只有一個分支的路徑壓縮,只保留真正能區分鍵值的位元位置。沿搜尋路徑,位元位置必須依序遞增;若某條連結回到位元位置較小或相同的節點,就是線索連結,搜尋會在位元位置不再遞增時停止,並核對目前節點的鍵值。
解題方法
原圖的六個鍵值是 0000、0010、0011、1000、1100、1101。以由左至右的第 1 至第 4 位計算,0000 與 0010 在第 3 位不同,0010 與 0011 在第 4 位不同;1000 與 1100 在第 2 位不同,1100 與 1101 在第 4 位不同。根部的第 1 位則區分 0xxx 與 1xxx。
因此,正確的 Patricia 樹必須保留這些分歧位置,並在第 4 位正確區分 1100 和 1101。判斷選項時,可沿各鍵值的搜尋路徑追蹤位元測試,並確認最後停下時核對到的鍵值正確。
選項分析
- (A) 正確。 此圖保留了必要的分歧位元。搜尋
1100時,第 4 位為 0,會回指1100;搜尋1101時,第 4 位為 1,會到達1101。搜尋所得鍵值與輸入相符。
第 11 題10 分
- (10%) Consider three matrices, denoted as , , and . Each of these matrices is partitioned into four submatrices. Assuming that is an exact power of 2, we can guarantee that, for , the dimension is an integer. The subdivisions for matrix yield , , , and . Similarly, matrix is decomposed into , , , and , while matrix undergoes division into , , , and . We have the following procedure:
It is important to note that the procedure for is invoked only once and “” is matrix addition. Let represent the time required to process two matrices using this particular procedure. Provide an asymptotic tight bound () for , assuming that is a constant for sufficiently small .
登入後即可作答並保存紀錄。
核心觀念
本題考查分治演算法的遞迴式與主定理。若每次將問題縮小為原來的 ,產生 個子問題,並在每層額外花費 時間,遞迴式可寫成:
解題方法
逐一計算程序中的遞迴呼叫數,以及矩陣加法的成本:
- 有 2 次遞迴呼叫,兩個結果相加。
- 有 2 次遞迴呼叫,兩個結果相加。
- 有 2 次遞迴呼叫,兩個結果相加。
- 只有 1 次遞迴呼叫,依題意不再補上第二次呼叫。
因此,每次處理 矩陣時,總共有 次、每次規模為 的遞迴呼叫。
前三個子矩陣各需要一次矩陣加法,共 3 次。每次矩陣加法處理 個元素,成本為 。所以遞迴式為:
基底情況為 。
套用主定理,、,而:
第 12 題10 分
- (10%) Consider a neural network with layers of fully connected layers, where the first layer is the input layer, the th layer is the output layer, and the rest are hidden layers. The th layer has neurons, where . Each neuron in the th layer is connected to every neuron in the th layer, where . Let represent the weight of the connection from the th neuron in the th layer to the th neuron in the th layer. Let be the values of neurons in the th layer, where . Then, we can obtain , where . Given that , we want to achieve the result from the input layer to the output layer in the fastest possible way. Please determine the minimum number of multiplications required for this neural network.
登入後即可作答並保存紀錄。
核心觀念
本題考的是矩陣鏈乘法。依題目給定的公式,每一層都是線性運算,因此可將整個網路寫成矩陣與輸入向量的連乘,再選擇乘法順序以降低乘法次數。
兩個維度分別為 與 的矩陣相乘,需要 次乘法。矩陣鏈乘法的動態規劃遞迴式為:
其中 是矩陣鏈的維度序列, 表示將第 個至第 個矩陣相乘所需的最少乘法次數。
解題方法
令 表示由第 層連到第 層的權重矩陣,則整個網路的輸出為:
各矩陣及輸入向量的尺寸依序為:
因此維度序列為 。將六個矩陣依序編號為 ,其中 ,動態規劃結果如下:
| 矩陣區間 | 最少乘法次數 |
|---|---|
第 13 題10 分
- (10%) Given two sequences and , define to be the length of an LCS (longest common subsequence) of the sequences and . Write the recursive formula to compute .
登入後即可作答並保存紀錄。
核心觀念
本題考查最長共同子序列(LCS)的動態規劃。子序列保留原有元素的相對順序,但不要求連續。
令 表示 的前 個元素與 的前 個元素的 LCS 長度。計算時比較兩個前綴的最後一個元素 與 。
解題方法
若其中一個前綴為空,沒有共同子序列,因此:
接著分兩種情況:
- 若 ,這個相同元素可接在 與 的 LCS 後面,長度增加 。
- 若 ,LCS 不會同時以 和 結尾;分別略去其中一個末尾元素,取兩種結果中較大的長度。
因此遞迴公式為:
關鍵程式碼與複雜度
第 14 題10 分
- (10%) Given a sequence of numbers, what is the lower bound for sorting algorithms employing comparison and exchange operations?
登入後即可作答並保存紀錄。
核心觀念
本題考查比較式排序的下界。在只透過元素間的比較來判斷順序時,排序演算法必須能分辨輸入元素的不同排列。交換操作可以改變元素位置,但無法取代比較所提供的順序資訊。
以下假設 個元素互異;若有重複元素,可能排列數會減少。
解題方法
個互異元素共有 種排列。把排序過程表示成一棵決策樹:每次比較是一次分支,葉節點代表演算法判定的一種輸入排列。正確排序必須能區分全部 種排列,因此決策樹至少要有 個葉節點。
若樹的高度為 ,最多有 個葉節點,故:
取以 為底的對數,可得最壞情況比較次數至少為:
第 15 題10 分
- (10%) Find the set of feasible solutions or determine that no feasible solution exists for the following system of difference constraints:
登入後即可作答並保存紀錄。
核心觀念
差分限制式的一般形式為 。可將它表示成有向邊 ,邊權重為 。差分限制式系統可行,等價於其限制圖中沒有負權重環;若能找出一組滿足所有限制的變數值,就能直接證明系統可行。
本題要求的是所有可行解,因此除了確認可行,也要描述各變數之間可取的範圍。
解題方法
由於每個限制只涉及變數的差值,所有變數同時加上相同常數仍是可行解。令 作為基準,並定義
原限制式改寫為
先由 得 。又由 得 ,搭配 可得 。因此
接著, 且 ,所以 。為使 同時滿足 與 ,必須有 。因此