免費詳解 / 中央 / 軟體 / 102

102 中央資工所考古題解答|軟體逐題詳解

102 中央資工所軟體考古題的逐題詳解,共 6 題,免費公開。

逐題詳解

125 分

There are N courses in a computer science department. Each course contains two data fields: (1) number (3 digits) as key and (2) name (string). Insert the following 4 courses:

123 Data Structure / 234 Software Engineering / 345 Programming Language / 233 Compiler

into each of the 5 data structures below, and draw a sketch for each. Note that you should draw each and every data field, including pointers. Null pointers should be specified as ●.

(1) array (5%) (2) doubly-linked list (need a dummy node) (5%) (3) red-black tree (need a color field) (5%) (4) chained hashing with a hash table of size 11 and a hash function \(h\) where \(h(\text{key})=\text{key}\bmod11\) (5%) (5) heap (5%)

(1) array 依插入順序放進 index 0、1、2、3,每格兩個欄位(key、name)

123Data Structure0234Software Engineering1345Programming Language2233Compiler3…N-1

(2) doubly-linked list(dummy node) dummy 不放資料,尾端接在最後;每個 node 有 prev、key、name、next,第一個資料 node 的 prev 指回 dummy,dummy.prev 與最後一個 node 的 next 是 null(●)

dummy123Data Structu…234Software Eng…345Programming …233Compilerprev=●(null)next=●(null)

(3) red-black tree 每個 node 有 left、key、name、color、right

插 123:root,塗黑 → 插 234:紅,在 123 右邊

123234

插 345:紅,在 234 右邊,parent 234 是紅且 uncle 為 null(黑) → RR case,對 123 左旋,234 塗黑、123 塗紅

123234345 → 123234345

插 233:\(233\lt234\) → 往左 \(233\gt123\) → 123 的右邊,紅色;parent 123 與 uncle 345 都是紅 → 兩個塗黑、234 塗紅,234 是 root 再塗黑

123233234345 → 123233234345

234Software Engineeringcolor: BLACK123Data Structurecolor: BLACK345Programming Languagecolor: BLACK233Compilercolor: RED● = null pointer

(4) chained hashing

\(123\bmod11=2\),\(234\bmod11=3\),\(345\bmod11=4\),\(233\bmod11=2\)(與 123 同一格,新的接在最前面)

012345678910233 Compiler123 Data Structure234 Software Eng.345 Prog. Language

chaining 若接在串列尾端,index 2 的順序變成 123 → 233;兩種都被接受

(5) heap 取 min-heap(key 小的在上面)

插 123 → [123];插 234 → [123, 234];插 345 → [123, 234, 345]

插 233 放在 index 4,parent 是 index 2 的 234,\(233\lt234\) 交換;再比 index 1 的 123,\(123\lt233\) 停

1231233234532344

234233123345

heap 沒指定 min/max;若取 max-heap,結果為 [345, 233, 234, 123](插 234 與 123 交換、插 345 上到 root、插 233 與 123 交換)

3451233223431234

見上列五張圖

28 分

Insert the integers 4, 2, 6, 1, 3, 5, 7 into an initially empty binary search tree.

(1) Show the resulting tree. (4%) (2) Show the result of deleting 4 and 2 from the tree in (1) above. (4%)

(1) 4 當 root;2、6 在左右;1、3 在 2 下;5、7 在 6 下

1234567

(2) 刪 4(root、兩個子樹):用 inorder successor(右子樹最小的 5)取代,並把原來的 5 拿掉

123567

刪 2(兩個子樹):用 inorder successor 3 取代

13567

若改用 inorder predecessor:刪 4 以 3 取代、刪 2 時只剩左子 1,直接接上,結果為 3 為 root,左 1、右 6(5, 7)

(1) 完全二元樹;(2) 5 為 root,左子樹 3(1, —),右子樹 6(—, 7)

310 分

The following array represents a binary tree. The first row is the index, and the second row indicates the content of each node. The concept here is similar to the implementation of (min or max) heap using the array.

index1234567891011121314151617181920
nodeABCDEFG

(1) Is it possible for index 14 to contain some information? Why or why not? (2%) (2) Give the result of the inorder traversal of the tree. (2%) (3) preorder (2%) (4) postorder (2%) (5) level order (2%)

index \(i\) 的 left child 是 \(2i\)、right child 是 \(2i+1\),parent 是 \(\lfloor i/2\rfloor\)

A(1)→ B(2)、C(3);B 的 right 是 5(D);C 的 left 是 6(E);E 的 children 是 12(F)、13(G)

BDAFEGC

(1) 14 的 parent 是 \(\lfloor14/2\rfloor=7\),而 index 7 是空的 → 沒有 parent 就不可能有 node → 不可能

(2) inorder:B D A F E G C

(3) preorder:A B D C E F G

(4) postorder:D B F G E C A

(5) level order:A B C D E F G

(1) 不可能(parent 7 為空);(2) B D A F E G C;(3) A B D C E F G;(4) D B F G E C A;(5) A B C D E F G

424 分

Consider the following variation of the Minimum Spanning Tree Problem: Given a graph \(G\) of \(n\) vertices and \(m\) edges AND a minimum spanning tree \(T\) of graph \(G\), we wish to add a new edge \(e\) with weight \(w_e\) to \(G\) forming a new graph \(G'\) and construct the minimum spanning tree of the new graph \(G'\).

(1) Give an algorithm which constructs the minimum spanning tree of \(G'\) in time faster than Prim's or Kruskal's algorithm. (Zero credit for giving an algorithm which simply constructs the minimum spanning tree of \(G'\) from scratch. For full credit, your algorithm should run in \(O(n)\) time.) (12%)

(2) Prove (argue convincingly) your algorithm is correct. (8%) (3) Analyze the running time of your algorithm. (4%)

設 \(e=(u,v)\)

(1) 演算法

① 在 \(T\) 上從 \(u\) 做 BFS/DFS,記下每個點的 parent,找到 \(v\);沿 parent 從 \(v\) 走回 \(u\),得唯一的 path \(P\)

② 在 \(P\) 上找權重最大的邊 \(f\)

③ 若 \(w_e\lt w(f)\):\(T'=T-f+e\);否則 \(T'=T\)

(2) 正確性

\(T+e\) 恰有一個 cycle \(C=P+e\)

(a) \(G'\) 中的邊 \(g\notin T\cup\{e\}\):\(T\) 是 \(G\) 的 MST,所以 \(g\) 是 \(g\) 與 \(T\) 上 path 組成的 cycle 中最重的邊(cycle property)。這個 cycle 在 \(G'\) 裡依然存在 → \(g\) 仍是某個 cycle 的最重邊,\(G'\) 的 MST 可以不用 \(g\)

(b) 所以 \(G'\) 的 MST 就是 \(T\cup\{e\}\)(\(n\) 點、\(n\) 邊、只有一個 cycle \(C\))的 MST

(c) 要把 \(n\) 條邊砍到 \(n-1\) 條就必須從 \(C\) 拿掉一條;cycle property:\(C\) 上最重的邊不在某個 MST 裡 → 拿掉 \(C\) 上最重的邊

\(C\) 上最重的邊 \(=\max(w(f),w_e)\):\(w_e\lt w(f)\) 拿掉 \(f\);否則拿掉 \(e\)(即 \(T'=T\)) → 與 ③ 一致,得證

(3) 時間

\(T\) 只有 \(n-1\) 條邊,BFS/DFS 找 path 為 \(O(n)\);走回 \(u\) 找最大邊最多 \(n-1\) 步;換邊 \(O(1)\)

總共 \(O(n)\),比 Prim/Kruskal(\(O(m\log n)\))快,且與 \(m\) 無關

\(O(n)\):找出 \(T\) 上 \(u\)–\(v\) path 的最大邊 \(f\),若 \(w_e\lt w(f)\) 就換成 \(e\)

521 分

For an undirected graph \(G=(V,E)\), a vertex \(v\) in \(V\), and an edge \((x,y)\) in \(E\), let \(G\backslash v\) denote the subgraph of \(G\) obtained by removing \(v\) and all the edges incident to \(v\) from \(G\); and let \(G\backslash(x,y)\) denote the subgraph of \(G\) obtained by removing the edge \((x,y)\) from \(G\). If \(G\) is connected, then \(G\backslash v\) can be disconnected or connected. For example, if \(G\) is a tree, \(G\backslash v\) is connected for any leaf node \(v\); otherwise, it is disconnected. Note also that if \(G\) is a tree, \(G\backslash(x,y)\) is disconnected for any edge \((x,y)\).

(1) Prove that for any connected graph \(G\), we can always find a vertex \(v\) in \(G\) such that \(G\backslash v\) is connected. (5%)

(2) Given a connected graph \(G\), design an \(O(|V|)\) time algorithm to find a vertex \(v\) in \(G\) such that \(G\backslash v\) is connected. (8%)

(3) Given a connected graph \(G\), design an \(O(|V|)\) time algorithm to either find an edge \((x,y)\) in \(G\) such that \(G\backslash(x,y)\) is connected or report that no such an edge exists. (8%)

(1) \(G\) 連通,有 spanning tree \(T\)

\(|V|\ge2\) 時 \(T\) 至少有兩個 leaf;取其中一個 leaf \(v\)

\(T\backslash v\) 連通,且包含 \(G\backslash v\) 全部的點 → \(G\backslash v\) 連通,得證(\(|V|=1\) 時 \(G\backslash v\) 是空圖,不討論)

(2) 對 \(G\) 做 DFS(或 BFS)建出 spanning tree,傳回任一個 leaf,例如 BFS 最後被走到的點、DFS 中最後一個加入的點

正確性就是 (1):leaf 拿掉後 spanning tree 仍連通

時間:走一次 tree 的 \(|V|-1\) 條邊;掃 adjacency list 的成本是 \(O(|V|+|E|)\)

題目寫 \(O(|V|)\):建 tree 本身只動 \(|V|-1\) 條邊,讀 adjacency list 的 \(|E|\) 是線性時間輸入成本;(3) 才是真的 \(O(|V|)\)

(3) \(G\backslash(x,y)\) 連通 \(\iff\) \((x,y)\) 不是 bridge \(\iff\) \((x,y)\) 在某個 cycle 上

做 DFS,每掃到一條邊 \((a,b)\):

  \(b\) 沒走過 → tree edge,往下走

  \(b\) 走過,且是 \(a\) 剛來的 parent(同一條邊) → 略過

  \(b\) 走過,不是 parent → non-tree edge,它與 tree 上 \(a\)–\(b\) 的 path 形成 cycle,刪掉它 \(G\) 仍連通 → 傳回 \((a,b)\) 並立刻停止

整個 DFS 掃完都沒遇到 → \(G\) 是 tree,每條邊都是 bridge → 回報沒有這樣的邊

時間:停止之前每掃一條邊不是 tree edge(至多 \(|V|-1\) 條)就是回到 parent 的那一條(至多 \(|V|-1\) 條),再加最後一條,共掃至多 \(2|V|-1\) 條 → \(O(|V|)\),與 \(|E|\) 無關

(1) 取 spanning tree 的 leaf;(2) DFS tree 的 leaf;(3) DFS 遇到第一條 non-tree edge 就傳回,沒有就是 tree

612 分

On the real line, a closed interval \([a,b]\) is said to be unit-length if \(b-a=1\). Given a set \(\{x_1,x_2,\ldots,x_n\}\) of \(n\) points on the real line, we say that a set \(S\) of unit-length closed intervals covers \(\{x_1,x_2,\ldots,x_n\}\) if each point \(x_i\) is contained in some interval of \(S\). The problem is to find such an \(S\) with minimal size \(|S|\). Assume that the given points are sorted as \(x_1\le x_2\le\cdots\le x_n\). Design an \(O(n)\) time algorithm to solve this problem. (12%)

Greedy:永遠從最左邊還沒被蓋到的點開始放一個區間

\(S\leftarrow\varnothing,\ i\leftarrow1\)

\(i\le n\) 時:把 \([x_i,\,x_i+1]\) 放進 \(S\);\(j\leftarrow i\),當 \(j\le n\) 且 \(x_j\le x_i+1\) 就 \(j\leftarrow j+1\);\(i\leftarrow j\)

傳回 \(S\)

最佳性(exchange argument)

最左的點 \(x_1\) 一定被某個區間 \([a,a+1]\)(\(a\le x_1\))蓋住;把它右移成 \([x_1,x_1+1]\),原本它蓋住的點都 \(\ge x_1\) 且 \(\le a+1\le x_1+1\),仍被蓋住

→ 存在最佳解含 \([x_1,x_1+1]\);剩下的是 \(x_j\gt x_1+1\) 的點,同樣的子問題,歸納可得 greedy 最佳

時間:\(i\)、\(j\) 只往右走,每個點只掃一次 → \(O(n)\)(已排序,不需再排)

greedy:每次從最左未蓋到的點 \(x_i\) 放 \([x_i,x_i+1]\),\(O(n)\)

109–115 的詳解也是這樣寫的。

國立中央大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 510 頁,逐題推導。

購買 · NT$ 850 看中央其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

題目有錯、詳解看不懂,都歡迎來信

想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。

william.chou.123123@gmail.com