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

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

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

逐題詳解

一-120 分

1. (20%) Given a red-black tree in the following figure. [圖:根 419(黑);左子樹以 330(黑)為根;右邊是紅色的 788 → 593(黑)、837(黑);593 的左子 450(紅,子節點 428 黑、505 黑,505 有兩個紅子)、右子 614(紅,子節點 603 黑、757 黑,757 有兩個紅子 700 與 765);837 → 821(黑,兩個紅子)、860(黑,右子紅)。紅點是白字灰底,掃描後數字多半糊掉,以上只列看得清楚的值]

After one node with value 778 is inserted, the resulting red-black tree is shown as follows. [圖:插入後的樹,根與其他節點無值,A、B 為其中兩個節點]

(a) (10%) Please describe the operation procedures for this insertion.

(b) (10%) Please indicate the color and value of node A and node B, respectively.

(a) 先照 BST 規則找位置:\(419\to\) 右(788)\(\to\) 左(\(778\lt788\),593)\(\to\) 右(614)\(\to\) 右(757)\(\to\) 右(765)\(\to\) 右(空)。新節點 778 染紅,成為 765 的右子

紅黑樹要處理連續兩個紅點。圖中紅點是紅筆、黑點是黑底;⋯ 是沒動到的子樹

330⋯419428450505⋯593603614700757765778788837⋯
① 778 插在 765 的右邊(紅),765 也是紅,紅紅衝突
330⋯419428450505⋯593603614700757765778788837⋯
② uncle 700 是紅:765、700 變黑,757 變紅;757 與 614 紅紅衝突
330⋯419428450505⋯593603614700757765778788837⋯
③ uncle 450 是紅:614、450 變黑,593 變紅;593 與 788 紅紅衝突
330⋯419428450505⋯593603614700757765778788837⋯
④ uncle 330 是黑,593 是 788 的左子、788 是 419 的右子(RL):雙旋轉,593 成為根

①:778(紅)的 parent 765 是紅,grandparent 757 是黑,uncle 700 是紅 → Case 1:765、700 改黑,757 改紅,往上檢查 757

②:757(紅)的 parent 614 是紅,grandparent 593 是黑,uncle 450 是紅 → Case 1:614、450 改黑,593 改紅,往上檢查 593

③:593(紅)的 parent 788 是紅,grandparent 419(根,黑),uncle 330 是黑 → Case 2/3:593 是 788 的左子、788 是 419 的右子,是 RL 型。先以 788 右旋,再以 419 左旋,593 成為這棵子樹的根;593 改黑,419、788 改紅

④:沒有紅紅衝突,各路徑黑點數相同,結束。根換成 593(黑)

(b) 圖中的位置:B = 根的左子、A = 根的右子(788)的左子

  最終結構:\(593\)(根) → 左子 \(419\),右子 \(788\);\(788\) 的左子是 \(614\)

330⋯419428450505⋯593603614700757765778788837⋯

  B = 419,紅(雙旋轉後 419、788 為紅)

  A = 614,黑(步驟 ② 被改黑)

上面是插入完再往上修的做法(bottom-up)。若用 top-down 插入(往下走時遇到兩個 child 都是紅的點就先 color flip),做出的最後一棵樹相同,A、B 的答案也一樣

(a) 兩次 recolor(uncle 紅)加一次 RL 雙旋轉(uncle 黑) (b) A=614(黑)、B=419(紅)

一-210 分

2. (10%) Given the expression, \((x+y)*w+u/(v+x*w)+z\), Please show the content in the stack after the operand \(v\) is read in postfix transformation.

infix → postfix:operand 直接輸出;operator 放 stack,遇到優先權較低(或相等)的 operator 或 ) 時先把 stack 上優先權較高的彈出

讀入    輸出            stack (底→頂)
(                        (
x       x               (
+       x               ( +
y       x y             ( +
)       x y +
*       x y +           *
w       x y + w         *
+       x y + w *       +        ← * 優先權較高,先彈出
u       x y + w * u     +
/       x y + w * u     + /      ← / 優先權高於 +,不彈
(       x y + w * u     + / (
v       x y + w * u v   + / (

讀入 \(v\) 之後 stack(由底到頂):+ / (;輸出為 x y + w * u v

stack:+、/、((底→頂)

一-320 分

3. (20%) (a) (10%) Please finish the lost code for the choose function in the following Dijkstra shortest path implementation for a graph without negative-weight edges.

void shortestPath(int v, int cost[][MAX_VERTICES], int distance[],
                  int n, short int found[])
{ /* cost is the adjacency matrix */
    int i, u, w;
    for (i=0; i<n; i++) {
        found[i] = FALSE;
        distance[i] = cost[v][i];
    }
    found[v] = TRUE;
    distance[v] = 0;
    for (i=0; i<n-2; i++) {
        u = choose(distance, n, found);
        found[u] = TRUE;
        for (w=0; w<n; w++)
            if (!found[w])
                if (distance[u]+cost[u][w] < distance[w])
                    distance[w] = distance[u]+cost[u][w];
    }
}
int choose(int distance[], int n, short int found[])
{
    int i, min, minpos;
    min = INT_MAX;
    minpos = -1;
    ________
    return minpos;
}

(b) (10%) In a directed graph without a cycle of negative length but with a negative-length edge, we can implement Bellman-Ford algorithm as follows to compute shortest paths. Please fill in the lost code.

void BellmanFord(int n, int v){
    for (int i=0; i<n; i++)
        distance[i] = cost[v][i];
    for (int k=2; k<=n-1; k++)
        for ( ______________ )
            for ( ______________ )
                if (distance[u] > distance[i]+cost[i][u])
                    distance[u] = distance[i]+cost[i][u];
}

(a) choose 要在還沒 found 的點中,找 distance 最小的那個的編號:

for (i = 0; i < n; i++)
    if (!found[i] && distance[i] < min) {
        min = distance[i];
        minpos = i;
    }

(b) Bellman–Ford:初始 distance[i]=cost[v][i] 是「最多 1 條邊」的最短距離;第 \(k\) 輪(\(k=2..n-1\))把「最多 \(k\) 條邊」的結果算出來,對每個終點 \(u\)、每個前驅 \(i\) 鬆弛:

for (int u = 0; u < n; u++)       /* u:要更新的終點,u != v 也可以 */
    for (int i = 0; i < n; i++)   /* i:前驅,邊 i → u */

  最短路徑最多 \(n-1\) 條邊(沒有 negative cycle),所以做到 \(k=n-1\) 就夠;時間 \(O(n^3)\)(用 adjacency matrix)

(a) if(!found[i] && distance[i]<min){min=distance[i]; minpos=i;}(掃 \(i=0..n-1\)) (b) int u=0; u<n; u++、int i=0; i<n; i++

二-410 分

4. (10%) Prove or disprove: The single-source shortest paths problem can be solved in linear time in directed acyclic graphs.

成立(prove)。演算法(先 topological sort,再依序放鬆):

① 對 DAG 做 topological sort(DFS 或 Kahn):\(\Theta(V+E)\)

② \(d[s]=0\),其餘 \(\infty\):\(\Theta(V)\)

③ 依 topological order 對每個點 \(u\),把 \(u\) 的每條出邊 \((u,w)\) 做 relax:\(d[w]=\min(d[w],\,d[u]+w(u,w))\):每條邊 relax 一次,\(\Theta(E)\)

正確性:從 \(s\) 到 \(t\) 的任何路徑 \(s=v_0,v_1,\ldots,v_k=t\) 在 topological order 中依序出現,輪到 \(v_i\) 時 \(d[v_i]\) 已經是最終值(它的所有入邊的起點都在它前面、已處理完),所以邊 \((v_i,v_{i+1})\) 剛好依路徑順序被 relax → path-relaxation property,\(d[t]=\delta(s,t)\)

總時間 \(\Theta(V+E)\),是線性。負權邊也成立(只要無環就沒有 negative cycle)

成立:topological sort+依序 relax,\(\Theta(V+E)\)

二-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\times15\), \(A_3\) has dimension \(15\times5\), \(A_4\) has dimension \(5\times10\), \(A_5\) has dimension \(10\times20\), \(A_6\) has dimension \(20\times30\). Please calculate the minimum number of scalar multiplications.

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

長度 2:\(m[1,2]=30\cdot35\cdot15=15750\);\(m[2,3]=35\cdot15\cdot5=2625\);\(m[3,4]=15\cdot5\cdot10=750\);\(m[4,5]=5\cdot10\cdot20=1000\);\(m[5,6]=10\cdot20\cdot30=6000\)

長度 3:\(m[1,3]=\min(0+2625+30\cdot35\cdot5,\ 15750+0+30\cdot15\cdot5)=\min(7875,\,18000)=7875\)

  \(m[2,4]=\min(0+750+35\cdot15\cdot10,\ 2625+0+35\cdot5\cdot10)=\min(6000,\,4375)=4375\)

  \(m[3,5]=\min(0+1000+15\cdot5\cdot20,\ 750+0+15\cdot10\cdot20)=\min(2500,\,3750)=2500\)

  \(m[4,6]=\min(0+6000+5\cdot10\cdot30,\ 1000+0+5\cdot20\cdot30)=\min(7500,\,4000)=4000\)

往上逐長度算下去(表):

j=1j=2j=3j=4j=5j=6
m[1,·]015,7507,8759,37511,87516,375
m[2,·]02,6254,3757,12511,875
m[3,·]07502,5006,250
m[4,·]01,0004,000
m[5,·]06,000
m[6,·]0

最佳切點 \(s[i,j]\):

j=1j=2j=3j=4j=5j=6
s[1,·]11333
s[2,·]2333
s[3,·]333
s[4,·]45
s[5,·]5

\(m[1,6]=\min\{m[1,1]+m[2,6]+30\cdot35\cdot30,\ \ldots,\ m[1,3]+m[4,6]+30\cdot5\cdot30,\ \ldots\}\),在 \(k=3\) 取到:\(7875+4000+4500=16375\)

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

\(\big(A_1(A_2A_3)\big)\big((A_4A_5)A_6\big)\)

最少 16375 次純量乘法

二-610 分

6. (10%) Give asymptotic upper and lower bounds for \(T(n)=2T(n/4)+\sqrt n\). Assume that \(T(n)\) is constant for \(n\le2\). Make your bounds as tight as possible.

master theorem:\(a=2,\ b=4,\ f(n)=\sqrt n\)

\(n^{\log_ba}=n^{\log_42}=n^{1/2}\)

\(f(n)=\Theta(n^{1/2})=\Theta(n^{\log_ba})\) → case 2:\(T(n)=\Theta(n^{1/2}\lg n)\)

遞迴樹確認:第 \(i\) 層 \(2^i\) 個節點,每個 \(\sqrt{n/4^i}=\sqrt n/2^i\),每層總和都是 \(\sqrt n\);共 \(\log_4n=\tfrac12\lg n\) 層 → \(\tfrac12\sqrt n\lg n\);葉子 \(2^{\log_4n}=\sqrt n\) 個,\(\Theta(\sqrt n)\)

\(T(n)=\Theta(\sqrt n\,\lg n)\),即 \(O(\sqrt n\lg n)\) 且 \(\Omega(\sqrt n\lg n)\)

二-715 分

7. (15%) Consider the problem of finding the 5-vector \(x=(x_i)\) that satisfies

\(\begin{bmatrix}1&-1&0&0&0\\1&0&0&0&-1\\0&1&0&0&-1\\-1&0&1&0&0\\-1&0&0&1&0\\0&0&-1&1&0\\0&0&-1&0&1\\0&0&0&-1&1\end{bmatrix}\begin{bmatrix}x_1\\x_2\\x_3\\x_4\\x_5\end{bmatrix}\le\begin{bmatrix}0\\-1\\1\\5\\-4\\-1\\-3\\-3\end{bmatrix}\)

Determine whether there exists a solution or there is no solution.

每一列都是 \(x_i-x_j\le c\) 的形式(difference constraints):

\(x_1-x_2\le0\);\(x_1-x_5\le-1\);\(x_2-x_5\le1\);\(x_3-x_1\le5\);\(x_4-x_1\le-4\);\(x_4-x_3\le-1\);\(x_5-x_3\le-3\);\(x_5-x_4\le-3\)

constraint graph:\(x_i-x_j\le c\) → 邊 \(x_j\to x_i\),權重 \(c\)

x1x2x3x4x50−115−4−1−3−3

有 negative cycle(紅線)\(x_1\to x_4\to x_5\to x_1\):\(-4+(-3)+(-1)=-8\lt0\)

對應三條不等式相加:\((x_4-x_1)+(x_5-x_4)+(x_1-x_5)=0\le-4-3-1=-8\) → \(0\le-8\),矛盾

這組式子與標準範例只差第 5 列右邊(那裡是 \(x_4-x_1\le4\),有解);本題是 \(-4\),沒有解

no solution(constraint graph 有 negative cycle,\(x_1\to x_4\to x_5\to x_1\) 總權重 \(-8\))

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com