跳转至

12 Graph Valid Tree — Interview English Script (C++)

Source aligned with: docs/15_Graphs/12_Graph_Valid_Tree.md

Quick links: Source Solution · Chapter Script Index · Global Index

1) 30-second problem restatement script

English line Traditional Chinese meaning (short) Interview stage
Let me restate graph valid tree. 我先重述 Graph Valid Tree。 Restatement
We have n nodes and undirected edges. 題目給 n 個節點與無向邊。 Restatement
We need to decide whether the graph is a valid tree. 要判斷這張圖是否為有效樹。 Restatement
A valid tree must be connected and acyclic. 有效樹必須連通且無環。 Restatement
A necessary property is edge count equals n minus one. 必要條件是邊數要等於 n-1。 Restatement
I will use Union Find plus edge-count check. 我會用 Union Find 搭配邊數檢查。 Restatement

2) Clarifying questions (5 lines)

English line Traditional Chinese meaning (short) Interview stage
Are node labels from zero to n minus one? 節點編號是否為 0 到 n-1? Clarify
Is graph undirected for every edge pair? 每條邊是否都是無向? Clarify
Can there be duplicate edges or self-loops? 是否可能有重複邊或自迴圈? Clarify
Should I return boolean only? 是否只需回傳布林值? Clarify
Is early check edges size not equal n minus one acceptable? 可否先做 edges 數量不等於 n-1 的早退檢查? Clarify

3) Approach discussion

Brute force (3 lines)

English line Traditional Chinese meaning (short) Interview stage
DFS or BFS can test connectivity and cycle. DFS/BFS 可以檢查連通與環。 Approach
But we still need careful parent tracking for cycle in undirected graph. 但無向圖判環仍需小心父節點追蹤。 Approach
Union Find gives concise checks for both conditions. Union Find 可精簡地同時處理兩條件。 Approach

Optimized approach (5 lines)

English line Traditional Chinese meaning (short) Interview stage
First check edges size equals n minus one. 先檢查邊數是否為 n-1。 Approach
If not equal, graph cannot be a tree. 若不相等,圖不可能是樹。 Approach
Then run Union Find over all edges. 接著用 Union Find 掃所有邊。 Approach
If any edge connects nodes already in same set, cycle exists so false. 若任何邊連到同集合,表示有環,回 false。 Approach
If all unions succeed under n minus one edges, graph is connected and valid. 在 n-1 邊下若都可合併成功,圖即連通且有效。 Approach

4) Coding-and-speaking script (line-by-line, in coding order)

English line Traditional Chinese meaning (short) Interview stage
I first check if edges size is not n minus one, then return false. 我先檢查 edges 數量若不是 n-1 就回 false。 Coding
I initialize parent array parent[i]=i. 我初始化 parent 陣列 parent[i]=i。 Coding
I may keep components count starting at n. 我可以維護 components 從 n 開始。 Coding
For each edge u v, compute roots ru and rv. 對每條邊 u,v 計算根 ru、rv。 Coding
If ru equals rv, this edge forms cycle so return false. 若 ru==rv,這條邊成環,回 false。 Coding
Otherwise union ru and rv. 否則合併 ru 與 rv。 Coding
Optionally decrement components after each union. 可選擇在每次 union 後遞減 components。 Coding
Continue processing all edges. 持續處理直到所有邊完成。 Coding
If no cycle found, return true. 若未發現環,就回 true。 Coding
This works because edge-count precheck guarantees connectivity. 這可行是因為邊數前置檢查已保障連通性條件。 Coding

5) Dry-run script using one sample input

English line Traditional Chinese meaning (short) Interview stage
Let me dry-run n five with edges [0,1],[0,2],[0,3],[1,4]. 我手跑 n=5,邊為 [0,1],[0,2],[0,3],[1,4]。 Dry-run
Edge count is four, equal to n minus one. 邊數是 4,等於 n-1。 Dry-run
Union zero one succeeds. 合併 0 與 1 成功。 Dry-run
Union zero two and zero three both succeed. 合併 0-2 與 0-3 都成功。 Dry-run
Union one four also succeeds without same-root conflict. 合併 1-4 也成功,沒有同根衝突。 Dry-run
No cycle is detected in all edges. 所有邊都未偵測到環。 Dry-run
Return true for valid tree. 回傳 true,表示有效樹。 Dry-run

6) Edge/corner test script (at least 4 cases)

English line Traditional Chinese meaning (short) Interview stage
Case one: edges less than n minus one means disconnected, return false. 案例一:邊少於 n-1 代表不連通,回 false。 Edge test
Case two: edges greater than n minus one means cycle guaranteed, return false. 案例二:邊多於 n-1 保證有環,回 false。 Edge test
Case three: exactly n minus one with cycle still false. 案例三:雖是 n-1 但有環仍回 false。 Edge test
Case four: n one with no edge should be true. 案例四:n=1 且無邊應為 true。 Edge test
Case five: self-loop edge should fail cycle check. 案例五:自迴圈邊應觸發成環失敗。 Edge test

7) Complexity script

Short version (2 lines)

English line Traditional Chinese meaning (short) Interview stage
Time complexity is O(N alpha N). 時間複雜度是 O(N·α(N))。 Complexity
Space complexity is O(N). 空間複雜度是 O(N)。 Complexity

Full version (4 lines)

English line Traditional Chinese meaning (short) Interview stage
We process about n minus one edges after precheck. 前置檢查後大約處理 n-1 條邊。 Complexity
Each find or union is near constant with path compression. 路徑壓縮下每次 find/union 近乎常數。 Complexity
Total runtime is O(N alpha N). 總時間是 O(N·α(N))。 Complexity
Parent array uses O(N) memory. parent 陣列使用 O(N) 記憶體。 Complexity

8) If stuck rescue lines (10 lines)

English line Traditional Chinese meaning (short) Interview stage
Let me separate tree conditions clearly. 我先把樹的條件分開講清楚。 If stuck
Condition one: exactly n minus one edges. 條件一:邊數必須是 n-1。 If stuck
Condition two: no cycle. 條件二:不可有環。 If stuck
Under condition one, no cycle implies connected. 在條件一成立下,無環就能推出連通。 If stuck
So edge-count precheck simplifies final logic. 所以邊數前檢可簡化後續邏輯。 If stuck
In Union Find, same root means cycle. Union Find 中同根代表成環。 If stuck
Let me test a cycle sample quickly. 我快速測一個成環案例。 If stuck
Same-root detection should trigger false immediately. 同根檢測應立即回 false。 If stuck
Then test simple chain should return true. 再測簡單鏈狀圖應回 true。 If stuck
Great, correctness intuition is now solid. 很好,正確性直覺已穩固。 If stuck

9) Final wrap-up lines (5 lines)

English line Traditional Chinese meaning (short) Interview stage
I solved valid tree with edge-count check plus Union Find. 我用邊數檢查加 Union Find 解了 valid tree。 Wrap-up
Edge count filters impossible cases early. 邊數檢查可先過濾不可能案例。 Wrap-up
Same-root union attempt detects cycles directly. union 時同根可直接偵測環。 Wrap-up
Complexity is near linear with O(N alpha N) time and O(N) space. 複雜度近線性:O(N·α(N)) 時間、O(N) 空間。 Wrap-up
This is a standard DSU tree-validation pattern. 這是樹驗證題的標準 DSU 模式。 Wrap-up

10) Ultra-short cheat sheet (20 lines total)

English line Traditional Chinese meaning (short) Interview stage
Goal: check if undirected graph is a tree. 目標:判斷無向圖是否為樹。 Cheat sheet
Tree needs connected plus acyclic. 樹需要連通且無環。 Cheat sheet
Precheck edges count == n-1. 先檢查邊數是否 n-1。 Cheat sheet
If not, return false immediately. 若否,立即回 false。 Cheat sheet
Initialize Union Find parent array. 初始化 Union Find parent。 Cheat sheet
Process each edge u v. 處理每條邊 u,v。 Cheat sheet
Find roots ru and rv. 查根 ru、rv。 Cheat sheet
If ru==rv, cycle -> false. 若 ru==rv,有環 -> false。 Cheat sheet
Else union roots. 否則合併兩根。 Cheat sheet
Continue all edges. 持續處理所有邊。 Cheat sheet
No cycle found => true. 未發現環 => true。 Cheat sheet
n=1 no edge => true. n=1 無邊 => true。 Cheat sheet
Too few edges => disconnected false. 邊太少 => 不連通 false。 Cheat sheet
Too many edges => cycle false. 邊太多 => 有環 false。 Cheat sheet
Time O(N alpha N). 時間 O(N·α(N))。 Cheat sheet
Space O(N). 空間 O(N)。 Cheat sheet
Common bug: forgetting edge-count precheck. 常見錯誤:忘記邊數前檢。 Cheat sheet
Common bug: wrong index bounds in parent. 常見錯誤:parent 索引範圍錯。 Cheat sheet
DFS/BFS can also solve. DFS/BFS 也能解。 Cheat sheet
Explain two conditions clearly to interviewer. 跟面試官清楚說兩個條件。 Cheat sheet

Quality check

  • Consistency with source solution: ✅ Edge-count precheck + DSU cycle detection aligned.
  • No hallucinated constraints: ✅ Tree definition and return semantics preserved.
  • Language simplicity: ✅ Short and practical interview speech.