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

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

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

逐題詳解

115 分

True or false? Briefly describe the reasons. (a) \(f(n)=O(g(n))\) implies \(g(n)=O(f(n))\). (3 分) (b) \(2^{2n}=O(2^{n})\). (3 分) (c) \(2^{n+1}=O(2^{n})\). (3 分) (d) The average-case efficiency is the average of the best-case and the worst-case complexities. (3 分) (e) The worst-case running time of QUICKSORT is \(O(n^2)\). (3 分)

(a) F。\(O\) 只是上界,沒有對稱性。反例:\(f(n)=n\),\(g(n)=n^{2}\):\(n=O(n^2)\),但 \(n^{2}\ne O(n)\)(\(n^2/n=n\) 沒有上界)

(b) F。\(2^{2n}/2^{n}=2^{n}\to\infty\),找不到常數 \(c\) 使 \(4^{n}\le c\cdot2^{n}\) 對所有大的 \(n\) 成立

(c) T。\(2^{n+1}=2\cdot2^{n}\),取 \(c=2\)

(d) F。average case 是把「所有輸入(依機率分布)」的執行時間取期望值,不是 best 與 worst 兩個數字的平均。例:quicksort best \(O(n\log n)\)、worst \(O(n^2)\),average 是 \(O(n\log n)\),不是 \((n\log n+n^2)/2\)

(e) T。每次 pivot 選到最大或最小(例如輸入已排序且 pivot 取第一個)時 \(T(n)=T(n-1)+O(n)=O(n^2)\),這也是最壞情況的上界

(a)(b)(c)(d)(e)
FFTFT
215 分

Given a weighted, directed graph \(G\) as shown below, please describe the output of running the following graph algorithms on \(G\). If multiple solutions exist, please provide one of them.

sabcdt16131241492074

(a) Depth-first-search. Start with \(s\). (5 分) (b) Dijkstra's algorithm with the source \(s\). (5 分) (c) Ford-Fulkerson method with the source \(s\) and the sink \(t\). (5 分)

邊(容量/權重):\(s\to a\ 16\),\(s\to c\ 13\),\(a\to b\ 12\),\(c\to a\ 4\),\(c\to d\ 14\),\(b\to c\ 9\),\(b\to t\ 20\),\(d\to b\ 7\),\(d\to t\ 4\)

(a) 鄰居依字母序走:\(s\to a\to b\to c\to d\to t\)

點sabcdt
discovery / finish1/122/113/104/95/86/7

走法:\(s\) 先到 \(a\);\(a\to b\);\(b\) 的鄰居 \(c\)、\(t\) 先走 \(c\);\(c\to a\) 已拜訪,走 \(d\);\(d\to b\) 已拜訪,走 \(t\);\(t\) 沒有出邊,往回退;最後 \(s\to c\) 已拜訪

拜訪順序:\(s,a,b,c,d,t\);DFS tree 邊:\(s\to a,\ a\to b,\ b\to c,\ c\to d,\ d\to t\)(一條路徑)

(b) Dijkstra,每次取目前距離最小且未確定的點:

取出dist(a)dist(b)dist(c)dist(d)dist(t)
初始∞∞∞∞∞
s(0)16∞13∞∞
c(13)16(\(13+4=17\) 不更新)∞1327∞
a(16)16281327∞
d(27)1628(\(27+7=34\) 不更新)132731
b(28)1628132731(\(28+20=48\) 不更新)
t(31)1628132731

最短距離:\(a=16,\ b=28,\ c=13,\ d=27,\ t=31\);shortest-path tree:\(s\to a,\ s\to c,\ a\to b,\ c\to d,\ d\to t\)

(c) Ford–Fulkerson:在殘餘網路裡找 \(s\to t\) 的增廣路徑,沿路徑加上瓶頸流量,重複到找不到為止

sabcdt16131241492074路徑 1:s→a→b→t,瓶頸 12(圖上數字:殘餘容量)sabcdt41304149874路徑 2:s→c→d→t,瓶頸 4(圖上數字:殘餘容量)sabcdt4904109870路徑 3:s→c→d→b→t,瓶頸 7(圖上數字:殘餘容量)

路徑 1:\(s\to a\to b\to t\),瓶頸 \(\min(16,12,20)=12\),流量 12

路徑 2:\(s\to c\to d\to t\),瓶頸 \(\min(13,14,4)=4\),流量 \(12+4=16\)

路徑 3:\(s\to c\to d\to b\to t\),瓶頸 \(\min(9,10,7,8)=7\),流量 \(16+7=23\)

再找:\(s\) 還能到 \(a\)(剩 4)、\(c\)(剩 2),\(a\) 的唯一出邊 \(a\to b\) 已滿;\(c\to d\) 還有 3,但 \(d\to t\)、\(d\to b\) 已滿;到不了 \(b\)、\(t\),結束

sabcdt12/1611/1312/120/411/140/919/207/74/4

圖上 f/c = 流量/容量,紅色是 min cut 的邊。min cut:\(S=\{s,a,c,d\}\),\(T=\{b,t\}\),容量 \(a\to b\,(12)+d\to b\,(7)+d\to t\,(4)=23\),等於流量。增廣路徑的選法不只一種,最大流 23 是唯一的

max flow = 23

320 分

In the longest-common-subsequence (LCS) problem, we are given two sequences \(X=\langle x_1,x_2,\dots,x_m\rangle\) and \(Y=\langle y_1,y_2,\dots,y_n\rangle\) and want to find a maximum-length common subsequence (not necessarily a continuous sequence of characters) of \(X\) and \(Y\).

(a) Write down the recursive formula that solves the LCS problem. (5 分) (b) Compute a LCS for the sequences \(X=\)GAC and \(Y=\)AGCAT. (5 分) (c) Edit distance. Now we are interested in the similarity of \(X\) and \(Y\). Given the following operations, the edit distance from \(X\) and \(Y\) is the minimal number of operations that transform \(X\) to \(Y\). Replace: set \(x_i\) to some other character; Insert: add a character into the sequence; Delete: remove a character into the sequence. For example, given the sequence \(X=\)GATCG and \(Y=\)CAATG, the edit distance is 3 because we will need 3 operations: replace the first G by C, insert A and delete C. GAATCG → CAATG. Please write down the recursive formula that finds the edit distance of two sequences. (5 分) (d) Please compute the edit distance for the sequences \(X=\)GAC and \(Y=\)AGCAT. (5 分)

(a) \(c[i,j]\) = \(X_i=\langle x_1..x_i\rangle\) 與 \(Y_j\) 的 LCS 長度

\(c[i,j]=\begin{cases}0&i=0\text{ 或 }j=0\\c[i-1,j-1]+1&i,j\gt0,\ x_i=y_j\\\max\bigl(c[i-1,j],\ c[i,j-1]\bigr)&i,j\gt0,\ x_i\ne y_j\end{cases}\)

(b) \(X=\)GAC(列),\(Y=\)AGCAT(行):

AGCAT
000000
G001111
A011122
C011222

\(c[3,5]=2\)。從 \(c[3,5]\) 往回追:\(x_3=\)C 與 \(y_5=\)T 不同,取較大的 \(c[3,4]=2\)(與 \(c[2,5]\) 同值,往上)\(\to\) 再 \(c[2,4]\):\(x_2=y_4=\)A,相同,取 A,退到 \(c[1,3]=1\) \(\to c[1,2]\):\(x_1=y_2=\)G,取 G,反過來讀得 G、A

長度 2 的 LCS 不只一個:GA、AC、GC 都是 X 與 Y 的共同子序列;沒有長度 3(GAC 不是 AGCAT 的子序列)

LCS 長度 2,例如 “GA”

(c) \(d[i,j]\) = 把 \(X_i\) 變成 \(Y_j\) 的最少操作數

\(d[i,j]=\begin{cases}j&i=0\\i&j=0\\\min\begin{cases}d[i-1,j]+1&\text{delete }x_i\\d[i,j-1]+1&\text{insert }y_j\\d[i-1,j-1]+[x_i\ne y_j]&\text{match(0)或 replace(1)}\end{cases}&i,j\gt0\end{cases}\)

(d) 

AGCAT
012345
G111234
A212223
C322233

\(d[3,5]=3\),例:GAC → AGAC(insert A)→ AGCAC(insert C)→ AGCAT(replace C 為 T)

edit distance = 3

410 分

Give the following table which shows an original list of integers from 1 to 24 to be sorted. The last column shows the integers in sorted order. The other columns show the contents during one of the 7 sorting algorithms listed below.

Original(1)(2)(3)(4)(5)(6)(7)Sorted
14454222411
189139442322
41442752233
918914971844
222171192155
17731812111766
19178714121977
7197191714988
2311101218171499
20126111918201010
122012202019161111
112311222320151212
2211112242213
5514552351814
1222266111415
24242410102472416
6620822661717
10823212410102018
8101933881919
2121211382122120
33171613332321
131318151513131322
161516231616121623
151615242115111524

(a) Insertion sort (b) Selection sort (c) Iterative Merge sort (d) Recursive Merge sort (e) Bubble sort (f) Heap sort (g) Quick sort. Please match each algorithm with the number on the corresponding column.

欄演算法怎麼看出來
(1)(c) Iterative Merge sort每連續 4 個一組都已排好:[4 9 14 18]、[2 7 17 19]、[11 12 20 23]、[1 5 22 24]、[6 8 10 21]、[3 13 15 16],各組內容就是原始輸入對應 4 個一組排序的結果。bottom-up 由 1→2→4 合併,剛做完區塊大小 4 這一輪
(2)(g) Quick sort第 14 個位置是 14(原輸入第一個數,pivot),它左邊 13 個都比 14 小、右邊 10 個都比 14 大,兩邊內部沒有排好 → 以第一個元素為 pivot 的第一次 partition
(3)(e) Bubble sort最後兩個已是 23、24(最大的兩個已推到尾端);前面 22 個沒有整段排好的區塊,也不是 heap、不是 partition 結果。其餘六個演算法已分別對到其他欄,剩下的是 bubble sort(約做完兩趟)
(4)(d) Recursive Merge sort前 12 個 [2 4 7 9 … 23] 已排好;後 12 個是兩段各 6 個的已排好片段 [1 5 6 10 22 24]、[3 8 13 15 16 21],還沒合併。top-down 先分成 12/12、再分 6/6,左半 12 合併完、右半兩個 6 個的片段排好但尚未合併
(5)(a) Insertion sort前 14 個 [2 4 5 7 9 … 22 23] 已排好(就是原輸入前 14 個的排序),後 10 個 1, 24, 6, 10, 8, 21, 3, 13, 16, 15 與原輸入第 15 到 24 個完全一樣,還沒碰過
(6)(f) Heap sort整列是 max-heap(24 為根,每個節點 ≥ 子節點:24 的子節點 23、22;23 的子節點 18、21;…),是 build-heap 做完、還沒開始取出最大值的狀態
(7)(b) Selection sort前 12 個就是 1 到 12,已在最終位置;後 12 個是原輸入被一連串「最小值與目前位置對調」打亂的剩餘元素

(3) 欄與標準 bubble sort(每趟由左往右、相鄰比較交換)做完兩趟的結果不完全一樣:兩趟後應為 …17, 7, 18, 19… 與 …20, 5, 1, 22, 6…,表上是 …17, 18, 7, 19… 與 …20, 22, 1, 5, 6…,比較像第一趟與第二趟混在一起,可能是原表的筆誤。其他六欄都能逐步重現,(3) 欄最大的兩個已推到尾端、又不是其他六種的狀態,所以仍答 bubble sort

(1)=(c) (2)=(g) (3)=(e) (4)=(d) (5)=(a) (6)=(f) (7)=(b)

515 分

Give the adjacent matrix of an undirected graph G as follows: where 0 indicates that there is no corresponding edge, and the non-zero values indicate the costs of the corresponding edges.

\(\begin{matrix}&A&B&C&D&E&F&G&H\\A&0&3&4&0&5&0&2&0\\B&3&0&1&2&0&2&0&3\\C&4&1&0&0&5&3&0&0\\D&0&2&0&0&0&0&0&5\\E&5&0&5&0&0&0&4&0\\F&0&2&3&0&0&0&2&6\\G&2&0&0&0&4&2&0&0\\H&0&3&0&5&0&6&0&0\end{matrix}\)

(a) Please perform depth-first search (DFS), starting at vertex A. Assume the visiting order of the adjacent nodes are in lexicographic order, e.g., when exploring the adjacent nodes of vertex A, consider A-B before A-C or A-E. Please write the list of vertices in the order that they are visited by DFS and BFS, respectively. (2 分) (b) Please perform breadth-first search (BFS), starting at vertex A. (2 分) (c) Give one situation that you would need to perform BFS instead of DFS. (2 分) (d) Give one situation that you would need to perform DFS instead of BFS. (2 分) (e) Please explain Kruskal's algorithm and find the minimum-cost spanning tree of the graph G. (4 分) (f) How many articulation points are in the graph G? Please explain your answer. (3 分)

由矩陣讀出邊(無向):\(AB\,3,\ AC\,4,\ AE\,5,\ AG\,2,\ BC\,1,\ BD\,2,\ BF\,2,\ BH\,3,\ CE\,5,\ CF\,3,\ DH\,5,\ EG\,4,\ FG\,2,\ FH\,6\)(14 條)

鄰居(字母序):A:B C E G;B:A C D F H;C:A B E F;D:B H;E:A C G;F:B C G H;G:A E F;H:B D F

(a) DFS:A → B(A 的第一個)→ C(B 的鄰居 A 已訪、C 未訪)→ E(C:A、B 已訪,E 未訪)→ G(E:A、C 已訪)→ F(G:A、E 已訪)→ H(F:B、C、G 已訪,H 未訪)→ D(H:B 已訪、D 未訪)→ D 的鄰居都已訪,一路退回

DFS:A, B, C, E, G, F, H, D

(b) BFS(queue):A 出列,把 B C E G 入列;B 出列,新鄰居 D F H 入列;C、E、G、D、F、H 出列時沒有新的點

BFS:A, B, C, E, G, D, F, H

(c) 要找「經過最少邊數」的路徑(unweighted graph 的 shortest path)、或一層一層往外找離起點最近的點時用 BFS。例:社群網站找「朋友的朋友」(距離 2 以內)、網路爬蟲依層級爬、迷宮最短步數;DFS 不保證先找到的是最短路徑

(d) 要走到底再回頭、需要完整的路徑與回溯、或記憶體吃緊時用 DFS(stack 深度只要路徑長度,BFS 的 queue 要放一整層)。例:偵測 cycle、topological sort、找 connected/strongly connected components、找 articulation point、走迷宮/backtracking 窮舉所有路徑

(e) Kruskal:把所有邊依權重由小到大排序,依序檢查每條邊;若兩端點在不同的連通元件(用 union-find 判斷),就加入並合併兩個元件,否則(會形成 cycle)跳過;選滿 \(|V|-1=7\) 條邊就停

邊(權重)BC 1AG 2BD 2BF 2FG 2AB 3BH 3CF 3AC 4EG 4
結果選選選選選跳過(A、B 已同組)選跳過跳過選

選到第 7 條(EG)時 8 個點全部連起來,後面的 AE 5、CE 5、DH 5、FH 6 不用再看

MST 邊:BC, AG, BD, BF, FG, BH, EG;總成本 \(1+2+2+2+2+3+4=16\)

這棵 MST 唯一:權重 2 的四條邊(AG、BD、BF、FG)加上 BC 不構成 cycle,D、H、E 各自只有一條最輕的連接邊(BD、BH、EG)

(f) articulation point:拿掉它之後圖不再連通的點。逐點檢查:拿掉任一點後,剩下的點仍連通——例如拿掉 B,D 還能經 H–F 連回去;拿掉 F,H 還有 B、D;拿掉 A,E 還有 C、G;其餘點的鄰居彼此另有路徑可通。用 DFS(A, B, C, E, G, F, H, D)的 low-link 也可以確認:沒有任何點有 child 的 low 值 ≥ 它自己的 discovery time(根 A 的 DFS 樹只有一個 child B)

0 個(G 是 biconnected graph)

615 分

Assume the structure of a linked list node is as follows.

struct node { int data; struct node *next; };

(a) Please show the result of the following C functions on a given linked list: (4 分) A -> C -> D -> B -> E -> null

void fun2(struct node* head)
{
   if (head == NULL)
       return;
   printf("%d", head->data);

   if (head->next != NULL)
       fun2(head->next->next);
   printf("%d", head->data);
}

(b) Give a linked list pointed by list_head, please use pseudocode to write a function for printing out the data in the linked list in reverse order. (5 分)

(c) Assume an array of size 5 is used to implement the (1) stack and (2) circular queue data structures, respectively. To perform the following sequence: add(B), add(C), add(A), remove, add(C), add(E), remove, add(D), add(A) on the two data structures, respectively. For the two data structures, please show the content of the array for “each step”. Please also show TOP (top of the stack) for stack and both Front and Rear for queue. (6 分).

(a) list:A → C → D → B → E。每次遞迴跳 2 個節點(head->next->next)

fun2(A):印 A;A.next=C 不是 NULL → fun2(A.next.next = D)

fun2(D):印 D;D.next=B 不是 NULL → fun2(D.next.next = E)

fun2(E):印 E;E.next = NULL,不遞迴;印 E;回到 fun2(D):印 D;回到 fun2(A):印 A

題目的 data 是 A~E 各節點的資料(以字母代表)

A D E E D A

(b) 遞迴:先處理後面的節點、再印自己

printReverse(node):
    if node == NULL: return
    printReverse(node->next)      // 先印後面的
    print(node->data)             // 再印自己

printReverse(list_head)

不用遞迴的作法:走訪一遍,每個節點的 data push 進 stack,走完後一個一個 pop 出來印;或先把 list 反轉再印。時間 \(O(n)\)

(c) (1) stack:array 大小 5,add = push(TOP 加 1 再放入),remove = pop(取走 TOP 再減 1)。TOP=−1 表示空

步驟[0][1][2][3][4]TOP
開始−1
add(B)B0
add(C)BC1
add(A)BCA2
remove(取出 A)BC1
add(C)BCC2
add(E)BCCE3
remove(取出 E)BCC2
add(D)BCCD3
add(A)BCCDA4

(2) circular queue:add 從 rear 端加(\(\text{rear}=(\text{rear}+1)\bmod5\) 再放入),remove 從 front 端取(取 front 位置的元素,\(\text{front}=(\text{front}+1)\bmod5\))。front=第一個元素的位置,rear=最後一個元素的位置,初始 front=0、rear=−1(空)

步驟[0][1][2][3][4]FrontRear
開始0−1
add(B)B00
add(C)BC01
add(A)BCA02
remove(取出 B)CA12
add(C)CAC13
add(E)CACE14
remove(取出 C)ACE24
add(D)DACE20
add(A)DAACE21

rear 繞回 [0]、[1] 重用被 remove 騰出的空格,所以最後 5 格都滿;若採「front 指在第一個元素前一格、永遠留一格空」的寫法,最多只放 4 個,第 9 步 add(A) 就會 overflow

710 分

Answer the following questions: (a) Consider a hash table consisting of 8 buckets, where each bucket can store a value. By using the hash function: “k%8” and linear probing. Please insert the keys: 5, 22, 21, 0, 24 and 15 into the hash table and show the result in the table. (5 分) (b) Consider the data structure: a hash table and a binary search tree. Assume that no data collision occurring in the hash table and the constructed binary tree is balanced. In each of the following situations, which data structure can perform more efficiently than the other one? Please explain your answer. (5 分) (1) Insert or retrieve an element. (2) Print out all the elements in the data structure in sorted order.

(a) \(h(k)=k\bmod8\),被占了就往下一格(到 7 後繞回 0)

5:\(5\bmod8=5\) → 放 5

22:\(22\bmod8=6\) → 放 6

21:\(21\bmod8=5\) 有人;6 有人;7 空 → 放 7

0:\(0\) → 放 0

24:\(24\bmod8=0\) 有人;1 空 → 放 1

15:\(15\bmod8=7\) 有人;繞回 0 有人;1 有人;2 空 → 放 2

bucket01234567
key0241552221

(b) (1) insert/retrieve:hash table 較有效率。沒有 collision 時算一次 hash 就直接定位,\(O(1)\);balanced BST 要從根往下比較,\(O(\log n)\)

(2) 依大小順序印出全部:binary search tree 較有效率。對 BST 做 in-order traversal 就是由小到大,\(O(n)\);hash table 裡的位置與 key 大小無關,要先把全部元素取出再排序,\(O(n\log n)\)(還要掃過所有 bucket)

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com