免費詳解 / 師大 / 軟體 / 102

102 師大資工所考古題解答|軟體逐題詳解

102 師大資工所軟體考古題的逐題詳解,共 11 題,免費公開。

逐題詳解

16 分

Simple answer questions.

(a) Given the array of integers arr shown below (3 分)

4 | 8 | 10 | 36 | 48 | 60 | 20 | 30 | 21 | 19

What is the output of the following statements?

int[] a2 = arr;
a2[3] = arr[2];
arr[2] = arr[3];
arr[3] = a2[3];
System.out.println(arr[2] + " " + arr[3]);

(b) If a binary search tree is not allowed to have duplicates, there is more than one way to delete a node in the tree when that node has two children. One way involves choosing a replacement node from the left subtree. Please explain how to find the replacement node from the left subtree. (3 分)

(a) a2 = arr 是同一個陣列的別名,改 a2 就是改 arr

a2[3] = arr[2] → arr[3] = 10

arr[2] = arr[3] → arr[2] = 10

arr[3] = a2[3] → arr[3] = 10

10 10

(b) replacement = 左子樹中最大的節點(inorder predecessor)

① 走到被刪節點的左子節點 ② 一直往右子節點走,直到沒有右子節點 ③ 停下的節點就是 replacement

把它的 key 複製到被刪節點,再刪掉這個 replacement 節點(它沒有右子,最多一個左子,用左子接上它的位置)

這個值比左子樹其他節點都大、又比右子樹所有節點小,換上去後 BST 性質不變

29 分

Multiple choices questions.

(a) An array is used to store a heap structure. After integers are inserted into the heap structure, which of the following is a possible ordering in the array? (3 分)

A) 90 49 87 42 47 51 53 B) 90 87 49 42 47 51 53 C) 90 51 87 53 49 47 42 D) 90 87 53 51 49 47 42 E) 42 90 87 53 51 49 47

(b) Which of the following sorting algorithms does not require \(O(n^2)\) steps in the worst case? (3 分)

A) insertion sort B) selection sort C) heap sort D) bubble sort E) quicksort

(c) A binary tree is constructed of nodes that are instances of the following class:

public class Node { public int val; public Node left; public Node right; }

Consider the following method:

public static Node mystery(Node root) {
  if (root.right == null) return root;
  else return mystery(root.right);
}

troot is a pointer of Node, which points to the root node of a binary search tree T. Which of the following statements is correct by calling the method mystery(troot) regardless of the contents of the tree T? (3 分)

A) It returns the last node of the tree T visited by an inorder traversal B) … by a postorder traversal C) … by a level-order traversal D) It returns the smallest node of the tree T E) It returns the largest node the tree T

(a) max-heap:每個節點 ≥ 子節點(位置 \(i\) 的子節點在 \(2i,2i+1\))

A:90 ≥ 49, 87;49 ≥ 42, 47;87 ≥ 51, 53 ✓

B:49 的子節點是 51、53,比 49 大 ✗

C:51 的子節點是 53、49,53 比 51 大 ✗

D:90 ≥ 87, 53;87 ≥ 51, 49;53 ≥ 47, 42 ✓

E:根 42 比子節點小 ✗

滿足 heap 性質的陣列,照陣列順序一個一個插入,每個元素都不用上浮,所以 A、D 都做得到。若題目指的是 min-heap,只有 E 成立

A、D

(b) insertion、selection、bubble 最壞 \(O(n^2)\);quicksort 最壞(每次 pivot 取到最大或最小)也是 \(O(n^2)\);heap sort 最壞 \(O(n\log n)\)

C

(c) mystery 一路往右走到沒有右子節點的節點,也就是整棵樹最右邊的節點

inorder(左、根、右)最後拜訪的是最右節點 → A ✓

postorder 最後拜訪的是 root;level-order 最後拜訪的是最深一層最右邊的節點,不一定是最右路徑上的節點 → B、C ✗

T 是 BST:最右節點 key 最大 → E ✓,D ✗

A、E

38 分

Whether the following statement is true or false. Please justify your answers. NO CREDIT will be given to any answer that only states TRUE or FALSE without proper justification.

(a) Let \(G=(V,E)\) be a weighted and undirected graph and let \(M\) be a minimum spanning tree of \(G\). The path in \(M\) between any pair of vertices \(v_1\) and \(v_2\) must be a shortest path in \(G\). (4 分)

(b) Given an undirected graph \(G=(V,E)\) with distinct costs on edges and a set \(S=V\), let \((u,v)\) be an edge such that \((u,v)\) is the minimum cost edge between any vertex in \(S\) and any vertex in \(V-S\). Then, the minimum spanning tree of \(G\) must include the edge \((u,v)\). (4 分)

(a) F

反例:三角形 \(x,y,z\),\(w(xy)=3,\ w(yz)=3,\ w(xz)=4\)

MST = \(\{xy,\ yz\}\)(總和 6,捨掉最重的 \(xz\))

MST 中 \(x\) 到 \(z\) 的路徑是 \(x-y-z\),長度 6;但 \(G\) 中直接走邊 \(xz\) 長度 4 較短 → 不是 shortest path

F

(b) T(cut property;題目的 \(S=V\) 應為 \(S\subset V\),否則 \(V-S\) 是空集合)

邊權兩兩不同,所以 MST 唯一。設 MST \(T\) 不含 \(e=(u,v)\)

\(T+e\) 形成一個 cycle,這個 cycle 一定還會再穿過 \((S,\ V-S)\) 這個 cut 一次,經過另一條邊 \(e'\)

\(e\) 是這個 cut 上最輕的邊且權重不同 → \(w(e)\lt w(e')\)

\(T+e-e'\) 仍是 spanning tree、權重更小,與 \(T\) 是 MST 矛盾 → \(e\in T\)

T

45 分

Please show the process of constructing AVL tree step by step that results from the successive addition of the given elements: 35, 87, 64, 78, 81, 85 to an empty tree.

35插 353587插 87356487插 64:35 失衡(右的左,RL)356487對 87 右旋、對 35 左旋35647887插 783564788187插 81:87 失衡(左的右,LR)3564788187對 78 左旋、對 87 右旋356478818587插 85:根 64 失衡(右的右,RR)356478818587對 64 左旋,完成

每步先照 BST 插入,再從插入點往上找第一個 balance factor 超出 ±1 的節點,照形狀做旋轉

最後:81 為根,左子 64(子 35、78),右子 87(左子 85)

55 分

Consider the set of initially unrelated elements: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.

(a) Please draw the forest of trees that results from the following sequence of union operations using on weighting rule for union(i,j). Break ties by keeping the first argument as the root. (3 分)

Union(0, 1), Union(3, 4), Union(7, 5), Union(9, 3), Union(6, 8), Union(6, 0), Union(12, 6), Union(1, 11), Union(9, 6), Union(2, 5).

(b) Please draw the new forest of trees that results from doing a Find(11) with collapsing rule on the result of (a). (2 分)

weighting rule:節點少的樹接到節點多的樹下;一樣多時第一個參數當 root;參數是元素就先找到它所在樹的 root

操作兩棵樹(root:size)結果
Union(0,1)0:1、1:1 平手0 為 root,子 1
Union(3,4)3:1、4:1 平手3 為 root,子 4
Union(7,5)7:1、5:1 平手7 為 root,子 5
Union(9,3)9:1、3:29 接到 3 下(3 的 size 3)
Union(6,8)6:1、8:1 平手6 為 root,子 8
Union(6,0)6:2、0:2 平手0 接到 6 下(6 的 size 4)
Union(12,6)12:1、6:412 接到 6 下(size 5)
Union(1,11)1 的 root 是 6(size 5)、11:111 接到 6 下(size 6)
Union(9,6)9 的 root 是 3(size 3)、6:63 接到 6 下(size 9)
Union(2,5)2:1、5 的 root 是 7(size 2)2 接到 7 下(size 3)

(a) 三棵樹:6(size 9)、7(size 3)、10(size 1)

8101211493652710

(b) Find(11):11 的 parent 就是 root 6,沒有中間節點可以收攏,森林跟 (a) 一樣

collapsing rule 只會改變深度 ≥ 2 的節點:Find(1)、Find(4)、Find(9) 會把該節點(與路徑上的節點)直接掛到 6 下面

65 分

A hash table consisting of 12 buckets is given, where each bucket can store 1 value. The hash function used is \(H(k)=k\bmod12\). Open addressing and quadratic probing is used to resolve collisions. Please show the content of the hash table after inserting the following sequence of values: 21, 22, 9, 25, 36, 57.

probe 序列:\((H(k)+i^2)\bmod12\),\(i=0,1,2,\dots\)

21:\(21\bmod12=9\) → 放 9

22:\(22\bmod12=10\) → 放 10

9:\(H=9\) 有人;\(i=1\):\(10\) 有人;\(i=2\):\(9+4=13\bmod12=1\) → 放 1

25:\(H=1\) 有人;\(i=1\):\(2\) → 放 2

36:\(H=0\) → 放 0

57:\(H=9\) 有人;\(i=1\):10 有人;\(i=2\):1 有人;\(i=3\):\(9+9=18\bmod12=6\) → 放 6

bucket01234567891011
value36925572122

若 probe 改用 \((H+i+i^2)/2\) 這類寫法,位置會不同;這裡照標準 \(h+i^2\)

75 分

Given an infix arithmetic expression: ((A+B-C)/D)/E-(F+G)*(H/I). During the process of converting the infix expression into a postfix expression by using a stack, what is the snapshot right after the token of H is processed. Please show the content of the stack and the partial output expression at this moment.

規則:運算元直接輸出;運算子先把堆疊頂端優先序 ≥ 自己的 pop 出來再 push;( 直接 push,) pop 到 ( 為止

tokenstack(底 → 頂)output
( (( (
A( (A
+( ( +A
B( ( +A B
−( ( −(先 pop +)A B +
C( ( −A B + C
)(A B + C −
/( /A B + C −
D( /A B + C − D
)(空)A B + C − D /
//A B + C − D /
E/A B + C − D / E
−−(先 pop /)A B + C − D / E /
(− (
F− (… F
+− ( +… F
G− ( +… F G
)−A B + C − D / E / F G +
*− *(* 優先序高於 −,不 pop)A B + C − D / E / F G +
(− * (
H− * (A B + C − D / E / F G + H

stack(底→頂):− * ( output:A B + C − D / E / F G + H

87 分

Given the following linked list, where a and b point to the front and end of the list, respectively. Please answer the following questions:

a → [A] → [B] → [C] → [D | NULL] ← b

(a) Please describe how to insert a new data into front of the list? (2 分)

(b) Please describe how to delete a data from end of the list? (2 分)

(c) Suppose we want to implement a stack by the linked list. Which side (a or b) is more suitable to be the top of the stack? NO CREDIT will be given to any answer that only states a or b without proper justification. (3 分)

(a) ① 建立新節點 \(x\),放入資料 ② \(x.next=a\) ③ \(a=x\)

與串列長度無關,\(O(1)\)

(b) singly linked list,每個節點只有 next,沒辦法從 D 找回 C

① 從 \(a\) 走到 \(b\) 的前一個節點 C(\(C.next=b\)) ② \(C.next=\text{NULL}\) ③ \(b=C\),釋放 D

要掃過整串,\(O(n)\);只剩一個節點時,刪完 \(a=b=\text{NULL}\)

(c) 選 a 當 top

push = 在 a 端插入 \(O(1)\);pop = 刪掉 a 端節點,\(a=a.next\),也是 \(O(1)\)

若 b 當 top:push 在尾端加節點 \(O(1)\),但 pop 要刪尾端節點,需從頭走到倒數第二個,\(O(n)\)

a(push、pop 都是 \(O(1)\))

916 分

Heapsort is an in-place algorithm for sorting a list of \(n\) numbers in nondecreasing order using heaps.

(a) What is in-place algorithm? (4 分)

(b) The following tree is not a heap. Why? (4 分)

6984725315

(c) Please illustrate how Heapsort arrange the nodes in (b) to make it a heap. (4 分)

(d) What is the time complexity of Heapsort? (4 分)

(a) in-place:只用 \(O(1)\) 額外記憶體,直接在輸入陣列上就地改排(交換),不另外配置 \(O(n)\) 的輔助陣列

heapsort 把 heap 存在原陣列裡,每次把最大值換到陣列尾端

(b) 形狀:逐層由左到右填滿,是 complete binary tree ✓

大小順序(max-heap:節點 ≥ 子節點):\(9\ge4,7\) ✓ \(8\ge2,5\) ✓ \(4\ge3,1\) ✓ \(7\ge5\) ✓

根 6 的子節點 9、8 都比 6 大 ✗(圖中紅圈)

根 6 比子節點 9、8 小,不滿足 max-heap 性質

(c) 陣列 \([6,9,8,4,7,2,5,3,1,5]\),\(n=10\)。bottom-up heapify:從最後一個有子節點的位置 \(\lfloor n/2\rfloor=5\) 往前,每個節點下沉到定位

\(i=5\)(7):子節點 5 ✓ \(i=4\)(4):子節點 3、1 ✓ \(i=3\)(8):子節點 2、5 ✓ \(i=2\)(9):子節點 4、7 ✓

\(i=1\)(6):子節點 9、8,與較大的 9 交換;6 到位置 2,子節點 4、7,與 7 交換;6 到位置 5,子節點 5 ✓

6984725315原樹(i=5、4、3、2 都已滿足)9684725315i=1:6 與較大子節點 9 交換97846253156 繼續下沉,與 7 交換:完成

max-heap:\([9,7,8,4,6,2,5,3,1,5]\)

(d) 建 heap \(O(n)\);之後 \(n-1\) 次「根與最後一個交換、heap 大小減 1、根下沉」,每次 \(O(\log n)\)

\(O(n\log n)\)(best、average、worst 都是)

1016 分

The following figure shows a weighted, directed graph.

v₁v₂v₅v₄v₃1935123243

(a) A problem that has many applications is finding the shortest paths from each vertex to all other vertices. What is the shortest path from \(v_2\) to \(v_1\) in the above graph? (4 分)

(b) Floyd's algorithm for shortest paths is a dynamic programming algorithm. It defines \(D^{(k)}[i][j]\) as the length of a shortest path from \(v_i\) to \(v_j\) that is allowed to pass through only vertices in the set \(\{v_1,v_2,\dots,v_k\}\) as intermediate vertices. According to this definition, what is the value of \(D^{(1)}[2][5]\) in the above graph? (4 分)

(c) Using Floyd's Algorithm, we can compute \(D^{(k)}\) from \(D^{(k-1)}\) as follows: \(D^{(k)}[i][j]=\text{minimum}(D^{(k-1)}[i][j],\ \underline{\qquad\qquad})\). Please fill the above blank. (4 分)

(d) What is the time complexity of Floyd's Algorithm? (4 分)

邊:\(v_1\to v_2\):1、\(v_2\to v_1\):9、\(v_1\to v_4\):1、\(v_1\to v_5\):5、\(v_5\to v_1\):3、\(v_2\to v_3\):3、\(v_2\to v_4\):2、\(v_4\to v_3\):2、\(v_3\to v_4\):4、\(v_4\to v_5\):3

(a) \(v_2\) 直接到 \(v_1\):9

\(v_2\to v_4\to v_5\to v_1\):\(2+3+3=8\)

\(v_2\to v_3\to v_4\to v_5\to v_1\):\(3+4+3+3=13\)

v₁v₂v₅v₄v₃1935123243

\([v_2,v_4,v_5,v_1]\),長度 8

(b) \(D^{(1)}\) 只能經過 \(v_1\):\(v_2\to v_5\) 沒有直接邊(∞)

經 \(v_1\):\(D^{(0)}[2][1]+D^{(0)}[1][5]=9+5=14\)

\(D^{(1)}[2][5]=14\)

(c) 第 \(k\) 步看「經過 \(v_k\)」會不會更短:\(i\to k\) 再 \(k\to j\)

\(D^{(k-1)}[i][k]+D^{(k-1)}[k][j]\)

(d) \(k,i,j\) 三層迴圈,每層 \(n\) 次,每次 \(O(1)\)

\(O(n^3)\)

1118 分

Huffman algorithm is a greedy algorithm for finding a Huffman encoding for a given file.

(a) Suppose the character set is {a, b, c, d, e, f} and each character appears in the file the number of times indicated in the following table: a 16、b 5、c 12、d 17、e 10、f 25. The following figure shows the set of trees, constructed by Huffman algorithm, in the first and the second passes: 第一回合為 b:5、e:10、c:12、a:16、d:17、f:25 六棵單點樹;第二回合 b:5 與 e:10 合併成 15(左邊 0 右邊 1),其餘 c:12、a:16、d:17、f:25 不動。Please illustrate the third and the fourth passes for this example. (4 分)

(b) Please show the Huffman code created by this algorithm for the above example. (4 分)

(c) The problem of data compression is to find an efficient method for encoding a data file using storage as less as possible. Does Huffman's algorithm always produce an optimal binary code? (4 分)

(d) Huffman's algorithm always produces a prefix code. What is prefix code? (3 分)

(e) Huffman algorithm is classified as a greedy algorithm. What is greedy algorithm? (3 分)

(a) 每回合取權重最小的兩棵合併(較小的放左、邊標 0,較大的放右、邊標 1)

第 3 回合:最小的兩棵是 c:12 與 15 → 合成 27;第 4 回合:剩 a:16、d:17、f:25、27,最小的是 a:16 與 d:17 → 合成 33

0101c:1227b:515e:10a:16d:17f:25
第 3 回合:c:12 與 15 合併
0101c:1227b:515e:1001a:1633d:17f:25
第 4 回合:a:16 與 d:17 合併
010101f:2552c:1227b:515e:1001a:1633d:17
第 5 回合:f:25 與 27 合併
0011010101a:1633d:1785f:2552c:1227b:515e:10
第 6 回合:33 與 52 合併,完成

第 5、6 回合是為了得到編碼:f:25 與 27 合成 52,再 33 與 52 合成根 85

(b) 由根往下讀邊上的 0/1:

字元adfcbe
碼00011011011101111

總 bit 數 \(16\cdot2+17\cdot2+25\cdot2+12\cdot3+5\cdot4+10\cdot4=32+34+50+36+20+40=212\),也等於所有內部節點權重和 \(15+27+33+52+85=212\)

(c) 是。Huffman code 是所有 prefix code 中總長度(\(\sum f_i\cdot|c_i|\))最小的

理由:greedy choice——最小的兩個符號可以放在最深一層成為兄弟(交換論證不會使總長度變大);optimal substructure——把這兩個合併成一個新符號後,剩下的問題最佳解再展開即得原問題最佳解

最佳碼不一定唯一(頻率相同時分支可換)

是

(d) prefix code:任何一個 codeword 都不是另一個 codeword 的前綴(例如有 01 就不能再有 011)

碼字都放在樹的葉節點,所以解碼時由左到右讀 bit,走到葉子就能確定一個字元,不會有歧義

(e) greedy algorithm:分步驟建解,每一步只選當下看起來最好的選擇(local optimum),選了就不回頭。要正確需要 greedy-choice property 與 optimal substructure

Huffman 每次合併目前權重最小的兩棵樹;MST 的 Kruskal、Prim 也是 greedy

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com