108 年 國立中央大學資訊工程學系軟體工程碩士班《資料結構與演算法》
第 1 題5 分
Suppose that the following list is the result of the first partition step of quick sort.
Which of the following statements is correct about the partition step?
(A) The pivot could have been either or .
(B) The pivot must be .
(C) Both and are pivots in the first partition.
(D) Neither nor could have been the pivot.
(E) None of the above.
登入後即可作答並保存紀錄。
核心觀念
快速排序的一次分割會選定一個樞紐值(pivot),並將資料分到樞紐兩側:左側元素不大於樞紐,右側元素不小於樞紐。分割後,樞紐位於兩側資料之間;不要求同一側的元素彼此排序。
解題方法
圖中給出的分割結果是 ,題目詢問第一次分割的樞紐可能是哪個值。檢查各候選值左右兩側是否符合分割條件:
- 若樞紐是 ,左側為 ,都小於 ;右側為 ,都大於 。
- 若樞紐是 ,左側為 ,都小於 ;右側為 ,都大於 。
第 2 題5 分
Build a binary search tree for the input sequence . It is assumed that the tree root is on level .
(A) There are five levels in the tree.
(B) is on level .
(C) and have the same father.
(D) The left subtree of has node.
(E) None of the above.
登入後即可作答並保存紀錄。
核心觀念
二元搜尋樹(BST)插入新鍵值時,若新值小於目前節點,就往左子樹;若大於目前節點,就往右子樹,直到遇到空位置後插入。題目指定根節點位於第 層,因此根的子節點在第 層。
解題方法
依原卷圖,題目給定插入序列 ,並指定根在第 層。依序插入各值:
- 為根。
- ,放在 左側;,放在 右側;,放在 左側。
- ,放在 右側;,放在 左側;,放在 左側。
- ,放在 左側。
- 插入 時,、、、,所以放在 左側。
建成的樹如下:
第 3 題5 分
Which of the following tree is a legal max-heap?
(A) A tree with level-order traversal sequence .
(B) A tree with level-order traversal sequence .
(C) A tree with level-order traversal sequence .
(D) A tree with level-order traversal sequence .
(E) None of the above.
登入後即可作答並保存紀錄。
核心觀念
最大堆(max-heap)必須同時符合兩個條件:
- 完全二元樹:除最後一層外,每層都填滿;最後一層由左至右排列。
- 堆序性質:每個父節點的值都大於或等於其子節點的值。
題目以「層序走訪」(level-order traversal)列出節點,因此可直接把序列依完全二元樹的位置排列,再檢查父子大小關係。
解題方法
依原卷圖,選項列出的層序序列分別為:
(A) 、(B) 、(C) 、(D) 。
The following procedure recursively generates all the permutations of to .
void perm(char , int , int )
{
int , ;
if () { / print the newly generated permutation /
for (; ; ++)
printf("%c", );
printf(" ");
}
else { / generate permutations recursively */
for (; ; ++) {
(B1) ____
(B2) ____
swap(, , );
}
}
}
第 4 題5 分
Blank (B1) in the algorithm above should be ____.
(A) swap(, , )
(B) swap(, , )
(C) swap(, , )
(D) swap(, , )
(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(, , )
(B) perm(, , )
(C) perm(, , )
(D) perm(, , )
(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 buckets and each bucket has slots. Suppose that the hash function is , and the following numbers are sequentially inserted into the hash table:
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 .
(B) If linear probing is adopted to handle overflow, the summation of the numbers in the full buckets of the hash table is .
(C) If quadratic probing is adopted to handle overflow, the summation of the numbers in the full buckets of the hash table is .
(D) Compared with quadratic probing, linear probing requires fewer bucket accesses (in average).
(E) None of the above.
登入後即可作答並保存紀錄。
核心觀念
每個 bucket 有 個 slot,因此只有已放入 個數字的 bucket 才是「full bucket」。線性探測遇到滿 bucket 時,依序檢查下一個 bucket;二次探測採標準探測序列 ,其中 。每次檢查一個 bucket,計為一次 bucket access。
解題方法
原卷列出 個 bucket、每個 bucket 個 slot,雜湊函數為 ,依序插入 。先按插入順序記錄每個數字的落點,再加總所有滿 bucket 中的數字。
線性探測的插入結果如下:
| Bucket | 內容 |
|---|---|
| 2 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 |
例如, 的初始位置是 bucket ,但 bucket 、 都已滿,因此放入 bucket 的第二個 slot; 接著探測到 bucket , 則放入 bucket 的第二個 slot。
線性探測的滿 bucket 數字總和為
二次探測時, 從 bucket 出發,依序檢查 、,放入 bucket 。接著 探測 bucket 、、,放入 bucket 的第二個 slot; 放入 bucket 。結果如下:
第 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 , where 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.
登入後即可作答並保存紀錄。
核心觀念
本題考查雜湊函數的除法取餘法、碰撞處理方式,以及動態雜湊的桶數調整。
除法取餘法通常寫成 。若多個鍵值對 同餘,它們就會被映射到同一個桶。動態雜湊則會隨著資料量變化,動態分裂或合併資料桶。
解題方法
圖中第 7 題詢問哪些敘述正確;頁面上方標示本大題為複選題。判斷時要看敘述是否可能成立:選項 (A) 使用「可能造成」碰撞,因此檢查是否存在鍵值分布會讓碰撞嚴重;選項 (D) 則核對它對動態雜湊的描述是否符合桶會隨資料量調整的特性。
選項分析
(A) 正確。 若多個鍵值都與同一數值模 同餘,就會被映射到同一個桶。例如鍵值若全是 的倍數,雜湊值都為 ,會造成嚴重碰撞。
Consider the problem of solving all-pairs shortest-paths on a weighted directed graph . A famous dynamic programming algorithm gives a recursive formula to compute , the length of a shortest path from to using only vertices with indices not greater than .
Here it is assumed that the vertex set if the given graph contains vertices.
Now if the given graph has vertices and edges:
where each triple represents there is an edge directed from to with weight . Then, after the execution of the algorithm, each term will be computed correctly.
第 8 題5 分
For the following items, choose the correct one(s):
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
本題考 Floyd–Warshall 全點對最短路徑演算法。 表示從頂點 到頂點 ,只允許編號不大於 的頂點作為中繼點時的最短距離。遞迴式為
題圖中的邊為 、、、、、、、、;各邊權重依序為第三個數字。
解題方法
從題圖讀到有向加權圖的頂點為 至 ,並依題意逐步納入頂點 、、 作為中繼點。以下只需計算與選項相關的距離。
納入中繼點 、 後:
- 可走 ,距離為 。
- 可走 ,距離為 。
接著納入中繼點 :
- 。
第 9 題5 分
Follow the previous question. Choose the correct item(s):
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
本題考 Floyd–Warshall 動態規劃。 表示從頂點 到頂點 ,且中途只允許經過編號不大於 的頂點時,最短路徑的長度。遞迴式為
兩項分別代表路徑不經過頂點 ,以及路徑經過頂點 。
解題方法
依原卷圖中的共用題幹,頂點為 到 ,有向加權邊為 、、、、、、、、。先求只允許經過頂點 至 時的相關距離,再將頂點 納入遞迴。
由邊 可得 ;由 可得 。另有 、、、,而 。
Given a weighted undirected graph , let denote the distance (the length of a shortest path) from vertex to vertex .
A path is said to be a progress path, if for .
For example, consider the graph with vertex set and edge set:
where each triple represents there is an undirected edge between and with weight .
Then, in this graph, there are totally shortest paths from to (i.e. and ) and there are progress paths from to (they are , and ).
Now consider another graph with vertex set and edge set:
第 10 題5 分
According to this graph, choose the correct item(s):
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
題目考加權無向圖的最短路徑。 表示從 到 的所有路徑中,邊權總和的最小值。由於邊權皆為正,可用 Dijkstra 演算法逐步確定各頂點到目標 的最短距離。
解題方法
依原圖讀取頂點 及無向邊與權重:、、、、、、、、、、、。從 開始計算各點到 的最短距離:
- 的相鄰點為 、,因此 、。
- 經由 到 ,可得 、。
- 到 的候選路徑有 ,長度 ;以及 ,長度 ,故 。
- 到 的最短路徑為 ,長度 。
- 到 的最短路徑為 ,長度 。
因此各點到 的距離為:
第 11 題5 分
Follow the previous question. Choose the correct item(s):
(A) is a progress path.
(B) is a progress path.
(C) is a progress path.
(D) is a progress path.
(E) is a progress path.
登入後即可作答並保存紀錄。
核心觀念
本題的核心觀念為加權無向圖(Weighted Undirected Graph)上的**最短路徑距離(Shortest Path Distance)以及進展路徑(Progress Path)**的定義與判斷。
-
最短路徑距離 :
在圖 中,節點 到節點 的最短路徑長度記為 。可藉由 Dijkstra 演算法計算出目標終點到所有其他頂點的最短距離。 -
進展路徑(Progress Path):
根據題幹定義,路徑 為 progress path 的充要條件是:路徑上每往後走一步,當前節點到終點 的最短距離都必須嚴格遞減,即:這意味著序列 必須為嚴格單調遞減數列(其中最後一項 )。
解題方法
從題目附圖中可讀出無向圖包含 8 個頂點 ,其邊與權重分別為:
。
由於所有選項給出的路徑終點皆為 (即 ),我們首先以節點 為起點,使用 Dijkstra 演算法求出各節點至終點 的最短距離 :
- 初始化:
,其餘頂點設為 。 - 由 出發鬆弛(Relaxation):
- 至 :
- 至 :
- 選取當前距離最小之未確定節點 (距離 4):
- 經 至 :
- 經 至 :
- 選取節點 (距離 6):
- 經 至 :
- 經 至 :(不更新)
- 經 至 :(不更新)
- 選取節點 (距離 7)與節點 (距離 7):
- 經 至 :(保持 11)
- 經 至 :
- 選取節點 (距離 11):
- 經 至 :
- 經 至 :
- 選取節點 (距離 12):
- 經 至 :
- 選取節點 (距離 13):全部節點處理完畢。
各頂點到 的最短距離整理如下表:
| 頂點 | 最短路徑 | 最短距離 |
|---|---|---|
| 或 | ||
| 或 |
第 12 題5 分
Follow the previous question. Choose the correct item(s):
(A) There is totally progress path from to .
(B) There are totally progress paths from to .
(C) There is totally progress path from to .
(D) There are totally progress paths from to .
(E) There are totally progress paths from to .
登入後即可作答並保存紀錄。
核心觀念
進展路徑不必是最短路徑;它只要求路徑上每走一步,所在頂點到終點 的最短距離都嚴格縮短。也就是對路徑上的每條邊 ,必須有 。
先算出各頂點到 的最短距離,再只沿著距離嚴格下降的邊前進,便能計算進展路徑數。
解題方法
依圖讀取頂點 ,以及無向加權邊
。以 為終點,從 往外計算最短距離。
各頂點到 的距離為:
例如, 經過 到 的長度為 ; 經過 或經過 到 的最短距離都是 ; 經過 到 的距離為 。
依照距離嚴格下降的條件,保留以下方向的邊:
第 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 , where is an array and , , and are indices numbering elements of the array such that . The procedure assumes that the subarrays and are in sorted order. It merges them to form a single sorted subarray that replaces the current subarray .
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. ()
(b) Analyze the time complexity of the merge sort algorithm with the big-O notation. ()
Input:
: an array of elements;
: indices of , where , and the subarrays and are in sorted order (from small to large).
Output:
: an array of elements, where the subarray is in sorted order (from small to large).
- create arrays and
- for to
-
do $L[i]\leftarrow A[p+i-1]$ - for to
-
do $R[j]\leftarrow A[q+j]$ - for to
-
do if $L[i]\le R[j]$ -
then $A[k]\leftarrow L[i]$ -
$i\leftarrow i+1$ -
else $A[k]\leftarrow R[j]$ -
$j\leftarrow j+1$
登入後即可作答並保存紀錄。
核心觀念
本題考「分治法」與合併排序。分治法把大問題切成較小的子問題,遞迴求解後再合併結果。合併排序將陣列分成左右兩段,分別排序,再用題目提供的 合併。
圖中的合併程序假設 與 已由小到大排序;它使用左右暫存陣列,並在末端放入 作為哨兵,最後將合併結果寫回 。
解題方法
先將待排序範圍 切成兩段,令中點為 。遞迴排序左右兩段後,呼叫 合併。當範圍只剩零個或一個元素時,該範圍已經排序完成,直接返回。
(a) 合併排序演算法
輸入: 陣列 ,以及待排序範圍的起點與終點索引 ,其中 。
輸出: 將 由小到大排序;範圍外的元素不變。
第 2 題8 分
Given a chain of matrices, where matrix , , has dimension , the matrix-chain multiplication problem is to fully parenthesize the product 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 to be the minimum number of scalar multiplications needed to compute the matrix product , .
Since we know the value of for , we can then calculate in a bottom-up manner as the minimum number of scalar multiplications for the product by a recursive form of .
Write down the recursive form of . ()
登入後即可作答並保存紀錄。
第 3 題7 分
Below is the algorithm AllPairCost which computes the shortest distances between all pairs of vertices , where .
Formally, given distances of edges in graph , determine the shortest distances between all pairs of vertices in . Note that the distance of an arbitrary edge in is non-negative.
(a) Blanks (B3) and (B4) in the algorithm below should be ____ and ____. ()
(b) Blank (B5) in the algorithm below should be ____. ()
Algorithm AllPairCost
Input: a two dimensional array , where denotes the distance of directed edge (i.e. the edge from vertex to vertex ).
Output: a two dimensional array , where denotes the shortest distance from vertex to vertex .
1: int , , ;
2: for (; ; ++)
3: for (; ; ++)
4: (B5) ____;
5: for (; ; ++)
6: for (; ; ++)
7: for (; ; ++)
8: if ((B3) ____)
9: (B4) ____;
登入後即可作答並保存紀錄。
核心觀念
本題考 Floyd–Warshall 全點對最短路徑演算法。令 表示目前已允許特定中間頂點集合時,從頂點 到頂點 的最短距離。
每次把頂點 加入可作為中間點的集合時,從 到 的最短距離有兩種情形:不經過 ,或經過 。因此更新公式為
解題方法
依原卷圖,演算法先用兩層迴圈初始化 ,再以 為外層迴圈逐一加入中間頂點,並檢查是否能透過 縮短 到 的距離。因此,若經過 的路徑較短,就更新 。
初始化時,直接邊的距離就是目前已知的初始距離:令 。若頂點 到 沒有直接邊,矩陣中應以 表示;對角線則設為 ,因為頂點到自身的最短距離為 。
第 4 題8 分
Suppose that array maintains a binary tree. For a binary tree node stored in , its two children (if exist) are stored in and , respectively.
The program below aims to complete the following two tasks:
i. adjust array 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 ____. ()
(b) Blank (B7) in the program below should be ____. ()
void adjust(int [], int , int )
{
int , ;
int ;
;
;
;
while () {
if (() && ())
++;
if ()
break;
else {
(B6) ____;
;
}
}
(B7) ____;
}
void heapsort(int [], int )
{ /* perform a heap sort on /
int , ;
int ;
for (; ; ++) / adjust the binary tree to establish the max heap /
adjust(, , );
for (; ; --) { / heap sort /
swap(, , ); / exchange and */
adjust(, , );
}
}
登入後即可作答並保存紀錄。