115 年 國立陽明交通大學資訊工程學系碩士班《資料結構與演算法》

📄 以下 3 題共用同一段題幹

以下題目共用同一張有向加權圖。圖中邊的方向與權重如圖所示。
🖼️【此處有附圖,請對照原卷】

第 1 題

Given the following graph where vertex SS is the source and the weight of each edge is specified alongside the edge, please apply the Bellman-Ford algorithm. During each pass/iteration of edge-by-edge relaxation, the order of edges to be processed is: AB, AD, BC, BF, CA, CD, CE, CF, DG, EG, FG, SA, SB (i.e., lexicographic order, assuming that an edge from vertex XX to vertex YY is named XYXY).

For this particular graph and based on the aforementioned order of edge relaxation, how many passes/iterations are required for the Bellman-Ford algorithm to converge (i.e., to correctly find shortest paths from SS to each of other vertices), and what is the shortest path distance from SS to GG? (NOTE: The Bellman-Ford algorithm requires PP passes to converge if, for every vertex, the (P+1)th(P+1)^{\text{th}} pass makes no improvement in shortest path distance.)

(A) 2 passes; shortest path distance from SS to GG = 16
(B) 2 passes; shortest path distance from SS to GG = 18
(C) 3 passes; shortest path distance from SS to GG = 16
(D) 3 passes; shortest path distance from SS to GG = 18
(E) 3 passes; shortest path distance from SS to GG = 19

🖼️ 本題附圖:
第 1 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Bellman–Ford 演算法以「鬆弛」更新最短距離。對有向邊 u→vu\to v,權重為 w(u,v)w(u,v),更新公式為:

d(v)←min⁡{d(v), d(u)+w(u,v)}.d(v)\leftarrow \min\{d(v),\,d(u)+w(u,v)\}.

初始設定 d(S)=0d(S)=0,其餘頂點的距離皆為 ∞\infty。本題採用逐邊立即更新:同一輪前面更新的距離,後面處理邊時就可以使用,因此邊的處理順序會影響收斂輪數。

題目定義:若第 P+1P+1 輪所有距離皆無改善,則收斂所需輪數為 PP,不把最後的確認輪計入。

解題方法

原圖共有 S,A,B,C,D,E,F,GS,A,B,C,D,E,F,G 八個頂點;所有邊皆為有向邊,其中斜向連接 CC 與 AA 的邊是 C→AC\to A,權重為 33。依指定處理順序,各邊權重如下:

邊ABABADADBCBCBFBFCACACDCDCECECFCFDGDGEGEGFGFGSASASBSB
權重441111881111332255112211993366

以下距離向量皆依 (S,A,B,C,D,E,F,G)(S,A,B,C,D,E,F,G) 排列。

第 1 輪

初始距離為:

(0,∞,∞,∞,∞,∞,∞,∞).(0,\infty,\infty,\infty,\infty,\infty,\infty,\infty).

由於 SASA、SBSB 排在最後,處理前十一條邊時,其起點距離皆為 ∞\infty,不會產生更新。最後兩條邊得到:

d(A)=0+3=3,d(B)=0+6=6.d(A)=0+3=3,\qquad d(B)=0+6=6.

因此第 1 輪結束時為:

(0,3,6,∞,∞,∞,∞,∞).(0,3,6,\infty,\infty,\infty,\infty,\infty).

第 2 輪

依指定順序逐邊鬆弛:

處理邊更新計算結果
ABABd(B)←min⁡(6,3+4)d(B)\leftarrow\min(6,3+4)d(B)=6d(B)=6,不變
ADADd(D)←min⁡(∞,3+11)d(D)\leftarrow\min(\infty,3+11)d(D)=14d(D)=14
BCBCd(C)←min⁡(∞,6+8)d(C)\leftarrow\min(\infty,6+8)d(C)=14d(C)=14
BFBFd(F)←min⁡(∞,6+11)d(F)\leftarrow\min(\infty,6+11)d(F)=17d(F)=17
CACAd(A)←min⁡(3,14+3)d(A)\leftarrow\min(3,14+3)d(A)=3d(A)=3,不變
CDCDd(D)←min⁡(14,14+2)d(D)\leftarrow\min(14,14+2)d(D)=14d(D)=14,不變
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題

Consider the same graph as in Question 7. If you can determine the order of edge relaxation, what are the minimum and maximum numbers of passes for the Bellman-Ford algorithm to converge (i.e., to correctly find shortest paths from SS to each of other vertices)?

(A) minimum: 1; maximum: N−1N-1 (where NN is the number of vertices in the graph)
(B) minimum: 1; maximum: NN
(C) minimum: 2; maximum: N−1N-1
(D) minimum: 2; maximum: NN
(E) minimum: 3; maximum: N−1N-1

🖼️ 本題附圖:
第 2 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Bellman–Ford 每一輪依指定順序掃描所有邊,並以

d(v)←min⁡{d(v), d(u)+w(u,v)}d(v)\leftarrow\min\{d(v),\,d(u)+w(u,v)\}

更新距離。掃描時若前面的邊已更新某個頂點,後面的邊可以在同一輪接著使用新距離,因此一輪可能沿著多條邊傳遞最短距離。若從 SS 到某頂點的最短路徑有 kk 條邊,掃描 kk 輪後必能找到該距離;實際輪數也會受到邊的處理順序影響。

解題方法

圖中有 88 個頂點 S,A,B,C,D,E,F,GS,A,B,C,D,E,F,G,邊為 SA,SB,AB,AD,BC,BF,CA,CD,CE,CF,DG,EG,FGSA,SB,AB,AD,BC,BF,CA,CD,CE,CF,DG,EG,FG。依圖讀得 S→AS\to A 權重 33、S→BS\to B 權重 66、B→CB\to C 權重 88、C→FC\to F 權重 11、A→DA\to D 權重 1111、D→GD\to G 權重 22,其餘邊權重如圖。

選擇合適的鬆弛順序,可以在一輪內得到全部正確距離。例如依序處理 SA,SB,BC,CA,AD,CD,CE,CF,DG,EG,FG,BF,ABSA,SB,BC,CA,AD,CD,CE,CF,DG,EG,FG,BF,AB,會得到

d(A)=3,d(B)=6,d(C)=14,d(D)=14,d(E)=19,d(F)=15,d(G)=16.d(A)=3,\quad d(B)=6,\quad d(C)=14,\quad d(D)=14,\quad d(E)=19,\quad d(F)=15,\quad d(G)=16.

因此最少只需 11 輪。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題

Consider the same graph as in Question 7. Which one of the following orders of edge relaxation results in the minimum number of passes for the Bellman-Ford algorithm to converge?

(A) AB, AD, BC, BF, CA, CD, CE, CF, DG, EG, FG, SA, SB (same as in Question 7, i.e., lexicographic order)

(B) AB, AD, BC, BF, CA, CD, CE, CF, DG, EG, FG, SB, SA (reverse the last 2 edges, SA and SB, from (A))

(C) FG, EG, DG, CF, CE, CD, CA, BF, BC, AD, AB, SB, SA (reverse the first 11 edges, from (B))

(D) SB, SA, FG, EG, DG, CF, CE, CD, CA, BF, BC, AD, AB (move the last 2 edges to the top, from (C))

(E) SB, SA, AB, AD, BC, BF, CA, CD, CE, CF, DG, EG, FG (reverse the last 11 edges, from (D))

🖼️ 本題附圖:
第 3 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Bellman–Ford 對有向邊 u→vu\to v 的鬆弛規則為:

d(v)←min⁡{d(v), d(u)+w(u,v)}.d(v)\leftarrow \min\{d(v),\,d(u)+w(u,v)\}.

初始化為 d(S)=0d(S)=0,其餘頂點距離皆為 ∞\infty。每一輪依指定順序處理全部邊,且本輪更新的距離可立即供後續邊使用,因此邊的排列順序會影響收斂輪數。

依原卷前題的定義,若第 PP 輪後所有距離已正確,而第 P+1P+1 輪沒有任何改善,則收斂輪數為 PP;確認沒有改善的那一輪不計入。

解題方法

圖中共有 S,A,B,C,D,E,F,GS,A,B,C,D,E,F,G 八個頂點,起點為 SS。依圖上箭頭讀得 C→AC\to A,權重為 33;其餘邊與權重如下:

有向邊權重有向邊權重
S→AS\to A33S→BS\to B66
A→BA\to B44A→DA\to D1111
B→CB\to C88B→FB\to F1111
C→AC\to A33C→DC\to D22
C→EC\to E55C→FC\to F11
D→GD\to G22E→GE\to G11
F→GF\to G99

先求出正確的最短距離,作為各順序的收斂判準:

d(A)=3,d(B)=min⁡{6,3+4}=6,d(C)=6+8=14,d(D)=min⁡{3+11,14+2}=14,d(E)=14+5=19,d(F)=min⁡{6+11,14+1}=15,d(G)=min⁡{14+2,19+1,15+9}=16.\begin{aligned} d(A)&=3,\\ d(B)&=\min\{6,3+4\}=6,\\ d(C)&=6+8=14,\\ d(D)&=\min\{3+11,14+2\}=14,\\ d(E)&=14+5=19,\\ d(F)&=\min\{6+11,14+1\}=15,\\ d(G)&=\min\{14+2,19+1,15+9\}=16. \end{aligned}

其中 GG 的最短路徑為 S→A→D→GS\to A\to D\to G,總權重為 3+11+2=163+11+2=16。

依各選項順序逐輪鬆弛,可得下表;距離向量依序為 (d(A),d(B),d(C),d(D),d(E),d(F),d(G))(d(A),d(B),d(C),d(D),d(E),d(F),d(G)),並省略始終為 00 的 d(S)d(S)。

選項第 11 輪結束第 22 輪結束第 33 輪結束
(A)(3,6,∞,∞,∞,∞,∞)(3,6,\infty,\infty,\infty,\infty,\infty)(3,6,14,14,19,15,16)(3,6,14,14,19,15,16)無改善
(B)(3,6,∞,∞,∞,∞,∞)(3,6,\infty,\infty,\infty,\infty,\infty)(3,6,14,14,19,15,16)(3,6,14,14,19,15,16)無改善
(C)(3,6,∞,∞,∞,∞,∞)(3,6,\infty,\infty,\infty,\infty,\infty)(3,6,14,14,∞,17,∞)(3,6,14,14,\infty,17,\infty)(3,6,14,14,19,15,16)(3,6,14,14,19,15,16)
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

以下題目共用同一個 4-bit ripple carry adder 邏輯網路。A4A3A2A1A_4A_3A_2A_1 與 B4B3B2B1B_4B_3B_2B_1 是兩個 4-bit 輸入運算元,CinC_{in} 是 carry-in(輸入),S4S3S2S1S_4S_3S_2S_1 是 4-bit sum(輸出),而 CoutC_{out} 是 carry-out(輸出)。
🖼️【此處有附圖,請對照原卷】

第 4 題

What is the number of distinct paths in this logic network? (HINT: There are 3 distinct paths to output S1S_1, and you have to consider all of the outputs.)
(A) 78
(B) 103
(C) 118
(D) 143
(E) 158

🖼️ 本題附圖:
第 4 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

路徑是從任一輸入,沿著導線與邏輯閘到達某一輸出的連續路徑。若訊號進入同一個閘的不同輸入端,或分別經過不同邏輯閘,便是不同路徑;因此要把訊號分流到不同閘的路徑分別計數。

解題方法

圖中每一位的進位邏輯由三個 AND 閘接到一個 OR 閘,分別形成 AiBiA_iB_i、AiCi−1A_iC_{i-1}、BiCi−1B_iC_{i-1};加總輸出 SiS_i 則由一個三輸入 XOR 閘產生。第一位的進位輸入 C0=CinC_0=C_{in} 是一個輸入訊號。

令 CiC_i 表示從所有輸入到第 ii 位進位輸出 CiC_i 的路徑數。第一位的三個 AND 閘各接兩個輸入,所以:

C1=2+2+2=6C_1=2+2+2=6

對後續各位,AiBiA_iB_i 的 AND 閘有 2 條輸入路徑;AiCi−1A_iC_{i-1} 與 BiCi−1B_iC_{i-1} 兩個 AND 閘,各有 1 條來自新輸入的路徑,以及 Ci−1C_{i-1} 的 Ci−1C_{i-1} 條路徑。因此:

Ci=2+(1+Ci−1)+(1+Ci−1)=4+2Ci−1C_i=2+(1+C_{i-1})+(1+C_{i-1})=4+2C_{i-1}

逐位計算:

C1=6,C2=16,C3=36,C4=76C_1=6,\qquad C_2=16,\qquad C_3=36,\qquad C_4=76
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題

Consider the same logic network as in Question 10. Among all of the possible paths (from any input, to any output), the one passing through the largest number of gates (vertices) is defined as the critical path. In this particular logic network, how many gates does the critical path pass through?

(A) 4
(B) 6
(C) 8
(D) 10
(E) 12

🖼️ 本題附圖:
第 5 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

臨界路徑是從任一輸入到任一輸出的路徑中,經過最多個邏輯閘的路徑;只計算沿途經過的閘數,不計導線或輸入、輸出端點。

解題方法

圖中是 4-bit ripple carry adder:每一位的進位由三個 AND 閘與一個 OR 閘組成,並傳給下一位;各位的和由 XOR 閘組成。沿著進位鏈走到最高位的 CoutC_{out},會得到最長路徑。

從 CinC_{in} 到 CoutC_{out},第一位進位經過一個 AND 閘與一個 OR 閘,共 22 個閘;之後第 2、3、4 位各再經過一個 AND 閘與一個 OR 閘,共 66 個閘。因此:

2+3×2=82+3\times 2=8

其他輸入到輸出的路徑至多經過這段進位鏈,再接上和位所需的 XOR 閘,不會超過 8 個閘。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6 題

Continue the previous question. What is the time complexity of the best/fastest algorithm for solving Question 11?

(A) O(M+N)O(M+N), where MM and NN are the numbers of edges (wires) and vertices (gates)
(B) O(Mlg⁡N)O(M\lg N)
(C) O(Mlg⁡M)O(M\lg M)
(D) O(MN)O(MN)
(E) O(M2)O(M^2)

🖼️ 本題附圖:
第 6 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題承接前一題的「關鍵路徑」:從任一輸入到任一輸出,經過最多邏輯閘的路徑。將邏輯閘視為頂點、訊號線視為有向邊,組合邏輯電路沒有回授迴圈,因此形成有向無環圖(DAG)。

一般圖的最長路徑問題很困難,但 DAG 可利用拓樸排序與動態規劃,在線性時間內求得最長路徑。

解題方法

圖中有四級加法電路,進位訊號依序由低位元傳往高位元,各級也分支產生 S1,S2,S3,S4S_1,S_2,S_3,S_4,最後產生 CoutC_{out};訊號連線沒有形成有向環。原卷第 6 題寫的是求解「Question 11」的時間複雜度,但本頁前一題實際標為第 5 題,題號引用不一致。

依「承接前一題,求關鍵路徑」的合理解讀,演算法如下:

  1. 建立圖並進行拓樸排序。
    使用鄰接串列記錄連線,計算各頂點的入度,依拓樸順序處理邏輯閘,確保處理某個閘時,它的所有前驅都已處理完畢。

  2. 計算抵達每個邏輯閘的最長路徑。
    設 d(v)d(v) 為從任一輸入到邏輯閘 vv,最多經過的邏輯閘數。外部輸入的路徑長度設為 00,每經過一個邏輯閘增加 11,因此:

    d(v)=1+max⁡u→vd(u).d(v)=1+\max_{u\to v}d(u).

    若 vv 直接連接外部輸入,該輸入在上式中的值就是 00。

  3. 取所有輸出端的最大值。
    比較通往 S1,S2,S3,S4S_1,S_2,S_3,S_4 與 CoutC_{out} 的最長路徑,最大值就是關鍵路徑經過的邏輯閘數。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7 題

Which one of the following 2-SAT problems (in conjunctive normal form, CNF) is satisfiable?

(A) F=(a+b)(a+¬b)(¬a+b)(¬a+¬b)F=(a+b)(a+\neg b)(\neg a+b)(\neg a+\neg b)
(B) F=(a+b)(a+¬b)(¬a+c)(¬a+¬c)F=(a+b)(a+\neg b)(\neg a+c)(\neg a+\neg c)
(C) F=(a+b)(¬a+b)(¬b+c)(¬b+d)(¬c+¬d)F=(a+b)(\neg a+b)(\neg b+c)(\neg b+d)(\neg c+\neg d)
(D) F=(a+b)(¬a+b)(¬b+¬c)(¬b+¬d)(c+d)F=(a+b)(\neg a+b)(\neg b+\neg c)(\neg b+\neg d)(c+d)
(E) F=(a+b)(¬a+b)(¬b+¬c)(¬b+¬d)(c+¬d)F=(a+b)(\neg a+b)(\neg b+\neg c)(\neg b+\neg d)(c+\neg d)

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

2-SAT 的每個子句是兩個文字的析取,例如 (x+y)(x+y) 表示 x∨yx\lor y。整個公式以括號相乘表示合取,因此每個子句都必須為真,公式才可滿足。

判斷時可利用:若某個文字已為真,子句 (¬x+y)(\neg x+y) 會迫使 yy 為真;若 xx 已為真,子句 (¬x+y)(\neg x+y) 則迫使 yy 為真。若推導出互相矛盾的要求,該公式便不可滿足。

解題方法

先找能迫使變數取值的子句,再將推得的值代入其餘子句。特別地,(a+b)(a+¬b)(a+b)(a+\neg b) 會迫使 aa 為真:若 aa 為假,兩個子句分別要求 bb 與 ¬b\neg b 為真,造成矛盾。同理,(¬a+b)(¬a+¬b)(\neg a+b)(\neg a+\neg b) 會迫使 ¬a\neg a 為真。

選項分析

  • (A) 不可滿足。 前兩個子句 (a+b)(a+¬b)(a+b)(a+\neg b) 迫使 aa 為真;後兩個子句 (¬a+b)(¬a+¬b)(\neg a+b)(\neg a+\neg b) 迫使 aa 為假,互相矛盾。

  • (B) 不可滿足。 (a+b)(a+¬b)(a+b)(a+\neg b) 迫使 aa 為真;(¬a+c)(¬a+¬c)(\neg a+c)(\neg a+\neg c) 迫使 aa 為假,因此無法同時滿足。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 8 題

Which one of the following statements about the implication graph for 2-SAT is true?

CNF: F=(a+b)(¬a+b)(¬b+c)(¬b+d)(¬c+¬d)F=(a+b)(\neg a+b)(\neg b+c)(\neg b+d)(\neg c+\neg d)

(A) The implication graph for a 2-SAT CNF is an undirected graph.
(B) There are 8 vertices and 12 edges in FF’s implication graph.
(C) FF’s implication graph is connected.
(D) There are 2 strongly connected components (SCCs) in FF’s implication graph.
(E) Finding SCC(s) in FF’s implication graph takes O(MN)O(MN) time where MM and NN are the numbers of edges and vertices in the graph.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

對每個子句 (x+y)(x+y),2-SAT 的蘊含圖會加入兩條有向邊:

¬x→y,¬y→x\neg x\to y,\qquad \neg y\to x

圖中的頂點是每個變數及其否定文字。本題有 a,b,c,da,b,c,d 四個變數,因此共有 88 個頂點。

解題方法

依序將每個子句轉成兩條有向邊:

子句對應的有向邊
(a+b)(a+b)¬a→b\neg a\to b、¬b→a\neg b\to a
(¬a+b)(\neg a+b)a→ba\to b、¬b→¬a\neg b\to\neg a
(¬b+c)(\neg b+c)b→cb\to c、¬c→¬b\neg c\to\neg b
(¬b+d)(\neg b+d)b→db\to d、¬d→¬b\neg d\to\neg b
(¬c+¬d)(\neg c+\neg d)c→¬dc\to\neg d、d→¬cd\to\neg c

因此圖有 88 個頂點、1010 條邊。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 9 題8 分

Three students tried to build red-black trees from the following numbers: 10, 20, 30, 15, 25, 35, 45, 12, 22, 32. They ended up with three different trees (for clarity, the external nodes are not shown). The diagrams identify black nodes and red nodes as shown in the legend.
🖼️【此處有附圖,見下方】

(A) For each of these trees, indicate whether it is a valid red-black tree. If your answer is NO for any tree, you need to state the reason why it is invalid to get the credit.

(B) The depth ratio between two subtrees of the same parent in a red-black tree is no more than ____ (give the closest integer).

🖼️ 本題附圖:
第 9 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

讀圖(實心黑底為黑色節點、白底為紅色節點)

  • Tree A:根 20(黑);左子 15(紅)、右子 30(紅)。15 只有左子 10(黑),10 的右子 12(紅)。30 的子節點 25(黑,左子 22 紅)、35(黑,子節點 32 紅、45 紅)。
  • Tree B:根 20(黑);左子 12(黑,子節點 10 紅、15 紅)、右子 30(紅)。30 的子節點 25(黑,子節點 22 紅、32 紅)、35(黑,右子 45 紅)。
  • Tree C:根 22(黑);左子 15(紅,子節點 10 黑、20 黑;10 的右子 12 紅)、右子 30(紅,子節點 25 黑、35 黑;35 的子節點 32 紅、45 紅)。

核心觀念

合法的紅黑樹必須同時是二元搜尋樹,並滿足:(1) 根為黑;(2) 紅節點的子節點必為黑;(3) 從任一節點到其下所有 NIL 的路徑,黑節點數(黑高)相同。

(A) 解題

  • Tree A:不合法(黑高不一致)。 紅節點 15 的右邊是 NIL:路徑 20→15→NIL20\to15\to\text{NIL} 只經過 1 個黑節點(20);但 20→15→10→NIL20\to15\to10\to\text{NIL} 經過 2 個黑節點(20、10)。同一個節點到不同 NIL 的黑節點數不同,違反性質 (3)。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 5 題共用同一段題幹

以下各小題共用題目所示的有向圖,頂點內的數字為頂點索引。
🖼️【此處有附圖,請對照原卷】

第 10-(a) 題2 分

Give the count of "1" elements in the transitive closure of this graph.

🖼️ 本題附圖:
第 10-(a) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

讀圖

有向圖共 10 個頂點(0~9),邊為:
2→82\to8、8→18\to1、1→01\to0、2→42\to4、2→32\to3、4→34\to3、3→03\to0、2→52\to5、0→70\to7、0→60\to6、6→56\to5、5→95\to9。

核心觀念

遞移閉包(transitive closure)矩陣 A+A^+ 中,A+[i][j]=1A^+[i][j]=1 表示存在一條長度至少 1 的路徑從 ii 到 jj。題目問矩陣中 1 的個數,就是「所有可達的 (i,j)(i,j) 配對數」。本圖沒有環,所以對角線全為 0(頂點不能經由長度 ≥1\ge1 的路徑回到自己)。

解題方法

由沒有出邊的頂點往上推每個頂點的可達集合:

頂點可達集合個數
9∅0
7∅0
5{9}1
6{5, 9}2
0{7, 6, 5, 9}4
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 10-(b) 題2 分

Give a topological order of the vertices in the graph. (The answer is not unique; you only need to give a valid one.)

🖼️ 本題附圖:
第 10-(b) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

拓樸排序是將有向圖的所有頂點排成線性順序,使每條有向邊 u→vu\to v 都滿足:uu 排在 vv 前面。有向無環圖才存在拓樸排序,且答案不一定唯一。

本題採用 Kahn 演算法:每次選取目前入度為 00 的頂點,加入排序並刪除其出邊,接著更新其餘頂點的入度。

解題方法

圖中共有 00 至 99 共十個頂點;其中箭頭方向為 2→42\to4,而 22 也有邊指向 88、33、55。依圖中箭頭,各頂點的出邊如下:

頂點出邊指向的頂點
224,8,3,54,8,3,5
4433
8811
1100
3300
007,67,6
6655
5599
7,97,9無

依序選取入度為 00 的頂點:

  1. 先選 22:初始只有 22 的入度為 00。刪除它的出邊後,44、88 的入度變成 00;33 仍有來自 44 的邊,55 仍有來自 66 的邊。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 10-(c) 題2 分

Assume now that the direction of edge 2→52\to5 is flipped to 5→25\to2. What happens to the topological order? If there still exists a topological order, give one. If no topological order exists now, explain why.

🖼️ 本題附圖:
第 10-(c) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

有向圖存在拓樸排序,若且唯若圖中沒有有向環。若沿著有向邊能從某頂點回到該頂點,就形成有向環,因而不存在拓樸排序。

解題方法

依圖中箭頭,原圖有路徑 2→3→0→6→52\to3\to0\to6\to5。將邊 2→52\to5 反向後,新增的邊是 5→25\to2,因此形成有向環:

5→2→3→0→6→55\to2\to3\to0\to6\to5
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 10-(d) 題2 分

Starting from the original graph, if one more edge 1→31\to3 is added, what happens to the topological order? If there still exists a topological order, give one. If no topological order exists now, explain why.

🖼️ 本題附圖:
第 10-(d) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

拓樸排序是將有向圖的所有頂點排成一個序列,使每條邊 u→vu\to v 都滿足「uu 排在 vv 前面」。有向圖存在拓樸排序,若且唯若它沒有有向環。

在原本無環的圖中新增 1→31\to3,只有當原圖已存在從 33 到 11 的有向路徑時,才會形成有向環。

解題方法

依圖中的箭頭,原圖包含 2→4→3→02\to4\to3\to0、2→8→1→02\to8\to1\to0 與 0→6→5→90\to6\to5\to9 這三條路徑,另有 2→32\to3、2→52\to5、0→70\to7。本小題從原圖出發,僅新增 1→31\to3,因此 2→52\to5 的方向維持不變。

由頂點 33 出發,只能沿箭頭到達 0,7,6,5,90,7,6,5,9,無法到達 11。所以新增 1→31\to3 不會形成有向環,仍然存在拓樸排序;新增的限制是 11 必須排在 33 前面。

使用「每次取出入度為 00 的頂點」的方法,可依序排列:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 10-(e) 題2 分

Consider the following statement: "If a suitable starting vertex is selected, one can obtain the topological order of a directed graph using breadth-first search (BFS) if one exists." Indicate whether the statement is TRUE or FALSE. If your answer is TRUE, indicate how you will select the suitable starting vertex. If you answer FALSE, describe one condition that makes BFS fail this task.

🖼️ 本題附圖:
第 10-(e) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

拓樸排序要求每條有向邊 u→vu\to v 都符合「uu 排在 vv 前面」。一般 BFS 依照距離逐層拜訪頂點,並不會檢查某個頂點的所有前置頂點是否都已排在它之前。

解題方法

附圖是頂點編號為 00 至 99 的有向圖;本小題詢問的是 BFS 能否一般性地產生拓樸順序,不要求替附圖列出拓樸排序。考慮以下有向無環圖:邊為 s→as\to a、s→bs\to b、b→xb\to x、x→ax\to a,其中 ss 是唯一的起點。

從 ss 執行 BFS 時,aa 和 bb 都在第一層,xx 在第二層,因此 BFS 會先拜訪 aa,再拜訪 xx。但圖中有邊 x→ax\to a,拓樸排序必須讓 xx 在 aa 前面。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

以下各小題共用題目所示的無向加權圖;頂點內的數字為頂點索引,邊旁的數字為邊權重。
🖼️【此處有附圖,請對照原卷】

第 11-(a) 題2 分

Assume that you're using Kruskal's algorithm to build the minimum spanning tree (MST). Indicate the edges selected in the second and the fifth steps. You get credits only if both answers are correct.

🖼️ 本題附圖:
第 11-(a) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Kruskal 演算法將所有邊依權重由小到大檢查;只有在加入某邊不會形成環時,才將它選入最小生成樹(MST)。

解題方法

圖中是頂點 00 至 99 的無向加權圖。由小到大檢查相關邊:55–99 的權重為 11、00–33 為 22、22–33 為 33、00–55 為 44、00–66 為 55。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 11-(b) 題2 分

Assume that you're using Prim's algorithm to build the minimum spanning tree. Indicate the edges selected in the second and the fifth steps. You get credits only if both answers are correct.

🖼️ 本題附圖:
第 11-(b) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Prim 演算法從指定起始頂點出發,逐步擴張最小生成樹。設 SS 為已加入樹中的頂點集合,每一步都選擇「一端在 SS、另一端在 SS 外」的最小權重邊,並加入該邊連接的新頂點。

原題未指定起始頂點,因此選邊順序沒有唯一答案;以下採用從頂點 00 起始,並將加入第一條邊計為第一步。

解題方法

依圖,頂點 00 連到 3、5、6、7、13、5、6、7、1 的邊權重依序為 2、4、5、8、132、4、5、8、13;另有 w(2,3)=3w(2,3)=3、w(5,9)=1w(5,9)=1 及 w(3,4)=6w(3,4)=6,這些邊會影響前五步的選擇。

初始 S={0}S=\{0\},依序執行:

步驟選取的邊權重加入後的頂點集合 SS
第一步(0,3)(0,3)22{0,3}\{0,3\}
第二步(2,3)(2,3)33{0,2,3}\{0,2,3\}
第三步(0,5)(0,5)44{0,2,3,5}\{0,2,3,5\}
第四步(5,9)(5,9)11{0,2,3,5,9}\{0,2,3,5,9\}
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 11-(c) 題3 分

You get credits only if your answers to the following three questions are all correct.

(1) Is the MST of this graph unique? Answer YES or NO.

(2) One can apply Dijkstra's algorithm to an undirected weighted graph from any vertex to obtain a spanning tree. Is this tree always an MST? Answer YES or NO.

(3) Give the number of edges in the MSTs obtained in (a) and (b).

🖼️ 本題附圖:
第 11-(c) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

最小生成樹(MST)是在連通無向加權圖中,連接所有頂點且總權重最小的生成樹。若圖中所有邊的權重互不相同,MST 唯一。Dijkstra 演算法則是從指定起點找出各頂點的最短路徑;它產生的最短路徑樹,目標不等同於總邊權重最小的生成樹。

解題方法

圖中有 1010 個頂點,邊權重由 11 到 1414,各不相同。為判斷 Dijkstra 的最短路徑樹是否必為 MST,可用頂點 11 作起點,比較該樹與 Kruskal 算法得到的 MST。

Kruskal 依權重由小到大選取不形成環的邊,得到權重為 1,2,3,4,5,6,7,8,101,2,3,4,5,6,7,8,10 的 99 條邊,總權重為

1+2+3+4+5+6+7+8+10=46.1+2+3+4+5+6+7+8+10=46.

從頂點 11 執行 Dijkstra,最短路徑樹的邊為 1 ⁣− ⁣81\!-\!8(7)、1 ⁣− ⁣01\!-\!0(13)、8 ⁣− ⁣28\!-\!2(10)、0 ⁣− ⁣30\!-\!3(2)、0 ⁣− ⁣50\!-\!5(4)、0 ⁣− ⁣70\!-\!7(8)、0 ⁣− ⁣60\!-\!6(5)、

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 1 題共用同一段題幹

以下為 binary search 程式。程式內含有 ONE BUG。
🖼️【此處有附圖,請對照原卷】

第 12 題5 分

Binary Search

There is ONE BUG in the following binary search program. Please provide:

  • The line number where the bug occurs, and
  • The corrected solution in C.
🖼️ 本題附圖:
第 12 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

二分搜尋適用於已排序的陣列。每次比較中間元素與目標值,排除不可能包含目標值的一半,因此時間複雜度為 O(log⁡n)O(\log n),額外空間複雜度為 O(1)O(1)。

解題方法

圖中程式使用升冪排列的陣列,搜尋範圍由 left 和 right 表示,中間索引為 mid = left + (right - left) / 2。若 arr[mid] 小於目標值,目標只可能在右半部,應令 left = mid + 1;若大於目標值,目標只可能在左半部,應令 right = mid - 1。

第 13 行的條件方向與第 14、17 行的範圍更新不一致。將條件改為 arr[mid] > target,即可使兩個分支符合二分搜尋的規則。

修正程式

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 2 題共用同一段題幹

以下為 doubly linked list 程式。程式內含兩個錯誤。
🖼️【此處有附圖,請對照原卷】

第 13-(1) 題5 分

There are TWO BUGS in the following doubly linked list program. For each bug, please provide:

  • The line number where the bug occurs, and
  • The corrected solution in C.
🖼️ 本題附圖:
第 13-(1) 題附圖第 13-(1) 題附圖第 13-(1) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

雙向鏈結串列的每個節點都有 prev 與 next 兩個指標。插入節點時,前後兩個方向都要接好;刪除節點時,必須先更新相鄰節點的指標,再釋放節點記憶體。

解題方法

原卷印刷內容顯示,struct Node 包含 data、prev、next;insertEnd 走訪到尾端後只設定新節點的前向連結,deleteNode 則在更新串列連結前就呼叫 free(temp)。依照雙向鏈結串列的連結規則,可定位出以下兩個錯誤。

錯誤一:第 27 行未設定新節點的 prev

尾端節點的 next 指向新節點後,新節點的 prev 也必須指回原尾端節點。否則從新節點往前走會失效。

temp->next = newNode;
newNode->prev = temp;

錯誤二:第 39 行過早釋放節點

第 39 行釋放 temp 後,後續程式仍會讀取 temp->prev 與 temp->next,造成釋放後使用。應先完成前後節點的連結更新,再釋放 temp。

修正後的程式碼

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 13-(2) 題5 分

There are TWO BUGS in the following doubly linked list program. For each bug, please provide:

  • The line number where the bug occurs, and
  • The corrected solution in C.
🖼️ 本題附圖:
第 13-(2) 題附圖第 13-(2) 題附圖第 13-(2) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

雙向鏈結串列的每個節點都有 prev 與 next 兩個指標。插入節點時,前後兩個方向都要接好;刪除節點時,必須先更新相鄰節點的指標,再釋放節點記憶體。

解題方法

原卷印刷內容顯示,struct Node 包含 data、prev、next;insertEnd 走訪到尾端後只設定新節點的前向連結,deleteNode 則在更新串列連結前就呼叫 free(temp)。依照雙向鏈結串列的連結規則,可定位出以下兩個錯誤。

錯誤一:第 27 行未設定新節點的 prev

尾端節點的 next 指向新節點後,新節點的 prev 也必須指回原尾端節點。否則從新節點往前走會失效。

temp->next = newNode;
newNode->prev = temp;

錯誤二:第 39 行過早釋放節點

第 39 行釋放 temp 後,後續程式仍會讀取 temp->prev 與 temp->next,造成釋放後使用。應先完成前後節點的連結更新,再釋放 temp。

修正後的程式碼

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 2 題共用同一段題幹

以下為 min heap 程式。Heap 中儲存的資料不含相同值。
🖼️【此處有附圖,請對照原卷】

第 14-(1) 題5 分

There are TWO BUGS in the following min heap program. Note that the data stored in a min heap should not contain identical values. For each bug, please provide:

  • The line number where the bug occurs, and
  • The corrected solution in C.
🖼️ 本題附圖:
第 14-(1) 題附圖第 14-(1) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Min heap 必須滿足每個節點的值小於子節點,也就是父節點值小於或等於子節點值。插入時要把新值向上移動;刪除最小值時,則要用最後一個元素補到根節點,再向下調整。

解題方法

圖中第 14 題給出 insert、heapify 與 deleteMin 函式;insert 在第 41 行比較父節點與新元素,deleteMin 在第 74 行搬移元素。逐一對照 min heap 的向上與向下調整規則,可找出兩處錯誤。

第一處在第 41 行。min heap 插入時,只有父節點大於新元素,才需要交換;原程式使用 <,會把較大的元素往上移,破壞 min heap 性質。

while (i != 0 && heap->arr[parent(i)] > heap->arr[i]) {
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 14-(2) 題5 分

There are TWO BUGS in the following min heap program. Note that the data stored in a min heap should not contain identical values. For each bug, please provide:

  • The line number where the bug occurs, and
  • The corrected solution in C.
🖼️ 本題附圖:
第 14-(2) 題附圖第 14-(2) 題附圖

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Min heap 必須滿足每個節點的值小於子節點,也就是父節點值小於或等於子節點值。插入時要把新值向上移動;刪除最小值時,則要用最後一個元素補到根節點,再向下調整。

解題方法

圖中第 14 題給出 insert、heapify 與 deleteMin 函式;insert 在第 41 行比較父節點與新元素,deleteMin 在第 74 行搬移元素。逐一對照 min heap 的向上與向下調整規則,可找出兩處錯誤。

第一處在第 41 行。min heap 插入時,只有父節點大於新元素,才需要交換;原程式使用 <,會把較大的元素往上移,破壞 min heap 性質。

while (i != 0 && heap->arr[parent(i)] > heap->arr[i]) {
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 15 題4 分

A binary min-heap (the one for heap-sort) is stored in an array. In a C array, we store values starting from index 1. The array in the table below is not a min-heap. Build the min-heap from the array shown above using the bottom-up linear-time algorithm. Present your result in the same array where each value is in the correct array entry.

Index01234567891011
Value-1151029384761

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考的是以 bottom-up heap construction 將陣列轉成 min-heap。Min-heap 必須滿足:每個父節點的值都小於或等於其子節點的值。

題目使用從索引 11 開始的陣列,因此索引 ii 的父節點與子節點位置為:

parent⁡(i)=⌊i2⌋,left⁡(i)=2i,right⁡(i)=2i+1\operatorname{parent}(i)=\left\lfloor\frac{i}{2}\right\rfloor,\qquad \operatorname{left}(i)=2i,\qquad \operatorname{right}(i)=2i+1

葉節點不需要調整。若共有 nn 個元素,最後一個非葉節點是 ⌊n/2⌋\lfloor n/2\rfloor;本題 n=11n=11,因此從索引 55 開始,依序往上調整至索引 11。

解題方法

每次對節點執行向下調整:比較節點與其子節點,若父節點比最小的子節點大,就交換,並在交換後的位置繼續調整。

初始陣列(索引 11 至 1111)為:

[11, 5, 10, 2, 9, 3, 8, 4, 7, 6, 1][11,\ 5,\ 10,\ 2,\ 9,\ 3,\ 8,\ 4,\ 7,\ 6,\ 1]

依序由索引 55 調整至索引 11:

  1. **調整索引 55:**值為 99,子節點是索引 1010 的 66 與索引 1111 的 11。與較小的 11 交換。

    [11, 5, 10, 2, 1, 3, 8, 4, 7, 6, 9][11,\ 5,\ 10,\ 2,\ 1,\ 3,\ 8,\ 4,\ 7,\ 6,\ 9]

  2. **調整索引 44:**值為 22,子節點為 44 和 77。父節點已小於兩個子節點,不需交換。

  3. **調整索引 33:**值為 1010,子節點為 33 和 88。與較小的 33 交換。

    [11, 5, 3, 2, 1, 10, 8, 4, 7, 6, 9][11,\ 5,\ 3,\ 2,\ 1,\ 10,\ 8,\ 4,\ 7,\ 6,\ 9]

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 16 題5 分

Given a graph G=(V,E)G=(V,E), to answer whether there is a Hamiltonian Cycle in GG is NP-Complete. Do you think the function f(G,u,v,k)f(G,u,v,k) is tractable? The function ff returns yes if there is a path of length kk from uu to vv in GG, otherwise it returns no. If you think it is tractable, give me your reasons, or try to argue that the function is not tractable.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題的關鍵在於「path」的定義,以及 kk 是否要求路徑中的頂點不能重複。

圖論通常將 path(簡單路徑) 定義為頂點不重複的路徑。判定問題可寫成:

輸入圖 GG、起點 uu、終點 vv 與長度 kk,判斷是否存在一條從 uu 到 vv、恰含 kk 條邊的簡單路徑。

這個問題屬於 NP:給定一條候選路徑,可以在多項式時間內檢查端點、邊數及頂點是否重複。

解題方法

以 Hamiltonian Cycle 問題歸約到此判定問題。給定一個有 nn 個頂點的圖 GG,任選頂點 rr,將 rr 拆成兩個頂點 r1,r2r_1,r_2:

  1. 保留 G−rG-r 中原有的邊。
  2. 對每個 rr 在 GG 中的鄰點 xx,在新圖中加入邊 (r1,x)(r_1,x) 與 (r2,x)(r_2,x)。
  3. 新增頂點 s,ts,t,並只加入邊 (s,r1)(s,r_1) 與 (r2,t)(r_2,t)。

令新圖為 G′G'。它共有 n+2n+2 個頂點,並將題目中的參數設為:

u=s,v=t,k=n+1u=s,\qquad v=t,\qquad k=n+1

因為 s,ts,t 的度數都是 11,任何從 ss 到 tt、長度為 n+1n+1 的簡單路徑,都必須經過 G′G' 的全部 n+2n+2 個頂點。

若 GG 有 Hamiltonian Cycle,沿著該環移除 rr,會留下 G−rG-r 上的一條涵蓋所有其餘頂點的路徑。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 17-(a) 題4 分

The following pseudo-code defines a Node class used in a binary search tree TT. In the Node, vv stores the key value, lclc and rcrc are the pointers to the left child and right child, respectively, and pp is the pointer to the parent. If TT contains a single node XX, then X.pX.p, X.lcX.lc, X.rcX.rc are NULL. Show that there are n+2n+2 NULL pointers in TT, where TT is an nn-node binary search tree.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

每個節點有三個指標:左子節點指標 lclc、右子節點指標 rcrc,以及父節點指標 pp。要計算整棵樹中的 NULL 指標,可分成「子節點指標」與「父節點指標」兩類計數。

含有 nn 個節點的二元樹有 n−1n-1 條父子連線:除根節點外,每個節點恰有一個父節點。

解題方法

一、計算左、右子節點指標中的 NULL 數量

每個節點各有兩個子節點指標,因此共有 2n2n 個子節點指標。每條父子連線會占用一個子節點指標;共有 n−1n-1 條連線,因此非 NULL 的子節點指標有 n−1n-1 個。

所以 NULL 子節點指標數為:

2n−(n−1)=n+12n-(n-1)=n+1
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 17-(b) 題6 分

Consider the following pseudo-code:

a=n1a=n1

b=n2b=n2

while a≠ba\ne b:

  a=a.pa=a.p if aa else n2n2

  b=b.pb=b.p if bb else n1n1

return aa

In the above pseudo-code, n1n1 and n2n2 are two nodes in a binary search tree TT. a=a.pa=a.p if aa else n2n2 means if aa is not NULL then set aa to a.pa.p otherwise set aa to n2n2. What does this piece of code compute (what is "aa" returned by the function)?

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這段程式利用每個節點的父指標 p,沿著節點到根節點的路徑向上走。兩條路徑第一次相交的節點,就是 n1 和 n2 的最低共同祖先(Lowest Common Ancestor,LCA)。

在同一棵樹中,兩個節點往上走的祖先路徑會在 LCA 相交;LCA 是兩者共同祖先中距離它們最遠、也就是最深的那一個。

解題方法

令 n1、n2 到根節點的距離分別為 d1d_1、d2d_2。若直接讓兩個指標各自往上走,較深的節點會先到根,因此兩者不一定會同時走到 LCA。

程式讓指標在到達 NULL 時改從另一個起點重新走:

  • a 先走 n1 到根,再從 n2 開始走。
  • b 先走 n2 到根,再從 n1 開始走。

兩條路徑的長度差因此被補齊,指標會在 LCA 相遇。若其中一個節點本身就是另一個節點的祖先,兩者則會在該祖先相遇。

關鍵程式碼

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 17-(c) 題7 分

Consider the following pseudo-code:

a=n1a=n1

b=n2b=n2

while a≠ba\ne b:

  a=a.pa=a.p if aa else n2n2

  b=b.pb=b.p if bb else n1n1

return aa

In the above pseudo-code, n1n1 and n2n2 are two nodes in a binary search tree TT. a=a.pa=a.p if aa else n2n2 means if aa is not NULL then set aa to a.pa.p otherwise set aa to n2n2.

Show the correctness of the piece of code, i.e., the code correctly computes the answer you gave in 17-(b) (it returns the lowest common ancestor of n1n1 and n2n2).

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這段程式和「找兩條鏈結串列的交會點」是同一招:讓兩個指標各走「自己的路+對方的路」,兩邊總長度相同,所以會同時抵達交會點。

設根的深度為 00,d1=depth⁡(n1)d_1=\operatorname{depth}(n1)、d2=depth⁡(n2)d_2=\operatorname{depth}(n2),LL 為兩者的最低共同祖先(LCA),ℓ=depth⁡(L)\ell=\operatorname{depth}(L)。

證明

1. 兩個指標的路線。 每一輪迴圈 aa、bb 各前進一步。

  • aa:第 00 步在 n1n1,往上走 d1d_1 步到根,第 d1+1d_1+1 步變成 NULL,第 d1+2d_1+2 步跳到 n2n2,接著從 n2n2 往上走。
  • bb:對稱地,第 d2+2d_2+2 步跳到 n1n1,接著從 n1n1 往上走。

2. 同時抵達 LL。 令

t=d1+d2−ℓ+2t=d_1+d_2-\ell+2

第 tt 步時,aa 已從 n2n2 往上走了 t−(d1+2)=d2−ℓt-(d_1+2)=d_2-\ell 步,恰好到達 n2n2 的深度 ℓ\ell 祖先,也就是 LL;bb 已從 n1n1 往上走了 t−(d2+2)=d1−ℓt-(d_2+2)=d_1-\ell 步,同樣到達 LL。所以第 tt 步時 a=b=La=b=L,迴圈最晚在這一步停止。

(若 d1=d2d_1=d_2,兩者在第 d1−ℓd_1-\ell 步就同時到達 LL,還沒走到 NULL;以下證明這也是最早的相遇點。)

3. 不會更早在別處相遇。 假設第 s<ts<t 步時 a=b=Xa=b=X:

  • 兩者都還在第一段(往上走自己的祖先):aa 在 n1n1 的祖先、bb 在 n2n2 的祖先,同一步時深度分別為 d1−sd_1-s、d2−sd_2-s。若 XX 是同一個節點,深度必須相同,所以 d1=d2d_1=d_2,而且 XX 是兩者的共同祖先;
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題