112 年 國立成功大學電腦與通信工程研究所丁組《資料結構》
第 1 題10 分
- (10%) Show the asymptotic notations for the following statements.
(a)
(b)
登入後即可作答並保存紀錄。
核心觀念
本題考查漸近符號,用來描述輸入規模 足夠大時,函數的成長速度。
- :上界,表示成長速度不超過 的量級。
- :下界,表示成長速度至少達到 的量級。
- :緊確界,同時滿足 與 。
題目通常要求找出最精確的漸近成長量級。
解題方法
(a)
由階乘定義:
若只使用初等估計,由於每一項皆不大於 ,可得:
因此:
但 並不是最精確的描述。使用 Stirling 公式:
因此:
其中常數 不影響 表示法。
若課程僅要求以常見簡化形式表示,也可寫成:
這是比 更精確的答案;事實上, 並非 ,因為:
(b)
將多項式分解:
第 2 題10 分
- (10%) Write the prefix and postfix form of the following expressions:
(a)
(b)
// assuming C precedence
登入後即可作答並保存紀錄。
核心觀念
本題考查中序表示式轉換為:
- Prefix(前序式):運算子寫在運算元之前。
- Postfix(後序式):運算子寫在運算元之後。
轉換時依據 C 語言的:
- 運算子優先權
- 相同優先權時的結合方向
- 括號所指定的運算順序
本題涉及的優先權由高至低為:
其中 皆為左結合。
解題方法
先依照優先權與括號,將表示式分解成二元運算樹,再以樹根、左子樹、右子樹的順序讀出 Prefix;以左子樹、右子樹、樹根的順序讀出 Postfix。
(a)
1. 依優先權加上結構
原式可分解為:
其中:
以及:
括號內:
因此完整結構為:
2. Prefix(前序式)
由最外層運算子開始:
- 最外層為
- 左側為
- 的左側為
- 的左側為
依序寫出:
3. Postfix(後序式)
先寫左子樹,再寫右子樹,最後寫運算子:
- :
- :
- :
- :
- 最後與前半部相加,再減去
因此:
(b)
1. 依 C 語言優先權加上結構
括號已明確指定主要運算順序:
第 3 題10 分
- (10%) Let Z() be a function that inputs a string and outputs a string, defined as follows:
Base Case: If the input string X is a single letter L, then Z(X)=L.
Recursive Case: If the input string X is a string of the form LS, then Z(X)="Z(S)L".
(a) If X="happy", what is Z(X)?
(b) What does the function Z() actually perform?
登入後即可作答並保存紀錄。
核心觀念
本題考查遞迴函數的定義與字串串接。
對於輸入字串 :
- 是字串第一個字母。
- 是去除第一個字母後的剩餘字串。
- 遞迴定義為:
這表示先對剩餘字串 遞迴處理,再把原本的第一個字母 接到結果最後面。因此,字母的順序會被反轉。
解題方法
依照遞迴定義逐層拆解,再由最底層的 Base Case 往回代入。
令:
逐步展開:
其中:
是因為 y 只有一個字母,符合 Base Case。
函數實際功能
第 4 題15 分
- (15%)
(a) Using recursive definition (i.e., having Base case and Recursive case) to define a binary tree.
(b) Show the maximum number of nodes in a binary tree on level i.
登入後即可作答並保存紀錄。
核心觀念
本題考查二元樹的遞迴定義,以及二元樹各層節點數量的上限。
二元樹具有以下特性:
- 每個節點最多有兩個子節點。
- 子樹本身仍然是二元樹。
- 空樹也是二元樹的基本情況。
以下採用常見定義:根節點位於第 層(level )。
(a) 以遞迴方式定義二元樹
Base case:基底情況
空樹 是一棵二元樹。
也可以將只有一個根節點、沒有左右子樹的樹視為最基本的非空二元樹。
Recursive case:遞迴情況
若 與 都是二元樹,且新增一個節點 作為根節點,令 成為 的左子樹、 成為 的右子樹,則所形成的樹也是二元樹:
其中:
- 是根節點;
- 是左子樹;
- 是右子樹;
- 與 可以是空樹。
因此,二元樹可形式化表示為:
這個定義完整表達二元樹的結構:先以空樹作為終止條件,再由兩棵較小的二元樹組合出較大的二元樹。
(b) 第 層的最大節點數
解題方法
採用逐層觀察與數學歸納法。
二元樹中,每個節點最多產生兩個子節點,因此下一層的最大節點數至多是上一層的兩倍。
逐層推導
根節點位於第 層,因此:
- 第 層最多有 個節點;
- 第 層最多有 個節點;
- 第 層最多有 個節點;
- 第 層最多有 個節點。
因此可觀察出第 層的最大節點數為:
第 5 題10 分
- (10%) Landscapes in films are often computer generated. Ever wondered how they do it? Here is an algorithmic
drawing example. Draw enough repeating patterns until you can tell what it is. Start by drawing a single straight
vertical line as below
🖼️【此處有附圖,請對照原卷】
Then execute Draw() from this vertical line until you can show how the algorithmic drawing works.
Draw()
{
Draw 3 shorter lines at an angle in the top two-thirds of the line on its left side.
Draw 3 shorter lines at an angle in the top two-thirds of the line on its right side.
Choose a new existing line and Draw() from that line again
}
登入後即可作答並保存紀錄。
核心觀念
本題考的是遞迴作圖與自相似結構:先畫一條線,再把同一套分枝規則套用到新產生的短線上,逐步形成樹狀圖形。
每次對一條線執行 Draw(),都會在該線的上方三分之二範圍內,向左、向右各畫 條較短的斜線,因此一次新增 條子線。子線必須連接到母線,且能繼續產生自己的分枝。
解題方法
原卷附圖只有一條直立線,作為最初的主幹;框內規則要求先畫左右各 條短枝,再選一條新線重複作圖。題目未指定分枝角度、長度比例與停止深度,因此採用對稱分枝、子線逐次縮短的方式示範。
第一步:在主幹上畫出六條短枝。
在主幹上方三分之二內選取三個位置,每個位置向左上與右上各畫一條較短的線,下方保留不分枝的幹段。
第二步:在子線上重複相同規則。
將每條子線與主幹相接的一端視為基部,另一端視為頂端。在子線靠近頂端的三分之二範圍內,再向它自己的左右兩側各畫 條更短的線。
下圖示範主幹的六條分枝,並進一步對最上方左右兩條子線各執行一次 Draw()。● 表示實際連接點,⋮ 表示省略的下方幹段;圖形為比例示意。
第 6 題15 分
- (15%)
(a) Given a graph G=(V,E), write a pseudo-code to generate all pairs shortest paths.
(b) Also, you need to define the input, and output of the data structure.
登入後即可作答並保存紀錄。
核心觀念
本題考查「全點對最短路徑」(All-Pairs Shortest Paths, APSP)問題:給定圖 ,求任意兩個頂點 之間的最短距離。
最典型的方法是 Floyd–Warshall 演算法。其核心定義為:
當新增頂點 作為中繼點時,從 到 的最短路徑只有兩種情況:
-
不經過 :
-
經過 :
因此遞迴公式為:
Floyd–Warshall 可處理負權重邊,但不能處理負權重循環,因為負權重循環會使最短距離沒有有限值。
解題方法
令頂點數為 ,使用二維矩陣 dist 儲存距離:
dist[i][j]:目前已知的頂點 到頂點 的最短距離。- 若 ,初始化為邊權重 。
- 若 與 之間沒有直接邊,初始化為 。
dist[i][i]=0。
接著依序將每個頂點 作為中繼點,更新所有起點 與終點 :
虛擬碼
FloydWarshall(V, E, w):
n ← |V|
dist ← n × n 的矩陣
for each i in V:
for each j in V:
if i = j:
dist[i][j] ← 0
else if (i, j) ∈ E:
dist[i][j] ← w(i, j)
else:
dist[i][j] ← ∞
for each k in V:
for each i in V:
for each j in V:
if dist[i][k] ≠ ∞ and dist[k][j] ≠ ∞:
dist[i][j] ← min(
dist[i][j],
dist[i][k] + dist[k][j]
)
return dist
其中三層迴圈的順序必須是:
k → i → j
因為第 層代表「允許頂點 作為新的中繼點」,這是 Floyd–Warshall 正確性的關鍵。
輸入資料結構
輸入包含:
- 頂點集合 ,頂點數為 。
- 邊集合 。
- 邊權重函數 ,其中:
可使用下列資料結構表示:
V:頂點陣列或頂點編號 。w: 的權重矩陣。∞:代表兩點之間沒有直接邊。
若圖為有向圖,必須分別記錄 與 。若圖為無向圖,則有:
第 7 題15 分
- (15%) A word-chain automaton is a directed graph with a finite number of vertices V, satisfying the
following properties. (i) There is a specified initial node vo ∈ V. (ii) There is subset F of set which
contains terminal nodes. (iii) Each edge is labeled with a symbol from a finite set W. A string w1w2...wk
of symbols in W is recognized by the automaton if there is a directed path starting at the initial node
Vo and ending at a terminal node w∈ F such that the sequence of edges in this directed path is
w1w2...wk. The set L of all strings recognized by the automaton is called the language recognized by the
automaton. A word-chain automaton is a special type of finite-state automaton, which in turn, is a type of
finite-sate machine.
(a) Draw a word-chain automaton such that the following five sentences can be recognized: 1. I like
his rabbit. 2. They hate his cat. 3. I think that his hamster is cute. 4. I know that his rabbit is soft. 5.
They hate to have a hamster.
(b) Explain how the above automaton is used to check whether a sentence is recognized or not.
登入後即可作答並保存紀錄。
(a) 文字鍊自動機構圖
狀態集合 ,初始狀態 ,終止狀態 。邊以單字為標籤( 為所有單字),如下:
| 起始 | 標籤 | 終點 |
|---|---|---|
| I | ||
| like | ||
| his | ||
| rabbit | ||
| They | ||
| hate | ||
| his | ||
| cat | ||
| to | ||
| have | ||
| a | ||
| hamster | ||
| I | ||
| think | ||
| that | ||
| his | ||
| hamster |
第 8 題15 分
- (15%) The following puzzle is called a word ladder: In a word ladder puzzle, you must make the change
occur gradually by changing one letter at a time. At each step you must transform one word into another
word, you are not allowed to transform a word into a non-word. The following sequence of words shows
one possible solution to the problem "Transform the word "FOOL" into the word "SAGE"."
Example: FOOL -> POOL -> POLL -> POLE-> PALE -> SALE -> SAGE
Here, we are interested in figuring out the smallest number of transformations needed to turn the
starting word into the ending word.
(a) Construct an example graph by using these words: FOOL, POOL, POLL, POLE, PALE, SALE,
SAGE, COOL, FOUL, FOIL, FAIL, FALL, PALL, POLE, POPE, PAGE. Represent the relationships
between the words as a word ladder graph. (Hint: Think about the path where we get from the word
"FOOL" to the word "SAGE").
(b) Discuss which search algorithm you can use to find an efficient path from the starting word to the
ending word, why it is more efficient compared with other. Discuss the complexity.
(c) There is one way to improve efficiency instead of using a graph to show the relationship. Suppose
that we have a huge number of buckets, each of them with a four-letter word on the outside, except
that one of the letters in the label has been replaced by an underscore. The labels on the buckets are
the keys in our dictionary. The value stored for that key is a list of words. Show the data structure
and list the buckets using the following words (don't list the buckets containing only one item):
FOOL, POOL, POLL, POLE, PALE, SALE, SAGE, COOL, FOUL, FOIL, FAIL, FALL, PALL,
POLE, POPE, PAGE
登入後即可作答並保存紀錄。
核心觀念
本題考查兩個概念:
-
字梯圖(word ladder graph)
每個單字視為一個頂點;若兩個單字長度相同,且恰好只有一個字母不同,便在兩頂點之間連一條邊。 -
無權重圖的最短路徑
每次改變一個字母視為成本 ,因此所有邊權重相同。從起點到終點所需的最少轉換次數,就是圖上的最短路徑長度。
(a) 建立字梯圖
判斷相鄰頂點的方法
比較兩個單字的四個位置:
- 恰好一個位置不同:相鄰,連邊。
- 不同位置超過一個:不相鄰。
- 完全相同:不另外建立新邊。
例如:
FOOL與POOL只有第一個字母不同,因此相鄰。FOOL與POLL有兩個字母不同,因此不相鄰。POLE與POPE只有第三個字母不同,因此相鄰。
圖中的邊
由題目所給單字建立圖,可得到以下相鄰關係:
FOOL:POOL、COOL、FOUL、FOILPOOL:FOOL、POLLPOLL:POOL、POLE、PALLPOLE:POLL、PALE、POPEPALE:POLE、SALE、PAGESALE:PALE、SAGESAGE:SALE、PAGECOOL:FOOLFOUL:FOOL、FOILFOIL:FOOL、FOUL、FAILFAIL:FOIL、FALLFALL:FAIL、PALLPALL:FALL、POLLPOPE:POLEPAGE:PALE、SAGE
其中 POLE 在題目中重複出現,圖論上仍視為同一個頂點。
圖形表示
可用下列方式表示主要連線:
此外還有:
以及:
因此,題目給出的範例路徑為:
此路徑共有 條邊,也就是 次字母轉換。
(b) 搜尋最有效率的路徑
適合使用廣度優先搜尋 BFS
此圖是無權重圖,每次字母轉換的成本均為 ,因此應使用廣度優先搜尋(Breadth-First Search, BFS)。
BFS 從 FOOL 開始,按照距離由近到遠逐層搜尋:
- 第 層:
FOOL - 第 層:所有與
FOOL只差一個字母的單字 - 第 層:距離
FOOL為 的單字 - 依此類推,直到找到
SAGE
BFS 第一次抵達某個頂點時,所使用的路徑一定是從起點到該頂點的最短路徑。因此,第一次找到 SAGE 時,即得到最少轉換次數。
本題的一條最短路徑
沿著範例路徑搜尋:
每一步都只改變一個字母,共有:
次轉換。
另一條同樣長度的最短路徑為:
為何不用 DFS
深度優先搜尋(DFS)可能先走進很長的路徑,再回頭尋找較短的路徑,因此:
- DFS 可以找到一條可行路徑;
- 但第一次找到的路徑不保證是最短路徑;
- 必須搜尋更多分支,才能確認最短答案。
為何不用 Dijkstra
Dijkstra 適用於邊權重不同且沒有負權重的圖。本題所有邊權重都是 ,使用 Dijkstra 會增加不必要的優先佇列處理;BFS 已能直接得到最短路徑。