boykov_kolmogorov-eg.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2006, Stephan Diederich
  2. //
  3. // This code may be used under either of the following two licences:
  4. //
  5. // Permission is hereby granted, free of charge, to any person
  6. // obtaining a copy of this software and associated documentation
  7. // files (the "Software"), to deal in the Software without
  8. // restriction, including without limitation the rights to use,
  9. // copy, modify, merge, publish, distribute, sublicense, and/or
  10. // sell copies of the Software, and to permit persons to whom the
  11. // Software is furnished to do so, subject to the following
  12. // conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. // OTHER DEALINGS IN THE SOFTWARE. OF SUCH DAMAGE.
  25. //
  26. // Or:
  27. //
  28. // Distributed under the Boost Software License, Version 1.0.
  29. // (See accompanying file LICENSE_1_0.txt or copy at
  30. // http://www.boost.org/LICENSE_1_0.txt)
  31. #include <boost/config.hpp>
  32. #include <iostream>
  33. #include <string>
  34. #include <boost/graph/adjacency_list.hpp>
  35. #include <boost/graph/boykov_kolmogorov_max_flow.hpp>
  36. #include <boost/graph/read_dimacs.hpp>
  37. #include <boost/graph/graph_utility.hpp>
  38. // Use a DIMACS network flow file as stdin.
  39. // boykov_kolmogorov-eg < max_flow.dat
  40. //
  41. // Sample output:
  42. // c The total flow:
  43. // s 13
  44. //
  45. // c flow values:
  46. // f 0 6 3
  47. // f 0 1 6
  48. // f 0 2 4
  49. // f 1 5 1
  50. // f 1 0 0
  51. // f 1 3 5
  52. // f 2 4 4
  53. // f 2 3 0
  54. // f 2 0 0
  55. // f 3 7 5
  56. // f 3 2 0
  57. // f 3 1 0
  58. // f 4 5 0
  59. // f 4 6 4
  60. // f 5 4 0
  61. // f 5 7 1
  62. // f 6 7 7
  63. // f 6 4 0
  64. // f 7 6 0
  65. // f 7 5 0
  66. int main()
  67. {
  68. using namespace boost;
  69. typedef adjacency_list_traits < vecS, vecS, directedS > Traits;
  70. typedef adjacency_list < vecS, vecS, directedS,
  71. property < vertex_name_t, std::string,
  72. property < vertex_index_t, long,
  73. property < vertex_color_t, boost::default_color_type,
  74. property < vertex_distance_t, long,
  75. property < vertex_predecessor_t, Traits::edge_descriptor > > > > >,
  76. property < edge_capacity_t, long,
  77. property < edge_residual_capacity_t, long,
  78. property < edge_reverse_t, Traits::edge_descriptor > > > > Graph;
  79. Graph g;
  80. property_map < Graph, edge_capacity_t >::type
  81. capacity = get(edge_capacity, g);
  82. property_map < Graph, edge_residual_capacity_t >::type
  83. residual_capacity = get(edge_residual_capacity, g);
  84. property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g);
  85. Traits::vertex_descriptor s, t;
  86. read_dimacs_max_flow(g, capacity, rev, s, t);
  87. std::vector<default_color_type> color(num_vertices(g));
  88. std::vector<long> distance(num_vertices(g));
  89. long flow = boykov_kolmogorov_max_flow(g ,s, t);
  90. std::cout << "c The total flow:" << std::endl;
  91. std::cout << "s " << flow << std::endl << std::endl;
  92. std::cout << "c flow values:" << std::endl;
  93. graph_traits < Graph >::vertex_iterator u_iter, u_end;
  94. graph_traits < Graph >::out_edge_iterator ei, e_end;
  95. for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
  96. for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
  97. if (capacity[*ei] > 0)
  98. std::cout << "f " << *u_iter << " " << target(*ei, g) << " "
  99. << (capacity[*ei] - residual_capacity[*ei]) << std::endl;
  100. return EXIT_SUCCESS;
  101. }