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

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

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

逐題詳解

一-115 分

1. (15%) [圖:13 個節點的無向圖。節點 7 的鄰居是 2、3、4、5、6,節點 10 的鄰居是 2、3。]

We define common friends between node A and node B are the common neighbors of node A and node B. For example, in the above graph the common friends between node 7 and node 10 are node 2 and node 3. Assuming the neighbor list of each node is given, please write a function NumCommonFriend(node A, node B) to return the number of common friends between node A and B in the given graph. Note that you should optimize your code by reducing the number of operations.

兩個 neighbor list 求交集的大小。令 \(d_A,d_B\) 是兩點的 degree

做法 1(list 已排序):兩個指標同時走,像 merge:\(O(d_A+d_B)\)

做法 2(最省):走 degree 較小的那個 list,對每個鄰居查「它是不是另一個點的鄰居」。另一個點的鄰居事先放進 hash set(或 bit vector,每個點一個 \(O(1)\) 查詢):\(O(\min(d_A,d_B))\)

int NumCommonFriend(node A, node B)
{
    if (deg[A] > deg[B]) swap(A, B);        /* A 的 neighbor 較少 */
    int cnt = 0;
    for (each v in neighbor[A])             /* 只走較短的 list */
        if (isNeighbor(B, v))               /* O(1):hash set 或 bit vector */
            cnt++;
    return cnt;
}

若沒有事先建好 hash set,也可以拿較長的 list 建 hash(\(O(d_B)\))再走較短的;或 list 已排序時,對較短 list 的每個元素在長 list 做 binary search:\(O(d_A\lg d_B)\),\(d_A\ll d_B\) 時比 merge 少

題目的例子:node 7 的鄰居 \(\{2,3,4,5,6\}\)、node 10 的鄰居 \(\{2,3\}\);走 10 的 list,2、3 都是 7 的鄰居 → 回傳 2

走較小的 neighbor list,每個元素在另一邊的 hash set 查詢,\(O(\min(d_A,d_B))\)

一-215 分

2. (15%) Insert a sequence of values 100, 99, 98, 97, 96, 70, 90, 72, 89 into an empty AVL tree. You will experience \(x\) times LL, \(y\) times LR, \(z\) times RL, and \(w\) times RR. Please find \((x,y,z,w)\).

依序插入並檢查每個點的 balance factor,失衡就旋轉;圖是每次插入(含旋轉)後的樹:

100
插 100:不失衡
99100
插 99:不失衡
9899100
插 98:在 100 失衡,左左 → LL(以 100 右旋)
979899100
插 97:不失衡
96979899100
插 96:在 98 失衡,左左 → LL(以 98 右旋)
7096979899100
插 70:在 99 失衡,左左 → LL(以 99 右旋)
709096979899100
插 90:在 96 失衡,左右 → LR(先以 70 左旋,再以 96 右旋)
70729096979899100
插 72:不失衡
7072899096979899100
插 89:在 70 失衡,右右 → RR(以 70 左旋)

100、99 不失衡;插 98 時 100 失衡(LL);97 不失衡;插 96 時 98 失衡(LL);插 70 時 99 失衡(LL);插 90 時 96 失衡(LR);插 72 不失衡;插 89 時 70 失衡(RR)

LL 3 次、LR 1 次、RL 0 次、RR 1 次

\((x,y,z,w)=(3,1,0,1)\)

一-320 分

3. (20%) Symmetric Min-Max Heap (SMMH) is defined as a complete binary tree, which supports the implementation of double-ended priority queue. The insert, delete-min, and delete-max are three major operations with complexity \(O(\log n)\). It satisfies the following properties: (1) Root contains no data. (2) Left sibling data \(\le\) Right sibling data. (3) If node A has a grandparent X, the data in the left child of the node X is equal to or smaller than the data in X. (4) If node A has a grandparent X, the data in the right child of the node X is equal to or larger than the data in X. Build an SMMH by inserting elements 13, 25, 18, 30, 40, 50, 22, 16, 19, 96 in order. If SMMH is implemented with the array data structure T[] starting with index 1, which represents the root node. Please show T[3] in the final SMMH (5%). If we apply delete-min, please show T[4] in the resulted SMMH (5%). Instead, if we apply delete-max to the original SMMH, please show T[5] in the final SMMH (5%). Please present another two data structures which also support the implementation of double-ended priority queue. (5%)

(原題 (3)、(4) 最後寫「than the data in X」,依 SMMH 的定義,應是 A 與 grandparent X 的左/右 child 比較,下面照定義作答。)

陣列 \(T[1]\) 是空的根,\(T[2],T[3]\) 是第一層(\(T[2]\) 是最小、\(T[3]\) 是最大)。節點 \(i\) 的 grandparent 是 \(\lfloor i/4\rfloor\),其左 child \(2g\)、右 child \(2g+1\):\(T[2g]\le T[i]\le T[2g+1]\)

insert:放在最後一格 \(i\);① 若是右 child 且 \(T[i-1]\gt T[i]\) 就與左 sibling 交換;② 與 grandparent 的左/右 child 比:比左 child 小就與它交換、比右 child 大就與它交換,往上重複

逐步(陣列 \(T[2],T[3],\ldots\)):

插入T[2], T[3], …
1313
2513, 25
1813, 25, 18
3013, 30, 18, 25
4013, 40, 18, 25, 30
5013, 50, 18, 25, 30, 40
2213, 50, 18, 25, 30, 40, 22
1613, 50, 16, 25, 30, 40, 18, 22
1913, 50, 16, 25, 30, 40, 18, 22, 19
9613, 96, 16, 50, 30, 40, 18, 22, 19, 25

\(30\) 插在 \(T[5]\),比 \(T[3]=25\) 大,換上去;\(40\)、\(50\) 同樣把當時的最大值換到 \(T[3]\);\(16\) 先與左 sibling 22 交換(\(T[8]=16\)),再比 \(T[4]=18\) 小,換到 \(T[4]\);\(96\) 先比 \(T[5]=25\) 大換上去,再比 \(T[3]=50\) 大換到 \(T[3]\)

最後的 SMMH:

18162213195025∅309640

T[2]T[3]T[4]T[5]T[6]T[7]T[8]T[9]T[10]T[11]
13961650304018221925

\(T[3]=96\)(全部的最大值)

delete-min:拿走 \(T[2]=13\),把最後一個 \(T[11]=25\) 取出補空缺:

  洞在 \(T[2]\),往下看 \(T[4]=16,\ T[6]=30\)(偶數位置構成 min heap),較小的 16 小於 25,上移到 \(T[2]\);洞到 \(T[4]\),看 \(T[8]=18,\ T[10]=19\),18 上移到 \(T[4]\);洞到 \(T[8]\),沒有子孫,25 放進去

  再檢查 sibling:\(T[9]=22\lt25\),與 \(T[8]\) 交換,\(T[8]=22,\ T[9]=25\);\(T[9]\) 的 grandparent 右 child \(T[5]=50\ge25\),不用再動

T[2]T[3]T[4]T[5]T[6]T[7]T[8]T[9]T[10]
169618503040222519

  \(T[4]=18\)

delete-max(對原本的 SMMH):拿走 \(T[3]=96\),取出最後一個 \(T[11]=25\):

  洞在 \(T[3]\),看 \(T[5]=50,\ T[7]=40\)(奇數位置構成 max heap),較大的 50 大於 25,上移到 \(T[3]\);洞到 \(T[5]\),看 \(T[9]=22\),不大於 25,25 放進 \(T[5]\);左 sibling \(T[4]=16\le25\),OK

T[2]T[3]T[4]T[5]T[6]T[7]T[8]T[9]T[10]
135016253040182219

  \(T[5]=25\)

另外兩種 double-ended priority queue 資料結構:min-max heap(偶數層 min、奇數層 max)、interval heap(每個節點存一個區間 \([a,b]\),左端點構成 min heap、右端點構成 max heap);另有 deap、twin heap/dual heap(兩個 heap 互相對應)、balanced BST(AVL 樹,取最左與最右)

\(T[3]=96\);delete-min 後 \(T[4]=18\);delete-max 後 \(T[5]=25\);另外:min-max heap、interval heap

二-410 分

4. (10%) What are the minimum and maximum numbers of elements in a heap of height \(h\)?

高度 \(h\)=根到最深葉的邊數(根的高度 0)。heap 是 complete binary tree:前面 \(h\) 層(高度 \(0..h-1\))全滿,最後一層至少 1 個、最多全滿

最少:前 \(h\) 層 \(1+2+\cdots+2^{h-1}=2^h-1\),加最後一層 1 個 → \(2^h\)

最多:整棵全滿 → \(1+2+\cdots+2^h=2^{h+1}-1\)

例 \(h=0\):1 個;\(h=1\):2 到 3 個;\(h=2\):4 到 7 個

最少 \(2^h\),最多 \(2^{h+1}-1\)

二-515 分

5. (15%) The matrix-chain multiplication problem can be stated as follows: Given a chain \(\langle A_1,A_2,\ldots,A_n\rangle\) of \(n\) matrices, where for \(i=1,2,\ldots,n\), matrix \(A_i\) has dimension \(p_{i-1}\times p_i\), fully parenthesize the product \(A_1A_2\cdots A_n\) in a way that minimizes the number of scalar multiplications. Suppose that you have 6 matrices: \(A_1\) has dimension \(30\times35\), \(A_2\) has dimension \(35\times18\), \(A_3\) has dimension \(18\times5\), \(A_4\) has dimension \(5\times10\), \(A_5\) has dimension \(10\times25\), \(A_6\) has dimension \(25\times30\). Please calculate the minimum number of scalar multiplications.

\(p=(30,35,18,5,10,25,30)\),\(m[i,j]=\min_{i\le k\lt j}\{m[i,k]+m[k+1,j]+p_{i-1}p_kp_j\}\)

長度 2:\(m[1,2]=30\cdot35\cdot18=18900\);\(m[2,3]=35\cdot18\cdot5=3150\);\(m[3,4]=18\cdot5\cdot10=900\);\(m[4,5]=5\cdot10\cdot25=1250\);\(m[5,6]=10\cdot25\cdot30=7500\)

長度 3:\(m[1,3]=\min(3150+30\cdot35\cdot5,\ 18900+30\cdot18\cdot5)=\min(8400,\,21600)=8400\)

  \(m[2,4]=\min(900+35\cdot18\cdot10,\ 3150+35\cdot5\cdot10)=\min(7200,\,4900)=4900\)

  \(m[3,5]=\min(1250+18\cdot5\cdot25,\ 900+18\cdot10\cdot25)=\min(3500,\,5400)=3500\)

  \(m[4,6]=\min(7500+5\cdot10\cdot30,\ 1250+5\cdot25\cdot30)=\min(9000,\,5000)=5000\)

長度 4、5、6 同樣算下去:

j=1j=2j=3j=4j=5j=6
m[1,·]018,9008,4009,90013,40017,900
m[2,·]03,1504,9008,77513,400
m[3,·]09003,5007,700
m[4,·]01,2505,000
m[5,·]07,500
m[6,·]0

\(m[1,6]\):\(k=1:44900\),\(k=2:42800\),\(k=3:8400+5000+30\cdot5\cdot30=17900\),\(k=4:26400\),\(k=5:35900\) → \(k=3\) 最小

括號:\(s[1,6]=3\),\(s[1,3]=1\),\(s[4,6]=5\) → \(\big(A_1(A_2A_3)\big)\big((A_4A_5)A_6\big)\)

最少 17900 次純量乘法

二-610 分

6. (10%) Give asymptotic tight bound for \(T(n)=T(n-1)+\lg n\). Assume that \(T(n)\) is constant for sufficiently small \(n\).

展開:\(T(n)=T(1)+\sum_{k=2}^n\lg k=\Theta(1)+\lg(n!)\)

\(\lg(n!)=\Theta(n\lg n)\)(Stirling:\(\lg n!=n\lg n-n\lg e+O(\lg n)\))

也可以直接夾:上界 \(\sum\lg k\le n\lg n\);下界 \(\sum_{k=n/2}^n\lg k\ge\tfrac n2\lg\tfrac n2=\Omega(n\lg n)\)

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

二-715 分

7. (15%) We are given a directed graph \(G=(V,E)\) on which each edge \((u,v)\in E\) has an associated value \(r(u,v)\), which is a real number in the range \(0\le r(u,v)\le1\) that represents the reliability of a communication channel from vertex \(u\) to vertex \(v\). We interpret \(r(u,v)\) as the probability that the channel from \(u\) to \(v\) will not fail, and we assume that these probabilities are independent. Give an efficient algorithm to find the most reliable path between two given vertices.

路徑的可靠度 \(=\prod r(e)\)(機率獨立),要找 乘積最大 的路徑

每條邊 \(r\le1\),沿路徑乘積只會不增 → 與「權重 \(\ge0\) 的最短路徑」同型。兩種做法:

(1) 取 \(-\lg\):\(w(u,v)=-\lg r(u,v)\ge0\)(\(r=0\) 的邊刪掉,等於 \(\infty\)),\(\prod r\) 最大 ⇔ \(\sum w\) 最小。用 Dijkstra 找 \(s\) 到 \(t\) 的最短路徑,可靠度 \(=2^{-d(t)}\)

(2) 直接改 Dijkstra:

for each v: R[v] = 0;  R[s] = 1;
Q = all vertices                          /* 依 R 的 max-priority queue */
while Q not empty:
    u = EXTRACT-MAX(Q)
    for each edge (u, v):
        if R[u] * r(u,v) > R[v]:         /* relax:乘積變大 */
            R[v] = R[u] * r(u,v);  pred[v] = u;  INCREASE-KEY(Q, v, R[v])

正確性:\(r\le1\),每個點被取出時 \(R\) 已是最大(取出的 \(u\) 之後的路徑只會讓乘積更小),和 Dijkstra 相同

時間:binary heap \(O((V+E)\lg V)\),Fibonacci heap \(O(V\lg V+E)\);由 \(pred\) 倒推回 \(s\) 得路徑

\(-\lg r\) 當權重,Dijkstra:\(O((V+E)\lg V)\)

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com