109 年 國立成功大學電機工程學系碩士班丁組《資料結構》

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

第 1 題15 分

Three of the following statements are incorrect. Pick out and correct them.
(A) If T(n)=2T(n/2)+O(n)T(n)=2T(n/2)+O(n) and T(1)=O(1)T(1)=O(1), then T(n)=O(nlog⁡n)T(n)=O(n \log n).
(B) If T(n)=T(n/2)+O(n)T(n)=T(n/2)+O(n) and T(1)=O(1)T(1)=O(1), then T(n)=O(n)T(n)=O(n).
(C) n10+1.1n=O(n10)n^{10}+1.1^n=O(n^{10})
(D) 2n2+1.5nlog⁡3n=O(n2)2n^2+1.5n\log^3n=O(n^2)
(E) The number of odd degree nodes in an undirected graph must be odd.
(F) To obtain the breadth first search tree of a graph, we need to use a stack data structure.

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

這一題的完整詳解

錯誤敘述與更正

  • (C) n10+1.1n=O(n10)n^{10}+1.1^{n}=O(n^{10})

    • 1.1n1.1^{n} 為指數函數,漸近上界超過任何多項式,故 n10+1.1n=Θ(1.1n)≠O(n10)n^{10}+1.1^{n}= \Theta(1.1^{n})\not=O(n^{10})。
    • 更正:n10+1.1n=O(1.1n)n^{10}+1.1^{n}=O(1.1^{n})(亦可寫成 Ω(1.1n)\Omega(1.1^{n}))。
  • (E) 「無向圖中奇度數節點的個數必為奇數」

    • 手握定理(Handshaking Lemma)給出 ∑deg⁡(v)=2∣E∣\sum\deg(v)=2|E| 為偶數,因而奇度數節點的個數必為 偶數。
🔒

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

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

免費註冊

第 2 題10 分

Suppose that you are asked to code a C function subprogram to compute the Fibonacci sequence, which is recursively defined as Fn=Fn−1+Fn−2F_n = F_{n-1}+F_{n-2}, F0=F1=1F_0=F_1=1. In particular, you are asked to get the value of F500F_{500}. Will you code it as a recursive function or an iterative function? Why?

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

這一題的完整詳解

核心觀念

本題考查遞迴與迭代的效率比較,以及 Fibonacci 數列的遞迴定義:

Fn=Fn−1+Fn−2,F0=F1=1F_n=F_{n-1}+F_{n-2},\qquad F_0=F_1=1

直接依照數學定義撰寫的遞迴函式,會重複計算大量相同的子問題。例如計算 F5F_5 時,F3F_3 會在不同分支中被重複計算。

此外,F500F_{500} 的數值非常大,已超過 C 語言一般整數型別的表示範圍,因此必須使用大數表示法,不能直接以 int、long 或 unsigned long long 儲存。

解題方法

應採用迭代法,由小到大依序計算:

F0=1,F1=1F_0=1,\quad F_1=1

接著使用兩個變數保存前兩項:

F2=F1+F0F_2=F_1+F_0 F3=F2+F1F_3=F_2+F_1

一般而言,第 ii 次迭代計算:

Fi=Fi−1+Fi−2F_i=F_{i-1}+F_{i-2}

計算完成後,最後一個變數即為 F500F_{500}。

若使用可儲存大數的陣列,關鍵程式架構如下:

#define MAX_DIGITS 200

typedef struct {
    int digit[MAX_DIGITS];  // 低位數放在前面
    int length;
} BigInteger;

BigInteger add(BigInteger a, BigInteger b) {
    BigInteger c = { {0}, 0 };
    int carry = 0;
    int i;

    for (i = 0; i < a.length || i < b.length || carry; i++) {
        int sum = carry;

        if (i < a.length)
            sum += a.digit[i];

        if (i < b.length)
            sum += b.digit[i];

        c.digit[i] = sum % 10;
        carry = sum / 10;
    }

    c.length = i;
    return c;
}

BigInteger fibonacci(int n) {
    BigInteger a = {{1}, 1};  // F0
    BigInteger b = {{1}, 1};  // F1
    BigInteger c;
    int i;

    if (n == 0)
        return a;

    if (n == 1)
        return b;

    for (i = 2; i <= n; i++) {
        c = add(a, b);
        a = b;
        b = c;
    }

    return b;
}

因此,計算 F500F_{500} 時,會從 F0,F1F_0,F_1 開始,反覆進行 499 次大數加法,最後得到 F500F_{500}。

遞迴法與迭代法比較

直接遞迴寫法如下:

int fibonacci(int n) {
    if (n == 0 || n == 1)
        return 1;

    return fibonacci(n - 1) + fibonacci(n - 2);
}

其時間複雜度滿足:

T(n)=T(n−1)+T(n−2)+O(1)T(n)=T(n-1)+T(n-2)+O(1)

因此:

🔒

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

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

免費註冊

第 3 題15 分

The following array represents a complete binary tree. Adjust it to be a max heap by showing each step.
10 9 20 6 15 48 14 8 90 17
🖼️【此處有附圖,請對照原卷】

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 1 頁

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

這一題的完整詳解

核心觀念

完整二元樹以陣列儲存時,若採用 1 起始索引:

  • 節點 ii 的左子節點為 2i2i
  • 右子節點為 2i+12i+1
  • 最後一個非葉節點為 ⌊n/2⌋\lfloor n/2 \rfloor

最大堆積(max heap)須滿足:每個父節點值皆大於或等於其子節點值。

本題共有 n=10n=10 個元素,因此從第 ⌊10/2⌋=5\lfloor 10/2 \rfloor=5 個節點開始,反向執行下沉調整(sift-down)。

解題方法

原始陣列為:

[10, 9, 20, 6, 15, 48, 14, 8, 90, 17][10,\ 9,\ 20,\ 6,\ 15,\ 48,\ 14,\ 8,\ 90,\ 17]

調整第 5 個節點

第 5 個節點為 1515,其子節點為第 10 個節點 1717。

因為 17>1517>15,交換兩者:

[10, 9, 20, 6, 17, 48, 14, 8, 90, 15][10,\ 9,\ 20,\ 6,\ 17,\ 48,\ 14,\ 8,\ 90,\ 15]

調整第 4 個節點

第 4 個節點為 66,子節點為 88 與 9090,取較大者 9090 交換:

[10, 9, 20, 90, 17, 48, 14, 8, 6, 15][10,\ 9,\ 20,\ 90,\ 17,\ 48,\ 14,\ 8,\ 6,\ 15]

調整第 3 個節點

第 3 個節點為 2020,子節點為 4848 與 1414,取較大者 4848 交換:

[10, 9, 48, 90, 17, 20, 14, 8, 6, 15][10,\ 9,\ 48,\ 90,\ 17,\ 20,\ 14,\ 8,\ 6,\ 15]

調整第 2 個節點

第 2 個節點為 99,子節點為 9090 與 1717,取較大者 9090 交換:

[10, 90, 48, 9, 17, 20, 14, 8, 6, 15][10,\ 90,\ 48,\ 9,\ 17,\ 20,\ 14,\ 8,\ 6,\ 15]

交換後,第 4 個節點為 99,其子節點為 88 與 66,已符合最大堆積條件。

調整第 1 個節點

第 1 個節點為 1010,子節點為 9090 與 4848,取較大者 9090 交換:

[90, 10, 48, 9, 17, 20, 14, 8, 6, 15][90,\ 10,\ 48,\ 9,\ 17,\ 20,\ 14,\ 8,\ 6,\ 15]

此時第 2 個節點為 1010,子節點為 99 與 1717,取較大者 1717 交換:

[90, 17, 48, 9, 10, 20, 14, 8, 6, 15][90,\ 17,\ 48,\ 9,\ 10,\ 20,\ 14,\ 8,\ 6,\ 15]
🔒

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

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

免費註冊

第 4 題10 分

(a) Give conditions under which quick sort algorithm has the worst case behavior in terms of time complexity. Explain your answer.
(b) Which sorting algorithm(s) is (are) stable, bubble sort, quick sort, merge sort, heap sort?

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

這一題的完整詳解

核心觀念

本題考查兩個重點:

  1. Quick sort 的最壞時間複雜度與 partition 後的子問題大小有關。
  2. Sorting algorithm 的穩定性(stable sorting)。

若排序前有兩筆資料的排序鍵相同,穩定排序會保留它們原本的相對順序。


(a) Quick sort 最壞情況

解題方法

Quick sort 每次選定一個 pivot,將資料分成:

  • pivot 左側:小於 pivot 的元素
  • pivot
  • pivot 右側:大於 pivot 的元素

理想情況是每次平均分割,遞迴式為:

T(n)=2T(n2)+Θ(n)T(n)=2T\left(\frac{n}{2}\right)+\Theta(n)

因此時間複雜度為:

Θ(nlog⁡n)\Theta(n\log n)

最壞情況發生在每次 partition 都極度不平衡,使其中一個子陣列大小為 00,另一個大小為 n−1n-1。此時:

T(n)=T(n−1)+Θ(n)T(n)=T(n-1)+\Theta(n)

展開可得:

T(n)=T(n−1)+cn=T(n−2)+c(n−1)+cn=⋯=T(1)+c(2+3+⋯+n)\begin{aligned} T(n) &=T(n-1)+cn\\ &=T(n-2)+c(n-1)+cn\\ &=\cdots\\ &=T(1)+c(2+3+\cdots+n) \end{aligned}

而:

2+3+⋯+n=Θ(n2)2+3+\cdots+n=\Theta(n^2)

所以:

T(n)=Θ(n2)T(n)=\Theta(n^2)

造成最壞情況的條件

當每次選出的 pivot 都是目前子陣列中的最小值或最大值時,partition 會產生大小為 00 與 n−1n-1 的兩個子問題。

典型例子如下:

  • 資料已經遞增排列,而每次選第一個元素作為 pivot。
  • 資料已經遞增排列,而每次選最後一個元素作為 pivot。
  • 資料已經遞減排列,而 pivot 固定選在開頭或結尾。
  • 資料排列方式使固定的 pivot 選擇規則每次都選到極端值。
  • 即使資料不是已排序,只要 pivot 在每一層遞迴都接近最小值或最大值,也會產生相同結果。

例如資料為:

1,2,3,4,5,61,2,3,4,5,6

若每次選第一個元素 11 作為 pivot,則每次分割結果為:

0與50 \quad\text{與}\quad 5

接著依序形成:

0+5,0+4,0+3,…0+5,\quad 0+4,\quad 0+3,\quad \ldots

partition 每層仍需掃描目前的資料,因此總成本為:

n+(n−1)+(n−2)+⋯+2=Θ(n2)n+(n-1)+(n-2)+\cdots+2=\Theta(n^2)

【答案】

Quick sort 的最壞情況發生在每次 partition 都產生極度不平衡的分割,即一邊有 00 個元素,另一邊有 n−1n-1 個元素。此時遞迴式為:

T(n)=T(n−1)+Θ(n)T(n)=T(n-1)+\Theta(n)

因此最壞時間複雜度為:

Θ(n2)\boxed{\Theta(n^2)}

(b) 穩定的排序演算法

判斷原則

判斷穩定性時,觀察排序過程是否可能交換兩筆「排序鍵相同、但原始順序不同」的資料。

標準實作下:

  • Bubble sort:穩定
  • Quick sort:不穩定
  • Merge sort:穩定
  • Heap sort:不穩定

1. Bubble sort:穩定

Bubble sort 通常只在相鄰元素符合:

A[j]>A[j+1]A[j]>A[j+1]

時交換它們。

若兩個元素的鍵值相等,因為不使用 A[j]≥A[j+1]A[j]\ge A[j+1] 作為交換條件,所以相等元素不會互換位置。

因此標準 Bubble sort 具有穩定性。

結論:正確,Bubble sort 是穩定排序。


2. Quick sort:不穩定

Quick sort 的 partition 可能將距離很遠的元素直接交換。即使兩筆資料的排序鍵相同,也可能因為 partition 的交換而改變相對順序。

例如資料為:

(2,A), (1,B), (2,C)(2,A),\ (1,B),\ (2,C)

其中 AA 與 CC 的排序鍵都為 22。partition 過程中的非相鄰交換可能使 CC 排到 AA 前面,導致相對順序改變。

🔒

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

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

免費註冊

第 5 題15 分

Let G=(V,E)G = (V, E) be a weighted undirected graph with any two vertices connected by at most one edge and HH be a subgraph of GG obtained by deleting an edge eie_i from GG, i.e., H=(V,E−{ei})H = (V, E - \{e_i\}). True or false? Explain if false.
(a) Minimal cost spanning tree of GG is unique.
(b) The path from vertex AA to vertex BB on a minimal cost spanning tree of GG is a shortest path from AA to BB.
(c) If the total cost of minimal cost spanning tree of HH is greater than that of GG, then eie_i must be an edge in any minimal cost spanning tree of GG.

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

這一題的完整詳解

核心觀念

本題考查最小成本生成樹(Minimum Spanning Tree, MST)的定義與性質:

  • 生成樹必須包含所有頂點、無環且有 ∣V∣−1|V|-1 條邊。
  • 最小成本生成樹是所有生成樹中邊權總和最小者。
  • MST 不一定唯一;只有在邊權皆相異等條件下,才能保證唯一。
  • MST 上兩點間的路徑只保證屬於某棵最小生成樹,不保證是全圖中的最短路徑。
  • 若刪除某條邊後,最小生成樹成本上升,表示原本所有最小生成樹都必須使用該邊。

解題方法

逐一檢驗各敘述,對錯誤敘述提供反例,對正確敘述利用生成樹集合的包含關係證明。


(a) Minimal cost spanning tree of GG is unique.

錯誤。

題目沒有給定所有邊的權重皆相異,因此最小生成樹不一定唯一。

例如,考慮三角形圖:

V={A,B,C}V=\{A,B,C\}

且三條邊權重皆為 11:

w(AB)=w(BC)=w(AC)=1w(AB)=w(BC)=w(AC)=1

任取其中兩條邊即可形成生成樹,而且總成本皆為

1+1=21+1=2

因此共有三棵最小生成樹,並非唯一。

最小生成樹唯一的充分條件之一,是圖中所有邊的權重皆相異。


(b) The path from vertex AA to vertex BB on a minimal cost spanning tree of GG is a shortest path from AA to BB.

錯誤。

MST 最小化的是「涵蓋所有頂點的總成本」,而最短路徑問題最小化的是「兩個指定頂點之間的路徑成本」。兩者的最佳化目標不同。

反例如下:

w(AB)=1,w(BC)=1,w(AC)=1.5w(AB)=1,\qquad w(BC)=1,\qquad w(AC)=1.5

考慮以 ABAB 與 BCBC 組成的生成樹,其總成本為

1+1=21+1=2

而其他生成樹的成本為

1+1.5=2.51+1.5=2.5

所以 ABAB 與 BCBC 組成唯一的 MST。在這棵 MST 上,從 AA 到 CC 的路徑為

A→B→CA\to B\to C

其成本為

1+1=21+1=2

但原圖中存在直接邊 ACAC,成本為

1.5<21.5<2

因此,MST 上的 AA 到 CC 路徑不是原圖中的最短路徑。

MST 路徑具有的正確性質是:對於 MST 上路徑中的任意兩點,該路徑上的最大權重邊具有特定的切割/環路性質;但不能因此推出整條路徑的總成本最短。


(c) If the total cost of minimal cost spanning tree of HH is greater than that of GG, then eie_i must be an edge in any minimal cost spanning tree of GG.

🔒

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

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

免費註冊

第 6 題20 分

Consider a sequence of keys: 27, 49, 17, 20, 61, 23, 92, 33, 77, 11, 31.
(a) Draw, step by step (showing clearly the type of rotation used), the result of inserting these keys into an empty AVL tree.
(b) Write down the inorder sequence of the AVL tree in (a).

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

這一題的完整詳解

(a) AVL 插入過程與旋轉

插入次序變化說明旋轉類型
27成為根節點–
4949 為 27 的右子–
1717 為 27 的左子–
2020 為 17 的右子;平衡因子仍在 [−1,1][-1,1]–
6161 為 49 的右子;仍平衡–
2323 為 20 的右子,導致節點 17 平衡因子 −2-2(右‑右) → 左旋於 17左旋 (LL)
9292 為 61 的右子,導致節點 49 平衡因子 −2-2(右‑右) → 左旋於 49左旋 (LL)
3333 為 49 的左子;仍平衡–
7777 為 92 的左子;仍平衡–
1111 為 17 的左子;仍平衡–
3131 為 33 的左子,導致節點 49 平衡因子 +2+2(左‑左) → 右旋於 49右旋 (RR)

最終 AVL 樹結構(簡易示意)

🔒

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

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

免費註冊

其他考古題