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

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

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

逐題詳解

19 分

Given the following binary search tree.

1245678910

A. Give inorder, preorder, and postorder traversal of the above tree. (6 points)

B. Please draw the resulting tree after inserting node 3 and to the BST above. (3 points)

A.

inorder(左、根、右):1, 2, 4, 5, 6, 7, 8, 9, 10

preorder(根、左、右):6, 2, 1, 4, 5, 9, 8, 7, 10

postorder(左、右、根):1, 5, 4, 2, 7, 8, 10, 9, 6

B. 3 與 6 比往左,與 2 比往右,與 4 比往左,4 的左邊是空的 → 3 成為 4 的 left child

12345678910

A. 見上;B. 3 接在 4 的左邊

210 分

Sequentially insert 1, 2, 3, 4, and 5 into an empty AVL tree. Draw the resulting AVL tree after each insertion. Notice some insertions may trigger AVL tree rotations. (10 points) PS: You are supposed to draw 5 AVL trees here.

插入 1

1

插入 2 2 在 1 的右邊,平衡

12

插入 3 node 1 失衡(左高 0、右高 2):RR,對 1 做 left rotation

123 → 123

插入 4 root 2 左高 1、右高 2,平衡,不轉

1234

插入 5 node 3 失衡:RR,對 3 做 left rotation(4 升上來)

12345 → 12345

圖中紅色是剛插入的點;每個箭頭前是旋轉前、後是旋轉後。第 3、5 次插入各旋轉一次,第 1、2、4 次不需旋轉

最後的樹:2 為 root,左 1,右 4(左 3、右 5)

312 分

Given the sequence of numbers: 5, 4, 3, 2, 1, we intend to sort them into ascending order.

A. How many swaps does it take to sort this sequence via Insertion sort? (4 points)

B. How many recursive calls does it take by the Merge sort to get the job done? (4 points)

C. How many times the partition procedure will be called by the Quicksort to do the same job? (4 points)

A. 完全反向,第 \(i\) 個元素要往前換 \(i-1\) 次(每次 swap 消去一個 inversion)

\(1+2+3+4=10\)(\(=\binom52\))次

B. MergeSort(l, r) 在 \(l\lt r\) 時再呼叫兩次

[5,4,3,2,1] → [5,4,3]、[2,1](2 次);[5,4,3] → [5,4]、[3];[2,1] → [2]、[1](4 次);[5,4] → [5]、[4](2 次)

遞迴呼叫 \(2+4+2=8\) 次(遞迴樹共 9 個節點,最外層的原始呼叫不算遞迴呼叫)

C. 以最後一個元素(或第一個)當 pivot,長度 \(\ge2\) 的區間才會呼叫 partition

[5,4,3,2,1] → pivot 1,剩 [4,3,2,5](1 次);[4,3,2,5] → pivot 5,剩 [4,3,2](2 次);[4,3,2] → pivot 2,剩 [3,4](3 次);[3,4] → pivot 4(4 次)

partition 共呼叫 4 次(此輸入對這種 pivot 是 worst case)

A. 10;B. 8(含最外層共 9 次呼叫);C. 4

412 分

Given the sequence of numbers: 15, 24, 33, 42, 51, we intend to sort them into ascending order.

A. What is the required storage space for the counting sort? (4 points)

B. What is the result of the first pass if the radix sort is used? (4 points)

C. Why does the radix sort need to use a stable sort inside the loop? (4 points)

A. counting sort 要 count array \(C[0..k]\)(\(k=\) 最大值 \(=51\),共 52 格)與 output array \(B[1..5]\)(5 格),輸入 \(A\) 本身 5 格

額外空間:\(52+5=57\) 格,即 \(O(n+k)\)

若 count array 只涵蓋值域 \([15,51]\)(\(51-15+1=37\) 格),則額外 \(37+5=42\) 格

B. LSD radix sort 第一個 pass 依個位數排序:個位數 \(5,4,3,2,1\)

結果:51, 42, 33, 24, 15

C. radix sort 由低位排到高位,排到某一位時,這一位相同的兩筆資料必須維持上一輪(低位)排好的相對次序,這靠 stable sort 才保得住;不 stable 會打亂低位的結果,最後就排不對

A. 57 格(\(O(n+k)\));B. 51, 42, 33, 24, 15;C. 保住低位已排好的次序

57 分

Given the sequence of numbers: 5, 4, 3, 2, 1.

A. Insert them into an empty B-tree of degree 3. Draw the resulting B-tree. (4 points)

B. In which kind of applications is B-tree frequently used? (3 points)

A. degree 3:每個 node 最多 3 個 child(2 個 key),放第 3 個 key 就 overflow,取中間的 key 上推並分裂

插入 5、4:同一個 node

4 | 5

插入 3:overflow(3 個 key),中間的 4 上推 → root 4,child 3、5

3 | 4 | 5 → 354

插入 2:比 4 小,放進 [3] → [2 | 3]

2 | 354

插入 1:放進 [2 | 3] → overflow,中間的 2 上推 → root [2 | 4],child [1]、[3]、[5]

1 | 2 | 354 → 1352 | 4

B. 資料量大、放不進 memory 的索引:database index、file system(目錄與檔案配置)

一個 node 對應一個 disk block,fan-out 大、樹高低,一次搜尋的 disk access 少

若 degree 讀成 CLRS 的 minimum degree \(t=3\)(每個 node 最多 5 個 key),五個 key 全放在同一個 node:[1 | 2 | 3 | 4 | 5]

A. root [2 | 4],三個 leaf [1]、[3]、[5];B. database index、file system

612 分

Assume it is a known fact that problem X is NP-hard.

A. How can we prove that problem Y is also NP-hard by taking advantage of polynomial-time reduction and the fact? (6 points)

B. How can we further prove that Y is NP-complete? (6 points)

A. 做 polynomial-time reduction \(X\le_pY\):設計一個多項式時間的轉換 \(f\),把 \(X\) 的任一 instance \(I\) 轉成 \(Y\) 的 instance \(f(I)\),並證明 \(I\) 是 yes-instance \(\iff\) \(f(I)\) 是 yes-instance

方向:從已知的 NP-hard 問題 \(X\) 轉到要證的 \(Y\)

因為 \(X\) 是 NP-hard,所有 NP 問題都能多項式時間 reduce 到 \(X\);再接上 \(X\le_pY\)(transitivity),所有 NP 問題都能 reduce 到 \(Y\) → \(Y\) 是 NP-hard

B. 再證 \(Y\in\)NP:給一組 certificate(解),存在多項式時間的 verifier 能檢查它是不是合法解

\(Y\in\)NP 且 \(Y\) 是 NP-hard → \(Y\) 是 NP-complete

A. X ≤p Y;B. 再證 Y ∈ NP

714 分

Below is the description of the Minimum Edit Cost (MEC) problem, described as follows. Assuming string \(A=a_1a_2\ldots a_m\) and string \(B=b_1b_2\ldots b_n\), the MEC problem is to find a sequence of edit operations such that the total cost of the operations in the sequence is minimized. There are three types of operations of costs 1, 2 and 3, as shown below. Operation 1: Deletion of a character from \(A\) (cost 1). Operation 2: Insertion of a character into \(A\) (cost 2). Operation 3: Substitution of a character in \(A\) with another character (cost 3).

The MEC problem can be solved by a dynamic programming algorithm, which stores in the array element \(c[i,j]\) the minimum cost of operations to transform substring \(a_1a_2\ldots a_i\) of \(A\) to substring \(b_1b_2\ldots b_j\) of \(B\), where \(0\le i\le m\) and \(0\le j\le n\). The recursive relations and the boundary conditions of \(c[i,j]\) are the core of the dynamic programming algorithm. Write down: A. The recursive relation of \(c[i,j]\) (8 points) B. The boundary conditions of \(c[i,j]\) (6 points)

A. 看最後一個字元 \(a_i\)、\(b_j\) 怎麼處理:

刪掉 \(a_i\):\(c[i-1,j]+1\)

在 \(A\) 後面插入 \(b_j\):\(c[i,j-1]+2\)

\(a_i\) 與 \(b_j\) 對齊:\(a_i=b_j\) 不用動作 \(c[i-1,j-1]\);\(a_i\ne b_j\) 要替換 \(c[i-1,j-1]+3\)

\(c[i,j]=\min\bigl\{c[i-1,j]+1,\ c[i,j-1]+2,\ c[i-1,j-1]+\delta(i,j)\bigr\}\),\(\delta(i,j)=\begin{cases}0&a_i=b_j\\3&a_i\ne b_j\end{cases}\) (\(1\le i\le m,\ 1\le j\le n\))

B. \(c[0,0]=0\)

\(c[i,0]=i\cdot1=i\)(\(1\le i\le m\)):把 \(a_1\ldots a_i\) 全部刪掉,\(i\) 次 deletion

\(c[0,j]=j\cdot2=2j\)(\(1\le j\le n\)):從空字串插入 \(b_1\ldots b_j\),\(j\) 次 insertion

答案 \(c[m,n]\);表格 \((m+1)(n+1)\) 格,每格 \(O(1)\) → \(O(mn)\)

A. c[i,j]=min{c[i−1,j]+1, c[i,j−1]+2, c[i−1,j−1]+δ};B. c[0,0]=0, c[i,0]=i, c[0,j]=2j

812 分

Below is the Bellman-Ford Shortest Path Algorithm (BFSPA). The input of the BFSPA is a weighted directed graph \(G=(V,E)\) and a specific source node \(s\), where \(V\) is the node set and \(E\) is the edge set and every edge in \(E\) has a negative or positive weight. For every node \(x\) in \(V-\{s\}\), the BFSPA can derive the shortest path from \(s\) to \(x\) and output the sum of weights of all edges on the path. Modify the algorithm to determine if the given graph has a negative-weighted cycle. The output of your modified algorithm is "YES" if the graph has a negative-weighted cycle; "NO", otherwise. You should write down the whole modified algorithm including the input, the output and all steps. Note that the BFSPA uses indentation to represent the block structure. So please use the indentation properly when writing the modified BFSPA algorithm. (12 points)

Algorithm BFSPA
Input: A weighted digraph G=(V, E), a source node s and a two dimension array w,
       where V is the node set, E is the edge set, and w[x][y] is the weight of the edge (x, y)
Output: An array d, where d[x] represents the sum of weights of all edges on the shortest path from s to x, a node in V-{s}
1.  d[s] <- 0;
2.  for every node x in V-{s}
3.      d[x] <- infinity
4.  for i <- 1 to |V|-1 do
5.      for every edge (x, y) in E do
6.          if d[y] > d[x] + w[x][y] then
7.              d[y] <- d[x] + w[x][y]
8.  return d

原本的 1–7 行不動,把 8 行的 return d 換成多做一輪檢查

Algorithm BFSPA-NEGCYCLE
Input:  A weighted digraph G=(V, E), a source node s and a two dimension array w,
        where w[x][y] is the weight of the edge (x, y)
Output: "YES" if G has a negative-weighted cycle; "NO" otherwise
1.  d[s] <- 0;
2.  for every node x in V-{s}
3.      d[x] <- infinity
4.  for i <- 1 to |V|-1 do
5.      for every edge (x, y) in E do
6.          if d[y] > d[x] + w[x][y] then
7.              d[y] <- d[x] + w[x][y]
8.  for every edge (x, y) in E do
9.      if d[y] > d[x] + w[x][y] then
10.         return "YES"
11. return "NO"

沒有 negative cycle 時,任何最短路徑至多 \(|V|-1\) 條邊,跑完第 4–7 行 \(|V|-1\) 輪每個 \(d\) 都已收斂,再掃一輪不會有邊能 relax

還找得到可以 relax 的邊,表示 \(d\) 還沒收斂,圖上有 negative-weighted cycle(從 \(s\) 可到達的)

時間 \(O(|V||E|)\)

這個寫法只偵測從 \(s\) 可到達的 negative cycle。要偵測整張圖的任何 cycle,把第 1–3 行改成對每個 node \(x\) 都設 \(d[x]\leftarrow0\)(等於加一個連到所有點、權重 0 的虛擬起點)

第 8–11 行:多掃一輪,還能 relax 就 YES,否則 NO

912 分

Given a knapsack with capacity \(C\), and \(n\) objects \(o_1,\ldots,o_n\) with weights \(w_1,\ldots,w_n\) and profits \(p_1,\ldots,p_n\), the fractional knapsack problem is to determine \(x_i\) (\(0\le x_i\le1\), \(1\le i\le n\)) such that \(\sum_{1\le i\le n}x_iw_i\le C\) and \(P=\sum_{1\le i\le n}x_ip_i\) is maximized. Write a greedy algorithm to solve the fractional knapsack problem to output \(x_1,\ldots,x_n\), and \(P\). You should write down the complete algorithm including the input, the output and all steps. (12 points)

依單位重量的 profit \(p_i/w_i\) 由大到小拿,裝不下時取剩下容量的比例

Algorithm FRACTIONAL-KNAPSACK
Input:  capacity C, objects o[1..n] with weights w[1..n] and profits p[1..n]
Output: x[1..n] (0 <= x[i] <= 1) and the maximum profit P
1.  for i <- 1 to n do
2.      x[i] <- 0
3.  sort the indices so that p[A[1]]/w[A[1]] >= p[A[2]]/w[A[2]] >= ... >= p[A[n]]/w[A[n]]
4.  W <- 0;  P <- 0
5.  for i <- 1 to n do
6.      k <- A[i]
7.      if W + w[k] <= C then
8.          x[k] <- 1
9.          W <- W + w[k];  P <- P + p[k]
10.     else
11.         x[k] <- (C - W) / w[k]
12.         P <- P + x[k] * p[k]
13.         break
14. return x[1..n] and P

時間:排序 \(O(n\log n)\),其餘 \(O(n)\) → \(O(n\log n)\)

greedy 成立的理由:最佳解若沒把 ratio 最高的物品拿滿,就把別的物品換掉一些重量換成它,profit 不會變差,所以存在從 ratio 最高者開始拿的最佳解

依 p/w 由大到小拿,最後一個取比例,O(n log n)

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com