1 // Copyright (c) 2006, Stephan Diederich
3 // This code may be used under either of the following two licences:
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
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
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.
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)
33 #define _CRT_SECURE_NO_WARNINGS
35 #include <boost/config.hpp>
38 #include <boost/graph/adjacency_list.hpp>
39 #include <boost/graph/read_dimacs.hpp>
40 #include <boost/graph/write_dimacs.hpp>
43 /*************************************
45 * example which reads in a max-flow problem from std::cin, augments all paths from
46 * source->NODE->sink and writes the graph back to std::cout
48 **************************************/
50 template <typename EdgeCapacityMap
>
51 struct zero_edge_capacity
{
53 zero_edge_capacity() { }
54 zero_edge_capacity(EdgeCapacityMap cap_map
):m_cap_map(cap_map
){};
56 template <typename Edge
>
57 bool operator() (const Edge
& e
) const {
58 return get(m_cap_map
, e
) == 0 ;
61 EdgeCapacityMap m_cap_map
;
66 using namespace boost
;
67 typedef adjacency_list_traits
< vecS
, vecS
, directedS
> Traits
;
68 typedef adjacency_list
< vecS
, vecS
, directedS
,
70 property
< edge_capacity_t
, long,
71 property
< edge_reverse_t
, Traits::edge_descriptor
> > > Graph
;
73 typedef graph_traits
<Graph
>::out_edge_iterator out_edge_iterator
;
74 typedef graph_traits
<Graph
>::edge_descriptor edge_descriptor
;
75 typedef graph_traits
<Graph
>::vertex_descriptor vertex_descriptor
;
79 typedef property_map
< Graph
, edge_capacity_t
>::type tCapMap
;
80 typedef tCapMap::value_type tCapMapValue
;
82 typedef property_map
< Graph
, edge_reverse_t
>::type tRevEdgeMap
;
84 tCapMap capacity
= get(edge_capacity
, g
);
85 tRevEdgeMap rev
= get(edge_reverse
, g
);
87 vertex_descriptor s
, t
;
88 /*reading the graph from stdin*/
89 read_dimacs_max_flow(g
, capacity
, rev
, s
, t
, std::cin
);
92 tCapMapValue augmented_flow
= 0;
94 //we take the source node and check for each outgoing edge e which has a target(p) if we can augment that path
95 out_edge_iterator oei
,oe_end
;
96 for(boost::tie(oei
, oe_end
) = out_edges(s
, g
); oei
!= oe_end
; ++oei
){
97 edge_descriptor from_source
= *oei
;
98 vertex_descriptor v
= target(from_source
, g
);
99 edge_descriptor to_sink
;
101 boost::tie(to_sink
, is_there
) = edge(v
, t
, g
);
103 if( get(capacity
, to_sink
) > get(capacity
, from_source
) ){
104 tCapMapValue to_augment
= get(capacity
, from_source
);
105 capacity
[from_source
] = 0;
106 capacity
[to_sink
] -= to_augment
;
107 augmented_flow
+= to_augment
;
109 tCapMapValue to_augment
= get(capacity
, to_sink
);
110 capacity
[to_sink
] = 0;
111 capacity
[from_source
] -= to_augment
;
112 augmented_flow
+= to_augment
;
117 //remove edges with zero capacity (most of them are the reverse edges)
118 zero_edge_capacity
<tCapMap
> filter(capacity
);
119 remove_edge_if(filter
, g
);
121 /*write the graph back to stdout */
122 write_dimacs_max_flow(g
, capacity
, identity_property_map(),s
, t
, std::cout
);
123 //print flow we augmented to std::cerr
124 std::cerr
<< "removed " << augmented_flow
<< " from SOURCE->NODE->SINK connects" <<std::endl
;