maximum_weighted_matching.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <html><head><!--
  2. Copyright 2018 Yi Ji
  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: Yi Ji
  7. --><title>Boost Graph Library: Maximum Weighted 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_weighted_matching">Maximum Weighted Matching</a>
  13. </h1>
  14. <pre>
  15. template &lt;typename Graph, typename MateMap&gt;
  16. void maximum_weighted_matching(const Graph&amp; g, MateMap mate);
  17. template &lt;typename Graph, typename MateMap, typename VertexIndexMap&gt;
  18. void maximum_weighted_matching(const Graph&amp; g, MateMap mate, VertexIndexMap vm);
  19. template &lt;typename Graph, typename MateMap&gt;
  20. void brute_force_maximum_weighted_matching(const Graph&amp; g, MateMap mate);
  21. template &lt;typename Graph, typename MateMap, typename VertexIndexMap&gt;
  22. void brute_force_maximum_weighted_matching(const Graph&amp; g, MateMap mate, VertexIndexMap vm);
  23. </pre>
  24. <p>
  25. <a name="sec:weighted_matching">Before you continue, it is recommended to read
  26. about <a href="./maximum_matching.html">maximal cardinality matching</a> first.
  27. A <i>maximum weighted matching</i> of an edge-weighted graph is a matching
  28. for which the sum of the weights of the edges is maximum.
  29. Two different matchings (edges in the matching are colored blue) in the same graph are illustrated below.
  30. The matching on the left is a maximum cardinality matching of size 8 and a maximal
  31. weighted matching of weight sum 30, meaning that is has maximum size over all matchings in the graph
  32. and its weight sum can't be increased by adding edges.
  33. The matching on the right is a maximum weighted matching of size 7 and weight sum 38, meaning that it has maximum
  34. weight sum over all matchings in the graph.
  35. </a></p><p></p><center>
  36. <table border="0">
  37. <tr>
  38. <td><a name="fig:maximal_weighted_matching"><img src="figs/maximal-weighted-match.png"></a></td>
  39. <td width="150"></td>
  40. <td><a name="fig:maximum_weighted_matching"><img src="figs/maximum-weighted-match.png"></a></td>
  41. </tr>
  42. </table>
  43. </center>
  44. <p>
  45. Both <tt>maximum_weighted_matching</tt> and
  46. <tt>brute_force_maximum_weighted_matching</tt> find a
  47. maximum weighted matching in any undirected graph. The matching is returned in a
  48. <tt>MateMap</tt>, which is a
  49. <a href="../../property_map/doc/ReadWritePropertyMap.html">ReadWritePropertyMap</a>
  50. that maps vertices to vertices. In the mapping returned, each vertex is either mapped
  51. to the vertex it's matched to, or to <tt>graph_traits&lt;Graph&gt;::null_vertex()</tt> if it
  52. doesn't participate in the matching. If no <tt>VertexIndexMap</tt> is provided, both functions
  53. assume that the <tt>VertexIndexMap</tt> is provided as an internal graph property accessible
  54. by calling <tt>get(vertex_index, g)</tt>.
  55. <p>
  56. The maximum weighted matching problem was solved by Edmonds in [<a href="bibliography.html#edmonds65:_max_weighted_match">74</a>].
  57. The implementation of <tt>maximum_weighted_matching</tt> followed Chapter 6, Section 10 of [<a href="bibliography.html#lawler76:_comb_opt">20</a>] and
  58. was written in a consistent style with <tt>edmonds_maximum_cardinality_matching</tt> because of their algorithmic similarity.
  59. In addition, a brute-force verifier <tt>brute_force_maximum_weighted_matching</tt> simply searches all possible matchings in any graph and selects one with the maximum weight sum.
  60. </p><h3>Algorithm Description</h3>
  61. Primal-dual method in linear programming is introduced to solve weighted matching problems. Edmonds proved that for any graph,
  62. the maximum number of edges in a matching is equal to the minimum capacity of an odd-set cover; this further enable us to prove a max-min duality theorem for weighted matching.
  63. Let <i>H<sub>k-1</sub></i> denote any graph obtained from G by contracting odd sets of three or more nodes and deleting single nodes,
  64. where the capacity of the family of odd sets (not necessarily a cover of G) is <i>k-1</i>. Let <i>X<sub>k</sub></i> denote any matching containing <i>k</i> edges.
  65. Each edge <i>(i, j)</i> has a weight <i>w<sub>ij</sub></i>. We have:
  66. <math>
  67. max<i><sub>X<sub>k</sub></sub></i> min {<i>w<sub>ij</sub>|(i, j) ϵ X<sub>k</sub></i>} = min<i><sub>H<sub>k-1</sub></sub></i> max {<i>w<sub>ij</sub>|(i, j) ϵ H<sub>k-1</sub></i>}.
  68. </math>
  69. This matching duality theorem gives an indication of how the matching problem should be formulated as a linear programming problem. That is,
  70. the theorem suggests a set of linear inequalities which are satisfied by any matching, and it is anticipated that these inequalities describe a convex polyhedron
  71. with integer vertices corresponding to feasible matchings.
  72. <p>
  73. For <tt>maximum_weighted_matching</tt>, the management of blossoms is much more involved than in the case of <tt>max_cardinality_matching</tt>.
  74. It is not sufficient to record only the outermost blossoms. When an outermost blossom is expanded,
  75. it is necessary to know which blossom are nested immediately with it, so that these blossoms can be restored to the status of the outermost blossoms.
  76. When augmentation occurs, blossoms with strictly positive dual variables must be maintained for use in the next application of the labeling procedure.
  77. <p>
  78. The outline of the algorithm is as follow:
  79. <ol start="0">
  80. <li>Start with an empty matching and initialize dual variables as a half of maximum edge weight.</li>
  81. <li>(Labeling) Root an alternate tree at each exposed node, and proceed to construct alternate trees by labeling, using only edges with zero slack value.
  82. If an augmenting path is found, go to step 2. If a blossom is formed, go to step 3. Otherwise, go to step 4. </li>
  83. <li>(Augmentation) Find the augmenting path, tracing the path through shrunken blossoms. Augment the matching,
  84. correct labels on nodes in the augmenting path, expand blossoms with zero dual variables and remove labels from all base nodes. Go to step 1.</li>
  85. <li>(Blossoming) Determine the membership and base node of the new blossom and supply missing labels for all non-base nodes in the blossom.
  86. Return to step 1.</li>
  87. <li>(Revision of Dual Solution) Adjust the dual variables based on the primal-dual method. Go to step 1 or halt, accordingly.</li>
  88. </ol>
  89. Note that in <tt>maximum_weighted_matching</tt>, all edge weights are multiplied by 4, so that all dual variables always remain as integers if all edge weights are integers.
  90. Unlike <tt>max_cardinality_matching</tt>, the initial matching and augmenting path finder are not parameterized,
  91. because the algorithm maintains blossoms, dual variables and node labels across all augmentations.
  92. The algorithm's time complexity is reduced from <i>O(V<sup>4</sup>)</i> (naive implementation of [<a href="bibliography.html#edmonds65:_max_weighted_match">74</a>])
  93. to <i>O(V<sup>3</sup>)</i>, by a delicate labeling procedure [<a href="bibliography.html#gabow76">75</a>] to avoid re-scanning labels after revision of the dual solution.
  94. Special variables <i>pi, tau, gamma</i> and two arrays <i>critical_edge, tau_idx</i> are introduced for this purpose.
  95. Please refer to [<a href="bibliography.html#lawler76:_comb_opt">20</a>] and code comments for more implementation details.
  96. </p><h3>Where Defined</h3>
  97. <p>
  98. <a href="../../../boost/graph/maximum_weighted_matching.hpp"><tt>boost/graph/maximum_weighted_matching.hpp</tt></a>
  99. </p><h3>Parameters</h3>
  100. IN: <tt>const Graph&amp; g</tt>
  101. <blockquote>
  102. An undirected graph. The graph type must be a model of
  103. <a href="VertexAndEdgeListGraph.html">Vertex and Edge List Graph</a> and
  104. <a href="IncidenceGraph.html">Incidence Graph</a>.
  105. The edge property of the graph <tt>property_map&lt;Graph, edge_weight_t&gt;</tt> must exist and have numeric value type.<br>
  106. </blockquote>
  107. IN: <tt>VertexIndexMap vm</tt>
  108. <blockquote>
  109. Must be a model of <a href="../../property_map/doc/ReadablePropertyMap.html">ReadablePropertyMap</a>, mapping vertices to integer indices.
  110. </blockquote>
  111. OUT: <tt>MateMap mate</tt>
  112. <blockquote>
  113. Must be a model of <a href="../../property_map/doc/ReadWritePropertyMap.html">ReadWritePropertyMap</a>, mapping
  114. 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
  115. <tt>graph_traits<Graph>::null_vertex()</tt> if v isn't matched.
  116. </blockquote>
  117. <h3>Complexity</h3>
  118. <p>
  119. Let <i>m</i> and <i>n</i> be the number of edges and vertices in the input graph, respectively. Assuming the
  120. <tt>VertexIndexMap</tt> supplied allows constant-time lookup, the time complexity for
  121. <tt>maximum_weighted_matching</tt> is <i>O(n<sup>3</sup>)</i>. For <tt>brute_force_maximum_weighted_matching</tt>, the time complexity is exponential of <i>m</i>.
  122. Note that the best known time complexity for maximum weighted matching in general graph
  123. is <i>O(nm+n<sup>2</sup>log(n))</i> by [<a href="bibliography.html#gabow90">76</a>], but relies on an
  124. efficient algorithm for solving nearest ancestor problem on trees, which is not provided in Boost C++ libraries.
  125. </p><p>
  126. </p><h3>Example</h3>
  127. <p> The file <a href="../example/weighted_matching_example.cpp"><tt>example/weighted_matching_example.cpp</tt></a>
  128. contains an example.
  129. <br>
  130. </p><hr>
  131. <table>
  132. <tbody><tr valign="top">
  133. <td nowrap="nowrap">Copyright © 2018</td><td>
  134. Yi Ji (<a href="mailto:jiy@pku.edu.cn">jiy@pku.edu.cn</a>)<br>
  135. </td></tr></tbody></table>
  136. </body></html>