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:對。鍵值 的路徑為 。
- (E) None of the above:錯。正確答案已包含在選項 (D)。
ii) 刪除鍵值 15 時的替代鍵值
鍵值 是根節點,且具有左右子樹,屬於非葉節點。依題意,可選:
- 左子樹中的最大鍵值;
- 右子樹中的最小鍵值。
左子樹如下:
8
/ \
6 13
/ \
11 14
左子樹的最大鍵值是沿著最右路徑找到的 。
右子樹如下:
第 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) 最大堆積(Max Heap)
依序將給定鍵值:20, 5, 10, 18, 4, 22, 11, 32, 21 逐一插入初始為空的最大堆積中(每次插入末尾並執行上浮調整):
- 插入 :
[20] - 插入 :
[20, 5] - 插入 :
[20, 5, 10] - 插入 :置於索引 4,因 ,與 交換
[20, 18, 10, 5] - 插入 :置於索引 5,因 ,不需交換
[20, 18, 10, 5, 4] - 插入 :置於索引 6,因 交換;再與根節點 比較, 交換
[22, 18, 20, 5, 4, 10] - 插入 :置於索引 7,因 ,不需交換
[22, 18, 20, 5, 4, 10, 11] - 插入 :置於索引 8,因 交換;再與父節點 比較, 交換;再與根節點 比較, 交換
[32, 22, 20, 18, 4, 10, 11, 5] - 插入 :置於索引 9,因 交換;再與父節點 比較, 停止
[32, 22, 20, 21, 4, 10, 11, 5, 18]
最終最大堆積之陣列(1-based 索引)為:
各層節點分布如下(根節點為第 1 層):
- 第 1 層(Level 1):索引 1
- 第 2 層(Level 2):索引 2, 3
- 第 3 層(Level 3):索引 4, 5, 6, 7
- 第 4 層(Level 4):索引 8, 9
節點 位於索引 8,其層級為 。故選 (D)。
ii) 最小堆積(Min Heap)
依序將給定鍵值:20, 5, 10, 18, 4, 22, 11, 32, 21 逐一插入最小堆積中:
- 插入 :
[20] - 插入 :置於索引 2,因 交換
[5, 20] - 插入 :置於索引 3,因 不變
[5, 20, 10] - 插入 :置於索引 4,因 交換
[5, 18, 10, 20] - 插入 :置於索引 5,因 交換;再與根節點 比較, 交換
[4, 5, 10, 20, 18] - 插入 :置於索引 6,因 不變
[4, 5, 10, 20, 18, 22] - 插入 :置於索引 7,因 不變
[4, 5, 10, 20, 18, 22, 11] - 插入 :置於索引 8,因 不變
[4, 5, 10, 20, 18, 22, 11, 32] - 插入 :置於索引 9,因 不變
[4, 5, 10, 20, 18, 22, 11, 32, 21]
最終最小堆積之陣列(1-based 索引)為:
檢驗各選項:
- (A) 根節點為索引 1,其左子樹包含索引 ,右子樹包含索引 。節點 位於索引 7,屬於右子樹,敘述錯誤。
- (B) 節點 位於索引 5,屬於左子樹,敘述錯誤。
- (C) 根節點(索引 1)包含的鍵值為 ,並非 ,敘述錯誤。
- (D) 節點 位於索引 9,其層級為 ,並非第 3 層,敘述錯誤。
- (E) 以上皆非,敘述正確。
故選 (E)。
iii) 對稱最小最大堆積(Symmetric Min-Max Heap, SMMH)
1. SMMH 定義與性質
SMMH 是一種雙向優先佇列(DEPQ),其實現為一棵根節點為空(不儲存任何元素)的完整二元樹(Complete Binary Tree)。因此,儲存 個鍵值需使用 個節點(索引 到 ,其中索引 為空根)。
對樹中任一節點 ,滿足以下三項性質:
- P1(兄弟性質): 若 有右兄弟節點,則 。
- P2(祖父左子性質): 若 有祖父節點,則 。
- P3(祖父右子性質): 若 有祖父節點,則 。
2. 逐一插入建構
依序插入 20, 5, 10, 18, 4, 22, 11, 32, 21:
- 插入 20: 置於節點 2(根之左子),無祖父,滿足。
- 插入 5: 置於節點 3(根之右子),其左兄弟為節點 2(值 20),因 違反 P1,交換 節點 2 為 、節點 3 為 。
- 插入 10: 置於節點 4(節點 2 之左子),祖父為節點 1,祖父左右子為節點 2 () 與節點 3 ()。檢查 ,滿足。
- 插入 18: 置於節點 5(節點 2 之右子),左兄弟為節點 4 (),祖父左右子為 。
第 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
登入後即可作答並保存紀錄。
核心觀念
-
二元樹的走訪定義(Binary Tree Traversals):
- 中序走訪(Infix / Inorder):先走訪左子樹,再拜訪根節點,最後走訪右子樹(Left Root Right)。
- 後序走訪(Postfix / Postorder):先走訪左子樹,再走訪右子樹,最後拜訪根節點(Left Right Root)。
- 前序走訪(Preorder):先拜訪根節點,再走訪左子樹,最後走訪右子樹(Root Left Right)。
- 層序走訪(Level-order):依樹的層級由上而下(從根節點開始)、每一層由左至右依序走訪(Breadth-First Search, BFS)。
-
由 Infix 與 Postfix 重建二元樹之原則:
- 後序序列的最末元素必為當前樹(或子樹)的根節點。
- 在中序序列中找到該根節點的位置,其左側所有元素構成左子樹,右側所有元素構成右子樹。
- 根據左、右子樹在中序序列中的節點數量,即可將後序序列劃分為對應的左子樹後序序列與右子樹後序序列,進而遞迴建構出整棵二元樹。
題目推導與二元樹重建
已知給定之走訪序列為:
- Postfix:
Z, P, C, L, Y, K, S, W, R, Q(共 10 個節點) - Infix:
Z, Y, P, L, C, Q, K, R, W, S(共 10 個節點)
步驟 1:確定整棵樹的根節點與左右子樹分割
- 由 Postfix 的最後一個元素可知,整棵樹的根節點為
Q。 - 在 Infix 中定位
Q:
- 對應分割 Postfix:
- 左子樹 Postfix:
[Z, P, C, L, Y](前 5 個元素) - 右子樹 Postfix:
[K, S, W, R](接續 4 個元素)
- 左子樹 Postfix:
步驟 2:重建左子樹(節點集合:)
- 由左子樹 Postfix
[Z, P, C, L, Y]末尾可知,左子樹的根為Y。 - 在左子樹 Infix
[Z, Y, P, L, C]中定位Y:Y的左側為[Z]Z為Y的左子節點。Y的右側為[P, L, C]構成Y的右子樹。
- 重建
Y的右子樹(節點集合:):- 對應 Postfix 為
[P, C, L],其末尾元素為L,故子樹根為L。 - 在 Infix
[P, L, C]中定位L:L的左側為[P]P為L的左子節點。L的右側為[C]C為L的右子節點。
- 對應 Postfix 為
步驟 3:重建右子樹(節點集合:)
- 由右子樹 Postfix
[K, S, W, R]末尾可知,右子樹的根為R。 - 在右子樹 Infix
[K, R, W, S]中定位R:R的左側為[K]K為R的左子節點。R的右側為[W, S]構成R的右子樹。
- 重建
R的右子樹(節點集合:):- 對應 Postfix 為
[S, W],其末尾元素為W,故子樹根為W。 - 在 Infix
[W, S]中定位W:W的左側為空W無左子節點。W的右側為[S]S為W的右子節點。
- 對應 Postfix 為
完整二元樹結構
綜合以上步驟,重建出的二元樹結構如下:
Q (Level 1)
/ \
Y R (Level 2)
/ \ / \
Z L K W (Level 3)
/ \ \
P C S (Level 4)
驗證此樹之走訪序列:
- Infix:,即
Z, Y, P, L, C, Q, K, R, W, S,完全符合。 - Postfix:,即
Z, P, C, L, Y, K, S, W, R, Q,完全符合。
第 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) 圖形走訪分析(DFS 與 BFS)
-
選項 (A):
走訪序列已為 。在 DFS 過程中,路徑為 ;由於 無其他未造訪相鄰節點,故回溯至 ,接著深入造訪 。
造訪完 後,因 的相鄰節點()皆已造訪,回溯至 ,而 亦無其他未造訪鄰點,再次回溯至 。
此時節點 尚有未造訪的相鄰節點 (因為尚未被探索),依 DFS 定則必須優先探索 的未造訪分支,因此下一個被造訪的節點必為 ,不可能立即造訪 。故 (A) 錯誤。 -
選項 (B):
在以 為起點的廣度優先搜尋(BFS)中, 的相鄰節點為 與 (位於第 1 層)。BFS 必須在造訪完所有第 1 層節點後,才會造訪第 2 層節點(如 的鄰點 )。若已造訪至第 2 層及更深層的 ,表示第 1 層的 在更早之前就必須被造訪完畢,不可能在此時才成為下一個造訪節點。故 (B) 錯誤。 -
選項 (C):
以 為起點進行 DFS:- 造訪起點 。
- 唯一的相鄰節點為 ,接著造訪 。
- 的未造訪相鄰節點有 與 ,演算法可合法選擇分支深入造訪 。
- 到達 後,其未造訪相鄰節點有 ,演算法可合法選擇分支深入造訪 。
因此在造訪完 之後,下一個造訪節點確實可以為 。故 (C) 正確。
-
選項 (D):
以 為起點進行 BFS:- 造訪第 0 層節點 , 的鄰點 加入佇列。
- 從佇列取出 (第 1 層),造訪 ,將其相鄰節點 與 (第 2 層)加入佇列。
- 佇列中目前包含 與 。若先取出 造訪(走訪順序 ),此時佇列中仍有同處第 2 層的節點 尚未取出。BFS 必須先造訪完所有第 2 層節點,才能依序造訪第 3 層節點( 的鄰點 )。因此下一個造訪節點必為第 2 層的 ,不可能跳過 直接造訪第 3 層的 。故 (D) 錯誤。
故小題 i) 正確答案為 (C)。
ii) 最大生成樹(MCST)分析
圖中共有 16 個節點( 至 ),生成樹需包含恰好 條邊。
使用 Kruskal 演算法,將所有邊依權重由大至小排序,依序加入不造成環(cycle)的邊:
-
邊權重由大至小排序:
- ,
- ,
- , ,
- ,
- ,
- ,
- ,
-
依序挑選邊:
- 納入
- 納入
- 納入
- 納入
- 捨棄 (與已選邊 形成環)
- 納入
第 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 , , and :
where represents the key and is the size of bit array for the bloom filter. Which of the following statements is true?
(A) When inserting , the bits in the bloom filter are not changed.
(B) To delete , the bits at indices 4, 5, and 6 are changed to 0.
(C) When querying the bloom filter for , the answer is "yes."
(D) When querying the bloom filter for , 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) 是一種空間效率很高的機率型資料結構,用於判斷一個元素是否屬於某集合:
- 基本參數:
- 位元陣列長度為 ,索引範圍為 。
- 使用 個獨立且均勻分布的雜湊函數 。
- 插入元素(Insertion):
- 將欲插入之鍵值 分別代入各雜湊函數,得到索引 。
- 將這些索引位置的位元全設為 。若原本為 則變為 ;原本已為 則維持 。
- 查詢元素(Query):
- 檢查所有對應位元 。
- 若有任一位元為 ,則該元素必定不存在(無假陰性,No False Negatives)。
- 若所有位元皆為 ,則判定元素可能存在,回答「yes」(可能有假陽性,False Positive)。
- 刪除操作(Deletion):
- 標準 Bloom Filter 不支援刪除。因為多個不同元素可能雜湊至相同的位元索引,若直接將某些位元歸零,會導致其他共用該位元的元素在查詢時出現 ,從而產生假陰性(False Negative),破壞 Bloom Filter 的正確性保證。
- 最佳雜湊函數個數:
- 當位元陣列長度為 、集合元素個數為 時,最小化假陽性機率的理論最佳雜湊函數個數為:
- 當位元陣列長度為 、集合元素個數為 時,最小化假陽性機率的理論最佳雜湊函數個數為:
本題中,位元陣列長度 。目前陣列內容如下:
即:
- 值為 的索引:
- 值為 的索引:
三個雜湊函數分別為:
第 i 小題分析
選項 (A)
插入 時,計算各雜湊位置:
檢查當前位元:
- (保持為 )
- (會被設為 )
- (保持為 )
由於索引 的位元由 變更為 ,位元陣列發生改變。因此選項 (A) 敘述錯誤。
選項 (B)
計算 的各雜湊位置:
對應的雜湊索引確實為 。然而:
- 標準 Bloom Filter 並不支援刪除操作。
- 若直接將索引 的位元設為 ,會抹除其他可能共用這些位元的元素資訊,導致後續查詢這些元素時出現假陰性(False Negative)。
因此不能透過將位元設為 來刪除元素,選項 (B) 敘述錯誤。
選項 (C)
查詢 時,計算各雜湊位置:
第 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(新根):左子樹為 (),右子樹為 ()。因左子樹的 -value 小於右子樹(),必須交換左右子樹。
第 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)。因 為根節點,級聯剪下停止。
- 刪除最小節點 ,並進行合併(Consolidate):
- 待合併根節點與度數(degree):(deg 1,子節點為 )、(deg 1,子節點為 )、(deg 0)、(deg 0)。
- 合併 與 (deg 0):因 ,將 作為 的子節點,得 (deg 1)。
第 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):
- 將雙親節點 與叔叔節點 改為黑色。
- 將祖父節點 改為紅色。
- 此步驟無須旋轉。
-
第二次修正(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 不可能單獨成為根。 | 錯誤 |
| (B) 刪除 72 後,節點 E 內鍵數為 2 | 刪除鍵後若葉節點少於 2 個鍵,會向相鄰葉節點 借鍵 或 合併,最終仍保持 至少 2 個鍵。但是否恰好為 2 取決於鄰節點的分布,題目未提供足夠資訊,不能斷言一定是 2。 | 錯誤 |
| (C) 插入鍵 28 後,節點 C 的鍵變為 16 | 插入操作只會 新增 鍵而不會改變已存在鍵的值。 |
第 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。搜尋所得鍵值與輸入相符。 - (B) 錯誤。 圖中的位元測試位置與鍵值實際分歧的位置不符。例如
0000、0010、0011這組鍵值應由第 3 位先區分0000與另外兩個鍵值,再由第 4 位區分0010與0011;選項中的測試位置無法正確完成這兩層區分。 - **(C) 錯誤。
第 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?
登入後即可作答並保存紀錄。
核心觀念
本題考查比較式排序的下界。在只透過元素間的比較來判斷順序時,排序演算法必須能分辨輸入元素的不同排列。交換操作可以改變元素位置,但無法取代比較所提供的順序資訊。
以下假設 個元素互異;若有重複元素,可能排列數會減少。
解題方法
個互異元素共有 種排列。把排序過程表示成一棵決策樹:每次比較是一次分支,葉節點代表演算法判定的一種輸入排列。正確排序必須能區分全部 種排列,因此決策樹至少要有 個葉節點。
若樹的高度為 ,最多有 個葉節點,故:
取以 為底的對數,可得最壞情況比較次數至少為:
由 Stirling 公式,,所以比較式排序的最壞情況時間下界為:
這是漸近下界;合併排序與堆積排序等演算法可在 時間完成,因此達到此下界的漸近階。
第 15 題10 分
- (10%) Find the set of feasible solutions or determine that no feasible solution exists for the following system of difference constraints:
登入後即可作答並保存紀錄。
核心觀念
差分限制式的一般形式為 。可將它表示成有向邊 ,邊權重為 。差分限制式系統可行,等價於其限制圖中沒有負權重環;若能找出一組滿足所有限制的變數值,就能直接證明系統可行。
本題要求的是所有可行解,因此除了確認可行,也要描述各變數之間可取的範圍。
解題方法
由於每個限制只涉及變數的差值,所有變數同時加上相同常數仍是可行解。令 作為基準,並定義
原限制式改寫為
先由 得 。又由 得 ,搭配 可得 。因此
接著, 且 ,所以 。為使 同時滿足 與 ,必須有 。因此
最後,、、 給出
所以所有可行解可用下列方式完整表示: