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

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

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

逐題詳解

一-120 分

Answer True or False for the following statements. Give correct answers for False statements. (20%)

(1) Let \(G\) be a graph with \(e\) edges and \(v\) vertices. If \(G\) is represented by adjacency lists, DFS requires \(O(v^2)\) time.

(2) If an AOV network represents a feasible project, its topological order is unique.

(3) In static hashing, the worst-case number of comparisons needed for a successful search is \(O(n)\) for open addressing. The number could be reduced to \(O(\log n)\) by using chaining method.

(4) Let \(d_i\) be the degree of vertex \(i\) in a graph \(G\) with \(|V|=n\) and \(|E|=e\), then \(e=\sum_{i=0}^{n-1}d_i\).

(5) The path from vertex \(u\) to vertex \(v\) on a minimal cost spanning tree of an undirected graph \(G\) is also a shortest path from \(u\) to \(v\).

(1) adjacency list 的 DFS 每個點走一次、每條邊看一次(有向 1 次、無向 2 次) → \(O(v+e)\);\(O(v^2)\) 是 adjacency matrix 的結果 → F,正確是 \(O(v+e)\)

(2) 可行(無環)的 AOV network 通常有多種 topological order,例:\(a\to c,\ b\to c\),\(abc\) 與 \(bac\) 都行;只有存在包含全部點的 Hamiltonian path 時才唯一 → F,不一定唯一

(3) open addressing 最壞 \(O(n)\),這句對;chaining 最壞所有 key 掉進同一條 chain,仍是 \(O(n)\);要 \(O(\log n)\) 得把 chain 換成 balanced BST → F,chaining 最壞也是 \(O(n)\)

(4) 每條邊貢獻 2 個 degree(握手引理) → \(\sum d_i=2e\) → F,正確是 \(e=\tfrac12\sum_{i=0}^{n-1}d_i\)

(5) MST 只保證邊權和最小,不保證兩點間最短。反例:三角形 \(w(u,w)=1,\ w(w,v)=1,\ w(u,v)=1.5\),MST 為 \(\{uw,wv\}\),但 \(u\to v\) 最短是直接 1.5 → F

(1) F (2) F (3) F (4) F (5) F

一-215 分

Given the following 8 runs: (15%)

710232641612
1318281511201721
2229321924303125

(1) Draw the corresponding winner tree.

(2) Draw the restructured winner tree after one record has been output.

(3) Draw the loser tree based on the answer of question (2).

(4) Derive the total required time to merge \(n\) records through a winner tree with \(k\) runs.

(1) 每個 run 目前最小的元素放在葉子(7、10、23、2、6、4、16、12),兩兩比較,小的往上(winner):

77102232226444161212

  根 \(=2\)(run 4 的第一個)

(2) 輸出 2,run 4 的下一個元素 15 補進同一片葉子,沿著那片葉子往上重新比較(只改這條路):\(23\ \text{vs}\ 15\to15\);\(7\ \text{vs}\ 15\to7\);根:\(7\ \text{vs}\ 4\to4\)

7710723151546444161212

  根 \(=4\)(run 6 的第一個)

(3) loser tree:每個內部節點存比較輸掉的那個,贏的往上傳,整棵的 winner 另放在最上面:

  最底層:(7,10) 輸 10;(23,15) 輸 23;(6,4) 輸 6;(16,12) 輸 16

  往上:(7,15) 輸 15;(4,12) 輸 12;根:(7,4) 輸 7,贏的 4 是全部的最小

7101015232315766412161612

圖中內部節點是 loser,最上面的 winner 是 4;下一次輸出 4 後,run 6 的下一個 11 補進來,只要和它路徑上的 loser(6、12、7)依序比較,不用跟兄弟節點比

(4) 建 tree:\(k-1\) 次比較,\(O(k)\)

  輸出 \(n\) 個 record,每個輸出後沿葉到根重新比較 \(\lceil\lg k\rceil\) 次 → \(O(n\lg k)\)

  總時間 \(O(k+n\lg k)=O(n\lg k)\)(\(n\ge k\))

(1)(2)(3) 如圖,winner:2 → 4 (4) \(O(n\log k)\)

一-315 分

Consider the following AOE network: (15%)

v0v1v2v3v4v5v6v7v8a1=6a2=3a3=5a4=2a5=2a6=3a7=4a8=5a9=1a10=1a11=1a12=6

(1) Obtain \(e(i)\) and \(l(i)\) for all activity \(i\).

(2) List all critical activities.

(3) List all critical paths.

先算事件的最早時間 ee(由前往後取 max):

\(ee(v_0)=0,\ ee(v_1)=6,\ ee(v_2)=3,\ ee(v_3)=5\)

\(ee(v_4)=\max(6+2,\ 3+2)=8\);\(ee(v_5)=5+3=8\);\(ee(v_6)=8+4=12\);\(ee(v_7)=\max(8+5,\ 8+1)=13\)

\(ee(v_8)=\max(12+1,\ 13+1,\ 8+6)=14\)

再算最晚時間 le(由後往前取 min,\(le(v_8)=14\)):

\(le(v_7)=14-1=13\);\(le(v_6)=14-1=13\);\(le(v_5)=\min(13-1,\ 14-6)=8\);\(le(v_4)=\min(13-4,\ 13-5)=8\)

\(le(v_3)=8-3=5\);\(le(v_2)=8-2=6\);\(le(v_1)=8-2=6\);\(le(v_0)=\min(6-6,\ 6-3,\ 5-5)=0\)

事件v0v1v2v3v4v5v6v7v8
ee063588121314
le066588131314

(1) \(e(i)=ee(\text{起點})\),\(l(i)=le(\text{終點})-\text{duration}\):

活動a1a2a3a4a5a6a7a8a9a10a11a12
e(i)00063588812138
l(i)030665981213138
l−e030030104100

(2) \(l(i)=e(i)\) 的是 critical activity

(3) critical path 全長 14:\(v_0\to v_1\to v_4\to v_7\to v_8\)(a1, a4, a8, a11:\(6+2+5+1\)),\(v_0\to v_3\to v_5\to v_8\)(a3, a6, a12:\(5+3+6\))

圖中紅線是 critical activity

(2) a1, a3, a4, a6, a8, a11, a12 (3) \(v_0v_1v_4v_7v_8\)、\(v_0v_3v_5v_8\)(長 14)

二-120 分

Answer True or False for the following statements. Give correct answers for False statements. (20%)

(1) If P equals NP, then NP equals NP-complete.

(2) Any \(n\)-node unbalanced tree can be balanced using \(O(\log n)\) rotations.

(3) Let \(A_1,A_2\), and \(A_3\) be three sorted arrays of \(n\) real numbers (all distinct). In the comparison model, constructing a balanced binary search tree of the set \(A_1\cup A_2\cup A_3\) requires \(\Omega(n\log n)\) time.

(4) Let \(P\) be a shortest path from some vertex \(s\) to some other vertex \(t\) in a graph. If the weight of each edge in the graph is increased by one, \(P\) remains a shortest path from \(s\) to \(t\).

(5) We can claim that there is a simpler way to reweight edges than the method used in Johnson's algorithm. Letting \(w^*=\min_{(u,v)\in E}\{w(u,v)\}\), just define \(\hat w(u,v)=w(u,v)-w^*\) for all edges \((u,v)\in E\).

(1) P = NP 時,NP 的問題全部屬於 P,但 NP-complete 要求不是「空集合」與「全部字串 \(\Sigma^*\)」這兩個平凡語言(它們無法被多項式歸約出別的語言) → NP-complete \(=\)NP \(\setminus\{\emptyset,\Sigma^*\}\),不等於 NP → F

(2) 一條左斜的鏈(\(n\) 個點)要變平衡,每次 rotation 最多讓高度減少一點點,需要 \(\Theta(n)\) 次 rotation(鏈變成完全右斜的鏈就要 \(n-1\) 次) → F,需要 \(\Theta(n)\)

(3) 三條 sorted array 做 3-way merge:\(3n\) 個元素、每個元素最多比較 2 次,\(O(n)\);有序的陣列建 balanced BST(取中間當 root、遞迴)\(O(n)\) → 總共 \(O(n)\),不需要 \(\Omega(n\lg n)\) → F

(4) 每條邊加 1,邊數多的路徑多加。反例:\(s\to t\) 直接 4;另一條 \(s\to a\to b\to t\) 每條邊 1(共 3)。原本最短是 3 的那條,每邊加 1 後 \(=6\),直接那條 \(=5\),最短路徑變了 → F

(5) reweight 一定要讓每條 \(s\to t\) 路徑加上同一個量(\(h(s)-h(t)\) 相消),才不改最短路徑。\(w-w^*\) 對 \(k\) 條邊的路徑減 \(kw^*\),邊數不同減的不同 → 會改變最短路徑(例同 (4)) → F

(1) 若不把 ∅、\(\Sigma^*\) 這兩個平凡語言算進去,P = NP 時 NP 的其餘問題都是 NP-complete;嚴格定義下它們不是,所以答 F

(1) F (2) F,需 \(\Theta(n)\) (3) F,\(O(n)\) (4) F (5) F

二-210 分

Give a tight asymptotic upper bound (\(O\) notation) on the solution to the following recurrence. (10%)

\(T(n)=\begin{cases}16\,T(n/2)+\Theta(1)&\text{if }n^2\gt M\\ M&\text{if }n^2\le M\end{cases}\)

遞迴樹:每層 16 倍,\(n\) 每次除以 2,到 \(n\le\sqrt M\) 停

層數 \(d=\log_2\dfrac{n}{\sqrt M}\),葉子數 \(16^d=\left(\dfrac n{\sqrt M}\right)^4=\dfrac{n^4}{M^2}\)

葉子每個 \(M\) → \(\dfrac{n^4}{M^2}\cdot M=\dfrac{n^4}{M}\)

內部節點每個 \(\Theta(1)\),共 \(\dfrac{16^d-1}{15}=\Theta\!\left(\dfrac{n^4}{M^2}\right)\),比葉子少

\(T(n)=\Theta\!\left(\dfrac{n^4}{M}\right)\)

master theorem:\(a=16,\ b=2,\ n^{\log_2 16}=n^4\),\(f=\Theta(1)\) 遠小於 \(n^4\),case 1;\(M\) 當常數就是 \(\Theta(n^4)\)

\(T(n)=O(n^4/M)\)(\(M\) 視為常數即 \(O(n^4)\))

二-310 分

Prove that the longest increasing subsequence problem can be reduced to the edit distance problem. (10%)

Edit distance problem: Input: Two sequences \(A\) and \(B\) and the cost for operations substitution (\(C_s\)), insertion (\(C_i\)), and deletion (\(C_d\)). Output: The minimum cost sequence of edit operations to transform \(A\) into \(B\).

Longest increasing subsequence problem: Input: A sequence \(S\). Output: The longest sequence of positions \(\{p_1,p_2,\ldots,p_k\}\) such that \(p_i\lt p_{i+1}\) and \(S_i\lt S_{i+1}\).

歸約:給 LIS 的輸入 \(S\)(\(n\) 個數):

① \(B=\)把 \(S\) 的值由小到大排好、重複的值只留一個(\(O(n\lg n)\))

② \(A=S\),edit distance 的 cost:\(C_i=C_d=1\),\(C_s=2\)

③ 解 edit distance,得最小 cost \(D\),答 LIS 長度 \(=\dfrac{|A|+|B|-D}{2}\);最佳 edit 序列中沒被動到(match)的位置就是 LIS 的 \(p_i\)

證明

(i) \(C_s=2=C_i+C_d\),substitute 不會比「刪+插」好;不用 substitute 時,match 的字元構成 \(A,B\) 的一個 common subsequence,其餘 \(|A|-m\) 個刪、\(|B|-m\) 個插:cost \(=|A|+|B|-2m\)

  最小 cost \(D\) ⇔ \(m\) 最大,\(m=\mathrm{LCS}(A,B)\),所以 \(\mathrm{LCS}=\dfrac{|A|+|B|-D}2\)

(ii) \(\mathrm{LCS}(S,B)=\mathrm{LIS}(S)\):

  • 若 \(S_{p_1}\lt\cdots\lt S_{p_k}\) 是 \(S\) 的 strictly increasing subsequence,這些值都在 \(B\) 裡,且 \(B\) 由小到大排,所以這 \(k\) 個值也按同順序出現在 \(B\) → 是 \(S\) 與 \(B\) 的 common subsequence

  • 反過來,common subsequence 在 \(B\) 裡的順序是由小到大且(\(B\) 無重複)嚴格遞增,它同時是 \(S\) 的 subsequence → 是 \(S\) 的 strictly increasing subsequence

  長度相等,所以 \(\mathrm{LIS}(S)=\mathrm{LCS}(S,B)\)

(iii) 歸約本身只有排序(\(O(n\lg n)\))加後處理,是多項式時間;因此 LIS 可由一次 edit distance 求得 得證

二-410 分

Consider the linear-programming system with 9 different constraints \(x_1-x_5\le-5,\ x_1-x_4\le-2,\ x_2-x_1\le-3,\ x_2-x_3=8,\ x_3-x_1\le5,\ x_3-x_5\le2,\ x_4-x_3\le-3,\ x_5-x_1\le6,\ x_5-x_4\le1\). (1) Draw the constraint graph for these constraints. (2) Solve for the unknowns \(x_1,x_2,x_3,x_4\), and \(x_5\) or explain if no solution exists. (10%)

(1) \(x_i-x_j\le c\) → 邊 \(x_j\to x_i\),權重 \(c\)(\(x_2-x_3=8\) 等於 \(x_2-x_3\le8\) 與 \(x_3-x_2\le-8\) 兩條)

x1x2x3x4x5−5−2−3−8852−361

(2) 有負環就無解。紅線的環 \(x_4\to x_5\to x_1\to x_3\to x_4\):

  \(1+(-5)+5+(-3)=-2\lt0\)

  對應的四條不等式相加:\((x_5-x_4)+(x_1-x_5)+(x_3-x_1)+(x_4-x_3)=0\le1-5+5-3=-2\) → \(0\le-2\),矛盾

  (另有負環 \(x_1\to x_2\to x_3\to x_4\to x_1\):\(-3-8-3-2=-16\))

Bellman–Ford 從新增的源點跑 \(n\) 輪後還能再鬆弛,就是偵測到負環

無解(constraint graph 有負環)

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com