|
| 1 | +--- |
| 2 | +tags: |
| 3 | + - OMSCS |
| 4 | + - Algorithms |
| 5 | + - Practice |
| 6 | +--- |
| 7 | +# 5.20 - Perfect Matching Tree |
| 8 | +![[Pasted image 20260316173646.png]] |
| 9 | + |
| 10 | +Note, the "feedback edge set" was not part of the problem recommended by course staff. |
| 11 | + |
| 12 | +## Exploration |
| 13 | +- The problem statement indicates that $G$ is a connected tree, and not a forest of subtrees. |
| 14 | +- If $G$ has an odd number of vertices, then the answer is automatically "no". If $G$ has no vertices, the answer is automatically "yes", with $PM=\emptyset$. The following steps presume that G has an even number of vertices, and at least 2 of them. |
| 15 | +- We define a "fringe" vertex as a vertex which only has one edge. |
| 16 | +- We can identify the number of edges connected to each v in O(n+m) time. We'll call this structure `cardinality[]`. If $cardinality[v]=1$, then $v$ is a fringe vertex. |
| 17 | +- We can then build a queue of the initial "fringe" vertices. There must be at least 2, since $G$ is a nontrivial tree. |
| 18 | +- While the queue has fringe vertices. |
| 19 | + - We pull a fringe vertex $v$ from the queue. |
| 20 | + - We first check whether $v$ has already been removed from $G$. If $v$ has already been removed from $G$, we ignore it, and pull the next vertex from the queue. |
| 21 | + - We iterate over all the edges leaving v ($e=(v,u) \in E$) to find the one neighbor of $v$ which has not already been removed from $G$. This will find either 0 or 1 options for $u$. |
| 22 | + - If we find 0 vertices, then $v$ was isolated by prior pruning operations, and there no longer exists an edge which touches $v$. Therefore there does not exist a "perfect matching" in G. |
| 23 | + - Otherwise, we iterate over all vertices adjacent to $u$, and reduce their cardinality. For all the vertices which now have a cardinality of 1, we add them to the queue. |
| 24 | + - We conclude by removing both $v$ and $u$ from $G$. |
| 25 | +- If we successfully remove all vertices from $G$ without issue, then $G$ has a perfect matching. We can build this $PM$ set of edges during the algorithm's execution, but the question doesn't ask for it, so we aren't doing it. |
| 26 | + |
| 27 | +I believe this works, but it doesn't use a black box from [[04.0.1 - Graphs - Black Box Algorithms]]. |
| 28 | + |
| 29 | +## Algorithm |
| 30 | +- Check for an odd number of vertices. If so, return no. |
| 31 | +- Check for 0 vertices. If so, return yes. |
| 32 | +- DFS on $G$ from any arbitrary vertex $s \in V$ to create `pre[]` and `prev[]` |
| 33 | +- Create an ordering of $V$ based on `pre[]`. We'll call this $V_{pre}$ |
| 34 | +- Walk backwards through $v \in V_{pre}$. |
| 35 | + - If $v$ is removed, skip it, and select the next $v$. |
| 36 | + - Otherwise |
| 37 | + - Remove $v$ from $G$. |
| 38 | + - Check whether $prev[v]$ is removed from $G$. If so, we answer "no". |
| 39 | + - Otherwise, remove $prev[v]$ from $G$. |
| 40 | +- If we managed to get through the whole list without returning "no", then we answer "yes". |
| 41 | + |
| 42 | +## Justifications |
| 43 | +- Iterating over vertices by descending order of preorder number prioritizes vertices which are "fringe" vertices. The vertex with the highest preorder number has only one edge. |
| 44 | +- We walk "up" the tree from those fringe vertices, towards the arbitrary starting vertex, removing connected pairs from the tree as we go. |
| 45 | +- We run into an issue when 2 vertices $v_1$ and $v_2$ share the same parent ($u=prev[v_1]=prev[v_2]$), and are independently selected as $v \in V_{pre}$ in our algorithm. This indicates that the DFS from $s$ encountered a branch at $u$. $u$ is the root of of a subtree $T_u$ of $G$, and removing $u$ from $T_u$ produces at least 2 additional subtrees. At least 2 of these subtrees, symbolized $T_{u,a}$ and $T_{u,b}$, are both independently incapable of producing a $PM$ set of edges. Therefore, both must include an edge to $u$ in order to produce a $PM$ set. However, this would require that the $PM$ set of $G$ contains $u$ at least twice, which is a contradiction in the definition of what a $PM$ set of edges is. |
| 46 | + |
| 47 | +## Runtime |
| 48 | +- Checking for odd number of vertices, or 0 vertices: $O(n)$ |
| 49 | +- DFS: $O(n+m)$ |
| 50 | +- Creating $V_{pre}$: $O(n)$ |
| 51 | +- Iterating over $V_{pre}$: $O(n)$ |
| 52 | + |
| 53 | +Overall: $O(n+m)$ |
0 commit comments