108 年 國立中央大學資訊工程學系軟體工程碩士班《資料結構與演算法》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題5 分

Suppose that the following list is the result of the first partition step of quick sort.

12, 9, 1, 13, 19, 24, 22, 2012,\ 9,\ 1,\ 13,\ 19,\ 24,\ 22,\ 20

Which of the following statements is correct about the partition step?

(A) The pivot could have been either 1313 or 1919.
(B) The pivot must be 1313.
(C) Both 1313 and 1919 are pivots in the first partition.
(D) Neither 1313 nor 1919 could have been the pivot.
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

快速排序的一次分割會選定一個樞紐值(pivot),並將資料分到樞紐兩側:左側元素不大於樞紐,右側元素不小於樞紐。分割後,樞紐位於兩側資料之間;不要求同一側的元素彼此排序。

解題方法

圖中給出的分割結果是 12,9,1,13,19,24,22,2012, 9, 1, 13, 19, 24, 22, 20,題目詢問第一次分割的樞紐可能是哪個值。檢查各候選值左右兩側是否符合分割條件:

  • 若樞紐是 1313,左側為 12,9,112, 9, 1,都小於 1313;右側為 19,24,22,2019, 24, 22, 20,都大於 1313。
  • 若樞紐是 1919,左側為 12,9,1,1312, 9, 1, 13,都小於 1919;右側為 24,22,2024, 22, 20,都大於 1919。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題5 分

Build a binary search tree for the input sequence 9,4,8,7,20,15,14,3,109,4,8,7,20,15,14,3,10. It is assumed that the tree root is on level 11.

(A) There are five levels in the tree.
(B) 2020 is on level 33.
(C) 77 and 1010 have the same father.
(D) The left subtree of 2020 has 11 node.
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

二元搜尋樹(BST)插入新鍵值時,若新值小於目前節點,就往左子樹;若大於目前節點,就往右子樹,直到遇到空位置後插入。題目指定根節點位於第 11 層,因此根的子節點在第 22 層。

解題方法

依原卷圖,題目給定插入序列 9,4,8,7,20,15,14,3,109,4,8,7,20,15,14,3,10,並指定根在第 11 層。依序插入各值:

  • 99 為根。
  • 4<94<9,放在 99 左側;8>48>4,放在 44 右側;7<87<8,放在 88 左側。
  • 20>920>9,放在 99 右側;15<2015<20,放在 2020 左側;14<1514<15,放在 1515 左側。
  • 3<43<4,放在 44 左側。
  • 插入 1010 時,10>910>9、10<2010<20、10<1510<15、10<1410<14,所以放在 1414 左側。

建成的樹如下:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題5 分

Which of the following tree is a legal max-heap?

(A) A tree with level-order traversal sequence {67,45,19,22,43,16}\{67,45,19,22,43,16\}.
(B) A tree with level-order traversal sequence {67,43,19,22,45,16}\{67,43,19,22,45,16\}.
(C) A tree with level-order traversal sequence {14,18,27,19,63,48}\{14,18,27,19,63,48\}.
(D) A tree with level-order traversal sequence {45,22,67,8,34,52}\{45,22,67,8,34,52\}.
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

最大堆(max-heap)必須同時符合兩個條件:

  1. 完全二元樹:除最後一層外,每層都填滿;最後一層由左至右排列。
  2. 堆序性質:每個父節點的值都大於或等於其子節點的值。

題目以「層序走訪」(level-order traversal)列出節點,因此可直接把序列依完全二元樹的位置排列,再檢查父子大小關係。

解題方法

依原卷圖,選項列出的層序序列分別為:
(A) {67,45,19,22,43,16}\{67,45,19,22,43,16\}、(B) {67,43,19,22,45,16}\{67,43,19,22,45,16\}、(C) {14,18,27,19,63,48}\{14,18,27,19,63,48\}、(D) {45,22,67,8,34,52}\{45,22,67,8,34,52\}。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 2 題共用同一段題幹

The following procedure recursively generates all the permutations of list[i]list[i] to list[n]list[n].

void perm(char listlist, int ii, int nn)
{
int jj, temptemp;
if (i==ni==n) { /
print the newly generated permutation /
for (j=0j=0; j≤nj\le n; jj++)
printf("%c", list[j]list[j]);
printf(" ");
}
else { /
generate permutations recursively */
for (j=ij=i; j≤nj\le n; jj++) {
(B1) ____
(B2) ____
swap(list[i]list[i], list[j]list[j], temptemp);
}
}
}

第 4 題5 分

Blank (B1) in the algorithm above should be ____.

(A) swap(list[i+1]list[i+1], list[j]list[j], temptemp)
(B) swap(list[i]list[i], list[j]list[j], temptemp)
(C) swap(list[i]list[i], list[j+1]list[j+1], temptemp)
(D) swap(list[i+1]list[i+1], list[j+1]list[j+1], temptemp)
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考遞迴與回溯法產生排列。每一層固定一個位置:把目前位置 i 與候選位置 j 的元素交換,再遞迴排列後面的元素;遞迴返回後,再交換一次以還原陣列,讓下一個候選位置能從原始狀態開始。

解題方法

依原卷圖,迴圈範圍是 j=i 到 j=n;(B1)、(B2) 之後還有一行 swap(list[i], list[j], temp)。因此,(B1) 應先把 list[j] 放到目前要固定的 list[i],接著 (B2) 遞迴排列剩餘元素,最後再用題目原有的 swap 還原。

關鍵程式結構如下:

for (j = i; j <= n; j++) {
    swap(list[i], list[j], temp);  /* B1:選定本層元素 */
    perm(list, i + 1, n);          /* B2:排列剩餘元素 */
    swap(list[i], list[j], temp);  /* 還原,供下一輪使用 */
}

所以 (B1) 必須是 swap(list[i], list[j], temp)。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題5 分

Blank (B2) in the algorithm above should be ____.

(A) perm(listlist, i+1i+1, nn)
(B) perm(listlist, ii, nn)
(C) perm(listlist, jj, nn)
(D) perm(listlist, j+1j+1, nn)
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考遞迴產生排列時的「固定一個位置,再排列其後元素」:

  • list[i] 到 list[n] 是目前尚未固定的部分。
  • 每次迴圈先把 list[j] 換到位置 i,再遞迴排列剩下的 list[i+1] 到 list[n]。
  • 遞迴返回後,再交換一次,把陣列恢復,供下一輪迴圈使用。

解題方法

圖中共用程式的順序是:先執行空格(B1)、再執行空格(B2),最後交換 list[i] 和 list[j]。因此,前面的(B1)負責把選定元素移到目前位置,後面的交換負責還原;(B2)則應遞迴處理下一個位置。

swap(list[i], list[j], temp);  /* B1:固定目前位置 */
perm(list, i+1, n);            /* B2:排列剩餘部分 */
swap(list[i], list[j], temp);  /* 還原陣列 */

當 i 增加到 n 時,所有位置都已固定,程式便輸出一個排列。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6 題5 分

Consider an empty hash table with 1010 buckets and each bucket has 22 slots. Suppose that the hash function is h(n)=n mod 10h(n)=n\bmod 10, and the following numbers are sequentially inserted into the hash table:

6, 16, 22, 45, 54, 25, 7, 75, 5, 1086,\ 16,\ 22,\ 45,\ 54,\ 25,\ 7,\ 75,\ 5,\ 108

Which of the following statements are true?

(A) If linear probing is adopted to handle overflow, the summation of the numbers in the full buckets of the hash table is 287287.
(B) If linear probing is adopted to handle overflow, the summation of the numbers in the full buckets of the hash table is 172172.
(C) If quadratic probing is adopted to handle overflow, the summation of the numbers in the full buckets of the hash table is 221221.
(D) Compared with quadratic probing, linear probing requires fewer bucket accesses (in average).
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

每個 bucket 有 22 個 slot,因此只有已放入 22 個數字的 bucket 才是「full bucket」。線性探測遇到滿 bucket 時,依序檢查下一個 bucket;二次探測採標準探測序列 h(n)+i2(mod10)h(n)+i^2 \pmod{10},其中 i=1,2,…i=1,2,\ldots。每次檢查一個 bucket,計為一次 bucket access。

解題方法

原卷列出 1010 個 bucket、每個 bucket 22 個 slot,雜湊函數為 h(n)=n mod 10h(n)=n\bmod 10,依序插入 6,16,22,45,54,25,7,75,5,1086,16,22,45,54,25,7,75,5,108。先按插入順序記錄每個數字的落點,再加總所有滿 bucket 中的數字。

線性探測的插入結果如下:

Bucket內容
22222
45454
545,2545,25
66,166,16
77,757,75
85,1085,108

例如,7575 的初始位置是 bucket 55,但 bucket 55、66 都已滿,因此放入 bucket 77 的第二個 slot;55 接著探測到 bucket 88,108108 則放入 bucket 88 的第二個 slot。

線性探測的滿 bucket 數字總和為

(45+25)+(6+16)+(7+75)+(5+108)=287(45+25)+(6+16)+(7+75)+(5+108)=287

二次探測時,7575 從 bucket 55 出發,依序檢查 66、99,放入 bucket 99。接著 55 探測 bucket 55、66、99,放入 bucket 99 的第二個 slot;108108 放入 bucket 88。結果如下:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7 題5 分

A hash function maps a key into a bucket in the hash table. Which of the following statements is true?

(A) A division hash function with divisor D=7rD=7^r, where rr is an integer, may result in serious collision.
(B) Compared with hash chaining, open addressing requires fewer bucket accesses (in average).
(C) When hash chaining is used to resolve overflows, the search for a key involves comparison with keys that have different hash values.
(D) In dynamic hashing, data buckets grow or shrink (added or removed dynamically) as the records increase or decrease.
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查雜湊函數的除法取餘法、碰撞處理方式,以及動態雜湊的桶數調整。

除法取餘法通常寫成 h(k)=k mod Dh(k)=k\bmod D。若多個鍵值對 DD 同餘,它們就會被映射到同一個桶。動態雜湊則會隨著資料量變化,動態分裂或合併資料桶。

解題方法

圖中第 7 題詢問哪些敘述正確;頁面上方標示本大題為複選題。判斷時要看敘述是否可能成立:選項 (A) 使用「可能造成」碰撞,因此檢查是否存在鍵值分布會讓碰撞嚴重;選項 (D) 則核對它對動態雜湊的描述是否符合桶會隨資料量調整的特性。

選項分析

(A) 正確。 若多個鍵值都與同一數值模 7r7^r 同餘,就會被映射到同一個桶。例如鍵值若全是 7r7^r 的倍數,雜湊值都為 00,會造成嚴重碰撞。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 2 題共用同一段題幹

Consider the problem of solving all-pairs shortest-paths on a weighted directed graph G=(V,E)G=(V,E). A famous dynamic programming algorithm gives a recursive formula to compute d(k)[i,j]d^{(k)}[i,j], the length of a shortest path from ii to jj using only vertices with indices not greater than kk.

Here it is assumed that the vertex set V={1,2,…,n}V=\{1,2,\ldots,n\} if the given graph contains nn vertices.

Now if the given graph has 55 vertices and edges:

(1,4,−5), (1,5,2), (2,1,6), (3,1,1), (3,2,7), (4,3,4), (5,2,−4), (5,3,3), (5,4,8)(1,4,-5),\ (1,5,2),\ (2,1,6),\ (3,1,1),\ (3,2,7),\ (4,3,4),\ (5,2,-4),\ (5,3,3),\ (5,4,8)

where each triple (i,j,t)(i,j,t) represents there is an edge directed from ii to jj with weight tt. Then, after the execution of the algorithm, each term d(k)[i,j]d^{(k)}[i,j] will be computed correctly.

第 8 題5 分

For the following items, choose the correct one(s):

(A) d(2)[5,1]=2d^{(2)}[5,1]=2
(B) d(2)[5,4]=−3d^{(2)}[5,4]=-3
(C) d(3)[4,1]=4d^{(3)}[4,1]=4
(D) d(3)[4,2]=11d^{(3)}[4,2]=11
(E) d(3)[4,5]=6d^{(3)}[4,5]=6

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考 Floyd–Warshall 全點對最短路徑演算法。d(k)[i,j]d^{(k)}[i,j] 表示從頂點 ii 到頂點 jj,只允許編號不大於 kk 的頂點作為中繼點時的最短距離。遞迴式為

d(k)[i,j]=min⁡(d(k−1)[i,j],d(k−1)[i,k]+d(k−1)[k,j]).d^{(k)}[i,j] = \min\left( d^{(k-1)}[i,j], d^{(k-1)}[i,k]+d^{(k-1)}[k,j] \right).

題圖中的邊為 (1,4,−5)(1,4,-5)、(1,5,2)(1,5,2)、(2,1,6)(2,1,6)、(3,1,1)(3,1,1)、(3,2,7)(3,2,7)、(4,3,4)(4,3,4)、(5,2,−4)(5,2,-4)、(5,3,3)(5,3,3)、(5,4,8)(5,4,8);各邊權重依序為第三個數字。

解題方法

從題圖讀到有向加權圖的頂點為 11 至 55,並依題意逐步納入頂點 11、22、33 作為中繼點。以下只需計算與選項相關的距離。

納入中繼點 11、22 後:

  • d(2)[5,1]d^{(2)}[5,1] 可走 5→2→15\to2\to1,距離為 −4+6=2-4+6=2。
  • d(2)[5,4]d^{(2)}[5,4] 可走 5→2→1→45\to2\to1\to4,距離為 −4+6−5=−3-4+6-5=-3。

接著納入中繼點 33:

  • d(3)[4,1]=d(2)[4,3]+d(2)[3,1]=4+1=5d^{(3)}[4,1]=d^{(2)}[4,3]+d^{(2)}[3,1]=4+1=5。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 9 題5 分

Follow the previous question. Choose the correct item(s):

(A) d(4)[1,2]=7d^{(4)}[1,2]=7
(B) d(4)[1,3]=−1d^{(4)}[1,3]=-1
(C) d(5)[1,2]=−2d^{(5)}[1,2]=-2
(D) d(5)[3,2]=−2d^{(5)}[3,2]=-2
(E) d(5)[4,2]=3d^{(5)}[4,2]=3

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考 Floyd–Warshall 動態規劃。d(k)[i,j]d^{(k)}[i,j] 表示從頂點 ii 到頂點 jj,且中途只允許經過編號不大於 kk 的頂點時,最短路徑的長度。遞迴式為

d(k)[i,j]=min⁡(d(k−1)[i,j],d(k−1)[i,k]+d(k−1)[k,j]).d^{(k)}[i,j] = \min\left( d^{(k-1)}[i,j], d^{(k-1)}[i,k]+d^{(k-1)}[k,j] \right).

兩項分別代表路徑不經過頂點 kk,以及路徑經過頂點 kk。

解題方法

依原卷圖中的共用題幹,頂點為 11 到 55,有向加權邊為 (1,4,−5)(1,4,-5)、(1,5,2)(1,5,2)、(2,1,6)(2,1,6)、(3,1,1)(3,1,1)、(3,2,7)(3,2,7)、(4,3,4)(4,3,4)、(5,2,−4)(5,2,-4)、(5,3,3)(5,3,3)、(5,4,8)(5,4,8)。先求只允許經過頂點 11 至 44 時的相關距離,再將頂點 55 納入遞迴。

由邊 1→4→31\to4\to3 可得 d(4)[1,3]=−5+4=−1d^{(4)}[1,3]=-5+4=-1;由 1→4→3→21\to4\to3\to2 可得 d(4)[1,2]=−5+4+7=6d^{(4)}[1,2]=-5+4+7=6。另有 d(4)[3,2]=7d^{(4)}[3,2]=7、d(4)[4,2]=4+7=11d^{(4)}[4,2]=4+7=11、d(4)[3,5]=1+2=3d^{(4)}[3,5]=1+2=3、d(4)[4,5]=4+1+2=7d^{(4)}[4,5]=4+1+2=7,而 d(4)[5,2]=−4d^{(4)}[5,2]=-4。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

Given a weighted undirected graph G=(V,E)G=(V,E), let δ[u,v]\delta[u,v] denote the distance (the length of a shortest path) from vertex uu to vertex vv.

A path [v1,v2,…,vk][v_1,v_2,\ldots,v_k] is said to be a progress path, if δ[vi,vk]>δ[vi+1,vk]\delta[v_i,v_k]>\delta[v_{i+1},v_k] for 1≤i<k1\le i<k.

For example, consider the graph with vertex set {1,2,3,4}\{1,2,3,4\} and edge set:

(1,2,2), (2,4,1), (3,1,2), (3,4,2), (1,4,3)(1,2,2),\ (2,4,1),\ (3,1,2),\ (3,4,2),\ (1,4,3)

where each triple (i,j,t)(i,j,t) represents there is an undirected edge between ii and jj with weight tt.

Then, in this graph, there are totally 22 shortest paths from 11 to 44 (i.e. [1,2,4][1,2,4] and [1,4][1,4]) and there are 33 progress paths from 11 to 44 (they are [1,2,4][1,2,4], [1,4][1,4] and [1,3,4][1,3,4]).

Now consider another graph with vertex set {a,b,c,d,e,f,g,h}\{a,b,c,d,e,f,g,h\} and edge set:

(a,b,3), (a,c,1), (b,c,1), (b,e,5), (b,f,4), (c,d,9), (d,e,5), (d,g,3),(a,b,3),\ (a,c,1),\ (b,c,1),\ (b,e,5),\ (b,f,4),\ (c,d,9),\ (d,e,5),\ (d,g,3),
(e,f,2), (e,g,2), (f,h,7), (g,h,4)(e,f,2),\ (e,g,2),\ (f,h,7),\ (g,h,4)

第 10 題5 分

According to this graph, choose the correct item(s):

(A) δ[a,h]=13\delta[a,h]=13
(B) δ[b,h]=12\delta[b,h]=12
(C) δ[d,h]=6\delta[d,h]=6
(D) δ[e,h]=5\delta[e,h]=5
(E) δ[f,h]=7\delta[f,h]=7

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

題目考加權無向圖的最短路徑。δ[u,v]\delta[u,v] 表示從 uu 到 vv 的所有路徑中,邊權總和的最小值。由於邊權皆為正,可用 Dijkstra 演算法逐步確定各頂點到目標 hh 的最短距離。

解題方法

依原圖讀取頂點 {a,b,c,d,e,f,g,h}\{a,b,c,d,e,f,g,h\} 及無向邊與權重:(a,b,3)(a,b,3)、(a,c,1)(a,c,1)、(b,c,1)(b,c,1)、(b,e,5)(b,e,5)、(b,f,4)(b,f,4)、(c,d,9)(c,d,9)、(d,e,5)(d,e,5)、(d,g,3)(d,g,3)、(e,f,2)(e,f,2)、(e,g,2)(e,g,2)、(f,h,7)(f,h,7)、(g,h,4)(g,h,4)。從 hh 開始計算各點到 hh 的最短距離:

  • hh 的相鄰點為 ff、gg,因此 δ[f,h]≤7\delta[f,h]\le 7、δ[g,h]≤4\delta[g,h]\le 4。
  • 經由 gg 到 hh,可得 δ[e,h]≤2+4=6\delta[e,h]\le 2+4=6、δ[d,h]≤3+4=7\delta[d,h]\le 3+4=7。
  • 到 bb 的候選路徑有 b→f→hb\to f\to h,長度 4+7=114+7=11;以及 b→e→g→hb\to e\to g\to h,長度 5+2+4=115+2+4=11,故 δ[b,h]=11\delta[b,h]=11。
  • 到 cc 的最短路徑為 c→b→f→hc\to b\to f\to h,長度 1+4+7=121+4+7=12。
  • 到 aa 的最短路徑為 a→c→b→f→ha\to c\to b\to f\to h,長度 1+1+4+7=131+1+4+7=13。

因此各點到 hh 的距離為:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 11 題5 分

Follow the previous question. Choose the correct item(s):

(A) [b,c,d,g,h][b,c,d,g,h] is a progress path.
(B) [b,e,d,g,h][b,e,d,g,h] is a progress path.
(C) [b,e,f,h][b,e,f,h] is a progress path.
(D) [a,b,f,h][a,b,f,h] is a progress path.
(E) [a,b,e,g,h][a,b,e,g,h] is a progress path.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題的核心觀念為加權無向圖(Weighted Undirected Graph)上的**最短路徑距離(Shortest Path Distance)以及進展路徑(Progress Path)**的定義與判斷。

  1. 最短路徑距離 δ[u,v]\delta[u, v]:
    在圖 G=(V,E)G=(V, E) 中,節點 uu 到節點 vv 的最短路徑長度記為 δ[u,v]\delta[u, v]。可藉由 Dijkstra 演算法計算出目標終點到所有其他頂點的最短距離。

  2. 進展路徑(Progress Path):
    根據題幹定義,路徑 [v1,v2,…,vk][v_1, v_2, \ldots, v_k] 為 progress path 的充要條件是:路徑上每往後走一步,當前節點到終點 vkv_k 的最短距離都必須嚴格遞減,即:

    δ[vi,vk]>δ[vi+1,vk],∀1≤i<k\delta[v_i, v_k] > \delta[v_{i+1}, v_k], \quad \forall 1 \le i < k

    這意味著序列 (δ[v1,vk],δ[v2,vk],…,δ[vk,vk])(\delta[v_1, v_k], \delta[v_2, v_k], \ldots, \delta[v_k, v_k]) 必須為嚴格單調遞減數列(其中最後一項 δ[vk,vk]=0\delta[v_k, v_k] = 0)。


解題方法

從題目附圖中可讀出無向圖包含 8 個頂點 {a,b,c,d,e,f,g,h}\{a, b, c, d, e, f, g, h\},其邊與權重分別為:
(a,b,3),(a,c,1),(b,c,1),(b,e,5),(b,f,4),(c,d,9),(d,e,5),(d,g,3),(e,f,2),(e,g,2),(f,h,7),(g,h,4)(a,b,3), (a,c,1), (b,c,1), (b,e,5), (b,f,4), (c,d,9), (d,e,5), (d,g,3), (e,f,2), (e,g,2), (f,h,7), (g,h,4)。

由於所有選項給出的路徑終點皆為 hh(即 vk=hv_k = h),我們首先以節點 hh 為起點,使用 Dijkstra 演算法求出各節點至終點 hh 的最短距離 δ[v,h]\delta[v, h]:

  1. 初始化:
    δ[h,h]=0\delta[h, h] = 0,其餘頂點設為 ∞\infty。
  2. 由 hh 出發鬆弛(Relaxation):
    • 至 gg:δ[g,h]=4\delta[g, h] = 4
    • 至 ff:δ[f,h]=7\delta[f, h] = 7
  3. 選取當前距離最小之未確定節點 gg(距離 4):
    • 經 gg 至 ee:4+2=6  ⟹  δ[e,h]=64 + 2 = 6 \implies \delta[e, h] = 6
    • 經 gg 至 dd:4+3=7  ⟹  δ[d,h]=74 + 3 = 7 \implies \delta[d, h] = 7
  4. 選取節點 ee(距離 6):
    • 經 ee 至 bb:6+5=11  ⟹  δ[b,h]=116 + 5 = 11 \implies \delta[b, h] = 11
    • 經 ee 至 ff:6+2=8>76 + 2 = 8 > 7(不更新)
    • 經 ee 至 dd:6+5=11>76 + 5 = 11 > 7(不更新)
  5. 選取節點 dd(距離 7)與節點 ff(距離 7):
    • 經 ff 至 bb:7+4=117 + 4 = 11(保持 11)
    • 經 dd 至 cc:7+9=167 + 9 = 16
  6. 選取節點 bb(距離 11):
    • 經 bb 至 cc:11+1=12<16  ⟹  δ[c,h]=1211 + 1 = 12 < 16 \implies \delta[c, h] = 12
    • 經 bb 至 aa:11+3=1411 + 3 = 14
  7. 選取節點 cc(距離 12):
    • 經 cc 至 aa:12+1=13<14  ⟹  δ[a,h]=1312 + 1 = 13 < 14 \implies \delta[a, h] = 13
  8. 選取節點 aa(距離 13):全部節點處理完畢。

各頂點到 hh 的最短距離整理如下表:

頂點 vv最短路徑最短距離 δ[v,h]\delta[v, h]
hh[h][h]00
gg[g,h][g, h]44
ee[e,g,h][e, g, h]66
dd[d,g,h][d, g, h]77
ff[f,h][f, h]77
bb[b,f,h][b, f, h] 或 [b,e,g,h][b, e, g, h]1111
cc[c,b,f,h][c, b, f, h] 或 [c,b,e,g,h][c, b, e, g, h]1212
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 12 題5 分

Follow the previous question. Choose the correct item(s):

(A) There is totally 11 progress path from ee to hh.
(B) There are totally 99 progress paths from aa to hh.
(C) There is totally 11 progress path from ff to hh.
(D) There are totally 33 progress paths from bb to hh.
(E) There are totally 55 progress paths from cc to hh.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

進展路徑不必是最短路徑;它只要求路徑上每走一步,所在頂點到終點 hh 的最短距離都嚴格縮短。也就是對路徑上的每條邊 u→vu\to v,必須有 δ[u,h]>δ[v,h]\delta[u,h]>\delta[v,h]。

先算出各頂點到 hh 的最短距離,再只沿著距離嚴格下降的邊前進,便能計算進展路徑數。

解題方法

依圖讀取頂點 {a,b,c,d,e,f,g,h}\{a,b,c,d,e,f,g,h\},以及無向加權邊
(a,b,3),(a,c,1),(b,c,1),(b,e,5),(b,f,4),(c,d,9),(d,e,5),(d,g,3),(e,f,2),(e,g,2),(f,h,7),(g,h,4)(a,b,3),(a,c,1),(b,c,1),(b,e,5),(b,f,4),(c,d,9),(d,e,5),(d,g,3),(e,f,2),(e,g,2),(f,h,7),(g,h,4)。以 hh 為終點,從 hh 往外計算最短距離。

各頂點到 hh 的距離為:

δ[h,h]=0,δ[g,h]=4,δ[e,h]=6,δ[f,h]=7,δ[d,h]=7,δ[b,h]=11,δ[c,h]=12,δ[a,h]=13.\begin{aligned} \delta[h,h]&=0, & \delta[g,h]&=4, & \delta[e,h]&=6,\\ \delta[f,h]&=7, & \delta[d,h]&=7, & \delta[b,h]&=11,\\ \delta[c,h]&=12, & \delta[a,h]&=13. \end{aligned}

例如,ee 經過 gg 到 hh 的長度為 2+4=62+4=6;bb 經過 e,ge,g 或經過 ff 到 hh 的最短距離都是 1111;cc 經過 bb 到 hh 的距離為 1+11=121+11=12。

依照距離嚴格下降的條件,保留以下方向的邊:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 1 題17 分

A divide-and-conquer algorithm solves a problem directly if the problem is small. If the problem is large, it is first divided into two or more parts called subproblems. Each subproblem is then recursively solved (conquered) in the same manner. Afterwards, the solutions to the subproblems are combined (merged) into a solution to the original problem.

The merge sort algorithm is a well-known example following the divide-and-conquer paradigm. The key operation of the merge sort algorithm is the merging of two sorted sequences.

To perform the merging, we use an auxiliary procedure MERGE⁡(A,p,q,r)\operatorname{MERGE}(A,p,q,r), where AA is an array and pp, qq, and rr are indices numbering elements of the array such that p≤q<rp\le q<r. The procedure assumes that the subarrays A[p…q]A[p\ldots q] and A[q+1…r]A[q+1\ldots r] are in sorted order. It merges them to form a single sorted subarray that replaces the current subarray A[p…r]A[p\ldots r].

The pseudo code of the MERGE operation is shown below. Based on the operation, we can design the merge sort algorithm easily.

(a) Write a detailed divide-and-conquer merge sort algorithm using the MERGE operation. Note that the algorithm MUST include well-described input and output. (9%9\%)

(b) Analyze the time complexity of the merge sort algorithm with the big-O notation. (8%8\%)

MERGE⁡(A,p,q,r)\operatorname{MERGE}(A,p,q,r)

Input:
AA: an array of elements;
p,q,rp,q,r: indices of AA, where p≤q<rp\le q<r, and the subarrays A[p…q]A[p\ldots q] and A[q+1…r]A[q+1\ldots r] are in sorted order (from small to large).

Output:
AA: an array of elements, where the subarray A[p…r]A[p\ldots r] is in sorted order (from small to large).

  1. n1←q−p+1n_1\leftarrow q-p+1
  2. n2←r−qn_2\leftarrow r-q
  3. create arrays L[1…n1+1]L[1\ldots n_1+1] and R[1…n2+1]R[1\ldots n_2+1]
  4. for i←1i\leftarrow 1 to n1n_1
  5. do $L[i]\leftarrow A[p+i-1]$
    
  6. for j←1j\leftarrow 1 to n2n_2
  7. do $R[j]\leftarrow A[q+j]$
    
  8. L[n1+1]←∞L[n_1+1]\leftarrow\infty
  9. R[n2+1]←∞R[n_2+1]\leftarrow\infty
  10. i←1i\leftarrow 1
  11. j←1j\leftarrow 1
  12. for k←pk\leftarrow p to rr
  13. do if $L[i]\le R[j]$
    
  14.     then $A[k]\leftarrow L[i]$
    
  15.          $i\leftarrow i+1$
    
  16.     else $A[k]\leftarrow R[j]$
    
  17.          $j\leftarrow j+1$
    

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考「分治法」與合併排序。分治法把大問題切成較小的子問題,遞迴求解後再合併結果。合併排序將陣列分成左右兩段,分別排序,再用題目提供的 MERGE⁡(A,p,q,r)\operatorname{MERGE}(A,p,q,r) 合併。

圖中的合併程序假設 A[p…q]A[p\ldots q] 與 A[q+1…r]A[q+1\ldots r] 已由小到大排序;它使用左右暫存陣列,並在末端放入 ∞\infty 作為哨兵,最後將合併結果寫回 A[p…r]A[p\ldots r]。

解題方法

先將待排序範圍 A[p…r]A[p\ldots r] 切成兩段,令中點為 q=⌊(p+r)/2⌋q=\lfloor(p+r)/2\rfloor。遞迴排序左右兩段後,呼叫 MERGE⁡(A,p,q,r)\operatorname{MERGE}(A,p,q,r) 合併。當範圍只剩零個或一個元素時,該範圍已經排序完成,直接返回。

(a) 合併排序演算法

輸入: 陣列 AA,以及待排序範圍的起點與終點索引 p,rp,r,其中 p≤rp\le r。

輸出: 將 A[p…r]A[p\ldots r] 由小到大排序;範圍外的元素不變。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題8 分

Given a chain ⟨A1,…,An⟩\langle A_1,\ldots,A_n\rangle of nn matrices, where matrix AiA_i, i=1,…,ni=1,\ldots,n, has dimension pi−1×pip_{i-1}\times p_i, the matrix-chain multiplication problem is to fully parenthesize the product A1…AnA_1\ldots A_n in a way that minimizes the number of scalar multiplications.

We can use the dynamic programming strategy to solve the matrix-chain multiplication problem as follows. Define m[i,j]m[i,j] to be the minimum number of scalar multiplications needed to compute the matrix product Ai…AjA_i\ldots A_j, i≤ji\le j.

Since we know the value of m[i,j]m[i,j] for i=ji=j, we can then calculate m[1,n]m[1,n] in a bottom-up manner as the minimum number of scalar multiplications for the product A1…AnA_1\ldots A_n by a recursive form of m[i,j]m[i,j].

Write down the recursive form of m[i,j]m[i,j]. (8%8\%)

登入後即可作答並保存紀錄。

這一題的完整詳解
載入中…

第 3 題7 分

Below is the algorithm AllPairCost which computes the shortest distances between all pairs of vertices i,ji,j, where i≠ji\ne j.

Formally, given distances of edges in graph GG, determine the shortest distances between all pairs of vertices in GG. Note that the distance of an arbitrary edge in GG is non-negative.

(a) Blanks (B3) and (B4) in the algorithm below should be ____ and ____. (4%4\%)

(b) Blank (B5) in the algorithm below should be ____. (3%3\%)

Algorithm AllPairCost

Input: a two dimensional array CC, where C[i][j]C[i][j] denotes the distance of directed edge (i,j)(i,j) (i.e. the edge from vertex ii to vertex jj).

Output: a two dimensional array DD, where D[i][j]D[i][j] denotes the shortest distance from vertex ii to vertex jj.

1: int ii, jj, kk;
2: for (i=0i=0; i<ni<n; ii++)
3: for (j=0j=0; j<nj<n; jj++)
4: (B5) ____;
5: for (k=0k=0; k<nk<n; kk++)
6: for (i=0i=0; i<ni<n; ii++)
7: for (j=0j=0; j<nj<n; jj++)
8: if ((B3) ____)
9: (B4) ____;

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考 Floyd–Warshall 全點對最短路徑演算法。令 D[i][j]D[i][j] 表示目前已允許特定中間頂點集合時,從頂點 ii 到頂點 jj 的最短距離。

每次把頂點 kk 加入可作為中間點的集合時,從 ii 到 jj 的最短距離有兩種情形:不經過 kk,或經過 kk。因此更新公式為

D[i][j]=min⁡(D[i][j], D[i][k]+D[k][j])D[i][j] = \min\bigl(D[i][j],\ D[i][k]+D[k][j]\bigr)

解題方法

依原卷圖,演算法先用兩層迴圈初始化 DD,再以 kk 為外層迴圈逐一加入中間頂點,並檢查是否能透過 kk 縮短 ii 到 jj 的距離。因此,若經過 kk 的路徑較短,就更新 D[i][j]D[i][j]。

初始化時,直接邊的距離就是目前已知的初始距離:令 D[i][j]=C[i][j]D[i][j]=C[i][j]。若頂點 ii 到 jj 沒有直接邊,矩陣中應以 ∞\infty 表示;對角線則設為 00,因為頂點到自身的最短距離為 00。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題8 分

Suppose that array A[1:n]A[1:n] maintains a binary tree. For a binary tree node stored in A[i]A[i], its two children (if exist) are stored in A[2i]A[2i] and A[2i+1]A[2i+1], respectively.

The program below aims to complete the following two tasks:

i. adjust array AA to establish a max heap, and
ii. apply heap sort on the max heap built in (i) in nondecreasing order.

(a) Blank (B6) in the program below should be ____. (4%4\%)

(b) Blank (B7) in the program below should be ____. (4%4\%)

void adjust(int AA[], int rootroot, int nn)
{
int childchild, rootkeyrootkey;
int temptemp;
temp=A[root]temp=A[root];
rootkey=A[root]rootkey=A[root];
child=2∗rootchild=2*root;
while (child≤nchild\le n) {
if ((child≤nchild\le n) && (A[child]<A[child+1]A[child]<A[child+1]))
childchild++;
if (rootkey>A[child]rootkey>A[child])
break;
else {
(B6) ____;
child∗=2child\mathrel{*{=}}2;
}
}
(B7) ____;
}

void heapsort(int AA[], int nn)
{ /* perform a heap sort on A[1:n]A[1:n] /
int ii, jj;
int temptemp;
for (i=n/2i=n/2; i>0i>0; ii++) /
adjust the binary tree to establish the max heap /
adjust(AA, ii, nn);
for (i=n−1i=n-1; i>0i>0; ii--) { /
heap sort /
swap(A[1]A[1], A[i+1]A[i+1], temptemp); /
exchange A[i]A[i] and A[i+1]A[i+1] */
adjust(AA, 11, ii);
}
}

登入後即可作答並保存紀錄。

這一題的完整詳解
載入中…

其他考古題