maximum_matching.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <html><head><!--
  2. Copyright 2005 Aaron Windsor
  3. Use, modification and distribution is subject to the Boost Software
  4. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt)
  6. Author: Aaron Windsor
  7. --><title>Boost Graph Library: Maximum Cardinality Matching</title></head>
  8. <body alink="#ff0000" bgcolor="#ffffff" link="#0000ee" text="#000000" vlink="#551a8b">
  9. <img src="../../../boost.png" alt="C++ Boost" height="86" width="277">
  10. <br clear="">
  11. <h1>
  12. <a name="sec:maximum_cardinality_matching">Maximum Cardinality Matching</a>
  13. </h1>
  14. <pre>
  15. template &lt;typename Graph, typename MateMap&gt;
  16. void edmonds_maximum_cardinality_matching(const Graph&amp; g, MateMap mate);
  17. template &lt;typename Graph, typename MateMap, typename VertexIndexMap&gt;
  18. void edmonds_maximum_cardinality_matching(const Graph&amp; g, MateMap mate, VertexIndexMap vm);
  19. template &lt;typename Graph, typename MateMap&gt;
  20. bool checked_edmonds_maximum_cardinality_matching(const Graph&amp; g, MateMap mate);
  21. template &lt;typename Graph, typename MateMap, typename VertexIndexMap&gt;
  22. bool checked_edmonds_maximum_cardinality_matching(const Graph&amp; g, MateMap mate, VertexIndexMap vm);
  23. </pre>
  24. <p>
  25. <a name="sec:matching">A <i>matching</i> is a subset of the edges
  26. of a graph such that no two edges share a common vertex.
  27. Two different matchings in the same graph are illustrated below (edges in the
  28. matching are colored blue.) The matching on the left is a <i>maximal matching</i>,
  29. meaning that its size can't be increased by adding edges. The matching on the
  30. right is a <i>maximum cardinality matching</i>, meaning that is has maximum size
  31. over all matchings in the graph.
  32. </a></p><p></p><center>
  33. <table border="0">
  34. <tr>
  35. <td><a name="fig:maximal_matching"><img src="figs/maximal-match.png"></a></td>
  36. <td width="150"></td>
  37. <td><a name="fig:maximum_matching"><img src="figs/maximum-match.png"></a></td>
  38. </tr>
  39. </table>
  40. </center>
  41. <p>
  42. Both <tt>edmonds_maximum_cardinality_matching</tt> and
  43. <tt>checked_edmonds_maximum_cardinality_matching</tt> find the
  44. maximum cardinality matching in any undirected graph. The matching is returned in a
  45. <tt>MateMap</tt>, which is a
  46. <a href="../../property_map/doc/ReadWritePropertyMap.html">ReadWritePropertyMap</a>
  47. that maps vertices to vertices. In the mapping returned, each vertex is either mapped
  48. to the vertex it's matched to, or to <tt>graph_traits&lt;Graph&gt;::null_vertex()</tt> if it
  49. doesn't participate in the matching. If no <tt>VertexIndexMap</tt> is provided, both functions
  50. assume that the <tt>VertexIndexMap</tt> is provided as an internal graph property accessible
  51. by calling <tt>get(vertex_index,g)</tt>. The only difference between
  52. <tt>edmonds_maximum_cardinality_matching</tt> and
  53. <tt>checked_edmonds_maximum_cardinality_matching</tt> is that as a final step,
  54. the latter algorithm runs a simple verification on the matching computed and
  55. returns <tt>true</tt> if and only if the matching is indeed
  56. a maximum cardinality matching.
  57. <p>
  58. Given a matching M, any vertex that isn't covered by an edge in M is called <i>free</i>. Any
  59. simple path containing exactly <i>2n + 1</i> edges that starts and ends at free vertices and contains
  60. <i>n</i> edges from M is called an <i>alternating path</i>. Given an alternating path <i>p</i>, all matching and
  61. non-matching edges on <i>p</i> can be swapped, resulting in a new matching that's larger than the
  62. original matching by exactly one edge. This method of incrementally increasing the size of matching, along
  63. with the following fact, forms the basis of Edmonds' matching algorithm:
  64. <blockquote>
  65. <i>An alternating path through the matching M exists if and only if M is not a maximum cardinality matching.</i>
  66. </blockquote>
  67. The difficult part is, of course, finding an augmenting path whenever one exists.
  68. The algorithm we use for finding a maximum cardinality matching consists of three basic steps:
  69. <ol>
  70. <li>Create an initial matching.
  71. <li>Repeatedly find an augmenting path and use it to increase the size of the matching until no augmenting path exists.
  72. <li>Verify that the matching found is a maximum cardinality matching.
  73. </ol>
  74. If you use <tt>checked_edmonds_maximum_cardinality_matching</tt> or
  75. <tt>edmonds_maximum_cardinality_matching</tt>, all three of these
  76. steps are chosen for you, but it's easy to plug in different algorithms for these three steps
  77. using a generic matching function discussed below - in fact, both <tt>checked_edmonds_maximum_cardinality_matching</tt>
  78. and <tt>edmonds_maximum_cardinality_matching</tt> are just inlined specializations of this function.
  79. <p>
  80. When quoting time bounds for algorithms, we assume that <tt>VertexIndexMap</tt> is a property map
  81. that allows for constant-time mapping between vertices and indices (which is easily achieved if,
  82. for instance, the vertices are stored in contiguous memory.) We use <i>n</i> and <i>m</i> to represent the size
  83. of the vertex and edge sets, respectively, of the input graph.
  84. <h4>Algorithms for Creating an Initial Matching</h4>
  85. <ul>
  86. <li><b><tt>empty_matching</tt></b>: Takes time <i>O(n)</i> to initialize the empty matching.
  87. <li><b><tt>greedy_matching</tt></b>: The matching obtained by iterating through the edges and adding an edge
  88. if it doesn't conflict with the edges already in the matching. This matching is maximal, and is therefore
  89. guaranteed to contain at least half of the edges that a maximum matching has. Takes time <i>O(m log n)</i>.
  90. <li><b><tt>extra_greedy_matching</tt></b>: Sorts the edges in increasing order of the degree of the vertices
  91. contained in each edge, then constructs a greedy matching from those edges. Also a maximal matching, and can
  92. sometimes be much closer to the maximum cardinality matching than a simple <tt>greedy_matching</tt>.
  93. Takes time <i>O(m log n)</i>, but the constants involved make this a slower algorithm than
  94. <tt>greedy_matching</tt>.
  95. </ul>
  96. <h4>Algorithms for Finding an Augmenting Path</h4>
  97. <ul>
  98. <li><b><tt>edmonds_augmenting_path_finder</tt></b>: Finds an augmenting path in time <i>O(m alpha(m,n))</i>,
  99. where <i>alpha(m,n)</i> is an inverse of the Ackerman function. <i>alpha(m,n)</i> is one of the slowest
  100. growing functions that occurs naturally in computer science; essentially, <i>alpha(m,n)</i> &le; 4 for any
  101. graph that we'd ever hope to run this algorithm on. Since we arrive at a maximum cardinality matching after
  102. augmenting <i>O(n)</i> matchings, the entire algorithm takes time <i>O(mn alpha(m,n))</i>. Edmonds' original
  103. algorithm appeared in [<a href="bibliography.html#edmonds65">64</a>], but our implementation of
  104. Edmonds' algorithm closely follows Tarjan's
  105. description of the algorithm from [<a href="bibliography.html#tarjan83:_data_struct_network_algo">27</a>].
  106. <li><b><tt>no_augmenting_path_finder</tt></b>: Can be used if no augmentation of the initial matching is desired.
  107. </ul>
  108. <h4>Verification Algorithms</h4>
  109. <ul>
  110. <li><b><tt>maximum_cardinality_matching_verifier</tt></b>: Returns true if and only if the matching found is a
  111. maximum cardinality matching. Takes time <i>O(m alpha(m,n))</i>, which is on the order of a single iteration
  112. of Edmonds' algorithm.
  113. <li><b><tt>no_matching_verifier</tt></b>: Always returns true
  114. </ul>
  115. Why is a verification algorithm needed? Edmonds' algorithm is fairly complex, and it's nearly
  116. impossible for a human without a few days of spare time to figure out if the matching produced by
  117. <tt>edmonds_matching</tt> on a graph with, say, 100 vertices and 500 edges is indeed a maximum cardinality
  118. matching. A verification algorithm can do this mechanically, and it's much easier to verify by inspection
  119. that the verification algorithm has been implemented correctly than it is to verify by inspection that
  120. Edmonds' algorithm has been implemented correctly.
  121. The Boost Graph library makes it incredibly simple to perform the subroutines needed by the verifier
  122. (such as finding all the connected components of odd cardinality in a graph, or creating the induced graph
  123. on all vertices with a certain label) in just a few lines of code.
  124. <p>
  125. Understanding how the verifier works requires a few graph-theoretic facts.
  126. Let <i>m(G)</i> be the size of a maximum cardinality matching in the graph <i>G</i>.
  127. Denote by <i>o(G)</i> the number of connected components in <i>G</i> of odd cardinality, and for a set of
  128. vertices <i>X</i>, denote by <i>G - X</i> the induced graph on the vertex set <i>V(G) - X</i>. Then the
  129. Tutte-Berge Formula says that
  130. <blockquote>
  131. <i>2 * m(G) = min ( |V(G)| + |X| - o(G-X) )</i>
  132. </blockquote>
  133. Where the minimum is taken over all subsets <i>X</i> of the vertex set <i>V(G)</i>. A side effect of the
  134. Edmonds Blossom-Shrinking algorithm is that it computes what is known as the Edmonds-Gallai decomposition
  135. of a graph: it decomposes the graph into three disjoint sets of vertices, one of which achieves the minimum
  136. in the Tutte-Berge Formula.
  137. An outline of our verification procedure is:
  138. Given a <tt>Graph g</tt> and <tt>MateMap mate</tt>,
  139. <ol>
  140. <li>Check to make sure that <tt>mate</tt> is a valid matching on <tt>g</tt>.
  141. <li>Run <tt>edmonds_augmenting_path_finder</tt> once on <tt>g</tt> and <tt>mate</tt>. If it finds an augmenting
  142. path, the matching isn't a maximum cardinality matching. Otherwise, we retrieve a copy of the <tt>vertex_state</tt>
  143. map used by the <tt>edmonds_augmenting_path_finder</tt>. The Edmonds-Gallai decomposition tells us that the set
  144. of vertices labeled <tt>V_ODD</tt> by the <tt>vertex_state</tt> map can be used as the set X to achieve the
  145. minimum in the Tutte-Berge Formula.
  146. <li>Count the number of vertices labeled <tt>V_ODD</tt>, store this in <tt>num_odd_vertices</tt>.
  147. <li>Create a <a href="filtered_graph.html"><tt>filtered_graph</tt></a>
  148. consisting of all vertices that aren't labeled <tt>V_ODD</tt>. Count the number of odd connected components
  149. in this graph and store the result in <tt>num_odd_connected_components</tt>.
  150. <li>Test to see if equality holds in the Tutte-Berge formula using |X| = <tt>num_odd_vertices</tt> and o(G-X) = <tt>num_odd_connected_components</tt>. Return true if it holds, false otherwise.
  151. </ol>
  152. Assuming these steps are implemented correctly, the verifier will never return a false positive,
  153. and will only return a false negative if <tt>edmonds_augmenting_path_finder</tt> doesn't compute the
  154. <tt>vertex_state</tt> map correctly, in which case the <tt>edmonds_augmenting_path_finder</tt>
  155. isn't working correctly.
  156. <h4>Creating Your Own Matching Algorithms</h4>
  157. Creating a matching algorithm is as simple as plugging the algorithms described above into a generic
  158. matching function, which has the following signature:
  159. <pre>
  160. template &lt;typename Graph,
  161. typename MateMap,
  162. typename VertexIndexMap,
  163. template &lt;typename, typename, typename&gt; class AugmentingPathFinder,
  164. template &lt;typename, typename&gt; class InitialMatchingFinder,
  165. template &lt;typename, typename, typename&gt; class MatchingVerifier&gt;
  166. bool matching(const Graph& g, MateMap mate, VertexIndexMap vm)
  167. </pre>
  168. The matching functions provided for you are just inlined specializations of this function:
  169. <tt>edmonds_maximum_cardinality_matching</tt> uses <tt>edmonds_augmenting_path_finder</tt>
  170. as the <tt>AugmentingPathFinder</tt>, <tt>extra_greedy_matching</tt> as the <tt>InitialMatchingFinder</tt>,
  171. and <tt>no_matching_verifier</tt> as the <tt>MatchingVerifier</tt>.
  172. <tt>checked_edmonds_maximum_cardinality_matching</tt> uses the same parameters except that
  173. <tt>maximum_cardinality_matching_verifier</tt> is used for the <tt>MatchingVerifier</tt>.
  174. <p>
  175. These aren't necessarily the best choices for any situation - for example, it's been claimed in the literature
  176. that for sparse graphs, Edmonds' algorithm converges to the maximum cardinality matching more quickly if it
  177. isn't supplied with an intitial matching. Such an algorithm can be easily assembled by calling <tt>matching</tt> with
  178. <ul>
  179. <li><tt>AugmentingPathFinder = edmonds_augmenting_path_finder</tt>
  180. <li><tt>InitialMatchingFinder = empty_matching</tt>
  181. </ul>
  182. and choosing the <tt>MatchingVerifier</tt> depending on how careful you're feeling.
  183. <p>
  184. Suppose instead that you want a relatively large matching quickly, but are not exactly interested in a maximum matching.
  185. Both extra_greedy_matching and greedy_matching find maximal matchings, which means they're guaranteed to be at
  186. least half the size of a maximum cardinality matching, so you could call <tt>matching</tt> with
  187. <ul>
  188. <li><tt>AugmentingPathFinder = no_augmenting_path_finder</tt>
  189. <li><tt>InitialMatchingFinder = extra_greedy_matching</tt>
  190. <li><tt>MatchingVerifier = maximum_cardinality_matching_verifier</tt>
  191. </ul>
  192. The resulting algorithm will find an extra greedy matching in time <i>O(m log n)</i> without looking for
  193. augmenting paths. As a bonus, the return value of this function is true if and only if the extra greedy
  194. matching happens to be a maximum cardinality matching.
  195. </p><h3>Where Defined</h3>
  196. <p>
  197. <a href="../../../boost/graph/max_cardinality_matching.hpp"><tt>boost/graph/max_cardinality_matching.hpp</tt></a>
  198. </p><h3>Parameters</h3>
  199. IN: <tt>const Graph&amp; g</tt>
  200. <blockquote>
  201. An undirected graph. The graph type must be a model of
  202. <a href="VertexAndEdgeListGraph.html">Vertex and Edge List Graph</a> and
  203. <a href="IncidenceGraph.html">Incidence Graph</a>.<br>
  204. </blockquote>
  205. IN: <tt>VertexIndexMap vm</tt>
  206. <blockquote>
  207. Must be a model of <a href="../../property_map/doc/ReadablePropertyMap.html">ReadablePropertyMap</a>, mapping vertices to integer indices.
  208. </blockquote>
  209. OUT: <tt>MateMap mate</tt>
  210. <blockquote>
  211. Must be a model of <a href="../../property_map/doc/ReadWritePropertyMap.html">ReadWritePropertyMap</a>, mapping
  212. vertices to vertices. For any vertex v in the graph, <tt>get(mate,v)</tt> will be the vertex that v is matched to, or
  213. <tt>graph_traits<Graph>::null_vertex()</tt> if v isn't matched.
  214. </blockquote>
  215. <h3>Complexity</h3>
  216. <p>
  217. Let <i>m</i> and <i>n</i> be the number of edges and vertices in the input graph, respectively. Assuming the
  218. <tt>VertexIndexMap</tt> supplied allows constant-time lookups, the time complexity for both
  219. <tt>edmonds_matching</tt> and <tt>checked_edmonds_matching</tt> is <i>O(mn alpha(m,n))</i>.
  220. <i>alpha(m,n)</i> is a slow growing function that is at most 4 for any feasible input.
  221. </p><p>
  222. </p><h3>Example</h3>
  223. <p> The file <a href="../example/matching_example.cpp"><tt>example/matching_example.cpp</tt></a>
  224. contains an example.
  225. <br>
  226. </p><hr>
  227. <table>
  228. <tbody><tr valign="top">
  229. <td nowrap="nowrap">Copyright © 2005</td><td>
  230. Aaron Windsor (<a href="mailto:aaron.windsor@gmail.com">aaron.windsor@gmail.com</a>)<br>
  231. </td></tr></tbody></table>
  232. </body></html>