112 年 國立成功大學電腦與通信工程研究所丁組《資料結構》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題10 分

  1. (10%) Show the asymptotic notations for the following statements.
    (a) n!n!
    (b) n3+106n2n^3 + 10^6 n^2

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查漸近符號(Asymptotic Notations),用來描述輸入規模 n→∞n \to \infty 時函數的成長速度:

  • O(g(n))O(g(n))(上界):存在正常數 c,n0c, n_0,使所有 n≥n0n \ge n_0 皆滿足 0≤f(n)≤c⋅g(n)0 \le f(n) \le c \cdot g(n)。
  • Ω(g(n))\Omega(g(n))(下界):存在正常數 c,n0c, n_0,使所有 n≥n0n \ge n_0 皆滿足 0≤c⋅g(n)≤f(n)0 \le c \cdot g(n) \le f(n)。
  • Θ(g(n))\Theta(g(n))(緊確界):同時滿足 f(n)=O(g(n))f(n) = O(g(n)) 與 f(n)=Ω(g(n))f(n) = \Omega(g(n))。

當題目要求給出函數的漸近表示法時,應優先找出最精確的緊確界 Θ\Theta(若無法直接寫出閉式,亦可補充常見的 OO 與 Ω\Omega 上下界)。


解題方法

(a) n!n!

  1. 初等上下界估計:
    由階乘定義 n!=1×2×⋯×nn! = 1 \times 2 \times \cdots \times n,每一項皆不大於 nn,故:
    n!≤nn  ⟹  n!=O(nn)n! \le n^n \implies n! = O(n^n)
    另一方面,當 n≥4n \ge 4 時,n!>2nn! > 2^n,故 n!=Ω(2n)n! = \Omega(2^n)。

    但須注意 n!n! 並非 Θ(nn)\Theta(n^n),因為兩者比值趨近於 00:
    lim⁡n→∞n!nn=0\lim_{n \to \infty} \frac{n!}{n^n} = 0

  2. 使用 Stirling 公式求緊確界:
    根據 Stirling 近似公式:
    n!∼2πn(ne)nn! \sim \sqrt{2\pi n}\left(\frac{n}{e}\right)^n
    常數係數 2π\sqrt{2\pi} 不影響漸近量級,因此最精確的緊確界為:
    n!=Θ(n(ne)n)n! = \Theta\left(\sqrt{n}\left(\frac{n}{e}\right)^n\right)


(b) n3+106n2n^3 + 10^6 n^2

當 n→∞n \to \infty 時,最高次項 n3n^3 主導成長速度,低階項 106n210^6 n^2 相對可忽略:
lim⁡n→∞n3+106n2n3=lim⁡n→∞(1+106n)=1\lim_{n \to \infty} \frac{n^3 + 10^6 n^2}{n^3} = \lim_{n \to \infty} \left(1 + \frac{10^6}{n}\right) = 1

亦可用定義正式證明上下界:

  • 下界:對所有 n≥1n \ge 1,有 n3+106n2≥n3n^3 + 10^6 n^2 \ge n^3,故:
    n3+106n2=Ω(n3)n^3 + 10^6 n^2 = \Omega(n^3)
  • 上界:當 n≥106n \ge 10^6 時,106≤n10^6 \le n,故 106n2≤n310^6 n^2 \le n^3,因此:
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題10 分

  1. (10%) Write the prefix and postfix form of the following expressions:
    (a) (A∗B)/D+E∗(f∗A+D)−C(A*B)/D+E*(f*A+D)-C
    (b) !((a&&c)∣∣(c>d))!((a \&\& c) || (c > d))
    // assuming C precedence

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查中序表示式轉換為:

  • Prefix(前序式):運算子寫在運算元之前。
  • Postfix(後序式):運算子寫在運算元之後。

轉換時依據 C 語言的:

  1. 運算子優先權
  2. 相同優先權時的結合方向
  3. 括號所指定的運算順序

本題涉及的優先權由高至低為:

!  >  ∗、/  >  +、−  >  >  >  &&  >  ∣∣! \;>\; *、/ \;>\; +、- \;>\; > \;>\; \&\& \;>\; ||

其中 ∗、/、+、−*、/、+、- 皆為左結合。


解題方法

先依照優先權與括號,將表示式分解成二元運算樹,再以樹根、左子樹、右子樹的順序讀出 Prefix;以左子樹、右子樹、樹根的順序讀出 Postfix。


(a)(A∗B)/D+E∗(f∗A+D)−C(A*B)/D+E*(f*A+D)-C

1. 依優先權加上結構

原式可分解為:

(A∗BD+E∗(f∗A+D))−C\left(\frac{A*B}{D}+E*(f*A+D)\right)-C

其中:

(A∗B)/D(A*B)/D

以及:

E∗(f∗A+D)E*(f*A+D)

括號內:

f∗A+D=(f∗A)+Df*A+D=(f*A)+D

因此完整結構為:

(((A∗B)/D)+(E∗((f∗A)+D)))−C\bigl(((A*B)/D)+(E*((f*A)+D))\bigr)-C

2. Prefix(前序式)

由最外層運算子開始:

  • 最外層為 −-
  • 左側為 ++
  • ++ 的左側為 //
  • // 的左側為 ∗*

依序寫出:

−+/∗ABD∗E+∗fADC\boxed{- + / * A B D * E + * f A D C}

3. Postfix(後序式)

先寫左子樹,再寫右子樹,最後寫運算子:

  • A∗BA*B:A B ∗A\ B\ *
  • (A∗B)/D(A*B)/D:A B ∗ D /A\ B\ *\ D\ /
  • f∗A+Df*A+D:f A ∗ D +f\ A\ *\ D\ +
  • E∗(f∗A+D)E*(f*A+D):E f A ∗ D + ∗E\ f\ A\ *\ D\ +\ *
  • 最後與前半部相加,再減去 CC

因此:

A B ∗ D / E f A ∗ D + ∗ + C −\boxed{A\ B\ *\ D\ /\ E\ f\ A\ *\ D\ +\ *\ +\ C\ -}

(b)!((a&&c)∣∣(c>d))!((a \&\& c) || (c > d))

1. 依 C 語言優先權加上結構

括號已明確指定主要運算順序:

!((a && c)∣∣(c>d))! \bigl((a\ \&\&\ c)||(c>d)\bigr)

其中:

  • 左側為 a && ca\ \&\&\ c
  • 右側為 c>dc>d
  • 最外層為邏輯或 ∣∣||
  • 最後由一元運算子 !! 作用於整體結果

完整結構為:

!((a && c)∣∣(c>d))!( (a\ \&\&\ c) || (c>d) )

2. Prefix(前序式)

最外層是一元運算子 !!,其運算元是 ∣∣|| 表示式:

! ∣∣ && a c > c d\boxed{! \ || \ \&\& \ a \ c \ > \ c \ d}

3. Postfix(後序式)

先處理兩個子表示式:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題10 分

  1. (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?

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查遞迴函數(recursive function)的定義與字串串接操作。

對於長度大於 11 的輸入字串 X=LSX = LS:

  • LL 代表字串的第一個字元。
  • SS 代表去除第一個字元後的剩餘子字串。
  • 遞迴規則為 Z(LS)=Z(S)LZ(LS) = Z(S)L,代表先對剩餘字串 SS 進行遞迴處理,再將原本位於最前方的字元 LL 串接到結果的尾端。

由於每次遞迴都把開頭字元移至結果的最後方,最終會使整個字串的字元順序完全反轉。

解題步驟

(a) 求 Z("happy")Z(\text{"happy"})

依照遞迴定義逐層拆解,直到剩餘字串僅剩單一字元(符合 Base Case:Z(L)=LZ(L) = L),再逐層代回串接:

Z("happy")=Z("appy")"h"=Z("ppy")"ah"=Z("py")"pah"=Z("y")"ppah"="y""ppah"="yppah"\begin{aligned} Z(\text{"happy"}) &= Z(\text{"appy"})\text{"h"} \\ &= Z(\text{"ppy"})\text{"ah"} \\ &= Z(\text{"py"})\text{"pah"} \\ &= Z(\text{"y"})\text{"ppah"} \\ &= \text{"y"}\text{"ppah"} \\ &= \text{"yppah"} \end{aligned}

因此,Z("happy")="yppah"Z(\text{"happy"}) = \text{"yppah"}。

(b) Z()Z() 的實際功能

設輸入字串為 X=L1L2L3⋯LnX = L_1 L_2 L_3 \cdots L_n(n≥1n \ge 1),依遞迴展開可得:

Z(L1L2⋯Ln)=Z(L2⋯Ln)L1=⋯=Z(Ln)Ln−1⋯L2L1=LnLn−1⋯L2L1Z(L_1 L_2 \cdots L_n) = Z(L_2 \cdots L_n)L_1 = \cdots = Z(L_n)L_{n-1}\cdots L_2 L_1 = L_n L_{n-1}\cdots L_2 L_1
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題15 分

  1. (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.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查二元樹的遞迴定義,以及二元樹各層節點數量的上限。

二元樹具有以下特性:

  • 每個節點最多有兩個子節點。
  • 子樹本身仍然是二元樹。
  • 空樹也是二元樹的基本情況。

以下採用常見定義:根節點位於第 00 層(level 00)。


(a) 以遞迴方式定義二元樹

Base case:基底情況

空樹 ∅\varnothing 是一棵二元樹。

也可以將只有一個根節點、沒有左右子樹的樹視為最基本的非空二元樹。

Recursive case:遞迴情況

若 TLT_L 與 TRT_R 都是二元樹,且新增一個節點 rr 作為根節點,令 TLT_L 成為 rr 的左子樹、TRT_R 成為 rr 的右子樹,則所形成的樹也是二元樹:

T=Node⁡(r,TL,TR)T = \operatorname{Node}(r,T_L,T_R)

其中:

  • rr 是根節點;
  • TLT_L 是左子樹;
  • TRT_R 是右子樹;
  • TLT_L 與 TRT_R 可以是空樹。

因此,二元樹可形式化表示為:

B={∅}∪{Node⁡(r,TL,TR)∣TL,TR∈B}\mathcal{B}= \{\varnothing\} \cup \{\operatorname{Node}(r,T_L,T_R)\mid T_L,T_R\in\mathcal{B}\}

這個定義完整表達二元樹的結構:先以空樹作為終止條件,再由兩棵較小的二元樹組合出較大的二元樹。


(b) 第 ii 層的最大節點數

解題方法

採用逐層觀察與數學歸納法。

二元樹中,每個節點最多產生兩個子節點,因此下一層的最大節點數至多是上一層的兩倍。

逐層推導

根節點位於第 00 層,因此:

  • 第 00 層最多有 1=201=2^0 個節點;
  • 第 11 層最多有 2=212=2^1 個節點;
  • 第 22 層最多有 4=224=2^2 個節點;
  • 第 33 層最多有 8=238=2^3 個節點。

因此可觀察出第 ii 層的最大節點數為:

2i2^i

數學歸納證明

基底:
當 i=0i=0 時,只有根節點,因此節點數為:

1=201=2^0

命題成立。

歸納假設:
假設第 ii 層最多有:

2i2^i

個節點。

歸納步驟:
第 ii 層的每個節點最多有兩個子節點,因此第 i+1i+1 層最多有:

2⋅2i=2i+12\cdot 2^i=2^{i+1}

個節點。

所以對所有 i≥0i\ge 0,第 ii 層最多有:

2i\boxed{2^i}

個節點。


解題技巧與常見陷阱

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題10 分

  1. (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
    }
🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 2 頁原卷第 3 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考的是遞迴作圖與自相似結構:先畫一條線,再把同一套分枝規則套用到新產生的短線上,逐步形成樹狀圖形。

每次對一條線執行 Draw(),都會在該線的上方三分之二範圍內,向左、向右各畫 33 條較短的斜線,因此一次新增 66 條子線。子線必須連接到母線,且能繼續產生自己的分枝。

解題方法

原卷附圖只有一條直立線,作為最初的主幹;框內規則要求先畫左右各 33 條短枝,再選一條新線重複作圖。題目未指定分枝角度、長度比例與停止深度,因此採用對稱分枝、子線逐次縮短的方式示範。

第一步:在主幹上畫出六條短枝。

在主幹上方三分之二內選取三個位置,每個位置向左上與右上各畫一條較短的線,下方保留不分枝的幹段。

第二步:在子線上重複相同規則。

將每條子線與主幹相接的一端視為基部,另一端視為頂端。在子線靠近頂端的三分之二範圍內,再向它自己的左右兩側各畫 33 條更短的線。

下圖示範主幹的六條分枝,並進一步對最上方左右兩條子線各執行一次 Draw()。● 表示實際連接點,⋮ 表示省略的下方幹段;圖形為比例示意。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6 題15 分

  1. (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)問題:給定圖 G=(V,E)G=(V,E),求任意兩個頂點 i,ji,j 之間的最短距離。

最典型的方法是 Floyd–Warshall 演算法。其核心定義為:

D(k)[i][j]=只允許前 k 個頂點作為中繼點時,i 到 j 的最短距離D^{(k)}[i][j] = \text{只允許前 } k \text{ 個頂點作為中繼點時,}i\text{ 到 }j\text{ 的最短距離}

當新增頂點 kk 作為中繼點時,從 ii 到 jj 的最短路徑只有兩種情況:

  1. 不經過 kk:

    D(k−1)[i][j]D^{(k-1)}[i][j]
  2. 經過 kk:

    D(k−1)[i][k]+D(k−1)[k][j]D^{(k-1)}[i][k]+D^{(k-1)}[k][j]

因此遞迴公式為:

D(k)[i][j]=min⁡(D(k−1)[i][j],D(k−1)[i][k]+D(k−1)[k][j])D^{(k)}[i][j] = \min\left( D^{(k-1)}[i][j], D^{(k-1)}[i][k]+D^{(k-1)}[k][j] \right)

Floyd–Warshall 可處理負權重邊,但不能處理負權重循環,因為負權重循環會使最短距離沒有有限值。


解題方法

令頂點數為 n=∣V∣n=|V|,使用二維矩陣 dist 儲存距離:

  • dist[i][j]:目前已知的頂點 ii 到頂點 jj 的最短距離。
  • 若 (i,j)∈E(i,j)\in E,初始化為邊權重 w(i,j)w(i,j)。
  • 若 ii 與 jj 之間沒有直接邊,初始化為 ∞\infty。
  • dist[i][i]=0。

接著依序將每個頂點 kk 作為中繼點,更新所有起點 ii 與終點 jj:

dist[i][j]←min⁡(dist[i][j],dist[i][k]+dist[k][j])\text{dist}[i][j] \leftarrow \min\left( \text{dist}[i][j], \text{dist}[i][k]+\text{dist}[k][j] \right)

虛擬碼

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

因為第 kk 層代表「允許頂點 kk 作為新的中繼點」,這是 Floyd–Warshall 正確性的關鍵。


輸入資料結構

輸入包含:

  1. 頂點集合 VV,頂點數為 nn。
  2. 邊集合 EE。
  3. 邊權重函數 ww,其中: w(i,j)=從 i 到 j 的邊權重w(i,j)=\text{從 }i\text{ 到 }j\text{ 的邊權重}

可使用下列資料結構表示:

  • V:頂點陣列或頂點編號 1,2,…,n1,2,\ldots,n。
  • w:n×nn\times n 的權重矩陣。
  • ∞:代表兩點之間沒有直接邊。

若圖為有向圖,必須分別記錄 w(i,j)w(i,j) 與 w(j,i)w(j,i)。若圖為無向圖,則有:

w(i,j)=w(j,i)w(i,j)=w(j,i)

初始化矩陣如下:

D[i][j]={0,i=jw(i,j),(i,j)∈E∞,(i,j)∉ED[i][j]= \begin{cases} 0, & i=j\\ w(i,j), & (i,j)\in E\\ \infty, & (i,j)\notin E \end{cases}

輸出資料結構

輸出為一個 n×nn\times n 的距離矩陣 dist:

dist[i][j]=頂點 i 到頂點 j 的最短距離\text{dist}[i][j] = \text{頂點 }i\text{ 到頂點 }j\text{ 的最短距離}

若 dist[i][j]=∞,表示頂點 ii 無法到達頂點 jj。

若需要輸出實際最短路徑,而不只是距離,還必須額外維護前驅矩陣 pred 或下一頂點矩陣 next。例如使用 next:

next[i][j] ← j       若 (i, j) ∈ E
next[i][j] ← NIL     若 i 無法直接到達 j

當發現經過 kk 可以改善路徑時:

dist[i][j] ← dist[i][k] + dist[k][j]
next[i][j] ← next[i][k]

如此即可由 next[i][j] 逐步還原 ii 到 jj 的完整路徑。


正確性說明

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7 題15 分

  1. (15%) A word-chain automaton is a directed graph with a finite number of vertices VV, satisfying the following properties. (i) There is a specified initial node v0∈Vv_0 \in V. (ii) There is subset FF of set VV which contains terminal nodes. (iii) Each edge is labeled with a symbol from a finite set WW. A string w1w2…wkw_1 w_2 \dots w_k of symbols in WW is recognized by the automaton if there is a directed path starting at the initial node v0v_0 and ending at a terminal node w∈Fw \in F such that the sequence of edges in this directed path is w1w2…wkw_1 w_2 \dots w_k. The set LL 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) 單字鏈自動機(Word-Chain Automaton)設計

為使五個句子都能被正確辨識且不致誤認未定義的混合句型,可採用確定性前綴樹(Trie)結構來建構有向圖 G=(V,E)G=(V, E):

  • 頂點集合 VV:{v0,v1,…,v24}\{v_0, v_1, \dots, v_{24}\}(共 25 個節點)
  • 初始節點:v0v_0
  • 終止節點集合 FF:{v4,v10,v16,v20,v24}\{v_4, v_{10}, v_{16}, v_{20}, v_{24}\}(下圖以雙括號 (( )) 標示)
  • 單字集合 WW:{I,They,like,hate,think,know,that,his,to,have,a,rabbit,cat,hamster,is,cute,soft}\{\text{I}, \text{They}, \text{like}, \text{hate}, \text{think}, \text{know}, \text{that}, \text{his}, \text{to}, \text{have}, \text{a}, \text{rabbit}, \text{cat}, \text{hamster}, \text{is}, \text{cute}, \text{soft}\}

狀態轉移圖:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 8 題15 分

  1. (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(字梯) 問題(出自 Bradley N. Miller 與 David L. Ranum 所著《Problem Solving with Algorithms and Data Structures using Python》圖論章節),主要考查:

  1. 字梯圖(Word Ladder Graph)建模:將每個合法單字視為一個頂點(Vertex),若兩個相同長度的單字恰好只有一個字母不同(Hamming distance =1= 1),則在兩頂點之間連一條無向邊(Undirected Edge)。
  2. 無權重圖最短路徑(Unweighted Shortest Path):每次字母轉換的成本均為 11,從起點到終點的最少轉換次數即為圖上的最短路徑長度。
  3. Bucket 索引優化建圖:利用雜湊表(Hash Table / Dictionary)將單字依「替換單一字母為底線 _」的模式分桶,避免兩兩單字暴力比對的 O(V2L)O(V^2 L) 成本。

(a) 建立字梯圖

題目給定的 16 個單字中,POLE 重複出現兩次,在圖論中視為同一個頂點,因此共有 1515 個頂點:
FOOL, POOL, POLL, POLE, PALE, SALE, SAGE, COOL, FOUL, FOIL, FAIL, FALL, PALL, POPE, PAGE。

相鄰關係(Adjacency List)

比較每對單字的四個字元位置,恰好一個位置不同即相鄰(共 1919 條無向邊):

  • FOOL:POOL, COOL, FOUL, FOIL
  • POOL:FOOL, POLL, COOL
  • POLL:POOL, POLE, PALL
  • POLE:POLL, PALE, POPE
  • PALE:POLE, PALL, SALE, PAGE
  • SALE:PALE, SAGE
  • SAGE:SALE, PAGE
  • COOL:FOOL, POOL
  • FOUL:FOOL, FOIL
  • FOIL:FOOL, FOUL, FAIL
  • FAIL:FOIL, FALL
  • FALL:FAIL, PALL
  • PALL:FALL, POLL, PALE
  • POPE:POLE
  • PAGE:PALE, SAGE

將上列相鄰關係中的每一對單字以一條無向邊相連,即得字梯圖(例如 FOOL、POOL、COOL 三者兩兩相鄰;PALE 同時與 POLE、PALL、SALE、PAGE 相鄰)。

從 FOOL 到 SAGE 的最短路徑共有 66 條邊(66 次轉換),例如題目範例路徑:
FOOL→POOL→POLL→POLE→PALE→SALE→SAGE\text{FOOL} \to \text{POOL} \to \text{POLL} \to \text{POLE} \to \text{PALE} \to \text{SALE} \to \text{SAGE}
(另有 POLE→PALE→PAGE→SAGE\text{POLE} \to \text{PALE} \to \text{PAGE} \to \text{SAGE}、POLL→PALL→PALE→SALE→SAGE\text{POLL} \to \text{PALL} \to \text{PALE} \to \text{SALE} \to \text{SAGE} 與 POLL→PALL→PALE→PAGE→SAGE\text{POLL} \to \text{PALL} \to \text{PALE} \to \text{PAGE} \to \text{SAGE} 共 44 條長度同為 66 的最短路徑。)


(b) 搜尋最有效率的路徑與複雜度分析

適用演算法:廣度優先搜尋(Breadth-First Search, BFS)

本題的字梯圖為無權重圖(每一步轉換成本均為 11),最適合使用 BFS:

  • BFS 從起點 FOOL 出發,利用佇列(Queue)依距離由近而遠逐層擴展(第 00 層為 FOOL,第 11 層為距離 11 的鄰居,依此類推)。
  • 正確性與效率:BFS 第一次拜訪到終點 SAGE 時,所經過的路徑保證是邊數最少的最短路徑,一旦找到即可立即終止搜尋。

與其他搜尋演算法的比較

演算法能否保證第一次找到即為最短路徑比較分析
BFS是(無權重圖)逐層搜尋,找到終點即停,無需額外優先佇列維護成本,最有效率。
DFS(深度優先搜尋)否可能先深入錯誤或繞遠路的長分支,必須遍歷大量路徑或改用迭代加深(IDDFS)才能確認最短路徑,效率較差。
Dijkstra 演算法是專為「非負權重加權圖」設計,需使用最小堆積(Min-Heap / Priority Queue),在所有邊權重皆為 11 時反而多出 O(log⁡V)O(\log V) 的堆積操作開銷。

複雜度分析

令 VV 為圖的頂點數(不重複單字數),EE 為邊數,LL 為單字長度(本題 L=4L = 4):

  • BFS 搜尋時間複雜度:使用鄰接串列(Adjacency List)時,每個頂點與每條邊至多拜訪常數次,時間複雜度為
    O(V+E)O(V + E)
  • BFS 空間複雜度:需要佇列、拜訪紀錄(visited)與前驅節點紀錄(parent)以及圖的儲存空間,空間複雜度為
    O(V+E)O(V + E)
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題