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

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

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

逐題詳解

120%

(20 points) Prove, calculate or rank the asymptotic bounds for the following functions.

(a) (10 points) Given the n-th harmonic number \(H(n)\):

\(H(n)=\sum_{k=1}^{n}1/k\quad(n\gt0)\),

prove \(H(n)=O(\log n)\) and \(H(n)=\Omega(\log n)\).

(b) (5 points) Give the asymptotically tight bound (\(\theta\)-notation) for the recurrence \(T(n)\):

\(T(n)=2T(n/2)+n/\log n\quad(\text{assume }T(1)=1)\).

Show the derivation.

(c) (5 points) Rank the following functions \(f_1\ldots f_5\) by order of growth. If \(f_i=O(f_j)\) (\(i\ne j\)), then \(f_i\prec f_j\).

\(f_1=\log^2n\), \(f_2=n\log n\), \(f_3=\sqrt n\), \(f_4=\log(n!)\), \(f_5=(3/2)^n\).

Please complete the form: ___ \(\prec\) ___ \(\prec\) ___ \(\prec\) ___ \(\prec\) ___.

(a) \(1/x\) 遞減,用積分夾 \(H(n)\)

上界:\(\dfrac1k\le\displaystyle\int_{k-1}^{k}\dfrac{dx}x\ (k\ge2)\) → \(H(n)\le1+\displaystyle\int_1^n\dfrac{dx}x=1+\ln n\)

下界:\(\dfrac1k\ge\displaystyle\int_{k}^{k+1}\dfrac{dx}x\) → \(H(n)\ge\displaystyle\int_1^{n+1}\dfrac{dx}x=\ln(n+1)\)

\(\ln(n+1)\le H(n)\le1+\ln n\)

\(H(n)=O(\log n)\),\(H(n)=\Omega(\log n)\) (\(H(n)=\Theta(\log n)\)) 得證

(b) 展開遞迴樹,第 \(i\) 層 \(2^i\) 個節點,每個 cost \(\dfrac{n/2^i}{\log(n/2^i)}\)

第 \(i\) 層總和 \(=\dfrac{n}{\log n-i}\),\(i=0,1,\ldots,\log n-1\)

\(\displaystyle\sum_{i=0}^{\log n-1}\dfrac n{\log n-i}=n\sum_{j=1}^{\log n}\dfrac1j=n\,H(\log n)=\Theta(n\log\log n)\) (用 (a))

葉子 \(n\cdot T(1)=n\),比 \(n\log\log n\) 小

另解:\(n=2^k\),\(A_k=T(2^k)/2^k\) → \(A_k=A_{k-1}+\dfrac1{k\ln 2}\),\(A_k=\Theta(\log k)\);數值上 \(T(n)/(n\log\log n)\) 隨 \(n\) 變大往 1 靠

\(T(n)=\Theta(n\log\log n)\)

(c) \(\log^2n\) 是 polylog,\(\sqrt n\) 是 polynomial → \(f_1\prec f_3\)

\(\sqrt n\prec n\log n\);\(n\log n\) 是 polynomial,\((3/2)^n\) 是 exponential → \(f_2\prec f_5\)

Stirling:\(\log(n!)=n\log n-n\log e+O(\log n)=\Theta(n\log n)\),所以 \(f_4\) 與 \(f_2\) 同階,\(f_4\le f_2\)

\(f_2=O(f_4)\) 與 \(f_4=O(f_2)\) 都成立,兩者誰在前只差 lower-order 項 \(n\log e\);照 \(f_4\le f_2\) 排

\(f_1\prec f_3\prec f_4\prec f_2\prec f_5\) (\(f_4=\Theta(f_2)\))

210%

(10 points) A red-black tree is a special type of binary search trees. Given the following binary search tree on 8 keys (i.e., integers 1…8), please answer the questions.

12345678

(a) (5 points) The tree cannot be simply transformed to a red-black tree by coloring its internal nodes. Explain what property of the red-black tree cannot be satisfied.

(b) (5 points) Explain how to transform this tree to a red-black tree through a single rotation. Draw the constructed red-black tree and label its nodes with either B (black) or R (red) color.

(a) root 2 → 1 → nil:2 個 internal node(最短)

root 2 → 4 → 6 → 8 → 7 → nil:5 個 internal node(最長)

red-black tree 每條 root-to-nil path 的 black 數相同(設 \(b\))、且不能有連續兩個 red

→ 每條 path 長度介於 \(b\) 與 \(2b\);最短 path 長 2 → \(b\le2\) → 最長 path \(\le4\),但這棵有一條 5

無法滿足「每條 root-to-leaf path 的 black-height 相同」(不管怎麼塗,最長 path 都超過最短 path 的 2 倍)

(b) 對 root 2 做 left rotation:4 上來當 root,2 掉下去當 4 的左子(帶著 1、3)

最長 path 變 4→6→8→7(4 個),最短 4→2→1(3 個),\(4\le2\cdot3\)

塗色:4 B;2、6 R;1、3、5、8 B;7 R → 每條 path 都 2 個 black,red 的子節點都是 black(nil 為 black)

12345678

left-rotate(root 2),顏色:4(B)、2(R)、6(R)、1(B)、3(B)、5(B)、8(B)、7(R)

317%

(17 points) Huffman coding is a type of coding schemes used for lossless data compression. In the coding scheme, each character in a file is represented by a unique string, which is called a codeword. The length of a codeword that represents a frequently appeared character in the input file is usually shorter than other codewords as well as the original character. Thus, when the entire file is encoded by converting all characters in the file to codewords which represent them, the total length can usually be significantly reduced. In a binary Huffman code, the number of symbols that can be used to form a codeword is 2. We can also use a \(n\)-ary Huffman code, in which the number of symbols that can be used to form a codeword is \(n\).

(a) (8 points) Please prove that the decoding tree which represents an optimal code for a file (using which the encoded file has a minimum length) must be a full \(n\)-ary tree, i.e., all non-leaf nodes in the tree have \(n\) children.

(b) (4 points) You are given the numbers of appearances of all characters in a file as follows: {'a' : 700, 'b' : 400, 'c' : 200, 'd' : 100, 'e' : 1300, 'f' : 2400, 'g' : 100}. What are the lengths of the codewords for the characters 'a' and 'c', respectively, in a 3-ary Huffman code derived for this file?

(c) (5 points) Which of the following statements are correct? Please mark all correct statements.

i. A Huffman code is a fixed-length code.

ii. A Huffman code is a prefix code.

iii. If a file contains a sequence of 8-bit characters such that the maximum character frequency is less than three times of the minimum character frequency, then binary Huffman codings always cannot do better than an ordinary 8-bit fixed-length code.

iv. If a file contains a sequence of 8-bit characters such that the maximum character frequency is less than twice of the minimum character frequency, then binary Huffman codings always cannot do better than an ordinary 8-bit fixed-length code.

v. If a file contains a sequence of 8-bit characters such that the maximum character frequency is less than 1.5 times of the minimum character frequency, then binary Huffman codings always cannot do better than an ordinary 8-bit fixed-length code.

(a) 設 internal node \(x\) 的 child 少於 \(n\) 個(還有空的 symbol 沒用)

若有 leaf \(\ell\) 的深度 \(\gt\operatorname{depth}(x)+1\):把 \(\ell\) 搬成 \(x\) 的 child

→ 仍是 prefix code(\(x\) 的空 symbol 本來沒人用);\(\ell\) 的 codeword 變短,其他 leaf 的深度不變

(\(\ell\) 原來的 parent 若只剩一個 child,就把它縮掉,更短)

頻率 \(\gt0\) → 總長度嚴格變小,與 optimal 矛盾

所以沒填滿的 internal node 只可能在倒數第二層(它的 child 全是最深的 leaf)

這種節點用 \(n-k\) 個頻率 0 的 dummy leaf 補滿,總長度不變 → optimal tree 可取成每個 non-leaf 都有 \(n\) 個 child,得證

symbol 數 \(m\) 若滿足 \((m-1)\bmod(n-1)=0\),連 dummy 都不用,tree 本來就 full;否則要補 \((1-m)\bmod(n-1)\) 個 dummy。例:\(n=3\)、只有 2 個 symbol,root 只能有 2 個 child,此時要靠 dummy 才是 full 3-ary

(b) \(m=7\),\(n=3\):\((7-1)\bmod2=0\),不用 dummy,每次合併最小的 3 個

\(\{d,g,c\}=100+100+200=400\)  剩 \(a700,\ b400,\ e1300,\ f2400,\ [dgc]400\)

\(\{b,[dgc],a\}=400+400+700=1500\)  剩 \(e1300,\ f2400,\ [\cdots]1500\)

\(\{e,[\cdots],f\}=1300+1500+2400=5200\)(=總次數 \(700+400+200+100+1300+2400+100\))

深度:e、f 在 1;a、b 在 2;c、d、g 在 3

'a' 長度 \(2\),'c' 長度 \(3\)

(c)

i Huffman code 是 variable-length(頻率高的字碼短) → F

ii 由 tree 的 leaf 讀出 codeword,沒有一個是另一個的 prefix → T

iv 設 256 個字元頻率都在 \([w,2w)\)。最小兩個相加 \(\ge2w\gt\) 任何單一字元,所以 256 個 leaf 會先兩兩合併完(128 個新節點,權重在 \([2w,4w)\),最大 \(\lt\) 最小的 2 倍);同樣的論證再來 7 輪

→ 得到深度 8 的 complete binary tree,每個 codeword 8 bit,跟 fixed-length 一樣長 → T

v 條件比 iv 強(\(\lt1.5\) 蘊含 \(\lt2\)) → T

iii 條件放寬到 \(\lt3\),反例:128 個字元頻率 2、128 個字元頻率 5(比值 \(2.5\lt3\))

Huffman 總長 \(=7104\) bits \(\lt 8\times(128\cdot2+128\cdot5)=8\times896=7168\),比 8-bit fixed-length 短 → F

Huffman 總長 7104;另一個小例子:4 個字元頻率 \(1,1,2.5,2.9\) 的小例子也是 \(\tfrac{139}{74}\approx1.88\lt2\) bit

ii、iv、v

410%

(10 points) You are given a convex polygon \(P=\{v_1,v_2,\ldots,v_n\}\), where \(v_i\), \(1\le i\le n\), are the vertices that form the polygon. In a convex polygon, each internal angle is less than or equal to 180 degrees and each line segment between two vertices remains inside or on the boundary of the polygon.

A triangulation of a convex polygon is a set of non-intersecting diagonals that partitions the polygon into triangles. We define that the weight of a triangulation is the sum of the lengths of the perimeters of all component triangles. The minimum triangulation problem is to find the triangulation of the given polygon that has the minimum weight.

Denote the cost of a minimum triangulation of the polygon formed by \(\{v_i,v_{i+1},\ldots,v_j\}\) as \(c(i,j)\). Then the minimum triangulation has a weight of \(c(1,n)\).

(a) (6 points) Fill the blanks below to complete the recurrences which can be used for a dynamic programming algorithm to solve this problem.

\(c(i,j)=\begin{cases}0&,\ \underline{\quad\text{(a)}\quad}\\\min_{i\lt k\lt j}\{\ \underline{\quad\text{(b)}\quad}\ \}&,\ \text{otherwise}\end{cases}\)

(b) (4 points) Using the recurrences shown in the previous subproblem to develop a dynamic programming algorithm to solve the problem as efficient as possible, what would be its time complexity in terms of \(n\), the number of vertices in the given convex polygon \(P\)?

(a) \(v_i\ldots v_j\) 只有 2 個點(\(j=i+1\))不成多邊形,cost 0

其他情況:選 \(k\)(\(i\lt k\lt j\)),讓 \(\triangle v_iv_kv_j\) 當邊 \(v_iv_j\) 所在的那個三角形,剩下 \(v_i\ldots v_k\) 與 \(v_k\ldots v_j\) 兩個子多邊形

三角形的 perimeter \(w(i,k,j)=|v_iv_k|+|v_kv_j|+|v_iv_j|\)

(a) \(j=i+1\)(寫 \(j\le i+1\) 亦可)

(b) \(c(i,k)+c(k,j)+w(i,k,j)\),\(w(i,k,j)=|v_iv_k|+|v_kv_j|+|v_iv_j|\)

(b) 子問題 \(c(i,j)\),\(1\le i\lt j\le n\):\(O(n^2)\) 個;每個枚舉 \(k\):\(O(n)\);\(w\) 用事先算好的距離表 \(O(1)\) 查

依 \(j-i\) 由小到大填表

\(O(n^3)\)

513%

(13 points) Suppose that we use disjoint-set forests with union by rank and path compression to represent disjoint sets.

(a) (6 points) Fill the blanks in the following programs that perform the three operations for disjoint sets.

Make-Set(x)
    x.parent=x
    x.rank=0

Union(x,y)
    Link(Find-Set(x),Find-Set(y))

Link(x,y)
    if ____(a)____
        y.parent=x
    else
        x.parent=y
        if ____(b)____
            y.rank=y.rank+1

Find-Set(x)
    if x≠x.parent
        ____(c)____
    return x.parent

(b) (7 points) Draw the data structure that results after running the following program. For each tree node, please mark its parent and labels its identification number (in the form of \(x_i\), \(1\le i\le16\))

for i=1 to 16
    Make-Set(x_i)
for i=1 to 15 by 2
    Union(x_i, x_{i+1})
for i=1 to 13 by 4
    Union(x_i, x_{i+2})
Union(x_1, x_15)
Union(x_11, x_13)
Union(x_1, x_10)
Find-Set(x_2)
Find-Set(x_9)

(a) rank 大的當 parent;rank 一樣才要 \(+1\);Find-Set 遞迴時順便把 \(x\) 接到 root

(a) x.rank > y.rank (b) x.rank == y.rank (c) x.parent = Find-Set(x.parent)

(b) 逐步(Link 的 \(x,y\) 同 rank 時 \(x\) 接到 \(y\) 下面):

① 16 個單點 rank 0

② \(i\) 奇數:\(x_1\to x_2,\ x_3\to x_4,\ \ldots,\ x_{15}\to x_{16}\);root 是偶數編號,rank 1

③ \(x_2\to x_4,\ x_6\to x_8,\ x_{10}\to x_{12},\ x_{14}\to x_{16}\);root \(x_4,x_8,x_{12},x_{16}\) rank 2(\(x_1\) 仍在 \(x_2\) 下)

④ Union(\(x_1,x_{15}\)):Find-Set(\(x_1\)) 把 \(x_1\) 接到 \(x_4\);root \(x_4,x_{16}\) 同為 rank 2 → \(x_4\to x_{16}\),\(x_{16}\) rank 3

⑤ Union(\(x_{11},x_{13}\)):root \(x_{12}\)(rank 2)、\(x_{16}\)(rank 3)→ \(x_{12}\to x_{16}\);Find-Set(\(x_{13}\)) 把 \(x_{13}\) 接到 \(x_{16}\)

⑥ Union(\(x_1,x_{10}\)):Find-Set(\(x_1\)) 把 \(x_1\) 接到 \(x_{16}\),Find-Set(\(x_{10}\)) 把 \(x_{10}\) 接到 \(x_{16}\);已在同一棵,Link(\(x_{16},x_{16}\)) 不改樹

⑦ Find-Set(\(x_2\)):\(x_2\to x_4\to x_{16}\),把 \(x_2\) 接到 \(x_{16}\);Find-Set(\(x_9\)):\(x_9\to x_{10}\to x_{16}\),把 \(x_9\) 接到 \(x_{16}\)

x1x2x3x4x9x10x11x12x13x14x15x16x5x6x7x8

⑥ 的 Link(\(x_{16},x_{16}\)) 走 else 分支,\(x_{16}\).rank 從 3 變 4(題目的 code 沒擋 \(x=y\);rank 只是 height 的上界,不影響上面的樹)

兩棵樹:root \(x_{16}\)(children \(x_1,x_2,x_4,x_9,x_{10},x_{12},x_{13},x_{14},x_{15}\);\(x_3\) 在 \(x_4\) 下、\(x_{11}\) 在 \(x_{12}\) 下)與 root \(x_8\)(children \(x_6,x_7\);\(x_5\) 在 \(x_6\) 下)

615%

(15 points) What is the value of minimum cut of the following flow network? What is the residual network corresponding to your solution?

sabcdeft105159415151081015643010

題目圖的節點沒有名字,標成:\(a,b\) 上排左右、\(c,d\) 中排、\(e,f\) 下排。先找 max flow(流量/容量):

\(s\to a\to b\to t\):9;\(s\to a\to d\to t\):1;\(s\to c\to d\to t\):5 + \(s\to e\to f\to c\to d\to t\):3;\(s\to e\to f\to t\):10

sabcdeft10/105/513/159/90/41/150/159/108/89/100/153/60/413/3010/10

總流量 \(=10+5+13=28\) (流入 \(t\):\(b\to t\) 9+\(d\to t\) 9+\(f\to t\) 10)

residual network(forward:\(c-f\);backward:\(f\);容量 0 的邊不畫):

sabcdeft10521394141151981915334171310

從 \(s\) 走 residual:\(s\to e\to f\to c\to e\),到不了 \(a,b,d,t\)

\(S=\{s,c,e,f\}\),\(T=\{a,b,d,t\}\);\(S\to T\) 的邊:\(s\to a\ (10)\)、\(c\to d\ (8)\)、\(f\to t\ (10)\),總和 \(28\)(\(a\to c\)、\(d\to f\) 是 \(T\to S\),不算)

所有 \(s\)–\(t\) cut 中次小的是 29(\(S=\{s,a,c,d,e,f\}\)),28 的 cut 只有這一個;max flow 不唯一(例如 \(s\to e\) 的 13 單位可以改走別條),但 residual 中 \(s\) 可達的集合都一樣是 \(\{s,c,e,f\}\)

minimum cut \(=28\),\(S=\{s,c,e,f\}\);residual network 如上圖

715%

(15 points) 3-SAT and SUBSET-SUM are two NP-complete problems. In 3-SAT, we are asked whether a given boolean formula \(\phi\) is satisfiable, where \(\phi\) is in 3-conjunctive normal form (3-CNF). In the SUBSET-SUM problem, we are given a finite set of integers, \(S\), and a target integer \(t\). We ask whether there is a subset \(S'\subseteq S\) whose elements sum to \(t\). It has been shown that 3-SAT is polynomial time reducible to SUBSET-SUM. Given the following boolean formula in 3-CNF,

\(\phi=(x_1\lor x_2\lor x_3)\land(x_1\lor\overline{x_2}\lor x_3)\land(\overline{x_1}\lor\overline{x_2}\lor\overline{x_3})\)

what is its corresponding SUBSET-SUM problem? (You need to show your construction process.)

\(C_1=(x_1\lor x_2\lor x_3)\),\(C_2=(x_1\lor\overline{x_2}\lor x_3)\),\(C_3=(\overline{x_1}\lor\overline{x_2}\lor\overline{x_3})\)

每個數字 6 位(十進位):前 3 位對應 \(x_1,x_2,x_3\)、後 3 位對應 \(C_1,C_2,C_3\)

每個變數 2 個數:\(v_i\)(\(x_i\) 設 T):\(x_i\) 位是 1;\(x_i\) 出現在 \(C_j\) 就把 \(C_j\) 位設 1。\(v_i'\)(\(x_i\) 設 F):\(x_i\) 位是 1;\(\overline{x_i}\) 出現在 \(C_j\) 就把 \(C_j\) 位設 1

每個 clause 2 個 slack:\(C_j\) 位分別是 1、2,其他位 0

target:變數位都是 1、clause 位都是 4

\(x_1\)\(x_2\)\(x_3\)\(C_1\)\(C_2\)\(C_3\)
\(v_1\)100110
\(v_1'\)100001
\(v_2\)010100
\(v_2'\)010011
\(v_3\)001110
\(v_3'\)001001
\(s_1\)000100
\(s_1'\)000200
\(s_2\)000010
\(s_2'\)000020
\(s_3\)000001
\(s_3'\)000002
\(t\)111444

為什麼可以:每一位最多進位不會發生(clause 位:3 個 literal 最多 3,加 slack 最多 \(1+2=3\),總共 \(\le6\lt10\)),各位獨立

變數位要 1 → \(v_i\)、\(v_i'\) 恰選一個 → 一組 assignment

clause 位要 4:被選的 \(v\) 提供「true literal 個數」\(\in\{0,1,2,3\}\),slack 最多補 3 → 至少 1 個 true literal 才湊得到 4;有 \(\ge1\) 個,剩下 \(4-k\in\{1,2,3\}\) 都能由 \(\{0,1,2,3\}\) 湊出

例:\(x=(1,0,1)\) 滿足 \(\phi\):選 \(v_1,v_2',v_3\) → \(111231\);補 \(s_1'\ (200)\)、\(s_2\ (10)\)、\(s_3+s_3'\ (3)\) → \(111231+213=111444\) ✓

和為 111444 的子集剛好 5 個,對應的 assignment 恰是 \(\phi\) 的 5 組滿足解 \((0,0,1),(0,1,1),(1,0,0),(1,0,1),(1,1,0)\)

\(S=\{100110,\ 100001,\ 010100,\ 010011,\ 001110,\ 001001,\ 100,\ 200,\ 10,\ 20,\ 1,\ 2\}\)(前六個含前導 0 的 6 位數寫法;後六個是 slack),\(t=111444\)

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com