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 <boost/config.hpp>
33 #include <iostream>
34 #include <string>
35 #include <boost/graph/adjacency_list.hpp>
36 #include <boost/graph/boykov_kolmogorov_max_flow.hpp>
37 #include <boost/graph/read_dimacs.hpp>
38 #include <boost/graph/graph_utility.hpp>
39
40 // Use a DIMACS network flow file as stdin.
41 // boykov_kolmogorov-eg < max_flow.dat
42 //
43 // Sample output:
44 // c The total flow:
45 // s 13
46 //
47 // c flow values:
48 // f 0 6 3
49 // f 0 1 6
50 // f 0 2 4
51 // f 1 5 1
52 // f 1 0 0
53 // f 1 3 5
54 // f 2 4 4
55 // f 2 3 0
56 // f 2 0 0
57 // f 3 7 5
58 // f 3 2 0
59 // f 3 1 0
60 // f 4 5 0
61 // f 4 6 4
62 // f 5 4 0
63 // f 5 7 1
64 // f 6 7 7
65 // f 6 4 0
66 // f 7 6 0
67 // f 7 5 0
68
main()69 int main()
70 {
71 using namespace boost;
72
73 typedef adjacency_list_traits< vecS, vecS, directedS > Traits;
74 typedef adjacency_list< vecS, vecS, directedS,
75 property< vertex_name_t, std::string,
76 property< vertex_index_t, long,
77 property< vertex_color_t, boost::default_color_type,
78 property< vertex_distance_t, long,
79 property< vertex_predecessor_t,
80 Traits::edge_descriptor > > > > >,
81
82 property< edge_capacity_t, long,
83 property< edge_residual_capacity_t, long,
84 property< edge_reverse_t, Traits::edge_descriptor > > > >
85 Graph;
86
87 Graph g;
88 property_map< Graph, edge_capacity_t >::type capacity
89 = get(edge_capacity, g);
90 property_map< Graph, edge_residual_capacity_t >::type residual_capacity
91 = get(edge_residual_capacity, g);
92 property_map< Graph, edge_reverse_t >::type rev = get(edge_reverse, g);
93 Traits::vertex_descriptor s, t;
94 read_dimacs_max_flow(g, capacity, rev, s, t);
95
96 std::vector< default_color_type > color(num_vertices(g));
97 std::vector< long > distance(num_vertices(g));
98 long flow = boykov_kolmogorov_max_flow(g, s, t);
99
100 std::cout << "c The total flow:" << std::endl;
101 std::cout << "s " << flow << std::endl << std::endl;
102
103 std::cout << "c flow values:" << std::endl;
104 graph_traits< Graph >::vertex_iterator u_iter, u_end;
105 graph_traits< Graph >::out_edge_iterator ei, e_end;
106 for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
107 for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
108 if (capacity[*ei] > 0)
109 std::cout << "f " << *u_iter << " " << target(*ei, g) << " "
110 << (capacity[*ei] - residual_capacity[*ei])
111 << std::endl;
112
113 return EXIT_SUCCESS;
114 }
115