免費詳解 / 交大 / 軟體 / 104

104 交大資工所考古題解答|軟體逐題詳解

104 交大資工所軟體考古題的逐題詳解,共 40 題,免費公開。

逐題詳解

12.5 分

The transitive closure matrix of the given directed graph have \(k\) elements that are 1's. What is true about \(k\)? (A) \(k\le6\) (B) \(7\le k\le10\) (C) \(11\le k\le14\) (D) \(15\le k\le18\) (E) \(k\ge19\)

(圖:\(0\to1,\ 1\to4,\ 1\to3,\ 4\to2,\ 3\to0,\ 3\to2\))

01423

\(0\to1\to3\to0\) 成環,0、1、3 互相可達(自己到自己也算 1)

起點可達的點個數
00, 1, 2, 3, 45
10, 1, 2, 3, 45
2(沒有出邊)0
30, 1, 2, 3, 45
421

\(k=5+5+0+5+1=16\)

(D)

22.5 分

Consider a pair of corresponding infix and postfix arithmetic expressions. Which of the following statements is true?

(A) In a postfix expression, the order of operators is from high to low precedence.

(B) The infix and postfix expressions always have the same number of parentheses.

(C) The infix and postfix expressions always have the same order of operands.

(D) A priority queue for the operators is required when evaluating a postfix expression.

(E) All of the above.

(A) postfix 中 operator 的順序是實際運算的順序,不是 precedence 高到低 → F

(B) postfix 不需要括號 → F

(C) infix 轉 postfix 時 operand 的相對順序不變,只有 operator 被搬位置 → T

(D) 求 postfix 的值只需要一個 stack → F

(C)

32.5 分

Below is the C++ implementation of ADTX, an unknown abstract data type. For clarity, exception handling codes are omitted. Which of the following is the most likely abstract data type implemented here? (A) stack (B) first-in-first-out queue (C) priority queue (D) linked list (E) none of the above

template <class T> class ADTX {
private:
  T *x;
  int f, r, cp;
public:
  ADTX(int C=10) { x = new T[C]; cp = C; f = r = 0; }
  ~ADTX() { delete [] x; }
  bool isEmpty() const { return (f == r); }
  bool isFull() const { return ((r+1)%cp == f); }
  T& front() const { return x[(f+1)%cp]; }
  T& rear() const { return x[r]; }
  void push(const T& v) { r = (r+1)%cp; x[r] = v; }
  void pop() { f = (f+1)%cp; }
};

push 只動 rear \(r\)、pop 只動 front \(f\),陣列用 \(\%cp\) 繞回,\(f==r\) 為空、\((r+1)\%cp==f\) 為滿 → circular queue

從 front 端取、從 rear 端放 → FIFO queue

(B)

42.5 分

For the ADTX implementation in the previous question, after the following C++ code is executed, what is the value in adtx.x[1]? (A) 6 (B) 9 (C) 3 (D) 7 (E) 5

ADTX<int> adtx(4);
adtx.push(3); adtx.push(2); adtx.pop(); adtx.push(5);
adtx.push(9); adtx.pop(); adtx.pop(); adtx.push(7); adtx.push(6);

\(cp=4\),起始 \(f=r=0\)

動作\(f\)\(r\)陣列 x[0..3]
push(3)01_ 3 _ _
push(2)02_ 3 2 _
pop()12_ 3 2 _
push(5)13_ 3 2 5
push(9)109 3 2 5
pop()209 3 2 5
pop()309 3 2 5
push(7)319 7 2 5
push(6)329 7 6 5

x[1] 最後被 push(7) 覆蓋

(D)

52.5 分

For the postorder traversal sequence of the binary tree below, which of these statements is true?

(圖:A 的左子 B、右子 D;B 的左子 E;E 的子 K、L;D 的左子 H、右子 J;H 的子 F、M)

KELBAFHMDJ

(A) Node B appears at the second position.

(B) Node M appears after node D.

(C) Node H appears immediately before node J.

(D) The first four nodes in the sequence are all leaf nodes in the tree.

(E) Node A appears between node B and node D.

postorder(左、右、根):K L E B F M H J D A

(A) 第二個是 L → F

(B) M 在第 6 位,D 在第 9 位,M 在 D 之前 → F

(C) H 在第 7 位、J 在第 8 位,緊鄰 → T

(D) 前四個是 K, L, E, B,E、B 不是 leaf → F

(E) A 是最後一個 → F

(C)

62.5 分

If we make the binary tree in the previous question a threaded binary tree, which node is pointed to by a thread of node B? (A) node A (B) node E (C) node D (D) node F (E) node L

inorder:K E L B A F H M D J

B 的左子是 E(不是 thread);B 沒有右子,right thread 指向它的 inorder successor

A:(A)

72.5 分

For a certain binary tree, its inorder traversal sequence is EKHDAGBJFC and its level-order traversal sequence is GKBEAFHJCD. Here each letter represents a node of the tree. Which of the following statements about this binary tree is incorrect?

(A) Node D is a descendent of node K.

(B) There are exactly four leaf nodes.

(C) Node G is the root and has two children.

(D) The parent of node H is node A.

(E) None of the above.

level-order 第一個 G 是 root;inorder 中 G 左邊 EKHDA 是左子樹,右邊 BJFC 是右子樹

左子樹 level-order:K, E, A, H, D → K 是根,E 在左,HDA 在右;其中 A 是根(level-order 先 A 再 H、D),inorder H D A → A 的左子樹是 H D:H 先出現 → H 是 A 的左子,D 是 H 的右子

右子樹 level-order:B, F, J, C → B 是根,inorder B J F C → 右邊是 JFC,F 先 → F 是 B 的右子,F 的子 J(左)、C(右)

EKHDAGBJFC

(A) D 在 K 的子樹裡 → T

(B) leaf:E, D, J, C 共四個 → T

(C) G 是 root,有兩個子 K、B → T

(D) H 的 parent 是 A → T

四個敘述都成立,沒有不正確的

(E)

82.5 分

Starting with an empty binary search tree, what is the maximum level of the nodes in the tree after this series of insertions: 3, 9, 1, 2, 7, 6, 5, 4, 8, 0? Note: the level of the root is one. No balancing operation occurs at insertions. (A) 3 or less (B) 4 (C) 5 (D) 6 (E) 7 or more

3 為 root;9、1 分兩邊;2 在 1 的右;7 在 9 的左;6 在 7 的左;5 在 6 的左;4 在 5 的左;8 在 7 的右;0 在 1 的左

0123456789

路徑 \(3\to9\to7\to6\to5\to4\) 有 6 個節點,root 算 level 1,4 在 level 6

官方標準答案表列「C or D」都給分;這裡照題目「root 為 level 1」的算法得 6

(D)

92.5 分

A max-heap is given below; the numbers at the nodes are their keys. Now after one insertion operation (the number inserted is 14) and then one deletion operation, which of the following statements is false regarding the resulting heap? Here node(n) represents the node with key n.

(圖:21;11 與 17;11 的子 4、10,17 的子 13、8;4 的左子 3)

3411102113178

(A) The parent of node(4) is node(11).

(B) node(13) has two children.

(C) The position of node(10) in the tree does not change.

(D) node(17) is the root.

(E) The number of leaf nodes does not change.

陣列 \([21,11,17,4,10,13,8,3]\)

insert 14:放在最後(第 9 格,4 的右子),sift up:與 4 交換、再與 11 交換 → \([21,14,17,11,10,13,8,3,4]\)

311414102113178

delete max:把最後的 4 搬到 root,sift down:與較大的子 17 交換,再與 13 交換 → \([17,14,13,11,10,4,8,3]\)

3111410174138

(A) 4 的 parent 是 13,不是 11 → F(敘述錯誤)

(B) 13 有兩個子 4、8 → T

(C) 10 仍在第 5 格 → T

(D) 17 是 root → T

(E) leaf:3, 10, 4, 8,原本是 3, 10, 13, 8,都是 4 個 → T

(A)

102.5 分

For a doubly-linked list class, each of its nodes has two links left and right to its two neighboring nodes. Assume that we want to implement a member function ShiftRight, which takes an input x, a pointer to a node, and swaps that node with the node to its right. Let us further assume that neither x nor the node to its right is the first or the last node of the list. There are six pointers to be set, and the statements, labeled s1 to s6, are listed in the box below. Which of the following ordering of these statements will produce the correct behavior?

if (x && x->right && x->right->right && x->left) {
  DoublyLinkedListNode<T> *y = x->right;
  x->left->right = y;      // s1
  x->left = y;             // s2
  y->right->left = x;      // s3
  x->right = y->right;     // s4
  y->right = x;            // s5
  y->left = x->left;       // s6
}

(A) s5→s2→s6→s1→s4→s3

(B) s2→s5→s4→s3→s1→s6

(C) s4→s3→s6→s1→s2→s5

(D) s1→s2→s3→s6→s5→s4

(E) s4→s5→s3→s6→s1→s2

令 \(w=x\to\text{left}\)(舊值),\(y=x\to\text{right}\),\(z=y\to\text{right}\)(舊值);目標 \(w\leftrightarrow y\leftrightarrow x\leftrightarrow z\)

用到舊值的敘述要早於覆蓋它的敘述:s4(用 \(y\to\text{right}\))、s3(用 \(y\to\text{right}\))要早於 s5(改 \(y\to\text{right}\));s1、s6(用 \(x\to\text{left}\))要早於 s2(改 \(x\to\text{left}\))

順序敘述效果
1s4\(x\to\text{right}=z\)
2s3\(z\to\text{left}=x\)
3s6\(y\to\text{left}=w\)
4s1\(w\to\text{right}=y\)
5s2\(x\to\text{left}=y\)
6s5\(y\to\text{right}=x\)

其他選項都在還要用舊值時就先覆蓋掉了

(C)

112.5 分

Consider the AVL tree resulting from sequentially inserting the following numbers into an empty AVL tree: 40 60 55 15 20 5 25 30. What is the sum of the numbers in the leaf nodes in the AVL tree? (A) 90 (B) 130 (C) 155 (D) 160 (E) none of the above

40, 60:40 root,60 在右;55 插在 60 的左,在 40 失衡(RL) → 55 上來:55(40, 60)

15 在 40 左;20 在 15 右,40 失衡(LR) → 20 上來:20(15, 40) → 55(20(15,40), 60)

5 在 15 左:55 的左子樹高 3(20 → 15 → 5)、右子樹(60)高 1,失衡;5 在左子 20 的左邊(LL) → 以 55 為軸右旋,20 上來:20(15(5, –), 55(40, 60))

25 在 40 左;30 在 25 右,40 失衡(LR) → 30 上來:30(25, 40),接回 55 的左邊

405560
① 插 55:40 失衡(RL)
405560
② 雙旋轉後
1520405560
③ 插 20:40 失衡(LR)
1520405560
④ 雙旋轉後
51520405560
⑤ 插 5:55 失衡(LL)
51520405560
⑥ 以 55 為軸右旋後
515202530405560
⑦ 插 25、30:40 失衡(LR)

紅筆是剛插入的點和失衡的點;⑦ 雙旋轉後就是下圖

515202530405560

leaf:5, 25, 40, 60

\(5+25+40+60=130\)

(B)

122.5 分

Consider the Red-Black tree resulting from sequentially inserting the following numbers into an empty Red-Black tree: 40 60 55 15 20 5 25 30. What is the sum of the numbers in the red nodes in the Red-Black tree? (A) 90 (B) 130 (C) 155 (D) 160 (E) none of the above

40(B);60(R);55 插在 60 左,叔叔是 null(黑) → 旋轉:55(B),40(R)、60(R)

15 插在 40 左,父 40 與叔 60 都是紅 → recolor:40、60 變黑,55 變紅再改回黑(root);15(R)

20 插在 15 右,叔叔 null(黑),LR 旋轉:20(B),15(R)、40(R)

5 插在 15 左,父 15 與叔 40 都紅 → recolor:15、40 變黑,20 變紅

25 插在 40 左(40 是黑) → 25(R) 不用調整

30 插在 25 右,父 25 紅、叔 null → LR 旋轉:30(B),25(R)、40(R)

405560
① 插 55:60、55 紅紅相連,叔叔是空的
405560
② 旋轉、改色後
15405560
③ 插 15:父 40、叔 60 都紅
15405560
④ recolor 後
1520405560
⑤ 插 20:15、20 紅紅相連,叔叔是空的
1520405560
⑥ 雙旋轉、改色後
51520405560
⑦ 插 5:父 15、叔 40 都紅
51520405560
⑧ recolor 後
515202530405560
⑨ 插 25 不用調;插 30:25、30 紅紅相連

深色是黑點、紅色是紅點;⑨ 雙旋轉、改色後就是下圖

515202530405560

紅色節點:20, 5, 25, 40

\(20+5+25+40=90\)

(A)

132.5 分

Consider the B-tree, say \(T'\), resulting from sequentially inserting the following numbers into the following B-tree \(T\) with order 3: 15 20 5 25 30. What is the sum of the keys in the non-leaf nodes in \(T'\)? (A) 95 (B) 100 (C) 110 (D) 120 (E) none of the above

(\(T\):root [55],左子 [40],右子 [60];order 3 = 每個節點最多 2 個 key)

15:leaf [40] → [15, 40]

20:[15, 20, 40] 溢位,中間的 20 上推 → root [20, 55],leaf [15]、[40]、[60]

5:[15] → [5, 15]

25:落在 20 與 55 之間的 leaf [40] → [25, 40]

30:[25, 30, 40] 溢位,30 上推 → root [20, 30, 55] 溢位,30 再上推成新 root

406055
① 題目給的 T
15 406055
② 插 15
15 20 406055
③ 插 20:[15 20 40] 溢位
15406020 55
④ 20 上推後
5 1525 406020 55
⑤ 插 5、25
5 1525 30 406020 55
⑥ 插 30:[25 30 40] 溢位
5 1525406020 30 55
⑦ 30 上推,根 [20 30 55] 溢位

紅筆是溢位的節點;⑦ 的 30 再上推成新 root,就是下圖

5 15252040605530

non-leaf 的 key:30, 20, 55 → \(30+20+55=105\),不在選項中

(E)

142.5 分

Consider the B+-tree, say \(T'\), resulting from sequentially inserting the following numbers into the following B+-tree \(T\) with order 3: 15 20 5 25 30. What is the sum of the keys in the non-leaf nodes in \(T'\)? (A) 95 (B) 100 (C) 110 (D) 120 (E) none of the above

(\(T\):root [55],左 leaf [40],右 leaf [55, 60],leaf 之間有指標)

B+-tree:leaf 分裂時把中間 key「複製」到父節點;內部節點分裂時才把中間 key「搬」上去

15:leaf [40] → [15, 40]

20:[15, 20, 40] 溢位 → [15]、[20, 40],複製 20 上去 → root [20, 55]

5:[15] → [5, 15]

25:[20, 25, 40] 溢位 → [20]、[25, 40],複製 25 上去 → root [20, 25, 55] 溢位,內部分裂:25 搬上去成新 root,左 [20]、右 [55]

30:[25, 30, 40] 溢位 → [25]、[30, 40],複製 30 上去 → 右內部節點 [30, 55]

4055 6055
① 題目給的 T
15 4055 6055
② 插 15
15 20 4055 6055
③ 插 20:leaf [15 20 40] 溢位
1520 4055 6020 55
④ 分裂成 [15]、[20 40],複製 20 上去
5 1520 4055 6020 55
⑤ 插 5
5 1520 25 4055 6020 55
⑥ 插 25:leaf [20 25 40] 溢位
5 152025 4055 6020 25 55
⑦ 分裂、複製 25 上去,根 [20 25 55] 溢位
5 15202025 4055 605525
⑧ 內部點分裂,25 搬上去成新 root
5 15202025 30 4055 605525
⑨ 插 30:leaf [25 30 40] 溢位

紅筆是溢位的節點;⑨ 分裂成 [25]、[30 40] 並複製 30 上去,就是下圖

5 1520202530 4055 6030 5525

non-leaf 的 key:25;20;30, 55 → \(25+20+30+55=130\),不在選項中

(E)

152.5 分

Consider the given directed graph. Let dist(i, j) be the length of the shortest path from node i to node j. What is dist(A,C)+dist(A,E)+dist(A,F)? (A) 10 (B) 12 (C) 13 (D) 15 (E) None of the above.

(圖:\(A\to B\) 5、\(A\to D\) 7、\(B\to C\) 5、\(C\to B\) 1、\(B\to E\) −1、\(B\to D\) 8、\(D\to C\) 2、\(D\to E\) −3、\(E\to A\) −3、\(E\to C\) 1、\(E\to F\) 5、\(C\to F\) 1、\(F\to C\) 2)

ABCDEF5751-182-3-31512

有負權邊、沒有負 cycle(\(A\to B\to E\to A\) 為 \(5-1-3=1\);\(A\to D\to E\to A\) 為 \(7-3-3=1\)),用 Bellman–Ford 從 \(A\) 逐輪 relax

ABCDEF
初始0∞∞∞∞∞
第 1 輪(各邊依序)055746
第 2 輪055746

\(\text{dist}(A,E)=\min(A\to B\to E=4,\ A\to D\to E=4)=4\)

\(\text{dist}(A,C)=A\to B\to E\to C=5\)(比 \(A\to D\to C=9\) 小)

\(\text{dist}(A,F)=A\to B\to E\to C\to F=6\)(比 \(E\to F\) 的 \(4+5=9\) 小)

\(5+4+6=15\)

(D)

162.5 分

Consider the graph in the previous problem. Let dist(i, j) be the length of the shortest path from node i to node j. What is dist(A,F)+dist(B,F)+dist(C,F)+dist(D,F)+dist(E,F)? (A) 7 (B) 8 (C) 9 (D) 10 (E) none of the above

每個起點都經 \(C\to F\)(權 1)

起點最短路徑長度
AA→B→E→C→F6
BB→E→C→F−1+1+1 = 1
CC→F1
DD→E→C→F−3+1+1 = −1
EE→C→F1+1 = 2

\(6+1+1-1+2=9\)

(C)

172.5 分

Consider the following activity network. What is the length of the critical path? (A) 14 (B) 16 (C) 18 (D) 20 (E) none of the above

(圖:\(A\to B\) 4、\(A\to C\) 3、\(A\to D\) 6、\(B\to E\) 2、\(B\to J\) 5、\(B\to G\) 3、\(C\to F\) 5、\(D\to F\) 5、\(D\to H\) 2、\(E\to I\) 6、\(F\to K\) 3、\(G\to K\) 6、\(H\to K\) 3、\(I\to L\) 2、\(J\to L\) 3、\(K\to L\) 4)

ABCDEFGHIJKL4362535526363234

每個節點取最長路徑(最早完成時間)

節點BCDEFGHIJKL
最長436611781291418

\(F=\max(C+5,D+5)=11\);\(K=\max(F+3,G+6,H+3)=14\);\(L=\max(I+2,J+3,K+4)=\max(14,12,18)=18\)

critical path:\(A\to D\to F\to K\to L\)

(C)

182.5 分

Consider six nodes with weights 2, 4, 6, 8, 10 and 12, respectively. Give the weighted external path length of the Huffman tree with the above six nodes as external nodes. (A) 95 (B) 100 (C) 102 (D) 104 (E) none of the above

weighted external path length = 所有 internal node 的值的總和

合併:\(2+4=6\);\(6+6=12\);\(8+10=18\);\(12+12=24\);\(18+24=42\)

\(6+12+18+24+42=102\)

(C)

192.5 分

Consider an empty hash table with 10 buckets and each bucket has 2 slots. Suppose that linear probing is used to handle overflow and the following is used as the hash function: \(h(n)=n\%10\). Now we sequentially insert the following numbers into the hash table: 5 15 23 44 3 53 6 4 109. What is the summation of the numbers in the full buckets of the hash table? (A) 153 (B) 150 (C) 262 (D) 43 (E) none of the above

插入落點
5B5
15B5(滿)
23B3
44B4
3B3(滿)
53B3 已滿 → B4(滿)
6B6
4B4 已滿、B5 已滿 → B6(滿)
109B9

full bucket:B3 {23, 3}、B4 {44, 53}、B5 {5, 15}、B6 {6, 4};B9 只有一個不算

\(26+97+20+10=153\)

(A)

202.5 分

What is the cost of minimum cost spanning tree of the given graph? (A) 28 (B) 30 (C) 32 (D) 33 (E) none of the above

(圖:A–B 2、A–C 14、A–E 16、A–F 7、B–D 12、C–E 3、D–E 4、D–F 10、F–G 8、G–E 6)

ABCDEFG21416712341086

Kruskal,由小到大掃

邊權重是否收入
A–B2✓
C–E3✓
D–E4✓
E–G6✓
A–F7✓
F–G8✓(接起 {A,B,F} 與 {C,D,E,G})

7 個點收到 6 條邊就結束:\(2+3+4+6+7+8=30\)

(B)

212.5 分

Let \(f(n)\) and \(g(n)\) be positive functions. Notation: \(\lg n=\log n=\log_2n\). Which one of the following statements is not correct?

(A) \(f(n)=O(g(n))\) implies \(\lg(f(n))=O(\lg(g(n)))\), where \(\lg(g(n))\ge1\) and \(f(n)\ge1\) for all sufficiently large \(n\).

(B) \(f(n)=O(g(n))\) implies \(2^{f(n)}=O(2^{g(n)})\).

(C) \(f(n)+o(f(n))=\Theta(f(n))\).

(D) \([\log(\log n)]!=O(n)\).

(E) If \(\log f(n)=\Theta(\log n)\), then \(f(n)\) is polynomially bounded.

(A) \(f\le cg\),\(\lg f\le\lg c+\lg g\le(\lg c+1)\lg g\)(\(\lg g\ge1\)) → T

(B) 取 \(f=2n,\ g=n\):\(f=O(g)\),但 \(2^{2n}/2^n=2^n\to\infty\),\(2^{f}\ne O(2^g)\) → F(敘述錯誤)

(C) \(o(f)\) 比 \(f\) 小,和仍是 \(\Theta(f)\) → T

(D) \(\log((\log\log n)!)\approx\log\log n\cdot\log\log\log n=o(\log n)\),所以 \((\log\log n)!=n^{o(1)}=O(n)\) → T

(E) \(\log f=\Theta(\log n)\) → \(f\le n^c\) → T

(B)

222.5 分

Let \(f(n)\) and \(g(n)\) be asymptotically positive functions. \(T(n)\) is the time an algorithm takes for an input of size \(n\). Which one of the following statement is not correct?

(A) For the two functions \(f(n)\) and \(g(n)\), either \(f(n)=O(g(n))\) or \(f(n)=\Omega(g(n))\).

(B) \(\lg(n^c)=O(\lg n)\) for constant \(c\gt0\).

(C) \(n^c=O(a^n)\) for constants \(c\gt0\) and \(a\gt1\).

(D) For any uniform cost RAM program \(T(n)=\Omega(S(n))\), where \(S(n)\) is the space an algorithm uses for an input of size \(n\).

(E) \(\max(f(n),g(n))=\Theta(f(n)+g(n))\).

(A) 任兩個函數不一定可比:\(f(n)=n,\ g(n)=n^{1+\sin n}\),\(g\) 在 \(n^0\) 與 \(n^2\) 之間來回震盪,\(f\) 既不是 \(O(g)\) 也不是 \(\Omega(g)\) → F(敘述錯誤)

(B) \(\lg n^c=c\lg n\) → T

(C) 指數壓過多項式 → T

(D) 每個時間單位最多碰一個新 cell,用的空間不超過時間 → T

(E) \(\max\le f+g\le2\max\) → T

(A)

232.5 分

Which of the following algorithms can sort \(n\) data in \(O(n\log n)\) worst-case time? (A) bubble sort (B) quick sort (C) merge sort (D) both quick sort and merge sort (E) both heap sort and bubble sort.

bubble sort worst case \(O(n^2)\);quick sort worst case \(O(n^2)\);merge sort \(O(n\log n)\);heap sort 雖然是 \(O(n\log n)\),但 (E) 還有 bubble sort

(C)

242.5 分

Let \(G=(V,E)\) be an undirected graph with \(n\) vertices, where \(n\gt1\), and \(M\) be the adjacency matrix of \(G\). Which of the following statements is not correct?

(A) \(M\) is a 2-dimension \(n\times n\) array. (B) \(M\) is symmetric. (C) The degree of any vertex \(i\) is equal to the \(i\)th row sum. (D) The graph has an even number of vertices of odd degree. (E) The number of edges of \(G\) is equal to \(\sum_{i=1}^n\sum_{j=1}^nM(i,j)\).

(A)(B)(C)(D) 都成立(握手定理) → T

(E) 無向圖每條邊在 \(M\) 出現兩次(\(M(i,j)\) 與 \(M(j,i)\)),全部加總是 \(2|E|\),不是 \(|E|\) → F(敘述錯誤)

(E)

252.5 分

Assume that \(T(n)\) is constant for sufficiently small \(n\). Which one of the following statements is correct?

(A) \(T(n)=2T(n/2)+n\log n\), then \(T(n)=\Theta(n\log n)\).

(B) \(T(n)=2T(n/2)+n/\log n\), then \(T(n)=\Theta(n)\).

(C) \(T(n)=2T(n/2)+n/\log^2n\), then \(T(n)=\Theta(n)\).

(D) \(T(n)=T(n-1)+\dfrac1n\), then \(T(n)=\Theta(n\log n)\).

(E) \(T(n)=T(n-1)+n\log n\), then \(T(n)=\Theta(n\log n)\).

遞迴樹第 \(i\) 層的成本(共 \(2^i\) 個子問題)

(A) \(2^i\cdot\dfrac n{2^i}\log\dfrac n{2^i}=n(\log n-i)\),加總 \(\Theta(n\log^2n)\) → F

(B) 每層 \(\dfrac{n}{\log n-i}\),加總 \(n\sum_k\tfrac1k=\Theta(n\log\log n)\) → F

(C) 每層 \(\dfrac{n}{(\log n-i)^2}\),加總 \(n\sum_k\tfrac1{k^2}=\Theta(n)\)(葉子也是 \(\Theta(n)\)) → T

(D) \(\sum\tfrac1k=\Theta(\log n)\) → F

(E) \(\sum k\log k=\Theta(n^2\log n)\) → F

(C)

262.5 分

Let \(G=(V,E)\) be an undirected connected graph, where each edge \(e\) has a given length \(l(e)\gt0\). Let \(s\) be a fixed vertex of this graph and let \(\delta(v)\) be the length of a shortest path from \(s\) to \(v\). Which one of the following statements is not correct?

(A) For each vertex \(v\ne s\), there is an incident edge \(e=(u,v)\) such that \(\delta(v)=\delta(u)+l(e)\).

(B) For each vertex \(v\ne s\), choose one incident edge \(e=(u,v)\) such that \(\delta(v)=\delta(u)+l(e)\), the collection of these edges forms a spanning tree of \(G\).

(C) For each vertex \(v\ne s\), choose one incident edge \(e=(u,v)\) such that \(\delta(v)=\delta(u)+l(e)\), the collection of these edges forms a minimum weight spanning tree of \(G\).

(D) Bellman-Ford algorithm can find a shortest path from \(s\) to \(v\) in this case.

(E) Dijkstra's algorithm can find a shortest path from \(s\) to \(v\) in this case.

(A)(B) 最短路徑樹 → T

(C) shortest path tree 不一定是 MST。反例:三角形 \(s\)–\(u\) 1、\(s\)–\(v\) 2、\(u\)–\(v\) 1.5:\(\delta(u)=1,\ \delta(v)=2\),最短路徑樹 \(\{su,sv\}\) 成本 3;MST 是 \(\{su,uv\}\) 成本 2.5 → F(敘述錯誤)

(D)(E) 邊長 \(\gt0\),兩個演算法都適用 → T

(C)

272.5 分

\(\log(n!)\) is asymptotically equal to (A) \(\Theta(n)\) (B) \(\Theta(n\log(\log n))\) (C) \(\Theta(n^2\log n)\) (D) \(\Theta(n\log n)\) (E) \(\Theta((\log n)^2)\)

\(\log(n!)=\sum_{i=1}^n\log i\le n\log n\)

取後一半:\(\ge\dfrac n2\log\dfrac n2=\Omega(n\log n)\)

\(\Theta(n\log n)\):(D)

282.5 分

Which one of the following statements is not correct?

(A) If an edge is contained in some minimum spanning tree, then it is a light edge crossing some cut of the graph. (Definition: An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut.)

(B) If a graph has a unique minimum spanning tree then, for every cut of the graph, there is a unique light edge crossing the cut.

(C) A graph has a unique minimum spanning tree if, for every cut of the graph, there is a unique light edge crossing the cut.

(D) Let \(e\) be a maximum-weight edge on some cycle of the graph \(G=(V,E)\), then there is a minimum spanning tree of \(G'=(V,E-\{e\})\) that is also a minimum spanning tree of \(G=(V,E)\).

(E) Let \((u,v)\) be a minimum-weight edge in a graph \(G\). Then \((u,v)\) belongs to some minimum spanning tree of \(G\).

(A) MST 的邊是它所切出的 cut 的 light edge → T

(B) MST 唯一不保證每個 cut 的 light edge 唯一。反例:\(a\)–\(b\) 1、\(b\)–\(c\) 1、\(a\)–\(c\) 3:MST 只有 \(\{ab,bc\}\);把 \(b\) 單獨切出的 cut,crossing edge \(ab\)、\(bc\) 權重都是 1,light edge 不唯一 → F(敘述錯誤)

(C) 每個 cut 的 light edge 都唯一 → MST 唯一 → T

(D) 最大權重的邊在 cycle 上,拿掉不影響 MST → T

(E) 最小權重邊一定屬於某棵 MST → T

(B)

292.5 分

The digraph shown below is a flow network from source \(s\) to sink \(t\), where the number on each edge represents the edge capacity. What is the minimum cut capacity between \(s\) and \(t\)? (A) 20 (B) 21 (C) 22 (D) 23 (E) 24

(圖:\(s\to a\) 18、\(s\to b\) 13、\(a\to c\) 12、\(a\)、\(b\) 之間兩條弧 10 與 4、\(a\to d\) 1、\(c\to b\) 9、\(d\to c\) 7、\(b\to d\) 14、\(c\to t\) 20、\(d\to t\) 4)

sabcdt18131210419714204

用下一題的 max flow 23,residual graph 上從 \(s\) 走得到的是 \(S=\{s,a,b,d\}\),\(T=\{c,t\}\)

跨過 cut 的邊容量
\(a\to c\)12
\(d\to c\)7
\(d\to t\)4
合計23

\(c\to b\) 是從 \(T\) 往 \(S\),不算

(D)

302.5 分

The digraph shown below is a flow network from source \(s\) to sink \(t\), where the number on each edge represents the edge capacity. What is the value of the maximum flow from \(s\) to \(t\)? (A) 19 (B) 20 (C) 21 (D) 22 (E) 23(同第 29 題的圖)

augmenting path增量累計
\(s\to a\to c\to t\)1212
\(s\to b\to d\to t\)416
\(s\to b\to d\to c\to t\)723

此時 \(a\to c\)、\(d\to c\)、\(d\to t\) 都滿,residual graph 已到不了 \(t\);max flow 等於上一題的 min cut 23

(E)

312.5 分

Which of the following algorithm employs dynamic programming? (A) Prim's algorithm for finding a minimum spanning tree (B) Dijkstra algorithm for solving the single source shortest paths problem (C) Floyd-Warshall for solving the all pair shortest paths problem (D) Huffman algorithm for constructing a Huffman code (E) Kruskal's algorithm for finding a minimum spanning tree

Floyd–Warshall 的第 \(k\) 輪表由第 \(k-1\) 輪推出 → DP;Prim、Dijkstra、Huffman、Kruskal 都是 greedy

(C)

322.5 分

Which of the following are false?

i. The worst-case running time and expected running time are equal to within constant factors for any randomized algorithm.

ii. Sorting 6 elements with a comparison sort requires at least 10 comparisons in the worst case.

iii. In Blum, Floyd, Pratt, Rivest, and Tarjan [1973] worst-case linear-time order statistics algorithm, instead of dividing the \(n\) elements into groups of 5, dividing them into groups of 3 gives the same linear time complexity.

iv. If a dynamic programming problem satisfies the optimal substructure property, then a locally optimal solution is a globally optimal.

(A) i (B) i, iii, iv (C) ii, iii, iv (D) i, iv (E) ii, iv

i randomized quicksort 的 worst case \(O(n^2)\),expected \(O(n\log n)\) → F(敘述錯誤)

ii \(\lceil\log_26!\rceil=\lceil\log_2720\rceil=10\) → T

iii 5 個一組:\(T(n/5)+T(7n/10)+O(n)\),係數和 \(\lt1\),線性;3 個一組:\(T(n/3)+T(2n/3)+O(n)\),係數和 \(=1\),變 \(O(n\log n)\) → F

iv optimal substructure 只讓你用子問題最佳解組出答案;「局部最佳就是全域最佳」是 greedy-choice property → F

錯的是 i, iii, iv:(B)

332.5 分

Which of the following are true? i. The maximum flow problem is in P ii. If a problem is in P, it must also be in NP iii. If a problem is in NP, it must also be in P iv. If a problem is NP-complete, it must also be in NP

(A) i, ii (B) i, ii, iii, iv (C) i, iii, iv (D) i, ii, iv (E) i, iv

i max flow 有多項式時間演算法 → T

ii \(P\subseteq NP\) → T

iii \(NP\subseteq P\) 是未解問題,不能說「一定」 → F

iv NP-complete 的定義本身包含「屬於 NP」 → T

(D)

342.5 分

Huffman codes are used as a very effective technique for compressing data. A data file of 100 characters contains only the characters a–f, with frequency 45, 12, 13, 9, 16, and 5. Use the Huffman variable-length codes, the file can be encode in how many bits? (A) 224 (B) 300 (C) 100 (D) 124 (E) 324

每次取兩個最小的合併

次序取出新節點剩下
15, 91412, 13, 14, 16, 45
212, 132514, 16, 25, 45
314, 163025, 30, 45
425, 305545, 55
545, 55100100

總 bit 數 = 所有 internal node 的和:\(14+25+30+55+100=224\)

(A)

352.5 分

Complete the pseudo code below for the counting sort algorithm. We assume Input: \(A[1..n]\), where \(A[j]\in\{0,1,\ldots,k\}\) for \(j=1,2,\ldots,n\). Array \(A\) and values \(n\) and \(k\) are given as parameters. Output: \(B[1..n]\), sorted. \(B\) is assumed to be already allocated and is given as a parameter. Auxiliary storage: \(C[0..k]\).

COUNTING-SORT(A, B, n, k)
  for i <- 0 to k:        C[i] <- 0
  for j <- 1 to n:        C[A[j]] <- C[A[j]] + 1
  for i <- 1 to k:        C[i] <- C[i] + C[i-1]
  for j <- n downto 1:
      ______________

(A) A[B[C[j]]] ← A[j]; C[A[j]] ← C[A[j]]−1 (B) B[C[A[j]]] ← A[j]; C[A[j]] ← C[A[j]]+1 (C) B[C[A[j]]] ← A[j]; C[j] ← C[j]−1 (D) B[A[j]] ← A[j]; A[j] ← A[j]−1 (E) B[C[A[j]]] ← A[j]; C[A[j]] ← C[A[j]]−1

前綴和做完之後,\(C[A[j]]\) 是 key \(A[j]\) 在輸出中最後一個位置

把 \(A[j]\) 放進 \(B[C[A[j]]]\),再把 \(C[A[j]]\) 減 1,下一個相同的 key 才會往前放,由後往前掃才 stable

(A) 把 B 當成索引、寫進 A 的某格,位置不對;(B) 要減 1,不是加 1;(C) 減的是 \(C[j]\),不是 \(C[A[j]]\);(D) 用 key 當 index,位置完全不對

B[C[A[j]]] ← A[j]; C[A[j]] ← C[A[j]] − 1:(E)

362.5 分

For a chain of matrix \(A_1A_2A_3A_4\) with their dimensions in the following table, parenthesize the product to minimize the number of scalar multiplications. What is the minimum number of scalar multiplications?

matrixdimension
\(A_1\)2×3
\(A_2\)3×5
\(A_3\)5×4
\(A_4\)4×2

(A) 82 (B) 72 (C) 112 (D) 99 (E) 70

\(p=(2,3,5,4,2)\)

\(A_2\)\(A_3\)\(A_4\)
\(A_1\)307082
\(A_2\)6070
\(A_3\)40

\(m[1,3]=\min(0+60+2\cdot3\cdot4,\ 30+0+2\cdot5\cdot4)=\min(84,70)=70\);\(m[2,4]=\min(0+40+3\cdot5\cdot2,\ 60+0+3\cdot4\cdot2)=\min(70,84)=70\)

\(m[1,4]\):\(k=1\):\(0+70+2\cdot3\cdot2=82\);\(k=2\):\(30+40+2\cdot5\cdot2=90\);\(k=3\):\(70+0+2\cdot4\cdot2=86\)

82,括號化 \((A_1(A_2(A_3A_4)))\):(A)

372.5 分

Assume that quicksort is being used to sort an array \(A\) with values 4 2 8 7 3 1 5 6. After the first invocation to the partition algorithm the array looks like this: 2 3 1 4 7 5 8 6. Which value could have been chosen as the pivot? (A) 4 (B) 7 (C) 1 (D) 3 (E) 8

partition 之後 pivot 落在它的最終位置,左邊都小於它、右邊都大於它

2 3 1 | 4 | 7 5 8 6:4 的左邊 \(\{2,3,1\}\) 都小於 4,右邊 \(\{7,5,8,6\}\) 都大於 4

其他數都不滿足:例如 7 的右邊有 5;3 的右邊有 1;8 的右邊有 6

(A)

382.5 分

Professor Chen uses the following algorithm for merging \(k\) sorted lists, each having \(n/k\) elements. He takes the first list and merges it with the second list using a linear-time algorithm for merging two sorted lists, such as the merging algorithm used in merge sort. Then, he merges the resulting list of \(2n/k\) elements with the third list, merges the list of \(3n/k\) elements that results with the fourth list, and so forth, until she ends up with a single sorted list of all elements. What is the worst-case running time of the professor Chen's algorithm in terms of \(n\) and \(k\)? (A) \(\Theta(\log(nk))\) (B) \(\Theta(nk)\) (C) \(\Theta(n\log k)\) (D) \(\Theta(\log n\log k)\) (E) \(\Theta(n)\)

第 \(i\) 次 merge 的兩邊長度是 \(in/k\) 與 \(n/k\),成本 \(\Theta(in/k)\),\(i=1..k-1\)

\(\sum_{i=1}^{k-1}\Theta\!\left(\dfrac{in}{k}\right)=\Theta\!\left(\dfrac nk\cdot k^2\right)=\Theta(nk)\)

(B)

392.5 分

A hiker faces the Knapsack problem. There are 7 items to be packed into the knapsack, each with value \(v_i\) and weight \(w_i\) as shown in the following table.

\(i\)1234567
\(v_i\)86153259
\(w_i\)2354326

The knapsack, which is initially empty, can hold a maximum weight of 16, so some item(s) must be left behind. The optimality criterion is to maximize the total value of the items that are placed in the knapsack. The hiker fills the knapsack one item at a time. Now consider the following two cases: (a) fractions of items cannot be packed, and (b) fractions of items can be packed. What are the optimal values of the items that are packed in these two cases respectively? Give the answer in the form of (no-fractions-allowed, fractions-allowed).

(A) (28, 30.5) (B) (37, 40) (C) (30, 34) (D) (38, 40) (E) (34, 38.5)

fractional:依 \(v/w\) 由大到小:item 1(4)、3(3)、6(2.5)、2(2)、7(1.5)…

取 1、3、6、2:重 \(2+5+2+3=12\),值 \(8+15+5+6=34\);剩 4 單位重量取 item 7 的 \(\tfrac46\):\(9\times\tfrac46=6\)

合計 \(34+6=40\)

0/1:總重 25、總值 48,容量 16 → 要丟掉重量 \(\ge9\) 且價值最小的組合

丟 item 4、5、6:重 \(4+3+2=9\),值 \(3+2+5=10\) → 剩 \(48-10=38\),剛好重 16(item 1、2、3、7:\(8+6+15+9=38\))

用 DP 檢查 0/1 的最佳值也是 38

(38, 40):(D)

402.5 分

Determine the expected search cost of an optimal binary search tree for a set of \(n=5\) keys with the following probabilities. We assume that all searches are successful.

key12345
probability.25.2.05.2.3

(A) 2.15 (B) 1.15 (C) 2.10 (D) 1.10 (E) 2.0

只有成功搜尋,沒有 dummy key:\(w[i,j]=w[i,j-1]+p_j\),\(e[i,j]=\min_{i\le r\le j}\big(e[i,r-1]+e[r+1,j]+w[i,j]\big)\),\(e[i,i-1]=0\)

\(e[i,j]\)\(j=1\)2345
\(i=1\)0.250.650.801.252.10
\(i=2\)0.200.300.751.35
\(i=3\)0.050.300.85
\(i=4\)0.200.70
\(i=5\)0.30

\(e[1,5]\):\(r=2\):\(e[1,1]+e[3,5]+1=0.25+0.85+1=2.10\);\(r=4\):\(e[1,3]+e[5,5]+1=0.80+0.30+1=2.10\);其他較大

一棵最佳樹(root \(k_2\)):

k1k2k3k4k5

2.10:(C)

109–115 的詳解也是這樣寫的。

國立陽明交通大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 579 頁,逐題推導。

購買 · NT$ 850 看交大其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

題目有錯、詳解看不懂,都歡迎來信

想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。

william.chou.123123@gmail.com