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

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

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

逐題詳解

115%

(15 points) Give asymptotically upper (big O) bounds for \(T(n)\) in each of the following recurrences, where we assume that \(T(n)\) is constant for small \(n\). Make your bounds as tight as possible, and justify your answers.

(a) (5 points) \(T(n)=2T(\sqrt n)+\lg n\)

(b) (5 points) \(T(n)=T(n/2+\sqrt n)+n\)

(c) (5 points) \(T(n)=T(n-1)+1/n\)

(a) 令 \(m=\lg n\)(\(n=2^m\)),\(S(m)=T(2^m)\);\(T(\sqrt n)=T(2^{m/2})=S(m/2)\)

\(S(m)=2S(m/2)+m\) → Master:\(a=b=2,\ f(m)=m=\Theta(m^{\log_22})\) → \(S(m)=\Theta(m\lg m)\)

代回 \(m=\lg n\):\(T(n)=\Theta(\lg n\cdot\lg\lg n)\)

\(O(\lg n\cdot\lg\lg n)\)

(b) \(n\ge9\) 時 \(\sqrt n\le n/3\) → \(\dfrac n2+\sqrt n\le\dfrac{5n}6\)

substitution:設 \(T(m)\le cm\)(\(m\lt n\)),\(T(n)\le c\cdot\dfrac{5n}6+n\le cn\) 當 \(c\ge6\)

下界 \(T(n)\ge n\)(每層至少加 \(n\))→ 緊

遞迴樹:只有一條鏈,每層的 cost 是 \(n,\ \le\tfrac56n,\ \le(\tfrac56)^2n,\ \ldots\),等比級數和 \(\le6n\)

\(O(n)\) (\(\Theta(n)\))

(c) 展開:\(T(n)=T(1)+\sum_{i=2}^{n}\dfrac1i=T(1)+H_n-1\)

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

\(O(\log n)\) (\(\Theta(\log n)\))

220%

(20 points) The binomial tree \(B_k\) is a rooted tree defined recursively. The binomial tree \(B_0\) consists of a single node. The binomial tree \(B_k\) consists of two binomial trees \(B_{k-1}\) that are linked together: the root of one is the leftmost child of the root of the other. Answer the following two questions. Each solution can use at most 200 Chinese characters or 200 English words. Over-sized description will be ignored.

(a) (10 points) For the binomial tree \(B_k\), how many nodes are there at depth \(i\) for \(i=0,1,\ldots,k\)? Justify your answer.

(b) (10 points) What is the maximum degree of any node in an \(n\)-node binomial tree? Justify your answer.

\(B_3\)(每個圓圈一個 node):依 depth 有 1、3、3、1 個

(a) 設 \(L(k,i)\) 為 \(B_k\) 在 depth \(i\) 的 node 數

\(B_k\) = 兩棵 \(B_{k-1}\),其中一棵掛在另一棵的 root 下 → 那一棵的每個 node 深度 \(+1\)

\(L(k,i)=L(k-1,i)+L(k-1,i-1)\),\(L(0,0)=1\),\(L(k,-1)=0\)

這就是 Pascal 恆等式,所以(對 \(k\) 歸納)\(L(k,i)=\dbinom ki\)

驗:\(\sum_i\binom ki=2^k\) 正好是 \(B_k\) 的 node 數

\(\dbinom ki\)

(b) \(n\)-node binomial tree = \(B_k\),\(n=2^k\)(\(k=\lg n\))

\(B_k\) 的 root 是由 \(B_{k-1}\) 的 root 再多一個 child 得到,degree \(=\deg(B_{k-1}\ \text{root})+1\),\(B_0\) 為 0 → root degree \(=k\)

root 的 children 分別是 \(B_{k-1},B_{k-2},\ldots,B_0\) 的 root,degree 是 \(k-1,\ldots,0\);子樹內的 node 同理都 \(\lt k\)

所以最大 degree 就是 root 的 \(k\)

\(k=\log_2n\)

315%

(15 points) We consider the following problems of undirected graphs.(原題附 Figure 1:三張無向圖 (a)(b)(c),(a) 五點 A–E 的圖、(b) 同圖加邊權、(c) 同圖用粗線標出一棵 spanning tree。)

Hamiltonian cycle problem is to determine whether a graph has a Hamiltonian cycle. A Hamiltonian cycle of a graph is a cycle that goes through every node of the graph exactly once. For example, A-B-D-E-C-A is a Hamiltonian cycle in Figure 1 (a).

Hamiltonian path problem is to determine whether a graph has a Hamiltonian path. A Hamiltonian path from a node \(a\) to a node \(b\) is a path from \(a\) to \(b\) that goes through all the other nodes of the graph exactly once. For example, E-D-C-A-B is a Hamiltonian path from E to B in Figure 1 (a). We will consider two flavors of the Hamiltonian path problem. For the given endpoint Hamiltonian path problem we will be given two nodes \(a\) and \(b\) and determine if there is a Hamiltonian path between them. For the any endpoint Hamiltonian path problem we will determine if there is a Hamiltonian path between any two nodes in the graph.

Traveling salesman problem is to determine whether a traveling salesman can go through every city exactly once and return to the starting city, and the total traveling distance is within a given bound, when given the positive integer distance between every two cities. For example, A-B-D-E-C-A is a solution for the graph in Figure 1 (b) with a total distance 12.

Bounded degree spanning tree problem is to determine if a graph has a bounded degree spanning tree of a given degree \(D\). A spanning tree of degree \(D\) is a spanning tree in which the degrees of all tree nodes are bounded by \(D\). A spanning tree is a tree that spans all nodes, i.e., all nodes are in the tree. For example, the thick edges in Figure 1 (c) is a spanning tree of bounded degree 3 because it covers all nodes and the maximum degree among all tree nodes is 3.

Please prove the following three statements. Your proof must be concise and address the key ideas only. Each proof can only use at most 100 Chinese characters or 100 English words. Over-sized proofs will be ignored.

(a) (5 points) If the given endpoint Hamiltonian path problem is NP-complete, then the Hamiltonian cycle problem is also NP-complete.

(b) (5 points) If the Hamiltonian cycle problem is NP-complete, then the traveling salesman problem is also NP-complete.

(c) (5 points) If the any endpoint Hamiltonian path problem is NP-complete, then the bounded degree spanning tree problem is also NP-complete.

三個問題都在 NP:給一個 cycle/tour/spanning tree,多項式時間可驗證。所以只要從已知 NP-complete 的問題 reduce 過來

(a) given endpoint HP \((G,a,b)\) ≤ HC:\(G'=G\) 加新點 \(c\)、邊 \(ca,cb\)

\(a\)→\(b\) 的 Hamiltonian path + \(b\!-\!c\!-\!a\) = \(G'\) 的 Hamiltonian cycle;反過來 \(c\) 的 degree 只有 2,cycle 一定走 \(ca,cb\),拿掉 \(c\) 得 \(a\)→\(b\) 的 Hamiltonian path

\(G\) 有 \(a\)→\(b\) 的 Hamiltonian path \(\iff G'\) 有 Hamiltonian cycle

(b) HC \(G=(V,E)\) ≤ TSP:建完全圖,\(uv\in E\) 距離 1,否則 2;bound \(=|V|\)

tour 剛好 \(|V|\) 條邊,總長 \(\le|V|\) ⟺ 每條邊都是 1 ⟺ 每條都在 \(E\) 裡 ⟺ \(G\) 有 Hamiltonian cycle

\(G\) 有 Hamiltonian cycle \(\iff\) TSP 有長度 \(\le|V|\) 的 tour

(c) any endpoint HP \(G\) ≤ bounded degree spanning tree:圖不變,\(D=2\)

degree \(\le2\) 的 spanning tree 是連通、無 cycle、每點 degree \(\le2\) → 一條走過所有點的 path;反過來 Hamiltonian path 本身就是這種 tree

\(G\) 有 Hamiltonian path \(\iff G\) 有 degree \(\le2\) 的 spanning tree

420%

(20 points) Find a shortest path in a DAG with given constraints. A graph is a DAG if it is directed and it has no cycle. Every edge has a positive integer distance, and is either thick or thin. Figure 2 illustrates such a DAG. For example, the edge from A to B is thick and with a distance 1, and the edge from A to C is thin and has a distance 2.

ABCDEF1239464105

Figure 2: A DAG(粗線=thick,這裡畫成紅線:A→B、B→E、C→E、D→E 為 thick,其餘為 thin)

Now given nodes A and F, and two integers \(k\), \(n\), find the shortest path from A to F that goes through exactly \(k\) thick edges and \(n\) thin edges, and does not go through any node twice. For example, the shortest distance from A to F that goes through two thick edges and two thin edges is 15 by going through A-B-C-E-F. The path A-C-E-F is not a valid solution because it only goes through one thick edge. The path A-B-D-E-F does go through the right number of edges, but its total distance is 25, which is not the minimum.

Please describe the key ideas in your algorithm without using pseudo code. Finally analyze the time complexity of your algorithm. We assume that there are \(N\) nodes and \(M\) edges in the DAG.

Your entire description and analysis can only use at most 400 Chinese characters or 400 English words. Over-sized description or analysis will be ignored. Pseudo code will also be ignored.

① 把狀態加上「已經用了幾條 thick、幾條 thin」:\(d[v][i][j]\) = 從 A 到 \(v\)、剛好用 \(i\) 條 thick 與 \(j\) 條 thin 的最短距離,\(d[A][0][0]=0\),其餘 \(\infty\)

② DAG 先做 topological sort;依序處理每個點 \(u\),對每條出邊 \((u,v)\)(距離 \(w\)):

thick:\(d[v][i+1][j]=\min\big(d[v][i+1][j],\ d[u][i][j]+w\big)\) thin:\(d[v][i][j+1]=\min\big(d[v][i][j+1],\ d[u][i][j]+w\big)\)

\(i\le k,\ j\le n\) 以外的狀態不用算

③ 答案 \(d[F][k][n]\)(\(\infty\) 表示沒有這種路徑)

為什麼不用管「不重複經過同一點」:DAG 沒有 cycle,沿 topological order 走的路徑本來就不會重複

為什麼可以只留最小:同一個 \((v,i,j)\) 之後能接的邊完全一樣,距離較大的永遠不會比較好(subproblem 最佳子結構)

例(\(k=n=2\)):每個點的 \((i,j)\!:\) 最短距離

點\((i,j)\):距離
A(0,0):0
B(1,0):1
C(0,1):2 (1,1):4
D(1,1):10
E(2,0):5 (1,1):8 (2,1):10
F(0,2):6 (1,2):8 (2,1):10 (2,2):15

\(d[F][2][2]=\min\big(d[E][2][1]+5\big)=15\)(路徑 A-B-C-E-F;A-B-D-E-F 的 \(d[E][2][1]\) 經 D 是 20,被 10 取代)

時間:狀態 \(N\cdot(k+1)(n+1)\) 個,每條邊對每組 \((i,j)\) 做 \(O(1)\) 更新 → topological sort \(O(N+M)\) + \(O(M\,kn)\);空間 \(O(N\,kn)\)

時間 \(O\big((N+M)\,kn\big)\),空間 \(O(Nkn)\)

530%

(30 points) Computational geometry has applications in a number of fields such as computer graphics, computer vision, robotics, molecular modeling and statistics. We consider the following problems of computational geometry in 2D space where the points are specified by their \(x\) and \(y\) coordinates, \(p_i=(x_i,y_i)\).

(a) (5 points) Give an algorithm to determine whether two line segments intersect. The inputs are segments \(\overline{p_1p_2}\) and \(\overline{p_3p_4}\), and the algorithm returns TRUE if the two segments intersect and FALSE if they do not.

(b) (10 points) Give an efficient algorithm to determine whether any two line segments in a set of segments intersect. Please describe the key ideas and discuss its time complexity.

(c) (15 points) Give an efficient algorithm to determine whether a given point is inside a given polygon. The vertices of the polygon are in counterclockwise order. We assume that the boundary of the polygon has no self-intersections, and that the given point is not on the polygon's boundary. Please describe the key ideas and discuss its time complexity.

Figure 3 illustrates a polygon. Point A is within the polygon and point B is outside of the polygon.

AB

Figure 3: A Polygon and two points

(a) orientation(cross product):\(\operatorname{orient}(a,b,c)=(b_x-a_x)(c_y-a_y)-(b_y-a_y)(c_x-a_x)\),\(\gt0\) 逆時針、\(\lt0\) 順時針、\(=0\) 共線

\(d_1=\operatorname{orient}(p_3,p_4,p_1)\),\(d_2=\operatorname{orient}(p_3,p_4,p_2)\),\(d_3=\operatorname{orient}(p_1,p_2,p_3)\),\(d_4=\operatorname{orient}(p_1,p_2,p_4)\)

① \(d_1,d_2\) 異號 且 \(d_3,d_4\) 異號(嚴格)→ 兩線段互相跨過 → TRUE

② 某個 \(d=0\)(例如 \(d_1=0\):\(p_1\) 與 \(p_3,p_4\) 共線)→ 看那個端點是否落在另一條線段的 bounding box 內(\(\min\le\cdot\le\max\)),是 → TRUE

③ 其他 → FALSE

只算 4 個 cross product 與幾次比較,\(O(1)\)

(b) sweep line(Shamos–Hoey):假設沒有垂直線段、沒有三條線段交於同一點

把 \(m\) 條線段的 \(2m\) 個端點依 \(x\) 排序(\(x\) 相同:左端點先、再依 \(y\));掃描線由左往右掃,維護「目前被掃描線碰到的線段」,依與掃描線的交點高度排成 balanced BST

遇到左端點:把線段插入 BST,只檢查它跟上方、下方兩個 neighbor 有沒有相交(用 (a))

遇到右端點:先檢查它的上、下 neighbor 彼此是否相交,再把它刪掉

任何一次檢查為 TRUE 就回傳 TRUE;掃完都沒有就 FALSE

為什麼只看 neighbor:若有交點,最左邊的交點的兩條線段在交點之前一定是上下相鄰,那時會被檢查到

排序 \(O(m\log m)\);\(2m\) 個事件每個做 \(O(\log m)\) 的 BST 操作與 \(O(1)\) 次檢查 → \(O(m\log m)\)(比兩兩比較的 \(O(m^2)\) 好)

(c) ray casting:從點 \(p=(x_p,y_p)\) 往右射水平射線,數它跟多邊形各邊的交點數,奇數 → 在內部,偶數 → 在外部

逐邊 \((v_i,v_{i+1})\) 檢查:\(y_p\) 落在兩端點 \(y\) 之間(半開區間,\(\min(y_i,y_{i+1})\le y_p\lt\max(y_i,y_{i+1})\),避免射線剛好穿過頂點時重複算,水平邊自動略過),且交點的 \(x\) 大於 \(x_p\)

交點 \(x=x_i+\dfrac{(y_p-y_i)(x_{i+1}-x_i)}{y_{i+1}-y_i}\)

圖 3:A 往右射,只穿過右下那條邊,1 個交點 → 內;B 往右射,先穿過左邊那條邊進入多邊形、再穿過右下那條邊出去,2 個交點 → 外

AB

另一種做法是 winding number(頂點已知逆時針,用角度或 cross product 累加);ray casting 不需要頂點順序,只要邊集合

逐邊掃一次,時間 \(O(m)\)(\(m\) 為頂點數),空間 \(O(1)\)

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com