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

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

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

逐題詳解

110 分

A is a 2-dimensional array of real numbers with 100 rows and 100 columns. Assume that each real number occupies 4 bytes, and the address of \(A[0,0]\) is \(B000_{16}\). The index of both row and column is assumed to start from 0.

(1) Compute the address of \(A[43,21]\) if the array \(A\) is stored row-major. (5 points)

(2) Compute the address of \(A[43,21]\) if the array \(A\) is stored column-major. (5 points)

\(B000_{16}=11\cdot4096=45056\)

(1) row-major

\(A[43,21]\) 前面有 \(43\times100+21=4321\) 個元素

address \(=45056+4321\times4=45056+17284=62340=\text{F384}_{16}\)

(2) column-major

前面有 \(21\times100+43=2143\) 個元素

address \(=45056+2143\times4=45056+8572=53628=\text{D17C}_{16}\)

(1) F384₁₆(62340);(2) D17C₁₆(53628)

210 分

Give 2 different binary trees, each with three nodes, that have the same preorder and postorder sequences. (10 points)

每個內部節點只有一個 child 時,這個 child 掛在左邊或右邊,preorder 與 postorder 都不會變

兩棵都是三個節點的單鏈:一棵全往左、一棵全往右

CBA   ABC

兩棵的 preorder 都是 A B C、postorder 都是 C B A,但形狀不同(inorder 一個是 C B A、一個是 A B C)

preorder 加 postorder 不能唯一決定 binary tree;要有 inorder 才行

左鏈 A→B→C 與右鏈 A→B→C

315 分

Given 6 external nodes A, B, C, D, E, and F, and their corresponding weights 2, 3, 5, 7, 9, 13, respectively.

(1) Use Huffman's algorithm to construct a binary tree with the minimal total weighted external path length. (10 points)

(2) Compute the total weighted external path length of the constructed tree in part (1). (5 points)

(1) 每次取最小的兩個合併

\(\{2,3,5,7,9,13\}\) → \(2+3=5\)

\(\{5,5,7,9,13\}\) → \(5+5=10\)(C 與剛合併的 5)

\(\{7,9,10,13\}\) → \(7+9=16\)

\(\{10,13,16\}\) → \(10+13=23\)

\(\{16,23\}\) → \(16+23=39\)

D:716E:939C:510A:25B:323F:13

(2) 各 external node 的深度 × weight

\(D,E,F\) 深度 2、\(C\) 深度 3、\(A,B\) 深度 4

\(7\cdot2+9\cdot2+13\cdot2+5\cdot3+2\cdot4+3\cdot4=14+18+26+15+8+12=93\)

等於所有 internal node 的和:\(5+10+16+23+39=93\)

(2) 93

415 分

Provide three different data structures to represent the graph in Figure 1 with 4 nodes. (15 points)

1234

無向圖,5 條邊:(1,2)、(1,3)、(2,3)、(2,4)、(3,4)

① adjacency matrix 對稱、對角線 0,空間 \(O(n^2)\),查兩點是否相鄰 \(O(1)\)

1234
10110
21011
31101
40110

② adjacency list 每條邊存兩次,空間 \(O(n+e)\)

1 → 2 → 3 → ●

2 → 1 → 3 → 4 → ●

3 → 1 → 2 → 4 → ●

4 → 2 → 3 → ●

③ adjacency multilist 每條邊一個 node (V1, V2, link1, link2),link1 接下一條含 V1 的邊、link2 接下一條含 V2 的邊

nodeV1V2link1link2
N112N2N3
N213nullN3
N323N4N5
N424nullN5
N534nullnull

各點的入口:1→N1、2→N1、3→N2、4→N4

第三種也可以用 edge list(5 筆 (u,v))作答

adjacency matrix、adjacency list、adjacency multilist

525 分

A tree is a connected undirected graph \(G=(V,E)\) which contains no cycles. Many problems can be solved efficiently in trees. For example, it takes super-linear time to solve the maximum matching (a matching is a set of edges without common vertices) problem in general graphs or bipartite graphs. However, in trees, the problem can be solved in linear time. In the input file, each tree is given in several lines: The first line contains an integer \(n\) indicating the number of vertices (denoted by numbers \(1,2,\ldots,n\)) in the tree. The following \(n-1\) lines contain the edges given by a pair of vertex numbers (recall that each tree with \(n\) vertices has exactly \(n-1\) edges), which are separated by a space.

(1) "Do not assume the input is always valid." It is a good advice for programmers. For example, consider the following input: 5 / 1 2 / 2 3 / 1 3 / 4 5. The input indicates that there is a graph with 5 vertices and 4 edges. Note that this graph is not a tree because it is not connected and has a cycle. Design an \(O(n)\) time algorithm to detect whether the given input represents a tree. (7 points)

(2) Argue that the following observation is correct: For each leaf \(v\) and the only edge \((u,v)\) incident to it, there exists a maximum matching \(M\subseteq E\) such that \((u,v)\in M\). (6 points)

(3) Based on the observation in (2), give an \(O(n)\) time algorithm to solve the maximum matching problem in trees. Please describe what data structure is used for storing the given tree, and what mechanisms are used in its implementation to achieve the linear running time. (12 points)

(1) 判斷是不是 tree

輸入已固定是 \(n\) 個點、\(n-1\) 條邊。\(n\) 點 \(n-1\) 邊的圖:connected \(\iff\) tree(連通又只有 \(n-1\) 條邊,擠不出 cycle)

① 讀 \(n-1\) 條邊,先檢查端點是否都在 \(1..n\)(不在就不是 tree),建 adjacency list

② 從 vertex 1 做一次 DFS/BFS,數走到的點數 count

③ \(\text{count}=n\) → tree;否則不是

自環、重複邊也會被檢查到:它們白占一條邊的額度,剩下的邊接不起 \(n\) 個點,count \(\lt n\)

題目的例子從 1 只走得到 \(\{1,2,3\}\),count \(=3\ne5\) → 不是 tree

時間:建表 \(O(n)\),DFS 每個點、每條邊各處理常數次 → \(O(n)\)

(2) leaf edge 在某個 maximum matching 裡

設 \(v\) 是 leaf,\((u,v)\) 是唯一與 \(v\) 相鄰的邊,\(M\) 是任一 maximum matching

\(v\) 已被 \(M\) 配對:能配 \(v\) 的只有 \((u,v)\),所以 \((u,v)\in M\)

\(v\) 沒被配對:\(u\) 一定已被配對,否則 \(M\cup\{(u,v)\}\) 還是 matching 且更大,與 \(M\) maximum 矛盾

設 \((u,w)\in M\),令 \(M^{\prime}=M-\{(u,w)\}+\{(u,v)\}\):\(u\) 仍只用一次、\(v\) 原本沒用、\(w\) 變成沒用 → \(M^{\prime}\) 是 matching,\(|M^{\prime}|=|M|\)

\(M^{\prime}\) 也是 maximum matching 且含 \((u,v)\),得證

(3) \(O(n)\) 演算法

資料結構:adjacency list(\(2(n-1)\) 個 list node),加兩個長度 \(n\) 的 array:parent[]、matched[]

① 任選 root 做一次 DFS(用自己的 stack 迭代,不靠遞迴),記下 parent,並依「離開順序」得 postorder

② 依 postorder(從最下面的 leaf 往 root)掃每個 \(v\ne\text{root}\):

  \(u\leftarrow\text{parent}[v]\);若 matched[v] 與 matched[u] 都是 false,就把 \((u,v)\) 放進 \(M\),兩個都標成 true

③ 傳回 \(M\)

正確性:postorder 最先遇到的 \(v\) 是(剩餘樹的)leaf,由 (2) 把 leaf edge 放進 \(M\) 不吃虧;拿掉 \(u,v\) 之後剩下若干棵樹,同樣論證反覆做

線性的關鍵:adjacency list 讓每條邊只被碰兩次;matched[]、parent[] 是 array,查詢更新 \(O(1)\),不需要搜尋;整體 \(O(n)\)

(1) 檢查連通性(count = n);(2) leaf edge exchange;(3) postorder 貪心配對 O(n)

612 分

Given a set of \(n\) planar points and the corresponding Voronoi diagram \(D\), write an \(O(n)\)-time algorithm to find out the closest pair of points. Note that you can use the fact that the closest pair of points must share a Voronoi edge in the Voronoi diagram and there are at most \(3n-6\) Voronoi edges. (12 points)

closest pair 一定共用一條 Voronoi edge,所以只要看 \(D\) 的每條邊兩側的兩個 site,不必比 \(\binom n2\) 組

\(\text{best}\leftarrow\infty\)

對 \(D\) 的每一條 Voronoi edge \(e\):\((p,q)\leftarrow\) \(e\) 兩側的 site;若 \(\text{dist}(p,q)\lt\text{best}\),更新 \(\text{best},\ (p^*,q^*)\leftarrow(p,q)\)

傳回 \((p^*,q^*)\)

正確性:closest pair 必在其中一條邊兩側,掃遍所有邊不會漏掉

時間:邊數 \(\le3n-6\),每條邊只做一次距離計算與比較(\(O(1)\)),共 \(O(3n-6)=O(n)\);Voronoi diagram 用 DCEL 之類的結構存,每條邊直接記著兩側的 site

掃每條 Voronoi edge 兩側的 site 取最短,\(O(n)\)

713 分

The sum of subsets problem is described as follows. Given a set \(S\) of \(n\) integers and a specific integer \(M\), is there a subset of the integers that sum to \(M\)? Write a non-deterministic algorithm to solve the sum of subsets problem in \(O(n)\) time. (13 points)

non-deterministic algorithm 用 choice 一步「猜」出一個值,再用 \(O(n)\) 驗證

SumOfSubsets(S[1..n], M)

 for i = 1 to n: x[i] = choice(0, 1)  // 猜每個元素選或不選

 sum = 0

 for i = 1 to n: sum = sum + x[i]*S[i]

 if sum == M: success else: failure

choice、success、failure 各 \(O(1)\),兩個迴圈各 \(n\) 次 → \(O(n)\)

non-deterministic 的 \(O(n)\) 指「存在一串猜測能在 \(O(n)\) 內走到 success」;deterministic 要列舉 \(2^n\) 個 subset

猜 x[1..n]∈{0,1},驗證 Σx[i]S[i]=M,O(n)

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com