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

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

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

逐題詳解

112%

1. (12 points) For each of the following algorithm, what is the tightest asymptotic upper bound for its worst-case running time? Use the following table for your answers. You need not justify your answers.

ABCDEFGH
\(O(\lg n)\)\(O(n)\)\(O(n\lg n)\)\(O(n^2)\)\(O(n^2\lg n)\)\(O(n^{2.5})\)\(O(n^{\lg7})\)\(O(n^3)\)

(a) Insertion sort for \(n\) numbers (b) Strassen's algorithm for multiplying two \(n\times n\) matrices (c) Graham scan for \(n\) points (d) BUILD-HEAP for \(n\) numbers (e) Merge sort for \(n\) numbers (f) Floyd-Warshall algorithm for a graph of \(n\) vertices

(a) worst case 是反向排序,\(1+2+\cdots+(n-1)\) 次移動 → \(O(n^2)\) → D

(b) \(T(n)=7T(n/2)+\Theta(n^2)\) → \(n^{\lg7}\approx n^{2.81}\) → G

(c) 先依極角排序 \(O(n\lg n)\),掃描 stack 每點進出一次 \(O(n)\) → C

(d) BUILD-HEAP:\(\sum_h\lceil n/2^{h+1}\rceil\cdot O(h)=O(n)\) → B

(e) \(T(n)=2T(n/2)+\Theta(n)\) → C

(f) 三重迴圈 \(k,i,j\) 各 \(n\) → H

(a) D (b) G (c) C (d) B (e) C (f) H

218%

2. (18 points) Give tight asymptotic bounds for the following recurrences. You need not justify your answers.

(a) (6 points) \(T(n)=\begin{cases}3T(n/8)+\sqrt n&\text{if }n\gt1\\1&\text{if }n=1,\end{cases}\)

(b) (6 points) \(T(n)=\begin{cases}T(\sqrt n)+1&\text{if }n\gt2\\1&\text{if }n\le2,\end{cases}\)

(c) (6 points) \(T(n)=\begin{cases}8T(n/2)+\Theta(1)&\text{if }n^2\gt M\\M&\text{if }n^2\le M,\end{cases}\) where \(M\) is a variable independent from \(n\).

(a) \(a=3,\ b=8\):\(n^{\log_83}=n^{0.528\ldots}\);\(f(n)=n^{1/2}=n^{0.5}\),\(0.5\lt\log_83\) → Master case 1

\(\Theta\big(n^{\log_83}\big)\)

(b) 展開 \(k\) 層:\(T(n)=T(n^{1/2^k})+k\);降到 \(n^{1/2^k}\le2\):\(2^k\ge\lg n\),\(k=\lg\lg n\)

令 \(m=\lg n\),\(S(m)=T(2^m)=S(m/2)+1\) → \(S(m)=\Theta(\lg m)\)

\(\Theta(\lg\lg n)\)

(c) 遞迴樹:每層 \(n\) 減半、node 數 \(\times8\);停在 \((n/2^h)^2\le M\),即 \(2^h\approx n/\sqrt M\)

leaf:\(8^h=(n/\sqrt M)^3\) 個,每個成本 \(M\) → \(M\cdot\dfrac{n^3}{M^{3/2}}=\dfrac{n^3}{\sqrt M}\)

內部 node:\(\sum_{i\lt h}8^i\cdot\Theta(1)=\Theta(8^h)=\Theta\big(n^3/M^{3/2}\big)\),比 leaf 的總和小 \(M\) 倍

\(\Theta\big(n^3/\sqrt M\big)\)

315%

3. (15 points) Various collision resolution strategies have been explored to tackle the problem of hash collisions. Given a hash table \(H\) with \(m\) slots that store \(n\) elements, please answer the following questions under the assumption of simple uniform hashing.

(a) (5 points) What is the expected time complexity of searching in \(H\) if

i. \(H\) resolves collisions by chaining and each chain is stored in an AVL tree.

ii. another hash table \(H'\) is used to hash all the elements with the same hash value in \(H\). Collisions in \(H'\) are resolved recursively. Assume that each \(H'\) also has \(m\) slots.

(b) (5 points) Determine \(m\) (in terms of \(n\)) such that the expected number of pairwise collisions can be reduced to \(O(1)\). Show your calculation.

(c) (5 points) Suppose \(H\) maps elements to values from 0 to 6, i.e., \(m=7\). Draw \(H\) that results from inserting 8, 22, 16, 28, 11 (in order) into an initially empty \(H\) by double hashing, where the primary hash function is defined as \(h_1(k)=k\bmod7\) and the secondary hash function is defined as \(h_2(k)=1+(k\bmod5)\).

(a) simple uniform hashing:每個 slot 的 chain 平均 \(\alpha=n/m\) 個元素

i. 算 hash 值 \(O(1)\),在 AVL tree 找 \(O(\log(\text{chain 長度}))\);chain 長度 \(X\) 的期望 \(\alpha\),\(\log\) 是凹函數(Jensen):\(E[\log(1+X)]\le\log(1+\alpha)\)

\(O\big(1+\log(1+n/m)\big)\) (\(n\gg m\) 時 \(O(\log(n/m))\))

ii. 每往下一層,同一個 slot 裡的元素期望數除以 \(m\):\(n\to n/m\to n/m^2\to\cdots\);降到 \(O(1)\) 個要 \(\log_mn\) 層,每層算一次 hash \(O(1)\)

\(O(1+\log_mn)\) (\(=O(\lg n/\lg m)\))

(b) 每一對元素 \((x,y)\) 碰撞(同 slot)的機率是 \(1/m\) → 令 \(X\) 為碰撞的對數

\(E[X]=\dbinom n2\cdot\dfrac1m=\dfrac{n(n-1)}{2m}\)

要 \(E[X]=O(1)\) → \(m=\Omega(n^2)\),取 \(m=\Theta(n^2)\)(例:\(m\ge n(n-1)/2\) 時 \(E[X]\le1\))

\(m=\Theta(n^2)\)

(c) probe:\(h(k,i)=(h_1(k)+i\cdot h_2(k))\bmod7\)

\(k\)\(h_1\)\(h_2\)probe放入
81\(1+3=4\)1 空slot 1
221\(1+2=3\)1 已有 8;\(1+3=4\) 空slot 4
162\(1+1=2\)2 空slot 2
280\(1+3=4\)0 空slot 0
114\(1+1=2\)4 已有 22;\(4+2=6\) 空slot 6
slot0123456
key288162211

\(H=[28,\ 8,\ 16,\ -,\ 22,\ -,\ 11]\)

415%

4. (15 points) Different tree-based data structures such as min-heap, binary search tree (BST) and red-black tree (RB-tree) satisfy different properties.

(a) (5 points) Give a recursive algorithm for determining whether a binary tree satisfies the BST properties. The input is the root of a binary tree. The algorithm returns TRUE if the binary tree is a BST and FALSE if it isn't. Your algorithm must use at most a constant amount of extra space and its running time must be \(O(n)\), where \(n\) denotes the number of the nodes in the binary tree.

(b) (5 points) Give a recursive algorithm for determining the number of different BSTs with \(n\) nodes, where \(n\) is the input.

(c) (5 points) Draw a smallest AVL tree of height 3 (number of edges from the root to the leaves) and then label its nodes with either B (black) or R (red) to make it satisfy the RB-tree properties.

(a) BST 的 in-order 走訪必須嚴格遞增(key 互異)。用一個全域變數 prev 記前一個走訪到的 key:

prev = -∞
IsBST(x):
    if x == NIL: return TRUE
    if not IsBST(x.left): return FALSE
    if x.key <= prev:   return FALSE      // 違反遞增
    prev = x.key
    return IsBST(x.right)

呼叫 IsBST(root)

每個 node 只進出一次,每次 \(O(1)\) → 時間 \(O(n)\);額外的資料只有一個變數 prev(不算遞迴本身的 call stack)

另一個寫法是遞迴傳入 key 的上下界:IsBST(x, lo, hi),左子用 (lo, x.key)、右子用 (x.key, hi),初始 (-∞, +∞),也是 \(O(n)\)、每層常數額外參數。若允許重複 key,把 <= 改成 <

(b) \(n\) 個 key 選第 \(i\) 小的當 root:左子樹 \(i-1\) 個、右子樹 \(n-i\) 個,兩邊獨立

Count(n):
    if n <= 1: return 1
    sum = 0
    for i = 1 to n:
        sum += Count(i-1) * Count(n-i)
    return sum

\(C(0)=C(1)=1\),\(C(n)=\sum_{i=1}^{n}C(i-1)\,C(n-i)\)(Catalan number,\(C(n)=\dfrac1{n+1}\dbinom{2n}{n}\))

不記憶的遞迴是指數時間;把 \(Count(k)\) 存起來,每個 \(k\) 只算一次,就是 \(O(n^2)\)。\(C(1..5)=1,2,5,14,42\)

(c) 高度 \(h\) 的最少 node 的 AVL tree:左右子樹高 \(h-1\) 與 \(h-2\):\(N(h)=1+N(h-1)+N(h-2)\),\(N(0)=1,\ N(1)=2\)

\(N(2)=1+2+1=4\),\(N(3)=1+4+2=7\)

建一棵:root 5,左子樹(高 2,4 個 node):3 帶左右子 2、4,2 再帶左子 1;右子樹(高 1,2 個 node):7 帶左子 6

塗色:每條 root 到 nil 的路徑都要有相同個數的 black(這裡每條都是 2 個),red 的 child 都是 black:

1234567

5(B)、3(R)、7(B)、2(B)、4(B)、6(R)、1(R);路徑檢查:5-3-2-1:black 是 5、2;5-3-2-nil:5、2;5-3-4:5、4;5-7-6:5、7;5-7-nil:5、7,都是 2

7 個 node;5(B),3(R)、7(B),2(B)、4(B)、6(R),1(R)

512%

5. (12 points) Given a weighted directed graph \(G_d=(V_d,E_d)\), as shown in Figure 1, where the weight of an edge \(w_{G_d}(e)\) is given by the number next to the edge, please determine the following:

(a) (6 points) Using at most 4 edges, what is the minimum cost path from vertex A to vertex H? Please put down the sequence of vertices in the path in the order of visiting. In the case that there are multiple such minimum cost paths, you only need to put down one.

(b) (6 points) Convert \(G_d=(V_d,E_d)\) to a weighted undirected graph \(G_u=(V_u,E_u)\) as follows. An edge \(e=(i,j)\in E_u\) iff \(\langle i,j\rangle\in E_d\) or \(\langle j,i\rangle\in E_d\). \(\forall e=(i,j)\in E_u\), \(w_{G_u}(e)=w_{G_d}(\langle i,j\rangle)+w_{G_d}(\langle j,i\rangle)\) if both \(\langle i,j\rangle\in E_d\) and \(\langle j,i\rangle\in E_d\). Otherwise, \(w_{G_u}(e)=w_{G_d}(\langle i,j\rangle)\) if \(\langle i,j\rangle\in E_d\), and \(w_{G_u}(e)=w_{G_d}(\langle j,i\rangle)\) if \(\langle j,i\rangle\in E_d\). Please give the cost of \(G_u\)'s minimum spanning tree(s).

ABCDEFGHI151515203720352331015310735211

Figure 1: Directed graph G

(a) 限制最多 4 條邊 → 依邊數做 DP(Bellman-Ford 跑 4 輪):\(d_k(v)\)=最多 \(k\) 條邊到 \(v\) 的最小成本

最多 \(k\) 條邊BDEFGH
\(k=1\)1520————
\(k=2\)151823—30—
\(k=3\)151821262631
\(k=4\)151821242427

\(H\) 只有 \(E,F,G\) 三個入邊:\(d_4(H)=\min\{d_3(G)+1,\ d_3(F)+3,\ d_3(E)+10\}=\min\{26+1,\ 26+3,\ 21+10\}=27\)

\(d_3(G)=26\) 來自 \(d_2(E)+3=23+3\),\(d_2(E)=23\) 來自 A→D(20)再 D→E(3)

不限邊數時最短是 25(A→B→D→E→G→H,5 條邊),題目限制最多 4 條邊;4 條邊的路徑有 A→D→E→G→H 27、A→B→D→G→H 29、A→D→E→F→H 29、A→B→D→E→H 31

ABCDEFGHI151515203720352331015310735211

A → D → E → G → H,成本 \(20+3+3+1=27\)

(b) \(G_u\):有雙向邊的兩邊相加,只有單向的取該邊權重:

ABBCADBDBECECFDE
1515\(15+20=35\)3\(7+20=27\)3\(5+2=7\)3
EFDGEGEHFHFIGHHI
3\(10+15=25\)3\(10+7=17\)3\(5+2=7\)11

Kruskal:GH 1、HI 1、BD 3、CE 3、DE 3、EF 3、EG 3 都不成環(FH 3 與 E、G 已相連,成環,略過);剩 A 還沒接上,最便宜的是 AB 15

\(1+1+3+3+3+3+3+15=32\) (8 條邊連 9 個點)

ABCDEFGHI1515353273733253173711

權重 3 的邊 FH 與 BD、CE、DE、EF、EG 有不同取法(例如 EG 換成 FH 也行),MST 不唯一,成本都是 32

MST 成本 \(32\)

610%

6. (10 points) Consider an ordinary binary min-heap data structure \(H\) with \(n\) elements supporting the instructions INSERT and EXTRACT-MIN in \(O(\log n)\) worst-case time. Give a potential function \(\Phi(H)\) such that the amortized cost of INSERT is \(O(\log n)\) and the amortized cost of EXTRACT-MIN is \(O(1)\). The potential function should be zero when the heap is empty, and has positive value otherwise. Show that it works for both instructions.

heap 是 complete binary tree,形狀只由 \(n\) 決定;第 \(j\) 個 node 的深度是 \(\lfloor\lg j\rfloor\)

令 \(\Phi(H)=k\sum_{j=1}^{n}\big(\lfloor\lg j\rfloor+1\big)\) (每個 node 的 \((\text{深度}+1)\) 的 \(k\) 倍;\(k\) 是下面決定的常數)

空 heap:\(\Phi=0\);\(n\ge1\):每一項 \(\ge1\),\(\Phi\gt0\) ✓ (\(\Phi\) 不會是負的,總攤還成本 ≥ 實際成本)

INSERT:放在最後一個位置(深度 \(d=\lfloor\lg(n+1)\rfloor\))再 sift-up 最多 \(d\) 層

實際成本 \(\le c_1(d+1)\);\(\Delta\Phi=k(d+1)\) → 攤還成本 \(\le(c_1+k)(d+1)=O(\log n)\)

EXTRACT-MIN:把最後一個 node(深度 \(d=\lfloor\lg n\rfloor\))換到 root,sift-down 最多 \(d\) 層

實際成本 \(\le c_2(d+1)+c_3\);\(\Delta\Phi=-k(d+1)\)

取 \(k=c_2\):攤還成本 \(\le c_2(d+1)+c_3-c_2(d+1)=c_3=O(1)\)

\(\Phi(H)=k\sum_{j=1}^{n}(\lfloor\lg j\rfloor+1)\),\(k\ge c_2\):INSERT 攤還 \(O(\log n)\),EXTRACT-MIN 攤還 \(O(1)\)

718%

7. (18 points) A palindrome is a string that reads the same from the left and from the right. For example, REDDER, I, ROTATOR, MOM, are palindromes. From a non-palindrome string, you can always remove an arbitrary number of characters from it to make it a palindrome. For example, from both ADAM and ADMA, you can remove 'M' to obtain a palindrome, ADA. In both cases, we say that ADA is the longest palindrome subsequence in the original strings, ADAM and ADMA.

We would like to derive a dynamic programming algorithm to determine the length of the longest palindrome subsequence in a string. You are given the string \(S\) of length \(n\) in the form of a character array \(S=s[\,]\), where \(s[i]\) is the \(i\)-th character in the string, \(1\le i\le n\).

(a) (12 points) As the first step, please complete the following equation to represent the length of the longest palindrome subsequence in the given string \(S\) by filling out the blanks. \(L(i,j)\) represents the length of the longest palindrome subsequence in \(s[i..j]\), a substring of \(S\) that starts from the \(i\)-th character and ends at the \(j\)-th character of \(S\). \(L(i,j)\) is defined for \(1\le i\le n\), \(1\le j\le n\), \(j\ge i-1\), and

\(L(i,j)=\begin{cases}\underline{\ \ (1)\ \ }&,\text{ if }i=j+1\\\underline{\ \ (2)\ \ }&,\text{ if }i=j\\\underline{\ \ (3)\ \ }&,\text{ if }i\lt j\text{ and }s[i]==s[j]\\\underline{\ \ (4)\ \ }&,\text{ otherwise.}\end{cases}\)

(b) (6 points) Calculate the length of the longest palindrome sequence in the string CABDAACBADFA.

(a) \(i=j+1\):空字串 → (1) \(0\)

\(i=j\):一個字元本身就是 palindrome → (2) \(1\)

\(s[i]=s[j]\):兩端接在 \(s[i+1..j-1]\) 的最長 palindrome 兩邊 → (3) \(L(i+1,j-1)+2\)

\(s[i]\ne s[j]\):兩端不可能同時用,至少丟掉一個 → (4) \(\max\{L(i+1,j),\ L(i,j-1)\}\)

(1) \(0\) (2) \(1\) (3) \(L(i+1,j-1)+2\) (4) \(\max\{L(i+1,j),L(i,j-1)\}\)

(b) \(n=12\),依 \(j-i\) 由小到大填 \(L(i,j)\):

1
C
2
A
3
B
4
D
5
A
6
A
7
C
8
B
9
A
10
D
11
F
12
A
1 C111133556667
2 A11133346667
3 B1112244555
4 D112223555
5 A12223335
6 A1113333
7 C111113
8 B11113
9 A1113
10 D111
11 F11
12 A1

\(L(1,12)\):\(s[1]=\)C,\(s[12]=\)A 不同 → \(\max\{L(2,12),L(1,11)\}\);\(L(2,12)\):\(s[2]=s[12]=\)A → \(L(3,11)+2=5+2=7\)

一個長度 7 的 palindrome subsequence:ADABADA(位置 2、4、5、8、9、10、12)

\(L(1,12)=7\)

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com