109 年 國立中山大學電機工程學系碩士班丙組《資料結構》
第 1 題
Question set I
Suppose we have the following function:
kkk(A,i,n) {
if (n > 1) {
Exchange the values of A[i] and A[i+n-1];
kkk(A,i+1,n-2);
}
}
Note that A is an integer array starting with index 0. Each integer occupies 4 bytes in the byte-addressable memory. Please answer the following questions.
- If the address of A[2] is , what is the address of A[5]?
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
此題考查「陣列元素位址計算」:
- 記憶體是以位元組定址,每個位元組都有獨立位址。
- 每個整數佔 bytes,因此相鄰陣列元素的位址相差 bytes。
- 陣列元素位址公式為
程式中的 Exchange 只交換元素內容,不會改變陣列元素所在的記憶體位址,因此不影響本題計算。
解題方法
已知:
比 後面 個元素,因此位移量為
所以:
選項分析
-
(A) :錯誤。
,相當於每個元素只佔 byte,忽略了整數佔 bytes。 -
(B) :錯誤。
此選項假設 與 位址相同,忽略了兩者相差 個陣列元素。
第 2 題
Question set I
Suppose we have the following function:
kkk(A,i,n) {
if (n > 1) {
Exchange the values of A[i] and A[i+n-1];
kkk(A,i+1,n-2);
}
}
Note that A is an integer array starting with index 0. Each integer occupies 4 bytes in the byte-addressable memory. Please answer the following questions.
- How many times kkk will be called in the function body of kkk(A,0,4)?
(A) 1
(B) 2
(C) 3
(D) 4
登入後即可作答並保存紀錄。
核心觀念
本題考查遞迴函式的呼叫次數。每次呼叫時:
- 若 ,先交換元素,再遞迴呼叫
kkk(A,i+1,n-2)。 - 若 ,不再遞迴,直接結束。
- 每次遞迴都使 減少 ,因此呼叫流程只有單一路徑。
解題方法
關鍵程式碼為:
if (n > 1) {
Exchange A[i] and A[i+n-1];
kkk(A, i+1, n-2);
}
從 kkk(A,0,4) 開始追蹤:
| 呼叫次序 | 函式呼叫 | 是否遞迴 |
|---|---|---|
| 第 1 次 | kkk(A,0,4) | ,呼叫下一層 |
| 第 2 次 | kkk(A,1,2) | ,呼叫下一層 |
| 第 3 次 | kkk(A,2,0) | ,結束 |
因此,kkk 的總呼叫次數為
也可寫成遞迴式:
所以
這裡的總呼叫次數包含最初的 kkk(A,0,4);其中由遞迴語句額外產生的呼叫共有 次。
選項分析
第 3 題
Question set I
Suppose we have the following function:
kkk(A,i,n) {
if (n > 1) {
Exchange the values of A[i] and A[i+n-1];
kkk(A,i+1,n-2);
}
}
Note that A is an integer array starting with index 0. Each integer occupies 4 bytes in the byte-addressable memory. Please answer the following questions.
- What is the time complexity of this function?
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
本題考查遞迴函式的時間複雜度。判斷重點有兩個:
- 每次函式呼叫所做的工作量是否為常數。
- 遞迴參數如何縮小。
每次呼叫只交換 與 ,包含固定次數的陣列存取與指定,因此單次工作為 。
雖然每次遞迴將 減少 ,但不是除以 。因此遞迴深度與 成正比。
解題方法
令 表示輸入規模為 時的執行時間。
當 時,不進行交換,也不再遞迴:
當 時,函式執行一次常數時間的交換,再呼叫規模為 的函式:
展開遞迴:
每次遞迴只減少 ,大約需要
次交換。因此:
所以其時間複雜度為 。
題目提供的記憶體資訊不會改變結果。若陣列起始位址為 ,則:
位址計算與交換資料都只需固定時間,仍屬於 。
選項分析
- (A) :正確。
遞迴規模每次減少固定常數 ,總共執行 次常數工作,因此時間複雜度為 ,也就是 。
第 4 題
Question set I
Suppose we have the following function:
kkk(A,i,n) {
if (n > 1) {
Exchange the values of A[i] and A[i+n-1];
kkk(A,i+1,n-2);
}
}
Note that A is an integer array starting with index 0. Each integer occupies 4 bytes in the byte-addressable memory. Please answer the following questions.
- Let A=[20,80,30,40,10,70,50]. After executing kkk(A,0,7), which of the following are true?
(A) A[0]=70
(B) A[2]=10
(C) A[4]=50
(D) A[6]=20
登入後即可作答並保存紀錄。
核心觀念
函式每次交換目前子陣列的首、尾元素:
接著將起點向右移一格,子陣列長度縮短兩格:
因此,這個函式會由外向內交換元素,實際效果是將陣列反轉。題目所給的「每個整數佔 4 bytes」與本題只判斷陣列內容無關。
解題方法
初始陣列為:
依照每次交換的索引逐步計算:
| 遞迴呼叫 | 交換位置 | 陣列結果 |
|---|---|---|
| 不執行交換 |
當 時,條件 不成立,遞迴停止。
所以最後陣列為:
選項分析
-
(A) :錯誤
最後 ,因為第一次交換是 與 互換。
-
(B) :正確
第三次交換將 與 互換,因此最後 。
-
(C) :錯誤
第 5 題
Question set I
Suppose we have the following function:
kkk(A,i,n) {
if (n > 1) {
Exchange the values of A[i] and A[i+n-1];
kkk(A,i+1,n-2);
}
}
Note that A is an integer array starting with index 0. Each integer occupies 4 bytes in the byte-addressable memory. Please answer the following questions.
- Let A=[15,25,35,45,55,65,75,85]. After executing kkk(A,0,8), which of the following are true?
(A) A[1]=75
(B) A[3]=55
(C) A[5]=35
(D) A[7]=65
登入後即可作答並保存紀錄。
核心觀念
函式每次執行時,會交換目前區間的左右端點:
接著將起點向右移一格、區間長度縮短兩格:
因此,函式會依序交換:
這正是將陣列反轉的操作。題目提到每個整數佔 4 bytes,但本題只操作陣列索引與元素值,無須計算記憶體位址。
解題方法
初始陣列為:
逐層追蹤遞迴:
| 呼叫 | 交換位置 | 交換後陣列 |
|---|---|---|
kkk(A,0,8) | ||
kkk(A,1,6) | ||
kkk(A,2,4) | ||
kkk(A,3,2) | ||
kkk(A,4,0) | 不交換 | 遞迴結束 |
最後得到:
選項分析
- (A) :正確。
第二次交換後,原本的 由 移至 。
第 6 題
Question set II
Suppose we have a stack for storing integers. Initially, the stack is empty. We perform 10 operations push(1), push(2), pop(), push(3), pop(), pop(), push(4), push(5), push(6), and pop(). Please answer the following questions.
- We use an array A of 10 elements to implement the stack. Which one is true for declaring the array in C?
(A) integer A:10;
(B) integer A[0,9];
(C) integer A[1,10];
(D) integer A[10];
登入後即可作答並保存紀錄。
觀念解析
在 C 語言中,宣告包含 個元素的陣列時,標準語法為:
資料型態 陣列名稱[大小];
針對宣告存放 10 個元素的整數陣列 :
- (A)
integer A:10;:此為結構體位元欄(bit-field)語法誤用,非陣列宣告。
第 7 題
Question set II
Suppose we have a stack for storing integers. Initially, the stack is empty. We perform 10 operations push(1), push(2), pop(), push(3), pop(), pop(), push(4), push(5), push(6), and pop(). Please answer the following questions.
- We use the array A as defined above to implement the stack. We want to store the first integer at index=0. Let the latest input be stored at index=top. What is the value of top for the empty stack?
(A) top=0
(B) top=1
(C) top=-1
(D) top=9
登入後即可作答並保存紀錄。
核心觀念
本題考查陣列實作堆疊時,top 的定義。
題目規定:
- 第一個整數存放於索引 。
- 堆疊目前最上方、也就是最後存入的元素,位於索引
top。 top指向的是「目前堆疊最上方元素的索引」,不是堆疊元素數量。
空堆疊沒有任何元素,因此不存在有效的索引可供 top 指向。一般以 表示空堆疊。
解題方法
採用「執行一次 push,觀察 top 如何變化」的方式:
- 初始堆疊為空,設定
- 執行
push(1)時,先將top加 :
再將 存入 。 - 因此,
top=-1正好表示尚未有任何元素;第一次 push 後才會變成 。
也可用堆疊大小 與 top 的關係表示:
當堆疊為空時,,所以
題目列出的 10 次操作不會改變「初始空堆疊」的設定;該設定只由索引從 開始以及 top 指向最上方元素決定。
選項分析
-
(A)
top=0:錯誤。
top=0表示最上方元素位於 ,代表堆疊至少已有一個元素,不是空堆疊。 -
(B)
top=1:錯誤。
top=1表示最上方元素位於 ,代表堆疊至少已有兩個元素,與空堆疊不符。
第 8 題
Question set II
Suppose we have a stack for storing integers. Initially, the stack is empty. We perform 10 operations push(1), push(2), pop(), push(3), pop(), pop(), push(4), push(5), push(6), and pop(). Please answer the following questions.
- We use the linked-based implementation for the stack. Let the latest input be stored at the location with address in first. What is the value of first for the empty stack?
(A) first=-1
(B) first=0
(C) first=1
(D) first=2
登入後即可作答並保存紀錄。
觀念解析
在鏈結串列(Linked List)的堆疊實作中,指標變數 first 充當堆疊頂端指標(Top Pointer),指向最新加入的節點位址:
- 鏈結實作(Link-based):當堆疊為空(Empty Stack)時,指標不指向任何記憶體節點,其位址設定為空指標
NULL(位址數值即為 ),故 。
第 9 題
Question set II
Suppose we have a stack for storing integers. Initially, the stack is empty. We perform 10 operations push(1), push(2), pop(), push(3), pop(), pop(), push(4), push(5), push(6), and pop(). Please answer the following questions.
- We use the array A as defined above to implement the stack. After executing the 10 operations in sequence, which of the following are true?
(A) top=1
(B) A[0]=4
(C) A[1]=5
(D) The stack has 3 integers.
登入後即可作答並保存紀錄。
核心觀念
堆疊(stack)採用後進先出(LIFO, Last In First Out)原則:
push(x):將 放到堆疊頂端。pop():移除並取出堆疊頂端元素。- 陣列採 0-based index,
top表示目前頂端元素在陣列中的索引。 - 空堆疊時 ;若堆疊有 個元素,則 。
解題方法
依序記錄每次操作後的堆疊內容,堆疊左側為底端、右側為頂端:
| 步驟 | 操作 | 操作結果 | 堆疊內容(底 頂) | top |
|---|---|---|---|---|
| 0 | 初始 | — | ||
| 1 | push(1) | 放入 | ||
| 2 | push(2) | 放入 | ||
| 3 | pop() | 取出 | ||
| 4 | push(3) | 放入 | ||
| 5 | pop() | 取出 | ||
| 6 | pop() | 取出 | ||
| 7 | push(4) | 放入 | ||
| 8 | push(5) | 放入 | ||
| 9 | push(6) | 放入 | ||
| 10 | pop() | 取出 |
因此,全部操作完成後:
堆疊目前共有 個整數。
選項分析
第 10 題
Question set II
Suppose we have a stack for storing integers. Initially, the stack is empty. We perform 10 operations push(1), push(2), pop(), push(3), pop(), pop(), push(4), push(5), push(6), and pop(). Please answer the following questions.
- We use the linked-based implementation as defined above for the stack. After executing the 10 operations, which of the following are true?
(A) first is not 0
(B) first contains the address where 5 is stored
(C) A[first]=4
(D) A -> first = 4
登入後即可作答並保存紀錄。
一、 堆疊操作動態追蹤
定義 first 為指向鏈結串列頂端(Top)節點之指標。依序執行 10 次堆疊操作:
push(1)Stack:push(2)Stack:pop()Stack:push(3)Stack:pop()Stack:pop()Stack:push(4)Stack:push(5)Stack:push(6)Stack:pop()Stack: (頂端元素為 )
第 11 題
Question set III
Suppose we have an array A containing 9 integers (with the first index being 0):
240, 119, 682, 341, 225, 556, 122, 661, 245
Please answer the following questions.
- We use Bubble Sort to sort the array in ascending order. What is A[8] after the first pass?
(A) 661
(B) 556
(C) 682
(D) 245
登入後即可作答並保存紀錄。
核心觀念
Bubble Sort(冒泡排序)進行升冪排序時,依序比較相鄰兩個元素:
- 若左邊元素大於右邊元素,便交換兩者。
- 若左邊元素小於或等於右邊元素,則不交換。
- 完成第一趟掃描後,原陣列中的最大值會一路向右交換,最後固定在最右端。
本題陣列共有 個元素,索引範圍為 到 。第一趟需比較:
解題方法
初始陣列為:
依序比較相鄰元素:
| 比較 | 動作 | 陣列結果 |
|---|---|---|
| 交換 | ||
| 不交換 | ||
| 交換 | ||
| 交換 | ||
| 交換 | ||
| 交換 | ||
| 交換 | ||
| 交換 |
第一趟完成後:
這是因為 是原陣列中的最大值,經由相鄰交換會持續向右移動,最後到達索引 。
選項分析
第 12 題
Question set III
Suppose we have an array A containing 9 integers (with the first index being 0):
240, 119, 682, 341, 225, 556, 122, 661, 245
Please answer the following questions.
- We use Bubble Sort to sort the array in ascending order. What is A[7] after the second pass?
(A) 122
(B) 661
(C) 556
(D) 225
登入後即可作答並保存紀錄。
核心觀念
Bubble Sort 反覆比較相鄰元素:
- 若左邊元素大於右邊元素,便交換兩者。
- 以升冪排序時,每一趟由左向右掃描,該趟最大的未排序元素會逐步「冒泡」到右端。
- 第 1 趟後,最大值會固定在 ;第 2 趟後,次大值會固定在 。
解題方法
原始陣列為:
第 1 趟
由左至右比較相鄰元素,最大值 會一路交換到最後:
因此第 1 趟完成後, 固定於 。
第 2 趟
此時只需處理前 8 個元素:
依序比較並交換:
-
,不交換。
-
,不交換。
-
,交換:
-
,不交換。
-
,交換:
-
,不交換。
-
,交換:
因此第二趟結束時:
第 13 題
Question set III
Suppose we have an array A containing 9 integers (with the first index being 0):
240, 119, 682, 341, 225, 556, 122, 661, 245
Please answer the following questions.
- We use Quick Sort to sort the array in ascending order. For each partition, let the rightmost element be the pivot. Which one stores 245 after the first partition?
(A) A[3]
(B) A[7]
(C) A[5]
(D) A[4]
登入後即可作答並保存紀錄。
核心觀念
Quick Sort 的 partition 會選定一個 pivot,重新排列資料,使其滿足:
Partition 完成後,pivot 已經位於「排序後的正確索引」,但左右兩側的元素尚未完全排序。
本題索引從 開始,右端元素為 pivot:
小於 的元素有:
共有 個,因此 pivot 左側會有 個元素。由於索引從 開始,pivot 的位置為:
所以 會存放在 。
解題方法
依照常見的 Lomuto partition 觀念,掃描 pivot 前面的元素:
- ,留在左側。
- ,留在左側。
- ,放入右側。
- ,放入右側。
- ,移至左側。
- ,留在右側。
- ,移至左側。
- ,留在右側。
完成左右分區後,可排列成:
最後將 pivot 放到左右分區的交界處:
因此:
選項分析
- (A) :錯誤。
Partition 後 位於 pivot 左側,存放小於 的元素,例如 ,不是
第 14 題
Question set III
Suppose we have an array A containing 9 integers (with the first index being 0):
240, 119, 682, 341, 225, 556, 122, 661, 245
Please answer the following questions.
- We use Quick Sort to sort the array in ascending order. Which of the following are false after the first partition?
(A) A[0]=240
(B) A[8]=682
(C) A[3]=341
(D) A[7]=661
登入後即可作答並保存紀錄。
核心觀念
題目單獨摘錄時未重述分割規則;依原題組前題設定,每次 partition 取目前子陣列的最右端元素為 pivot。原題組題目頁
採用標準的 Lomuto partition:
- 取最右端元素為 pivot。
- 將小於 pivot 的元素移到左側,大於 pivot 的元素留在右側。
- 最後將 pivot 放到其排序後的正確位置。
第一次分割時:
pivot 為
小於 的元素共有 個:
因此 pivot 最後必定位於 。
解題方法
由左至右掃描 到 :
- ,留在左側。
- ,留在左側。
- ,暫留右側。
- ,暫留右側。
- ,與目前右側第一個大元素 交換:
- ,暫留右側。
- ,與 交換:
- ,暫留右側。
最後將 pivot 與 交換:
第 15 題
Question set III
Suppose we have an array A containing 9 integers (with the first index being 0):
240, 119, 682, 341, 225, 556, 122, 661, 245
Please answer the following questions.
- We use Heap Sort to sort the array in ascending order. But first, we have to convert the array into a maxheap. Which of the following are true in the resulting maxheap?
(A) A[0]=682
(B) A[3]=341
(C) A[5]=240
(D) A[8]=245
登入後即可作答並保存紀錄。
觀念說明
建立 Max Heap 採用 Bottom-up 之 Build-Max-Heap 演算法。陣列索引自 開始,長度 ,最後一個非葉節點索引為 。依序對索引 執行 Max-Heapify 調整。
其中節點 的左子節點索引為 ,右子節點索引為 。
步驟推導
原始陣列:
-
():
子節點為 。最大值為 ,與 交換。
-
():
子節點為 。因 ,無須交換。 -
():
子節點為 。最大值為 ,與 交換:
向下檢查 (),子節點為 。最大值為 ,與 交換:
第 16 題
Question set IV
Let the height of a tree be the number of nodes along the longest path from the root node to the leaf nodes. Suppose we have 10 integers 35, 46, 30, 34, 99, 42, 75, 28, 70, 80. Please answer the following questions.
- We create binary trees for these integers. What is the minimum height of all the possible trees?
(A) 1
(B) 2
(C) 3
(D) 4
登入後即可作答並保存紀錄。
核心觀念
二元樹的高度定義為「從根節點到最深葉節點所經過的節點數」。因此:
- 高度 :只有根節點。
- 第 層最多有 個節點。
- 高度為 的二元樹,最多包含
個節點。
本題有 個整數,數值本身不影響樹的最小高度。
解題方法
要求最小高度 ,必須滿足:
因此:
逐層計算:
- 高度 最多有 個節點,不足 個。
- 高度 最多有 個節點,足以容納 個節點。
高度為 的樹確實可以放入這 個數。例如甚至可以排列成二元搜尋樹:
35
/ \
30 75
/ \ / \
28 34 46 80
/ \ \
42 70 99
最長路徑如 ,共有 個節點,因此高度為 。
選項分析
- (A) 1:錯誤。 高度 的二元樹只有根節點,最多容納 個整數。
- (B) 2:錯誤。 高度 最多容納 個節點。
- **(C) 3:錯誤。
第 17 題
Question set IV
Let the height of a tree be the number of nodes along the longest path from the root node to the leaf nodes. Suppose we have 10 integers 35, 46, 30, 34, 99, 42, 75, 28, 70, 80. Please answer the following questions.
- We create binary trees for these integers. What is the maximum height of all the possible trees?
(A) 10
(B) 9
(C) 8
(D) 7
登入後即可作答並保存紀錄。
核心觀念
題目考查二元樹高度的定義與極端樹形。
- 高度定義為:從根節點到葉節點的最長路徑上,所包含的節點數。
- 二元樹中,每個節點最多有兩個子節點,但允許只有一個子節點。
- 對含有 個節點的二元樹而言,最大高度為 ,發生在所有節點連成單鏈的情況。
解題方法
本題共有 個整數,因此建立的二元樹共有 個節點。
將所有節點安排成單側傾斜的樹形,例如:
此時根節點到最深葉節點的路徑包含全部 個節點,因此:
題目採用「路徑上的節點數」定義高度,所以不需扣除根節點,也不需將節點數減一。
選項分析
- (A) 10:正確。
可以將 個節點排成單鏈,使根到葉的最長路徑包含全部 個節點,因此最大高度為 。
第 18 題
Question set IV
Let the height of a tree be the number of nodes along the longest path from the root node to the leaf nodes. Suppose we have 10 integers 35, 46, 30, 34, 99, 42, 75, 28, 70, 80. Please answer the following questions.
- We create an AVL tree by considering the integers one by one, starting with the first one, 30. Which of the following is true?
(A) The height of the resulting tree is 5
(B) The root of the resulting tree is 75
(C) The node for 46 has one child
(D) The parent of 99 is the node for 75
登入後即可作答並保存紀錄。
觀念說明
AVL 樹為高度平衡二元搜尋樹(Height-Balanced BST),每個節點的平衡因子(Balance Factor, )必須維持在 。若插入節點後破壞平衡,需依不平衡型態(LL、RR、LR、RL)進行旋轉調整。
AVL 樹建構步驟
依序插入數值 35, 46, 30, 34, 99, 42, 75, 28, 70, 80:
- 插入 35, 46, 30, 34, 99, 42:皆符合平衡條件,無須旋轉。
- 插入 75:
- 節點 之右子節點為 ,於 之左側插入 ,導致節點 發生 RL 不平衡()。
- 對以 為根的子樹執行 RL 雙旋轉(先對 右旋,再對 左旋),由 升為該子樹之根節點。
- 插入 28, 70, 80:依序放入對應位置,整棵樹皆維持平衡。
最終形成的 AVL 樹結構如下:
第 19 題
Question set IV
Let the height of a tree be the number of nodes along the longest path from the root node to the leaf nodes. Suppose we have 10 integers 35, 46, 30, 34, 99, 42, 75, 28, 70, 80. Please answer the following questions.
- We create a binary search tree by considering the integers one by one, starting with the first one, 30. Which of the following are true?
(A) The node for 75 is a leaf node
(B) The node for 42 is an internal node
(C) The node for 99 has only one child
(D) The height of the resulting tree is 5
登入後即可作答並保存紀錄。
二元搜尋樹建構過程
依序將整數序列 插入二元搜尋樹(BST):
- 插入 :作為根節點(Root)。
- 插入 :,放置於 的右子節點。
- 插入 :,放置於 的左子節點。
- 插入 : 且 ,放置於 的右子節點。
- 插入 : 且 ,放置於 的右子節點。
- 插入 : 且 ,放置於 的左子節點。
- 插入 : 且 ,放置於 的左子節點。
- 插入 :,放置於 的左子節點。
- 插入 :,放置於 的左子節點。
- 插入 :,放置於 的右子節點。
建構完成之二元搜尋樹結構如下:
第 20 題
Question set IV
Let the height of a tree be the number of nodes along the longest path from the root node to the leaf nodes. Suppose we have 10 integers 35, 46, 30, 34, 99, 42, 75, 28, 70, 80. Please answer the following questions.
- Based on the binary search tree obtained in Question 19, we delete 99 from the tree. Which of the following are true?
(A) The parent of 75 is the node for 46
(B) The node for 30 has two children
(C) The node for 70 is an internal node
(D) The height of the resulting tree is 4
登入後即可作答並保存紀錄。
一、 建樹與刪除節點推導
1. 原始二元搜尋樹(BST)建立
將數列 依序插入空 BST 中:
- 根節點為 。
- 的左子樹:根為 (左子 、右子 )。
- 的右子樹:根為 (左子 、右子 ); 的左子為 (左子 、右子 )。
2. 刪除節點 99
- 節點 僅有單一子節點(左子節點 )。
- 依 BST 刪除規則,直接由 接替 的位置,成為 的右子節點。
刪除後的樹狀結構如下:
35
/ \
30 46
/ \ / \
28 34 42 75
/ \
70 80
二、 選項分析
第 21 題
Question set V
Suppose we have an undirected graph G=(V,E), where V={a,b,c,d,e,f,g,i} and E={[(a,i), 2], [(a,b), 6], [(a,g), 4], [(b,c), 7], [(b,e), 9], [(c,d), 4], [(d,g), 5], [(e,g), 8], [(f,g), 2], [(f,i), 3]}. Note that [(x,y),w] indicates that there is an edge, with weight w, between vertices x and y. Please answer the following questions.
- We find a spanning tree for the graph using the depth-first search algorithm, starting with vertex e. Note that if two or more vertices qualify, then the one with the least alphabetical order is selected. What is the sum of the weights involved in the resulting tree?
(A) 31
(B) 32
(C) 33
(D) 34
登入後即可作答並保存紀錄。
核心觀念
深度優先搜尋(Depth-First Search, DFS)從起點出發,沿著尚未拜訪的頂點一路深入;當目前頂點沒有未拜訪鄰點時,便回溯至上一層。
DFS 所形成的 spanning tree:
- 每個新拜訪的頂點,透過一條邊連接到發現它的頂點。
- 若圖有 個頂點,spanning tree 必有 條邊。
- 本題共有 個頂點,因此結果應有 條樹邊。
- 邊的選擇依 DFS 與字母順序決定,並非依權重大小選擇,因此不是最小生成樹問題。
解題方法
各頂點的鄰接頂點依字母順序排列:
從 開始進行 DFS:
- 拜訪 ,選最小字母鄰點 :加入 ,權重 。
- 在 選尚未拜訪的 :加入 ,權重 。
- 在 選尚未拜訪的 :加入 ,權重 。
- 在 的鄰點中, 已拜訪,選 :加入 ,權重 。
- 在 選尚未拜訪的 :加入 ,權重 。
- 的鄰點皆已拜訪,回溯至 ; 再選尚未拜訪的 :加入 ,權重 。
- 在 選尚未拜訪的 :加入 ,權重 。
- 的鄰點 皆已拜訪,搜尋結束。
因此 DFS spanning tree 的邊為:
第 22 題
Question set V
Suppose we have an undirected graph G=(V,E), where V={a,b,c,d,e,f,g,i} and E={[(a,i), 2], [(a,b), 6], [(a,g), 4], [(b,c), 7], [(b,e), 9], [(c,d), 4], [(d,g), 5], [(e,g), 8], [(f,g), 2], [(f,i), 3]}. Note that [(x,y),w] indicates that there is an edge, with weight w, between vertices x and y. Please answer the following questions.
- We find a spanning tree for the graph using the breadth-first search algorithm, starting with vertex e. Note that if two or more vertices qualify, then the one with the least alphabetical order is selected. What is the sum of the weights involved in the resulting tree?
(A) 40
(B) 39
(C) 38
(D) 37
登入後即可作答並保存紀錄。
核心觀念
廣度優先搜尋(Breadth-First Search, BFS)以「距離起點的層數」逐層拜訪頂點:
- 先拜訪起點。
- 將目前頂點尚未發現的鄰接點加入佇列。
- 依佇列先進先出順序處理。
- 若有多個候選頂點,依題意採字母順序較小者優先。
BFS 產生的 spanning tree(生成樹)中,每個頂點第一次被發現時所使用的邊,便是生成樹的邊。注意:BFS 的選擇依鄰接關係與拜訪順序決定,不會依邊的權重大小選邊。
解題方法
由頂點 開始,依字母順序處理鄰接頂點。
第 1 層:由 出發
的鄰接頂點為 ,依字母順序先 後 :
- 加入邊 ,權重
- 加入邊 ,權重
佇列:
處理
的鄰接頂點為 。其中 已發現,因此依序發現:
- 加入邊 ,權重
- 加入邊 ,權重
佇列:
處理
的鄰接頂點為 。
- 已被 發現
- 已被拜訪
- 尚未發現,加入邊 ,權重
- 尚未發現,加入邊 ,權重
佇列:
處理
的鄰接頂點為 。
- 已發現
- 尚未發現,加入邊 ,權重
佇列:
此時所有頂點皆已發現,生成樹的邊為:
Suppose we have an undirected graph , where
and
Note that indicates that there is an edge, with weight , between vertices and . Please answer the following questions.
第 23 題
We find a spanning tree for the graph using Prim’s algorithm, starting with vertex . Note that if two or more vertices qualify, then the one with the least alphabetical order is selected.
What is the sum of the weights involved in the resulting tree?
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
Prim 演算法從指定起點開始,逐步加入「連接已加入頂點與尚未加入頂點」的最小權重邊,直到所有頂點都加入。若有多個頂點符合條件,依題意選字母順序最小者。
解題方法
原卷圖中的圖形資訊與題幹相符:頂點為 ,邊及權重包含 相連的 權重 、 權重 ;依 Prim 演算法從 開始選邊。各步驟中最小的候選邊如下:
| 已加入的頂點 | 選入的邊 | 權重 |
|---|---|---|
最後加入了全部 個頂點,共選入 條邊,權重總和為
第 24 題
Which of the following are true for the resulting tree obtained in Question 21?
(A) The node following node is node .
(B) The edge is contained in the tree.
(C) The edge is contained in the tree.
(D) The edge is contained in the tree.
登入後即可作答並保存紀錄。
核心觀念
本題承接第 21 題的深度優先搜尋(DFS)生成樹。DFS 從指定起點開始,沿著尚未拜訪的鄰點深入;若有多個鄰點可選,依題目規定選字母順序最前者。生成樹包含 DFS 過程中首次拜訪新節點時所走的邊。
解題方法
原卷第 21 題指定從頂點 開始做 DFS,若有多個鄰點符合條件,選字母順序最前者。圖中給出頂點 與加權邊;DFS 只依鄰接關係與字母順序決定走訪,加權數值不影響結果。
依字母順序排列各頂點的鄰點,走訪如下:
- 從 出發,鄰點為 ,先走 ,選取樹邊 。
- 在 ,未拜訪的鄰點為 ,先走 ,選取 。
- 在 ,未拜訪的鄰點為 ,先走 ,選取 。
- 在 ,未拜訪的鄰點為 ,先走 ,選取 。
- 在 ,走到未拜訪的 ,選取 ; 的鄰點已拜訪,回溯。
- 回到 後走向 ,選取 ;在 走到未拜訪的 ,選取 。
因此 DFS 拜訪順序為
第 21 題的生成樹邊為
選項分析
第 25 題
Which of the following are true for the resulting tree obtained in Question 23?
(A) The edge is contained in the tree.
(B) The edge is contained in the tree.
(C) The edge is contained in the tree.
(D) The edge is contained in the tree.
登入後即可作答並保存紀錄。
核心觀念
第 23 題以 Prim 演算法從 開始建構最小生成樹。每一步都從已納入樹的頂點,選一條連到樹外頂點且權重最小的邊;選取後若會形成環,就不納入。題目規定權重相同時按字母順序選頂點,本題實際選取過程沒有遇到平手。
解題方法
原卷第 25 題詢問第 23 題所得的樹包含哪些邊;第 23 題指定從 開始執行 Prim 演算法。依題目給的無向加權圖,逐步選取:
| 步驟 | 新納入頂點 | 選取的邊 | 權重 |
|---|---|---|---|
| 1 | 8 | ||
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | 2 | ||
| 5 | 5 | ||
| 6 | 4 | ||
| 7 | 6 |
例如,納入 後,候選邊中 的權重為 ,小於 的 ,因此先納入 ; 加入後,連到樹外頂點 的邊 權重為 ,比連到 的候選邊更小,所以接著納入 。
最後得到的生成樹邊集合為
選項分析
- (A) 包含在樹中:錯。 當 加入時,是透過權重 的 連到樹;之後 已在樹內, 不會被選入。
- (B) 包含在樹中:對。 加入後, 仍在樹外;