115 年 國立中央大學資訊工程學系軟體工程碩士班《資料結構與演算法》
第 1 題
- Which of the following statements about Quicksort are true?
(A) In the worst case, Quicksort runs in time.
(B) Choosing the median as the pivot guarantees that Quicksort will run in time.
(C) Standard in-place Quicksort is a stable sorting algorithm.
(D) The expected running time of randomized Quicksort is .
(E) Quicksort can be implemented using only extra space on average.
登入後即可作答並保存紀錄。
核心觀念
本題考驗**快速排序法(Quicksort)**的各項核心理論特性,包含:
- 時間複雜度(Time Complexity):最壞情況(Worst-case)與隨機化下的期望值(Expected time)。
- 樞紐碼選擇策略(Pivot Selection Strategy):中位數(Median)選擇對遞迴時間複雜度的保證。
- 排序穩定度(Stability):原地(In-place)Partition 運算對相同鍵值元素相對順序的影響。
- 空間複雜度(Space Complexity):遞迴呼叫堆疊(Recursion Stack)空間的消耗。
解題方法
分析 Quicksort 時,可將演算法分為 Partition(分割) 與 Recursive Calls(遞迴呼叫) 兩部分:
-
遞迴關係式(Recurrence Relation):
設輸入資料量為 ,分割後兩側子陣列的大小分別為 與 (其中 ),則執行時間滿足:
-
最壞情況分析:
當分割極度不均勻(如 或 )時:
-
最佳與理想分割分析:
若每次分割均能平分陣列():
根據主定理(Master Theorem),此式解得 。 -
空間複雜度分析:
Quicksort 雖然是在原陣列進行交換(In-place),但系統必須維持遞迴呼叫堆疊(Recursion Stack)。堆疊深度取決於遞迴樹的高度。
選項分析
- (A) 正確
在最壞情況下(例如:輸入資料已排序或逆序,且每次皆選擇最邊緣元素作為 Pivot),分割結果為 與 個元素。遞迴關係式為 ,展開後得 。
第 2 題
- Which of the following statements about amortized analysis are true?
(A) Amortized analysis provides an upper bound on the average cost per operation over any sequence of operations, not assuming any probability distribution.
(B) If the amortized cost of an operation is , then every individual operation must also run in time.
(C) The potential method assigns a potential value to the data structure state, which is used to account for future expensive operations.
(D) In the accounting method, some operations may be charged more than their actual cost to pay for later operations.
(E) Amortized analysis and average-case analysis are equivalent concepts.
登入後即可作答並保存紀錄。
核心觀念
攤還分析(Amortized Analysis)為演算法分析中的核心技術,主要用於分析資料結構在一連串操作(Sequence of operations)下的平均成本。
關鍵觀念與定義包含:
- 與平均情況分析(Average-Case Analysis)的本質差異:
- 攤還分析不假設任何輸入資料的機率分佈(Probability Distribution),保證的是在**最壞狀況的操作序列(Worst-case sequence of operations)**下,每個操作的平均成本上界。
- 平均情況分析則高度依賴輸入資料的機率分佈,求取隨機輸入下的期望成本(Expected Cost)。
- 單一操作與序列成本:
- 攤還成本(Amortized Cost)為 並不代表每一個單一操作的實際執行時間(Actual Cost)皆為 。序列中允許存在少數極為昂貴(如 )的操作,只要其成本能被其他操作預先存納的成本所抵銷即可。
- 三大分析方法:
- 聚合分析(Aggregate Method):計算 個操作的總實際成本 ,則每個操作的攤還成本為 。
- 記帳法(Accounting Method):為不同的操作設定「攤還費用(Amortized Cost)」。當操作的攤還費用高於實際成本時,多出的部分轉為信用(Credit)存入資料結構中,用以支付未來實際成本大於攤還費用的高昂操作。
- 位能法(Potential Method):定義位能函數(Potential Function) 將資料結構狀態 映射至實數。第 個操作的攤還成本 定義為:
其中 為實際成本。若位能增加(),代表將能量/費用預存於資料結構中,用以補貼未來高耗時操作。
解題方法
本題為觀念型複選題,切入點為精準掌握攤還分析的三大分析技術(Aggregate, Accounting, Potential methods)之定義,以及攤還分析與平均情況分析的本質差別。
解法為依據 CLRS《Introduction to Algorithms》經典教科書之嚴謹定義,逐一驗證選項 (A) 至 (E) 的敘述正確性。
選項分析
- (A) 正確:
攤還分析(Amortized Analysis)旨在評估任意操作序列(Any sequence of operations)下平均每個操作成本的上界(Upper bo
第 3 題
- Let represent the length of the Longest Common Subsequence of strings and . Which of the following statements are correct?
(A) If , then .
(B) If , then .
(C) If , then .
(D) If , then .
(E) If , then .
(F) If , then .
(G) If , then .
(H) If , then .
(I) If , then .
(J) The time complexity of the standard DP solution for LCS is , where and are the lengths of the two input sequences.
(K) The DP table can be optimized to use only two rows without affecting correctness.
登入後即可作答並保存紀錄。
核心觀念
本題考核**動態規劃(Dynamic Programming, DP)在最長共同子序列(Longest Common Subsequence, LCS)**問題中的應用,包含遞迴關係式(狀態轉移方程式)推導、時間複雜度分析以及空間複雜度最佳化(空間壓縮)。
-
LCS 狀態定義:
設 表示字串 (前 個字元)與字串 (前 個字元)的 LCS 長度。 -
遞迴關係式(Recurrence Relation):
- 空間最佳化原理(Rolling Array):
計算第 列 時,僅需參考當前列 與前一列 的值。因此可使用大小僅為 的表格進行輪替更新,完成答案計算。
解題方法
1. 字元匹配狀況推導 ()
當末尾字元 與 相同,此字元必包含於 與 的最長共同子序列中。因此,最佳解等於「排除該字元後的前綴子串 與 之 LCS 長度」加上 :
2. 字元不匹配狀況推導 ()
當末尾字元不同,兩字元無法同時作為 LCS 的末尾字元。此時有兩種互斥的選擇情境:
- 排除 :考慮 與 ,長度為 。
- 排除 :考慮 與 ,長度為 。
取兩者最大值即可確保覆蓋最佳解:
針對選項 (I) 的數學等價性推導:
因為子字串包含關係, 表格具單調非遞減特性,即:
將 併入 計算中,結果依然不變:
因此選項 (I) 在邏輯與數值結果上皆完全正確。
第 4 題
- Which of the following statements about greedy algorithms are true?
(A) A greedy algorithm is correct only if the problem satisfies both the greedy-choice property and optimal substructure.
(B) The fractional knapsack problem can be optimally solved using a greedy strategy, whereas the 0/1 knapsack problem cannot.
(C) Dijkstra's shortest-path algorithm fails to produce correct results if the graph contains edges with negative weights.
(D) Every problem that can be solved using dynamic programming can also be solved optimally using a greedy algorithm.
(E) Prim's and Kruskal's algorithms are greedy algorithms for finding a minimum spanning tree.
登入後即可作答並保存紀錄。
核心觀念
本題旨在考驗對**貪婪演算法(Greedy Algorithm)基本性質、適用條件、經典應用及其與動態規劃(Dynamic Programming, DP)**之間關係的理解。
- 貪婪演算法的兩大核心要素:
- 貪婪選擇性質(Greedy-Choice Property):可透過做出局部最佳選擇(Local Optimal Choice)來導出全域最佳解(Global Optimal Solution),且先前的選擇不會因後續決策而改變。
- 最佳子結構(Optimal Substructure):原問題的最佳解包含其子問題的最佳解。
- 背包問題(Knapsack Problems)之差異:
- 分數背包問題(Fractional Knapsack Problem):物品可任意分割,滿足貪婪選擇性質。
- 0/1 背包問題(0/1 Knapsack Problem):物品不可分割,不滿足貪婪選擇性質,必須採用動態規劃求解。
- 單源最短路徑演算法之限制:
- Dijkstra 演算法基於貪婪選擇,前提假設為所有邊的權重皆為非負值(Non-negative Edge Weights)。若含有負權邊,已選定的最短距離頂點可能會被後續鬆弛(Relaxation)操作更新,導致貪婪策略失效。
- 貪婪演算法與動態規劃之包含關係:
- 貪婪演算法可視為動態規劃的特例。能用貪婪演算法求解的問題必定能用動態規劃求解,但反之不成立(DP 適用範圍大於 Greedy)。
- 最小生成樹(Minimum Spanning Tree, MST)演算法:
- Prim 演算法與 Kruskal 演算法皆基於切割定理(Cut Property),每次選擇當前最佳的「輕邊(Light Edge)」,屬於典型的貪婪演算法。
解題方法
判定貪婪演算法相關敘述的正確性時,可依循以下邏輯切入:
- 驗證正確性條件:欲證明一個貪婪演算法永遠能得到最佳解,必須同時證明該問題具備「貪婪選擇性質」與「最佳子結構」。兩者缺一不可。
- 比較 Greedy 與 DP 的適用界線:
- 檢查決策過程是否需要「回溯」或「列舉所有子問題結果」。若局部最佳選擇可能導致後續空間浪費或無效狀態(如 0/1 背包),則無法使用貪婪演算法。
- 檢視演算法的邊界假設:
- 檢視演算法在執行貪婪步驟時的基礎假設。例如 Dijkstra 演算法假設「離開未存取集合的頂點,其距離必為最終最短距離」,當存在負邊時此假設被打破。
- 確認經典演算法範疇:
- 熟記常見演算法的設計範式(Greedy, DP, Divide-and-Conquer 等)。Prim 與 Kruskal 演算法在每一步均選取最小權重的邊,符合貪婪演算法定義。
選項分析
-
(A) 正確
分析:一個貪婪演算法能夠保證求得全域最佳解的充要條件是該問題必須同時滿足貪婪選擇性質(Greedy-Choice Property)與最佳子結構(Optimal Substructure)。若缺乏貪婪選擇性質,局部最佳選擇可能會錯失全域最佳解;若缺乏最佳子結構,則子問題的最佳解無法遞迴組合出原問題的最佳解。因此本敘述正確。 -
(B) 正確
分析:- 在分數背包問題中,物品可以分割,採用貪婪策略依單位重量價值比值 由大到小排序並優先填入背包,
第 5 題
- Which of the following statements are necessarily true?
(A) If a polynomial-time algorithm exists for the optimization version of an NP-complete problem, then the corresponding decision version is also solvable in polynomial time.
(B) If a language is NP-hard and , then .
(C) There exists an NP-complete problem whose complement is also NP-complete.
(D) If problem is NP-complete and , then is NP-complete.
(E) If an NP-complete problem admits a polynomial-time approximation scheme (PTAS), then .
登入後即可作答並保存紀錄。
各選項詳細解析
(A) 正確
最佳化版本為求出最佳值 ,決策版本為判定是否存在解滿足目標值 (或 )。若最佳化版本存在多項式時間演算法求得 ,只需將 與門檻值 進行 時間比對,即可完成決策版本的求解。因此最佳化版本可在多項式時間求解,必然蘊涵決策版本亦可於多項式時間求解。
(B) 正確(註:題幹 為 之誤植)
若 ,根據定義,對任意語言 皆滿足 。又已知 ,且複雜度類別 在多項式時間歸約下具有封閉性(Closed under ),故 ,推得 。兩邊取補集得 ,綜上可得 。
(C) 錯誤
設 ,其補集 必為 -complete。
第 6 題
- Let and be non-negative functions defined for all sufficiently large integer . Select one or more correct statements.
(A) If , then .
(B) If , then .
(C) .
(D) If and , then .
(E) .
登入後即可作答並保存紀錄。
核心觀念
本題旨在考驗漸近記號(Asymptotic Notation)的基本定義、代數性質(如對稱性與遞移性)以及函數成長速率的比較。解答本題需掌握以下標準定義:
-
記號(Big-O Notation,漸近上界):
表示存在正常數 與 ,使得對於所有 ,恆成立:
-
記號(Big-Omega Notation,漸近下界):
表示存在正常數 與 ,使得對於所有 ,恆成立:
-
記號(Big-Theta Notation,漸近緊確界):
當且僅當 且 。 -
漸近符號的轉置對稱性(Transpose Symmetry):
解題方法
本題為複選題,應採用「定義直接推導」結合「極限檢驗與反例驗證」的策略:
- 依據定義證明正命題:將選項給予的前提條件帶入 Big-O 與 Big-Theta 的數學定義,利用不等式的運算推導出結論。
- 構造反例反駁假命題:若命題不成立,只需找出一個滿足前提條件但違反結論的具體函數組合 與 即可。
- 最高次項主導原則:評估多項式函數的漸近階數時,當 ,僅最高次項決定成長速率,低次項與任意有限常數係數均可忽略。
選項分析
-
(A) 錯誤
- 說明:Big-O 表示「漸近上界」(成長速率小於等於),不具備對稱律(Symmetry)。若 ,代表 的成長速率不超過 ,但不代表 的成長速率不超過 。
- 反例:設 ,。當 時,,故 成立;但不存在正常數 使得對所有足夠大的 恆有 ,因此 。
-
(B) 正確
- 說明:依據 記號的定義, 充要條件為 且 。
第 7 題
- Consider lower bounds for algorithmic problems in the comparison-based model. Select one or more correct statements.
(A) Any comparison-based algorithm for sorting distinct elements has a worst-case lower bound of .
(B) The lower bound for sorting applies to all sorting algorithms, including counting sort and radix sort.
(C) The decision-tree model is commonly used to prove lower bounds for comparison-based sorting.
(D) The information-theoretic argument for comparison-based sorting relies on the fact that there are possible input permutations.
(E) A lower bound of for a problem implies that no algorithm can solve the problem faster than linear time in the worst case, regardless of the computational model.
登入後即可作答並保存紀錄。
核心觀念
本題考驗計算複雜度理論中的比較模型下界(Lower Bounds in Comparison-Based Model)、決策樹模型(Decision-Tree Model)、**資訊理論論證(Information-Theoretic Argument)以及計算模型(Computational Model)**對演算法複雜度下界的約束與定義。
- 比較排序模型與下界:比較型排序演算法僅能透過「比較兩元素大小(如 )」來確定元素之間的相對順序。在此模型下,排序 個相異元素的最壞情況時間複雜度下界為 。
- 決策樹模型(Decision-Tree Model):用於描述比較型演算法操作流程的嚴謹模型。輸入規模為 的比較排序演算法可對應至一棵二元決策樹:
- 樹的內部節點(Internal Nodes)代表一次比較操作。
- 樹的葉節點(Leaves)代表一種可能的輸出排列(Permutation)。
- 樹的高度(Height )代表最壞情況下的比較次數。
- 資訊理論論證(Information-Theoretic Argument): 個相異元素共有 種可能的全排列組合。每次比較結果為二元選擇(Yes / No),最多僅能提供 1 bit 的資訊。要從 種可能性中確定唯一的正確排列,至少需要 次比較。根據 Stirling 近似公式,。
- 計算模型限制:演算法的時間複雜度下界嚴格依附於所採用的計算模型與允許的基本操作。若超越比較模型(例如採用 Counting Sort, Radix Sort 等基於鍵值直接索引的非比較型排序),則不受 下界的限制。
解題方法
透過決策樹模型與資訊理論推導比較排序演算法的下界步驟如下:
-
葉節點數與可能排列數:
處理 個相異元素時,共有 種可能的排序結果。為保證演算法對任何輸入皆能正確排序,決策樹的葉節點數量 必須涵蓋所有可能排列:
-
樹高與最壞情況比較次數:
高度為 的二元決策樹,其最多擁有 個葉節點,故:
兩邊取以 2 為底的對數得到樹高 的下界:
-
Stirling 近似推導:
利用對數性質與 Stirling 公式展開:
第 8 題
- Consider the single-source or all-pairs shortest path problem on a given graph, where edge weights may be positive, zero, or negative. Select one or more correct statements.
(A) Dijkstra's shortest path algorithm may fail when negative-weight edges exist.
(B) Bellman-Ford algorithm can detect the existence of negative-weight cycles.
(C) If a graph contains a negative-weight cycle reachable from the source, then the shortest path to some nodes is not well-defined.
(D) Floyd-Warshall algorithm can be used to detect negative-weight cycles.
(E) The all-pairs shortest path problem can be solved in the time complexity, where is the number of nodes and is the number of edges.
登入後即可作答並保存紀錄。
核心觀念
本題考查圖論(Graph Theory)中「單源最短路徑」(Single-Source Shortest Path, SSSP)與「全對最短路徑」(All-Pairs Shortest Path, APSP)的核心演算法、負權邊(Negative-weight edges)與負權環(Negative-weight cycles)對路徑長度定義及演算法正確性的影響,以及各最短路徑演算法的時間複雜度與負環偵測機制。
涉及的重點理論與定理包含:
- Dijkstra 演算法:採用貪婪策略(Greedy Strategy)。若圖中包含負權邊,已選定為最短距離的頂點可能會被後續的負權邊鬆弛(Relax),破壞貪婪選擇性質(Greedy Choice Property),導致演算法失效。
- Bellman-Ford 演算法:採用動態規劃思想,對所有邊進行 輪鬆弛操作。若在第 輪鬆弛時仍有邊可被更新,即代表圖中存在從源點可達的負權環。
- 負權環(Negative-weight cycle)對最短路徑的影響:若源點可達某負權環,且該負權環可達目標頂點,則繞行該環無限次可使路徑總權重趨近於 ,導致最短路徑長度無下界,即「未妥善定義」(Not well-defined)。
- Floyd-Warshall 演算法:採用動態規劃解 APSP 問題,時間複雜度為 。演算法結束後,若對角線元素滿足 ,表示頂點 位於某負權環上或可達負權環,可用於偵測負權環。
- 全對最短路徑的時間複雜度:常見演算法的時間複雜度包含 Floyd-Warshall 的 、重複執行 次 Bellman-Ford 的 ,以及 Johnson 演算法的 。
解題方法
依據單源與全對最短路徑演算法的設計前提、鬆弛機制、負環判定定理與時間複雜度理論,對各選項進行邏輯推導與正確性判定。
選項分析
- (A) 正確。Dijkstra 演算法的貪婪策略建立在「所有邊權重非負」()的假設上。在此假設下,當一個頂點從未造訪集合中被挑出並加入確定集合時,其當前估計值即為最終最短距離。若圖中存在負權邊,已確定最短距離的頂點可能透過後續延伸出的負權邊獲得更短的路徑,導致 Dijkstra 演算法計算出錯誤的結果。
- (B) 正確。
第 9 題
- Consider the branch-and-bound algorithm and the A* algorithm used for solving optimization problems. Select one or more correct statements.
(A) The branch-and-bound algorithm systematically explores a search tree while using bounds to prune branches.
(B) The A* algorithm can be viewed as a special case of the branch-and-bound algorithm.
(C) If the heuristic function used in the A* algorithm never overestimates the true remaining cost, then the A* algorithm is guaranteed to find an optimal solution.
(D) The A* algorithm does not guarantee a lower worst-case time complexity than an exhaustive search of the entire solution space.
(E) The branch-and-bound algorithm does not guarantee polynomial-time performance in the worst case.
登入後即可作答並保存紀錄。
選項解析
- (A) 正確:分支限界法(Branch-and-Bound)建立搜尋樹進行系統化探索,並藉由計算各節點的邊界值(bound)實施剪枝(pruning),剔除不可能包含最佳解的枝葉以縮小搜尋範圍。
- (B) 正確:A* 演算法採用評估函數 作為搜尋優先權。當啟發式函數 具備可採納性(admissible)時,即代表剩餘成本的下界估計,故 A* 可被視為最佳優先分支限界法(Best-First Branch-and-Bound)的一種特例。
第 10 題
- Consider the prune-and-search algorithm using the medians of medians for finding the -th smallest element in an unsorted array of distinct elements, where , as described below:
Algorithm Select(, ):
Input: An array of elements and an integer , .
Output: The -th smallest element in .
Step 1: If is small (e.g., ), then sort (with any method, e.g., insertion sort) and return the element with rank (i.e., the -th element in the sorted array).
Step 2: Divide the array into groups of five elements each (ignore the last group if it has fewer than five elements).
Step 3: Find the median of each group.
Step 4: Recursively compute the median of the found medians, denoted by .
Step 5: Partition the original array into the following three sets:
Step 6: If , then return Select(, ).
Else if , then return .
Else return Select(, ).
Select one or more correct statements.
(A) After grouping elements into blocks of five and choosing the median of medians as the pivot denoted , at least elements are guaranteed to be less than or equal to , and at least elements are guaranteed to be greater than or equal to .
(B) The recurrence implies that the algorithm runs in time.
(C) Using groups of size 3 instead of 5 still guarantees linear worst-case running time.
(D) The correctness of the pruning step relies on the fact that the pivot is an approximate median of the entire array.
(E) The median-finding problem is a special case of the -th smallest selection problem, where , and both can be solved in worst-case linear time.
登入後即可作答並保存紀錄。
核心觀念
本題考查**中位數之中位數演算法(Median-of-Medians Algorithm,亦稱 Select 演算法)與減治法(Prune-and-Search / Divide-and-Conquer)**的分析。重點包含:
- 中位數選取與剪枝下界推導:透過將陣列劃分為固定大小的子組(如每 5 個一組),利用各組中位數的中位數 作為樞軸(Pivot),保證每次剪枝至少能排除固定比例的元素(約 )。
- 遞迴關係式(Recurrence Relation)與時間複雜度:評估遞迴式的解是否滿足最壞情況線性時間 。
- 分組大小對複雜度的影響:分析為何每組大小選擇 5 能保證 ,而選擇 3 則無法。
- 演算法正確性(Correctness)與執行效率(Efficiency)的區別:區分 Pivot 的品質是影響時間複雜度還是演算法答案的正確性。
解題方法
1. 剪枝比例與下界推導
設陣列元素個數為 ,將其劃分為 個組別(每組 5 個元素)。
取得每組的中位數後,遞迴求出這些中位數的中位數 :
- 在 個組中位數中,至少有包含 在內的 個組別的中位數大於等於 。
- 對於每一個中位數大於等於 的組別,該組內包含中位數本身以及比中位數大的 2 個元素,共計 3 個元素皆大於等於 。
- 因此,整個陣列中保證大於等於 的元素個數至少為:
- 同理,保證小於等於 的元素個數亦至少為 。
- 剪枝後進入下一次遞迴的最大子陣列大小至多為 。
2. 時間複雜度遞迴式推導
Algorithm Select 的最壞情況執行時間 可拆解為三個部分:
- 找出各組中位數與 Partition 劃分:。
- 遞迴尋找中位數之中位數 :。
- 剪枝後對剩餘子陣列遞迴尋找:至多 。
整理得到遞迴關係式:
使用代入法(Substitution Method)驗證是否存在常數 使得 :
若要使 ,只需取 即可成立。
因為 ,遞迴樹每層的總工作量呈等比級數遞減,故 。
選項分析
- (A) 正確
根據上述推導,在每 5 個元素一組的設定下,共有約 個小組。其中至少有一半(約 個小組)的中位數大於等於 。
第 11 題
- Given a weighted directed graph G (V, E) below.
🖼️【此處有附圖,請對照原卷】
Which of the following statement(s) about G is (are) true?
(A) ACEGFBD is a possible topological order of the network if G is an activity-on-vertex (AOV) network.
(B) ABECGFD is a possible topological order of the network if G is an activity-on-edge (AOE) network.
(C) Suppose G is an AOE network. If each task takes exactly one day, then ABCDGF is a critical path.
(D) Suppose G is an AOE network. If each activity takes exactly one day, then AEGF is a critical path.
登入後即可作答並保存紀錄。
核心觀念
拓樸排序要求每條有向邊 的起點 都排在終點 前面。判斷選項時,逐條檢查圖中的先後限制即可。
AOE 網路的活動位於邊上,關鍵路徑則是從起點到終點、總工期最長的路徑。若每條邊代表的活動都需一天,路徑工期就是邊數。
解題方法
由圖可讀出邊的方向:
檢查拓樸排序時,確認每條邊的起點都排在終點之前;比較關鍵路徑時,則列出起點 到終點 的路徑並比較工期。
選項分析
(A) 錯誤。 排序 ACEGFBD 中, 排在 前面,違反邊 ; 也排在 前面,違反邊 。因此不是合法的拓樸排序。
第 12 題
- Given an empty 11-bucket hash table with hash function . Below is the resulting hash table after inserting 8 items.
| Bucket | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | 24 | 37 | 27 | 16 | 17 | 15 | 38 |
Which of the following statements is/are true?
(A) 27 37 16 17 15 38 57 24 is a possible input sequence when quadratic probing is adopted.
(B) 37 27 16 17 15 38 57 24 is a possible input sequence when quadratic probing is adopted.
(C) 27 37 16 17 15 38 57 24 is a possible input sequence when linear probing is adopted.
(D) 27 37 16 17 15 38 57 24 is a possible input sequence when linear probing is adopted.
登入後即可作答並保存紀錄。
核心觀念
雜湊函數為
發生碰撞時:
- 線性探測(linear probing):依序檢查下一個 bucket。
- 二次探測(quadratic probing):第 次探測位置為
依考卷圖片,最後的雜湊表為:
| Bucket | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | 24 | 57 | 37 | 27 | 16 | 17 | 15 | 38 |
各鍵值的雜湊位置為:
解題方法
依各選項給定的輸入順序逐一插入,最後比較是否得到考卷中的表格。
以序列
為例。
採用線性探測:
- 放入 bucket 。
- 放入 bucket 。
- 原本要放 bucket ,碰撞後放入 bucket 。
- 原本要放 bucket ,碰撞後放入 bucket 。
- 原本要放 bucket ,依序檢查 ,放入 bucket 。
- 原本要放 bucket ,依序檢查 ,放入 bucket 。
- 放入 bucket 。
- 原本要放 bucket ,碰撞後放入 bucket 。
所得結果正好是:
採用二次探測時:
第 13 題
- Given a weighted undirected graph G (V, E) below. Which of the following statement(s) about the minimum spanning tree (MST) of G is (are) true?
🖼️【此處有附圖,請對照原卷】
(A) If the MST is constructed by Kruskal's algorithm, edge (E, G) is the 6-th edge added to the MST.
(B) If the MST is constructed by Kruskal's algorithm, the MST is a binary tree.
(C) If the MST is constructed by Prim's algorithm and starting from vertex C, the edge (E, G) is the 5-th edge added to the MST.
(D) If the MST is constructed by Prim's algorithm and starting from vertex D, there is an edge connecting vertex A and vertex B.
登入後即可作答並保存紀錄。
核心觀念
最小生成樹(MST)是連通所有頂點且總權重最小的樹,因此若圖有 個頂點,MST 恰有 條邊。
- Kruskal 演算法:依邊權重由小到大檢查,只加入不會形成環的邊。
- Prim 演算法:從指定頂點出發,每次加入一條連接「已加入頂點」與「尚未加入頂點」的最小權重邊。
- 二元樹:根節點選定後,每個節點至多有兩個子節點。
解題方法
Kruskal 演算法
將邊依權重排序:
依序加入不形成環的邊:
此時已加入 條邊,所有頂點都連通,得到 MST。
Prim 演算法,從 出發
每步加入的邊依序為:
所以 是第 條加入 MST 的邊。
Prim 演算法,從 出發
每步加入的邊依序為:
第 14 題
- Given list . Which of the following statements is (are) true?
(A) The resulting sequence of the first phase of Quick Sort when the first element, i.e., 9, is chosen as pivot is .
(B) The resulting sequence of the first phase of Quick Sort when the second element, i.e., 7, is chosen as pivot is .
(C) At the end of the second pass of LSD Radix sort, the first element of the resulting chain is 11.
(D) At the end of the third pass of LSD Radix sort, the 7-th element of the resulting chain is 22.
登入後即可作答並保存紀錄。
核心觀念
-
快速排序法(Quick Sort)與分割演算法(Partition Algorithm):
- Quick Sort 採用分治法(Divide and Conquer)。第一階段(First Phase / Partition)選定一個 Pivot(樞紐碼)後,將小於等於 Pivot 的元素置於左區塊,大於 Pivot 的元素置於右區塊,最後將 Pivot 放回兩區塊之間的正確位置。
- 國內資訊研究所考試預設採用經典的 Horowitz Partitioning Algorithm(Ellis Horowitz《Fundamentals of Data Structures》標準):
- 設定指標 從左端往右掃描(尋找 者)。
- 設定指標 從右端往左掃描(尋找 者)。
- 當 與 皆停止時交換 與 ,重複此過程直到 ,最後將 Pivot 與 交換。
-
最低有效位數基數排序法(LSD Radix Sort):
- LSD(Least Significant Digit)Radix Sort 由最低位數(個位數)開始,向最高位數依次進行穩定排序(Stable Sort),通常搭配 10 個 Bucket(0~9)進行收集與搬移。
- 給定資料的最大值為 (共 3 位數),因此總共需執行 3 個回合(Passes):
- Pass 1:依個位數()分桶排序。
- Pass 2:依十位數()分桶排序。
- Pass 3:依百位數()分桶排序。
解題方法
給定原始陣列 (共 8 個元素)。
1. Quick Sort 第一階段 Partition 推導
-
Pivot 選擇第一個元素 :
- 初始陣列:,Pivot 。
- 指標 從 開始右移,指標 從 開始左移:
- 往右尋找 的元素:, , 停在 ()。
- 往左尋找 的元素:, , 停在 ()。
- 交換 與 陣列變為 。
- 繼續掃描:
- 繼續右移:, , 停在 ()。
- 繼續左移:, , 停在 ()。
- 此時 ,迴圈結束。
- 將 Pivot () 與 () 交換 最終序列為 。
-
Pivot 選擇第二個元素 :
- 將 Pivot () 交換至首位 初始陣列變為 ,Pivot 。
- 掃描停在 (); 掃描停在 ()。
- 交換 與 。
- 繼續掃描停在 (); 繼續掃描停在 ()。
- 交換 與 。
第 15 題
- Consider a red-black tree with level order 52, 33, 80, 100. Which of the following statements is (are) true?
(A) A node 33 is red.
(B) After inserting 65 and 55, node 100 in the resulting red-black tree is black.
(C) After inserting 65, 55, and 60, the resulting red-black tree is black.
(D) After inserting 65, 55, and 60, the level order traversal of the resulting red-black tree is 52, 33, 80, 100, 65.
登入後即可作答並保存紀錄。
1. 初始紅黑樹狀態推導
依 Level Order Traversal 建構二元搜尋樹結構:
- 為根節點,依紅黑樹性質必為黑色。
- 檢視各葉節點()至根節點的路徑黑高度(Black-Height):
- 若 為紅色,則 必為黑色(不可有連續紅節點),導致右子樹長路徑之黑高度大於短路徑,違反紅黑樹性質;故 必為黑色, 必為紅色。
- 為使左子樹( 方向)黑高度與右子樹一致, 必為黑色。
- 初始節點顏色:、、、。
- (A) 錯誤:節點 為黑色。
2. 依序插入節點之動態調整
Step 1:插入
為 之左子節點,預設顏色為紅色。
- 父節點 為黑色,無連續紅節點違規,無需調整。
Step 2:插入 (驗證選項 B)
為 之左子節點,預設顏色為紅色。
- 出現 Double Red 違規:新節點 、父節點 、叔節點 、祖父節點 。
- Case 1 處理(叔節點為紅):
- 將父節點 與叔節點 改塗為黑色。
- 將祖父節點 改塗為紅色。
- 調整後節點 之顏色變為黑色。
第 16 題
- Which of the following statements are correct?
(A) The postfix of is .
(B) The postfix of infix expression is .
(C) The infix expression is .
(D) The prefix of the postfix expression is .
登入後即可作答並保存紀錄。
核心觀念
本題考驗中序(Infix)、後序(Postfix)與前序(Prefix)算術運算式的相互轉換,以及運算樹(Expression Tree)與堆疊(Stack)的應用。
- 中序運算式(Infix):運算子位於兩個運算元中間,例如 。計算時需依循括號優先權與運算子優先順序(如乘除優先於加減)。
- 後序運算式(Postfix):運算子位於兩個運算元之後,例如 。不需括號即可明確表示計算順序,適合利用堆疊進行求值。
- 前序運算式(Prefix):運算子位於兩個運算元之前,例如 。同樣不需括號即可表達正確的計算順序。
- 運算樹(Expression Tree):運算式可表示為二元樹結構,其中內部節點(Internal Nodes)為運算子,葉節點(Leaves)為運算元。對運算樹進行中序追蹤(In-order Traversal)、後序追蹤(Post-order Traversal)與前序追蹤(Pre-order Traversal),可分別得到相應的中序、後序與前序運算式。
解題方法
-
括號法(Infix 轉 Postfix / Prefix):
- 步驟一:依據運算子優先順序與結合律,將整體運算式補齊所有括號。
- 步驟二(轉後序):將每個運算子移動到對應右括號的外側,最後將所有括號刪除。
- 步驟三(轉前序):將每個運算子移動到對應左括號的外側,最後將所有括號刪除。
-
堆疊建置法(Postfix 轉 Infix / Prefix):
- 由左至右依次掃描後序運算式:
- 若遇到運算元:推入(Push)堆疊。
- 若遇到運算子:自堆疊頂端彈出(Pop)兩個元素,先彈出者為右運算元 ,後彈出者為左運算元 。將其組合成對應的中序式 或前序式 後推回堆疊。
- 掃描完畢後,堆疊頂端的元素即為最終轉換結果。
- 由左至右依次掃描後序運算式:
選項分析
- (A) 錯誤
- 已知中序運算式:
- 推導步驟:
- 處理最內層括號:
- 處理除法運算:
- 處理加法運算:
- 正確結果:正確後序表示法應為 。
第 17 題
- Consider the code fragment below.
container.push('T');
container.pop();
container.push('L');
container.push('O');
container.push('V');
container.pop();
container.pop();
container.push('E');
container.push('N');
container.push('C');
container.push('U');
container.push('D');
Which of the following statements is (are) true?
(A) If the "container" is an empty standard stack. The sequence of elements inside the "container" from its top to the bottom is LOEC.
(B) If the "container" is an empty standard queue. The sequence of elements inside the "container" from its front to its rear is ENCU.
(C) If the "container" is an empty min heap, the level order traversal of the heap is NVOU.
(D) If the "container" is an empty max heap, the level order traversal of the heap is NLEC.
登入後即可作答並保存紀錄。
核心觀念
本題比較四種容器的操作規則:
- Stack(堆疊):後進先出,
push從頂端加入,pop移除頂端。 - Queue(佇列):先進先出,
push從 rear 加入,pop從 front 移除。 - Min heap(最小堆積):每次
pop移除最小值。 - Max heap(最大堆積):每次
pop移除最大值。
依考卷圖中的實際程式碼,最後三個操作為:
container.push('C');
container.push('U');
container.pop();
並非 push('D')。
解題方法
先依序執行程式:
push T, pop
push L, push O, push V, pop
push E, push N, pop
push C, push U, pop
第一組 T 立即被移除,之後分別依各容器規則模擬。
選項分析
(A) Standard stack
堆疊內容變化如下,左側為 bottom、右側為 top:
push L → L
push O → L O
push V → L O V
pop → L O
push E → L O E
push N → L O E N
pop → L O E
push C → L O E C
push U → L O E C U
pop → L O E C
最後由 top 到 bottom 應為:
C, E, O, L
不是 L, O, E, C,故 (A) 錯誤。
(B) Standard queue
佇列由 front 到 rear:
第 18 題
- Given a binary tree with inorder traversal ILIOVENCU, and postorder traversal LIVNUCEO. Which of the following statements is (are) true?
(A) The fifth letter in the level-order traversal is V.
(B) The height of the binary tree is 5.
(C) C is a leaf node.
(D) The six letter in the level-order traversal is U.
登入後即可作答並保存紀錄。
根據中序走訪(Inorder: I L O V E N C U)與後序走訪(Postorder: L I V N U C E O)重建二元樹:
1. 二元樹重建步驟
- 確定樹根(Root):
後序走訪最後一個節點為 ,故全樹的根節點為 。 - 劃分左右子樹:
在中序走訪中, 左側為左子樹I L,右側為右子樹V E N C U。- 左子樹:
- 中序:
I L,後序:L I。 - 後序最後節點為 ,故 為 的左子節點;中序顯示 在 右側,故 為 的右子節點。
- 中序:
- 右子樹:
- 中序:
V E N C U,後序:V N U C E。 - 後序最後節點為 ,故 為 的右子節點。
- 中序中 左側為 ,右側為
N C U:- 為 的左子節點。
- 中序:
- 左子樹:
第 19 題
There are online courses labeled from to . Each course is described by , where the course must be taken continuously for days and completed no later than . You start on day and may not take more than one course at a time. The goal is to determine the maximum number of courses that can be taken. Which of the following statements is/are true?
(A) Time complexity is .
(B) Let the latest start time of course be . The course with the minimum latest start time should be selected first.
(C) A min-heap-based greedy approach provides a suitable solution.
(D) The course with the earliest should be selected first.
登入後即可作答並保存紀錄。
第 20 題
Consider structurally unique binary trees with nodes. For example, when , there are 5 structurally unique binary trees.
🖼️【此處有附圖,請對照原卷】
Which of the following statements is (are) true?
(A) The space complexity of determining structurally unique -node binary trees is .
(B) The time complexity of determining structurally unique -node binary trees is .
(C) There are 429 structurally unique binary trees when .
(D) There are 4862 structurally unique binary trees when .
登入後即可作答並保存紀錄。
核心觀念
本題考二元樹的結構計數。左右子樹的位置有區別,因此左子樹和右子樹互換時,視為不同結構。這類結構數由卡特蘭數(Catalan number)計算。
令 表示含 個節點的結構不同二元樹數量,空樹有 種。根節點固定後,若左子樹有 個節點,右子樹便有 個節點,因此:
解題方法
圖中列出 時的 5 種結構:根節點有左右兩個子節點的 1 種,以及根、子節點、孫節點形成單側鏈的 4 種。這確認左右子樹的位置不同時,會算作不同結構。
用動態規劃依序計算卡特蘭數,初始值為 :
| 0 | 1 |
| 1 | 1 |
| 2 | 2 |
| 3 | 5 |
| 4 | 14 |
| 5 | 42 |
| 6 | 132 |
| 7 | 429 |