111 年 國立陽明交通大學資訊工程學系碩士班《資料結構與演算法》
第 1 題
Which of the following statement(s) is (are) true?
(A) Node is an ancestor of Node .
(B) Node is a leaf node.
(C) Node is an ancestor of Node .
(D) Node has no sibling.
登入後即可作答並保存紀錄。
核心觀念
AVL 樹是具有高度平衡條件的二元搜尋樹。定義節點的平衡因子為
每個節點都必須滿足 。插入新節點時,先依二元搜尋樹規則找到位置,再由插入處往根節點回溯,對第一個失衡節點進行旋轉。
本題也考查三個關係:祖先位於根至該節點的路徑上;葉節點沒有子節點;兄弟節點具有同一個父節點。
解題方法
原圖的根為 ,左子樹為 ,其左右子節點是 ,且 的右子節點為 。右子樹為 ,其左右子節點是 ,而 的右子節點為 。
依大小比較,插入 的搜尋路徑為
因為 ,所以 插入為 的左子節點。此時相關子樹為:
58
\
65
/
61
由下往上檢查, 的平衡因子為 ,仍符合 AVL 條件; 的平衡因子為 ,是第一個失衡節點。插入位置位於 的右子樹之左側,因此屬於 RL(右-左)型,需要兩次旋轉:
- 對 右旋,讓 成為 的右子節點, 成為 的右子節點。
- 對 左旋,讓 成為這棵子樹的根,左右子節點分別為 。
Given the following AVL tree, insert a node of while keeping it an AVL tree. 🖼️【此處有附圖,請對照原卷】
第 2 題
Following the result of the above, delete while keeping it an AVL tree. Which of the following statement(s) is (are) true?
(A) Nodes and are children of Node .
(B) Node is an ancestor of Node .
(C) Node is not a leaf node.
(D) Nodes and are at the same level.
登入後即可作答並保存紀錄。
核心觀念
AVL 樹是具有平衡條件的二元搜尋樹。定義節點 的平衡因子為
其中空樹高度為 、葉節點高度為 。每個節點都必須滿足 。
插入或刪除節點後,須沿著修改位置往根節點檢查,透過旋轉恢復平衡。旋轉同時必須維持「左子樹的鍵值小於節點,右子樹的鍵值大於節點」的搜尋樹性質。
解題方法
原圖的根節點為 ;左子節點 的左右子節點為 、,而 的右子節點為 。右子節點 的左右子節點為 、,而 的右子節點為 。
1. 先插入 ,完成前一題的調整
依二元搜尋樹規則,插入路徑為
因為 ,所以 成為 的左子節點。
此時 的平衡因子為 ,仍然平衡;往上遇到的第一個失衡節點是 :
新增節點位於 的「右子樹之左側」,屬於 RL 型失衡,依序進行:
- 對 右旋,使 位於 上方。
- 對 左旋,使 成為這棵子樹的根。
調整後, 以下的結構為:
73
/ \
61 79
/ \
58 65
因此,插入後 的左子節點是 ,而 的左右子節點分別為 、。
2. 刪除 ,恢復 AVL 平衡
是葉節點,可以直接刪除。此時 的左子樹為空,右子樹則是 及其右子節點 :
第 3 題
Which of the following statements are true about AVL trees? Let be the number of nodes.
(A) AVL trees are not search trees.
(B) The worst-case performance for searching is .
(C) Traversing an AVL tree takes time.
(D) Inserting a node takes time.
(E) AVL trees provide faster lookups than Red Black Trees.
登入後即可作答並保存紀錄。
核心觀念
AVL 樹是一種自平衡二元搜尋樹。對每個節點,其左、右子樹高度差至多為 。這項限制使 AVL 樹的高度維持在 ,其中 為節點數。
因此,搜尋或插入等沿樹高進行的操作需要 時間;走訪整棵樹則必須處理所有節點,時間為 。
解題方法
逐一判斷選項,區分兩種操作:
- 沿樹高走訪一條路徑:AVL 樹高度為 ,例如搜尋與插入。
- 走訪整棵樹:每個節點都要處理一次,因此為 。
再比較 AVL 樹與 Red-Black 樹的平衡程度:兩者操作的漸進時間皆為 ,但 AVL 樹的平衡限制較嚴格,查找路徑通常較短。
選項分析
(A) 錯誤。 AVL 樹本身就是二元搜尋樹,並額外維持左右子樹高度差至多為 的平衡條件。
第 4 題
Please refer to the following graph and solve the minimum spanning tree (MST) using Kruskal's algorithm. Which of the following statements are true? 🖼️【此處有附圖,見下方】
(A) The sum of the weights of all edges in the MST is .
(B) In the MST, Node connects to Node through exactly Nodes and .
(C) The sum of the weights of all edges connected to Node in the MST is .
(D) There are edges in total in the MST.
登入後即可作答並保存紀錄。
核心觀念
Kruskal 演算法將邊依權重由小到大排列,逐一加入不會形成環的邊,直到包含所有節點。含 個節點的最小生成樹會有 條邊。
解題方法
圖中有節點 至 ,共 個節點;相關邊及權重包括 –、–、–、–、–、–、–、–、–、–、– 等。
由小到大選邊,若加入後會形成環便跳過:
- 權重 :選 –、–。
- 權重 :選 –、–。
- 權重 :選 –、–。
- 權重 :選 –;–、– 都會形成環,跳過。
- 權重 :– 會形成環,跳過。
- 權重 :選 –,此時所有節點連通。
第 5 題
Please refer to the following pseudocode and find which statements are true.
🖼️【此處有附圖,見下方】
(A) The time complexity of this code is .
(B) The code is about finding the longest path using Bellman Ford’s algorithm.
(C) It works for graphs containing negative circles.
(D) It works for weighted graphs.
登入後即可作答並保存紀錄。
核心觀念
圖中的偽程式先將來源點距離設為 、其餘頂點設為無限大,再反覆從尚未處理的頂點中取出目前距離最小者,並用
更新鄰居 的距離。這是 Dijkstra 最短路徑演算法的作法;它適用於邊權重皆非負的加權圖。
解題方法
題目中的關鍵操作是「取出 中距離最小的頂點」以及「放鬆該頂點的每條鄰邊」。若以線性掃描尋找最小值,每次取最小值需 ,最多取出 次,合計 ;遍歷鄰邊另需 。因此總時間為 ,對簡單圖而言 ,可寫成 。
第 6 題
Which of the following graph algorithm(s) utilize dynamic programming?
(A) Kruskal's algorithm
(B) Dijkstra's algorithm
(C) Finding the longest common sequence in a pair of strings
(D) The Floyd-Warshall algorithm for computing the all-pairs shortest path of a graph
登入後即可作答並保存紀錄。
核心觀念
動態規劃(dynamic programming)的典型特徵是:把問題拆成較小的子問題,利用子問題的最佳解推導更大的問題,並儲存子問題結果以避免重複計算。常見形式是定義狀態,再寫出狀態轉移式。
貪婪法則是在每一步選擇當下看起來最好的方案,通常不會枚舉並保存一系列子問題的解。Kruskal 與 Dijkstra 都屬於貪婪演算法;最長共同子序列與 Floyd–Warshall 則使用動態規劃。
解題方法
逐一辨認各演算法的核心設計方式:若演算法以較小子問題的結果建立較大子問題的解,便是動態規劃;若依局部最佳選擇逐步完成,則屬於貪婪法。
最長共同子序列令 表示兩個字串前 個與前 個字元的最長共同子序列長度。若末字元相同,就接在較短前綴的解後面;若不同,就比較略去其中一方末字元的兩種情況:
Floyd–Warshall 令 表示只允許頂點 作為中繼點時,從頂點 到頂點 的最短距離。加入頂點 作為可能的中繼點後,最短距離取「不經過 」與「經過 」兩種路徑中較短者:
第 7 題
The hash function is used to map any integer key to one of the slots, indexed , of a hash table of slots.
(A) If , there is just one occurrence of collision among these keys: .
(B) Consider using chaining to handle collisions. The table is initially empty. After inserting the keys into the table in the given order, key is in the slot indexed .
(C) Consider using open addressing with linear probing to handle collisions. The table is initially empty. After inserting the keys into the table in the given order, key is in the slot indexed .
(D) Let us use quadratic probing to handle collisions. The table is initially empty. After inserting the keys into the table in the given order, key is in the slot indexed .
登入後即可作答並保存紀錄。
核心觀念
雜湊函數 會把鍵值映射到 到 的槽位。若新鍵映射到已被占用的槽位,就發生碰撞(collision)。
- 鏈結法(chaining):同一槽位可以存放多個鍵值,通常以鏈結串列等方式連接。
- 線性探測(linear probing):碰撞後依序檢查下一個槽位,槽位計算為 。
- 平方探測(quadratic probing):碰撞後依平方數偏移探測;本題採標準公式 ,其中 。
各選項使用的表格大小為 。
解題方法
先計算每個鍵值的初始雜湊位置:
| 鍵值 | 計算 | 初始槽位 |
|---|---|---|
| 1 | 1 | |
| 2 | 2 | |
| 4 | 4 | |
| 6 | 6 | |
| 8 | 1 | |
| 12 | 5 | |
| 15 | 1 |
這些初始位置可直接判斷碰撞;遇到碰撞時,再依題目指定的處理方法模擬探測或存放。
選項分析
(A) 錯誤。
初始雜湊位置依序為 。鍵值 的初始位置是 ,與先前放入槽位 的鍵值 碰撞;鍵值 的初始位置也是 ,該槽位已有鍵值,因此再發生一次碰撞。總共有兩次碰撞,不是一次。
(B) 正確。
鏈結法允許同一槽位存放多個鍵值。鍵值 的位置為:
第 8 題
For the directed weighted graph below, compute the shortest paths using Dijkstra's algorithm with Node as the source node. Which of the following statement(s) is (are) true? 🖼️【此處有附圖,見下方】
(A) The shortest path from to has the highest weight among all shortest paths from to the other nodes.
(B) The paths from to , from to , and from to have the same number of edges.
(C) The total weight of the path from to is .
(D) The longest path, in terms of number of edges, among all shortest paths from to the other nodes has four edges.
登入後即可作答並保存紀錄。
核心觀念
Dijkstra 演算法適用於邊權重皆非負的圖。從起點 出發,每次選取尚未確定、暫定距離最小的節點 ,再沿著其向外的有向邊更新鄰點:
本題須分清楚「最短路徑的總權重」與「最短路徑的邊數」;Dijkstra 最小化的是總權重,並非邊數。
解題方法
依原圖,雙向連線的權重分別為 、,、,以及 、。其餘有向邊為 、、、、、、、、、,其中底部權重 的邊方向是 。
初始化 ,其餘節點距離為 ,依序計算如下:
-
確定 ,距離為 :
由 的出邊得到 -
確定 ,距離為 :
經由 更新所得距離為 ,不優於 原有的 。
-
確定 ,距離為 :
經由 得到經由 的權重為 ,不優於 原有的 。
第 9 題
A red-black tree is initially empty. Now five keys are inserted in the order . Which of the following statement(s) is (are) true about the resulting red-black tree?
(A) There are more than external nodes.
(B) There are two red nodes.
(C) Node is black.
(D) There are two rotation operations occurring during the insertion of the five nodes.
(E) Nodes and have different colors.
登入後即可作答並保存紀錄。
核心觀念
紅黑樹插入時,新節點一律先著成紅色,再依父節點、叔叔節點的顏色修正。修正方式包括重新著色與旋轉,並須維持紅黑樹的性質:
- 根節點為黑色。
- 紅色節點的子節點必為黑色。
- 從任一節點到其後代外部節點的每條路徑,黑色節點數相同。
- 外部節點(NIL)視為黑色。
含有 個實際鍵值節點的二元樹,外部節點數為 。
解題方法
依序插入 ,每次檢查是否有紅色父子相連,並記錄旋轉次數。
-
插入 :新節點成為根,根必須為黑色。
-
插入 、:兩者均為紅色,分別成為 的左右子節點,沒有紅色父子相連。
-
插入 :成為 的右子節點。此時父節點 與叔叔節點 都是紅色,因此將 、 改為黑色,將祖父節點 暫改為紅色;根節點最後恢復為黑色。此步不需旋轉。
-
插入 :成為 的右子節點。父節點 是紅色,叔叔節點為黑色的 NIL。
第 10 題
Which of the following statement(s) is (are) true?
(A) In Breadth First traversal, we process all of a node's descendants before we move to an adjacent node.
(B) A directed graph is strongly connected if there is a path from each vertex to every other node in the digraph.
(C) A stack is used to hold nodes in breadth first search of a graph.
(D) A graph is said to be complete if there is no edge between every pair of nodes.
登入後即可作答並保存紀錄。
核心觀念
本題考查圖形走訪與圖形基本定義:
- 廣度優先搜尋(BFS)以起點為中心,依照與起點的距離由近至遠走訪頂點,使用佇列保存待處理頂點。
- 強連通有向圖:對任意兩個頂點 、,都存在從 到 的有向路徑。
- 完全圖:每一對不同頂點之間都有邊相連。
解題方法
逐一比對選項中的敘述與標準定義。判斷 BFS 時,注意它是先走訪同一層的頂點,再進入下一層;判斷圖形定義時,確認頂點間路徑的方向,以及「完全」代表每一對不同頂點都有邊。
選項分析
第 11 題
Show the correct one(s) for the following equalities:
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
本題考查漸近記號 、 的定義,以及比較多項式、階乘與指數函數成長速度的方法。
對非負函數 、,若存在常數 與 ,使得所有 都滿足
則 。若存在常數 與 ,使得所有 都滿足
則 。
判斷和式的漸近階時,可先找成長最快的項;但若要證明整個和式屬於某個 類別,仍須確認其他各項也都能被該上界控制。多項式次方固定時,指數底數較大的函數成長較快;指數函數也會快於任何固定次方的多項式。
解題方法
逐項依定義比較。對 ,可將候選函數相除,檢查比值是否有界;對 ,則需同時找出上界與下界。
選項分析
(A) :正確。
對每個 ,都有 ,因此
取常數 ,便符合 的定義,所以 。
(B) :正確。
分別比較左側各項與 :
而
第 12 題
Consider a stack containing where the stack top is . Show the correct one(s) for the following:
(A) After pushing and , the stack content is .
(B) After the work in (A), there are elements left in the stack.
(C) Now, the top second and third elements in the stack are equal.
(D) Popping times after the work in (A), the stack becomes .
(E) The number of nonempty continuous popping is five if it is done from the above original stack.
登入後即可作答並保存紀錄。
核心觀念
堆疊(stack)遵循「後進先出」(LIFO):每次 push 都把新元素放到頂端,每次 pop 都移除頂端元素。題目以括號由左至右列出堆疊內容,並指定頂端是 ,因此列出的順序就是由頂端往底端。
原堆疊有 個元素:。依序 push 3、再 push 5 後,頂端至底端為 ,共有 個元素。
解題方法
先依堆疊規則逐次更新頂端,再檢查每個選項的內容、元素數量與彈出結果。連續 pop 時,每次都移除當下的頂端元素。
選項分析
- (A) 錯誤。 依序推入 、 後,頂端至底端應為 。選項所列的 不是正確的堆疊內容,且只有 個元素。
第 13 題
Which of the following statement(s) is (are) correct?
(A) Both stack and queue can be implemented with a linked list.
(B) Both stack and queue are stack.
(C) When a queue is implemented with an array, two elements in the queue are both of the same type.
(D) The number of elements in a queue is less than some integer.
(E) An element in a queue is defined to point to its predecessor by default.
登入後即可作答並保存紀錄。
核心觀念
本題考查堆疊(stack)與佇列(queue)的基本定義、陣列的元素型態,以及佇列元素數量與連結方式。
- 堆疊採用後進先出(LIFO):最後加入的元素最先取出。
- 佇列採用先進先出(FIFO):最早加入的元素最先取出。
- 堆疊與佇列都是抽象資料型態,可用陣列或鏈結串列實作。採用陣列時,陣列中的元素具有相同型態。
- 在有限記憶體的實作中,佇列當下存放的元素數量有限,必小於某個整數。
解題方法
逐項對照資料結構的定義與常見實作方式:確認堆疊和佇列是否能用鏈結串列實作,再區分兩者的取出順序;接著檢查陣列的型態限制、佇列元素數量是否有限,以及佇列是否規定元素要指向前驅。
選項分析
(A) 正確。
堆疊與佇列都能用鏈結串列實作。堆疊可在串列一端進行插入與刪除;佇列則可用 front 與 rear 指標,分別管理取出端與加入端。
第 14 題
Consider a linked list of members. Show the following correct one(s).
(A) A linked list is static.
(B) The time complexity of sorting this list with merge technique is in the worst case.
(C) Changing a single linked list into a circular-linked list needs time.
(D) The best merging time of two linked lists, whose lengths are and , is if is the larger.
(E) If each linked list has an additional pointer to its last element, the time complexity of merging two linked lists is constant.
登入後即可作答並保存紀錄。
核心觀念
本題考查鏈結串列的動態特性、合併排序,以及串列尾端指標對操作時間的影響。以下將「合併兩個鏈結串列」按一般串列串接理解:保留兩串列各自的節點順序,將第二串列接到第一串列尾端。
單向鏈結串列只有頭指標時,若要找到尾端,必須沿著 next 逐節點走訪。若另有尾指標,則可直接取得最後一個節點。
解題方法
逐項判斷操作是否需要走訪節點:
- 修改串列結構時,若必須先找到尾端,需走訪串列。
- 合併排序會分割串列並反覆合併子串列,整體時間為 。
- 串接時,若沒有尾指標,要走訪第一個串列找到尾端;有尾指標時,可直接連接。
選項分析
(A) 錯誤。 鏈結串列是動態資料結構,可依需要配置或釋放節點;節點不必像陣列元素一樣連續存放。
(B) 錯誤。 使用合併排序對鏈結串列排序,遞迴分割的層數為 ,每一層合併所有節點需 ,因此最壞時間為
不是 。
第 15 題
Consider two doubly linked lists of integers. One has different member values from those of the other. Show the correct one(s) below.
(A) When deleting an element with some value, the time complexity is .
(B) If these lists are sorted in the same order, the time complexity is when merging them.
(C) When sorting one of them, whose length is , the time complexity in the worst case is .
(D) When merging and sorting them, the time complexity in the worst case is .
(E) These lists can be constructed as a binary search tree in time in the worst case.
登入後即可作答並保存紀錄。
核心觀念
雙向鏈結串列可在已知節點位置時,以 時間刪除節點;但若只給定值,必須先從串列中搜尋該值。兩條已排序串列可用雙指標合併,時間與總節點數成正比。
排序的複雜度取決於排序方法。對鏈結串列,可用合併排序達到 ;排序兩條串列合併後的全部元素,總數為 ,因此時間為 。
解題方法
逐項判斷是否需要搜尋、是否能線性合併,以及題目宣稱的複雜度是否符合標準高效率做法。題目未指定排序法;以下以鏈結串列常用的合併排序作為排序方法,並以緊確的漸近複雜度判斷。
選項分析
(A) 正確。 要刪除具有指定值的節點,最壞情況下須搜尋整條長度為 的串列,耗時 。找到節點後,更新前後節點的連結只需 ,總時間仍為 。
(B) 正確。 若兩條串列已依相同方向排序,從兩個串列的首端開始比較,每次取出較小(或較大)的值並移動對應指標。每個節點至多被檢查、處理一次;
第 16 題
For the sorting of a series of random numbers, show the following correct one(s):
(A) Applying a queue is faster than applying a stack.
(B) Applying a linked list is faster than applying an array.
(C) Applying a circular linked list is faster than applying a linked list.
(D) Applying a doubly linked list is faster than applying a single linked list.
(E) Applying a binary tree is faster than applying a doubly linked list.
登入後即可作答並保存紀錄。
核心觀念
題目問的是「用哪一種資料結構來輔助排序隨機數」比較快。每個選項都要想成:把資料逐一放進該結構、最後依序取出,需要多少時間。
| 結構 | 排序方式 | 時間(隨機資料) |
|---|---|---|
| 陣列 | 快速排序/合併排序 | |
| 單向/雙向/環狀鏈結串列 | 逐一插入到正確位置(插入排序) | |
| 二元搜尋樹 | 逐一插入後中序走訪(tree sort) | 平均 |
| 堆疊、佇列 | 只能存取兩端,本身不提供排序能力 | — |
選項分析
- (A) 錯誤。 佇列與堆疊都只能在端點存取,兩者都不會讓排序變快,沒有誰比較快的道理。
- (B) 錯誤。 鏈結串列不能隨機存取,難以套用快速排序等高效率方法;用插入方式排序是 。
第 17 題
For a doubly linked list and a binary search tree, show the correct one(s) below:
(A) A doubly linked list can be used to implement a binary search tree.
(B) The time complexity of the implementation is in the worst case.
(C) The time complexity for the implementation is on average.
(D) For a binary search tree, the time complexity for search is on average.
(E) The search time in a binary search tree is in the worst case.
登入後即可作答並保存紀錄。
核心觀念
本題考查二元搜尋樹(BST)的節點表示方式,以及建立與搜尋的時間複雜度。以下以「將 個鍵值逐一插入標準、未平衡的 BST」為前提。
BST 中,每個節點最多有左、右兩個子節點;若採用指標表示,每個節點需要兩個連結欄位。雙向鏈結串列節點也有兩個連結欄位,因此可將原本的前、後指標分別用作左、右子節點指標。
解題方法
設 BST 的高度為 。搜尋或插入一個鍵值時,從根節點開始,依鍵值大小選擇左子樹或右子樹,最多走訪 層,因此單次操作時間為 。
- 若樹大致平衡,,逐一插入 個鍵值的總時間為 。
- 若每次插入都使樹向單側延伸,。逐一插入時,各次操作成本依序可達 ,總時間為
- 單次搜尋在平均情況下,假設鍵值插入順序隨機且分布合理,樹高平均為 ;最壞情況下樹高為 。
選項分析
第 18 題
For a binary search tree, which one(s) of the following is (are) correct?
(A) Each element has a key, and each pair of elements have distinct keys.
(B) The keys (if any) in the left subtree are smaller than the key in the root.
(C) The keys (if any) in the right subtree are smaller than the key in the root.
(D) The left and right subtrees are also binary search trees.
(E) The height of a binary search tree is in general.
登入後即可作答並保存紀錄。
核心觀念
二元搜尋樹(BST)的核心性質是:每個節點的鍵值都能依照大小關係,分布在根節點的左、右子樹中;而且左右子樹本身也必須是二元搜尋樹。
本題採用標準定義:每個節點有一個鍵值,且不同節點的鍵值互異。對任一節點而言:
- 左子樹中的鍵值都小於該節點的鍵值。
- 右子樹中的鍵值都大於該節點的鍵值。
- 左右子樹各自也符合二元搜尋樹的定義。
解題方法
逐項對照二元搜尋樹的定義,再檢查高度是否能由節點數一般性地界定為對數級。BST 的形狀會受插入順序影響;只有樹形夠平衡時,高度才是 ,一般情況下不能保證如此。
選項分析
第 19 題
According to the infix regular expression , which one(s) of the statements is (are) correct?
(A) Its prefix form is .
(B) Its postfix form is .
(C) The operators are , , , , , and . If the token number of an infix expression is in the worst case, its tree height is .
(D) Same as Part (C), the length of its prefix form is .
(E) Similarly, the length of its postfix form is .
登入後即可作答並保存紀錄。
核心觀念
中序式轉成前序式或後序式時,先依照運算子優先順序與括號確定運算樹,再以不同順序走訪運算樹:
- 前序式:根節點、左子樹、右子樹。
- 後序式:左子樹、右子樹、根節點。
- 樹高取決於運算樹的形狀;最壞情況可能退化成單鏈。
- 每個運算元與運算子在前序式、後序式中各出現一次,因此輸出長度與中序式的 token 數量同階。
解題方法
原式為
依照括號與優先順序,其運算結構是:先算 ,再乘上 ;分母為 ;最後將兩部分相加。因此運算樹的根節點是 ,左子樹是 ,右子樹是 。
以前序走訪得到:
以後序走訪得到:
選項分析
第 20 題
Given a binary heap of integers whose amount is , show the correct one(s).
(A) If it is a minimum heap, getting the minimum from the heap needs time in the worst case.
(B) Similarly, if it is a maximum heap, getting the maximum from the heap needs time.
(C) Transforming a list of random integers into an ordered single linked list needs time.
(D) Transforming a series of random integers into a maximum heap needs time.
(E) Transforming a minimum heap of elements to a maximum heap needs time.
登入後即可作答並保存紀錄。
核心觀念
二元堆積(binary heap)是以陣列表示的完全二元樹,並滿足堆積順序性:
- 最小堆積:每個父節點的值小於或等於子節點,因此最小值在根節點。
- 最大堆積:每個父節點的值大於或等於子節點,因此最大值在根節點。
堆積的根節點位於陣列索引 ,直接讀取根節點需要 時間。若要移除根節點,才需將末端元素移至根部並向下調整,時間為 。
另外,將任意 個元素建立成堆積,可使用自底向上的堆積化(heapify),時間為 ,不必逐個插入而花費 。
解題方法
逐一判斷各敘述涉及的操作:
- 從堆積取得極值,先確認題目指的是讀取根節點,還是移除根節點。
- 將資料排成有序串列,視為排序問題;在一般比較模型下,排序需要 時間。
- 建立或轉換堆積,採用自底向上的堆積化,分析所有節點調整成本的總和。
選項分析
(A) 錯誤。 最小堆積的最小值位於根節點,直接讀取只需 ,不是最壞情況 。若操作是移除最小值,則需調整堆積,時間為 ,仍不是 。
(B) 正確。 最大堆積的最大值位於根節點,直接讀取根節點所需時間為 。
第 21 題
Given , which of the following are the asymptotic bound(s) of ?
(A)
(B)
(C)
(D)
(E) None of the above
登入後即可作答並保存紀錄。
核心觀念
這題考遞迴式的漸近分析,可使用主定理(Master Theorem)。對於
比較 與 的成長速度。本題中 、、。假設 ,且 可視為 的次方。
解題方法
先計算遞迴部分的基準成長率:
因為 ,所以 的成長速度低於 ,即 多項式級地較大。
再檢查主定理第三種情況的規則性條件:
因此可取常數 ,使得 。符合主定理第三種情況,得到:
第 22 題
What is the time complexity of the following algorithm?
🖼️【此處有附圖,見下方】
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
遞迴程式的時間複雜度可由「每次呼叫執行的工作量」與「遞迴呼叫次數」推導。若每次只做常數時間的工作,並依序呼叫規模減少 的子問題,總時間通常與遞迴深度成正比。
解題方法
圖中的程式在 時直接回傳;否則呼叫 sum(n-1),再加上 :
int sum(int n) {
if (n <= 1)
return 1;
else
return sum(n-1) + n;
}
每次呼叫只執行一次條件判斷、一次遞迴呼叫及一次加法,這些工作皆為常數時間。因此,對 ,時間遞迴式為
展開可得
第 23 題
The insertion sort algorithm in the following sorts the elements in . Which of the following are true?
🖼️【此處有附圖,見下方】
(A) The missing code at Line 6 is “”.
(B) The missing code at Line 6 is “”.
(C) The algorithm sorts in ascending order.
(D) The insertion sort algorithm is a stable sorting algorithm.
(E) None of the above are true.
登入後即可作答並保存紀錄。
核心觀念
插入排序每一輪取出一個待插入元素 key,再把已排序區段中應排在它後面的元素往右移,最後將 key 放入空出的位置。排序方向由 while 條件中的比較符號決定;排序是否穩定,則要看相等元素是否會改變原本的先後次序。
解題方法
圖中的程式從 開始,令 ;第 9 行條件是 arr[j] <= key,符合條件時便把 arr[j] 往右移,最後將 key 放在 arr[j+1]。因此第 6 行應取出目前要插入的元素 arr[i]。由於比 key 小或相等的元素會被移到右邊,程式會將陣列排成遞減順序。
key = arr[i]
j = i - 1
while (j >= 0 && arr[j] <= key) {
arr[j + 1] = arr[j]
j = j - 1
}
arr[j + 1] = key
例如插入 key = 4 到已排序區段 [7, 5, 2]:7 和 5 都大於 4,不會移動;4 放在 5 後面,結果為 [7, 5, 4, 2]。這也驗證了排序方向是遞減。
第 24 題
Assume we use an array to keep a max-heap such that the parent node of is and . After we extract the largest elements from the max-heap, which of the following elements will remain in the array?
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
最大堆(max-heap)是完全二元樹,且每個父節點都不小於子節點,因此根節點是目前最大的元素。執行一次 extract-max 時,移除根節點,把堆尾元素移到根部,再向下調整,使最大堆性質恢復。
題目寫明 ,而給定陣列符合零起始索引的最大堆規則: 的父節點索引是 。題目所寫的 與 為根節點不相容;若照該公式檢查, 的父節點會是 ,不符合最大堆性質。
解題方法
依符合給定陣列的零起始索引規則,連續執行 6 次移除最大值。每次移除根節點後,將堆尾元素移到根部,並依序與較大的子節點交換。
| 次數 | 移除的最大值 | 調整後的堆 |
|---|---|---|
| 1 | 94 | |
| 2 | 82 |
第 25 題
Following is the quicksort algorithm from Wikipedia (https://en.wikipedia.org/wiki/Quicksort). Assume that the input array has elements. Which of the following statements are true?
🖼️【此處有附圖,見下方】
(A) If the numbers in are already sorted in ascending order, the running time of the quicksort algorithm will be .
(B) If the numbers in are already sorted in descending order, the running time of the quicksort algorithm will be .
(C) If the numbers in are all identical, the running time of the quicksort algorithm will be .
(D) There are calls to partition.
(E) The depth of recursion is .
登入後即可作答並保存紀錄。
核心觀念
本題考查固定選擇最後一個元素作為樞紐(pivot)的快速排序:
- 執行時間取決於各次分割的子陣列大小,以及每次
partition掃描的成本。 partition的總呼叫次數是整棵遞迴樹的分割節點數。- 遞迴深度是遞迴樹最長路徑的長度,不能直接等同總呼叫次數。
若每次分割都只排定一個元素,剩下 個元素繼續排序,時間為 ;若每次分割大致平均,時間為 。
解題方法
圖中程式以 A[hi] 為 pivot,迴圈從 lo 掃描到 hi,包含 pivot 本身,比較條件為 A[k] <= pivot。partition 回傳位置 後,遞迴處理 lo 到 與 到 hi,且只有至少含兩個元素的區間才呼叫 partition。
關鍵程式為:
pivot := A[hi]
i := lo - 1
for k := lo to hi do
if A[k] <= pivot then
i := i + 1
swap A[i] with A[k]
return i
當 時,pivot 本身必定通過比較,因此被交換到最後的 位置。這就是回傳的 pivot 位置,圖中不需要再補一次交換。
設目前子陣列有 個元素,partition 掃描全部 個元素,成本為 。若左右分別有 、 個元素,則
因此,判斷各選項的關鍵是 pivot 的位置與遞迴樹形狀。
選項分析
(A) 正確:遞增排序時,時間為 。
最後一個元素是最大值,所有元素都符合 A[k] <= pivot,所以回傳 。右側為空,左側有 個元素,且仍維持遞增排序。
每次都產生大小為 與 的分割:
各次掃描的元素數為 ,故
(B) 正確:遞減排序時,時間為 。
第一次的 pivot 是最小值,只有 pivot 本身通過比較,因此被交換到最左端,回傳 。
交換後,原本最左端的最大值移到最後一個位置,成為下一次分割的 pivot。此時 pivot 又被排定在最右端。後續持續交替取到剩餘區間的最小值與最大值,每次都只有一側含 個元素。
例如:
第 26 題
Assume we have keys stored in an open-addressing hash table with slots. Which of the following statements are true? The hash function can be computed in time.
(A) Finding the largest key can be done in time in the worst case.
(B) Finding the successor key (the smallest key that is larger than a given key) in the table can be done in time in the worst case.
(C) With proper selection of hash function, the worst-case search time can be .
(D) It is not possible to store keys in an open-addressing hash table with slots.
(E) None of the above are true.
登入後即可作答並保存紀錄。
核心觀念
這題考查開放定址雜湊表的容量、雜湊搜尋,以及在未排序資料中尋找最大值或後繼元素的時間複雜度。
- 開放定址法將每個鍵直接存放在表格槽位中; 個槽位最多可存放 個鍵。
- 若沒有可利用的排序資訊,掃描全部 個鍵可以找到最大值,時間為 。
- 若先排序,再查找某鍵的後繼,總時間可控制在 。
- 若鍵集合固定,且雜湊函數能讓集合中的每個鍵映射到不同槽位,便可用完美雜湊達成最壞情況 查找。
解題方法
逐項判斷各操作是否有符合敘述的演算法或資料配置。
尋找最大鍵時,逐一檢查所有槽位並記錄目前最大值即可,需檢查 個鍵。尋找後繼時,可先將鍵排序,再用排序結果定位給定鍵及其後一個鍵,排序成本為 ,定位成本不會超過 ,因此總時間仍為 。
對於最壞情況 搜尋,固定鍵集合可使用完美雜湊函數,讓每個已儲存鍵直接映射至自己的槽位;搜尋時只需檢查該槽位。
選項分析
第 27 題
Assume we use chaining to handle collision in hash table , which has slots and uses the hash function (i.e., is placed at ). If we insert and into the hash table, which table slot will have more than two keys?
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
雜湊函數 以除以 的餘數決定鍵值所在的槽位。採用鏈結法(chaining)時,雜湊到同一槽位的鍵值會串在該槽位的鏈結串列中,因此只要計算各鍵值的餘數並統計每個槽位的鍵數即可。
解題方法
逐一計算鍵值對 取餘數:
| 鍵值 | 槽位 | |
|---|---|---|
整理各槽位的鍵值:
第 28 題
Which of the following are true about a binary search tree?
(A) The time complexity of inserting a node is for a tree with height .
(B) The time complexity of deleting a node is for a tree with height .
(C) The time complexity of finding the maximum node is .
(D) The space complexity of a binary search tree with nodes is .
(E) The shape of a binary search tree is uniquely defined by the set of keys stored in the tree.
登入後即可作答並保存紀錄。
核心觀念
二元搜尋樹(BST)滿足:對任一節點,其左子樹的鍵值小於該節點,右子樹的鍵值大於該節點。搜尋、插入與刪除時,通常沿著樹的一條路徑往下操作,因此時間複雜度與樹高 有關。
若樹高以邊數計算,根節點深度為 ;以節點層數計算只會相差常數,不影響以下漸近複雜度。
解題方法
逐項判斷選項所述複雜度或性質是否對所有二元搜尋樹都成立。特別留意:未平衡的 BST 高度可能達到 ,因此不能把高度複雜度直接視為常數時間。
選項分析
(A) 正確。 插入時從根節點開始,比較鍵值並沿左、右子樹往下走,直到找到適合插入的位置。最多走過樹高量級的路徑,因此時間複雜度為 。
(B) 正確。 刪除前需先沿樹搜尋目標節點,耗時 。若目標節點有兩個子節點,通常以右子樹中的最小節點或左子樹中的最大節點替代;尋找該替代節點也只需沿一條高度不超過 的路徑。因此整體時間複雜度仍為 。
第 29 題
Assume we are going to do a tree rotation (could be a left-rotate or a right-rotate) on the root node for each of the following binary search trees. Which of the binary search trees might become taller (the height of the tree gets increased) after the rotation? 🖼️【此處有附圖,見下方】
登入後即可作答並保存紀錄。
核心觀念
樹高是根節點到最深葉節點的最長路徑長度。以下以「邊數」計算;若改用節點數,各樹高度都只會多 ,不影響是否變高的判斷。
單旋轉會改變根節點附近的父子關係,因此要比較旋轉前後的最長路徑。題目問哪些樹「可能」變高,只要該樹存在一種合法的根節點旋轉,能讓高度增加即可。
解題方法
圖中五棵樹的根節點分別為: 的 、 的 、 的 、 的 、 的 。對各樹檢查根節點可做的左旋或右旋,再比較旋轉前後的最長路徑。
選項分析
- (A) 正確。 原樹最長路徑為 ,樹高為 。在根節點 右旋後,根變成 ,路徑 長度為 ,樹高增加。
- (B) 正確。 原樹最長路徑為 ,樹高為 。
第 30 題
Given the AVL tree below, which of the following operations would make the tree unbalanced? 🖼️【此處有附圖,見下方】
(A) Insertion of key
(B) Insertion of key
(C) Insertion of key
(D) Deletion of key
(E) Deletion of key
登入後即可作答並保存紀錄。
核心觀念
AVL 樹要求每個節點的左右子樹高度差不超過 。定義平衡因子為
採用「空樹高度為 、葉節點高度為 」的慣例,則
只要有任一節點滿足 ,整棵樹就不符合 AVL 平衡條件。本題判斷的是插入或刪除後、尚未進行旋轉修復時是否失衡;各選項皆獨立作用於原樹。
解題方法
依附圖,根節點為 ,左右子節點分別為 、; 的左右子節點為 、,而 的左右子節點為 、。右子樹中, 的左右子節點為 、, 的左右子節點為 、,另有 的右子節點 與 的右子節點 。
插入時先依二元搜尋樹規則找到位置;刪除時,本題兩個待刪節點皆為葉節點,可直接移除。接著沿受影響路徑由下往上更新高度與平衡因子。
原樹的關鍵高度為
選項分析
(A) 插入鍵值 :會失衡。
搜尋路徑為 ,因此 成為 的左子節點。
插入後,;節點 的左子樹高度為 、右子樹高度為 ,所以
節點 的右子節點 仍是葉節點,因此
節點 失衡,故 (A) 正確。
(B) 插入鍵值 :不會失衡。
搜尋路徑為 ,因此 成為 的右子節點。插入後
第 31 題
The knapsack problem:
Given items with values and weights , , and a knapsack with maximum weight , where , , and are positive integers. Which of the following statement(s) is (are) correct?
(A) The fractional knapsack problem is to find the maximum value of , subject to the constraints and , to . This problem can be solved by a greedy method.
(B) The fractional knapsack problem has a polynomial-time algorithm to solve it.
(C) The 0-1 knapsack problem is to find the maximum value of , subject to the constraints , where or , to . This problem can be solved by dynamic programming with time complexity .
(D) The algorithm for solving the 0-1 knapsack problem with time complexity is a polynomial-time algorithm.
(E) The decision version of the fractional knapsack problem is in NP.
登入後即可作答並保存紀錄。
核心觀念
本題考查分數背包與 0-1 背包的模型差異、貪婪法與動態規劃的適用範圍,以及「多項式時間」和「偽多項式時間」的區別。
- 分數背包允許取物品的一部分,因此 可為 到 之間的實數。
- 0-1 背包不允許拆分物品,因此 。
- 若演算法時間複雜度以數值大小 表示,卻未以輸入長度表示,就要確認它是否只是偽多項式時間。
解題方法
分數背包中,每單位重量的價值為 。將物品依此比值由大到小排列,依序裝入背包;能整件放入時就取整件,剩餘容量不足時則取該物品的一部分。這種策略可由高價值密度開始使用容量,因此能求得最大總價值,且可在多項式時間內完成。
0-1 背包可用動態規劃。令 表示只考慮前 件物品、背包容量為 時的最大價值。對第 件物品,選擇不放入或在容量足夠時放入:
狀態數為 ,所以時間複雜度為 。但 是數值,不是其二進位表示所需的位數;輸入中表示 只需 位元。
第 32 題
Consider the following system of difference constraints:
Which of the following statement(s) is (are) correct?
(A) There are infinitely many solutions for 's, to .
(B) The maximum value of is .
(C) The maximum value of is .
(D) The maximum value of is .
(E) The maximum value of is .
登入後即可作答並保存紀錄。
核心觀念
差分限制式可寫成
並轉換成有向邊 ,邊權重為 。沿著路徑累加限制式,可得到起點與終點變數的上界。例如路徑 給出:
因此, 的最大值由圖中從頂點 到 的最短路徑距離決定,前提是限制式可行。若差分限制圖存在負權重環,則系統無解;本題的圖沒有環,因此系統可行。
解題方法
將各限制式轉成邊:
| 限制式 | 有向邊 |
|---|---|
| ,權重 | |
| ,權重 | |
| ,權重 | |
| ,權重 | |
| ,權重 | |
| ,權重 | |
| ,權重 | |
| ,權重 |
圖中從 出發到 的主要路徑長度為:
最短路徑長度是 ,所以 的最大值為 。
第 33 題
Minimum spanning trees: Which of the following statements is (are) correct?
(A) If an edge is contained in some minimum spanning tree, then it is a light edge crossing some cut of the graph. An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut.
(B) If a graph has a unique minimum spanning tree, then for every cut of the graph, there is a unique light edge crossing the cut.
(C) If, for every cut of the graph, there is a unique light edge crossing the cut, then the graph has a unique minimum spanning tree.
(D) Let be a maximum-weight edge on some cycle of the graph . Then there is a minimum spanning tree of that is also a minimum spanning tree of .
(E) If an edge is a light edge crossing some cut of the graph, then it is contained in every minimum spanning tree.
登入後即可作答並保存紀錄。
核心觀念
割(cut)是把頂點集合 分成兩個非空子集 與 。跨越此割的邊,其兩端點分別位於兩側。跨割邊中權重最小的邊稱為輕邊;若有多條邊並列最小,它們都是輕邊。
本題考查最小生成樹(MST)的割性質與環性質:
- 割性質:跨越某個割的輕邊,至少屬於一棵 MST。若它是該割唯一的輕邊,則屬於每一棵 MST。
- 環性質:若一條邊是某個環上的最大權重邊,則至少存在一棵 MST 不包含它;若它是環上唯一最大權重邊,則任何 MST 都不包含它。
以下假設圖為連通、無向加權圖,且「輕邊」允許有並列最小權重。
解題方法
逐項分辨「至少屬於一棵 MST」與「屬於每一棵 MST」,並留意權重相同時的情形。割性質中,唯一的輕邊必定出現在每棵 MST;若輕邊不唯一,則不能保證某一條特定輕邊出現在每棵 MST。對於環上的最大權重邊,則可透過沿環上的其他邊替換,構造一棵不含該邊且總權重不增加的生成樹。
選項分析
(A) 正確。 假設邊 屬於某棵 MST 。移除 後, 分成兩個連通分量,這兩群頂點形成一個割,而 跨越此割。若存在權重比 更小的跨割邊,以該邊取代 就能得到權重更小的生成樹,與 是 MST 矛盾。因此 是某個割的輕邊。
(B) 錯誤。 唯一 MST 不代表每個割都有唯一輕邊。例如三角形的三條邊權重分別為 。唯一 MST 包含兩條權重為 的邊;
第 34 題
Consider the maximum flow problem in the following graph.
Let be the source node and be the sink node. A flow in the network is indicated by the numbers on the edges. The edge capacities are exactly the same as the flows shown in the graph. For example, the number on an edge represents that the flow is , and that the capacity is also x(s,a)xx.
🖼️【此處有附圖,見下方】
Which of the following statement(s) is (are) correct?
(A) .
(B) The value of the flow shown in the graph is .
(C) The value of the flow shown in the graph is .
(D) The flow shown in the graph is a maximum flow.
(E) The maximum flow value is .
登入後即可作答並保存紀錄。
核心觀念
本題考查流量守恆、流量值的定義,以及最大流與割容量的關係。
- 除源點 、匯點 外,每個節點都必須滿足「流入量=流出量」。
- 流量值是源點的淨流出量,也等於匯點的淨流入量:
- 任一割的容量都是最大流的上界;找到達到該上界的合法流,即可證明它是最大流。
解題方法
依原圖,源點有 、,也有流入源點的 ;匯點有 ,也有流出匯點的 、。其餘關鍵方向為 、、、、、、,各邊容量皆等於原圖所示流量;原卷選項 (C) 的數值為 。
1. 由流量守恆求出未知數
依各中間節點列式:
| 節點 | 流入量=流出量 | 求得 |
|---|---|---|
因此,
2. 計算原圖的流量值
源點的淨流出量為
以匯點驗算,流入量為 ,流出量為 ,故
兩端計算一致。不能直接把流入 的 當成流量值,因為仍有流量從 流出。
3. 證明最大流為
取割
第 35 題
Let be an undirected graph with vertices, where , and let be the adjacency matrix of . Which of the following statement(s) is (are) correct?
(A) is a symmetric matrix.
(B) The degree of vertex is equal to the th row sum.
(C) The number of edges of is equal to .
(D) The graph has an even number of vertices of odd degree.
(E) The graph has an even number of vertices of even degree. Note that is defined as an even number.
登入後即可作答並保存紀錄。
核心觀念
無向圖的鄰接矩陣 中,若頂點 與頂點 相鄰,則 ;否則 。由於無向邊沒有方向, 與 相鄰等同於 與 相鄰,因此鄰接矩陣具有對稱性。
頂點 的度數等於與它相接的邊數。握手定理則指出,所有頂點度數總和等於邊數的兩倍:
解題方法
先用鄰接矩陣的定義判斷 (A)、(B),再檢查矩陣元素總和與邊數的關係。最後由握手定理判斷奇度數頂點的個數奇偶性,並用反例檢驗 (E)。
選項分析
(A) 正確。 因為圖是無向圖,若 與 相鄰,則 與 也相鄰,所以 。因此 是對稱矩陣。
(B) 正確。 第 列的總和為
第 36 題
Assume and consider the decision version of the following problems. Which of the following problems have polynomial-time verification algorithms?
(A) The shortest path problem.
(B) The longest path problem.
(C) Decide whether a positive number is a composite number.
(D) The Hamiltonian-cycle problem.
(E) The maximum flow problem.
登入後即可作答並保存紀錄。
核心觀念
多項式時間驗證,是指給定一個問題實例,以及一份候選證明(certificate),能在輸入長度的多項式時間內檢查這份證明是否有效。能以多項式時間驗證「是」答案的決策問題屬於 NP。
NP 與 P 的差別: 著重於能否在多項式時間內直接求解; 著重於能否在多項式時間內驗證候選解。即使假設 ,NP 完全問題仍有多項式時間驗證演算法;這個假設代表它們沒有多項式時間求解演算法。
解題方法
逐一為各問題列出候選證明,再檢查驗證所需的步驟和資料量是否為多項式。若候選解本身可用多項式長度表示,且其有效性可在多項式時間內檢查,該問題就有多項式時間驗證演算法。
選項分析
-
(A) 最短路徑問題:有。
決策版本可問:給定圖、起點、終點與上限 ,是否存在長度不超過 的路徑?候選證明是一條起點到終點的路徑。逐一檢查相鄰頂點是否有邊,並加總路徑長度與 比較,即可在多項式時間內驗證。此問題本身也有多項式時間求解演算法。 -
(B) 最長路徑問題:有。
標準決策版本可問:是否存在長度至少為 的簡單路徑?候選證明是一條路徑上的頂點序列。檢查相鄰頂點是否有邊、頂點是否重複,以及路徑長度是否達到 ,即可在多項式時間內驗證。此問題一般是 NP 完全問題;
第 37 題
Assume . The MINCUT problem is to partition the vertices in a graph into two disjoint sets so that the number of edges between vertices in different sets is minimized. Choose all and only those complexity classes that are known to contain the corresponding MINCUT decision problem.
(A)
(B)
(C) co-NP
(D) NP-complete
(E) NP-hard
登入後即可作答並保存紀錄。
核心觀念
將 MINCUT 寫成決策問題:輸入一張圖 與整數 ,判斷是否存在一種頂點二分割,使跨越兩組的邊數至多為 。這等價於判斷圖的最小割值是否不大於 。
最小割可在多項式時間內求得;求出最小割值後,與 比較即可。因此 MINCUT 決策問題屬於 。又因為 ,且 對補集封閉,所以 。
題目另給定 。若 MINCUT 是 NP-hard,則每個 問題都能在多項式時間內歸約到 MINCUT。由於 MINCUT 可在多項式時間內求解,這會導致每個 問題都能在多項式時間內求解,也就是 ;這與 合起來會得到 ,違反題目假設。
解題方法
先判定 MINCUT 決策問題能否多項式時間求解,再利用複雜度類別的包含關係判斷它是否也屬於 與 。最後用 排除 NP-hard 與 NP-complete。
選項分析
第 38 題
Assume . The VERTEX-COVER problem is to find a vertex cover of minimum size in a given undirected graph. Choose all and only those complexity classes that are known to contain the corresponding VERTEX-COVER decision problem.
(A)
(B)
(C) NP-complete
(D) NP-hard
(E) The VERTEX-COVER problem has a polynomial-time 2-approximation algorithm.
登入後即可作答並保存紀錄。
核心觀念
題目要分類的是頂點覆蓋的判定問題:給定無向圖 與整數 ,詢問是否存在大小至多為 的頂點集合 ,使每條邊至少有一個端點在 中。
判定問題屬於 ,代表若答案為「是」,給定一組候選頂點後,可以在多項式時間內檢查它的大小是否不超過 ,以及每條邊是否都被覆蓋。頂點覆蓋判定問題是 完全問題,因此也具有 困難性。
解題方法
先辨認題目問的是最小化問題的判定版本,再套用頂點覆蓋判定問題的分類:
- 屬於 ,因為候選解可在多項式時間內驗證。
- 是 完全問題,因為它屬於 ,且為 困難。
- 因為它是 困難問題,所以也符合 困難這項分類。
題目假設 。若頂點覆蓋判定問題能在多項式時間內求解,則由其 完全性可推出 ,與假設矛盾,因此不屬於 。
選項分析
- (A) :錯誤。 在 的假設下, 完全問題不可能屬於 ;否則所有 問題都能在多項式時間內求解,導致 。
- **(B) :正確。
第 39 題
Consider the following 10 problems and algorithms:
(1) Merge sort for the sorting problem.
(2) Heap sort for the sorting problem.
(3) Minimum spanning tree problem.
(4) Floyd-Marshall algorithm for the all-pairs shortest path problem.
(5) Finding a maximum independent set in a time interval graph.
(6) Huffman’s algorithm for the Huffman code problem.
(7) Strassen’s matrix multiplication algorithm.
(8) Constructing an optimal binary search tree for the weighted searching problem, i.e. to minimize the expected searching time.
(9) Longest common subsequence problem.
(10) The Ford-Fulkerson method for the maximum flow problem.
Each of the above 10 problems (or algorithms) can be solved by (or classified as) one of the following design techniques:
- Divide and Conquer
- Dynamic Programming
- Greedy method
- Others (none of the above three techniques)
Among the above 10 problems, there are , , , and of them respectively that can be solved by (or classified as) design techniques , , , and . Find , , , and , then choose the following correct ones.
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
本題考的是常見演算法的典型設計技巧分類:
- 分治法:把問題切成較小的子問題,分別求解後合併結果。
- 動態規劃:將問題拆成有重疊的子問題,保存子問題的最佳解,再組合成整體最佳解。
- 貪婪法:每一步選擇當下符合規則的最佳選擇,逐步建構解。
- 其他:不屬於上述三種典型設計技巧。
依一般教科書的分類,各題如下:
| 題號 | 問題或演算法 | 設計技巧 |
|---|---|---|
| (1) | 合併排序 | 分治法 |
| (2) | 堆積排序 | 其他 |
| (3) | 最小生成樹 | 貪婪法 |
| (4) | Floyd–Warshall 全點對最短路徑 | 動態規劃 |
| (5) | 區間圖最大獨立集 | 貪婪法 |
| (6) | Huffman 編碼 | 貪婪法 |
| (7) | Strassen 矩陣乘法 | 分治法 |
| (8) | 最佳二元搜尋樹 | 動態規劃 |
| (9) | 最長共同子序列 | 動態規劃 |
| (10) | Ford–Fulkerson 最大流方法 | 其他 |
解題方法
逐項依照演算法的典型設計方式分類:
- 分治法有 2 題:合併排序先排序左右兩半再合併;Strassen 將矩陣乘法拆成較小矩陣的乘法與加減運算。因此 。
- 動態規劃有 3 題:Floyd–Warshall 逐步納入中繼點,更新兩點間最短距離;最佳二元搜尋樹以子區間的最佳成本組合成大區間的最佳成本;最長共同子序列以較短前綴的解推得較長前綴的解。因此 。
- 貪婪法有 3 題:最小生成樹可用 Kruskal 或 Prim 演算法;
第 40 題
Assume . For each of the following 10 problems, decide whether it is a P-problem, an NP-hard (or NP-complete) problem, or neither.
(1) Find a longest simple path between two nodes, where the given graph has positive edge weights.
(2) Find a shortest simple path between two nodes in a directed graph with negative and/or positive edge weights, and containing negative weight cycles.
(3) Find a negative weight directed cycle in a weighted directed graph.
(4) Find a positive weight directed cycle in a weighted directed graph.
(5) Find a largest cycle in a graph, where the edge weight is for each edge.
(6) Find a smallest cycle in a graph, where the edge weight is for each edge.
(7) Find a maximum flow in a flow network.
(8) Find a minimum cut in a flow network.
(9) Find a maximum independent set in a time interval graph.
(10) The 2-CNF-Satisfiability problem.
Among the above 10 problems, of them are P-problems, of them are NP-hard (or NP-complete), and of them are neither. Find , , and , then choose the following correct ones.
(A)
(B)
(C)
(D)
(E)
登入後即可作答並保存紀錄。
核心觀念
本題要判斷各問題的計算複雜度:
- P 問題:可在輸入大小的多項式時間內求解。
- NP-hard 問題:至少和 NP 中最難的問題一樣難;若它同時屬於 NP,則稱為 NP-complete。
- neither:在題目假設下,既不屬於 P,也不是 NP-hard。
判斷路徑與環的問題時,須留意「簡單路徑」限制:每個頂點最多經過一次。即使存在負權環,簡單路徑的數量仍有限,但要找其中權重最小的路徑仍可能是 NP-hard。
解題方法與各小題分類
(1) 最長簡單路徑,邊權皆為正:NP-hard。
令所有邊權皆為 ,此時最長簡單路徑就是經過最多邊的簡單路徑。若能有效求出它,就能判斷圖中是否存在經過所有頂點的簡單路徑,這可解決 Hamiltonian Path 問題。因此本題是 NP-hard。
(2) 含負權環的有向圖中的最短簡單路徑:NP-hard。
把每條邊的權重都設為 ,則一條簡單路徑的總權重等於其邊數的負值。找最短簡單路徑,等同於找邊數最多的簡單路徑。為確保圖中含有負權環,可額外加入一個與原圖分離、總權重為負的有向環;這不影響指定起點與終點間的路徑。故此問題仍是 NP-hard。
(3) 尋找有向圖中的負權環:P。
可用 Bellman–Ford 演算法檢查負權環。若要檢查圖中任意位置的負權環,可加入一個連到所有頂點的虛擬源點,再執行 Bellman–Ford;若第 輪仍有距離更新,即存在負權環。時間複雜度為 。
(4) 尋找正權有向環:P。
將每條邊的權重乘以 ,正權環就會變成負權環。接著用 Bellman–Ford 檢查負權環即可,時間複雜度為 。
(5) 尋找最大環,且每條邊權重為 :NP-hard。
最大環就是包含最多邊的簡單環。若圖有 個頂點,最大環包含 條邊,當且僅當圖中存在 Hamiltonian Cycle。因此此問題是 NP-hard。
(6) 尋找最小環,且每條邊權重為 :P。
對每條邊 ,暫時移除該邊,再用廣度優先搜尋尋找從 到 的最短路徑;該路徑加上 就構成一個環。取所有候選環中邊數最少者,即為最小環。逐邊執行 BFS 的時間複雜度為 。
(7) 求最大流:P。