edmonds_karp_max_flow.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek 2000
  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: Edmonds-Karp Maximum 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:edmonds_karp_max_flow">
  16. <TT>edmonds_karp_max_flow</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. typename detail::edge_capacity_value&lt;Graph, P, T, R&gt;::value_type
  22. edmonds_karp_max_flow(Graph& g,
  23. typename graph_traits&lt;Graph&gt;::vertex_descriptor src,
  24. typename graph_traits&lt;Graph&gt;::vertex_descriptor sink,
  25. const bgl_named_params&lt;P, T, R&gt;&amp; params = <i>all defaults</i>)
  26. <i>// non-named parameter version</i>
  27. template &lt;class <a href="./Graph.html">Graph</a>,
  28. class CapacityEdgeMap, class ResidualCapacityEdgeMap,
  29. class ReverseEdgeMap, class ColorMap, class PredEdgeMap&gt;
  30. typename property_traits&lt;CapacityEdgeMap&gt;::value_type
  31. edmonds_karp_max_flow(Graph&amp; g,
  32. typename graph_traits&lt;Graph&gt;::vertex_descriptor src,
  33. typename graph_traits&lt;Graph&gt;::vertex_descriptor sink,
  34. CapacityEdgeMap cap, ResidualCapacityEdgeMap res, ReverseEdgeMap rev,
  35. ColorMap color, PredEdgeMap pred)
  36. </PRE>
  37. <P>
  38. The <tt>edmonds_karp_max_flow()</tt> function calculates the maximum flow
  39. of a network. See Section <a
  40. href="./graph_theory_review.html#sec:network-flow-algorithms">Network
  41. Flow Algorithms</a> for a description of maximum flow. The calculated
  42. maximum flow will be the return value of the function. The function
  43. also calculates the flow values <i>f(u,v)</i> for all <i>(u,v)</i> in
  44. <i>E</i>, which are returned in the form of the residual capacity
  45. <i>r(u,v) = c(u,v) - f(u,v)</i>.
  46. <p>
  47. There are several special requirements on the input graph and property
  48. map parameters for this algorithm. First, the directed graph
  49. <i>G=(V,E)</i> that represents the network must be augmented to
  50. include the reverse edge for every edge in <i>E</i>. That is, the
  51. input graph should be <i>G<sub>in</sub> = (V,{E U
  52. E<sup>T</sup>})</i>. The <tt>ReverseEdgeMap</tt> argument <tt>rev</tt>
  53. must map each edge in the original graph to its reverse edge, that is
  54. <i>(u,v) -> (v,u)</i> for all <i>(u,v)</i> in <i>E</i>. The
  55. <tt>CapacityEdgeMap</tt> argument <tt>cap</tt> must map each edge in
  56. <i>E</i> to a positive number, and each edge in <i>E<sup>T</sup></i>
  57. to 0.
  58. <p>
  59. The algorithm is due to <a
  60. href="./bibliography.html#edmonds72:_improvements_netflow">Edmonds and
  61. Karp</a>, though we are using the variation called the ``labeling
  62. algorithm'' described in <a
  63. href="./bibliography.html#ahuja93:_network_flows">Network Flows</a>.
  64. <p>
  65. This algorithm provides a very simple and easy to implement solution to
  66. the maximum flow problem. However, there are several reasons why this
  67. algorithm is not as good as the <a
  68. href="./push_relabel_max_flow.html"><tt>push_relabel_max_flow()</tt></a>
  69. or the <a
  70. href="./boykov_kolmogorov_max_flow.html"><tt>boykov_kolmogorov_max_flow()</tt></a>
  71. algorithm.
  72. <ul>
  73. <li>In the non-integer capacity case, the time complexity is <i>O(V
  74. E<sup>2</sup>)</i> which is worse than the time complexity of the
  75. push-relabel algorithm <i>O(V<sup>2</sup>E<sup>1/2</sup>)</i>
  76. for all but the sparsest of graphs.</li>
  77. <li>In the integer capacity case, if the capacity bound <i>U</i> is
  78. very large then the algorithm will take a long time.</li>
  79. </ul>
  80. <H3>Where Defined</H3>
  81. <P>
  82. <a href="../../../boost/graph/edmonds_karp_max_flow.hpp"><TT>boost/graph/edmonds_karp_max_flow.hpp</TT></a>
  83. <P>
  84. <h3>Parameters</h3>
  85. IN: <tt>Graph&amp; g</tt>
  86. <blockquote>
  87. A directed graph. The
  88. graph's type must be a model of <a
  89. href="./VertexListGraph.html">VertexListGraph</a> and <a href="./IncidenceGraph.html">IncidenceGraph</a> For each edge
  90. <i>(u,v)</i> in the graph, the reverse edge <i>(v,u)</i> must also
  91. be in the graph.
  92. </blockquote>
  93. IN: <tt>vertex_descriptor src</tt>
  94. <blockquote>
  95. The source vertex for the flow network graph.
  96. </blockquote>
  97. IN: <tt>vertex_descriptor sink</tt>
  98. <blockquote>
  99. The sink vertex for the flow network graph.
  100. </blockquote>
  101. <h3>Named Parameters</h3>
  102. IN: <tt>capacity_map(CapacityEdgeMap cap)</tt>
  103. <blockquote>
  104. The edge capacity property map. The type must be a model of a
  105. constant <a
  106. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property Map</a>. The
  107. key type of the map must be the graph's edge descriptor type.<br>
  108. <b>Default:</b> <tt>get(edge_capacity, g)</tt>
  109. </blockquote>
  110. OUT: <tt>residual_capacity_map(ResidualCapacityEdgeMap res)</tt>
  111. <blockquote>
  112. This maps edges to their residual capacity. The type must be a model
  113. of a mutable <a
  114. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property
  115. Map</a>. The key type of the map must be the graph's edge descriptor
  116. type.<br>
  117. <b>Default:</b> <tt>get(edge_residual_capacity, g)</tt>
  118. </blockquote>
  119. IN: <tt>reverse_edge_map(ReverseEdgeMap rev)</tt>
  120. <blockquote>
  121. An edge property map that maps every edge <i>(u,v)</i> in the graph
  122. to the reverse edge <i>(v,u)</i>. The map must be a model of
  123. constant <a href="../../property_map/doc/LvaluePropertyMap.html">Lvalue
  124. Property Map</a>. The key type of the map must be the graph's edge
  125. descriptor type.<br>
  126. <b>Default:</b> <tt>get(edge_reverse, g)</tt>
  127. </blockquote>
  128. UTIL: <tt>color_map(ColorMap color)</tt>
  129. <blockquote>
  130. Used by the algorithm to keep track of progress during the
  131. breadth-first search stage. At the end of the algorithm, the white
  132. vertices define the minimum cut set. The map must be a model of
  133. mutable <a
  134. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property Map</a>.
  135. The key type of the map should be the graph's vertex descriptor type, and
  136. the value type must be a model of <a
  137. href="./ColorValue.html">ColorValue</a>.<br>
  138. <b>Default:</b> an <a
  139. href="../../property_map/doc/iterator_property_map.html">
  140. <tt>iterator_property_map</tt></a> created from a <tt>std::vector</tt>
  141. of <tt>default_color_type</tt> of size <tt>num_vertices(g)</tt> and
  142. using the <tt>i_map</tt> for the index map.
  143. </blockquote>
  144. UTIL: <tt>predecessor_map(PredEdgeMap pred)</tt>
  145. <blockquote>
  146. Use by the algorithm to store augmenting paths. The map must be a
  147. model of mutable <a
  148. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property Map</a>.
  149. The key type must be the graph's vertex descriptor type and the
  150. value type must be the graph's edge descriptor type.<br>
  151. <b>Default:</b> an <a
  152. href="../../property_map/doc/iterator_property_map.html">
  153. <tt>iterator_property_map</tt></a> created from a <tt>std::vector</tt>
  154. of edge descriptors of size <tt>num_vertices(g)</tt> and
  155. using the <tt>i_map</tt> for the index map.
  156. </blockquote>
  157. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  158. <blockquote>
  159. Maps each vertex of the graph to a unique integer in the range
  160. <tt>[0, num_vertices(g))</tt>. This property map is only needed
  161. if the default for the color or predecessor map is used.
  162. The vertex index map must be a model of <a
  163. href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  164. Map</a>. The key type of the map must be the graph's vertex
  165. descriptor type.<br>
  166. <b>Default:</b> <tt>get(vertex_index, g)</tt>
  167. Note: if you use this default, make sure your graph has
  168. an internal <tt>vertex_index</tt> property. For example,
  169. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  170. not have an internal <tt>vertex_index</tt> property.
  171. </blockquote>
  172. <h3>Complexity</h3>
  173. The time complexity is <i>O(V E<sup>2</sup>)</i> in the general case
  174. or <i>O(V E U)</i> if capacity values are integers bounded by
  175. some constant <i>U</i>.
  176. <h3>Example</h3>
  177. The program in <a
  178. href="../example/edmonds-karp-eg.cpp"><tt>example/edmonds-karp-eg.cpp</tt></a>
  179. reads an example maximum flow problem (a graph with edge capacities)
  180. from a file in the DIMACS format and computes the maximum flow.
  181. <h3>See Also</h3>
  182. <a href="./push_relabel_max_flow.html"><tt>push_relabel_max_flow()</tt></a><br>
  183. <a href="./boykov_kolmogorov_max_flow.html"><tt>boykov_kolmogorov_max_flow()</tt></a>.
  184. <br>
  185. <HR>
  186. <TABLE>
  187. <TR valign=top>
  188. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  189. <A HREF="http://www.boost.org/users/people/jeremy_siek.html">Jeremy Siek</A>, Indiana University (<A HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)
  190. </TD></TR></TABLE>
  191. </BODY>
  192. </HTML>
  193. <!-- LocalWords: HTML Siek Edmonds BGCOLOR ffffff ee VLINK ALINK ff IMG SRC
  194. -->
  195. <!-- LocalWords: gif ALT BR sec edmonds karp TT DIV CELLPADDING TR TD PRE lt
  196. -->
  197. <!-- LocalWords: typename VertexListGraph CapacityEdgeMap ReverseEdgeMap gt
  198. -->
  199. <!-- LocalWords: ResidualCapacityEdgeMap VertexIndexMap src rev ColorMap pred
  200. -->
  201. <!-- LocalWords: PredEdgeMap tt href html hpp ul li nbsp br LvaluePropertyMap
  202. -->
  203. <!-- LocalWords: num ColorValue DIMACS cpp pre config iostream dimacs int std
  204. -->
  205. <!-- LocalWords: namespace vecS directedS cout endl iter ei HR valign nowrap
  206. -->
  207. <!-- LocalWords: jeremy siek htm Univ mailto jsiek lsc edu
  208. p -->