113 年 國立陽明交通大學資訊工程學系碩士班《資料結構與演算法》
The following contains the C++ code for a queue implemented with a singly linked list.
struct Qnode {
int key;
Qnode *next;
Qnode(int n = 0) : key(n), next(NULL) {}
};
struct Qlist {
Qnode *head, *tail;
Qlist() : head(NULL), tail(NULL) {}
void push(int n) {
Qnode* node = new Qnode(n);
if (!head) ① = node;
if (tail) ② = node;
tail = ③;
}
void pop() {
if (head) {
Qnode* node = ④;
⑤ = ⑥;
delete node;
}
if (!head) tail = NULL;
}
};
第 1-(1) 題7 分
Which way of filling in the code will make the push function work correctly?
(A) ①: head; ②: tail->next; ③: node
(B) ①: head->next; ②: tail; ③: node->next
(C) ①: tail; ②: tail->next; ③: head
(D) ①: head->next; ②: tail; ③: tail->next
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
以單向鏈結串列實作佇列時,head 指向隊首,tail 指向隊尾。執行 push 後必須維持:
- 空佇列時,新節點同時是
head與tail。 - 非空佇列時,原隊尾的
next指向新節點,然後更新tail指向新節點。
解題方法
依序檢查空佇列與非空佇列兩種情況。
選項 A 填入後,程式相當於:
if (!head) head = node;
if (tail) tail->next = node;
tail = node;
空佇列時,head 原本為 NULL,因此設為新節點;tail 原本為 NULL,不執行第二個 if;最後將 tail 設為新節點。此時 head 與 tail 都指向新節點。
非空佇列時,head 已有節點,不執行第一個 if;接著將原隊尾的 next 設為新節點,最後更新 tail。兩種情況都符合佇列操作。
選項分析
- (A) 正確。
head = node處理空佇列;tail->next = node將新節點接在原隊尾後;tail = node更新隊尾。
第 1-(2) 題7 分
Which way of filling in the code will make the pop function work correctly?
(A) ④: tail; ⑤: tail->next; ⑥: tail
(B) ④: head; ⑤: head->next; ⑥: node->next
(C) ④: tail; ⑤: tail; ⑥: node
(D) ④: head; ⑤: head; ⑥: head->next
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
佇列採先進先出(FIFO),head 指向隊首,也就是下一個要移除的節點;tail 指向隊尾。pop 必須先保存隊首節點,再將 head 往後移一格,最後刪除原隊首節點。
解題方法
正確的操作順序是:
- 令
node指向目前的head。 - 將
head更新為原隊首的下一個節點。 - 刪除原隊首節點。
- 若刪除後
head為空,代表佇列已空,將tail設為NULL。
因此,應填入 ④: head、⑤: head、⑥: head->next,也就是選項 D。
選項分析
- (A) 錯誤。
node指向tail,而tail->next = tail會讓尾節點指向自己;接著刪除node,使tail成為懸空指標,佇列鏈結遭到破壞。 - **(B) 錯誤。
第 1-(3) 題7 分
Based on the code above, given the following statements, what are the key values of the two nodes pointed to by head and tail?
Qlist L = Qlist();
L.pop(); L.push(10); L.push(5); L.push(3); L.pop();
L.push(12); L.push(8); L.pop();
(A) head: 10, tail: 8
(B) head: 10, tail: 12
(C) head: 3, tail: 8
(D) head: 3, tail: 10
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
單向鏈結串列佇列遵守先進先出(FIFO):
head指向隊首,也就是下一個要被pop()移除的節點。tail指向隊尾,也就是最近加入的節點。push(n)將新節點接到隊尾;若佇列為空,head與tail都指向新節點。pop()移除隊首,令head指向原隊首的下一個節點;若移除後佇列為空,tail設為NULL。
解題方法
依序追蹤節點順序,以及 head、tail 所指的位置:
| 操作 | 佇列由隊首至隊尾 | head | tail |
|---|---|---|---|
| 初始狀態 | 空 | — | — |
L.pop() | 空 | — | — |
L.push(10) | 10 | 10 | 10 |
L.push(5) | 10 → 5 | 10 | 5 |
L.push(3) | 10 → 5 → 3 | 10 | 3 |
L.pop() | 5 → 3 | 5 | 3 |
The following code is a C implementation of a sorting algorithm:
void FSort(int* a, int left, int right)
{
if (left < right) {
static int c = 0;
int i = left, j = right + 1;
int d = a[left];
do {
do i++; while (a[i] < d);
do j--; while (a[j] > d);
if (i < j) {
int z = a[i]; a[i] = a[j]; a[j] = z;
}
} while (i < j);
int z = a[left]; a[left] = a[j]; a[j] = z;
printf("c=%d\\n", ++c);
FSort(a, left, j - 1);
FSort(a, j + 1, right);
}
}
第 2-(1) 題5 分
Assume that we want to use this function to sort the 10-element array given by:
Which of the following is the correct call?
(A) FSort(m, 1, 9)
(B) FSort(m, 0, 9)
(C) FSort(m, 1, 10)
(D) FSort(m, 0, 10)
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
C 語言陣列採用 0 起始索引。含有 10 個元素的陣列 m,有效索引為 到 。
FSort(a, left, right) 的 left、right 表示要排序區間的左右端點,且兩端都包含在內。程式以 a[left] 作為樞紐,並從 right + 1 設定右側掃描索引,因此呼叫時必須傳入有效的陣列索引範圍。
解題方法
陣列共有 10 個元素:
要排序整個陣列,左端點是第一個索引 ,右端點是最後一個索引 ,所以呼叫為:
FSort(m, 0, 9)
程式使用快速排序的分割方式,完成樞紐分割後,再遞迴排序樞紐左右兩側。static int c 只用來累計並印出分割次數,不影響陣列索引或排序範圍。
選項分析
第 2-(2) 題5 分
For the above array , assuming that the call above is correct, what is the largest value of in the text output?
(A) 9
(B) 10
(C) 4
(D) 5
(E) 6
登入後即可作答並保存紀錄。
核心觀念
這段程式是快速排序(Quick Sort),以 a[left] 作為樞紐值 d,先將樞紐放到分割後的位置 j,再遞迴處理樞紐左右兩側。
c 是函式內的 static 變數,只初始化一次,所有遞迴呼叫共用同一個計數器。每次執行 left < right 的呼叫完成分割後,程式就會輸出一次並將 c 加一。
解題方法
每次分割會讓一個樞紐元素到達最終位置,之後的遞迴不再處理該位置。若陣列有 個元素,當所有區段都縮小到零個或一個元素時,共有 次非平凡分割,因此輸出中的最大 c 為 。
題幹提到的陣列 未附在訊息中;依本題標準答案所對應的情況,取 有 個元素:
選項分析
第 2-(3) 題5 分
Which of the following input arrays will result in the most number of calls to FSort?
(A) int m[] = {3,5,9,8,2,0,4,1,6,7};
(B) int m[] = {1,3,5,7,9,0,2,4,6,8};
(C) int m[] = {5,6,7,8,9,0,1,2,3,4};
(D) int m[] = {0,1,2,3,4,5,6,7,8,9};
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
FSort 是以第一個元素 a[left] 為樞紐的快速排序。每次分割後,樞紐會放到最終位置,再分別遞迴處理左右子陣列:
d = a[left]
...
a[left] = a[j]
FSort(a, left, j - 1)
FSort(a, j + 1, right)
遞迴深度取決於每次分割的平衡程度。若樞紐每次都是子陣列的最小值或最大值,便會分成大小為 與 的兩側,形成最差情況,遞迴深度為 。
解題方法
觀察樞紐 d 的選法:每次都取目前子陣列最左側的元素。選項 (D) 是遞增排列,因此每次的樞紐都是目前子陣列的最小值。
以 (D) 為例,第一次分割以 為樞紐,分割大小為 與 ;接著右側子陣列以 為樞紐,再分成 與 。這種情形一路持續:
因此快速排序形成最長的遞迴鏈,最多需要 次分割,時間複雜度為 ,遞迴堆疊空間為 。
選項分析
Insert the keys into a hash table of size using double hashing with the hash functions and for collision resolution with open addressing.
第 3-(1) 題6 分
Which index will the key be placed in after resolving any collisions using double hashing?
(A) 0
(B) 4
(C) 5
(D) 8
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
開放定址的雙重雜湊法以第二個雜湊函數決定每次碰撞後的探查步長。鍵值 的第 次探查位置為
其中 是初始位置;若該位置已被占用,就依序探查後續位置,直到找到空格。
解題方法
依題目給定的鍵值順序,先插入 之前的鍵值:
- :,放在索引 。
- :,放在索引 。
- :,放在索引 。
- :,放在索引 。
- :初始位置為 ,與 碰撞。步長為 。接著探查 索引 已被 占用;再探查 索引 空著,因此 放在索引 。
插入 時,初始位置為
第 3-(2) 題6 分
Which key pair(s) below will cause a collision when inserting the given keys to the hash table using the first hash function ?
(A) 80 and 69
(B) 16 and 41
(C) 73 and 30
(D) All of the above
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
雙重雜湊的第一個雜湊函數 會給出每個鍵值的初始位置。若該位置在插入當下已被先前插入的鍵值占用,就會發生碰撞,再依 尋找其他位置。
因此,判斷本題各選項時,要依照題目給定的插入順序,確認每個鍵值的初始位置在輪到它插入時是否已被占用。
解題方法
依序計算各鍵值的初始位置:
依照 的順序插入:
- 插入位置 ; 插入位置 ; 插入位置 。
- 的初始位置是 ,當時為空,因此沒有碰撞。
- 的初始位置是 ,已被 占用,因此發生碰撞。
- 的初始位置是 ,已被 占用,因此發生碰撞。
第 3-(3) 題6 分
Which index(es) will remain empty after all keys have been inserted?
(A) 1
(B) 5 and 6
(C) 7 and 9
(D) 10 and 12
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
雙重雜湊使用開放定址法處理碰撞。對鍵值 ,第 次探查的位置為
其中 是起始位置, 是每次碰撞後的探查步長。若位置已被占用,就依序增加 ,直到找到空格。
解題方法
依題目給定順序插入各鍵值。每次先算起始位置與步長,若起始位置已占用,再依探查序列尋找下一個空格:
| 鍵值 | 探查位置 | 插入位置 | ||
|---|---|---|---|---|
| 80 | 2 | 4 | 2 | 2 |
| 69 | 4 | 4 | 4 | 4 |
| 99 | 8 | 1 | 8 | 8 |
| 16 | 3 | 6 | 3 | 3 |
| 73 | 8 | 8 | 8、3、11 | 11 |
| 30 | 4 | 9 | 4、0 | 0 |
| 41 | 2 | 9 | 2、11、7 | 7 |
Given the graph with nodes labeled A to G, apply Kruskal's algorithm to find the minimum spanning tree (MST). The edges and their respective weights are as follows:
| Edge | A-B | A-D | B-C | B-D | B-E | C-E | D-E | D-F | E-F | E-G | F-G |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Weight | 7 | 5 | 8 | 9 | 7 | 5 | 15 | 6 | 8 | 9 | 11 |
第 4-(1) 題6 分
Which of the following edges are included in the MST after applying Kruskal's algorithm?
(A) A-D
(B) C-E
(C) D-E
(D) F-G
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
Kruskal 演算法依邊權重由小到大檢查,每次加入一條「不會形成環」的邊,直到涵蓋全部頂點。含有 個頂點的生成樹恰有 條邊;本題有 7 個頂點,因此 MST 有 6 條邊。
解題方法
將邊依權重由小到大排列,並逐條檢查是否會形成環:
| 權重 | 邊 | 處理結果 |
|---|---|---|
| 5 | A-D | 加入 |
| 5 | C-E | 加入 |
| 6 | D-F | 加入 |
| 7 | A-B | 加入 |
| 7 | B-E | 加入,連接兩個不同的連通分量 |
| 8 | B-C | 拒絕,B、C 已連通,加入會形成環 |
| 8 | E-F | 拒絕,E、F 已連通,加入會形成環 |
| 9 | B-D | 拒絕,B、D 已連通,加入會形成環 |
| 9 | E-G | 加入,將 G 接入生成樹 |
第 4-(2) 題6 分
What is the total weight of the MST for this graph?
(A) 39
(B) 40
(C) 41
(D) 42
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
Kruskal 演算法將所有邊依權重由小到大排序,逐一考慮:若加入某邊不會形成環,就將它納入最小生成樹;若會形成環,就略過。含有 個頂點的生成樹恰有 條邊,因此本題 7 個頂點的最小生成樹需要 6 條邊。
解題方法
依權重由小到大考慮各邊:
| 權重 | 邊 | 判斷 |
|---|---|---|
| 5 | 納入,連接 、 | |
| 5 | 納入,連接 、 | |
| 6 | 納入, 尚未連通 | |
| 7 | 納入, 尚未連通 | |
| 7 | 納入,連接目前的兩個連通分量 | |
| 8 | 略過; 與 已經連通,加入會形成環 | |
| 8 | 略過; 與 已經連通,加入會形成環 | |
| 9 | 略過; |
第 4-(3) 題6 分
If the edge(s) with the lowest weight in the MST is(are) removed, which node pair(s) become disconnected?
(A) A and D
(B) B and E
(C) C and E
(D) D and F
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
Kruskal 演算法依邊權重由小到大選邊;若加入某邊會形成環,就略過。含 個節點的最小生成樹(MST)有 條邊,且移除樹中的任一邊都會使樹分成兩個連通部分。
解題方法
依權重排序並套用 Kruskal 演算法:
| 權重 | 邊 | 處理結果 |
|---|---|---|
| 5 | A–D | 加入 |
| 5 | C–E | 加入 |
| 6 | D–F | 加入 |
| 7 | A–B | 加入 |
| 7 | B–E | 加入 |
| 8 | B–C | 略過;加入會形成環 |
| 8 | E–F | 略過;加入會形成環 |
| 9 | E–G | 加入,所有節點連通 |
因此 MST 邊為 A–D、C–E、D–F、A–B、B–E、E–G,共 條。MST 中最低權重為 ,對應兩條邊:A–D 與 C–E。
第 5-(1) 題5 分
Consider the max-flow problem. Let be a network with edge capacity for all and source-sink pair . Which of the following statements is/are true?
(A) If and are both valid - cuts, then so is .
(B) If are both feasible - flow functions, then so is , which adds up the flows of each pair of nodes.
(C) For any feasible - flow function with value and any feasible - cut with total capacity , we always have .
(D) Applying the Bellman-Ford algorithm returns the maximum flow for .
(E) The Ford-Fulkerson algorithm can be modified to solve the maximum flow problem in polynomial time.
登入後即可作答並保存紀錄。
核心觀念
**- 割(cut)**是頂點集合的一個分割 ,其中 、。割的容量是所有從 指向 的邊容量總和:
可行流 必須滿足兩項條件:
- 容量限制:每條邊的流量不能為負,也不能超過容量,即 。
- 流量守恆:除 以外,每個頂點的流入量等於流出量。
流值 是從 送出的淨流量。最大流最小割定理指出,最大流值等於最小 - 割的容量;因此任何可行流值都不會大於任何可行割的容量。
解題方法
逐項檢查選項所述是否符合割的定義、可行流的容量與守恆條件,以及最大流演算法的性質。
選項分析
(A) 正確。
若 和 都是有效的 - 割,則 、,且 、。所以 ,而 。因此 仍符合 - 割的定義。
Consider the following residual flow network.
🖼️【此處有附圖,請對照原卷】
第 5-(2) 題5 分
Which of the following statements is/are true?
(A) The path is a valid augmenting path with value .
(B) Let and . Then is a minimum - cut with weight .
(C) The Edmonds-Karp algorithm will use as an augmenting path.
(D) Changing the residual weight settings of the three edges , , and to , , and will increase the value of the maximum flow by .
(E) If we add a new edge with capacity to the network, the value of the minimum - cut will also increase by .
登入後即可作答並保存紀錄。
核心觀念
殘餘網路中,標示 的邊表示目前流量為 、容量為 。沿原方向可再送的流量是 ,沿反方向可退回的流量是 。增廣路徑的增廣量,等於路徑上各殘餘容量的最小值。
有向 - 割的權重,是從 側指向 側的邊容量總和;由 側指回 側的邊不計入。最大流最小割定理指出,最大流值等於最小割權重。
解題方法
圖中可讀到的方向與容量包括: 為 、 為 、 為 、 為 、 為 、 為 、 為 、 為 、 為 、 為 。先由流量與容量求增廣路徑的殘餘容量,再依邊的方向計算割權重。
選項分析
(A) 正確。 路徑 上的殘餘容量依序為 ,因此增廣量為
Consider the following C++ procedures:
int sum(long long n)
{
int result = 0;
while (n) {
result += n % 10;
n /= 10;
}
return result;
}
long long foo(long long n, int target)
{
long long x = n, y = 1;
while (sum(n) > target) {
n = n / 10 + 1;
y *= 10;
}
return n * y - x;
}
第 6-(1) 題9 分
Let be the output of foo(12321, 7). Which of the following statements is/are correct?
(A) has 2 digits.
(B) .
(C) .
(D) is a prime number.
(E) .
登入後即可作答並保存紀錄。
核心觀念
sum(n) 計算 的十進位數字和。foo 每次在數字和大於 target 時,將 更新為 ,並讓 乘以 。最後回傳 ,其中 保存原始輸入值。
解題方法
依序追蹤每次迴圈的 與 :
| 次數 | sum(n) | ||
|---|---|---|---|
| 初始 | |||
| 第 1 次更新後 | |||
| 第 2 次更新後 |
當 時,sum(n)=7,不再大於 target,迴圈停止。因此
第 6-(2) 題9 分
What is the most likely problem statement with the above C++ procedures as a solution?
(A) Find the largest prime number that is smaller than target.
(B) Find the sum of the digits in .
(C) Find the minimum number added to such that the digit sum is at most target.
(D) Find the maximum number added to such that the digit sum is at most target.
(E) Find the minimum number subtracted from such that the digit sum is at most target.
登入後即可作答並保存紀錄。
核心觀念
sum(n) 計算整數 的各位數字總和。foo 則反覆把數字向較高位進位,直到目前候選數的數字總和不超過 target,再回傳候選數與原數的差。
解題方法
令原數為 。程式以 n 記錄進位後的數字,以 y 記錄位值;每次迴圈執行:
n = n / 10 + 1:捨去個位數後加一,表示向前一位進位。y *= 10:把位值提高十倍。
因此,迴圈中的候選數是 。當 sum(n) <= target 時,n * y 就是符合條件的數,回傳值為:
也就是從原數 起,至少要增加的數量。
例如 、target = 10:
- 初始數字總和為 ,不符合條件;進位後
n = 98 + 1 = 99、y = 10。 - 的數字總和為 ,仍不符合;再進位後
n = 9 + 1 = 10、y = 100。 - 的數字總和為 ,符合條件。
候選數為 ,所以最少須增加 。
每次進位都跳過了較低位數字尚未進位的數值;那些數字只會增加數字總和,無法比進位後的候選數更早達到目標。因此,程式回傳的是符合條件的最小增加量。
Let be a prime number and the set . We can map a vector into a vector , where , , and
第 7-(1) 題6 分
Which of the following statements is/are true?
(A) Each can be computed in time.
(B) Each cannot be computed in time.
(C) The vector can be computed in time.
(D) The mapping from to is a 1-1 mapping.
(E) For any -dimensional vector with entries from , we can find a corresponding -dimensional vector based on the above mapping.
登入後即可作答並保存紀錄。
核心觀念
題目把向量 視為有限體 上的多項式
並令 ,其中 。因此, 是一個次數至多 的多項式在 所有元素上的取值。
解題關鍵有兩點:用 Horner 法計算多項式值;以及利用「非零多項式的根數不超過其次數」判斷映射是否為一對一。
解題方法
對固定的 ,可用 Horner 法改寫:
這需要 次模乘法與模加法。因此逐一計算 個分量,總時間為 。
若兩個向量 和 對應到相同的 ,令
則 對所有 都成立。因為 ,而 的次數至多為 ,它若有 個根,只能是零多項式。因此 對所有 都成立,映射是一對一。
選項分析
第 7-(2) 題6 分
Consider two distinct vectors and , where for . Suppose is mapped to and is mapped to as above. Which of the following statements is/are true?
(A) .
(B) There are at most 's such that .
(C) Let . Then is mapped to .
(D) From we can reconstruct .
(E) If , then .
登入後即可作答並保存紀錄。
核心觀念
這個映射把係數向量 視為多項式
並將它在 的每個元素 上求值,因此 。由於 是質數, 是有限域;非零多項式在域上的根數不會超過它的次數。
解題方法
比較兩個不同向量 與 ,可將它們對應的多項式相減。若兩向量不同,差多項式不是零多項式,且次數至多為 。它在某個 的值為零,恰好代表兩個映射結果在該位置相同。根數定理因此能判斷相同位置的數量上限。
此外,多項式求值具有線性;而多項式次數小於 時,只要知道它在 個不同點上的值,就能用插值唯一還原其係數。
選項分析
- (A) 正確。 設 與 分別對應 與 。因 ,差多項式 非零,且次數至多為 。它在 中最多有 個根,所以不可能在所有 個位置都為零;因此 。
Consider binary search for finding extremal points of a convex polygon in the given direction.
🖼️【此處有附圖,請對照原卷】
Let be a convex polygon, represented by the vertices of its boundary in clockwise order, say, for example, with and , and let be a given vector. Consider the projection of points in onto . For any two points , we write if the projection of lies farther from the starting end of than the projection of .
A point is said to be maximal with respect to if there exists no point such that . Similarly, is minimal with respect to if there exists no such that .
For example, in Figure (a), both and are maximal points of and is the minimal point of .
Let and be integers with . Consider the points .
The following is a sketch of pseudo-code that aims to compute a maximal point of for the given direction by binary search:
- , . // To search the range
- While , repeat the following:
- .
- Set if contains a maximal point of in the direction . Otherwise, set .
- Output if it is maximal. Otherwise, output .
第 8-(1) 題6 分
Which of the following statements is/are true?
(A) For any , any vector , and any sufficiently small , exactly one of and lies inside .
(B) For any , there always exists with and a real number such that .
(C) For any , there are an infinite number of lines that are tangent to at .
(D) For any point in the plane and any vector , if we construct a ray starting from in the direction of , then it always hits for an even number of times.
(E) For any two points , the line segment is always contained in .
登入後即可作答並保存紀錄。
核心觀念
凸多邊形的凸性表示:任取 中兩點,連接它們的線段都包含在 內。邊界點與內部點的幾何性質不同;判斷選項時,要留意「內部」是否包含邊界,以及射線的起點位置。
解題方法
圖中顯示凸多邊形的頂點依順時針排列,並以向量 的投影比較點的位置;圖 (a) 標出 為最大點、 為最小點,圖 (b) 示意 。本小題判斷的是凸多邊形的幾何性質,逐項檢查邊界、內部與凸性的定義即可。
選項分析
- (A) 錯。 取一個邊的中點 ,令 沿著該邊方向。當 夠小時, 與 都仍在該邊上。若「inside」指多邊形內部,兩點都不在內部;若包含邊界,則兩點都在 中。因此都不符合「恰有一個」。
- **(B) 對。
第 8-(2) 題6 分
Which of the following statements is/are true?
(A) If and , then contains no maximal points of in the direction .
(B) If and , then no maximal point of in the direction is contained within .
(C) If contains a minimal point of in the direction , then is a possible scenario to happen.
(D) If neither nor holds for some , then must be either a minimal point or a maximal point of in the direction .
(E) For any , the point is a maximal point of in the direction if and only if and .
登入後即可作答並保存紀錄。
核心觀念
將各頂點在 方向上的投影值記為
則 等價於 ;最大點與最小點分別對應全域最大、最小投影值。
沿凸多邊形邊界繞行一圈,投影值由最小值沿一側上升至最大值,再沿另一側下降至最小值。這個單調結構是循環的,不能直接把任意索引區間視為單調區間;此外,區間 包含兩個端點。
解題方法
原圖的 指向上方,因此位置越高,投影值越大。圖 (a) 中 為最高點, 為最低點;圖 (b) 則顯示 ,與投影高度的判讀一致。
判斷各選項時,將比較關係轉成投影值的上升、下降或相等,再以原圖或合法的凸多邊形反例檢查。
選項分析
(A) 錯誤:區間起點仍可能是最大點。
直接使用圖 (a),取 、。圖中有
符合選項的兩個條件,但 包含最大點 。
因此,即使區間起始處下降、結束後上升,也不能排除區間端點是最大點。
(B) 錯誤:兩端皆上升,區間仍可跨過最大點與最小點。
取一個正六邊形,依順時針排列頂點,令 向上,其頂點投影值依序為
這可由單位圓上角度依序為 的頂點構成,確實是凸正六邊形。
取 、、,則
兩個條件皆成立,但 包含最大點 ,所以敘述不成立。
(C) 正確:包含最小點的區間,仍可能延伸到下降段。
第 8-(3) 題6 分
Regarding the above algorithm, which of the following statements is/are true?
(A) When this algorithm terminates, it always outputs a maximal point of in the direction .
(B) For some specific input instance, this algorithm may not terminate at all.
(C) This algorithm may output a point that is not maximal for .
(D) This algorithm always terminates on any input instance.
(E) The statement “if contains a maximal point of ” can be tested in time.
登入後即可作答並保存紀錄。
核心觀念
令頂點在方向 上的投影值為
投影時除以 不影響大小關係,因此最大點就是使 最大的頂點。
凸多邊形的關鍵性質是:沿邊界循環走一圈,投影值由最小值上升至最大值,再下降至最小值;若有邊垂直於 ,極值處可以出現相同投影值的相鄰頂點。這種循環單峰性,使區間是否含有最大點能由端點附近的資訊判定。
解題方法
原卷印出的更新規則為 ,再依 是否包含最大點,令 或 ;最後先檢查 ,否則輸出 。其中 是搜尋範圍 的左邊界哨兵, 的有效頂點解讀為 ; 則是重複的邊界頂點。
1. 正確性:維持「候選區間內存在最大點」
初始搜尋範圍包含全部不同頂點,因此至少包含一個最大點。每次更新:
- 若 含有最大點,令 ,保留下來的區間仍含最大點。
- 若 不含最大點,原區間中的最大點必在右側;令 ,仍保留至少一個最大點。
終止時 ,候選頂點只剩端點 。若 是最大點就輸出 ;否則依上述不變量, 必為最大點。
2. 終止性:區間長度嚴格縮小
令 。只要迴圈條件成立,就有 ,因此
不論更新哪一端,新區間長度都滿足
正整數長度持續縮小,必定終止;迴圈次數為 。
3. 如何直接在 時間判定 是否含最大點
只需讀取端點及其相鄰頂點,不必先找出最大點的位置。 以下給出包含投影值相等情況的完整判定。
先檢查端點是否為最大點。 對頂點 ,以循環索引定義
若 、,且至少一個不等式嚴格成立,凸性保證 是全域最大點。
若 ,須區分最大值與最小值的平坦邊。由於頂點按順時針排列,邊的外側在左方,因此此時
表示 指向該邊的外側, 位於最大值的平坦邊;行列式小於零則位於最小值的平坦邊。以上均只需常數次運算。
The following contains two functions for traversing a binary tree:
struct Node {
char key;
Node *left, *right;
};
void trv1(Node* root)
{
std::queue<Node*> Q;
Q.push(root);
while (!Q.empty()) {
Node* cur = Q.front();
Q.pop();
printf("%c", cur->key);
if (cur->left) Q.push(cur->left);
if (cur->right) Q.push(cur->right);
}
}
void trv2(Node* root)
{
std::stack<Node*> Q;
Q.push(root);
while (!Q.empty()) {
Node* cur = Q.top();
Q.pop();
printf("%c", cur->key);
if (cur->right) Q.push(cur->right);
if (cur->left) Q.push(cur->left);
}
}
The traversal sequences are:
trv1:OPMRTECUStrv2:OPRTCSUME
第 第二部分 1-(A) 題3 分
Use the two traversal sequences above (each character is the key of a node) to reconstruct the binary tree. There is more than one possible tree, and you only need to show one of them.
登入後即可作答並保存紀錄。
核心觀念
trv1 使用佇列,依序取出較早加入的節點,因此是由左至右的層序走訪。
trv2 使用堆疊,先將右子節點、再將左子節點推入;左子節點會先被取出,因此走訪順序是根、左子樹、右子樹,也就是先序走訪。
解題方法
層序序列為 OPMRTECUS,所以根節點是 O,其左、右子節點依序為 P、M。
先序序列為 OPRTCSUME。根節點 O 之後先走訪左子樹,再走訪右子樹;因此 P 所在左子樹的節點依序是 P、R、T、C、S、U,右子樹則是 M、E。
安排左子樹時,令 P 的左右子節點為 R、T。R 沒有子節點;T 的左右子節點為 C、U,且 C 的左子節點為 S。如此先序走訪會是 P、R、T、C、S、U。再令 M 的左子節點為 E。
建構結果與驗算
第 第二部分 1-(B) 題3 分
Start with an empty binary search tree and insert the characters in COMPUTERS into it. List the resulting preorder traversal sequence.
登入後即可作答並保存紀錄。
核心觀念
二元搜尋樹(BST)插入節點時,若新字元小於目前節點,就往左子樹走;若大於目前節點,就往右子樹走,直到遇到空位置才插入。本題字元彼此相異,因此不需處理重複值。
前序走訪的順序是「根、左、右」:先記錄目前節點,再走訪左子樹,最後走訪右子樹。
解題方法
依序插入 COMPUTERS。以英文字母順序比較,每次從根節點開始,沿著 BST 規則找到新字元應插入的位置:
| 字元 | 插入位置 |
|---|---|
| C | 根節點 |
| O | 大於 C,插在 C 的右側 |
| M | 大於 C、小於 O,插在 O 的左側 |
| P | 大於 C、O,插在 O 的右側 |
| U | 大於 C、O、P,插在 P 的右側 |
| T | 大於 C、O、P,小於 U,插在 U 的左側 |
| E | 大於 C、小於 O、M,插在 M 的左側 |
| R | 大於 C、O、P,小於 U、T,插在 T 的左側 |
| S | 大於 C、O、P,小於 U、T,又大於 R,插在 R 的右側 |
第 第二部分 1-(C) 題2 分
Among preorder, postorder, inorder, and level-order traversals, which one(s) can uniquely identify a binary search tree by itself?
登入後即可作答並保存紀錄。
核心觀念
二元搜尋樹(BST)要求:每個節點左子樹的鍵值小於該節點,右子樹的鍵值大於該節點。本題採一般考題慣例,假設鍵值互異。
要判斷某種走訪能否唯一決定 BST,關鍵在於走訪序列能否確定根節點,以及每個節點的左右子樹所包含的鍵值。
解題方法
對前序、後序走訪而言,序列直接指出根節點;BST 的大小關係再將其餘鍵值分到左、右子樹,之後對兩側遞迴重建。
以鍵值集合 為例:
- 前序序列 :第一個鍵值 是根, 屬於左子樹, 屬於右子樹。
- 後序序列 :最後一個鍵值 是根;其餘序列中小於 的部分屬於左子樹,大於 的部分屬於右子樹。
層序走訪也能唯一決定 BST。依層序序列逐一將鍵值插入空 BST,即可重建原樹:每個節點的祖先都在該節點之前出現,因此插入時會先建立它所在搜尋路徑上的祖先。
選項分析
第 第二部分 1-(D) 題3 分
A node of a B-tree is modified from the tree node definition above such that a node can hold up to distinct keys (in an array named key) and pointers to child nodes (in an array named child), where is a positive integer. For a B-tree node with all its keys and child node pointers being valid, list the requirements of the key values in the subtrees rooted at the nodes pointed to by child with respect to the key values in key.
登入後即可作答並保存紀錄。
核心觀念
B 樹節點中的鍵值必須依遞增順序排列,子樹則依照相鄰鍵值所形成的區間分配。這項規則讓查詢時可以根據鍵值大小,判斷要往哪個子樹搜尋。
解題方法
令節點中的鍵值依序為 ,且
依序檢查 個子樹:
child[0]中的所有鍵值都小於key[0]。- 對 ,
child[i]中的所有鍵值都大於key[i-1]且小於key[i]。 child[M]中的所有鍵值都大於key[M-1]。
因此,各子樹所含鍵值的範圍可表示為:
第 第二部分 1-(E) 題2 分
In B-trees, to minimize the tree depth, each non-leaf node (except the root) is required to keep at least valid keys. Specify with respect to .
登入後即可作答並保存紀錄。
核心觀念
依前一小題 1-(D) 的定義,一個節點最多 個鍵值、 個子節點指標(也就是 order 的 B-tree)。注意這和「 為最多子節點數」的寫法差 1,代公式前要先對清楚。
B-tree 的最低占用率來自分裂:節點已有 個鍵時再插入一個,變成 個鍵,取中位數上移到父節點,其餘 個鍵分給左右兩個新節點:
分裂出來的節點至少有 個鍵,所以這就是可以要求每個非根節點維持的下限;要求更多,分裂後的節點就會違反規定。
解題方法
Consider a directed graph consisting of four vertices labeled A, B, C, and D. The graph has the following edges and their respective weights: an edge from A to B with weight , an edge from B to D with weight , an edge from A to C with weight , and an edge from C to B with weight .
Given the pseudo-code for the Bellman-Ford algorithm:
function BellmanFord(G, s):
// Initialization
for each vertex v in V:
dist[v] := (1)
prev[v] := undefined
dist[s] := (2)
// Main Loop
for i from 1 to |V|-1:
for each edge (u, v) in E:
if dist[u] + weight(u, v) (3) dist[v]:
dist[v] := dist[u] + weight(u, v)
prev[v] := u
// Check for negative weight cycles
for each edge (u, v) in E:
if dist[u] + weight(u, v) (4) dist[v]:
return "Graph contains a negative weight cycle"
return dist, prev
第 第二部分 2-(A) 題4 分
What values should be filled in (1), (2), (3), and (4) in the pseudo-code?
登入後即可作答並保存紀錄。
核心觀念
Bellman–Ford 用來計算單一起點到各頂點的最短距離,允許邊權為負。演算法先將所有頂點的距離設為無限大,再將起點距離設為 ;接著反覆鬆弛每條邊。
對邊 ,若從起點到 的已知距離加上邊權,仍小於目前到 的距離,就更新 :
經過 輪鬆弛後,若某條邊仍可讓距離變小,表示存在可由起點到達的負權重環。
解題方法
依序填入初始化值與比較運算子:
- (1) 尚未找到路徑時,距離設為無限大,記作 。
- (2) 起點到自己的距離為 。
- (3) 鬆弛條件是新路徑距離小於目前距離,因此填入
<。 - (4) 負權重環檢查使用相同條件:若仍能使距離變小,就回報存在負權重環,因此也填入
<。
第 第二部分 2-(B) 題3 分
Let the source vertex be A. After the first iteration of the main loop, what is the updated value of dist[C]?
dist[C] = ____
登入後即可作答並保存紀錄。
核心觀念
Bellman–Ford 演算法以「鬆弛」更新從來源點到各頂點的目前最短距離。初始化時,來源點距離為 ,其餘頂點距離為無限大。若邊 能提供更短路徑,就更新:
解題方法
來源頂點是 ,因此初始化後 ,而 。
第 第二部分 2-(C) 題3 分
Let the source vertex be A. In the second iteration of the main loop, after processing the edge from C to B, what will be the new value of dist[B]?
dist[B] = ____
登入後即可作答並保存紀錄。
核心觀念
Bellman–Ford 演算法透過「鬆弛」逐步更新源點到各頂點的目前最短距離。對邊 ,若
就將 更新為 。本題的邊依題目列出的順序處理,且每輪更新會立即供同一輪後續邊使用。
解題方法
以 為源點,初始化為:
第一輪依序處理各邊:
| 處理的邊 | 更新結果 |
|---|---|
| ,權重 | |
| ,權重 |
第 第二部分 2-(D) 題3 分
Let the source vertex be A. What will be the final value of dist[D] after the algorithm completes its execution?
dist[D] = ____
登入後即可作答並保存紀錄。
核心觀念
Bellman–Ford 透過反覆鬆弛每條邊,逐步求出來源點到各頂點的最短距離。鬆弛邊 時,若
就將 更新為 。初始化時,來源點距離為 ,其他頂點距離為 。頂點數為 時,最多執行 輪鬆弛。
解題方法
本題頂點數為 ,因此主迴圈會執行 輪。依題目列出的邊順序鬆弛,初始距離為:
第一輪依序處理 、、、:
第 第二部分第 3 題 題14 分
Recurrence formula is what it takes to solve a problem with the dynamic programming technique. In this problem we examine the recurrence formulas for two different sequence problems.
Consider the longest increasing subsequence (LIS) problem. Let be the input sequence of numbers. For any , let denote the maximum length of any increasing subsequence of that ends at position . It is well-known that can be expressed as the following recurrence formula:
where in the formula we define for convenience. The answer to the input sequence is then .
Next, consider the “Longest Palindrome Bimodal Subsequence Problem,” which is defined as follows. Let be a sequence of numbers. A subsequence of is called a bimodal subsequence with pivot , , if is an increasing sequence before position and a decreasing sequence after position . That is, holds for all , and holds for all .
Furthermore, we say that a bimodal sequence with pivot is a palindrome bimodal sequence if .
For any , let denote the maximum length of any palindrome bimodal subsequence of that pivots at position . Write down a valid recurrence formula for in the format given above for for the LIS problem. You may define other auxiliary functions necessary to compose the recurrence formula for . However, make sure you provide a precise and succinct definition.
登入後即可作答並保存紀錄。
核心觀念
固定輸入序列中的位置 作為轉折點,合法子序列必須由兩部分組成:
- 左半部連同樞紐 ,是以 結尾的嚴格遞增子序列。
- 右半部連同樞紐 ,是以 開始的嚴格遞減子序列。
題目所稱的「回文雙峰」要求樞紐位於子序列正中央,因此左右兩側選取的元素數必須相同。這裡的「回文」指樞紐居中,不要求左右元素值相同。
解題方法
令 為以 結尾的最長嚴格遞增子序列長度,其遞迴式就是題目給的 LIS 公式:
其中 。
再令 為以 開始的最長嚴格遞減子序列長度。由於遞減子序列的下一個元素必須出現在 之後且比 小,因此:
第 第二部分第 4 題 題10 分
Consider a fixed positive integer and a sequence of positive real numbers , where and for all , such that
(A) [5%] Prove that such an exists.
(B) [5%] Design an efficient procedure to find the real number with error within . (Note: If you do not answer part (A) correctly, then you will not earn any point for this problem!)
登入後即可作答並保存紀錄。
核心觀念
令
因為 且 ,每一項都是連續函數,且隨 嚴格遞減。因此 連續且嚴格遞減。並且
所以 必定恰好一次通過 :由介值定理,存在實數 使 ;由嚴格遞減性,這個 唯一。
解題方法
數值求根使用二分搜尋法。先找出一個區間 ,使得
因為 嚴格遞減,根必在此區間內。每次取中點 :
- 若 ,根在右側,令 。
- 若 ,根在左側或正好等於 ,令 。
持續縮小區間,直到區間寬度不大於 ,回傳中點即可保證絕對誤差不超過 。
為避免計算 時發生數值溢位,改計算
方程 等價於 ,且 同樣連續、嚴格遞減。穩定計算 時,令 ,則