cycle_canceling.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <HTML>
  2. <!--
  3. Copyright (c) Piotr Wygocki 2013
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <Head>
  9. <Title>Boost Graph Library: Cycle Canceling for Min Cost Max Flow</Title>
  10. <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
  11. ALINK="#ff0000">
  12. <IMG SRC="../../../boost.png"
  13. ALT="C++ Boost" width="277" height="86">
  14. <BR Clear>
  15. <H1><A NAME="sec:cycle_canceling">
  16. <TT>cycle_canceling</TT>
  17. </H1>
  18. <PRE>
  19. <i>// named parameter version</i>
  20. template &lt;class <a href="./Graph.html">Graph</a>, class P, class T, class R&gt;
  21. void cycle_canceling(
  22. Graph &amp;g,
  23. const bgl_named_params&lt;P, T, R&gt; &amp; params = <i>all defaults</i>)
  24. <i>// non-named parameter version</i>
  25. template &lt;class <a href="./Graph.html">Graph</a>, class Pred, class Distance, class Reversed, class ResidualCapacity, class Weight&gt;
  26. void cycle_canceling(const Graph &amp; g, Weight weight, Reversed rev, ResidualCapacity residual_capacity, Pred pred, Distance distance)
  27. </PRE>
  28. <P>
  29. The <tt>cycle_canceling()</tt> function calculates the minimum cost flow of a network with given flow. See Section <a
  30. href="./graph_theory_review.html#sec:network-flow-algorithms">Network
  31. Flow Algorithms</a> for a description of maximum flow.
  32. For given flow values <i> f(u,v)</i> function minimizes flow cost in such a way, that for each <i>v in V</i> the
  33. <i> sum<sub> u in V</sub> f(v,u) </i> is preserved. Particularly if the input flow was the maximum flow, the function produces min cost max flow.
  34. The function calculates the flow values <i>f(u,v)</i> for all <i>(u,v)</i> in
  35. <i>E</i>, which are returned in the form of the residual capacity
  36. <i>r(u,v) = c(u,v) - f(u,v)</i>.
  37. <p>
  38. There are several special requirements on the input graph and property
  39. map parameters for this algorithm. First, the directed graph
  40. <i>G=(V,E)</i> that represents the network must be augmented to
  41. include the reverse edge for every edge in <i>E</i>. That is, the
  42. input graph should be <i>G<sub>in</sub> = (V,{E U
  43. E<sup>T</sup>})</i>. The <tt>ReverseEdgeMap</tt> argument <tt>rev</tt>
  44. must map each edge in the original graph to its reverse edge, that is
  45. <i>(u,v) -> (v,u)</i> for all <i>(u,v)</i> in <i>E</i>.
  46. The <tt>WeightMap</tt> has to map each edge from <i>E<sup>T</sup></i> to <i>-weight</i> of its reversed edge.
  47. Note that edges from <i>E</i> can have negative weights.
  48. <p>
  49. If weights in the graph are nonnegative, the
  50. <a href="./successive_shortest_path_nonnegative_weights.html"><tt>successive_shortest_path_nonnegative_weights()</tt></a>
  51. might be better choice for min cost max flow.
  52. <p>
  53. The algorithm is described in <a
  54. href="./bibliography.html#ahuja93:_network_flows">Network Flows</a>.
  55. <p>
  56. In each round algorithm augments the negative cycle (in terms of weight) in the residual graph.
  57. If there is no negative cycle in the network, the cost is optimized.
  58. <p>
  59. Note that, although we mention capacity in the problem description, the actual algorithm doesn't have to now it.
  60. <p>
  61. In order to find the cost of the result flow use:
  62. <a href="./find_flow_cost.html"><tt>find_flow_cost()</tt></a>.
  63. <H3>Where Defined</H3>
  64. <P>
  65. <a href="../../../boost/graph/successive_shortest_path_nonnegative_weights.hpp"><TT>boost/graph/successive_shortest_path_nonnegative_weights.hpp</TT></a>
  66. <P>
  67. <h3>Parameters</h3>
  68. IN: <tt>Graph&amp; g</tt>
  69. <blockquote>
  70. A directed graph. The
  71. graph's type must be a model of <a
  72. href="./VertexListGraph.html">VertexListGraph</a> and <a href="./IncidenceGraph.html">IncidenceGraph</a> For each edge
  73. <i>(u,v)</i> in the graph, the reverse edge <i>(v,u)</i> must also
  74. be in the graph.
  75. </blockquote>
  76. <h3>Named Parameters</h3>
  77. IN/OUT: <tt>residual_capacity_map(ResidualCapacityEdgeMap res)</tt>
  78. <blockquote>
  79. This maps edges to their residual capacity. The type must be a model
  80. of a mutable <a
  81. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property
  82. Map</a>. The key type of the map must be the graph's edge descriptor
  83. type.<br>
  84. <b>Default:</b> <tt>get(edge_residual_capacity, g)</tt>
  85. </blockquote>
  86. IN: <tt>reverse_edge_map(ReverseEdgeMap rev)</tt>
  87. <blockquote>
  88. An edge property map that maps every edge <i>(u,v)</i> in the graph
  89. to the reverse edge <i>(v,u)</i>. The map must be a model of
  90. constant <a href="../../property_map/doc/LvaluePropertyMap.html">Lvalue
  91. Property Map</a>. The key type of the map must be the graph's edge
  92. descriptor type.<br>
  93. <b>Default:</b> <tt>get(edge_reverse, g)</tt>
  94. </blockquote>
  95. IN: <tt>weight_map(WeightMap w)</tt>
  96. <blockquote>
  97. The weight (also know as ``length'' or ``cost'') of each edge in the
  98. graph. The <tt>WeightMap</tt> type must be a model of <a
  99. href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  100. Map</a>. The key type for this property map must be the edge
  101. descriptor of the graph. The value type for the weight map must be
  102. <i>Addable</i> with the distance map's value type. <br>
  103. <b>Default:</b> <tt>get(edge_weight, g)</tt><br>
  104. </blockquote>
  105. UTIL: <tt>predecessor_map(PredEdgeMap pred)</tt>
  106. <blockquote>
  107. Use by the algorithm to store augmenting paths. The map must be a
  108. model of mutable <a
  109. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property Map</a>.
  110. The key type must be the graph's vertex descriptor type and the
  111. value type must be the graph's edge descriptor type.<br>
  112. <b>Default:</b> an <a
  113. href="../../property_map/doc/iterator_property_map.html">
  114. <tt>iterator_property_map</tt></a> created from a <tt>std::vector</tt>
  115. of edge descriptors of size <tt>num_vertices(g)</tt> and
  116. using the <tt>i_map</tt> for the index map.
  117. </blockquote>
  118. UTIL: <tt>distance_map(DistanceMap d_map)</tt>
  119. <blockquote>
  120. The shortest path weight from the source vertex <tt>s</tt> to each
  121. vertex in the graph <tt>g</tt> is recorded in this property map. The
  122. shortest path weight is the sum of the edge weights along the
  123. shortest path. The type <tt>DistanceMap</tt> must be a model of <a
  124. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  125. Property Map</a>. The vertex descriptor type of the graph needs to
  126. be usable as the key type of the distance map.
  127. <b>Default:</b> <a
  128. href="../../property_map/doc/iterator_property_map.html">
  129. <tt>iterator_property_map</tt></a> created from a
  130. <tt>std::vector</tt> of the <tt>WeightMap</tt>'s value type of size
  131. <tt>num_vertices(g)</tt> and using the <tt>i_map</tt> for the index
  132. map.<br>
  133. </blockquote>
  134. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  135. <blockquote>
  136. Maps each vertex of the graph to a unique integer in the range
  137. <tt>[0, num_vertices(g))</tt>. This property map is only needed
  138. if the default for the distance or predecessor map is used.
  139. The vertex index map must be a model of <a
  140. href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  141. Map</a>. The key type of the map must be the graph's vertex
  142. descriptor type.<br>
  143. <b>Default:</b> <tt>get(vertex_index, g)</tt>
  144. Note: if you use this default, make sure your graph has
  145. an internal <tt>vertex_index</tt> property. For example,
  146. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  147. not have an internal <tt>vertex_index</tt> property.
  148. </blockquote>
  149. <h3>Complexity</h3>
  150. In the integer capacity and weight case, if <i>C</i> is the initial cost of the flow, then the complexity is <i> O(C * |V| * |E|)</i>,
  151. where <i>O(|E|* |V|)</i> is the complexity of the bellman ford shortest paths algorithm and <i>C</i> is upper bound on number of iteration.
  152. In many real world cases number of iterations is much smaller than <i>C</i>.
  153. <h3>Example</h3>
  154. The program in <a
  155. href="../example/cycle_canceling_example.cpp"><tt>example/cycle_canceling_example.cpp</tt></a>.
  156. <h3>See Also</h3>
  157. <a href="./successive_shortest_path_nonnegative_weights.html"><tt>successive_shortest_path_nonnegative_weights()</tt></a><br>
  158. <a href="./find_flow_cost.html"><tt>find_flow_cost()</tt></a>.
  159. <br>
  160. <HR>
  161. <TABLE>
  162. <TR valign=top>
  163. <TD nowrap>Copyright &copy; 2013</TD><TD>
  164. Piotr Wygocki, University of Warsaw (<A HREF="mailto:wygos@mimuw.edu.pl">wygos at mimuw.edu.pl</A>)
  165. </TD></TR></TABLE>
  166. </BODY>
  167. </HTML>
  168. <!-- LocalWords: HTML Siek Edmonds BGCOLOR ffffff ee VLINK ALINK ff IMG SRC
  169. -->
  170. <!-- LocalWords: gif ALT BR sec edmonds karp TT DIV CELLPADDING TR TD PRE lt
  171. -->
  172. <!-- LocalWords: typename VertexListGraph CapacityEdgeMap ReverseEdgeMap gt
  173. -->
  174. <!-- LocalWords: ResidualCapacityEdgeMap VertexIndexMap src rev ColorMap pred
  175. -->
  176. <!-- LocalWords: PredEdgeMap tt href html hpp ul li nbsp br LvaluePropertyMap
  177. -->
  178. <!-- LocalWords: num ColorValue DIMACS cpp pre config iostream dimacs int std
  179. -->
  180. <!-- LocalWords: namespace vecS directedS cout endl iter ei HR valign nowrap
  181. -->
  182. <!-- LocalWords: jeremy siek htm Univ mailto jsiek lsc edu
  183. p -->