108 年 國立成功大學資訊管理研究所乙組《資料結構》
第 1 題10 分
Show that .
登入後即可作答並保存紀錄。
核心觀念
本題考查立方和公式:
右側使用等差級數公式:
因此只要證明:
即可完成證明。
解題方法:數學歸納法
令命題 為:
第一步:驗證
當 時,
且
左右兩側相等,因此 成立。
第二步:歸納假設
假設當 時命題成立,即:
第三步:證明 時成立
考慮左側:
套用歸納假設:
提出 :
整理括號內的式子:
所以:
另一方面,
因此:
故:
第 2 題5 分
For any given positive integers x and n, write a program that uses the minimum number of multiplications to calculate (5%), and justify your answer. (5%)
登入後即可作答並保存紀錄。
核心觀念
本題考的是「最少乘法次數的冪次計算」,核心工具是加法鏈(addition chain)。
若要計算 ,先令:
每做一次乘法,只能將兩個已經計算出的次方相乘:
因此,指數必須形成:
且每個 都能表示為前面兩個指數之和:
這種序列稱為加法鏈,而乘法次數就是 。因此,題目要求的是找出從 到 的最短加法鏈。
例如:
對應計算:
共需 次乘法。
解題方法:以廣度優先搜尋找最短加法鏈
將每一條加法鏈視為搜尋狀態,從最短長度開始逐層搜尋:
- 初始鏈為 ,代表已知 。
- 從目前鏈中的任意兩個指數 產生新指數 。
- 新指數必須大於目前最後一項,且不超過 。
- 使用廣度優先搜尋(BFS),第一條抵達 的鏈必定是最短鏈。
- 依照該鏈實際進行乘法。
第 3 題15 分
Let an integer be expressed as a binary number for some integer , and let the remainder of divided by be denoted as . For example, integer 10 can be expressed as binary number 1010 for , and .
(1) Explain why the following algorithm can be used to calculate . (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 . (5%)
登入後即可作答並保存紀錄。
(1) 為何演算法正確
二進位表示 ,從最高位 到最低位 依序處理。
在第 次迴圈結束時保有不變式
即 等於已讀入的位元組成的指數的 次方。
- 先以 :把已計算的指數左移一位(乘以 )。
- 若當前位元 ,再乘以 (相當於在二進位中加上 )。
如此下去,遍歷完所有位元後,指數恰為 ,故最終 。
演算法每一步皆只做模乘,時間 ( 次平方與至多 次乘 )。
(2) 計算
560 的二進位為
依演算法逐步取模:
| | 前一步 | | | 若 再乘 | 新 |
第 4 題15 分
(1) Distinguish open hashing from closed hashing. (5%)
(2) Given input {25, 33, 64, 75, 24, 41} and a hash function , show the resulting open hash table, closed hash table using linear probing, and closed hash table using quadratic probing after each insertion. (10%)
登入後即可作答並保存紀錄。
核心觀念
本題考查雜湊(hashing)的碰撞處理方式:
- 雜湊函數:
- 雜湊表大小:,索引為 至
- 開放雜湊(open hashing):碰撞時,以鏈結串列等外部結構儲存多筆資料。
- 封閉雜湊(closed hashing):所有資料直接存放於雜湊表內,碰撞時依探測(probing)規則尋找其他空槽位。
本題假設:
- 雜湊表大小為 。
- 開放雜湊採用鏈結串列。
- 封閉雜湊的二次探測公式為
- 鏈結串列依插入順序排列。
解題方法
先計算每個鍵值的雜湊位置:
因此鍵值與初始位置如下:
| 鍵值 | |
|---|---|
| 25 | 1 |
| 33 | 1 |
| 64 | 0 |
| 75 | 3 |
| 24 | 0 |
| 41 | 1 |
(1) Open hashing 與 closed hashing 的區別
| 比較項目 | Open hashing | Closed hashing |
|---|---|---|
| 中文名稱 | 開放雜湊 | 封閉雜湊 |
| 常見別名 | Separate chaining | Open addressing |
| 儲存位置 | 碰撞資料可存於表外的鏈結串列或其他結構 | 所有資料必須存於雜湊表槽位內 |
| 碰撞處理 | 將相同雜湊位置的資料串接起來 | 依線性探測、二次探測等規則尋找空槽 |
| 負載因子 | 通常可大於 | 必須小於 ,否則沒有空槽可放置資料 |
| 刪除操作 | 直接從鏈結串列刪除 | 通常需使用刪除標記,不能直接設為空槽 |
| 主要問題 | 鏈結串列過長,查找時間增加 | 可能產生 clustering(群聚)問題 |
(2) Open hash table
開放雜湊直接將相同雜湊位置的資料放入同一個鏈結串列。
| 插入後 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| 插入 25 | 25 | |||||||
| 插入 33 | 25 → 33 | |||||||
| 插入 64 | 64 | 25 → 33 | ||||||
| 插入 75 | 64 | 25 → 33 | 75 | |||||
| 插入 24 | 64 → 24 | 25 → 33 | 75 | |||||
| 插入 41 | 64 → 24 | 25 → 33 → 41 | 75 |
最終開放雜湊表為:
Closed hash table:linear probing
線性探測公式為:
發生碰撞時,依序檢查下一個槽位。
逐次插入
- 插入
位置 為空,放入索引 。
- 插入
索引 已有 ,依序探測索引 ,放入索引 。
- 插入
索引 為空,放入索引 。
- 插入
索引 為空,放入索引 。
- 插入
索引 已滿,依序檢查:
索引 為空,放入索引 。
- 插入
依序檢查:
第 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 integers uniformly distributed in the range . If we use Bucket Sort to sort these integers, it takes time, because the range is .
(b) [4%] (T, F) Given integers whose values are uniformly distributed in the range . If we use Counting Sort to sort these integers, it takes 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 with nodes , let represent the length of any edge . 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 of nodes and arcs, let represent the length of any edge , and represent the sum of lengths for all arcs adjacent to node . To calculate , it takes time.
(f) [4%] (T, F) , then .
(g) [4%] (T, F) Given a min-heap of values, to find the maximum of these values takes time.
(h) [4%] (T, F) A binary search tree of numbers can NEVER be a min-heap.
登入後即可作答並保存紀錄。
核心觀念
本題考查:
- Bucket Sort、Counting Sort 的時間複雜度
- Complete Binary Tree 與 Min-Heap 定義
- Dijkstra 演算法適用條件
- 完全圖的邊數與計算複雜度
- 遞迴式求解
- Min-Heap 找最大值
- Binary Search Tree 與 Min-Heap 的性質衝突
(a)
判斷:F
Bucket Sort 的複雜度不單純由資料值域範圍決定。若使用 個 bucket,其複雜度通常表示為:
題目中的整數範圍為 ,範圍大小為:
若每一個可能值都配置一個 bucket,確實可能需要 空間與初始化時間;但 Bucket Sort 可以將值域劃分成 個區間。由於資料均勻分布,每個 bucket 的平均資料量為常數,排序時間可達平均 。
因此,不能直接斷言一定需要 時間。
解題技巧:
看到「值域是 ,所以 Bucket Sort 一定是 」通常是錯誤推論;必須先確認 bucket 數量及 bucket 內排序方式。
(b)
判斷:F
Counting Sort 的時間複雜度為:
其中 為資料值域大小。本題:
所以複雜度為:
題目給出的 只是限制輸入規模,並不表示 Counting Sort 實際上不需要處理資料,也不會消除其對 的依賴。僅讀取 個輸入就至少需要 時間。
因此不可能合理地判定為 。
解題技巧:
Counting Sort 的關鍵不是只看資料筆數 ,還要看值域大小 。值域很大時,Counting Sort 可能失去效率。
(c)
判斷:目前資訊不足,無法唯一判定
目前提供的掃描圖只有選擇題第 1 至第 7 題,未包含題目所引用的 Fig. 1,因此無法確認該樹的節點排列與節點值。
判斷一棵樹是否為 5 個元素的 Min-Heap,必須同時符合:
- 樹形是 complete binary tree。
- 每個父節點的值小於或等於其子節點:
若 Fig. 1 同時符合上述兩項,答案為 T;只要樹形不完整或有任一父節點大於子節點,答案即為 F。
(d)
判斷:F
Dijkstra 演算法適用於:
- 邊權重非負的圖
- 求單一起點到其他節點的最短路徑
本題要求的路徑具有額外限制:
- 從節點 出發,到節點
- 不得經過節點
- 必須經過所有其他節點
- 路徑必須是 simple path
這已不是一般的單源最短路徑問題。Dijkstra 只會尋找總長度最短的路徑,不會自動保證「必須經過所有指定節點」。
例如,Dijkstra 可能找到:
但該路徑沒有經過其他所有節點,因而不符合題目要求。
解題技巧:
只要題目出現「必須經過所有節點」或「指定順序經過節點」,就不能直接套用標準 Dijkstra。
(e)
判斷:T
完全無向圖 的邊數為:
第 6 題18 分
Given a social simple network G=(N,A) for n=|N| persons. Let node i be person i and be the number of "likes" given to j from i (so, may not equal to ). D is a given positive integer as a threshold. We construct a directed arc (i,j) if , and and 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 and represent the average Giver and Receiver index of person i. Can you calculate and for all within or better time? Why or why not?
(c) [6%] Suppose we have already calculated the and for all . Let represent an average Fortune index. Suppose and for all 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?
登入後即可作答並保存紀錄。
核心觀念
本題綜合考察:
- 有向圖的可達性:利用 DFS 或 BFS 找出由節點 可達的節點。
- 圖的時間複雜度:鄰接串列表示法下,遍歷圖的複雜度為 ;若只處理由 實際可達的部分,則可進一步分析為 。
- 邊集合的彙總計算:所有 、 可藉由掃描每條弧一次完成。
- 局部鄰域查詢:尋找距離 不超過 的節點,必須檢查相關鄰接節點,通常不能在 時間完成。
其中:
- 直接朋友: 且 。
- 潛在朋友:不是直接朋友,但可透過有向路徑互相連接。
- 為節點 對外連出的平均喜歡數。
- 為節點 接收到的平均喜歡數。
- 為 Fortune index。
(a)找出人物 的直接朋友與潛在朋友
解題方法
採用 DFS 或 BFS 遍歷圖。
若題意將「連接」理解為從 出發的有向可達性,則:
- 從 出發進行 DFS 或 BFS。
- 所有被搜尋到的節點,都是由 可透過有向路徑到達的節點。
- 對每一個可達節點 :
- 若 與 同時存在,則 是直接朋友。
- 否則,若存在有向路徑連接,則 是潛在朋友。
若題意的「connect to each other」允許 到 或 到 任一方向,則可同時在原圖與反向圖上進行搜尋,仍只需掃描每條弧有限次,時間複雜度不變。
時間複雜度
使用鄰接串列時,DFS 或 BFS 的一般複雜度為:
但本題只需找出與 實際連通的節點。除了起點 外,每一個被發現的節點至少需要透過一條弧進入,因此被訪問節點數至多為 。所以針對單一人物 ,可將複雜度寫成:
這裡假設圖以鄰接串列表示,且檢查反向弧可在 平均時間完成,例如使用雜湊集合,或已建立反向鄰接表。
常見陷阱
若直接套用整張圖的 DFS 複雜度,會寫成 。這是一般圖遍歷的保守寫法;本題只查詢單一起點 ,可利用「實際可達節點數受弧數 限制」進一步寫成 。
小題判定
可以在 時間內完成,前提是使用鄰接串列,並且不需逐一掃描所有未被 到達的孤立節點。
(b)計算所有 與
公式
對每個節點 :
解題方法
先為每個節點建立兩個累加值:
- :累計所有由 指向其他節點的 。
- :累計所有指向 的 。
掃描每條弧 時:
掃描完所有弧後,再對每個節點計算:
時間複雜度
掃描所有 條弧需要: