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