max_flow_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <vector>
  32. #include <iterator>
  33. #include <iostream>
  34. #include <algorithm>
  35. #include <fstream>
  36. #include <boost/test/minimal.hpp>
  37. //three max_flows we test here
  38. #include <boost/graph/boykov_kolmogorov_max_flow.hpp>
  39. #include <boost/graph/push_relabel_max_flow.hpp>
  40. #include <boost/graph/edmonds_karp_max_flow.hpp>
  41. //boost utilities we use
  42. #include <boost/graph/adjacency_list.hpp>
  43. #include <boost/graph/random.hpp>
  44. #include <boost/property_map/property_map.hpp>
  45. #include <boost/random/linear_congruential.hpp>
  46. #include <boost/lexical_cast.hpp>
  47. /***************
  48. * test which compares results of the three different max_flow implementations
  49. * command line parameters:
  50. * number_of_vertices: defaults to 100
  51. * number_of_edges: defaults to 1000
  52. * seeed: defaults to 1
  53. ***************/
  54. using namespace boost;
  55. int test_main(int argc, char* argv[])
  56. {
  57. typedef adjacency_list_traits < vecS, vecS, directedS > Traits;
  58. typedef adjacency_list < vecS, vecS, directedS,
  59. property < vertex_index_t, long,
  60. property < vertex_color_t, boost::default_color_type,
  61. property < vertex_distance_t, long,
  62. property < vertex_predecessor_t, Traits::edge_descriptor > > > >,
  63. property < edge_capacity_t, long,
  64. property < edge_residual_capacity_t, long,
  65. property < edge_reverse_t, Traits::edge_descriptor > > > > Graph;
  66. typedef graph_traits<Graph>::edge_descriptor tEdge;
  67. typedef graph_traits<Graph>::vertex_descriptor tVertex;
  68. graph_traits<Graph>::vertices_size_type n_verts = 100;
  69. graph_traits<Graph>::edges_size_type n_edges = 1000;
  70. std::size_t seed = 1;
  71. if (argc > 1) n_verts = lexical_cast<std::size_t>(argv[1]);
  72. if (argc > 2) n_edges = lexical_cast<std::size_t>(argv[2]);
  73. if (argc > 3) seed = lexical_cast<std::size_t>(argv[3]);
  74. Graph g;
  75. const int cap_low = 1;
  76. const int cap_high = 1000;
  77. //init random numer generator
  78. minstd_rand gen(seed);
  79. //generate graph
  80. generate_random_graph(g, n_verts, n_edges, gen);
  81. //init an uniform distribution int generator
  82. typedef variate_generator<minstd_rand, uniform_int<int> > tIntGen;
  83. tIntGen int_gen(gen, uniform_int<int>(cap_low, cap_high));
  84. //init edge-capacities
  85. randomize_property<edge_capacity_t, Graph, tIntGen> (g,int_gen);
  86. //get source and sink node
  87. tVertex source_vertex = random_vertex(g, gen);
  88. tVertex sink_vertex = graph_traits<Graph>::null_vertex();
  89. while(sink_vertex == graph_traits<Graph>::null_vertex() || sink_vertex == source_vertex)
  90. sink_vertex = random_vertex(g, gen);
  91. //add reverse edges (ugly... how to do better?!)
  92. property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g);
  93. property_map < Graph, edge_capacity_t >::type cap = get(edge_capacity, g);
  94. std::list<tEdge> edges_copy;
  95. graph_traits<Graph>::edge_iterator ei, e_end;
  96. boost::tie(ei, e_end) = edges(g);
  97. std::copy(ei, e_end, std::back_insert_iterator< std::list<tEdge> >(edges_copy));
  98. while( ! edges_copy.empty()){
  99. tEdge old_edge=edges_copy.front();
  100. edges_copy.pop_front();
  101. tVertex source_vertex = target(old_edge, g);
  102. tVertex target_vertex = source(old_edge, g);
  103. bool inserted;
  104. tEdge new_edge;
  105. boost::tie(new_edge,inserted) = add_edge(source_vertex, target_vertex, g);
  106. assert(inserted);
  107. rev[old_edge] = new_edge;
  108. rev[new_edge] = old_edge ;
  109. cap[new_edge] = 0;
  110. }
  111. typedef property_traits< property_map<Graph, edge_capacity_t>::const_type>::value_type tEdgeVal;
  112. tEdgeVal bk = boykov_kolmogorov_max_flow(g,source_vertex,sink_vertex);
  113. tEdgeVal push_relabel = push_relabel_max_flow(g,source_vertex,sink_vertex);
  114. tEdgeVal edmonds_karp = edmonds_karp_max_flow(g,source_vertex,sink_vertex);
  115. BOOST_REQUIRE( bk == push_relabel );
  116. BOOST_REQUIRE( push_relabel == edmonds_karp );
  117. return 0;
  118. }