免費詳解 / 台大 / 軟體 / 104

104 台大資工所考古題解答|軟體逐題詳解

104 台大資工所軟體考古題的逐題詳解,共 6 題,免費公開。

逐題詳解

130%

1. (30 points) In this problem, and this problem only, please choose among the following items and show your answers by the item labels.

[A] \(O(1)\) [B] \(O(n)\) [C] \(O(\log n)\) [D] \(O(n\log n)\)

The vector data structure is basically a flexible-size array, i.e., the number of elements inside the vector can be unlimited. The vector data structure can be implemented with either the usual fixed-length array or a singly linked list.

The vector can be implemented by a fixed-length array with an auxiliary length information without wasting any other space. Then, accessing an element within the vector by index is trivial. But in the worst case, when an element is inserted into the vector, a new array needs to be created and all the contents in the original array need to be moved; similarly, when an element is removed from the vector, the creating and moving steps need to be performed, too.

The vector can also be implemented by a singly linked list without wasting any other space. Then, inserting an element into or removing an element from the vector by index does not need moving any contents.

Assume there are \(n\) elements in a vector \(V\), what is the asymptotic worst-case running time for each operation under different implementations?

arraylinked list
Access(V,i)(1)(2)
Insert(V,x,i)(3)(4)
Delete(V,i)(5)(6)

Here \(Access(V,i)\) returns the element at the \(i\)-th position in \(V\), \(Insert(V,x,i)\) inserts an element \(x\) into the \(i\)-th position in \(V\), and \(Delete(V,i)\) deletes an element at the \(i\)-th position in \(V\). Both \(Insert\) and \(Delete\) will change the size of \(V\).

Besides the useful singly linked list, doubly linked list is a more convenient data structure, since a node can have two links point to both its successor and predecessor. Consider a list \(L\) that consists of \(n\) objects with one root pointer and one tail pointer pointing to its first and last object. What is the asymptotic worst-case running time for each dynamic-set operation listed in the following table?

singly linked listdoubly linked list
Search(L,k)(7)(8)
Remove(L,p)(9)(10)

Here \(Search(L,k)\) returns the object whose key equals \(k\) in \(L\), and \(Remove(L,x)\) removes a node pointed by pointer \(p\) from \(L\).

(1) array 用 index 直接算位址 → [A]

(2) linked list 要從 head 一個一個走到第 \(i\) 個 → [B]

(3) array insert:要建新 array、把 \(n\) 個元素搬過去 → [B]

(4) linked list insert 不用搬資料,但要先走到第 \(i\) 個位置(最壞 \(n\) 步) → [B]

(5) array delete:同樣要重建、搬動 → [B]

(6) linked list delete:先走到第 \(i\) 個位置 → [B]

(7) singly linked list 依 key 找,最壞整串走完 → [B]

(8) doubly linked list 也只能一個一個比 → [B]

(9) \(Remove(L,p)\),singly:要改 \(p\) 前一個節點的 next,但沒有 prev 指標,得從 root 走到 \(p\) 前面找 predecessor → [B]

(10) doubly:用 \(p\)'s prev、next 直接接起來,tail/root 另外更新 → [A]

(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)
ABBBBBBBBA

(1) A (2) B (3) B (4) B (5) B (6) B (7) B (8) B (9) B (10) A

220%

2. (20 points) Please answer the following short questions about trees. (Note: You do NOT need to prove the results.)

(1) (2 points) What is the maximum number of nodes on level \(i\) of a binary tree?

(2) (2 points) What is the maximum number of nodes in a binary tree of depth \(k\)?

(3) (2 points) For any non-empty binary tree \(T\), Let \(n_0\) be the number of leaf nodes and \(n_2\) the number of nodes of degree 2. What the relationship between \(n_0\) and \(n_2\)?

(4) (2 points) What is the depth of a complete binary tree with \(n\) nodes?

(5) (2 points) How many null links do we have in an \(n\)-node binary tree of linked representation?

(6) (4 points) Draw the binary tree that corresponds to the left child-right sibling representation of the following tree.

ebfhijgcda

(7) (2 points) In order to access the tree nodes quickly, the binary tree in the previous question is stored level-by-level in a one-dimensional array \(A[1..n]\). What are the indices of nodes g and j within the array?

(8) (2 points) Draw the binary tree if the preorder sequence is \([1,2,3,4,5,6,7]\) and the inorder sequence is \([2,4,3,1,6,5,7]\).

(9) (2 points) Draw the binary tree if the postorder sequence is \([7,4,2,5,6,3,1]\) and the inorder sequence is \([7,4,2,1,5,3,6]\).

level 從 1 開始、depth 是最大的 level(課本 Horowitz 的定義)

(1) level \(i\) 最多 \(2^{i-1}\) 個

(2) \(1+2+\cdots+2^{k-1}=2^k-1\)

(3) 邊數 \(=n-1=n_1+2n_2\)(每個 degree 1 的點 1 條、degree 2 的點 2 條往下),\(n=n_0+n_1+n_2\) → \(n_0+n_1+n_2-1=n_1+2n_2\) → \(n_0=n_2+1\)

(4) complete binary tree:\(2^{d-1}\le n\le2^d-1\) → \(d=\lceil\log_2(n+1)\rceil=\lfloor\log_2n\rfloor+1\)

(5) \(n\) 個 node 共 \(2n\) 個 link 欄位,非空的是 \(n-1\) 條 edge → \(2n-(n-1)=n+1\)

(1) \(2^{i-1}\) (2) \(2^k-1\) (3) \(n_0=n_2+1\) (4) \(\lfloor\log_2n\rfloor+1\) (5) \(n+1\)

(6) left child = 第一個小孩,right child = 下一個兄弟:

\(a\):left \(b\)  \(b\):left \(e\)、right \(c\)(\(b\) 的兄弟)  \(c\):left \(f\)、right \(d\)  \(f\):right \(g\)  \(g\):left \(h\)  \(h\):right \(i\)  \(i\):right \(j\)

ebfhijgcda

(7) 這棵 binary tree 依 level 存進 \(A[1..n]\):node 在 \(i\),left child 在 \(2i\)、right child 在 \(2i+1\)

\(a=1,\ b=2,\ e=4,\ c=5,\ f=10,\ d=11,\ g=21\ (=2\cdot10+1),\ h=42,\ i=85,\ j=171\ (=2\cdot85+1)\)

\(g=21\),\(j=171\)

(8) preorder 第一個是 root=1;在 inorder 裡切開:左 \([2,4,3]\)(preorder \([2,3,4]\)),右 \([6,5,7]\)(preorder \([5,6,7]\))

左:root 2,inorder \([2,4,3]\) 中 2 在最前 → 沒有左子;右子樹 \([4,3]\),preorder \([3,4]\):root 3,inorder 中 4 在 3 前 → 4 是 3 的左子

右:root 5,inorder \([6,5,7]\) → 左 6、右 7

2431657

(9) postorder 最後一個是 root=1;inorder 切成左 \([7,4,2]\)、右 \([5,3,6]\)

左:postorder \([7,4,2]\),root 2,inorder \([7,4,2]\) → 左子樹 \([7,4]\):root 4,左子 7

右:postorder \([5,6,3]\),root 3,inorder \([5,3,6]\) → 左 5、右 6

7421536

36%

3. (6 points) Please use a stack to illustrate how to evaluate the postfix expression "62/3-42*+" step by step. Please draw the stack status after each step.

由左往右讀:operand → push;operator → pop 兩個(先 pop 的是右運算元)、運算、結果 push

讀 6讀 2讀 /讀 3讀 −讀 4讀 2讀 *讀 +
      2  
 2 3 448 
663300008
push 6push 2pop 2、6 → 6/2=3push 3pop 3、3 → 3−3=0push 4push 2pop 2、4 → 4×2=8pop 8、0 → 0+8=8

對應的 infix:\((6/2-3)+(4\times2)\)

\(8\)

48%

4. (8 points) Please answer the following short questions about quick sort. (Note: Do NOT use the table in Question 1)

(1) (3 points) What is the average time complexity of quick sort on \(n\) numbers?

(2) (3 points) What is the worst-case time complexity of quick sort on \(n\) numbers?

(3) (2 points) For using quick sort towards a non-decreasing sequence, if quick sort is implemented by always choosing the first component as the midpoint, for all permutations of \(\{1,\ldots,6\}\), what is the permutation that exhibits the worst case behavior of quick sort?

(1) 平均每層 partition 共 \(O(n)\),期望 \(O(\log n)\) 層

\(O(n\log n)\)

(2) 每次 pivot 都是最小或最大 → \(T(n)=T(n-1)+O(n)\)

\(O(n^2)\)

(3) 第一個當 pivot:已經排好的 \(1,2,3,4,5,6\),pivot 每次都是目前最小值,左邊永遠是空的,右邊剩 \(n-1\) 個 → 比較 \(5+4+\cdots\) 平方量級

用 Horowitz 的 partition(i 找 \(\ge\) pivot、j 找 \(\le\) pivot)時,比較次數最多(25 次)的只有 \(1,2,3,4,5,6\);若用 Lomuto 類的 partition,\(6,5,4,3,2,1\)(pivot 每次是最大值)等 32 種也都是最壞

\(1,2,3,4,5,6\)(已排序)

518%

5. (18 points) (Note: You do NOT need to prove the results.) Prim's algorithm can be used for producing a minimum spanning tree. The essence of the algorithm involves greedily searching for a vertex that is connected by the lightest edge to but is not within the current tree. When running Prim's algorithm on a graph with \(V\) vertices and \(E=O(V^{1.5})\) edges stored with the adjacency list representation, what is the worst case asymptotic time complexity of the algorithm with respect to \(V\) if

(1) (6 points) no other data structures are used

(2) (6 points) a loser tree is used to store the vertices, where the leaves hold the smallest direct edge length to the current tree (or \(\infty\) if a direct edge does not exist or the vertex is within the current tree)

(3) (6 points) a Fibonacci heap is used to store the vertices

(1) 每輪線性掃所有 vertex 找最小 key:\(V\) 輪 \(\times O(V)\);掃 adjacency list 更新 key 共 \(O(E)\)

\(O(V^2+E)=O(V^2+V^{1.5})\)

\(O(V^2)\)

(2) loser tree:最小值在樹頂(取最小 \(O(1)\));改一個 leaf 的值要沿路徑重賽,\(O(\log V)\)

加入一個 vertex(把它的 leaf 設成 \(\infty\)):\(V\) 次 \(\times O(\log V)\);每條 edge 最多讓一個 leaf 的值變小一次:\(E\) 次 \(\times O(\log V)\)

\(O\big((V+E)\log V\big)=O(V^{1.5}\log V)\)

\(O(V^{1.5}\log V)\)

(3) Fibonacci heap:decrease-key amortized \(O(1)\)(\(E\) 次)、extract-min amortized \(O(\log V)\)(\(V\) 次)

\(O(E+V\log V)=O(V^{1.5}+V\log V)\)

\(O(V^{1.5})\)

618%

6. (18 points) (Note: You do NOT need to prove the results.) The recursive closest-point finding algorithm is a classic divide and conquer algorithm for finding closest points on a Euclidean plane. The algorithm recursively separates the set of points to two equal-sized subsets according to their \(x\)-axis, and find the closest points within the subsets as well as between the subsets. For a set of \(N\) points, what is the asymptotic time complexity of the algorithm with respect to \(N\) if

(1) (6 points) finding the closest points between two size-\(\tfrac N2\) subsets take \(O(N)\) time

(2) (6 points) finding the closest points between two size-\(\tfrac N2\) subsets take \(O(N\log N)\) time

(3) (6 points) finding the closest points between two size-\(\tfrac N2\) subsets take \(O(N^2)\) time

\(T(N)=2T(N/2)+f(N)\),\(N^{\log_22}=N\)

(1) \(f=\Theta(N)\):Master case 2 → 每層 \(N\)、共 \(\log N\) 層

\(O(N\log N)\)

(2) \(f=N\log N\):Master 的 case 2 擴充(\(f=\Theta(N\log^kN)\),\(k=1\))→ \(\Theta(N\log^{k+1}N)\);或看遞迴樹:第 \(i\) 層 \(2^i\) 個子問題,大小 \(N/2^i\),每個 \(\tfrac N{2^i}\log\tfrac N{2^i}\),每層和 \(N(\log N-i)\),加總 \(N\sum_{i}(\log N-i)=\Theta(N\log^2N)\)

\(O(N\log^2N)\)

(3) \(f=N^2=\Omega(N^{1+\varepsilon})\),且 \(2f(N/2)=N^2/2\le\tfrac12f(N)\)(regularity):Master case 3

\(O(N^2)\)

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

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

購買 · NT$ 850 看台大其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com