113 年 國立成功大學人工智慧科技碩士學位學程《程式設計(含資料結構與演算法)》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題3 分

  1. [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. 插入規則:左子樹所有鍵值小於節點,右子樹所有鍵值大於節點。
  2. 節點層級:根節點為第 1 層,往下一層增加 1。
  3. 非葉節點刪除:以左子樹的最大鍵值,或右子樹的最小鍵值取代被刪節點。

i) 插入後,鍵值 5 的層級

依序插入:

  • 1515:成為根節點。
  • 8<158<15:插入 1515 的左子樹。
  • 13<1513<15 且 13>813>8:插入 88 的右子樹。
  • 18>1518>15:插入 1515 的右子樹。
  • 17>1517>15 且 17<1817<18:插入 1818 的左子樹。
  • 6<156<15 且 6<86<8:插入 88 的左子樹。
  • 11<15, 11>8, 11<1311<15,\ 11>8,\ 11<13:插入 1313 的左子樹。
  • 14<15, 14>8, 14>1314<15,\ 14>8,\ 14>13:插入 1313 的右子樹。
  • 5<15, 5<8, 5<65<15,\ 5<8,\ 5<6:插入 66 的左子樹。

形成的 BST 為:

              15              第 1 層
             /  \
            8    18            第 2 層
           / \   /
          6  13 17             第 3 層
         /   / \
        5   11 14              第 4 層

從根節點到鍵值 55 的路徑為:

15→8→6→515\rightarrow 8\rightarrow 6\rightarrow 5

共有四個節點,因此鍵值 55 位於第 44 層。

選項分析

  • (A) 1:錯。第 1 層只有根節點 1515。
  • (B) 2:錯。鍵值 55 不在 1515 的直接子節點位置。
  • (C) 3:錯。鍵值 55 的父節點是 66,而 66 位於第 3 層,因此 55 位於第 4 層。
  • (D) 4:對。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題1 分

  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 位於索引 88,層級 =⌊log⁡28⌋+1=4= \lfloor\log_2 8\rfloor+1=4。
【答案】D


ii) 最小堆

同樣以「插入 → 上浮」建立,最終陣列:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題2 分

  1. [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 分

  1. [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 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

=== 第 4 題 ===

i) 觀念與推導

  • 選項 (A) 與 (B):若從 AA 開始進行 DFS 或 BFS,因 DD 為 AA 的鄰接節點(第一層),在 BFS 中勢必在造訪第二層的 I,N,M,HI, N, M, H 之前即先被造訪;而在 DFS 中,若走 A→C→I→NA \to C \to I \to N 後回溯至 I→M→HI \to M \to H,此時 HH 與 MM 已無未造訪鄰點,回溯至 II 時,其未造訪鄰點還有 JJ,故必須先造訪 JJ 才能回溯到 CC 進而造訪 DD。因此下一節點皆不可能為 DD。
  • 選項 (C):若 DFS 起點為 BB,走訪路徑可以為 B→E→FB \to E \to F。此時在 FF 節點上,其未造訪的鄰接節點有 G,J,KG, J, K,因此下一個造訪的節點可以選擇 KK。此敘述正確。
  • 選項 (D):若為 BFS,走訪完 BB(第 0 層)與 EE(第 1 層)後,必須先將 EE 的所有鄰接節點(DD 與 FF)皆加入佇列並造訪,不可能跳過 DD 直接造訪第 3 層的 KK。

【答案】:(C)


ii) 觀念與推導

使用 Kruskal 演算法由大到小選取邊以建構最大生成樹(MCST):

  1. 排序所有邊的權重:(G,L)=15(G, L)=15, (F,K)=14(F, K)=14, (J,I)=13(J, I)=13, (K,L)=12(K, L)=12, (F,G)=11(F, G)=11, (E,F)=10(E, F)=10, (C,I)=9(C, I)=9, (G,K)=9(G, K)=9, (C,D)=8(C, D)=8, (E,B)=8(E, B)=8, (D,E)=7(D, E)=7, (D,J)=6(D, J)=6, (A,D)=5(A, D)=5, (I,M)=5(I, M)=5, (O,P)=5(O, P)=5, (F,J)=4(F, J)=4, (H,M)=4(H, M)=4,
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題3 分

  1. [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) 5
ln(2)
(E) None of the above

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Bloom filter 使用 mm 個位元與多個雜湊函數:

  • 插入元素:將所有雜湊位置的位元設為 11。
  • 查詢元素:只要其中一個對應位元為 00,必定不存在;全部為 11 時回答「是」,但可能是假陽性。
  • 一般 Bloom filter 不支援刪除:直接將位元設為 00,可能同時影響其他元素。

本題位元陣列大小為:

m=15m=15

目前各索引的位元為:

[1,1,0,1,1,1,1,1,0,1,0,0,0,1,0][1,1,0,1,1,1,1,1,0,1,0,0,0,1,0]

i) 各選項判斷

選項(A)

插入 k=12k=12 時:

f1(12)=(3×12) mod 15=36 mod 15=6f_1(12)=(3\times 12)\bmod 15=36\bmod 15=6 f2(12)=(2×12+1) mod 15=25 mod 15=10f_2(12)=(2\times 12+1)\bmod 15=25\bmod 15=10 f3(12)=12 mod 15=12f_3(12)=12\bmod 15=12

對應位元為:

  • 索引 66:原本為 11
  • 索引 1010:原本為 00
  • 索引 1212:原本為 00

因此索引 1010、1212 會由 00 變成 11,位元陣列會改變。

所以(A)錯誤。

選項(B)

對 k=2k=2 計算雜湊位置:

f1(2)=6,f2(2)=5,f3(2)=2f_1(2)=6,\qquad f_2(2)=5,\qquad f_3(2)=2

正確位置為 22、55、66,不是選項所說的 44、55、66。

此外,一般 Bloom filter 不可直接刪除元素,因為索引 55、66 的位元可能也是其他元素所設為 11。任意清除位元會造成假陰性。

所以(B)錯誤。

選項(C)

對 k=15k=15 計算:

f1(15)=45 mod 15=0f_1(15)=45\bmod 15=0 f2(15)=31 mod 15=1f_2(15)=31\bmod 15=1 f3(15)=15 mod 15=0f_3(15)=15\bmod 15=0

需要檢查索引 00 與 11:

B[0]=1,B[1]=1B[0]=1,\qquad B[1]=1

所有對應位元皆為 11,因此查詢結果為「是」。

所以(C)正確。

選項(D)

對 k=8k=8 計算:

f1(8)=24 mod 15=9f_1(8)=24\bmod 15=9 f2(8)=17 mod 15=2f_2(8)=17\bmod 15=2
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6 題5 分

  1. [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
🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 5 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

根據 leftist tree 的合併與刪除最小元素(Delete-Min)步驟:

  1. 刪除根節點:刪除根節點 44 後,需合併左子樹 AA(以 99 為根)與右子樹 BB(以 77 為根)。
  2. 合併過程:
    • 比較 99 與 77,因 7<97 < 9,以 77 為新根,遞迴合併其右子樹(以 1616 為根)與樹 AA(以 99 為根)。
    • 比較 1616 與 99,因 9<169 < 16,以 99 為根,遞迴合併其右子樹(以 1414 為根)與樹(以 1616 為根)。
    • 比較 1414 與 1616,因 14<1614 < 16,以 1414 為根,遞迴合併其右子樹(空節點 null\text{null})與樹(以 1616 為根),結果為 1616 成為 1414 的右子樹。
  3. 更新 ss-value(最短外路徑長度)與調整:
    • 節點 14:左子樹為 1717(s=1s=1),右子樹為 1616(s=1s=1)。不需交換,更新 s(14)=1+min⁡(1,1)=2s(14) = 1 + \min(1, 1) = 2。
    • 節點 9:左子樹為 1616(s=2s=2),右子樹為 1414(s=2s=2)。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7 題5 分

  1. [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.
🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 5 頁原卷第 6 頁

登入後即可作答並保存紀錄。

這一題的完整詳解
  1. 初始狀態:

    • 根節點為 88。其子節點為 1010、1515、2121。
    • 1010 的子節點為 1414、1212,其中 1414 的子節點為 3030。
    • 1515 的子節點為 1818。
    • 2121 無子節點。
    • 此時無任何節點被標記(marked)。
  2. 操作一:decrease the key 14 by 5

    • 1414 減少 55 變為 99。
    • 因 9<109 < 10(其父節點),將 99 自 1010 切斷(cut)並移至根 list。
    • 1010 失去第一個子節點,標記 1010(1010 變為 marked)。無 cascading cut 發生(選項 (B) 錯誤)。
    • 根 list:[8, 9]
  3. 操作二:decrease the key 21 by 14

    • 2121 減少 1414 變為 77。
    • 因 7<87 < 8(其父節點),將 77 自 88 切斷並移至根 list。
    • 根 list:[8, 9, 7]
  4. 操作三:delete the key 12

    • 將 1212 的鍵值降為 −∞-\infty。
    • 將 −∞-\infty 自其父節點 1010 切斷並移至根 list。
    • 因 1010 先前已被標記,觸發 cascading cut:將 1010 自其父節點 88 切斷並移至根 list,並將 1010 取消標記(unmarked)。因 88 為根節點,級聯剪下停止。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 8 題5 分

  1. [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.
🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 6 頁原卷第 7 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

在紅黑樹中插入節點 6262 的步驟如下:

  1. 搜尋與插入:
    根據二元搜尋樹的性質,將新節點 6262 插入為 4747 的右子節點,且新插入節點 6262 初始顏色為紅色。

  2. 第一次修正(Recoloring):

    • 此時 6262 的雙親節點 4747 為紅色,發生雙紅衝突(Double Red violation)。
    • 6262 的叔叔節點 3232 為紅色。
    • 根據紅黑樹規則(叔叔節點為紅色),進行重新著色(Recoloring):
      • 將雙親節點 4747 與叔叔節點 3232 改為黑色。
      • 將祖父節點 3636 改為紅色。
    • 此步驟無須旋轉。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 9 題5 分

  1. [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 分

  1. [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.

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 8 頁原卷第 9 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

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 分

  1. (10%) Consider three n×nn\times n matrices, denoted as AA, BB, and CC. Each of these matrices is partitioned into four n/2×n/2n/2\times n/2 submatrices. Assuming that nn is an exact power of 2, we can guarantee that, for n≥2n\ge 2, the dimension n/2n/2 is an integer. The subdivisions for matrix AA yield A11A_{11}, A12A_{12}, A21A_{21}, and A22A_{22}. Similarly, matrix BB is decomposed into B11B_{11}, B12B_{12}, B21B_{21}, and B22B_{22}, while matrix CC undergoes division into C11C_{11}, C12C_{12}, C21C_{21}, and C22C_{22}. We have the following procedure:
F(A,B):n=A.rowsif n==1C11=a11⋅b11elseC11=F(A11,B11)+F(A12,B21)C12=F(A11,B12)+F(A12,B22)C21=F(A21,B11)+F(A22,B21)C22=F(A21,B12)return C\begin{aligned} F(A,B):\quad & n=A.\mathrm{rows}\\ &\text{if }n==1\\ &\quad C_{11}=a_{11}\cdot b_{11}\\ &\text{else}\\ &\quad C_{11}=F(A_{11},B_{11})+F(A_{12},B_{21})\\ &\quad C_{12}=F(A_{11},B_{12})+F(A_{12},B_{22})\\ &\quad C_{21}=F(A_{21},B_{11})+F(A_{22},B_{21})\\ &\quad C_{22}=F(A_{21},B_{12})\\ &\text{return }C \end{aligned}

It is important to note that the procedure for C22C_{22} is invoked only once and “++” is matrix addition. Let T(n)T(n) represent the time required to process two n×nn\times n matrices using this particular procedure. Provide an asymptotic tight bound (Θ\Theta) for T(n)T(n), assuming that T(n)T(n) is a constant for sufficiently small nn.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查分治演算法的遞迴式與主定理。若每次將問題縮小為原來的 1/21/2,產生 aa 個子問題,並在每層額外花費 f(n)f(n) 時間,遞迴式可寫成:

T(n)=aT(n/2)+f(n)T(n)=aT(n/2)+f(n)

解題方法

逐一計算程序中的遞迴呼叫數,以及矩陣加法的成本:

  • C11C_{11} 有 2 次遞迴呼叫,兩個結果相加。
  • C12C_{12} 有 2 次遞迴呼叫,兩個結果相加。
  • C21C_{21} 有 2 次遞迴呼叫,兩個結果相加。
  • C22C_{22} 只有 1 次遞迴呼叫,依題意不再補上第二次呼叫。

因此,每次處理 n×nn\times n 矩陣時,總共有 2+2+2+1=72+2+2+1=7 次、每次規模為 n/2n/2 的遞迴呼叫。

前三個子矩陣各需要一次矩陣加法,共 3 次。每次矩陣加法處理 (n/2)×(n/2)(n/2)\times(n/2) 個元素,成本為 Θ((n/2)2)=Θ(n2)\Theta((n/2)^2)=\Theta(n^2)。所以遞迴式為:

T(n)=7T(n/2)+Θ(n2)T(n)=7T(n/2)+\Theta(n^2)

基底情況為 T(1)=Θ(1)T(1)=\Theta(1)。

套用主定理,a=7a=7、b=2b=2,而:

nlog⁡ba=nlog⁡27n^{\log_b a}=n^{\log_2 7}
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 12 題10 分

  1. (10%) Consider a neural network with nn layers of fully connected layers, where the first layer is the input layer, the nnth layer is the output layer, and the rest are hidden layers. The iith layer has PiP_i neurons, where 1≤i≤n1\le i\le n. Each neuron in the iith layer is connected to every neuron in the (i+1)(i+1)th layer, where 1≤i<n1\le i<n. Let wj,kiw_{j,k}^{i} represent the weight of the connection from the jjth neuron in the iith layer to the kkth neuron in the (i+1)(i+1)th layer. Let [v1i v2i ⋯ vPii]T[v_1^i\ v_2^i\ \cdots\ v_{P_i}^i]^T be the values of neurons in the iith layer, where 1≤i≤n1\le i\le n. Then, we can obtain vki+1=∑j=1Piwj,kivjiv_k^{i+1}=\sum_{j=1}^{P_i}w_{j,k}^{i}v_j^i, where 1≤k≤Pi+11\le k\le P_{i+1}. Given that (P1,P2,P3,P4,P5,P6)=(10,6,12,5,50,3)(P_1,P_2,P_3,P_4,P_5,P_6)=(10,6,12,5,50,3), 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.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考的是矩陣鏈乘法。依題目給定的公式,每一層都是線性運算,因此可將整個網路寫成矩陣與輸入向量的連乘,再選擇乘法順序以降低乘法次數。

兩個維度分別為 a×ba\times b 與 b×cb\times c 的矩陣相乘,需要 abcabc 次乘法。矩陣鏈乘法的動態規劃遞迴式為:

m[i,j]=min⁡i≤k<j(m[i,k]+m[k+1,j]+di−1dkdj)m[i,j]=\min_{i\le k<j} \left(m[i,k]+m[k+1,j]+d_{i-1}d_kd_j\right)

其中 dd 是矩陣鏈的維度序列,m[i,j]m[i,j] 表示將第 ii 個至第 jj 個矩陣相乘所需的最少乘法次數。

解題方法

令 WiW_i 表示由第 ii 層連到第 i+1i+1 層的權重矩陣,則整個網路的輸出為:

W5W4W3W2W1v1W_5W_4W_3W_2W_1\mathbf{v}^{1}

各矩陣及輸入向量的尺寸依序為:

W5:3×50,W4:50×5,W3:5×12,W2:12×6,W1:6×10,v1:10×1W_5:3\times50,\quad W_4:50\times5,\quad W_3:5\times12,\quad W_2:12\times6,\quad W_1:6\times10,\quad \mathbf{v}^{1}:10\times1

因此維度序列為 d=(3,50,5,12,6,10,1)d=(3,50,5,12,6,10,1)。將六個矩陣依序編號為 A1,…,A6A_1,\ldots,A_6,其中 A6=v1A_6=\mathbf{v}^{1},動態規劃結果如下:

矩陣區間最少乘法次數
m[1,2],m[2,3],m[3,4],m[4,5],m[5,6]m[1,2],m[2,3],m[3,4],m[4,5],m[5,6]750, 3000, 360, 720, 60750,\ 3000,\ 360,\ 720,\ 60
m[1,3],m[2,4],m[3,5],m[4,6]m[1,3],m[2,4],m[3,5],m[4,6]930, 1860, 660, 132930,\ 1860,\ 660,\ 132
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 13 題10 分

  1. (10%) Given two sequences X=⟨x1,x2,…,xm⟩X=\langle x_1,x_2,\ldots,x_m\rangle and Y=⟨y1,y2,…,yn⟩Y=\langle y_1,y_2,\ldots,y_n\rangle, define c[i,j]c[i,j] to be the length of an LCS (longest common subsequence) of the sequences Xi=⟨x1,x2,…,xi⟩X_i=\langle x_1,x_2,\ldots,x_i\rangle and Yj=⟨y1,y2,…,yj⟩Y_j=\langle y_1,y_2,\ldots,y_j\rangle. Write the recursive formula to compute c[i,j]c[i,j].

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查最長共同子序列(LCS)的動態規劃。子序列保留原有元素的相對順序,但不要求連續。

令 c[i,j]c[i,j] 表示 XX 的前 ii 個元素與 YY 的前 jj 個元素的 LCS 長度。計算時比較兩個前綴的最後一個元素 xix_i 與 yjy_j。

解題方法

若其中一個前綴為空,沒有共同子序列,因此:

c[i,0]=0(0≤i≤m),c[0,j]=0(0≤j≤n)c[i,0]=0 \quad (0\le i\le m),\qquad c[0,j]=0 \quad (0\le j\le n)

接著分兩種情況:

  • 若 xi=yjx_i=y_j,這個相同元素可接在 Xi−1X_{i-1} 與 Yj−1Y_{j-1} 的 LCS 後面,長度增加 11。
  • 若 xi≠yjx_i\ne y_j,LCS 不會同時以 xix_i 和 yjy_j 結尾;分別略去其中一個末尾元素,取兩種結果中較大的長度。

因此遞迴公式為:

c[i,j]={0,i=0 或 j=0,c[i−1,j−1]+1,xi=yj,max⁡{c[i−1,j], c[i,j−1]},xi≠yj.c[i,j]= \begin{cases} 0, & i=0\text{ 或 }j=0,\\ c[i-1,j-1]+1, & x_i=y_j,\\ \max\{c[i-1,j],\,c[i,j-1]\}, & x_i\ne y_j. \end{cases}

關鍵程式碼與複雜度

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 14 題10 分

  1. (10%) Given a sequence of nn numbers, what is the lower bound for sorting algorithms employing comparison and exchange operations?

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查比較式排序的下界。在只透過元素間的比較來判斷順序時,排序演算法必須能分辨輸入元素的不同排列。交換操作可以改變元素位置,但無法取代比較所提供的順序資訊。

以下假設 nn 個元素互異;若有重複元素,可能排列數會減少。

解題方法

nn 個互異元素共有 n!n! 種排列。把排序過程表示成一棵決策樹:每次比較是一次分支,葉節點代表演算法判定的一種輸入排列。正確排序必須能區分全部 n!n! 種排列,因此決策樹至少要有 n!n! 個葉節點。

若樹的高度為 hh,最多有 2h2^h 個葉節點,故:

2h≥n!2^h \ge n!

取以 22 為底的對數,可得最壞情況比較次數至少為:

h≥⌈log⁡2(n!)⌉h \ge \lceil \log_2(n!) \rceil
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 15 題10 分

  1. (10%) Find the set of feasible solutions {x=(x1,x2,x3,x4,x5)}\{x=(x_1,x_2,x_3,x_4,x_5)\} or determine that no feasible solution exists for the following system of difference constraints:
x1−x3≤1x2−x3≤4x4−x5≤−2x3−x4≤4x5−x1≤3x4−x2≤−7x1−x2≤−2x5−x3≤1\begin{aligned} x_1-x_3&\le 1\\ x_2-x_3&\le 4\\ x_4-x_5&\le -2\\ x_3-x_4&\le 4\\ x_5-x_1&\le 3\\ x_4-x_2&\le -7\\ x_1-x_2&\le -2\\ x_5-x_3&\le 1 \end{aligned}

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

差分限制式的一般形式為 xj−xi≤cx_j-x_i\le c。可將它表示成有向邊 i→ji\to j,邊權重為 cc。差分限制式系統可行,等價於其限制圖中沒有負權重環;若能找出一組滿足所有限制的變數值,就能直接證明系統可行。

本題要求的是所有可行解,因此除了確認可行,也要描述各變數之間可取的範圍。

解題方法

由於每個限制只涉及變數的差值,所有變數同時加上相同常數仍是可行解。令 x3=sx_3=s 作為基準,並定義

t=x4−x3,y=x2−x3,z=x1−x3,w=x5−x3.t=x_4-x_3,\qquad y=x_2-x_3,\qquad z=x_1-x_3,\qquad w=x_5-x_3.

原限制式改寫為

z≤1,y≤4,t−w≤−2,−t≤4,w−z≤3,t−y≤−7,z−y≤−2,w≤1.\begin{aligned} z&\le 1,\\ y&\le 4,\\ t-w&\le -2,\\ -t&\le 4,\\ w-z&\le 3,\\ t-y&\le -7,\\ z-y&\le -2,\\ w&\le 1. \end{aligned}

先由 −t≤4-t\le4 得 t≥−4t\ge-4。又由 t−y≤−7t-y\le-7 得 y≥t+7y\ge t+7,搭配 y≤4y\le4 可得 t≤−3t\le-3。因此

−4≤t≤−3,t+7≤y≤4.-4\le t\le -3,\qquad t+7\le y\le4.

接著,z≤1z\le1 且 z−y≤−2z-y\le-2,所以 z≤min⁡(1,y−2)z\le\min(1,y-2)。為使 ww 同時滿足 w≥t+2w\ge t+2 與 w≤z+3w\le z+3,必須有 z≥t−1z\ge t-1。因此

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題

113 年成功大學的其他科目

成功大學《程式設計》其他年度