免費詳解 / 中央 / 軟體 / 104

104 中央資工所考古題解答|軟體逐題詳解

104 中央資工所軟體考古題的逐題詳解,共 17 題,免費公開。

逐題詳解

14 分

Which of the following statements is correct about recursive algorithms?

(A) Recursive algorithms always deal with math formulas.

(B) Recursive algorithms tend to be shorter and easier to understand.

(C) Recursive algorithms are easy to design and debug.

(D) Many recursive algorithms cannot be converted into equivalent iterative ones.

(E) Compilers can easily optimize recursive algorithms.

(A) 遞迴不限於數學式(樹、圖走訪、排序) → F

(B) 遞迴寫法短、貼近問題定義 → T

(C) 遞迴的設計與除錯並不容易(終止條件、深度) → F

(D) 任何遞迴都能改寫成迭代(自備 stack) → F

(E) 遞迴不易最佳化(只有 tail recursion 較容易) → F

(B)

24 分

What is the running time of the following code fragment?

for (int i = 0; i < 10; i++)
  for (int j = 0; j < N; j++)
    for (int k = N-2; k < N+2; k++)
      cout << i << " " << j << endl;

(A) \(O(\log N)\) (B) \(O(N\log N)\) (C) \(O(N)\) (D) \(O(N^2)\) (E) \(O(N^3)\)

最外層 10 次、中間 \(N\) 次、最內層 \(k=N-2,\ldots,N+1\) 共 4 次

\(10\times N\times4=40N\) → \(O(N)\)

(C)

34 分

A linear list is being maintained circularly in an array with front and rear set up as for circular queues, by the following functions (for adding and deleting an element from the queue).

template <class KeyType>
void Queue<KeyType>::Add(const KeyType& x)
// add x to the circular queue
{
  int k = (rear+1) % MaxSize;
  if (front == k) QueueFull();
  else queue[rear=k] = x;
}
template <class KeyType>
KeyType* Queue<KeyType>::Delete(KeyType& x)
// remove front element from queue
{
  if (front == rear) { QueueEmpty(); return 0; }
  x = queue[(++front) % MaxSize];
  return &x;
}

Assume that one such queue was created, and five elements were already added into the queue, as shown in Figure below. How many more elements can be added into the queue before the queue is full?

(A) 0 (B) 1 (C) 2 (D) 3 (E) 4

圖:環上 8 格,5 格有資料;front 指在第一個元素前面那一格(這一格是空的,不放資料),rear 指在最後一個元素

\(\text{MaxSize}=8\)

Add:\(k=(\text{rear}+1)\bmod8\);\(k=\text{front}\) 就是 full,所以 front 那格永遠留空,最多放 \(8-1=7\) 個

已放 5 個 → 還能放 \(7-5=2\) 個

(C)

44 分

The height of a complete binary tree with \(n\) nodes, \(H_n\), can be computed by

(A) \(H_n=n\log_2n\) (B) \(H_n=n\log_2n+1\) (C) \(H_n=\log_2n\) (D) \(H_n=\log_2n+1\) (E) \(H_n=\log_2(n+1)-1\)

root 在第 1 層,第 \(k\) 層至多 \(2^{k-1}\) 個節點,前 \(h-1\) 層全滿共 \(2^{h-1}-1\lt n\le2^h-1\)

\(h-1\le\log_2n\lt h\) → \(H_n=\lfloor\log_2n\rfloor+1\) → (D)

(E) 是 root 算第 0 層(以邊數計)、且樹全滿時的寫法;題目的高度以層數計

(D)

54 分

Which of the following data structures cannot be used to represent a graph.

(A) Adjacency Lists (B) Adjacency Matrix (C) Incident Matrix (D) Link List (E) All of the above data structures can be used to represent a graph.

(A)(B)(C) 都是標準的 graph 表示法

(D) linked list 可以存 edge list(每個 node 一條邊),adjacency list 本身就是 linked list → 也能用

→ (A)–(D) 都能用

(E)

64 分

What additional requirement is placed on an array, so that binary search may be used to locate an entry?

(A) The array elements must form a heap. (B) The array must have at least 2 entries. (C) The array must be sorted. (D) The size of the array must be a power of two. (E) The array must contain the searched entry.

binary search 每次與中間比較、捨棄一半,前提是有序

(C)

74 分

Suppose we are debugging a quicksort implementation that is supposed to sort an array in ascending order. After the first partition step has been completed, the contents of the array are in the following order: 3 9 1 14 17 24 22 20. Which of the following statements is correct about the partition step?

(A) The pivot could have been either 14 or 17.

(B) The pivot could have been 14, but could not have been 17.

(C) The pivot could have been 17, but could not have been 14.

(D) Both 14 and 17 are pivots in the first partition.

(E) Neither 14 nor 17 could have been the pivot.

partition 後 pivot 在最終位置:左邊都比它小、右邊都比它大

pivot \(=14\):左 \(3,9,1\) 都 \(\lt14\),右 \(17,24,22,20\) 都 \(\gt14\) → 可能

pivot \(=17\):左 \(3,9,1,14\) 都 \(\lt17\),右 \(24,22,20\) 都 \(\gt17\) → 可能

一次 partition 只有一個 pivot,(D) 不對;兩個都有可能是 pivot

(A)

84 分

If we use MergeSort to sort an array with \(n\) elements, what is the worst case time required for the sort?

(A) \(O(1)\) (B) \(O(\log n)\) (C) \(O(n)\) (D) \(O(n\log n)\) (E) \(O(n^2)\)

\(T(n)=2T(n/2)+O(n)\) → \(\Theta(n\log n)\),最壞情況也一樣

(D)

94 分

Depth-first search is best implemented using

(A) Recursion (B) A queue (C) A stack (D) A tree (E) A graph

DFS 要「走到底再退回上一個岔路」=後進先出,用 stack

(B) queue 是 BFS

recursion 也可以實作 DFS,但它靠的就是系統的 call stack;題目問的資料結構是 stack

(C)

104 分

If a 5 is inserted to the end of the Min-Heap below, and the heap condition is restored, which position will the 5 go?

Array index i012345678910
Array content A[i]39151017162319223033

(A) A[0] (B) A[1] (C) A[2] (D) A[5] (E) A[11]

index 從 0 起,parent\((i)\) \(=\lfloor(i-1)/2\rfloor\)

5 放在 \(A[11]\);parent \(=A[5]=16\),\(5\lt16\) → 交換,5 在 \(A[5]\)

parent \(=A[2]=15\),\(5\lt15\) → 交換,5 在 \(A[2]\)

parent \(=A[0]=3\),\(3\lt5\) → 停

\(A=[3,9,5,10,17,15,23,19,22,30,33,16]\)

(C)

114 分

Consider the following recursive function: f(k){ if (k <= 2) return 1; return f(k-1)+f(k-2); }. If the function is called as f(n), where n is a positive integer, then the time complexity of this function is:

(A) \(\Theta(n)\) (B) \(\Theta(n^2)\) (C) \(\Theta\left(\left(\tfrac{1+\sqrt5}2\right)^n\right)\) (D) \(\Theta(2^n)\) (E) \(\Theta(3^n)\)

\(T(n)=T(n-1)+T(n-2)+1\),\(T(1)=T(2)=1\)

特徵方程式 \(x^2=x+1\),\(x=\tfrac{1+\sqrt5}2=\varphi\)

\(T(n)=\Theta(\varphi^n)\)(\(T(n)=2F_n-1\))

(D) \(2^n\) 是上界、不是 tight bound(\(\varphi\approx1.618\lt2\))

(C)

124 分

Consider another recursive function: g(p,i,j){ if (i==j) return 0; m=∞; for(k=i;k

(A) \(\Theta(n)\) (B) \(\Theta(n^2)\) (C) \(\Theta\left(\left(\tfrac{1+\sqrt5}2\right)^n\right)\) (D) \(\Theta(2^n)\) (E) \(\Theta(3^n)\)

這是 matrix-chain 的純遞迴;\(T(n)\) 為子區間長 \(n\) 的呼叫數

\(T(1)=1\),\(T(n)=1+\sum_{k=1}^{n-1}\bigl(T(k)+T(n-k)\bigr)+\text{(迴圈)}=1+2\sum_{k=1}^{n-1}T(k)+(n-1)\)

相減:\(T(n)-T(n-1)=2T(n-1)+1\) → \(T(n)=3T(n-1)+1\)

\(T(n)=\Theta(3^n)\)

(E)

134 分

The following statements about minimum spanning tree (MST) may not be correct. Assume that the weighted graph \(G=(V,E)\) is undirected and connected. Do not assume that edge weights are distinct unless this is specifically stated. \(S_A\): If \(|E|\ge|V|-1\), and there is a unique heaviest edge, then this edge cannot be part of any MST of \(G\). \(S_B\): If \(G\) has a cycle with a unique heaviest edge \(e\), then \(e\) cannot be part of any MST.

(A) Both \(S_A\) and \(S_B\) are false. (B) Both \(S_A\) and \(S_B\) are true. (C) \(S_A\) is true and \(S_B\) is false. (D) \(S_A\) is false and \(S_B\) is true.

\(S_A\):連通圖必有 \(|E|\ge|V|-1\),條件恆成立。最重的邊若是 bridge(例:兩個點接一條邊的圖),它必在 MST 裡 → F

\(S_B\):cycle property:cycle 上唯一最重的邊不在任何 MST 裡 → T

(D)

144 分

Consider the following two statements about Huffman encoding scheme: \(S_E\): If some character occurs with frequency more than 2/5, then there is guaranteed to be a codeword of length 1. \(S_F\): If all characters occur with frequency less than 1/3, then there is guaranteed to be no codeword of length 1.

(A) Both \(S_E\) and \(S_F\) are false. (B) Both \(S_E\) and \(S_F\) are true. (C) \(S_E\) is true and \(S_F\) is false. (D) \(S_E\) is false and \(S_F\) is true.

\(S_F\):設有長度 1 的碼字,那個字元 \(a\) 是 root 的子節點,從頭到尾沒被合併

root 的另一個子樹 \(T=u+v\) 形成時,場上剩 \(u,v,a\) 三個 item,且 \(u,v\) 是最小的兩個 → \(a\ge u,\ a\ge v\)

\(1=a+u+v\le3a\) → \(a\ge1/3\),與「所有頻率 \(\lt1/3\)」矛盾 → \(S_F\) T

\(S_E\):設沒有長度 1 的碼字,最後合併的 \(X,Y\) 都由合併得來。令 \(Y=y_1+y_2\) 是較晚形成的,那時 \(X,y_1,y_2\) 在場,\(y_1,y_2\) 最小 → \(X\ge y_1,y_2\)

\(X=x_1+x_2\) 形成時,\(Y\) 的材料至少有 2 個 item、每個都 \(\ge\max(x_1,x_2)\) → \(Y\ge2\max(x_1,x_2)\ge X\)

設 \(f(a)\gt2/5\)。\(a\) 在 \(X\) 裡:\(a\) 所在的 \(x_1\ge f(a)\),則 \(Y\ge2x_1\gt4/5\),\(X\lt1/5\),但 \(X\ge x_1\gt2/5\) → 矛盾

\(a\) 在 \(Y\) 裡:\(y_1\ge f(a)\gt2/5\),\(X\ge y_1\gt2/5\),所以 \(y_2\lt1/5\);\(X\) 形成時 \(y_2\) 內的某個 item \(\le y_2\) 也 \(\ge\max(x_1,x_2)\),得 \(X=x_1+x_2\lt2/5\) → 矛盾

所以必有長度 1 的碼字(不一定是頻率超過 2/5 的那個字元,例:\(0.41,0.41,0.18\) 時是另一個 0.41 長度為 1) → \(S_E\) T

(B)

154 分

Suppose a file consists of 4 characters occurring with frequencies 31%, 20%, 9%, 40%, respectively. If Huffman encoding is applied to a file consisting of 1000 characters with the given frequencies, what is the length of the encoded file in bits:

(A) 1890 (B) 1900 (C) 1910 (D) 1920 (E) 1930

合併:\(9\%+20\%=29\%\);\(29\%+31\%=60\%\);\(60\%+40\%=100\%\)

碼長:40% 為 1、31% 為 2、20% 與 9% 為 3

\(1000\times(0.40\cdot1+0.31\cdot2+0.20\cdot3+0.09\cdot3)=1000\times1.89=1890\) bits

等於合併過程中內部節點的和:\(290+600+1000=1890\)

(A)

1620 分

What is the basic concept of the dynamic programming algorithms? (8%) The famous 0/1 knapsack problem is described as follows. Given the capacity \(m\) of a knapsack and \(n\) objects whose weights are \(w_1,\ldots,w_n\) and whose profits are \(p_1,\ldots,p_n\), find the largest value of \(\sum_{1\le i\le n}p_ix_i\) by assigning either 0 or 1 to \(x_1,\ldots,x_n\) under the constraint \(\sum_{1\le i\le n}w_ix_i\le m\), where \(w_1,\ldots,w_n\) and \(p_1,\ldots,p_n\) are positive integers. Write a dynamic programming algorithm to solve the 0/1 knapsack problem with the time complexity \(O(n\times m)\). You should show that the time complexity of your algorithm is indeed \(O(n\times m)\). (12%)

(8%) dynamic programming 的基本概念

① 問題有 optimal substructure:最佳解由子問題的最佳解組成

② 子問題大量重複(overlapping subproblems),所以每個子問題只算一次、把結果存在表格,之後查表

③ 由小到大(bottom-up)依遞迴式填表;對照 divide-and-conquer 的子問題彼此獨立、不會重複

(12%) 0/1 knapsack

\(V[i][c]\)=只考慮前 \(i\) 個物品、容量 \(c\) 時的最大 profit

\(V[0][c]=0\),\(V[i][0]=0\)

\(V[i][c]=\begin{cases}V[i-1][c]&c\lt w_i\\ \max\bigl(V[i-1][c],\ V[i-1][c-w_i]+p_i\bigr)&c\ge w_i\end{cases}\)

for i = 1 to n

 for c = 0 to m

  V[i][c] = (c < w[i]) ? V[i-1][c] : max(V[i-1][c], V[i-1][c-w[i]] + p[i])

答案 \(V[n][m]\);要還原 \(x_i\):從 \((i,c)=(n,m)\) 往回,若 \(V[i][c]\ne V[i-1][c]\) 則 \(x_i=1\)、\(c\leftarrow c-w_i\),否則 \(x_i=0\),\(i\leftarrow i-1\)

時間:表格有 \((n+1)(m+1)\) 格,每格 \(O(1)\)(一次比較、一次加法) → \(O(nm)\);還原 \(O(n)\);總共 \(O(n\times m)\)

這是 pseudo-polynomial:\(m\) 是數值,不是輸入長度(\(\log m\) bits)

V[i][c] 遞迴式填表,O(nm)

1720 分

One of the fundamental concepts in the NP-completeness theory is the polynomial-time problem reduction. How can we show that a problem \(X\) can polynomially reduce to another problem \(Y\)? (8%) It is well known that the problem lower bound of the sorting problem is \(\Omega(n\log n)\). Please use the problem reduction concept to prove that the problem lower bound of the Euclidean minimum spanning tree problem is also \(\Omega(n\log n)\). Note that the Euclidean minimum spanning tree problem is concerned with finding the minimum spanning tree for \(n\) planar points. (12%)

(8%) \(X\le_pY\) 怎麼證

給出一個 polynomial-time 的轉換 \(f\),把 \(X\) 的任一 instance \(I\) 轉成 \(Y\) 的 instance \(f(I)\),並證明:\(I\) 的答案是 yes \(\iff\) \(f(I)\) 的答案是 yes(\(f\) 保持答案)

這樣只要有 polynomial-time 的演算法解 \(Y\),先做 \(f\) 再解 \(Y\),就是 polynomial-time 解 \(X\);NP-completeness 的證明就是 \(X\)(已知 NP-complete)\(\le_p\) \(Y\),推得 \(Y\) 至少一樣難

(12%) Sorting \(\le\) Euclidean MST

給 \(n\) 個相異實數 \(x_1,\ldots,x_n\),轉成平面上的點 \(P_i=(x_i,0)\)(\(O(n)\))

這些點都在 \(x\) 軸上;其 Euclidean MST 就是依 \(x\) 座標排序後相鄰兩點連起來的 path(任何不相鄰的邊都比它「跨過的」相鄰邊更長,由 cycle property 不在 MST)

解 Euclidean MST 後,在 MST 上從 degree 為 1 且 \(x\) 較小的端點出發走一遍 path,依序讀出點,就是由小到大的排序(\(O(n)\))

所以 \(T_{\text{sort}}(n)\le T_{\text{EMST}}(n)+O(n)\)

若 EMST 有 \(o(n\log n)\) 的演算法,則 sorting 也有 \(o(n\log n)\)(比較模型),與 sorting 的 lower bound \(\Omega(n\log n)\) 矛盾

所以 \(T_{\text{EMST}}(n)=\Omega(n\log n)\),得證

Sorting 可在 O(n) 內 reduce 到 Euclidean MST,故 EMST 為 Ω(n log n)

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

國立中央大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 510 頁,逐題推導。

購買 · NT$ 850 看中央其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com