二分图最大匹配

$\newcommand{\symmdiff}{\mathbin{\triangle}}$

二分图最大匹配是很早就接触到的算法,不过原理至今都不太了然,这篇随笔做一个较为详尽的总结。

下面只讨论增广路算法,不讨论网络流解法。

Definition 1.

Let $G$ be a graph and $M$ a matching in $G$. We say that a vertex $v$ is covered by $M$ if $v \in e$ for some $e \in M$; otherwise $v$ is exposed (by $M$). $M$ is called a perfect matching if all vertices are covered by $M$.

Definition 2.

 Let $G$ be a graph (bipartite or not), and let $M$ be some matching in $G$. A path $P$ is an $M$-alternating path if $E(p) \setminus M$ is a matching. An $M$-alternating path is $M$-augmenting if it has positive length and its endpoints are exposed by $M$.

Theorem 3. (Petersen [1891], Berge [1957])    

Let $G$ be a graph (bipartite or not) with some matching $M$. Then $M$ is maximum if and only if there is no $M$-augmenting path.

Proof: If there is an $M$-augmenting path $P$, the symmetric difference $M \symmdiff E(P)$ is a matching and has greater cardinality than $M$, so $M$ is not maximum. On the other hand, if there is a matching $M'$ such that $|M'| > |M|$, the symmetric difference $M \symmdiff M'$ is the vertex-disjoint union of alternating circuts and paths, where at least one path must be $M$-augmenting.

This theorem motivates the following algorithm. Let $G$ be a bipartite graph with bipartition $V(G) = A \cup B$. Start with any matching $M$, say the empty matching. Repeatedly locate an $M\text{-augmenting}$ path $P$, augment $M$ along $P$ and replace $M$ by the resulting matching. Stop when no more augmenting path exists.

The question now is how to find an augmenting path. This task can be done as follows. Direct edges in $G$ according to $M$ as follows: An edge goes from $A$ to $B$ if it does not belong to the matching $M$ and from $B$ to $A$ if it does. Call this directed graph $D$.

Claim There exists an $M$-augmenting path in $G$ if and only if there exists a directed path in $D$ from an exposed vertex in $A$ to an exposed vertex in $B$.

This gives an $O(m)$ algorithm (where $m = |E(G)|$) for finding an augmenting path in $G$. Let $A^{∗}$ and $B^{∗}$ be the set of exposed vertices with respect to $M$ in $A$ and $B$ respectively. We can simply attach a vertex $s$ to all the vertices in $A^{∗}$ and do a depth-first-search from $s$ till we hit a vertex in $B^{∗}$ and then trace back our path.

Thus the overall complexity of finding a maximum cardinality matching is $O(nm)$. This can be improved to $O( \sqrt{n}m)$ by augmenting along several augmenting paths simultaneously.

There is a variant of the above algorithm that is easier to implement. Start with the empty matching. For each exposed vertex $a \in A$, do a DFS from $a$ in the directed graph $D$ till we hit an exposed vertex in $B$ and then trace back our path. If no exposed vertex in $B$ can be reached from $a$, remove all vertices which can be reached by a directed path from $a$, i.e. all vertices reached in the DFS from $a$.

Let $v$ be the first vetex in $A$ that a DFS from $v$ finds no augmenting path. Let $L_v$ be the set of vertices which can be reached by a directed path from $v$. Let $A_v = L_v \cap A$ and $B_v = L_v \cap B$. Obviously there is no directed edge from $A_v$ to $B - B_v$, so no subsequent augmenting path will visit $L_v$ and it is safe to remove $L_v$.

 References

Bernhard Korte, Jens Vygen Combinatorial Optimization, Theory and Algorithms 5th edition, Springer

Michel X. Goemans Lecture notes on bipartite matching   

 

posted @ 2016-11-22 11:45  Pat  阅读(350)  评论(0编辑  收藏  举报