114 年 國立成功大學智慧資訊安全碩士學位學程《資料結構》
第 1 題
A is a two dimension array, the locations of A(3, 2) and A(2, 3) are 1115 and 1207, respectively. Assume that each element occupies two addresses. Please give the location of A(5, 10).
a) 1841
b) 1851
c) 1861
d) 1871
e) None of the above.
登入後即可作答並保存紀錄。
解題步驟
- 設 為 的起始位址,元素大小 。
假設採用 Column‑major 佈局(因 Row‑major 會得到負的欄數),則
其中 為列數(行數)。
- 由已知兩點建立方程式
第 2 題
Given that the post-order traversal of a binary tree is "EFBCDA" and the in-order traversal of the same tree is "EFACBD", please provide the resulting pre-order traversal of the tree.
a) AFEDCB
b) AEFDCB
c) AFECDB
d) AFEBCD
e) None of the above.
登入後即可作答並保存紀錄。
- 從後序 取最後一個節點為根:A。
- 在中序 以 A 為界,左子樹中序為 ,右子樹中序為 。
左子樹
- 後序對應為前兩個字元 ,根為最後一個 F。
- 中序 ,左子樹僅有節點 E,右子樹空。
右子樹
- 後序剩餘 ,根為最後一個 D。
- 中序 ,左子樹中序為 ,右子樹空。
第 3 題
Given a directed graph in the following, please identify which edge would become a cross edge if we traverse the graph by the breadth first search algorithm from node C. Assume that the nodes are stored in the adjacent list according to alphabetic order. Please specify which edge forms a cross edge in the resulting breadth-first spanning tree.
a) (F, E)
b) (B, D)
c) (D, B)
d) (A, B)
e) All of the above.
🖼️【此處有附圖,請對照原卷】
登入後即可作答並保存紀錄。
核心觀念
廣度優先搜尋(BFS)從起點開始,依照相鄰串列的字母順序逐層探索。搜尋樹中的邊稱為樹邊;原圖中未被選為樹邊的邊,若連接的兩個頂點彼此都不是對方在搜尋樹中的祖先,便是交叉邊(cross edge)。
解題方法
由圖可讀出,從 出發可依序到達 。按照字母順序處理相鄰頂點,BFS 搜尋樹的層次為:
- 第 0 層:
- 第 1 層:
- 第 2 層:
因此,樹邊為 、、,以及 、。第 1 層的 互為同層頂點;第 2 層的 也互為同層頂點。選項所列的四條邊都不是搜尋樹邊,且各自連接的端點互不為祖先或後代,所以都是交叉邊。
選項分析
第 4 題
T(n)=3T(n/4) + n log n, Please give the tightly bound of T(n).
a) 0(n)
b) 0(n log n)
c) 0(n² log n)
d) 0(n²)
登入後即可作答並保存紀錄。
核心觀念
本題考查遞迴式的漸進時間複雜度,主要使用 Master Theorem。
標準形式為:
其中:
- :每次遞迴產生 3 個子問題
- :每個子問題的規模為
- :分割與合併等非遞迴工作的成本
首先計算臨界函數:
因為:
所以:
接著比較 與 的成長速度。
由於 的成長速度大於 ,因此遞迴外成本 會主導整體複雜度。
解題方法:套用 Master Theorem
Master Theorem 的第三種情況是:
若存在常數 ,使得
且滿足正規性條件:
其中 ,則:
本題中:
而:
可取 為任意小於 的正數,例如 ,則:
因此符合第三種情況。
再驗證正規性條件:
利用對數公式:
因此:
第 5 題
What will the array look like after a delete operation is applied on the maximum heap stored in the array?
75 58 67 42 55 30 29
[0] [1] [2] [3] [4] [5] [6] [7]
a)
67 58 30 55 42 29
[0] [1] [2] [3] [4] [5] [6] [7]
b)
67 58 55 42 30 29
[0] [1] [2] [3] [4] [5] [6] [7]
c)
67 58 30 42 55 29
[0] [1] [2] [3] [4] [5] [6] [7]
d)
29 42 55 30 67 58
[0] [1] [2] [3] [4] [5] [6] [7]
e) None of the above.
🖼️【此處有附圖,請對照原卷】
登入後即可作答並保存紀錄。
核心觀念
最大堆須符合:每個父節點的值都不小於子節點。以陣列表示時,索引 的左右子節點分別在 與 。
刪除最大值時,先移除根節點,再把堆中最後一個元素移到根部,逐層與較大的子節點交換,直到恢復最大堆性質。
解題方法
原卷圖中,索引 是空位,堆的元素從 開始:索引 到 依序為 。刪除根節點 後,堆大小由 減為 。
- 將最後一個元素 移到根部,並移除原索引 的元素:。
- 根節點 的子節點是 和 。與較大的 交換:。
- 索引 的 只有索引 的子節點 ;索引 已超出堆的範圍。將 與 交換,得到 。
第 6 題
Please identify the incorrect descriptions.
a) The max number of nodes in a binary tree with depth k is , where .
b) Using the most significant digit first (MSD) to sort multiple keys exist is simple than that using the least significant digit first (LSD).
c) Merge sort is not a in-place sorting algorithm.
d) The number of threads in a binary tree with n nodes is n+1.
e) The above descriptions are correct.
登入後即可作答並保存紀錄。
核心觀念
本題綜合考查:
- 二元樹深度與最大節點數
- MSD/LSD 基數排序
- Merge sort 是否為就地排序
- 二元樹的 threaded links 數量
- 複選題中「以上皆正確」的判斷
解題方法
逐一檢查各敘述是否符合標準定義與定理。只要有一個敘述錯誤,選項 e「以上敘述皆正確」也必定錯誤。
選項分析
a) 正確
若二元樹根節點深度定義為 ,則深度為 的滿二元樹共有:
因此,深度為 的二元樹最多有 個節點。題目給定 ,敘述成立。
b) 錯誤
MSD(Most Significant Digit first)由最高位開始排序;LSD(Least Significant Digit first)由最低位開始排序。
LSD 基數排序通常採用穩定排序,依序處理各位數即可,流程較單純。MSD 則從最高位開始分組,之後還要對各分組遞迴處理,實作與控制流程通常較複雜。
因此,題目所述「MSD 比 LSD 簡單」不正確。
c) 正確
第 7 題
The quick sort algorithm uses the divide and conquer technique. In each recursion, it will place a pivot of an array at the correct location and divide the array into two parts. Please identify the correct position of a pivot in the following array after the first recursion. To optimize performance, we apply the strategy of Median-of-3: median(key[left], key[middle], key[right]) to select pivot, where the index of middle = . Then, the pivot is then swapped with the key[left].
left =1, right =9
75 58 67 42 55 30 29 95 3
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
a) key[2]
b) key [4]
c) key [6]
d) key [8]
e) None of the above
🖼️【此處有附圖,請對照原卷】
登入後即可作答並保存紀錄。
核心觀念
本題考兩個步驟:先用 Median-of-3 選出 pivot,再透過 partition 將資料分到 pivot 兩側。完成分割後,pivot 左側的值都小於 pivot,右側的值都大於 pivot。
解題方法
題目給定 、,所以
三個候選值為 、、,其中位數是 。依題意將它與 交換,讓 pivot 位於分割範圍的左端。
接著計算有幾個值小於 :、、、 大於 ;、、、 小於 ,共有 個。因此 pivot 分割完成後,應位於第 個位置,也就是 。
以常見的雙指標分割法操作,分割後可得到:
第 8 題
Prim algorithm is used to construct a minimum spanning tree T by using the data structure "set". They form a set for all nodes in the beginning and gradually merge two nodes until T is found. Please identify the first edge which form a cycle in T during the procedure and the number of sets formed at that moment.
a) edge (3, 6) and 2 set
b) edge (4, 6) and 4 set
c) edge (0, 1) and 3 set
d) edge (4, 6) and 3 set
e) None of the above
🖼️【此處有附圖,請對照原卷】
登入後即可作答並保存紀錄。
核心觀念
題目描述「一開始每個節點各自成一個集合,再依序合併節點集合,遇到兩端已在同一集合的邊就會形成環」,這是 Kruskal 演算法搭配不相交集合(Disjoint Set)的作法。Prim 演算法則是從一個起點逐步擴張,不是依序合併所有節點的集合。
Kruskal 依邊權重由小到大檢查;若邊的兩端已在同一集合,加入該邊就會形成環。
解題方法
圖中相關的邊依權重由小到大排列如下:
一開始有 個集合,分別包含節點 到 。依序加入前五條邊後,集合變化為:
- 加入 :集合數由 變 。
- 加入 :集合數由 變 。
- 加入 :集合數由 變 。
- 加入 :集合數由 變 。
- 加入 :集合數由 變 。
第 9 題
Please identify the wrong descriptions in the following:
a) Heap is suitable for a priority queue.
b) Binary search is good for a dictionary.
c) Binary search is faster than Heap in the insert operation.
d) Max Heap is faster than binary serach in finding the maximum value.
e) None of the above.
🖼️【此處有附圖,請對照原卷】
登入後即可作答並保存紀錄。
核心觀念
本題比較最大堆積與二元搜尋樹在優先佇列、字典查詢、插入及找最大值時的操作效率。題目兩處寫「Binary search」,但因選項提到插入操作,以下依常見考題用法,將它視為「二元搜尋樹(Binary Search Tree, BST)」。
- 最大堆積的根節點是最大值,插入需向上調整。
- 二元搜尋樹的查詢、插入及找最大值,時間取決於樹高 ;平衡時約為 ,退化時可能為 。
解題方法
逐項比對各操作的用途與時間複雜度:
| 操作 | 最大堆積 | 二元搜尋樹 |
|---|---|---|
| 插入 | 平衡時 ;最壞 | |
| 找最大值 | ,直接看根節點 | ,沿右子樹走到最右側節點 |
最大堆積特別適合快速取得最大值,也適合實作優先佇列。插入方面,二元搜尋樹沒有比堆積更快的普遍保證。
選項分析
- a) Heap is suitable for a priority queue. 正確。
最大堆積可在 取得最高優先權的元素,並能在 插入或刪除最大值,因此適合實作優先佇列。
第 10 題
If Queue is implemented by the circular list, which operation requires linear time in the worst case.
a) Find();
b) isEmpty();
c) Push();
d) Pop();
e) Noe of above.
登入後即可作答並保存紀錄。
核心觀念
以循環鏈結串列(circular linked list)實作 Queue 時,通常維護一個 rear 指標:
- 隊首
front = rear->next - 從隊尾加入元素(enqueue / Push)
- 從隊首移除元素(dequeue / Pop)
rear = NULL表示佇列為空
循環結構可讓隊首與隊尾相鄰,因此加入與移除元素都能在固定次數的指標操作內完成。時間複雜度如下:
但若要尋找特定元素,必須從隊首逐一走訪,最壞情況可能檢查整個佇列:
其中 為佇列中的元素數量。
解題方法
判斷各操作是否需要線性時間,重點在於:
- 操作是否只需修改或檢查固定數量的指標。
- 操作是否必須逐一走訪所有節點。
循環鏈結串列的 rear 指標可直接定位隊尾,而 rear->next 可直接定位隊首。因此 Push() 和 Pop() 不需要搜尋整個串列。
Find() 則必須從隊首開始,依序比較節點內容。若目標元素位於最後一個節點,或根本不存在,就需要走訪 個節點,因此最壞時間為:
選項分析
第 11 題
Given a hash table with 11 buckets; labeled from 0 to 10. The hash function h(key)= key % 11 allocates data into a hash table. The chaining strategy is used to store data in each bucket. Please show the total number of comparisons required to store the numbers "3, 41, 15, 36, 74, 58, 91, 45, 48, 64".
a) 20 b) 16 c) 22 d) 14 e) None of the above.
登入後即可作答並保存紀錄。
哈希表設定
- 桶數 = 11 (0 ~ 10)
- 雜湊函式
- 使用鏈結法 (chaining)
依序插入資料並記錄衝突次數
| 資料 | 桶內已有元素數 | 衝突比較次數 | |
|---|---|---|---|
| 3 | 3 | 0 | 0 |
| 41 | 8 | 0 | 0 |
| 15 | 4 | 0 | 0 |
| 36 | 3 | 1 (3) | 1 |
第 12 題
Continue from the problem 11, instead of chaining, we adopt open addressing to handle overflow, where each bucket has one slot. The resulting position of data is determined by h(key)= {initial position + i* h₂(key) }%11, where h₂(key) = key % 7 and i denotes the iteration of the overflow. Please show the position of 58 in the table.
a) 7 b) 5 c) 10 d) 3 e) None of the above.
登入後即可作答並保存紀錄。
使用雙雜湊的開放定址法:
- 主雜湊
- 次雜湊
- 實際探測位置
對鍵值 :
- 初始位置
- 次雜湊步長
第 13 題
Please indicate the algorithm that cannot allow negative weighted edges in finding the shortest path.
(A) Johnson's algorithm
(B) Bellman and Ford's Algorithm
(C) Floyd Warshall's Algorithm
(D) Dijkstra's Algorithm
(E) None of above
登入後即可作答並保存紀錄。
核心觀念
本題考查最短路徑演算法對「負權重邊」的處理能力。負權重邊的權重小於 ;這和「負權重環」不同。負權重環會讓路徑成本持續下降,因而沒有有限的最短路徑。
Dijkstra 演算法的貪婪策略會將目前距離最小的頂點定案,並假設之後不會再找到更短的路徑。這個假設要求邊權重皆為非負;若有負權重邊,已定案的距離可能被後續路徑改寫。
解題方法
原卷第 13 題詢問哪個演算法「不能允許負權重邊」來求最短路徑,選項依序是 Johnson、Bellman-Ford、Floyd-Warshall、Dijkstra 與「以上皆非」。判斷關鍵是:負權重邊會不會破壞演算法的核心步驟。
以 權重 、 權重 、 權重 為例。Dijkstra 會先將距離為 的 定案,但經過 到 的路徑成本是 ,比 更小。這表示 Dijkstra 的貪婪定案在負權重邊存在時可能失效。
選項分析
- (A) Johnson's algorithm:錯。 Johnson 演算法可處理負權重邊,通常先用 Bellman-Ford 計算頂點勢能,再重新加權,使邊權重非負,之後使用
第 14 題
For any nonempty binary tree, with nodes, if is the number of leaf nodes. Please give the number of nodes in a binary tree with degree 1, in terms of and .
(A)
(B)
(C)
(D)
(E) None of above
登入後即可作答並保存紀錄。
核心觀念
二元樹中,每個節點的度數是其子女數,因此節點度數只能是 、 或 。度數為 的節點就是葉節點,題目以 表示其數量。
解題方法
題目給定一棵非空二元樹,共有 個節點,其中 個是葉節點;要求度數為 的節點數。非空樹共有 條邊,而每條邊都從一個節點連向其子女。
設度數為 、 的節點數分別為 、。依節點總數與邊數建立關係:
由第一式得 ,代入第二式:
整理可得:
第 15 題
Please give the maximum flow from to in the network flow graph in Fig 2, where the value associated to an edge is its capacity.
🖼️【此處有附圖,請對照原卷】
(A) 10
(B) 7
(C) 16
(D) 14
(E) None of above
登入後即可作答並保存紀錄。
核心觀念
本題考最大流與最小割定理:網路中的最大流量,等於所有 - 割中最小的割容量。割容量是從割的 側指向 側的所有邊容量總和;反向邊不計入。
解題方法
圖中由 指向上方左節點的容量為 ,由 指向下方左節點的容量為 ;下方左節點指向下方右節點的容量為 。斜邊方向為上方左節點指向下方右節點、容量 ,以及下方右節點指向上方左節點、容量 。
先取一組可行流量:
- 至上方左節點送 ,其中 沿上方水平邊到上方右節點,再送至 ;另 沿容量為 的斜邊到下方右節點,再送至 。
- 至下方左節點送 ,再沿容量為 的邊到下方右節點,最後送至 。
第 16 題
How many recursive calls would the call "Fibonacci(10)" generate in the following program?
int Fibonacci(int n) {
if (n < 1) return 0;
if (n < 3) return 1;
(A) 55
(B) 108
(C) 110
(D) 20
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
本題考的是遞迴函式的呼叫次數分析。Fibonacci 函式以遞迴方式計算費氏數列,由於存在大量重疊子問題(overlapping subproblems),導致呼叫次數呈指數成長。題目要求計算 Fibonacci(10) 所「產生」的遞迴呼叫次數。
解題方法
從原卷頁圖確認程式碼為:
int Fibonacci(int n) {
if (n < 1) return 0;
if (n < 3) return 1;
圖上只列出兩個基底條件,未明確寫出遞迴式。依標準 Fibonacci 遞迴定義,補上:
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
題目問的是 Fibonacci(10) 會**產生(generate)**多少次遞迴呼叫。注意:最初的 Fibonacci(10) 本身不算被「產生」的遞迴呼叫,只有它內部衍生出來的呼叫才算。
定義 為呼叫 Fibonacci(n) 時產生的遞迴呼叫總次數(含所有子孫呼叫)。
- 當 或 時,直接 return,不產生任何遞迴呼叫,故 。
- 當 時,
Fibonacci(n)呼叫Fibonacci(n-1)和Fibonacci(n-2),這本身就是 2 次呼叫,再加上這兩個子呼叫各自內部產生的呼叫數:
逐步計算:
| 0 | 0 |
| 1 | 0 |
| 2 | 0 |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 |
第 二-1 題10 分
Given a circular list (or, a singly-linked circular list) that the link field of the last node points to the first node as shown in Fig 1, please complete the code to insert a new node to the front of the list in the following:
template <class T>
void CircularList<T>::InsertFront(const T& e)
{
ChainNode <T> *n = new ChainNode <T>(e);
if (last) { // nonempty chain
a) ____;
b) ____;
} else {
c) ____;
d) ____;
}
}
template <class T> class Chain;
template <class T>
class ChainNode {
friend class Chain <T>;
private:
T data;
ChainNode <T> *link;
};
🖼️【此處有附圖,請對照原卷】
登入後即可作答並保存紀錄。
核心觀念
循環單向串列的尾端指標 last 指向最後一個節點,而 last->link 指向第一個節點。要在串列前端插入新節點,非空時須讓新節點接到原本的第一個節點,再讓 last->link 改指向新節點;空串列則讓新節點的 link 指回自己,並將 last 設為新節點。
解題方法
圖中 last 指向尾端節點,尾端節點的 link 繞回第一個節點。依此結構,非空時保留原本的尾端節點,只更新新節點與 last->link;空串列時,新節點同時是第一個與最後一個節點。
第 二-2 題10 分
Given a graph shown in Fig 2, please answer the following questions:
(a) which are the articulation points, and
(b) the biconnected components of the graph.
🖼️【此處有附圖,請對照原卷】
登入後即可作答並保存紀錄。
核心觀念
本題考無向圖的割點與雙連通分量。
- 若移除頂點 後,圖的連通分量數增加, 就是割點。
- 雙連通分量是無法再拆成更小雙連通子圖的部分;依常見定義,橋邊也單獨算一個分量。
- 使用 DFS 判斷時,令 為頂點 的發現時間, 為從 的 DFS 子樹經由樹邊與至多一條回邊可到達的最早發現時間。
- 非根節點 若有 DFS 子節點 滿足 ,則 是割點。DFS 根節點則須有至少兩個 DFS 樹子節點才是割點。
解題方法
圖中可讀出上方有三角形 、、,頂點 以邊 – 接到下方;中間的頂點 、、 形成三角形;頂點 、、、 之間的邊構成完整圖 。
以頂點 為 DFS 根,依序走訪 。依 DFS 樹與回邊可得:
| 頂點 | ||
|---|---|---|
| 1 | 1 | |
| 2 | 1 | |
| 3 | 1 | |
| 4 | 4 |