免費詳解 / 交大 / 軟體 / 103

103 交大資工所考古題解答|軟體逐題詳解

103 交大資工所軟體考古題的逐題詳解,共 12 題,免費公開。

逐題詳解

16%

Given a binary search tree, \(T\), as shown in Figure 1, split \(T\) at node 9 such that we have two trees Small and Large, and a node 9 (Small consists of nodes less than 9 and Large consists of nodes greater than 9). Draw the trees Small and Large.

(Figure 1:root 3,左子 2(左子 1)、右子 12;12 的左子 4、右子 13;4 的右子 11;11 的左子 7;7 的左子 6(左子 5)、右子 9(左子 8、右子 10);13 的右子 16;16 的左子 15;15 的左子 14)

12345678910111213141516

沿著找 9 的路徑 \(3\to12\to4\to11\to7\to9\) 走一次

小於 9 的節點(連同它的左子樹)接到 Small 的最右端;大於 9 的節點(連同右子樹)接到 Large 的最左端;走到 9 時,9 的左子樹 8 掛到 Small 最右端、右子樹 10 掛到 Large 最左端

遞迴寫法:\(\text{split}(t)\):\(t\lt9\) → \(t.\text{right}\) 換成 \(\text{split}(t.\text{right})\) 的 Small 部分,回傳 \((t,\ \text{Large})\);\(t\gt9\) 對稱

Small(1–8)

12345678

Large(10–16)

10111213141516

節點 9 單獨拿出來

兩棵樹的 in-order 分別是 1…8 與 10…16,仍是 BST

如上圖:Small、Large 與單獨的 9

26%

Given two min-leftist trees shown in Figure 2, merge the two trees to get a new min-leftist tree. Draw the resulted tree.

(Figure 2 左:2(左 7—11—13 一路向左、右 50—80);右:5(左 9、右 8);9 的左子 12(子 20、18)、右子 14(左子 90);8 的左子 10(左子 15))

1311728050  20121899014515108

\(s(\cdot)\)=到外部節點的最短距離;meld 時沿較小 root 的右邊走,回頭時若 \(s(\text{left})\lt s(\text{right})\) 就交換左右

\(\text{meld}(2,5)\):\(2\lt5\),2 為 root,把 2 的右子樹 50 與整棵 5 合併

\(\text{meld}(50,5)\):5 為 root,5 的右子樹 8 與 50 合併;\(\text{meld}(8,50)\):8 為 root,50 掛到 8 的右邊

回推:8 的左右 \(s\) 都是 1,不換;5 的左(9)\(s=2\)、右(8)\(s=2\),不換,\(s(5)=3\)

2 的左(7)\(s=1\)、右(5 那棵)\(s=3\),左小於右,要交換

201218990145151088050213117

只有一個子節點的一律掛在左邊(leftist tree 的規定)

root 2:左子樹 5(左 9、右 8),右子樹 7(左 11—13)

34%

Given the AVL tree shown in Figure 3, insert a node with key value 12.

(Figure 3:root 5;左 3(左 2(左 1)、右 4);右 8(左 6(右子 7)、右 10(左 9、右 11)))

1234567891011

12 沿 \(5\to8\to10\to11\) 往右走,成為 11 的右子

回推 balance factor(左高 − 右高):11:\(-1\);10:\(1-2=-1\);8:\(2-3=-1\);5:\(3-4=-1\)

全部落在 \([-1,1]\),不需要旋轉

123456789101112

不旋轉,如上圖

49%

Union/Find operation: Show that a tree obtained by applying the weighting rule for Union is bounded above by \(O(\log n)\).

weighting rule:union 時,節點數較少的那棵的 root 掛到節點數較多的那棵的 root 底下

令 \(h(n)\)=由 \(n\) 個節點 union 出的樹的最大高度(單一節點高 1),對 \(n\) 做數學歸納,證 \(h(n)\le\lfloor\log_2n\rfloor+1\)

\(n=1\):\(h=1=\lfloor\log_21\rfloor+1\),成立

設節點數小於 \(n\) 時都成立。\(T\) 由 \(T_1\)(\(n_1\) 個)與 \(T_2\)(\(n_2\) 個)union 而成,\(n_1+n_2=n\),\(n_1\ge n_2\),\(T_2\) 的 root 掛到 \(T_1\) 的 root 底下

\(h(T)=\max\{h(T_1),\ h(T_2)+1\}\)

\(h(T_1)\le\lfloor\log_2n_1\rfloor+1\le\lfloor\log_2n\rfloor+1\)

\(n_2\le n/2\) → \(h(T_2)+1\le\lfloor\log_2n_2\rfloor+2\le\lfloor\log_2(n/2)\rfloor+2=\lfloor\log_2n\rfloor+1\)

兩項都 \(\le\lfloor\log_2n\rfloor+1\) → \(h(T)\le\lfloor\log_2n\rfloor+1=O(\log n)\),得證

59%

Please derive the corresponding time complexity (Big-Oh) for each of the following six program segments.

(a) k=0;
    for (i=0; i<N; i++)
      for (j=0; j<i; j++)
        k++;
(b) k=0;
    for (i=0; i<N; i++)
      for (j=0; j<i*i; j++)
        for (z=0; z<j; z++)
          k++;
(c) k=0;
    for (i=1; i<N; i++)
      for (j=1; j<i*i; j++)
        if (j%i==0)
          for (z=0; z<j; z++)
            k++;

(a) \(\sum_{i=0}^{N-1}i=\dfrac{N(N-1)}{2}\)

\(O(N^2)\)

(b) \(\sum_{i}\sum_{j=0}^{i^2-1}j=\sum_i\dfrac{i^2(i^2-1)}{2}\approx\dfrac12\sum_ii^4\approx\dfrac{N^5}{10}\)

\(O(N^5)\)

(c) \(j\) 要是 \(i\) 的倍數,在 \(1\le j\lt i^2\) 內有 \(j=i,2i,\ldots,(i-1)i\) 共 \(i-1\) 個;每個進內層跑 \(j\) 次

\(\sum_{i}\sum_{t=1}^{i-1}ti=\sum_ii\cdot\dfrac{i(i-1)}{2}\approx\dfrac12\sum_ii^3\approx\dfrac{N^4}{8}\)

\(O(N^4)\)

612%

Given the following graph(0→1 權 2、0→2 權 3、0→3 權 6、1→0 權 1、1→2 權 5、1→3 權 10、2→3 權 2、3→0 權 2、3→2 權 8)

01232361510228

(a) (3%) Use adjacency lists represent this graph.

(b) (4%) According to the adjacency lists obtained, output DFS(0) and BFS(0).

(c) (5%) Please derive all-pairs shortest-paths of the graph.

(a) 鄰居照編號由小到大放

0 → 1(2) → 2(3) → 3(6)
1 → 0(1) → 2(5) → 3(10)
2 → 3(2)
3 → 0(2) → 2(8)

(b) DFS(0):0 → 1(0 的第一個鄰居)→ 1 的鄰居 0 已訪問,下一個 2 → 2 的鄰居 3 → 3 的鄰居 0、2 都訪問過,回溯結束

BFS(0):0 的鄰居 1, 2, 3 依序入 queue

DFS(0):0 1 2 3  BFS(0):0 1 2 3

(c) Floyd–Warshall,\(D^{(k)}\) 為允許 0..\(k\) 當中間點的距離(列為起點、欄為終點)

初始:

0123
00236
110510
2∞∞02
32∞80

\(k=0\):\(d(1,2)=5\to4\)(經 0:\(1+3\));\(d(1,3)=10\to7\)(經 0:\(1+6\));\(d(3,1)=\infty\to4\)(經 0:\(2+2\));\(d(3,2)=8\to5\)(經 0:\(2+3\))

0123
00236
11047
2∞∞02
32450

\(k=1\):沒有更短的路徑,整張表不變

0123
00236
11047
2∞∞02
32450

\(k=2\):\(d(0,3)=6\to5\)(經 2:\(3+2\));\(d(1,3)=7\to6\)(經 2:\(4+2\))

0123
00235
11046
2∞∞02
32450

\(k=3\):\(d(2,0)=\infty\to4\)(經 3:\(2+2\));\(d(2,1)=\infty\to6\)(經 3:\(2+4\))

0123
00235
11046
24602
32450

all-pairs shortest-path 距離矩陣如最後一張表

74%

Given input {4371, 1323, 6173, 4199, 4344, 9679, 1989} and a hash function \(h(X)=(X\bmod10)\), show the results: (a) open addressing hash table using linear probing (\(F(i)=i\)) (b) open addressing hash table using quadratic probing (\(F(i)=3\cdot i^2\))

位置 = \((h(X)+F(i))\bmod10\)

(a) 4371 → 1;1323 → 3;6173 → 3 已佔,放 4;4199 → 9;4344 → 4 已佔,放 5;9679 → 9 已佔,繞回 0;1989 → 9、0、1 都佔,放 2

index0123456789
key9679437119891323617343444199

(b) 4371 → 1;1323 → 3;6173 → 3 已佔,\(i=1\):\(3+3=6\);4199 → 9;4344 → 4

9679 → 9 已佔,\(i=1\):\(9+3=12\to2\)

1989 → 9 已佔;\(i=1\):2 已佔;\(i=2\):\(9+12=21\to1\) 已佔;\(i=3\):\(9+27=36\to6\) 已佔;\(i=4\):\(9+48=57\to7\) → 放 7

index0123456789
key4371967913234344617319894199

如上兩張表

810%

Consider the following problems and algorithms. (1) Merge sort for sorting problem. (2) Heap sort for sorting problem. (3) Minimum spanning tree problem. (4) Floyd-Warshall algorithm for all pairs shortest path problem. (5) Finding a maximum independent set in an interval graph. (6) Huffman's algorithm for Huffman code problem. (7) Strassen's matrix multiplication algorithm. (8) Constructing an optimal binary search tree for weighted searching problem, i.e. to minimize the expected searching time. (9) Longest common subsequence problem. (10) The Ford-Fulkerson method for maximum flow problem.

Each of the above problems (or algorithms) can be solved by (or classified as) one of the following design techniques: (a) Divide and Conquer. (b) Dynamic Programming. (c) Greedy method. (d) Others (none of the above three techniques). Choose the proper technique to each of the above problems.

題問題技巧
(1)Merge sort(a)
(2)Heap sort(d)
(3)Minimum spanning tree(c)
(4)Floyd–Warshall(b)
(5)Interval graph 的 maximum independent set(c)
(6)Huffman code(c)
(7)Strassen(a)
(8)Optimal binary search tree(b)
(9)Longest common subsequence(b)
(10)Ford–Fulkerson(d)

(2) heap sort 靠 heap 資料結構反覆取最大值,不屬於前三種

(3) Kruskal、Prim 都是 greedy

(5) interval graph 的 MIS 等價於 activity selection,依結束時間 greedy

(10) 不斷找 augmenting path 的 iterative improvement

(1) a (2) d (3) c (4) b (5) c (6) c (7) a (8) b (9) b (10) d

910%

Assume P ≠ NP. For each of the following problems, decide whether it is a P-problem, or an NP-hard (or NP-complete) problem, or neither. Please answer 1, if it is a P-problem, answer 2 if it is an NP-hard (or NP-complete) problem, and answer 3 if it is neither 1 nor 2.

(1) Find a longest simple path between two nodes, where the given graph has positive edge weights. (2) Find a shortest simple path between two nodes in a directed graph with negative and/or positive edge weights, and containing negative weight cycles. (3) Find a negative weight directed cycle in a weighted directed graph. (4) Find a positive weight directed cycle in a weighted directed graph. (5) Find a largest cycle in a graph, where the edge-weight is 1 for each edge. (6) Find a smallest cycle in a graph, where the edge-weight is 1 for each edge. (7) Find a maximum cut in a flow network. (8) Find a minimum cut in a flow network. (9) Find a maximum independent set in an interval graph. (10) The 2-CNF-Satisfiability problem.

題答案理由
(1)2由 Hamiltonian path 可 reduce 過來
(2)2邊權取負號,等於正權圖上的 longest simple path,同 (1);限定 simple path,所以沒有 −∞
(3)1Bellman–Ford 偵測
(4)1邊權取負後偵測 negative cycle
(5)2邊權皆 1 的最長 cycle 若多項式可解,就解了 Hamiltonian cycle
(6)1girth:每個點 BFS 一次
(7)2max cut 是經典 NP-complete
(8)1max-flow min-cut theorem,用最大流求
(9)1greedy(見第 8 題 (5))
(10)1implication graph 找 SCC,線性時間

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

105%

Consider a two-position switch with two inputs and two outputs, as shown in Fig. A. In one position inputs 1 and 2 are connected to outputs 1 and 2, respectively. In the other position inputs 1 and 2 are connected to outputs 2 and 1, respectively. Using these switches and applying the divide and conquer technique, we can design a network with n inputs and n outputs which is capable of achieving any of the n! possible permutations of the inputs, as shown in Fig. B.

(Fig. B:輸入端一排 \(n/2\) 個 switch(1,2/3,4/…/n−1,n 各一個),中間兩個 size \(n/2\) 的 switch network(上半、下半),輸出端一排 \(n/2\) 個 switch)

(1) Find the number of switches used in the networks for \(n=8\). (2) For general \(n=2^k\), find the number of switches used in the networks. (Write your answer in terms of \(n\) and/or \(k\). Give the exact number, NOT just the asymptotic big O order.)

\(S(n)\)=\(n\) 個輸入的 network 的 switch 數:前後各一排 \(n/2\) 個,加上兩個 size \(n/2\) 的子 network

\(S(n)=2S(n/2)+n,\qquad S(2)=1\)

(1) \(S(4)=2\cdot1+4=6\);\(S(8)=2\cdot6+8=20\)

20 個

(2) 兩邊同除 \(n\):令 \(T(k)=S(2^k)/2^k\),\(T(k)=T(k-1)+1\),\(T(1)=\tfrac12\) → \(T(k)=k-\tfrac12\)

\(S(n)=n\log_2n-\dfrac n2=nk-\dfrac n2\)

驗:\(n=8\):\(8\cdot3-4=20\)

1115%

For each of the following statements to indicate whether the statement is true or false, respectively. If the statement is true, briefly state why. If the statement is false, explain why or give a counterexample. You will be deducted one point with wrong justification or without justification.

a. Let \(G=(V,E)\) be a directed graph with negative-weight edges, but no negative-weight cycles. Then, one can compute all shortest paths from a source \(s\in V\) to all \(v\in V\) faster than Bellman-Ford using the technique of reweighting.

b. Let \(G=(V,E)\) be a connected, undirected graph with edge weights, and assume all edge weights are distinct. Consider a cycle \(\langle v_1,v_2,\ldots,v_k,v_{k+1}\rangle\) in \(G\), where \(v_{k+1}=v_1\), and let \((v_i,v_{i+1})\) be the edge in the cycle with the largest edge weight. The \((v_i,v_{i+1})\) does not belong to the minimum spanning tree \(T\) of \(G\).

c. Let \(T\) be a minimum spanning tree of \(G\). Then, for any pair of vertices \(s\) and \(t\), the shortest path from \(s\) to \(t\) in \(G\) is the path from \(s\) to \(t\) in \(T\).

d. Given a weighted directed graph \(G=(V,E,w)\) and a shortest path \(p\) from \(s\) to \(t\), if we doubled the weight \(w\) to every edge to produce \(G_0=(V,E,w_0)\), then \(p\) is also a shortest path in \(G_0\).

e. Let \(G\) be an arbitrary flow network, with a source \(s\), a sink \(t\), and a positive integer capacity \(c_e\) on every edge \(e\); and let \((A,B)\) be a minimum \(s\)-\(t\) cut with respect to these capacities. Now suppose we add 1 to every capacity; then \((A,B)\) is still a minimum \(s\)-\(t\) cut with respect to these new capacities.

a F。reweighting 要先算 \(h(v)\),而 \(h\) 本身就是在含負邊的圖上跑一次 Bellman–Ford,\(O(VE)\)。單一 source 的總時間不會比直接跑 Bellman–Ford 快(Johnson 的好處在 all-pairs)

b T。cycle property:若 \((v_i,v_{i+1})\) 在 MST \(T\) 中,拿掉它 \(T\) 被切成兩塊,cycle 上其餘的邊必有一條跨過這個切口,用它接回來得到權重更小的 spanning tree(權重互異),矛盾

c F。MST 只保證總權重最小。反例:點 \(s,a,t\),\(w(s,a)=2,\ w(a,t)=2,\ w(s,t)=3\);MST 是 \(\{sa,at\}\),\(T\) 上 \(s\) 到 \(t\) 是 4,圖上直接走 \(st\) 只要 3

d T。每條路徑的總權重都乘 2,路徑間的大小關係不變,原來最短的 \(p\) 仍然最短

e F。每條邊加 1,一個 cut 的容量增加它所含的邊數,邊多的 cut 漲得比較多。反例:\(S\to A\) 容量 4,\(A\) 到 \(T\) 有三條平行的容量各為 1 的邊;min cut 是後三條(容量 3);每條邊加 1 後變 6,而 \((S,A)\) 這個 cut 只有 5,min cut 換成 \((S,A)\)

a F b T c F d T e F

1210%

In the flow network illustrated below, each directed edge is labeled with its capacity. We are using the Ford-Fulkerson algorithm to find the maximum flow.

(圖:\(S\to A\) 2、\(S\to C\) 4、\(A\to B\) 3、\(A\to C\) 1、\(B\to C\) 3、\(B\to T\) 4、\(C\to D\) 3、\(D\to B\) 1、\(D\to T\) 3)

SABCDT243134313

a. (5 points) Show every augmentation path (but you do not need to show the whole network to save time) using Edmonds–Karp algorithm. Draw the residual network after you have updated the flow using these augmenting paths.

b. (5 points) What is maximum flow? Draw a dotted line through the original graph to represent the minimum cut.

a Edmonds–Karp:每次用 BFS 找邊數最少的 augmenting path

回合augmenting pathbottleneck累計流量
1\(S\to A\to B\to T\)\(\min(2,3,4)=2\)2
2\(S\to C\to D\to T\)\(\min(4,3,3)=3\)5

(兩條路徑都是 3 條邊,先後順序不影響結果)

第 3 回合 BFS:從 \(S\) 出發 \(S\to A\) 已滿,只能走 \(S\to C\)(residual 1);\(C\to D\) 已滿,\(C\) 沒有可退的反向邊 → 到不了 \(T\),停止

最後的 residual network(只畫 residual 為正的邊,標的是 residual capacity):

SABCDT111321222333

b residual network 中從 \(S\) 走得到的點是 \(\{S,C\}\),切口落在這裡

min cut \(=\{(S,A),(C,D)\}\),容量 \(2+3=5\),等於流量

max flow = 5;min cut 把 \(\{S,C\}\) 與 \(\{A,B,D,T\}\) 分開(\(S\to A\)、\(C\to D\))

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

國立陽明交通大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 579 頁,逐題推導。

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com