108 年 國立成功大學資訊管理研究所乙組《資料結構》

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

第 1 題10 分

Show that ∑i=1ni3=(∑i=1ni)2\sum_{i=1}^{n} i^3 = (\sum_{i=1}^{n} i)^2.

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

這一題的完整詳解

核心觀念

本題考查立方和公式:

∑i=1ni3=(∑i=1ni)2\sum_{i=1}^{n} i^3 = \left(\sum_{i=1}^{n} i\right)^2

右側使用等差級數公式:

∑i=1ni=n(n+1)2\sum_{i=1}^{n} i=\frac{n(n+1)}{2}

因此只要證明:

∑i=1ni3=n2(n+1)24\sum_{i=1}^{n} i^3=\frac{n^2(n+1)^2}{4}

即可完成證明。


解題方法:數學歸納法

令命題 P(n)P(n) 為:

∑i=1ni3=(∑i=1ni)2\sum_{i=1}^{n} i^3 = \left(\sum_{i=1}^{n} i\right)^2

第一步:驗證 n=1n=1

當 n=1n=1 時,

∑i=11i3=13=1\sum_{i=1}^{1}i^3=1^3=1

且

(∑i=11i)2=12=1\left(\sum_{i=1}^{1}i\right)^2=1^2=1

左右兩側相等,因此 P(1)P(1) 成立。

第二步:歸納假設

假設當 n=kn=k 時命題成立,即:

∑i=1ki3=(∑i=1ki)2=(k(k+1)2)2\sum_{i=1}^{k}i^3 = \left(\sum_{i=1}^{k}i\right)^2 = \left(\frac{k(k+1)}{2}\right)^2

第三步:證明 n=k+1n=k+1 時成立

考慮左側:

∑i=1k+1i3=∑i=1ki3+(k+1)3\sum_{i=1}^{k+1}i^3 = \sum_{i=1}^{k}i^3+(k+1)^3

套用歸納假設:

=(k(k+1)2)2+(k+1)3= \left(\frac{k(k+1)}{2}\right)^2+(k+1)^3

提出 (k+1)2(k+1)^2:

=k2(k+1)24+(k+1)3= \frac{k^2(k+1)^2}{4}+(k+1)^3 =(k+1)24[k2+4(k+1)]= \frac{(k+1)^2}{4}\left[k^2+4(k+1)\right]

整理括號內的式子:

k2+4k+4=(k+2)2k^2+4k+4=(k+2)^2

所以:

∑i=1k+1i3=(k+1)2(k+2)24\sum_{i=1}^{k+1}i^3 = \frac{(k+1)^2(k+2)^2}{4}

另一方面,

∑i=1k+1i=(k+1)(k+2)2\sum_{i=1}^{k+1}i = \frac{(k+1)(k+2)}{2}

因此:

(∑i=1k+1i)2=((k+1)(k+2)2)2=(k+1)2(k+2)24\left(\sum_{i=1}^{k+1}i\right)^2 = \left(\frac{(k+1)(k+2)}{2}\right)^2 = \frac{(k+1)^2(k+2)^2}{4}

故:

🔒

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

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

免費註冊

第 2 題5 分

For any given positive integers x and n, write a program that uses the minimum number of multiplications to calculate xnx^n (5%), and justify your answer. (5%)

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

這一題的完整詳解

核心觀念

本題考的是「最少乘法次數的冪次計算」,核心工具是加法鏈(addition chain)。

若要計算 xnx^n,先令:

a0=1a_0=1

每做一次乘法,只能將兩個已經計算出的次方相乘:

xai=xaj⋅xak=xaj+akx^{a_i}=x^{a_j}\cdot x^{a_k}=x^{a_j+a_k}

因此,指數必須形成:

1=a0<a1<a2<⋯<ar=n1=a_0<a_1<a_2<\cdots<a_r=n

且每個 aia_i 都能表示為前面兩個指數之和:

ai=aj+ak,j,k<ia_i=a_j+a_k,\qquad j,k<i

這種序列稱為加法鏈,而乘法次數就是 rr。因此,題目要求的是找出從 11 到 nn 的最短加法鏈。

例如:

1,2,3,6,12,151,2,3,6,12,15

對應計算:

x2=x⋅xx^2=x\cdot x x3=x2⋅xx^3=x^2\cdot x x6=x3⋅x3x^6=x^3\cdot x^3 x12=x6⋅x6x^{12}=x^6\cdot x^6 x15=x12⋅x3x^{15}=x^{12}\cdot x^3

共需 55 次乘法。


解題方法:以廣度優先搜尋找最短加法鏈

將每一條加法鏈視為搜尋狀態,從最短長度開始逐層搜尋:

  1. 初始鏈為 (1)(1),代表已知 x1=xx^1=x。
  2. 從目前鏈中的任意兩個指數 aj,aka_j,a_k 產生新指數 aj+aka_j+a_k。
  3. 新指數必須大於目前最後一項,且不超過 nn。
  4. 使用廣度優先搜尋(BFS),第一條抵達 nn 的鏈必定是最短鏈。
  5. 依照該鏈實際進行乘法。
🔒

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

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

免費註冊

第 3 題15 分

Let an integer mm be expressed as a binary number bkbk−1…b1b0b_k b_{k-1} \dots b_1 b_0 for some integer kk, and let the remainder of aa divided by nn be denoted as a(modn)a \pmod n. For example, integer 10 can be expressed as binary number 1010 for k=3k=3, and 10(mod8)=210 \pmod 8 = 2.

(1) Explain why the following algorithm can be used to calculate am(modn)a^m \pmod n. (10%)

f ← 1
For i ← k down to 0
  f ← (f × f) mod n
  If bᵢ = 1 Then f ← (f × a) mod n
Next i
Return f

(2) Use the above algorithm to calculate 7560(mod561)7^{560} \pmod{561}. (5%)

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

這一題的完整詳解

(1) 為何演算法正確

二進位表示 m=bkbk−1…b0m=b_kb_{k-1}\dots b_0,從最高位 bkb_k 到最低位 b0b_0 依序處理。
在第 ii 次迴圈結束時保有不變式

f≡a2 bkbk−1…bi(modn)f \equiv a^{\,b_kb_{k-1}\dots b_i}_2 \pmod n

即 ff 等於已讀入的位元組成的指數的 aa 次方。

  • 先以 f←f2(modn)f\leftarrow f^2\pmod n:把已計算的指數左移一位(乘以 22)。
  • 若當前位元 bi=1b_i=1,再乘以 aa(相當於在二進位中加上 11)。

如此下去,遍歷完所有位元後,指數恰為 bkbk−1…b0b_kb_{k-1}\dots b_0,故最終 f≡am(modn)f\equiv a^m\pmod n。
演算法每一步皆只做模乘,時間 O(k)O(k)(k+1k+1 次平方與至多 k+1k+1 次乘 aa)。


(2) 計算 7560(mod561)7^{560}\pmod{561}

560 的二進位為

56010=10001100002(k=9,  b9=1,b5=1,b4=1,其餘=0)560_{10}=1000110000_2\quad(k=9,\;b_9=1,b_5=1,b_4=1,\text{其餘}=0)

依演算法逐步取模:

| ii | 前一步 ff | f2(mod561)f^2\pmod{561} | bib_i | 若 bi=1b_i=1 再乘 77 | 新 ff |

🔒

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

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

免費註冊

第 4 題15 分

(1) Distinguish open hashing from closed hashing. (5%)
(2) Given input {25, 33, 64, 75, 24, 41} and a hash function h(x)=x(mod8)h(x) = x \pmod 8, show the resulting open hash table, closed hash table using linear probing, and closed hash table using quadratic probing after each insertion. (10%)

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

這一題的完整詳解

核心觀念

本題考查雜湊(hashing)的碰撞處理方式:

  • 雜湊函數:h(x)=x mod 8h(x)=x\bmod 8
  • 雜湊表大小:88,索引為 00 至 77
  • 開放雜湊(open hashing):碰撞時,以鏈結串列等外部結構儲存多筆資料。
  • 封閉雜湊(closed hashing):所有資料直接存放於雜湊表內,碰撞時依探測(probing)規則尋找其他空槽位。

本題假設:

  1. 雜湊表大小為 88。
  2. 開放雜湊採用鏈結串列。
  3. 封閉雜湊的二次探測公式為
    hi(x)=(h(x)+i2) mod 8,i=0,1,2,…h_i(x)=(h(x)+i^2)\bmod 8,\qquad i=0,1,2,\ldots
  4. 鏈結串列依插入順序排列。

解題方法

先計算每個鍵值的雜湊位置:

h(25)=25 mod 8=1h(33)=33 mod 8=1h(64)=64 mod 8=0h(75)=75 mod 8=3h(24)=24 mod 8=0h(41)=41 mod 8=1\begin{aligned} h(25)&=25\bmod 8=1\\ h(33)&=33\bmod 8=1\\ h(64)&=64\bmod 8=0\\ h(75)&=75\bmod 8=3\\ h(24)&=24\bmod 8=0\\ h(41)&=41\bmod 8=1 \end{aligned}

因此鍵值與初始位置如下:

鍵值h(x)h(x)
251
331
640
753
240
411

(1) Open hashing 與 closed hashing 的區別

比較項目Open hashingClosed hashing
中文名稱開放雜湊封閉雜湊
常見別名Separate chainingOpen addressing
儲存位置碰撞資料可存於表外的鏈結串列或其他結構所有資料必須存於雜湊表槽位內
碰撞處理將相同雜湊位置的資料串接起來依線性探測、二次探測等規則尋找空槽
負載因子通常可大於 11必須小於 11,否則沒有空槽可放置資料
刪除操作直接從鏈結串列刪除通常需使用刪除標記,不能直接設為空槽
主要問題鏈結串列過長,查找時間增加可能產生 clustering(群聚)問題

(2) Open hash table

開放雜湊直接將相同雜湊位置的資料放入同一個鏈結串列。

插入後01234567
插入 25∅\varnothing25∅\varnothing∅\varnothing∅\varnothing∅\varnothing∅\varnothing∅\varnothing
插入 33∅\varnothing25 → 33∅\varnothing∅\varnothing∅\varnothing∅\varnothing∅\varnothing∅\varnothing
插入 646425 → 33∅\varnothing∅\varnothing∅\varnothing∅\varnothing∅\varnothing∅\varnothing
插入 756425 → 33∅\varnothing75∅\varnothing∅\varnothing∅\varnothing∅\varnothing
插入 2464 → 2425 → 33∅\varnothing75∅\varnothing∅\varnothing∅\varnothing∅\varnothing
插入 4164 → 2425 → 33 → 41∅\varnothing75∅\varnothing∅\varnothing∅\varnothing∅\varnothing

最終開放雜湊表為:

索引內容064→24125→33→412∅3754∅5∅6∅7∅\begin{array}{c|c} \text{索引}&\text{內容}\\ \hline 0&64\to24\\ 1&25\to33\to41\\ 2&\varnothing\\ 3&75\\ 4&\varnothing\\ 5&\varnothing\\ 6&\varnothing\\ 7&\varnothing \end{array}

Closed hash table:linear probing

線性探測公式為:

hi(x)=(h(x)+i) mod 8,i=0,1,2,…h_i(x)=(h(x)+i)\bmod 8,\qquad i=0,1,2,\ldots

發生碰撞時,依序檢查下一個槽位。

逐次插入

  1. 插入 2525
h(25)=1h(25)=1

位置 11 為空,放入索引 11。

  1. 插入 3333
h(33)=1h(33)=1

索引 11 已有 2525,依序探測索引 22,放入索引 22。

  1. 插入 6464
h(64)=0h(64)=0

索引 00 為空,放入索引 00。

  1. 插入 7575
h(75)=3h(75)=3

索引 33 為空,放入索引 33。

  1. 插入 2424
h(24)=0h(24)=0

索引 00 已滿,依序檢查:

0→1→2→3→40\to1\to2\to3\to4

索引 44 為空,放入索引 44。

  1. 插入 4141
h(41)=1h(41)=1

依序檢查:

1→2→3→4→51\to2\to3\to4\to5
🔒

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

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

免費註冊

第 5 題32 分

Circle T or F for each of the following statements to indicate whether the statement is true or false, respectively. If the statement is correct, briefly state why. If the statement is wrong, explain why or give a counter example. Answers WITHOUT reasons will get at most 1 point.

(a) [4%] (T, F) Given nn integers uniformly distributed in the range [−n3,n2][-n^3, n^2]. If we use Bucket Sort to sort these nn integers, it takes O(n3)O(n^3) time, because the range is O(n3)O(n^3).

(b) [4%] (T, F) Given n<1020n < 10^{20} integers whose values are uniformly distributed in the range [−n3,n2][-n^3, n^2]. If we use Counting Sort to sort these nn integers, it takes O(1)O(1) time.

(c) [4%] (T, F) The tree in Fig. 1 is a min-heap of a completed binary tree of 5 elements.
🖼️【此處有附圖,請對照原卷】

(d) [4%] (T, F) In a complete undirected graph KnK_n with nn nodes {1,2,...,n}\{1,2,...,n\}, let cij>0c_{ij} > 0 represent the length of any edge (i,j)(i,j). We can use Dijkstra's algorithm to find a shortest simple path from node 3 to node 4 that CANNOT pass through nodes 1 and 2, but MUST pass through all other nodes.

(e) [4%] (T, F) In a complete undirected graph Kn=(N,A)K_n = (N,A) of ∣N∣=n|N|=n nodes and ∣A∣=m|A|=m arcs, let cij>0c_{ij} > 0 represent the length of any edge (i,j)∈A(i, j) \in A, and Wi=∑(i,j)∈AcijW_i = \sum_{(i,j)\in A} c_{ij} represent the sum of lengths for all arcs (i,j)(i, j) adjacent to node ii. To calculate min⁡i∈N{Wi}\min_{i \in N} \{W_i\}, it takes O(n2)O(n^2) time.

(f) [4%] (T, F) T(n)=T(n−1)+n,T(1)=1T(n)=T(n-1)+n, T(1)=1, then T(n)=O(n3)T(n)=O(n^3).

(g) [4%] (T, F) Given a min-heap of nn values, to find the maximum of these nn values takes O(log⁡n)O(\log n) time.

(h) [4%] (T, F) A binary search tree of n≥5n \ge 5 numbers can NEVER be a min-heap.

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

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

這一題的完整詳解

核心觀念

本題考查:

  • Bucket Sort、Counting Sort 的時間複雜度
  • Complete Binary Tree 與 Min-Heap 定義
  • Dijkstra 演算法適用條件
  • 完全圖的邊數與計算複雜度
  • 遞迴式求解
  • Min-Heap 找最大值
  • Binary Search Tree 與 Min-Heap 的性質衝突

(a)

判斷:F

Bucket Sort 的複雜度不單純由資料值域範圍決定。若使用 kk 個 bucket,其複雜度通常表示為:

O(n+k)O(n+k)

題目中的整數範圍為 [−n3,n2][-n^3,n^2],範圍大小為:

n3+n2+1=O(n3)n^3+n^2+1=O(n^3)

若每一個可能值都配置一個 bucket,確實可能需要 O(n3)O(n^3) 空間與初始化時間;但 Bucket Sort 可以將值域劃分成 O(n)O(n) 個區間。由於資料均勻分布,每個 bucket 的平均資料量為常數,排序時間可達平均 O(n)O(n)。

因此,不能直接斷言一定需要 O(n3)O(n^3) 時間。

解題技巧:
看到「值域是 O(n3)O(n^3),所以 Bucket Sort 一定是 O(n3)O(n^3)」通常是錯誤推論;必須先確認 bucket 數量及 bucket 內排序方式。


(b)

判斷:F

Counting Sort 的時間複雜度為:

O(n+k)O(n+k)

其中 kk 為資料值域大小。本題:

k=n3+n2+1=O(n3)k=n^3+n^2+1=O(n^3)

所以複雜度為:

O(n+n3)=O(n3)O(n+n^3)=O(n^3)

題目給出的 n<1020n<10^{20} 只是限制輸入規模,並不表示 Counting Sort 實際上不需要處理資料,也不會消除其對 nn 的依賴。僅讀取 nn 個輸入就至少需要 Ω(n)\Omega(n) 時間。

因此不可能合理地判定為 O(1)O(1)。

解題技巧:
Counting Sort 的關鍵不是只看資料筆數 nn,還要看值域大小 kk。值域很大時,Counting Sort 可能失去效率。


(c)

判斷:目前資訊不足,無法唯一判定

目前提供的掃描圖只有選擇題第 1 至第 7 題,未包含題目所引用的 Fig. 1,因此無法確認該樹的節點排列與節點值。

判斷一棵樹是否為 5 個元素的 Min-Heap,必須同時符合:

  1. 樹形是 complete binary tree。
  2. 每個父節點的值小於或等於其子節點:
key(parent)≤key(child)key(parent)\le key(child)

若 Fig. 1 同時符合上述兩項,答案為 T;只要樹形不完整或有任一父節點大於子節點,答案即為 F。


(d)

判斷:F

Dijkstra 演算法適用於:

  • 邊權重非負的圖
  • 求單一起點到其他節點的最短路徑

本題要求的路徑具有額外限制:

  • 從節點 33 出發,到節點 44
  • 不得經過節點 1,21,2
  • 必須經過所有其他節點
  • 路徑必須是 simple path

這已不是一般的單源最短路徑問題。Dijkstra 只會尋找總長度最短的路徑,不會自動保證「必須經過所有指定節點」。

例如,Dijkstra 可能找到:

3→5→43\rightarrow 5\rightarrow 4

但該路徑沒有經過其他所有節點,因而不符合題目要求。

解題技巧:
只要題目出現「必須經過所有節點」或「指定順序經過節點」,就不能直接套用標準 Dijkstra。


(e)

判斷:T

完全無向圖 KnK_n 的邊數為:

🔒

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

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

免費註冊

第 6 題18 分

Given a social simple network G=(N,A) for n=|N| persons. Let node i be person i and dijd_{ij} be the number of "likes" given to j from i (so, dijd_{ij} may not equal to djid_{ji}). D is a given positive integer as a threshold. We construct a directed arc (i,j) if dij≥Dd_{ij} \ge D, and degiout\text{deg}_{i}^{\text{out}} and degiin\text{deg}_{i}^{\text{in}} are the outdegree and indegree of node i. Note that it is not necessarily both (i,j) and (j,i) exist at the same time. Suppose there are m=|A| arcs in G, where m<n(n-1). Person i and j are direct friends if both (i,j) and (j,i) exist, and are potential friends if they are NOT friends but still connect to each other by directed paths in G.

(a) [6%] To identify all the direct and potential friends for a person k, can you do this in O(m) time? Why or why not?

(b) [6%] Let Gi=∑(i,j)∈Adij/degioutG_i = \sum_{(i,j)\in A} d_{ij} / \text{deg}_{i}^{\text{out}} and Ri=∑(j,i)∈Adji/degiinR_i = \sum_{(j,i)\in A} d_{ji} / \text{deg}_{i}^{\text{in}} represent the average Giver and Receiver index of person i. Can you calculate GiG_i and RiR_i for all i∈Ni \in N within O(m)O(m) or better time? Why or why not?

(c) [6%] Suppose we have already calculated the GiG_i and RiR_i for all i∈Ni \in N. Let Fi=Ri−GiF_i = R_i - G_i represent an average Fortune index. Suppose degiout\text{deg}_{i}^{\text{out}} and degiin\text{deg}_{i}^{\text{in}} for all i∈Ni \in N are also given. For a person k, we want to find the most fortunate person among his (direct and potential) friends and friends of his friends (i.e., within 2 arcs to or from node k). Can you identify this person in O(1) time, why or why not?

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

這一題的完整詳解

核心觀念

本題綜合考察:

  1. 有向圖的可達性:利用 DFS 或 BFS 找出由節點 kk 可達的節點。
  2. 圖的時間複雜度:鄰接串列表示法下,遍歷圖的複雜度為 O(n+m)O(n+m);若只處理由 kk 實際可達的部分,則可進一步分析為 O(m)O(m)。
  3. 邊集合的彙總計算:所有 GiG_i、RiR_i 可藉由掃描每條弧一次完成。
  4. 局部鄰域查詢:尋找距離 kk 不超過 22 的節點,必須檢查相關鄰接節點,通常不能在 O(1)O(1) 時間完成。

其中:

  • 直接朋友:(i,j)∈A(i,j)\in A 且 (j,i)∈A(j,i)\in A。
  • 潛在朋友:不是直接朋友,但可透過有向路徑互相連接。
  • GiG_i 為節點 ii 對外連出的平均喜歡數。
  • RiR_i 為節點 ii 接收到的平均喜歡數。
  • Fi=Ri−GiF_i=R_i-G_i 為 Fortune index。

(a)找出人物 kk 的直接朋友與潛在朋友

解題方法

採用 DFS 或 BFS 遍歷圖。

若題意將「連接」理解為從 kk 出發的有向可達性,則:

  1. 從 kk 出發進行 DFS 或 BFS。
  2. 所有被搜尋到的節點,都是由 kk 可透過有向路徑到達的節點。
  3. 對每一個可達節點 jj:
    • 若 (k,j)(k,j) 與 (j,k)(j,k) 同時存在,則 jj 是直接朋友。
    • 否則,若存在有向路徑連接,則 jj 是潛在朋友。

若題意的「connect to each other」允許 kk 到 jj 或 jj 到 kk 任一方向,則可同時在原圖與反向圖上進行搜尋,仍只需掃描每條弧有限次,時間複雜度不變。

時間複雜度

使用鄰接串列時,DFS 或 BFS 的一般複雜度為:

O(n+m)O(n+m)

但本題只需找出與 kk 實際連通的節點。除了起點 kk 外,每一個被發現的節點至少需要透過一條弧進入,因此被訪問節點數至多為 m+1m+1。所以針對單一人物 kk,可將複雜度寫成:

O(m+1)=O(m)O(m+1)=O(m)

這裡假設圖以鄰接串列表示,且檢查反向弧可在 O(1)O(1) 平均時間完成,例如使用雜湊集合,或已建立反向鄰接表。

常見陷阱

若直接套用整張圖的 DFS 複雜度,會寫成 O(n+m)O(n+m)。這是一般圖遍歷的保守寫法;本題只查詢單一起點 kk,可利用「實際可達節點數受弧數 mm 限制」進一步寫成 O(m)O(m)。

小題判定

可以在 O(m)O(m) 時間內完成,前提是使用鄰接串列,並且不需逐一掃描所有未被 kk 到達的孤立節點。


(b)計算所有 GiG_i 與 RiR_i

公式

對每個節點 ii:

Gi=∑(i,j)∈Adijdeg⁡ioutG_i= \frac{\displaystyle\sum_{(i,j)\in A}d_{ij}} {\deg_i^{\mathrm{out}}} Ri=∑(j,i)∈Adjideg⁡iinR_i= \frac{\displaystyle\sum_{(j,i)\in A}d_{ji}} {\deg_i^{\mathrm{in}}}

解題方法

先為每個節點建立兩個累加值:

  • giverSum[i]\text{giverSum}[i]:累計所有由 ii 指向其他節點的 dijd_{ij}。
  • receiverSum[i]\text{receiverSum}[i]:累計所有指向 ii 的 djid_{ji}。

掃描每條弧 (i,j)(i,j) 時:

giverSum[i]+=dij\text{giverSum}[i]\mathrel{+}=d_{ij} receiverSum[j]+=dij\text{receiverSum}[j]\mathrel{+}=d_{ij}

掃描完所有弧後,再對每個節點計算:

Gi=giverSum[i]deg⁡ioutG_i=\frac{\text{giverSum}[i]}{\deg_i^{\mathrm{out}}} Ri=receiverSum[i]deg⁡iinR_i=\frac{\text{receiverSum}[i]}{\deg_i^{\mathrm{in}}}

時間複雜度

掃描所有 mm 條弧需要:

🔒

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

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

免費註冊

其他考古題