push_relabel_max_flow.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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: Push-Relabel 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:push_relabel_max_flow">
  16. <TT>push_relabel_max_flow</TT>
  17. </H1>
  18. <P>
  19. <PRE>
  20. <i>// named parameter version</i>
  21. template &lt;class Graph, class P, class T, class R&gt;
  22. typename property_traits&lt;CapacityEdgeMap&gt;::value_type
  23. push_relabel_max_flow(Graph&amp; g,
  24. typename graph_traits&lt;Graph&gt;::vertex_descriptor src,
  25. typename graph_traits&lt;Graph&gt;::vertex_descriptor sink,
  26. const bgl_named_params&lt;P, T, R&gt;&amp; params = <i>all defaults</i>)
  27. <i>// non-named parameter version</i>
  28. template &lt;class Graph,
  29. class CapacityEdgeMap, class ResidualCapacityEdgeMap,
  30. class ReverseEdgeMap, class VertexIndexMap&gt;
  31. typename property_traits&lt;CapacityEdgeMap&gt;::value_type
  32. push_relabel_max_flow(Graph&amp; g,
  33. typename graph_traits&lt;Graph&gt;::vertex_descriptor src,
  34. typename graph_traits&lt;Graph&gt;::vertex_descriptor sink,
  35. CapacityEdgeMap cap, ResidualCapacityEdgeMap res,
  36. ReverseEdgeMap rev, VertexIndexMap index_map)
  37. </PRE>
  38. <P>
  39. The <tt>push_relabel_max_flow()</tt> function calculates the maximum flow
  40. of a network. See Section <a
  41. href="./graph_theory_review.html#sec:network-flow-algorithms">Network
  42. Flow Algorithms</a> for a description of maximum flow. The calculated
  43. maximum flow will be the return value of the function. The function
  44. also calculates the flow values <i>f(u,v)</i> for all <i>(u,v)</i> in
  45. <i>E</i>, which are returned in the form of the residual capacity
  46. <i>r(u,v) = c(u,v) - f(u,v)</i>.
  47. <p>
  48. There are several special requirements on the input graph and property
  49. map parameters for this algorithm. First, the directed graph
  50. <i>G=(V,E)</i> that represents the network must be augmented to
  51. include the reverse edge for every edge in <i>E</i>. That is, the
  52. input graph should be <i>G<sub>in</sub> = (V,{E U
  53. E<sup>T</sup>})</i>. The <tt>ReverseEdgeMap</tt> argument <tt>rev</tt>
  54. must map each edge in the original graph to its reverse edge, that is
  55. <i>(u,v) -> (v,u)</i> for all <i>(u,v)</i> in <i>E</i>. The
  56. <tt>CapacityEdgeMap</tt> argument <tt>cap</tt> must map each edge in
  57. <i>E</i> to a positive number, and each edge in <i>E<sup>T</sup></i>
  58. to 0.
  59. <p>
  60. This algorithm was developed by <a
  61. href="./bibliography.html#goldberg85:_new_max_flow_algor">Goldberg</a>.
  62. <H3>Complexity</H3>
  63. The time complexity is <i>O(V<sup>3</sup>)</i>.
  64. <H3>Where Defined</H3>
  65. <P>
  66. <a href="../../../boost/graph/push_relabel_max_flow.hpp"><TT>boost/graph/push_relabel_max_flow.hpp</TT></a>
  67. <P>
  68. <h3>Parameters</h3>
  69. IN: <tt>VertexListGraph&amp; g</tt>
  70. <blockquote>
  71. A directed graph. The
  72. graph's type must be a model of <a
  73. href="./VertexListGraph.html">Vertex List Graph</a>. For each edge
  74. <i>(u,v)</i> in the graph, the reverse edge <i>(v,u)</i> must also
  75. be in the graph.
  76. </blockquote>
  77. IN: <tt>vertex_descriptor src</tt>
  78. <blockquote>
  79. The source vertex for the flow network graph.
  80. </blockquote>
  81. IN: <tt>vertex_descriptor sink</tt>
  82. <blockquote>
  83. The sink vertex for the flow network graph.
  84. </blockquote>
  85. <h3>Named Parameters</h3>
  86. IN: <tt>capacity_map(EdgeCapacityMap cap)</tt>
  87. <blockquote>
  88. The edge capacity property map. The type must be a model of a
  89. constant <a
  90. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property Map</a>. The
  91. key type of the map must be the graph's edge descriptor type.<br>
  92. <b>Default:</b> <tt>get(edge_capacity, g)</tt>
  93. </blockquote>
  94. OUT: <tt>residual_capacity_map(ResidualCapacityEdgeMap res)</tt>
  95. <blockquote>
  96. The edge residual capacity property map. The type must be a model of
  97. a mutable <a
  98. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property Map</a>. The
  99. key type of the map must be the graph's edge descriptor type.<br>
  100. <b>Default:</b> <tt>get(edge_residual_capacity, g)</tt>
  101. </blockquote>
  102. IN: <tt>reverse_edge_map(ReverseEdgeMap rev)</tt>
  103. <blockquote>
  104. An edge property map that maps every edge <i>(u,v)</i> in the graph
  105. to the reverse edge <i>(v,u)</i>. The map must be a model of
  106. constant <a
  107. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property Map</a>. The
  108. key type of the map must be the graph's edge descriptor type.<br>
  109. <b>Default:</b> <tt>get(edge_reverse, g)</tt>
  110. </blockquote>
  111. IN: <tt>vertex_index_map(VertexIndexMap index_map)</tt>
  112. <blockquote>
  113. Maps each vertex of the graph to a unique integer in the range
  114. <tt>[0, num_vertices(g))</tt>. The map must be a model of constant <a
  115. href="../../property_map/doc/LvaluePropertyMap.html">LvaluePropertyMap</a>. The
  116. key type of the map must be the graph's vertex descriptor type.<br>
  117. <b>Default:</b> <tt>get(vertex_index, g)</tt>
  118. Note: if you use this default, make sure your graph has
  119. an internal <tt>vertex_index</tt> property. For example,
  120. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  121. not have an internal <tt>vertex_index</tt> property.
  122. <br>
  123. </blockquote>
  124. <h3>Example</h3>
  125. This reads in an example maximum flow problem (a graph with edge
  126. capacities) from a file in the DIMACS format. The source for this
  127. example can be found in <a
  128. href="../example/max_flow.cpp"><tt>example/max_flow.cpp</tt></a>.
  129. <pre>
  130. #include &lt;boost/config.hpp&gt;
  131. #include &lt;iostream&gt;
  132. #include &lt;string&gt;
  133. #include &lt;boost/graph/push_relabel_max_flow.hpp&gt;
  134. #include &lt;boost/graph/adjacency_list.hpp&gt;
  135. #include &lt;boost/graph/read_dimacs.hpp&gt;
  136. int
  137. main()
  138. {
  139. using namespace boost;
  140. typedef adjacency_list_traits&lt;vecS, vecS, directedS&gt; Traits;
  141. typedef adjacency_list&lt;vecS, vecS, directedS,
  142. property&lt;vertex_name_t, std::string&gt;,
  143. property&lt;edge_capacity_t, long,
  144. property&lt;edge_residual_capacity_t, long,
  145. property&lt;edge_reverse_t, Traits::edge_descriptor&gt; &gt; &gt;
  146. &gt; Graph;
  147. Graph g;
  148. long flow;
  149. property_map&lt;Graph, edge_capacity_t&gt;::type
  150. capacity = get(edge_capacity, g);
  151. property_map&lt;Graph, edge_reverse_t&gt;::type
  152. rev = get(edge_reverse, g);
  153. property_map&lt;Graph, edge_residual_capacity_t&gt;::type
  154. residual_capacity = get(edge_residual_capacity, g);
  155. Traits::vertex_descriptor s, t;
  156. read_dimacs_max_flow(g, capacity, rev, s, t);
  157. flow = push_relabel_max_flow(g, s, t);
  158. std::cout &lt;&lt; "c The total flow:" &lt;&lt; std::endl;
  159. std::cout &lt;&lt; "s " &lt;&lt; flow &lt;&lt; std::endl &lt;&lt; std::endl;
  160. std::cout &lt;&lt; "c flow values:" &lt;&lt; std::endl;
  161. graph_traits&lt;Graph&gt;::vertex_iterator u_iter, u_end;
  162. graph_traits&lt;Graph&gt;::out_edge_iterator ei, e_end;
  163. for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
  164. for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
  165. if (capacity[*ei] &gt; 0)
  166. std::cout &lt;&lt; "f " &lt;&lt; *u_iter &lt;&lt; " " &lt;&lt; target(*ei, g) &lt;&lt; " "
  167. &lt;&lt; (capacity[*ei] - residual_capacity[*ei]) &lt;&lt; std::endl;
  168. return 0;
  169. }
  170. </pre>
  171. The output is:
  172. <pre>
  173. c The total flow:
  174. s 4
  175. c flow values:
  176. f 0 1 4
  177. f 1 2 4
  178. f 2 3 2
  179. f 2 4 2
  180. f 3 1 0
  181. f 3 6 2
  182. f 4 5 3
  183. f 5 6 0
  184. f 5 7 3
  185. f 6 4 1
  186. f 6 7 1
  187. </pre>
  188. <h3>See Also</h3>
  189. <a href="./edmonds_karp_max_flow.html"><tt>edmonds_karp_max_flow()</tt></a><br>
  190. <a href="./boykov_kolmogorov_max_flow.html"><tt>boykov_kolmogorov_max_flow()</tt></a>.
  191. <br>
  192. <HR>
  193. <TABLE>
  194. <TR valign=top>
  195. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  196. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>, Indiana University (<A HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)
  197. </TD></TR></TABLE>
  198. </BODY>
  199. </HTML>
  200. <!-- LocalWords: HTML Siek BGCOLOR ffffff ee VLINK ALINK ff IMG SRC preflow
  201. -->
  202. <!-- LocalWords: gif ALT BR sec TT DIV CELLPADDING TR TD PRE lt
  203. -->
  204. <!-- LocalWords: typename VertexListGraph CapacityEdgeMap ReverseEdgeMap gt
  205. -->
  206. <!-- LocalWords: ResidualCapacityEdgeMap VertexIndexMap src rev ColorMap pred
  207. -->
  208. <!-- LocalWords: PredEdgeMap tt href html hpp ul li nbsp br LvaluePropertyMap
  209. -->
  210. <!-- LocalWords: num ColorValue DIMACS cpp pre config iostream dimacs int std
  211. -->
  212. <!-- LocalWords: namespace vecS directedS cout endl iter ei HR valign nowrap
  213. -->
  214. <!-- LocalWords: jeremy siek htm Univ mailto jsiek lsc edu
  215. -->