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

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

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

逐題詳解

15 分(3 小題全對)

A certain binary tree has the following traversal sequences (each letter represents a tree node): Inorder: FABEGHCD; postorder: FAEHCDGB.

(1) Which of the following is TRUE about the ordering of nodes in the preorder traversal sequence? (A) G is before A (B) H is before E (C) E is before B (D) C is before D (E) A is before F.

(2) Which node appears at the fifth position in the level-order traversal (from left to right) sequence? (A) E (B) F (C) B (D) D (E) G

(3) What is the height of this tree? Here we assume the lowest leaf node has height 1. (A) 3 (B) 4 (C) 5 (D) 6 (E) 7

postorder 的最後一個是 root:B。inorder 中 B 左邊 F A 是左子樹,右邊 E G H C D 是右子樹

左子樹(inorder F A,postorder F A):root A,F 是它的左子

右子樹(inorder E G H C D,postorder E H C D G):root G;左邊 E;右邊(inorder H C D,postorder H C D)root D,D 的左邊 H C(root C,左子 H)

FABEGHCD

preorder:B A F G E D C H

(1) (A) A 在 G 前面 F;(B) E 在 H 前面 F;(C) B 是 root,在最前面 F;(D) D 在 C 前面 F;(E) A 在 F 前面 T → (E)

(2) level-order:B;A, G;F, E, D;C;H → B A G F E D C H,第 5 個是 E → (A)

(3) 最長路徑 B–G–D–C–H 共 5 個節點,最下面的 leaf 高度為 1 → 高度 5 → (C)

第 3 小題官方更正為 A 或 C 或 AC 皆給分

(1) E (2) A (3) C

25 分(3 小題全對)

Consider the conversion of the following infix expression to its postfix form: a/(b+c*d)*(e-f)+g

(4) Consider the stack used for this conversion. What is the maximum number of tokens in this stack at any moment during the conversion? (A) 1 (B) 2 (C) 3 (D) 4 (E) 5

(5) The first operator that appears in the postfix expression is (A) ( (B) * (C) / (D) + (E) -

(6) In the postfix expression, which of these tokens is an operand? (A) the last token (B) the eighth token (C) the sixth token (D) the tenth token (E) all of the above

postfix:a b c d * + / e f - * g +

(4) 用 stack 轉換的過程:掃到 a 輸出;/ 入 stack;( 入;b 輸出;+ 入;c 輸出;* 入;此時 stack = / ( + *,4 個 token(最多);d 輸出;) 把 *、+ 彈出,直到 (

掃到stack(底→頂)輸出
aa
//
(/ (
b/ (a b
+/ ( +
c/ ( +a b c
*/ ( + *
d/ ( + *a b c d
)/a b c d * +
**(先彈出同優先權的 /)a b c d * + /
(* (
e* (… e
-* ( -
f* ( -… f
)*… -
++(先彈出 *)… *
g+… g
結束… +

最大 4 個 token → (D)

(5) 第一個出現的運算子是 *(c d *) → (B)

(6) token 位置:1 a、2 b、3 c、4 d、5 *、6 +、7 /、8 e、9 f、10 -、11 *、12 g、13 +;第 8 個 e 是 operand,最後一個 + 、第 6 個 +、第 10 個 - 都是運算子 → (B)

(4) D (5) B (6) B

35 分(3 小題全對)

Consider the following code of a singly linked list, with the function F to "rotate" the list by moving the first item of the list to the end of the list. The input to the function is the address of first, the pointer to the first item in the list. You need to select the correct expressions to put into the code in the following three questions so that the function works correctly.

struct node { int data; struct node *next; };
void F(struct node **pfirst) {
    struct node *x, *y, *first = *pfirst;
    if (first && first->next) {
        x = first;
        y = *pfirst = ①;
        while (y->next) y = y->next;
        ② = x;
        ③ = NULL;
    }
}

(7) What expression should be in ①? (A) x (B) x->next (C) y->next (D) first (E) x->next->next

(8) What expression should be in ②? (A) y (B) x->next (C) y->next (D) first (E) *pfirst

(9) What expression should be in ③? (A) x (B) x->next (C) y->next (D) first (E) *pfirst

目標:新的第一個是原來第二個,接著 y 走到最後一個,把 x(原第一個)接在最後,x 成為最後一個

效果
①x->next*pfirst 與 y 都指向原第二個
②y->nexty 是尾端,y->next = x 把原第一個接上去
③x->nextx->next = NULL,x 是新的尾端

順序不能反:③ 必須在 ① 之後,因為 ① 要用到 x->next 的舊值

(7) B (8) C (9) B

45 分(3 小題全對)

A binary search tree is constructed by inserting nodes with the following keys into an initially empty tree, in the given order: 10, 2, 4, 6, 8, 1, 3, 5, 12. The nodes are represented by their keys in the questions.

(10) Which of these nodes is at level 3? (The root is at level 1.) (A) 6 (B) 12 (C) 3 (D) 5 (E) 1

(11) How many leaf nodes are there in this tree? (A) 4 (B) 2 (C) 3 (D) 6 (E) 5

(12) Let us delete node 4, and then delete node 2. Which of these relations between nodes 3 and 6 is impossible in the resulting tree? (A) 3 is the parent of 6 (B) 3 is the grandparent of 6 (C) 3 is the sibling of 6 (D) 3 is a child of 6 (E) none of the above.

12345681012

(10) level 3 的節點是 1 與 4(level 1:10;level 2:2、12) → 1 → (E)

(11) leaf:1、3、5、8、12,共 5 個 → (E)

(12) 刪除 4(有兩個子)可用 inorder successor(5)或 predecessor(3)取代,再刪 2(有兩個子)也可用 successor 或 predecessor 取代;各種做法的結果:

刪 4 用刪 2 用結果中 3 與 6 的關係
successor 5successor 33 → 5 → 6:3 是 6 的 grandparent
predecessor 3predecessor 11 → 3 → 6:3 是 6 的 parent
successor 5predecessor 15 的子是 3 與 6:3 與 6 是 sibling
predecessor 3successor 33 取代 2,3 的右子 6:3 是 6 的 parent

四種做法的結果裡,3 是 6 的 parent、grandparent 或 sibling,沒有 3 在 6 底下的情況 → (D) 不可能

(10) E (11) E (12) D

(13) A (14) D (15) A

" id="q5">
55 分(3 小題全對)

A red-black tree is constructed by inserting nodes with the following keys into an initially empty tree, in the given order: 10, 2, 4, 6, 8, 1, 3, 5, 7, 9. The nodes are represented by their keys in the following questions.

(13) Which of the following node pairs contains two black nodes? (A) 2 and 6 (B) 4 and 8 (C) 7 and 10 (D) 3 and 10 (E) 4 and 5

(14) How many red nodes are there in this tree? (A) 3 (B) 4 (C) 5 (D) 6 (E) 7

(15) Which node is the root? (A) 4 (B) 5 (C) 6 (D) 8 (E) 10

依 CLRS 的插入規則逐步調整(叔叔紅 → recolor;叔叔黑 → 旋轉+recolor):

插入插入後的樹(root(左,右);B=黑、R=紅)
1010B
210B(2R,)
44B(2R,10R)
64B(2B,10B(6R,))
84B(2B,8B(6R,10R))
14B(2B(1R,),8B(6R,10R))
34B(2B(1R,3R),8B(6R,10R))
54B(2B(1R,3R),8R(6B(5R,),10B))
74B(2B(1R,3R),8R(6B(5R,7R),10B))
94B(2B(1R,3R),8R(6B(5R,7R),10B(9R,)))

12345678910

黑色:4、2、6、10;紅色:1、3、8、5、7、9

(13) 2 與 6 都是黑 → (A)

(14) 紅色 1、3、5、7、8、9 共 6 個 → (D)

(15) root 是 4 → (A)

65 分(3 小題全對)

This question is about Array:

(16) Which of the following 3 statements (a), (b) and (c) is(are) true? (a) C++ code, char *A; A=new char[1000]; cout << A[10] and cout << *(A+10) get the same output. (b) Array supports random access, an array A of length n, accessing A[1000] needs more time than to accessing A[1]. (c) Array is the most fundamental data structure. We can use it to implement any data structure, even the user define structure. (A) Only (a) is true. (B) Only (b) is true. (C) (a) and (b) are true, (c) is not. (D) (a) and (c) are true, (b) is not. (E) (b) and (c) are true, (a) is not.

(17) A length n vector (linear algebra vector) can be stored in an array. Which of the following statement(s) is(are) true? (a) The operation of adding two vectors takes Θ(n) time and Θ(n) space. (b) If the two vectors are sparse; there are m out of n non-zero terms. We can store the m terms in an array of length m. Each entry stores the index and the value. The operation of adding two vectors takes O(m) time and Θ(n) space. (c) The two sparse vectors as (b), we now store the two sparse vectors in the linked list. Each node has the index and the value. The operation of adding two vectors takes the same O(m) time and Θ(n) space. We prefer to use linked list representation because we don't have to worry about the number of non-zero terms that may not be m after addition. (A) Only (a) is true. (B) Only (c) is true. (C) (a) and (b) are true, (c) is not. (D) (a) and (c) are true, (b) is not. (E) (b) and (c) are true, (a) is not.

(18) C string is implemented using array. Given two string pat and text, string matching is to determine whether there is a matching of pat in text. Let the length of pat and text be respectively m and n. (a) A very efficient string matching algorithm needs to compute a failure function which is obtained from pat and text. (b) Using the failure function, string matching can be done in O(m+n) time. (c) A naive approach without using the failure function takes O(mn) time. (A) Only (a) (B) Only (c) (C) (a) and (b), (c) is not (D) (a) and (c), (b) is not (E) (b) and (c), (a) is not.

(16) (a) A[10] 與 *(A+10) 同義 → T;(b) array 的 random access 是 \(O(1)\),存取 A[1000] 與 A[1] 同樣快 → F;(c) array 可以實作其他資料結構 → T → (D)

(17) (a) 向量相加逐項加,\(\Theta(n)\) 時間與空間 → T;(b) 稀疏向量用長度 \(m\) 的 array 存,相加只需 \(O(m)\) 時間,空間也是 \(O(m)\),不是 \(\Theta(n)\) → F;(c) 同理空間是 \(O(m)\),不是 \(\Theta(n)\) → F → (A)

(18) (a) failure function 只由 pat 算出,不用 text → F;(b) KMP 的 \(O(m+n)\) → T;(c) naive 的 \(O(mn)\) → T → (E)

(16) D (17) A (18) E

75 分(3 小題全對)

This question is about Priority Queue.

(19) About the binary heap, which of the following statement(s) is(are) true? (a) Binary heap is a complete binary tree stored in an array, so we do not have to store the left-child and right-child pointers. (b) Building a min-heap of size n takes Θ(n log n) time. (c) Merging 2 min-heaps takes Θ(n) time. (A) (a) and (b) are true, (c) is not. (B) (a) and (c) are true, (b) is not. (C) (b) and (c) are true and (a) is not. (D) all of (a), (b), and (c) are correct. (E) only (a) is true.

(20) Identify the leftist tree(s).(圖:(a) 左右各一個子節點、各自再有兩個 leaf 的完全二元樹;(b) 一條向左下延伸的 4 個節點鏈;(c) root 的左子樹是一條「左子、再右子」的鏈,右子是 leaf)(A) (a) and (b) are leftist tree, (c) is not. (B) (b) and (c) are leftist trees, (a) is not. (C) (a) and (c) are leftist tree, (b) is not. (D) all of (a), (b), and (c) are leftist tree. (E) Only (c) is leftist tree.

(21) Which of the following statement(s) is(are) true. (a) The height of the leftist tree is O(log n). (b) Using a min-leftist to implement priority queue, it supports O(log n) merging two min-heaps. (c) Insert arbitrary and delete min are carried out by merging two min-heaps, so the time required is O(log n). (A) All of (a), (b), and (c) are true. (B) (a), (b) are true, (c) is not. (C) (b), (c) are true, (a) is not. (D) (a), (c) are true, (b) is not. (E) only (a) is true.

(19) (a) 完全二元樹存在 array,不用存 child pointer → T;(b) build heap 用 bottom-up 是 \(\Theta(n)\),不是 \(\Theta(n\log n)\) → F;(c) 合併兩個 binary heap 要全部重建 \(\Theta(n)\) → T → (B)

(20) leftist tree:任何節點 \(s(\text{left})\ge s(\text{right})\)(\(s\)=到外部節點的最短距離)

(a) 每個內部節點左右 \(s\) 相同 → 是;(b) 左鏈:每個節點右邊是 null(\(s=0\))、左邊 \(s\ge1\) → 是;(c) 左子樹中那個「只有右子」的節點:左邊 null(\(s=0\))、右邊是 leaf(\(s=1\)),\(s(\text{left})\lt s(\text{right})\) → 不是 → (A)

(21) (a) leftist tree 的高度可以是 \(O(n)\)(左鏈),只有 rightmost path 是 \(O(\log n)\) → F;(b) merge \(O(\log n)\) → T;(c) insert、delete-min 都靠 merge,\(O(\log n)\) → T → (C)

(19) B (20) A (21) C

85 分(3 小題全對)

This question is about Sorting:

(22) Which of the following 3 statement(s) is(are) true? (a) The worst case of quick sort takes O(n²) time. (b) The worst case of the randomized quick sort takes O(n²) time. (c) Quick sort is divide and conquer approach. After partitioning, we don't spend time for the merging step. (A) (a) and (b) are true, but (c) is not. (B) (a) and (c) are true, but (b) is not. (C) (b) and (c) are true, but (a) is not. (D) all of (a), (b), and (c) are true. (E) Only (a) is true.

(23) Implementation of a sorting algorithm, we can always make it "stable" by using some tricks. We assume that the records are stored in an array. If we want to sort the array into non-decreasing order, which of the statement(s) is(are) true? (a) To the quick sort, each record has 2 keys. The original key is the primary key and the index of the record in the array is secondary key. A comparison starts with comparing the primary key, if primary key is the same, we compare the secondary key. (b) To the merge sort, If we have same key during merging two arrays, the record in the first array is placed before the record in the second array. (c) To the insertion sort, when we insert record i, we insert it after record j where record j is the first one whose key is less than the key of record i. (A) (a) and (b) are true, but (c) is not. (B) (a) and (c) are true, but (b) is not. (C) (b) and (c) are true, but (a) is not. (D) all of (a), (b), and (c) are true. (E) Only (a) is true.

(24) About the lower bound to the sorting problem, which of the following 4 statement(s) is(are) true? (a) Heap sort needs at least n log n comparisons and heap sort can be done in at most n log n operations, heap sort is optimal. (b) Merge sort is divide and conquer approach and the time required is modeled by T(n) = 2T(n/2) + Θ(n). Solving T(n), we get Θ(n log n). Thus merge sort needs at least n log n time. (c) Lower bound for sorting algorithms using comparison operation established under the linear decision tree model. (d) Sorting algorithms using non-linear operators have chance to beat the O(n log n) lower bound. (A) (a) (b) (c) are true, but (d) is not. (B) (a) (b) (d) are true, but (c) is not. (C) (a) (c) (d) are true but (b) is not. (D) (b) (c) (d) are true but (a) is not. (E) All of (a) (b) (c) and (d) are true.

(22) (a) quick sort worst case \(O(n^2)\) → T;(b) randomized quick sort 的 worst case 仍是 \(O(n^2)\)(機率很小,但最壞情況存在) → T;(c) quick sort 是 divide and conquer,partition 之後不需要 merge → T → (D)

(23) (a) 用「原本的 index」當第二鍵,相同鍵的紀錄依原順序排,stable → T;(b) merge 時鍵相同,取第一個 array 的先放,stable → T;(c) insertion sort 要 stable,是把紀錄 \(i\) 插在「最後一個鍵 \(\le\)」它的紀錄之後;題目寫「第一個鍵小於它的」 → F → (A)

(24) (a) heap sort 的 comparison 下界與上界都是 \(n\log n\),optimal → T;(b) merge sort 的遞迴式只給出它自己的上界,不能證明所有演算法的下界 → F;(c) comparison sort 的下界是在 decision tree(線性 decision tree)模型下得到 → T;(d) 使用非線性運算(如 counting/radix sort 的位址運算)可以突破 \(n\log n\) → T → (C)

(22) D (23) A (24) C

95 分(3 小題全對)

Consider an undirected graph G=(V,E), an edge is a relation over the vertices; the relation is reflexive, symmetric, and transitive. Computing the connected components in the G is the same as to compute the equivalence classes of the vertices, V. Let the number of vertices be n and the number of edges be m.

(25) Which of the following statement(s) is(are) true? (a) Both breadth first search and Union/Find algorithm can compute the equivalence classes. (b) Both depth first search and breadth first search can compute the equivalence classes. Performance of depth first search is better than the breadth first search. (c) If graph G is big (large m and large n), among the breadth first search, depth first search, and Union/Find, we should choose depth first search because of space consideration. (A) (a) and (b) are true, but (c) is not. (B) (a) and (c) are true, but (b) is not. (C) (b) and (c) are true, but (a) is not. (D) all of (a), (b), and (c) are true. (E) Only (a) is true.

(26) Union/Find implements set operations by using forest. A set is a rooted tree where the root presents the set. Recall that there are n vertices and m edges. About computing the equivalence classes, which of the statement(s) is(are) true. (a) Union operation can be done in O(1) time, there are at most m Unions. (b) The time required by Find(i) operation depends on the path length from i to the root. There are at most m Finds. (c) Applying weighting rule for Union, the time required for a Find is bounded above by O(log n). Applying collapsing rule for Find, the path length can be further reduced. (A) (a) and (b) (B) (a) and (c) (C) (b) and (c), (a) is not (D) all (E) Only (a)

(27) Function f₁(k)=2^k, f₁ is the exponential function. Function f₂(k)=pow(2,pow(2,pow(2,…pow(2,2)))), a tower of power of 2, and the height of the tower is k. If n=f₁(k), then k=log n. If n=f₂(k), we say k=log* n. Which of the following statement(s) is(are) true? (a) f₂(k) grows faster than f₁(k). (b) log* n grows faster than log n. (c) According the speed of growth of the functions, we have n log n > n log* n > n^a, where 1

(25) (a) BFS 與 Union/Find 都能算 connected component(equivalence class) → T;(b) DFS 與 BFS 都可以,但 DFS 並沒有比較快(都是 \(O(n+m)\)) → F;(c) DFS/BFS 都要存整張圖與 stack/queue,Union/Find 只要 \(O(n)\) 的 parent array,大圖該選 Union/Find,不是 DFS → F → (E)

(26) (a) Union 只要兩個 root 就是 \(O(1)\),但實際成功的 Union 至多 \(n-1\) 次(不是 \(m\) 次;\(m\) 次是 Union 的呼叫數) → 敘述不成立(官方答案此項為 F);(b) Find 的時間由路徑長決定,至多 \(m\) 次(兩端各一次,\(2m\)) → T;(c) weighting rule 使樹高 \(O(\log n)\),collapsing rule 再縮短路徑 → T → (C)

(27) (a) tower 的成長遠快於 \(2^k\) → T;(b) \(\log^*n\) 長得比 \(\log n\) 慢得多 → F;(c) \(n^a\)(\(a\gt1\))的成長比 \(n\log n\) 快,次序寫反了 → F → (E)

(25) E (26) C (27) E

105 分(3 小題全對)

About amortized cost analysis and some related algorithms.

(28) Which of the following statement(s) is(are) true? (a) A sequence of operations, some of those can be done very fast but some can not. Amortized cost is to take the average of the costs of the sequence of operations. Similarly to the analysis of the quick sort, it is average case analysis and we estimate the expected cost. (b) Amortized cost can be calculated by the accounting method. We pre-pay some cost. If an operation costs more, it can be covered by the pre-paid. All we have to make sure is that the pre-paid is always positive. (c) STL in C++, the vector is implemented by using dynamic table. When an insertion into a full table, the vector size is double, all entries in the old table are copied into the new table, then the insertion is made. A sequence of n insertion takes linear time because a single insertion has amortized cost O(1). (A) (a) and (b), (c) is not (B) (a) and (c), (b) is not (C) (b) and (c), (a) is not (D) all (E) Only (a)

(29) About heap, which of the following statement(s) is(are) true? (a) Fibonacci heap support efficient deleting minimum, inserting arbitrary, and merging of two min-heaps. (b) Fibonacci heap supports delete minimum in O(log n) amortized time. (c) Fibonacci heap supports constant (O(1)) amortized cost for merging and inserting arbitrarily. (A) (a) and (b), (c) is not (B) (a) and (c), (b) is not (C) (b) and (c), (a) is not (D) all of (a), (b), and (c) are true (E) Only (a)

(30) Queue operations can be implemented using 2 stacks, S1 and S2. inQueue(e) == S1.push(e); dQueue() == if (S2.empty()) then { if (S1.empty()) the queue is empty; else pop out everything in S1 and push them into S1 (原卷此處印成 S1); return S1.pop(); } If we have a sequence of n inQueue and dQueue, which of the following statement(s) is(are) true? (a) inQueue(e) can be done in constant time. (b) A single dQueue() takes O(n) time because we may need to pop out O(n) items in the S2. (c) The sequence of n inter-mix inQueue and dQueue operations takes Θ(n) time. (A) (a) and (b), (c) is not (B) (a) and (c), (b) is not (C) (b) and (c), (a) is not (D) all (E) Only (a)

(28) (a) amortized cost 是整個 sequence 的最壞總成本除以操作數,沒有機率,不是 average case → F;(b) accounting method:預付的 credit 隨時不能為負 → T;(c) dynamic table 加倍:\(n\) 次 insertion 總成本 \(O(n)\),每次 amortized \(O(1)\) → T → (C)

(29) (a)(b)(c) Fibonacci heap:insert、merge \(O(1)\) amortized,delete-min \(O(\log n)\) amortized → 都 T → (D)

(30) 程式碼本意是 dQueue 把 S1 全部 pop 出來 push 進 S2,再從 S2 pop(原卷把 S2 印成 S1,與敘述 (b) 的 S2 對不上);(a) inQueue 是一次 push,常數時間 → T;(b) 單次 dQueue 最壞要搬 \(O(n)\) 個 → T;(c) 每個元素至多被搬一次,\(n\) 個操作總共 \(\Theta(n)\) → T → (D);官方因題目有筆誤,第 30 題送分

(28) C (29) D (30) 送分

115 分(3 小題全對)

Given a hash table of size m, please answer following questions.

(31) Under the uniform hashing assumption, if we use the hash table with open addressing to hash 3 keys, the probability that the third inserted key needs exactly three probes before being inserted into the table is exactly (A) 2/m (B) 2/(m−1) (C) 2/m(m−1) (D) 1/(m−1) (E) 1/m(m−1)

(32) We use the hash table with open addressing to hash n keys, where n is less than m. Under the uniform hashing assumption, the expected cost to insert another element into the table is at most (A) 1+n/m (B) 1/(1−n/m) (C) n/m (D) 1/(n/m) (E) 1−n/m

(33) After hashing n keys into the hash table that uses chaining to handle collisions, we hash two new keys k₁ and k₂. Under the simple uniform hashing assumption, the probability that k₁ and k₂ are hashed into the same table location is exactly (A) 1/n (B) 1/m (C) 1/nm (D) n/m (E) m/n

(31) 前兩個 key 佔了 2 個不同的 slot。第三個 key 的探測序列是 \(m\) 個 slot 的隨機排列:第一次落在已佔的 slot 的機率 \(\tfrac2m\);第二次落在剩下的另一個已佔 slot 的機率 \(\tfrac1{m-1}\);第三次就落在空的

\(\Pr=\dfrac2m\cdot\dfrac1{m-1}=\dfrac{2}{m(m-1)}\) → (C)

(32) uniform hashing 下,\(\alpha=n/m\lt1\),一次不成功探測(insert)的期望次數 \(\le\dfrac1{1-\alpha}\) → (B)

(33) simple uniform hashing:每個 key 各自等可能落在 \(m\) 個位置,\(k_2\) 與 \(k_1\) 同位置的機率 \(=\dfrac1m\) → (B)

(31) C (32) B (33) B

125 分(3 小題全對)

Solve the recurrences in (34) to (36). Whenever possible, apply the Master Theorem. If the Master Theorem does not apply, use a recursion tree or substitution method to determine your answer.

(34) \(T(n)=7T(n/2)+n^3\) (35) \(T(n)=T(n-2)+n\) (36) \(T(n)=8T(n/2)+n\) 選項皆為:(A) Θ(n) (B) Θ(log n) (C) Θ(n²) (D) Θ(n log n) (E) Θ(n³)

(34) \(a=7,\ b=2\),\(n^{\log_27}=n^{2.807}\);\(f(n)=n^3=\Omega(n^{2.807+\varepsilon})\),regularity \(7(n/2)^3=\tfrac78n^3\le c\,n^3\) → case 3 → \(\Theta(n^3)\) → (E)

(35) \(T(n)=n+(n-2)+(n-4)+\cdots\approx\dfrac{n^2}{4}\) → \(\Theta(n^2)\) → (C)

(36) \(a=8,\ b=2\),\(n^{\log_28}=n^3\),\(f(n)=n=O(n^{3-\varepsilon})\) → case 1 → \(\Theta(n^3)\) → (E)

(34) E (35) C (36) E

135 分(3 小題全對)

Consider the following algorithm. The algorithm FIND_MODE(A) has input array arr containing n different integer values.

#include <stdio.h>
int findPeakUtil(int arr[], int low, int high, int n)
{
    int mid = low + (high - low)/2;
    if ((mid == 0 || arr[mid-1] <= arr[mid]) &&
        (mid == n-1 || arr[mid+1] <= arr[mid]))
        return mid;
    else if (mid > 0 && arr[mid-1] > arr[mid])
        return findPeakUtil(arr, low, (mid - 1), n);
    else return findPeakUtil(arr, (mid + 1), high, n);
}
int FIND_MODE(int arr[], int n)
{
    return findPeakUtil(arr, 0, n-1, n);
}

(37) FIND_MODE employs what approach? (A) divide and conquer (B) dynamic programming (C) greedy (D) heuristic (E) approximation

(38) What is the time complexity of FIND_MODE? (A) O(log n) (B) O(n) (C) O(n log n) (D) Θ(n²) (E) None of above

(39) Assume input array arr containing 1, 3, 20, 4, 1, 0, what is the output? (A) 1 (B) 0 (C) 20 (D) 3 (E) 2

(37) 每次看中點,往較大的那一邊(peak 一定在那一邊)遞迴,把問題對半分 → divide and conquer(binary search 式) → (A)

(38) 每次問題大小減半,\(T(n)=T(n/2)+O(1)\) → \(O(\log n)\) → (A)

(39) \(\text{low}=0,\ \text{high}=5\):\(\text{mid}=2\),\(\text{arr}[2]=20\);\(\text{arr}[1]=3\le20\)、\(\text{arr}[3]=4\le20\),馬上 return mid

輸出是 index 2,不是值 20:(E)

(37) A (38) A (39) E

145 分(3 小題全對)

Calculate the minimum number of multiplications necessary to find the matrix product ABCDE. The dimensions of each of the matrices are listed below:

Matrix\(A_1\)\(A_2\)\(A_3\)\(A_4\)\(A_5\)
Dimensions2×44×33×22×55×1

(40) What kind of algorithm you should apply to obtain an optimal solution? (A) divide and conquer (B) dynamic programming (C) greedy (D) heuristic (E) approximation

(41) What is the minimum number of multiplications to compute matrix product ABCDE? (A) 24 (B) 56 (C) 64 (D) 36 (E) 28

(42) What is the optimal parenthesization of \(A_1A_2A_3A_4A_5\)? (A) \((A_1A_2)A_3(A_4A_5)\) (B) \((A_1(A_2A_3)(A_4A_5))\) (C) \(A_1(A_2((A_3A_4)A_5))\) (D) \(A_1(A_2A_3)(A_4A_5)\) (E) None of above

(40) matrix-chain multiplication 的標準解法 → dynamic programming → (B)

\(p=(2,4,3,2,5,1)\),\(m[i,j]=\min_k\,m[i,k]+m[k+1,j]+p_{i-1}p_kp_j\)

\(m[i,j]\)\(j=2\)345
\(i=1\)24365636
\(i=2\)246428
\(i=3\)3016
\(i=4\)10

\(m[1,5]\):\(k=1\):\(0+28+2\cdot4\cdot1=36\);\(k=2\):\(24+16+2\cdot3\cdot1=46\);\(k=3\):\(36+10+2\cdot2\cdot1=50\);\(k=4\):\(56+0+2\cdot5\cdot1=66\)

(41) 最小 36 → (D)

(42) 最佳括號化:\(A_1(A_2(A_3(A_4A_5)))\)(\(10+16+28+36\));選項 (A)–(D) 都不是這個 → (E)

(40) B (41) D (42) E

155 分(3 小題全對)

Construct Huffman codes for the letter a, b, c, d, e, and f with corresponding frequencies 6, 11, 7, 9, 18, and 2. Note that we label the left branch 0 and 1 for the right branch in the Huffman tree. For sibling nodes, the frequency of the left node is at most the frequency of the right node.

(43) Huffman invented what kind of algorithm that constructs a Huffman code? (A) divide and conquer (B) dynamic programming (C) greedy (D) heuristic (E) approximation

(44) The letter e should be coded as (A) 01 (B) 1000 (C) 101 (D) 11 (E) 0

(45) The string 01100001011 should be decoded as (A) bcda (B) abcde (C) aedfa (D) cdef (E) None of above

(43) 每次合併最小的兩個,greedy → (C)

合併:\(f(2)+a(6)=8\);\(c(7)+8=15\);\(d(9)+b(11)=20\);\(15+e(18)=33\);\(20+33=53\)(左子頻率較小者放左)

d•b•c•f•a•e

編碼:d=00、b=01、c=100、f=1010、a=1011、e=11

(44) e = 11 → (D)

(45) 01|100|00|1011 → b c d a → (A)

(43) C (44) D (45) A

165 分(3 小題全對)

This question is about the shortest path problem.(Figure 16:A–B 1、B–C 3、A–D 3、A–E 2、B–E 1、B–F 2、C–F 3、D–E 2、E–F 2)

ABCDEF133212322

(46) Which of the following statements is wrong? (A) Topological sort can be used to find the shortest paths when the input graph is a DAG. (B) The Bellman-Ford algorithm can find the shortest path from a single source. (C) The Floyd-Warshall algorithm can find all pairs of shortest paths. (D) When the input graph is a directed acyclic graph, then the shortest path can be found in O(V+E) time. (E) The Dijkstra algorithm cannot find the shortest path correctly when there is a cycle.

(47) Consider an undirected graph G(V, E) as in the Figure 16. Let vertex A be the source, then which of the following statements is correct? (A) The edge DE can appear in a shortest path tree. (B) The edge CF can appear in a shortest path tree. (C) The edge EF can appear in a shortest path tree. (D) The edge BF can appear in a shortest path tree. (E) The shortest path tree has 6 edges.

(48) Consider the following procedure, which of the following statements is wrong? RELAX(u, v, w) { 1. If v.d > u.d + w(u, v) // v.d is the current shortest distance from source to v 2. v.d = u.d + w(u, v) 3. v.p = u } (A) This procedure is used in the Dijkstra algorithm. (B) It takes O(1) amortized time for each call of RELAX by implementing the Dijkstra algorithm with a Fibonacci heap. (C) It takes O(log V) amortized time for each call of RELAX by implementing the Dijkstra algorithm with a binary heap. (D) This procedure is used in the Bellman-Ford algorithm as well. (E) Implementing the Dijkstra algorithm with a priority-queue, Step 2 relates to the decrease-key operation.

(46) (A)(B)(C)(D) 都是標準結論;(E) Dijkstra 要求邊權非負,與有沒有 cycle 無關,有 cycle 也能正確執行 → 錯的敘述 → (E)

(47) 從 A 出發的最短距離:B=1,E=2(A–E 或 A–B–E),D=3(A–D,經 E 是 4),F=3(A–B–F;經 E 是 4),C=4(A–B–C;經 F 是 6)

點距離最短路徑
B1A–B
E2A–E 或 A–B–E
D3A–D
F3A–B–F
C4A–B–C

(A) D 經 E 是 4,大於直接 3,DE 不會出現;(B) C 經 F 是 6,CF 不會出現;(C) F 經 E 是 4,EF 不會出現;(D) BF 在 A–B–F 上 → 可以出現 → (D);(E) 6 個點的 tree 有 5 條邊

(48) (C) binary heap 每次 decrease-key 是 \(O(\log V)\)(worst case),題目寫成 amortized,說法不精確;官方:第 48 題送分

(46) E (47) D (48) 送分

175 分(3 小題全對)

This problem is about the minimum spanning tree problem.(同 Figure 16)

(49) Consider the graph as shown in Figure 16. Which edge does not appear in a minimum spanning tree? (A) BE (B) AE (C) CF (D) BC (E) DE

(50) Consider the following algorithm with the disjoint set data structure to find a minimum spanning tree. MST(G, w): 1. A = empty set 2. For each vertex v Make-Set(v) 3. Sort the edges of G into non-decreasing order by weight 4. For each edge (u, v) in G taken in non-decreasing order by weight 5. If ______ 6. add (u, v) to A 7. Union(u, v) 8. Return A. What is the missing statement in line 5? (A) u=v (B) Find-Set(u) = Find-Set(v) (C) Find-Set(u) == Find-Set(v) (D) Find-Set(u) != Find-Set(v) (E) u!=v

(51) What is the weight of the minimum spanning tree of Figure 16? (A) 7 (B) 8 (C) 9 (D) 10 (E) 11

Kruskal:由小到大掃,權重 1:A–B、B–E;權重 2:A–E(A、E 已經連通,跳過)、B–F、D–E、E–F(跳過);權重 3:B–C、A–D、C–F 其中 C 還沒接

邊權重是否收入
A–B1✓
B–E1✓
A–E2✗(A、B、E 已連通,成環)
B–F2✓
D–E2✓
E–F2✗(成環)
B–C 或 C–F3✓(二選一,另一條成環)

(49) A–E 與 A–B、B–E 成環,且 A–E(2)是環上最大,永遠不在 MST → (B);其他(BE、DE 必選,BC 與 CF 二選一)都可以出現

(50) 只有 \(u\)、\(v\) 在不同集合時才加入(否則成環):\(\text{Find-Set}(u)\ne\text{Find-Set}(v)\) → (D)

(51) \(1+1+2+2+3=9\) → (C)

(49) B (50) D (51) C

185 分(3 小題全對)

This question is about the maximum flow problem.

(52) Suppose the capacity of each edge is integral. Which of the following statements is wrong? (A) It is possible to have integral flows on each edge. (B) It is possible to have fractional flow on some edge. (C) The Push-Relabel algorithm can find the maximum flow in polynomial time. (D) The Edmonds-Karp algorithm can find the maximum flow in O(V²E) time (E) The Edmonds-Karp algorithm can find the augmenting path with DFS.

(53) Which statement is wrong? (A) The flow across any cut is at most the capacity of the cut. (B) The maximum flow is equal to the capacity of a minimum cut. (C) The corresponding residual network of a maximum flow has no augmenting path between any 2 nodes. (D) The length of augmenting path between the source and sink is nondecreasing in the Edmonds-Karp algorithm. (E) The Ford-Fulkerson algorithm does not have a specific way to find the augmenting path.

(54) Which of the following is correct? (A) If there is no augmenting path in a residual network, then the corresponding flow is maximum. (B) If all edges of a flow network have different capacities, then the maximum flow is unique. (C) The maximum bipartite matching can be transformed into a maximum flow problem by assigning distinct capacities to different edges. (D) The residual capacity of an edge can be negative. (E) For each edge, its flow can be larger than its capacity.

(52) (A) 整數容量下存在整數的 max flow → T;(B) max flow 的值是整數,個別邊的流量「可以」是分數(只要滿足 conservation) → T;(C) push-relabel 是多項式時間 → T;(D) Edmonds-Karp 的時間是 \(O(VE^2)\),題目寫成 \(O(V^2E)\) → 錯;(E) Edmonds-Karp 規定用 BFS 找最短 augmenting path,用 DFS 就不是 Edmonds-Karp → 錯

兩個選項 (D)(E) 都是錯的敘述,官方更正為 (D) 或 (E) 或 (DE) 皆給分

(53) (C) 「最大流的 residual network 中任兩個點之間都沒有 augmenting path」是錯的:只有從 \(s\) 到 \(t\) 沒有 augmenting path;其他選項都是標準結論 → (C)

(54) (A) residual network 中沒有 \(s\to t\) 的 augmenting path ⇔ 流是最大 → T;(B) max flow 的值唯一,但流的分佈不一定唯一 → F;(C) 二分匹配轉成 flow,所有邊容量都是 1 → F;(D) residual capacity \(c(u,v)-f(u,v)\ge0\) → F;(E) 流量不超過容量 → F → (A)

(52) D 或 E (53) C (54) A

195 分(3 小題全對)

Consider the following graph algorithm.

A(G, w)   // w defines the weights of the edges
1. Compute G', where G' is obtained from G by adding a new vertex s and new edges (s, v)
   for all v in G(V), and with w(s, v) = 0
2. If Bellman-Ford(G', w, s) == False       // s is the starting vertex
       Print "the input has a negative weight cycle"
3. Else for each vertex v in G'
       Set h(v) to be the shortest distance from s to v, computed by the above Bellman-Ford algorithm
4.     for each edge (u, v) of G' set w'(u, v) := w(u, v) + h(u) - h(v)
5.     let D = (d(u, v)) be a new n by n matrix
6.     for each vertex u in G(V)
7.         run Dijkstra's algorithm on G' with w' and u as the starting vertex
8.         to compute the shortest path distance dist(u, v) from u to v for all v in G(V)
9.         for each vertex v in G(V) set d(u, v) = dist(u, v) + h(v) - h(u)
10.    Return D

(55) Which of the following is wrong? (A) For each edge (u, v) of G', w'(u,v) is non-negative. (B) This algorithm finds all pairs of shortest paths of G. (C) This algorithm is efficient when the number of edges is small. (D) If there is no negative cycle in G', then for some edge (u, v), h(v) > h(u) + w(u, v). (E) h(v) can be negative.

(56) By using the Fibonacci heap, what is the complexity of the above algorithm? (A) O(V+E) (B) O(V² lg V + VE) (C) O(V²) (D) O(E) (E) O(V lg V)

(57) Which of the following is wrong? Let \(p=\langle u=v_0,v_1,\ldots,v_k=v\rangle\) be any path from vertex u to vertex v. (A) p is a shortest path between u and v using weight function w if and only if p is also a shortest path using w' (B) w'(p) = w(p) + h(u) − h(v) (C) The Dijkstra's algorithm is called O(V) times. (D) If there is no cycle in G', then we can replace Bellman-Ford algorithm with the Dijkstra's algorithm (E) It takes O(V) time to compute G' from G.

這是 Johnson 的 all-pairs shortest paths 演算法

(55) (A) \(w'(u,v)=w(u,v)+h(u)-h(v)\ge0\)(\(h\) 是最短距離,滿足三角不等式 \(h(v)\le h(u)+w(u,v)\)) → T;(B)(C)(E) → T;(D) 無負環時對每條邊都有 \(h(v)\le h(u)+w(u,v)\),不可能出現 \(h(v)\gt h(u)+w(u,v)\) → 錯 → (D)

(56) Bellman–Ford \(O(VE)\),加上 \(V\) 次 Dijkstra(Fibonacci heap,每次 \(O(V\lg V+E)\)):\(O(V^2\lg V+VE)\) → (B)

(57) (A)(B) reweighting 使每條路徑的長度同加 \(h(u)-h(v)\),最短路徑不變 → T;(C) \(V\) 次 Dijkstra → T;(E) 建 \(G'\) 要加一個點與 \(V\) 條邊,\(O(V)\) → T;(D) 就算 \(G'\) 沒有 cycle,邊權 \(w\) 仍可能是負的,Dijkstra 不能處理負權;(DAG 要用 topological order 取代 Bellman–Ford) → 錯 → (D)

(55) D (56) B (57) D

205 分(3 小題全對)

This question is about the NP-complete problems.

(58) Which of the following is wrong? (A) If SAT has a polynomial time algorithm, then P = NP. (B) SAT can be reduced to the Vertex Cover problem in polynomial time. (C) 3-SAT problem is as difficult as SAT. (D) 2-SAT can be reduced to 3-SAT and hence is NP-complete. (E) SAT is NP-complete.

(59) Which of the following is correct? (A) If a problem is NP-hard, then it must be NP-complete. (B) Each problem in NP has a polynomial time verifying algorithm without any certificate. (C) Any problem in NP-hard can be reduced to any NP-complete problem in polynomial time. (D) If P = NP, then we can factorize any composite integer in polynomial time. (E) Any problem that can be reduced to SAT is NP-complete.

(60) Which of the following is wrong? (A) Any problem in P can be reduced to SAT in polynomial time. (B) The problem to determine if a graph has a Hamiltonian path is in NP. (C) It takes at most O(N!) time to determine if a graph of N vertices has a Hamiltonian path (D) It takes at most O(2^N) time to determine if a Boolean formula of N variables is satisfiable. (E) A clause of a CNF boolean formula of length > 3 can be transformed into several clauses, where each clause has exactly 3 literals, which all come from the original clauses.

(58) (A)(B)(C)(E) 都是標準結論;(D) 2-SAT 在 P(implication graph 的 SCC),把 2-SAT 歸約到 3-SAT 只證明 2-SAT 不比 3-SAT 難,不能說它是 NP-complete → 錯 → (D)

(59) (A) NP-hard 不一定在 NP → F;(B) NP 的定義是有 certificate 的多項式 verifier → F;(C) NP-hard 的問題不一定能歸約到特定的 NP-complete 問題(反過來才是) → F;(D) 若 \(P=NP\),整數分解(decision 版在 NP)可在多項式時間解出 → T;(E) 能歸約到 SAT 只表示它在 NP,不一定是 NP-hard → F → (D)

(60) (A) P ⊆ NP,任何 NP 問題都能歸約到 SAT → T;(B) Hamiltonian path 在 NP → T;(C) 窮舉 \(N!\) 種排列 → T;(D) 窮舉 \(2^N\) 組真值指定 → T;(E) 拆成 3 個 literal 的子句要引進新的變數,不可能全部來自原來的 literal → 錯 → (E)

(58) D (59) D (60) E

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com