A tree is a connected undirected graph \(G=(V,E)\) which contains no cycles. Many problems can be solved efficiently in trees. For example, it takes super-linear time to solve the maximum matching (a matching is a set of edges without common vertices) problem in general graphs or bipartite graphs. However, in trees, the problem can be solved in linear time. In the input file, each tree is given in several lines: The first line contains an integer \(n\) indicating the number of vertices (denoted by numbers \(1,2,\ldots,n\)) in the tree. The following \(n-1\) lines contain the edges given by a pair of vertex numbers (recall that each tree with \(n\) vertices has exactly \(n-1\) edges), which are separated by a space.
(1) "Do not assume the input is always valid." It is a good advice for programmers. For example, consider the following input: 5 / 1 2 / 2 3 / 1 3 / 4 5. The input indicates that there is a graph with 5 vertices and 4 edges. Note that this graph is not a tree because it is not connected and has a cycle. Design an \(O(n)\) time algorithm to detect whether the given input represents a tree. (7 points)
(2) Argue that the following observation is correct: For each leaf \(v\) and the only edge \((u,v)\) incident to it, there exists a maximum matching \(M\subseteq E\) such that \((u,v)\in M\). (6 points)
(3) Based on the observation in (2), give an \(O(n)\) time algorithm to solve the maximum matching problem in trees. Please describe what data structure is used for storing the given tree, and what mechanisms are used in its implementation to achieve the linear running time. (12 points)
(1) 判斷是不是 tree
輸入已固定是 \(n\) 個點、\(n-1\) 條邊。\(n\) 點 \(n-1\) 邊的圖:connected \(\iff\) tree(連通又只有 \(n-1\) 條邊,擠不出 cycle)
① 讀 \(n-1\) 條邊,先檢查端點是否都在 \(1..n\)(不在就不是 tree),建 adjacency list
② 從 vertex 1 做一次 DFS/BFS,數走到的點數 count
③ \(\text{count}=n\) → tree;否則不是
自環、重複邊也會被檢查到:它們白占一條邊的額度,剩下的邊接不起 \(n\) 個點,count \(\lt n\)
題目的例子從 1 只走得到 \(\{1,2,3\}\),count \(=3\ne5\) → 不是 tree
時間:建表 \(O(n)\),DFS 每個點、每條邊各處理常數次 → \(O(n)\)
(2) leaf edge 在某個 maximum matching 裡
設 \(v\) 是 leaf,\((u,v)\) 是唯一與 \(v\) 相鄰的邊,\(M\) 是任一 maximum matching
\(v\) 已被 \(M\) 配對:能配 \(v\) 的只有 \((u,v)\),所以 \((u,v)\in M\)
\(v\) 沒被配對:\(u\) 一定已被配對,否則 \(M\cup\{(u,v)\}\) 還是 matching 且更大,與 \(M\) maximum 矛盾
設 \((u,w)\in M\),令 \(M^{\prime}=M-\{(u,w)\}+\{(u,v)\}\):\(u\) 仍只用一次、\(v\) 原本沒用、\(w\) 變成沒用 → \(M^{\prime}\) 是 matching,\(|M^{\prime}|=|M|\)
\(M^{\prime}\) 也是 maximum matching 且含 \((u,v)\),得證
(3) \(O(n)\) 演算法
資料結構:adjacency list(\(2(n-1)\) 個 list node),加兩個長度 \(n\) 的 array:parent[]、matched[]
① 任選 root 做一次 DFS(用自己的 stack 迭代,不靠遞迴),記下 parent,並依「離開順序」得 postorder
② 依 postorder(從最下面的 leaf 往 root)掃每個 \(v\ne\text{root}\):
\(u\leftarrow\text{parent}[v]\);若 matched[v] 與 matched[u] 都是 false,就把 \((u,v)\) 放進 \(M\),兩個都標成 true
③ 傳回 \(M\)
正確性:postorder 最先遇到的 \(v\) 是(剩餘樹的)leaf,由 (2) 把 leaf edge 放進 \(M\) 不吃虧;拿掉 \(u,v\) 之後剩下若干棵樹,同樣論證反覆做
線性的關鍵:adjacency list 讓每條邊只被碰兩次;matched[]、parent[] 是 array,查詢更新 \(O(1)\),不需要搜尋;整體 \(O(n)\)
(1) 檢查連通性(count = n);(2) leaf edge exchange;(3) postorder 貪心配對 O(n)