114 年 國立成功大學資訊工程系碩士班《程式設計》
第 1 題3 分
Consider the following two functions written in C language:
void enqueue(int queue[], int *front, int *rear, int size, int value) {
if ((*rear + 1) % size == *front) {
printf("Queue is full\n");
return;
}
if (*front == -1) *front = 0;
*rear = (*rear + 1) % size;
queue[*rear] = value;
}
int dequeue(int queue[], int *front, int *rear, int size) {
if (*front == -1) {
printf("Queue is empty\n");
return -1;
}
int value = queue[*front];
if (*front == *rear) {
*front = *rear = -1;
} else {
*front = (*front + 1) % size;
}
return value;
}
Assume the queue has size and is initially empty, with and . What will be the content of the array after executing the following sequence of operations?
enqueue(Q, &front, &rear, size, 10);
enqueue(Q, &front, &rear, size, 20);
dequeue(Q, &front, &rear, size);
enqueue(Q, &front, &rear, size, 30);
enqueue(Q, &front, &rear, size, 40);
enqueue(Q, &front, &rear, size, 50);
dequeue(Q, &front, &rear, size);
enqueue(Q, &front, &rear, size, 60);
enqueue(Q, &front, &rear, size, 80);
enqueue(Q, &front, &rear, size, 90);
dequeue(Q, &front, &rear, size);
dequeue(Q, &front, &rear, size);
enqueue(Q, &front, &rear, size, 100);
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
這題考固定大小的循環佇列。front 指向目前隊首,rear 指向目前隊尾;空佇列以 front = rear = -1 表示。索引往後移動時使用
當 (*rear + 1) % size == *front 時,佇列視為已滿,因此這種實作最多存放 個元素。dequeue 只移動 front,不會清除陣列中原本的值。
解題方法
依序追蹤每次操作後的 front、rear 和陣列內容。陣列欄位以下按索引 到 列出;出隊只改變指標,不會清除已出隊的值。
| 操作 | front | rear | 陣列 |
|---|---|---|---|
| 初始 | -1 | -1 | [—, —, —, —, —] |
| 入列 10 | 0 | 0 | [10, —, —, —, —] |
| 入列 20 | 0 | 1 | [10, 20, —, —, —] |
| 出列 | 1 | 1 | [10, 20, —, —, —] |
| 入列 30 | 1 | 2 | [10, 20, 30, —, —] |
| 入列 40 | 1 | 3 | [10, 20, 30, 40, —] |
| 入列 50 | 1 | 4 | [10, 20, 30, 40, 50] |
| 出列 | 2 | 4 | [10, 20, 30, 40, 50] |
| 入列 60 | 2 | 0 | [60, 20, 30, 40, 50] |
| 入列 80 | 2 | 1 | [60, 80, 30, 40, 50] |
| 入列 90 | 2 | 1 | [60, 80, 30, 40, 50] |
| 出列 | 3 | 1 | [60, 80, 30, 40, 50] |
| 出列 | 4 | 1 | [60, 80, 30, 40, 50] |
| 入列 100 | 4 | 2 | [60, 80, 100, 40, 50] |
第 2 題3 分
Given a node structure as shown below:
typedef struct Node {
int data;
struct Node* next;
} Node;
What will the following function do when used on a singly linked list?
void unknownFunction(Node* node) {
if (node == NULL || node->next == NULL) {
return;
}
node->data = node->next->data;
Node* temp = node->next;
node->next = node->next->next;
free(temp);
}
(A) The function will delete the specified node, including the last node in the list.
(B) The function will delete the specified node only if it is a middle node or any node except the last one.
(C) The function will produce a segmentation fault when applied to the last node in the list because it attempts to access node->next->data.
(D) The function will remove the node but corrupt the list structure if the deleted node has duplicate values in the list.
(E) None of the above is correct.
登入後即可作答並保存紀錄。
核心觀念
單向鏈結串列的節點只能直接存取自己的 next,無法從指定節點往回找到前一個節點。刪除非尾端節點時,可以把下一個節點的資料複製到目前節點,再讓目前節點跳過下一個節點,最後釋放被跳過的節點。
解題方法
函式先檢查 node == NULL 或 node->next == NULL。任一條件成立就直接返回,因此空指標與尾端節點都不會被刪除,也不會在尾端節點上存取 node->next->data。
若 node 不是尾端節點,執行順序如下:
node->data = node->next->data;:把下一個節點的資料複製到指定節點。temp = node->next;:暫存下一個節點的指標。node->next = node->next->next;:讓指定節點直接連到原本的下下一個節點。free(temp);:釋放已從串列中移除的下一個節點。
例如,原串列為 A → B → C,指定 A 後,函式會把 B 的資料複製到 A,再釋放 B,結果為 A(原 B 的資料)→ C。
第 3 題3 分
The following C code is provided. Analyze the functionality of the code and answer the question.
#include <stdio.h>
int operation1(int parent[], int x) {
if (parent[x] != -1) {
int rootX = operation1(parent, parent[x]);
parent[x] = rootX;
return rootX;
}
return x;
}
void operation2(int parent[], int x, int y) {
int rootX = operation1(parent, x);
int rootY = operation1(parent, y);
if (rootX != rootY) {
parent[rootY] = rootX;
}
}
int main() {
int parent[6] = {-1, -1, -1, -1, -1, -1};
parent[2] = 1;
parent[3] = 2;
parent[4] = 3;
operation1(parent, 4);
operation2(parent, 1, 3);
operation2(parent, 5, 4);
for (int i = 0; i <= 5; i++) {
printf("%d ", parent[i]);
}
return 0;
}
Which of the following statements is correct?
(A) In the function main(), operation1(parent, 4) updates all nodes in the chain starting from 4, and operation2(parent, 1, 3) merges two different groups.
(B) The output of the program is “-1 -1 1 1 1 1”.
(C) The output of the program is “-1 5 1 2 3 -1”.
(D) The function operation1() compresses paths, and the function operation2() merges groups if they have different representatives.
(E) The function operation1() creates a loop, and the function operation2() merges all elements into one group.
登入後即可作答並保存紀錄。
核心觀念
這題考查**不相交集合(Disjoint Set Union, DSU)**的兩個基本操作:
operation1(parent, x):沿著parent指標往上找代表元;回傳根節點時,將沿途節點的父節點改成根節點,這稱為路徑壓縮。operation2(parent, x, y):先找出 與 所屬集合的代表元;只有代表元不同時,才合併兩個集合。
陣列中 parent[i] == -1 代表 是根節點。
解題方法
先依照程式執行順序追蹤 parent。
初始化及指定父節點後:
parent = [-1, -1, 1, 2, 3, -1]
此時從節點 開始的鏈為:
4 → 3 → 2 → 1
呼叫 operation1(parent, 4) 時,根節點是 。遞迴返回時,會把沿途節點的父節點設為 :
parent[2] = 1
parent[3] = 1
parent[4] = 1
因此:
parent = [-1, -1, 1, 1, 1, -1]
接著執行 operation2(parent, 1, 3)。節點 的代表元是 ;節點 經路徑壓縮後,代表元也是 。兩者已在同一集合,不需合併,陣列不變。
最後執行 operation2(parent, 5, 4)。節點 的代表元是 ,節點 的代表元是 ,兩者不同,因此執行 parent[5] = 1。
最後陣列為:
第 4 題3 分
Consider the following two functions written in C language for insertion into and deletion from a hash table:
void insert(int table[], int size, int key) {
int index;
for (int i = 0; i < size; i++) {
index = (key % size + i * i) % size;
if (table[index] == -1 || table[index] == -2) {
table[index] = key;
return;
}
}
}
void delete(int table[], int size, int key) {
int index;
for (int i = 0; i < size; i++) {
index = (key % size + i * i) % size;
if (table[index] == key) {
table[index] = -2;
return;
}
}
}
Given that the keys are all non-negative integers and the keys to be deleted are always in the hash table, which of the following implementations correctly searches for a specific key in the hash table, considering collision handling and the presence of deleted slots?
(A)
int search(int table[], int size, int key) {
int index;
for (int i = 0; i < size; i++) {
index = (key % size + i) % size;
if (table[index] == -1) {
return -1;
}
if (table[index] == key) {
return index;
}
}
return -1;
}
(B)
int search(int table[], int size, int key) {
int index;
for (int i = 0; i < size; i++) {
index = (key % size + i * i) % size;
if (table[index] == key) {
return index;
}
if (table[index] == -1 || table[index] == -2) {
return -1;
}
}
return -1;
}
(C)
int search(int table[], int size, int key) {
int index;
for (int i = 0; i < size; i++) {
index = (key % size + i * i) % size;
if (table[index] == -1) {
return -1;
}
if (table[index] == key) {
return index;
}
if (table[index] == -2) {
continue;
}
}
return -1;
}
(D)
int search(int table[], int size, int key) {
int index;
for (int i = 0; i < size; i++) {
index = (key % size + i * i) % size;
if (table[index] == -2) {
return -1;
}
if (table[index] == key) {
return index;
}
}
return -1;
}
(E) None of the above is correct.
登入後即可作答並保存紀錄。
核心觀念
本題考查開放定址法中的二次探測(quadratic probing),以及刪除資料時使用的墓碑標記(tombstone)。
插入與刪除都依序探查下列位置:
表格中的 -1 表示從未使用的空格;-2 表示曾經存放資料、後來已刪除的格子。查詢遇到 -1 時,可以確定該 key 尚未插入;遇到 -2 時,則必須繼續探查,因為 key 可能位於它後面的探查位置。
解題方法
搜尋必須沿用插入與刪除的相同探查公式,並遵守以下判斷順序:
- 探查位置是
-1:停止搜尋,回傳-1。 - 探查位置等於
key:找到資料,回傳該索引。 - 探查位置是
-2:繼續搜尋。 - 其他情況:該位置有其他 key,也繼續搜尋。
正確的搜尋程式如下:
第 5 題4 分
Please successively insert the data pairs containing the following keys into an empty height-based min leftist tree: . After each insertion, the tree should still be a min leftist tree. Which of the following descriptions are correct for the resultant tree?
Note: In a tree, each step from top to bottom is called the level of a tree. The level count starts with and increments by at each level or step.
(A) The node containing key has left child with key and right child with key .
(B) The node containing key and the node containing key are siblings.
(C) After repeatedly performing the delete-min operation twice on the leftist tree, the node containing is in the left subtree of the root.
(D) The difference of height between the left and right subtrees of the root is .
(E) The third level of the tree has four nodes.
登入後即可作答並保存紀錄。
核心觀念
高度型最小左偏樹(height-biased min leftist tree,HBLT):
- 最小堆序:每個節點的鍵值不大於其子節點。
- 左偏性質:每個節點左子樹的 值不小於右子樹的 值,。
- 值(shortest):節點到其子樹中最近的外部節點(空指標)的路徑長度。,。注意不是樹高。
- 插入=與單節點樹合併:根較小者當根,把它的右子樹與另一棵遞迴合併;回到每一層時,若 就交換左右子樹。
解題方法
以 表示根為 、左子樹 、右子樹 (「-」為空)。
| 插入 | 結果 | 說明 |
|---|---|---|
| 10 | ||
| 20 | 20 先接在右邊,,交換到左邊 | |
| 30 | 左右 都是 1,不交換 | |
| 40 | 40 併入 30 的右邊後換到左邊 | |
| 50 | 50 成為 30 的右子,,在 10 交換左右 | |
| 60 | ||
| 70 | ||
| 5 | 5 較小當根,原樹換到左邊 | |
| 4 | 同理 | |
| 15 | ,不交換 | |
| 16 | 16 併入 15 後換到左邊 |
第 6 題4 分
Please successively insert the data pairs containing the following keys into an empty min binomial heap (B-heap): . For each insertion of a single key, min-tree joining (pairwise combine) is performed. Which of the following descriptions are correct for the resultant B-heap?
Note: In a tree, each step from top to bottom is called the level of a tree. The level count starts with and increments by at each level or step.
(A) In the resultant B-heap, the maximum degree of binomial tree is .
(B) The number of singly linked circular lists in the B-heap to maintain siblings and roots is .
(C) After performing the delete-min operation on the B-heap once, the minimum degree of binomial tree is .
(D) After performing the delete-min operation on the B-heap twice, the root list of min trees contains nodes with keys and .
(E) After performing the delete-min operation on the B-heap three times, the node containing key is at the second level of a min tree.
登入後即可作答並保存紀錄。
核心觀念
二項堆由多棵二項樹組成,並維持兩項性質:
- 最小堆序:每個節點的鍵值不大於其子節點,因此最小值必在根節點中。
- 每種度數至多一棵樹:兩棵同度數的二項樹合併時,較小的根成為新根,另一棵樹接到它的子節點中,樹的度數加一。
二項樹 有 個節點,根的度數為 。堆中的樹度數可視為節點總數的二進位表示。例如 ,因此 11 個節點會形成 各一棵。
刪除最小值時,移除最小根,將該根的子樹拆成獨立二項樹,再與原根串列合併並整理同度數的樹。
解題方法
先依插入順序逐次合併同度數的樹。每次合併都由較小根保留為新根。
插入前 8 個鍵值 後,形成一棵 ,根為 5。其結構可整理為:
- 根 5 的子樹根為 10、50、70。
- 10 的子節點為 30、20;30 的子節點為 40。
- 50 的子節點為 60。
接著插入 4、15、16:
- 插入 4、15 後,4 與 15 合併成一棵根為 4 的 。
- 插入 16 後,形成一棵 。
所以最後的根串列包含:
刪除最小值時,依序追蹤根與合併結果:
| 操作 | 根串列(根鍵值與度數) |
|---|---|
| 插入完成 | |
| 刪除最小值 4 | |
| 再刪除最小值 5 | |
| 第三次刪除最小值 10 |
第一次刪除 4 時,4 的子節點 15 成為根;它與根 16 的 合併,形成以 15 為根的 。
第二次刪除 5 時,5 的三棵子樹 、、 放回根串列,再與原有的 兩兩合併,直到每種度數只剩一棵:
- 與 合併,15 較小當根,得 (子樹為 與 )。
- 又與 同度數,再合併,10 較小當根,得 (子樹為 、、)。
根串列只剩 與 。
第 7 題4 分
Given a tree containing nodes, which of the following statements are correct?
(A) If the tree is a maximum cost spanning tree, the number of edges is .
(B) If the tree is an AVL tree, the time complexity to delete an element is .
(C) If the tree is a compressed trie, the number of keys is .
(D) If the tree is a red-black tree with internal nodes, the rank of the root is at most .
(E) If the tree is a binary search tree, its height is .
登入後即可作答並保存紀錄。
核心觀念
本題考查生成樹、平衡搜尋樹、壓縮字典樹與紅黑樹的基本性質。判斷時要分清楚「樹的節點數」和「鍵值數」,也要注意一般二元搜尋樹不保證平衡。
解題方法
逐一比對每個選項中的敘述是否符合該資料結構的定義或定理:
- 生成樹一定連通且無環,因此有 條邊。
- AVL 樹具有高度平衡性,樹高為 ,因此沿樹高進行的刪除與調整也是 。
- 壓縮字典樹的節點數不等於鍵值數。
- 紅黑樹的根節點黑高度受內部節點數限制。
- 一般二元搜尋樹若插入順序不佳,可能退化成鏈狀。
選項分析
(A) 正確。 最大成本生成樹仍是生成樹。任何含 個節點的生成樹都有且只有 條邊;「最大成本」只影響選到哪些邊,不改變邊數。
(B) 正確。 AVL 樹的高度為 。刪除時先沿搜尋路徑找到元素,再沿路徑向上更新平衡資訊並做必要旋轉;每層處理為常數時間,總時間為 。
第 8 題4 分
Consider the following AVL tree:
🖼️【此處有附圖,請對照原卷】
Which of the following statements are correct?
(A) The deletion of requires rebalance operations.
(B) The insertion of does not require rebalance operations.
(C) After deleting , the nodes containing keys and become siblings in the new tree.
(D) After successively deleting and , the balance factor of the root in the new tree is .
(E) After successively inserting , , and , the balance factor of the root in the new tree is .
圖看不清楚?展開原卷第 8 頁核對
登入後即可作答並保存紀錄。
核心觀念
AVL 樹要求每個節點的平衡因子都在 、、 之間。本題採用
空子樹高度為 ,葉節點高度為 。插入或刪除後,若節點的平衡因子超出範圍,就依失衡方向進行單旋轉或雙旋轉。
解題方法
圖中的樹根為 ;左子樹以 為根,包含 ;右子樹以 為根,包含 。依序模擬各選項的插入或刪除,更新受影響節點的高度和平衡因子,再判斷是否需要旋轉。
選項分析
(A) 錯誤。 刪除 後, 成為葉節點。此時 的左右子樹高度分別為 、,所以 ;根 的左右子樹高度分別為 、,所以 。各節點仍符合 AVL 條件,不需要重新平衡。
(B) 正確。 插入 後, 成為 的左子節點。節點 的左右子樹高度都為 ,平衡因子為 ;節點 的平衡因子為 ,根 的平衡因子也為 。沒有節點失衡,因此不需旋轉。
第 9 題4 分
Considering the following two sequences, which are traversal results of a binary tree:
Inorder-traversal result: H D I N B E J A O K P F L C G M
Postorder-traversal result: H N I D J E B O P K L F M G C A
Please reconstruct the binary tree using the above two sequences. Which of the following descriptions are correct for the resultant binary tree?
Note: In a tree, each step from top to bottom is called the level of a tree. The level count starts with and increments by at each level or step.
(A) The balance factor of the root is .
(B) The height of the tree is .
(C) The total number of leaf nodes is .
(D) After performing a depth-first search starting at node B of the resultant tree, the shortest path from vertex B to vertex O in the spanning tree has edges.
(E) The last element in the result of level-order traversal of the tree is P.
登入後即可作答並保存紀錄。
核心觀念
中序走訪的順序是「左子樹、根、右子樹」,後序走訪的順序是「左子樹、右子樹、根」。因此,後序序列的最後一個節點是整棵樹的根;在中序序列找到根後,根左側與右側的節點分別屬於左、右子樹,再依序遞迴重建。
本題還會用到:
- 平衡因子:根節點左、右子樹高度的差;兩邊高度相同時,平衡因子為 。
- 樹高:本題以層數計算,根所在層為第 層。
- 葉節點:沒有子節點的節點。
- 深度優先搜尋(DFS)生成樹:原圖本身是樹時,DFS 生成樹保留原有邊,因此節點間的唯一路徑不變。
解題方法
後序序列最後一個節點為 ,所以根為 。在中序序列中, 左側為左子樹,右側為右子樹:
- 左子樹中序:;後序:。左子樹根為 。
- 的左子樹(中序 /後序 )根為 ; 的左子節點為 。 的右子樹中序 、後序 ,根為 , 是 的右子節點。
- 的右子樹根為 , 的右子節點為 。
- 右子樹中序:;後序:。右子樹根為 。
- 的左子樹根為 ,其左子樹根為 ; 的左右子節點分別為 、, 的右子節點為 。
- 的右子樹根為 , 的右子節點為 。
重建結果如下:
第 10 題4 分
Considering the following red-black tree, where the node colored as white indicates a red node and the node colored as black indicates a black node:
🖼️【此處有附圖,請對照原卷】
Which of the following statements are correct?
(A) After inserting into the tree, the number of black nodes becomes .
(B) After successively inserting and into the tree, the number of red nodes becomes .
(C) The deletion of from the tree may reduce the height of the tree.
(D) After successively deleting and from the tree, the rank of the root is .
(E) The deletion of can be done via moving to the root.
圖看不清楚?展開原卷第 9 頁核對
登入後即可作答並保存紀錄。
核心觀念
紅黑樹的調整必須同時維持二元搜尋樹的大小順序,以及下列性質:
- 根與空節點 為黑色。
- 紅色節點的子節點皆為黑色,不能出現相鄰的紅色節點。
- 每個節點到其下方各個 的路徑,黑色節點數必須相同。
插入時,新節點先設為紅色,再依父節點、叔叔節點的顏色進行變色與旋轉。刪除時,若實際移除的是黑色節點,必須修復該位置的黑色缺額。
本題計算紅、黑節點總數時,只計有鍵值的節點,不計 。根的 rank 依黑高度計算:往下到 的路徑不計根本身,但計入末端的黑色 ;因根為黑色,也等於該路徑上包含根、排除 的黑色節點數。
解題方法
原圖的黑色節點為 ,共 個;白色代表紅色,節點為 ,共 個。根為 ,完整父子關係如下,未列出的子節點皆為 :
| 節點 | 左子節點 | 右子節點 |
|---|---|---|
| (黑) | (黑) | (黑) |
| (黑) | (黑) | (黑) |
| (黑) | (紅) | (紅) |
| (黑) | (紅) | — |
| (黑) | (紅) | (黑) |
| (紅) | (黑) | (黑) |
| (黑) | — | (紅) |
| (黑) | (紅) | — |
原樹最長路徑為 ,共有 層。各選項均從原樹獨立開始操作,只有同一選項內的連續操作需要承接。
選項分析
(A)正確:插入 後,黑色節點共有 個
依搜尋順序, 成為 的左子節點,初始為紅色。此時:
- 父節點 為紅色。
- 叔叔節點 為紅色。
- 祖父節點 為黑色。
套用「父、叔皆紅」的插入修復:將 變黑, 變紅。由於 的父節點 為黑色,修復結束,不需旋轉。
黑色節點數因此為
新增的 仍為紅色,故(A)正確。
(B)正確:依序插入 後,紅色節點共有 個
先插入 ,它成為 的右子節點。父節點 為黑色,不需調整,紅色節點數由 增為 。
接著插入 ,搜尋路徑為
因此 成為 的左子節點。此時父節點 與叔叔節點 均為紅色,故:
- 由紅變黑。
- 祖父節點 由黑變紅。
的父節點 為黑色,修復結束。最終紅色節點數為
其紅色節點為 ,故(B)正確。
(C)正確:刪除 可以使樹高降低
第 11 題4 分
Please reconstruct the undirected graph based on the following adjacency matrix:
🖼️【此處有附圖,請對照原卷】
Which of the following statements related to the resultant graph are correct?
(A) The breadth-first search starting at vertex produces a spanning tree with vertices.
(B) The degree of vertex is .
(C) The graph has one articulation point.
(D) The graph has one connected component.
(E) The graph has two cycles.
圖看不清楚?展開原卷第 9、10 頁核對
登入後即可作答並保存紀錄。
核心觀念
本題考圖形的鄰接矩陣、廣度優先搜尋(BFS)、頂點度數、關節點、連通分量與環數。
無向圖的鄰接矩陣中,矩陣位置 為 表示頂點 與頂點 之間有邊。頂點的度數是與它相接的邊數;移除某頂點後若連通分量數增加,該頂點就是關節點。對有 個頂點、 條邊及 個連通分量的無向圖,環數為:
解題方法
由原卷矩陣讀得各列的相鄰頂點如下: 與 相鄰; 與 相鄰; 與 相鄰; 與 相鄰; 與 相鄰; 與 相鄰; 與 相鄰; 與 相鄰。
因此邊集合為:
圖形分成兩個部分:一個是由 組成的四邊環;另一個是由 組成的三角形,並且頂點 連接葉節點 。
第 12 題5 分
Considering the following B-tree of order :
🖼️【此處有附圖,請對照原卷】
Successively insert , , , and , one at a time, into the above B-tree and then delete , , , , and , one at a time. Please show the new tree after completing all the insertion and deletion operations.
圖看不清楚?展開原卷第 10 頁核對
登入後即可作答並保存紀錄。
核心觀念
階數為 的 B 樹,每個節點最多有 個子節點、 個鍵值;非根節點至少有 個鍵值,且所有葉節點位於同一層。
本題採用由上而下的操作方式:
- 插入:下降前先分裂已滿的節點,將中間鍵提升至父節點。
- 刪除:下降前,若目標子節點只有 個鍵,先向兄弟節點借鍵;兄弟也只有 個鍵時,將兩者與父節點的分隔鍵合併。合併完成後,仍須移除指定的鍵。
解題方法
原圖的根為 ,三個子節點由左至右為 、、。下方空白小方框表示空子樹,這三個含鍵的子節點皆為葉節點。
一、依序插入
1. 插入
右側葉節點 已滿,先分裂並將 提升至根,再把 插入右側葉節點:
[20,35,45]
/ | | \
[10,15] [25,30] [40] [50,62]
2. 插入
根 已滿,先分裂,將 提升為新根。接著把 插入最左側葉節點:
[35]
/ \
[20] [45]
/ \ / \
[5,10,15] [25,30] [40] [50,62]
3. 插入
將最右側葉節點 改為 ,其他節點不變。
4. 插入
最左側葉節點 已滿,分裂後把 提升至父節點,使父節點變成 。再將 插入分裂後的 :
[35]
/ \
[10,20] [45]
/ | \ / \
[5] [12,15] [25,30] [40] [50,62,85]
二、依序刪除
1. 刪除
目標葉節點 只有一個鍵,右兄弟 有兩個鍵,可以借鍵:
- 父節點的 下移至目標葉節點。
- 右兄弟的 上移,取代父節點中的 。
- 目標葉節點成為 ,刪除 後留下 。
因此左側內部節點變為 ,其子節點依序為 、、。
2. 刪除
第 13 題5 分
Considering the following graph :
🖼️【此處有附圖,請對照原卷】
(a) [1%] Write out the cost of the maximum cost spanning tree for graph .
(b) [2%] Write out the last edge selected into the maximum cost spanning tree for graph when using Kruskal’s method.
(c) [2%] Write out the fifth edge selected into the maximum cost spanning tree for graph when using Prim’s method starting with vertex .
圖看不清楚?展開原卷第 10 頁核對
登入後即可作答並保存紀錄。
核心觀念
最小生成樹會讓總權重最小;本題要求最大成本生成樹,因此要在連通所有頂點且不形成環的條件下,讓選入邊的權重總和最大。生成樹若有 個頂點,就恰有 條邊。
解題方法
圖中有頂點 ,共 個;邊及權重為 、、、、、、、、、、、。以下分別依最大版 Kruskal 與 Prim 方法選邊。
(a) 最大成本生成樹
最大版 Kruskal 將邊依權重由大到小檢查,若加入該邊不會形成環就選入:
| 權重 | 邊 | 處理 |
|---|---|---|
| 13 | 選入 | |
| 12 | 選入 | |
| 11 | 選入 | |
| 9 | 選入 | |
| 8 | 選入 | |
| 7 | 略過,會形成環 | |
| 6 | 選入 | |
| 5 | 略過,會形成環 | |
| 4 | 選入 |
共選入 條邊,已連通全部 個頂點。因此最大成本為
第 14 題10 分
[10%] Given a set of size , where , we use the following algorithm to perform clustering and labeling:
F(S)
- If , then return;
- Apply a clustering algorithm with a time complexity of to divide the set into three clusters, , , and , and label them, where , , and .
- Call F().
- Call F().
Let represent the time required to use this algorithm to cluster and label a set of size . Provide an asymptotic tight bound () for , assuming that is a constant for sufficiently small .
登入後即可作答並保存紀錄。
核心觀念
這題考遞迴式的漸近解。每次呼叫只對 、 遞迴,兩個子集合的大小都是 ; 不再遞迴處理。因此遞迴關係為
其中 是當次分群與標記的成本。
解題方法
利用題目給定的集合大小 。平方根會讓指數 減少 :
令 。每次遞迴的分群成本為
所以遞迴式改寫為
令 ,兩側除以 :
遞迴深度為 ,每層讓 增加 ,因此 ,進而得到
第 15 題10 分
[10%] We offer courses, where each course has a start time , a finish time , and a weight . Two courses are considered compatible if their time intervals do not overlap. For simplicity, we can assume that the courses are sorted in non-decreasing order of their finish times, denoted as . Additionally, let for a course represent the largest index such that courses and are disjoint. We define if no course is disjoint from .
Our goal is to find a set of mutually compatible courses such that the total weight of the courses in is maximized. We have designed a recursive algorithm for this purpose, but have omitted an essential part. Please assist us in completing the missing portion. Hint: This algorithm returns the value of the optimal solution to the problem consisting of courses .
F(i)
If , return .
Return ____ (10%)
登入後即可作答並保存紀錄。
核心觀念
本題考的是加權區間排程(Weighted Interval Scheduling)的動態規劃。每門課有開始時間 、結束時間 與權重 ,目標是在課程彼此不重疊的條件下,讓總權重最大。
課程已依結束時間排序。 是課程 之前,最後一門與課程 相容的課程索引;若沒有相容課程,則 。
解題方法
考慮最佳解是否選入第 門課:
- 不選第 門課:最佳總權重為 。
- 選第 門課:因為不能選與它衝突的課程,能搭配的前段課程最多到 ,總權重為 。
兩種情況取較大值,因此缺少的部分為:
配合題目給的初始條件 ,即可由小到大計算各前綴的最佳解。
第 16 題10 分
[10%] We aim to select courses from a pool of courses. Since this is still in the planning phase, each course has no specified start or finish times. Each course is characterized by its duration and weight . The total available classroom time is . We want to identify a subset of courses such that the total duration of courses in does not exceed and the total weight of is maximized.
To achieve this, we have developed a dynamic programming algorithm that uses an table to solve the problem. Here, represents the maximum total weight achievable when considering only the subset of courses and a classroom available for units of time. Write the recursive formula to compute .
登入後即可作答並保存紀錄。
核心觀念
這題是 0/1 背包問題。每門課只有「選」或「不選」兩種決定;選修課程會占用其時間 ,並取得權重 ,目標是在總時間不超過 的前提下,讓總權重最大。
表示只考慮前 門課、可用時間為 時,能取得的最大總權重。
解題方法
考慮第 門課時,只有兩種情況:
- 不選第 門課:最大權重為 。
- 選第 門課:只有在 時可選;選了之後,剩餘時間為 ,最大權重為 。
因此,遞迴式為:
對 、:
第 17 題10 分
[10%] Assuming that the available time for the classroom is unlimited, we instead focus on course completion time. We aim to schedule courses, where each course is characterized by its duration and weight . In this schedule, the finish time of each course is the total duration of all courses scheduled before it, plus its duration. A schedule can be defined as a function , representing the order of course in the schedule. That is, if , course is scheduled before course . Therefore, the finish time of course is denoted as .
This scheduling problem aims to minimize the total weighted finish time . Suppose we have courses with corresponding durations and weights . What is the value of the optimal schedule, that is, the minimum total weighted finish time?
登入後即可作答並保存紀錄。
核心觀念
這是單機排程問題:每門課程依序進行,課程 的完成時間為 ,目標是讓總加權完成時間
最小。適用的貪婪法則是 Smith 法則:依 由小到大排序;等價地,依 由大到小排序。
解題方法
考慮兩門相鄰課程 ,並假設它們開始前已經過了時間 。
若排成 在前,兩門課對目標值的貢獻為
若排成 在前,貢獻為
兩者相減後,前者減後者為 。因此, 排在 前面較佳的條件是
依此法則排序後,課程順序為:
例如課程 15 的比值為 ,課程 2 為 ,所以課程 15 排在課程 2 前面。依同一規則排列所有課程,再逐一累加完成時間:
| 排序位置 | 課程 | 完成時間 | |||
|---|---|---|---|---|---|
| 1 | 15 | 15 | 15 | 15 | 225 |
| 2 | 2 | 6 | 4 | 21 | 84 |
| 3 | 11 | 19 | 11 | 40 | 440 |
| 4 | 3 | 9 | 4 | 49 | 196 |
| 5 | 1 | 6 | 2 | 55 | 110 |
| 6 | 8 | 38 | 10 | 93 | 930 |
| 7 | 5 | 34 | 7 | 127 | 889 |
| 8 | 6 | 44 | 7 | 171 | 1,197 |
第 18 題10 分
[10%] The Floyd-Warshall algorithm can solve the all-pairs shortest-paths problem on a directed graph . Let be the weight of a shortest path from vertex to vertex for which all intermediate vertices are in the set , and let be an matrix. The Floyd-Warshall algorithm computes from using the following formula:
Please complete the above formula.
登入後即可作答並保存紀錄。
核心觀念
Floyd–Warshall 演算法逐步擴大「可作為路徑中間頂點」的集合。 表示從頂點 到頂點 ,且所有中間頂點都屬於 時的最短路徑權重。
解題方法
考慮最短路徑是否經過頂點 :
- 若不經過 ,路徑中間頂點都在 ,權重為 。
- 若經過 ,可將路徑拆成 到 、再由 到 兩段,權重為 。
取兩種情況中權重較小者,得到遞迴公式: