免費詳解 / 成大 / 軟體 / 102

102 成大資工所考古題解答|軟體逐題詳解

102 成大資工所軟體考古題的逐題詳解,共 7 題,免費公開。

逐題詳解

一15 分

是非題 (15 points). For each of the following statements, determine whether it is correct (T) or not (F).

1. Let \(T\) be a minimum spanning tree of \(G\). Then, for any pair of vertices \(s\) and \(t\), the shortest path from \(s\) to \(t\) in \(G\) is the path from \(s\) to \(t\) in \(T\).

2. The function \(\lceil\lg n\rceil!\) is polynomially bounded.

3. If the depth-first search of a graph \(G\) yields no back edges, then the graph \(G\) is acyclic.

4. Suppose \(P_1\) and \(P_2\) are problems and \(P_1\le_pP_2\). If \(P_2\) can be solved by an algorithm with time complexity \(\lceil\lg\lg n\rceil!\), then \(P_1\in\mathrm P\).

5. Sorting 8 elements with a comparison sort requires 24 comparisons in the worst case.

1. 反例:三角形邊 \(w(a,b)=1,\ w(b,c)=1,\ w(a,c)=1.5\);MST 取 \(ab,bc\),但 \(a\to c\) 最短路徑是直接那條 1.5 不在 T 內。MST 只管總和最小,不保證兩點間最短 → F

2. 令 \(m=\lceil\lg n\rceil\),\(m!\ge(m/e)^m=2^{m\lg m-m\lg e}\);而 \(n^c\le2^{c(m)}\)(\(\lg n\le m\))

  \(m\lg m-1.44m\) 比任何 \(cm\) 長得快,所以對任何常數 \(c\),\(n\) 夠大時 \(\lceil\lg n\rceil!\gt n^c\) → 不是多項式有界 → F

3. DFS 有環就一定出現 back edge(有向、無向都成立);沒有 back edge → 無環 → T

4. 令 \(m=\lceil\lg\lg n\rceil\),\(m!\le m^m=2^{m\lg m}\),而 \(m\lg m\approx\lg\lg n\cdot\lg\lg\lg n=o(\lg n)\) → \(m!=n^{o(1)}\),多項式有界,\(P_2\in\mathrm P\)

  \(P_1\le_pP_2\)(多項式時間歸約) → \(P_1\in\mathrm P\) → T

5. decision tree 有 \(8!=40320\) 個 leaf,高度 \(\ge\lceil\lg 40320\rceil=16\),merge insertion 恰 16 次就排得完,最壞情形 16、不是 24 → F

1 F 2 F 3 T 4 T 5 F

二-110 分

(10 points) Use the recursion-tree method to solve the recurrence \(T(n)=2T(n/2)+n/\lg n\).

第 \(i\) 層:\(2^i\) 個節點,每個 \(\dfrac{n/2^i}{\lg(n/2^i)}=\dfrac{n}{2^i(\lg n-i)}\)

第 \(i\) 層總和 \(=\dfrac{n}{\lg n-i}\),\(i=0,1,\ldots,\lg n-1\)

內部節點總和 \(=n\displaystyle\sum_{i=0}^{\lg n-1}\frac1{\lg n-i}=n\sum_{j=1}^{\lg n}\frac1j=n\,H_{\lg n}=\Theta(n\lg\lg n)\)

葉子:\(n\) 個,每個 \(\Theta(1)\),共 \(\Theta(n)\)

\(H_k=\Theta(\ln k)\),\(k=\lg n\) → \(\ln\lg n\)

\(T(n)=\Theta(n\lg\lg n)\)

二-210 分

(10 points) For COIN-CHANGE problem defined below, please calculate (a) how many different subproblems overall and (b) how many choices we have in determining which subproblem(s) to use in an optimal solution. [COIN-CHANGE Problem: An amount of money \(M\), and an array of \(d\) denominations \(c=(c_1,c_2,\ldots,c_d)\), in a decreasing order of value (\(c_1\gt c_2\gt\cdots\gt c_d\)). Please find a list of \(d\) integers \(i_1,i_2,\ldots,i_d\) such that \(c_1i_1+c_2i_2+\cdots+c_di_d=M\) and \(i_1+i_2+\cdots+i_d\) is minimal.]

令 \(C[m]\)=湊出金額 \(m\) 的最少硬幣數:\(C[m]=1+\min_{1\le k\le d,\ c_k\le m}C[m-c_k]\),\(C[0]=0\)

(a) 子問題=金額 \(0,1,\ldots,M\),共 \(M+1\) 個(去掉 \(C[0]\) 這個 base case 是 \(M\) 個)→ \(\Theta(M)\)

(b) 最後一枚硬幣選哪一種面額,共 \(d\) 種選擇(\(c_k\le m\) 的才可用,最多 \(d\))→ \(\Theta(d)\)

總時間 \(\Theta(Md)\)

(a) \(M+1\) 個(\(\Theta(M)\)) (b) \(d\) 種選擇

二-315 分

(15 points) Please analyze the running time of Dijkstra's algorithm under the following data structure implementation: (1) array, (2) binary heap, and (3) Fibonacci heap [where Extract-Min \(O(\lg V)\), Decrease-Key \(O(1)\)].

Dijkstra:\(V\) 次 Extract-Min、最多 \(E\) 次 Decrease-Key(每條邊鬆弛一次)、初始化 \(O(V)\)

(1) array:Extract-Min 掃描 \(O(V)\)、Decrease-Key \(O(1)\) → \(V\cdot O(V)+E\cdot O(1)=O(V^2+E)=O(V^2)\)

(2) binary heap:Extract-Min、Decrease-Key 都是 \(O(\lg V)\);建 heap \(O(V)\) → \(O((V+E)\lg V)\);連通圖 \(E\ge V-1\) → \(O(E\lg V)\)

(3) Fibonacci heap:Extract-Min \(O(\lg V)\)(amortized)、Decrease-Key \(O(1)\)(amortized) → \(V\cdot O(\lg V)+E\cdot O(1)=O(V\lg V+E)\)

稠密圖(\(E=\Theta(V^2)\))array 最好:\(O(V^2)\) 對 \(O(V^2\lg V)\);稀疏圖 heap 較好

(1) \(O(V^2)\) (2) \(O(E\lg V)\) (3) \(O(V\lg V+E)\)

三-115 分

(15 points) Consider the following graph:

dbfaec7121392124151829

Compute minimum cost and construct minimum spanning tree step by step by using: (1) Kruskal's algorithm, (2) Prim's algorithm, (3) Sollin's algorithm.

邊:\(db=7,\ be=9,\ df=12,\ de=13,\ ac=15,\ fe=18,\ bc=21,\ ba=24,\ ec=29\) (6 點、要選 5 邊)

(1) Kruskal:邊由小到大,不成環才收

7 \((d,b)\) 收 → 9 \((b,e)\) 收 → 12 \((d,f)\) 收 → 13 \((d,e)\):\(d,e\) 已連通(\(d\)-\(b\)-\(e\)),略過

→ 15 \((a,c)\) 收 → 18 \((f,e)\):略過 → 21 \((b,c)\) 收,5 條邊,結束

dbfaec7
① 7 (d,b)
dbfaec79
② 9 (b,e)
dbfaec7129
③ 12 (d,f)
dbfaec712915
④ 15 (a,c)
13、18 略過
dbfaec71292115
⑤ 21 (b,c)

(2) Prim(從 \(a\) 開始):每次取連到樹外的最小邊

\(\{a\}\):\(ac=15,\ ab=24\) → 取 15 → \(\{a,c\}\):\(cb=21,\ ce=29,\ ab=24\) → 取 21

\(\{a,c,b\}\):\(bd=7,\ be=9\) → 取 7 → \(\{a,c,b,d\}\):\(be=9,\ de=13,\ df=12\) → 取 9

\(\{a,c,b,d,e\}\):\(df=12,\ ef=18\) → 取 12 → 全部連通

dbfaec15
① 15 (a,c)
dbfaec2115
② 21 (c,b)
dbfaec72115
③ 7 (b,d)
dbfaec792115
④ 9 (b,e)
dbfaec71292115
⑤ 12 (d,f)

(3) Sollin:每一輪每棵樹同時選離開自己的最小邊,合併

dbfaec712915
第 1 輪:a、c 選 15;b、d 選 7;e 選 9;f 選 12
→ 兩棵樹 {a,c}、{b,d,e,f}
dbfaec71292115
第 2 輪:兩棵樹各選離開自己的最小邊 21 (b,c)
→ 一棵樹,結束

三種的樹相同(邊權全相異,MST 唯一)

\(7+9+12+15+21=64\)

MST 邊 \(\{db,be,df,ac,bc\}\),minimum cost \(=64\)

三-215 分

(15 points) Jeremy is a waiter working in a restaurant. The chef there is sloppy; when he prepares a stack of pancakes, they come out all different sizes. When Jeremy delivers the pancakes to the customer, he wants to rearrange them by grabbing several from the top and flipping them over on the way. After repeating this for several times, the smallest pancake is on top, and so on, down to the largest at the bottom. If there are \(n\) pancakes, how many flips are required? Design an algorithm to help Jeremy, and analyze its time complexity.

flip(\(k\)):把最上面 \(k\) 片整疊翻過來(前 \(k\) 片順序反轉)

演算法:\(k=n,\,n-1,\ldots,2\):

 ① 在上面 \(k\) 片中找最大的,位置 \(j\)(從頂端數)

 ② \(j=k\) → 已在底部,不翻

 ③ \(j\gt1\) → flip(\(j\)),最大的翻到頂端

 ④ flip(\(k\)) → 最大的翻到第 \(k\) 位;之後只處理上面 \(k-1\) 片

每一輪最多 2 次 flip(\(k=n\) 到 3),\(k=2\) 最多 1 次:\(2(n-2)+1=2n-3\)

時間:每輪掃描找最大 \(O(k)\),flip 本身 \(O(k)\) → \(\sum_{k=2}^nO(k)=O(n^2)\);flip 次數 \(O(n)\)

例 \(n=3\),由上而下 2、3、1:最大 3 在第 2 位 → flip(2) 得 3、2、1 → flip(3) 得 1、2、3,共 2 次

最多 \(2n-3\) 次 flip(\(n\ge2\));時間 \(O(n^2)\)

三-320 分

(20 points) A Bloom Filter is a space-efficient probabilistic data structure used to test whether a key is in a large data set. Instead of answering "yes" or "no," a Bloom Filter answers "maybe" or "no." A Bloom Filter consists of \(m\) bits of memory and \(h\) uniform and independent hash functions \(f_1,f_2,\ldots,f_h\). Each \(f_i\) hashes a key \(k\) to an integer in the range \([1,m]\). Initially all \(m\) filter bits are zero, and the data set is empty. When key \(k\) is added to the data set, bits \(f_1(k),f_2(k),\ldots,f_h(k)\) of the filter are set to 1. When a query "Is key \(k\) in the data set?" is made, bits \(f_1(k),f_2(k),\ldots,f_h(k)\) are examined. The query answer is "maybe" if all these bits are 1. Otherwise, the answer is "no."

(1) When the answer is "no," the key is not in the data set; when the answer is "maybe," the key may or may not be in the data set. Explain why.

(2) A filter error occurs whenever the answer is "maybe" and the key is not in the data set. Assume that key \(k\) is an integer in the range \([1,n]\) and \(u\) updates are made. Compute the probability of filter error for an arbitrary query after the \(u\)-th update.

(1) bit 只會被設成 1、不會被清掉。key 一旦加入,它的 \(h\) 個 bit 都是 1

  答 no = 至少有一個 bit 是 0 → 這個 key 從來沒加過 → 一定不在

  答 maybe = \(h\) 個 bit 都是 1,但這些 bit 可能是別的 key 設的(hash collision),不能確定是自己加的 → 可能在、可能不在

(2) 一次更新設 \(h\) 個 bit(hash 均勻獨立,每次設到某個特定 bit 的機率 \(1/m\))

  \(u\) 次更新共 \(hu\) 次設 bit,某個 bit 仍是 0 的機率 \(\left(1-\dfrac1m\right)^{hu}\);是 1 的機率 \(1-\left(1-\dfrac1m\right)^{hu}\)

  不在 data set 的 key,它的 \(h\) 個 bit 位置獨立,全是 1 才會誤判:\(\left[1-\left(1-\dfrac1m\right)^{hu}\right]^h\)

  query 是 \([1,n]\) 中任一 key,要先「不在」才算 error。\(u\) 次更新放進 \(u\) 個不同 key,不在的機率 \(\dfrac{n-u}{n}\)

\((1-1/m)^{hu}\approx e^{-hu/m}\),故近似 \(\left(1-e^{-hu/m}\right)^h\);\(n\gg u\) 時 \(\frac{n-u}n\approx1\)

\(\dfrac{n-u}{n}\left[1-\left(1-\dfrac1m\right)^{hu}\right]^h\)(已知 key 不在時為 \(\left[1-(1-\frac1m)^{hu}\right]^h\))

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com