graph_push_relabel.cc 3.16 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2015 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "graph_filtering.hh"
#include "graph_selectors.hh"
#include "graph_properties.hh"
#include "graph.hh"

#include "graph_augment.hh"

#include <boost/mpl/if.hpp>
#include <boost/mpl/or.hpp>
namespace std
{
using namespace boost;

// we need a min() function with arguments of different types

template <class T1, class T2>
Tiago Peixoto's avatar
Tiago Peixoto committed
34
35
36
typename boost::mpl::if_<
    typename boost::mpl::or_<typename std::is_floating_point<T1>::type,
                             typename std::is_floating_point<T2>::type>::type,
Tiago Peixoto's avatar
Tiago Peixoto committed
37
38
39
    double, int>::type
min(const T1& v1, const T2& v2)
{
40
    if (v1 <= T1(v2))
Tiago Peixoto's avatar
Tiago Peixoto committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        return v1;
    else
        return v2;
}
}

#include <boost/graph/push_relabel_max_flow.hpp>

#include <boost/bind.hpp>

using namespace graph_tool;
using namespace boost;



struct get_push_relabel_max_flow
{
58
    template <class Graph, class VertexIndex, class EdgeIndex, class CapacityMap,
Tiago Peixoto's avatar
Tiago Peixoto committed
59
              class ResidualMap>
60
61
62
    void operator()(Graph& g, VertexIndex vertex_index, EdgeIndex edge_index,
                    size_t max_e, size_t src, size_t sink, CapacityMap cm,
                    ResidualMap res) const
Tiago Peixoto's avatar
Tiago Peixoto committed
63
64
    {
        typedef typename graph_traits<Graph>::edge_descriptor edge_t;
65
66
        checked_vector_property_map<bool,EdgeIndex>
            augmented(edge_index);
Tiago Peixoto's avatar
Tiago Peixoto committed
67
68
        unchecked_vector_property_map<edge_t,EdgeIndex>
            reverse_map(edge_index, max_e);
69

Tiago Peixoto's avatar
Tiago Peixoto committed
70
        typedef typename std::remove_const<Graph>::type GT;
71
72
73
        GT& u = const_cast<GT&>(g);

        augment_graph(u, augmented, cm,
Tiago Peixoto's avatar
Tiago Peixoto committed
74
                      reverse_map.get_checked(), res);
75

76
        boost::push_relabel_max_flow(g, vertex(src, g), vertex(sink, g),
77
78
79
                                     get_unchecked(cm),
                                     res.get_unchecked(),
                                     reverse_map, vertex_index);
80
        deaugment_graph(u, augmented);
Tiago Peixoto's avatar
Tiago Peixoto committed
81
82
83
84
85
86
87
    }
};


void push_relabel_max_flow(GraphInterface& gi, size_t src, size_t sink,
                           boost::any capacity, boost::any res)
{
Tiago Peixoto's avatar
Tiago Peixoto committed
88
89
    run_action<graph_tool::detail::always_directed, boost::mpl::true_>()
        (gi, std::bind(get_push_relabel_max_flow(),
90
                       placeholders::_1, gi.get_vertex_index(), gi.get_edge_index(),
91
                       gi.get_edge_index_range(),
Tiago Peixoto's avatar
Tiago Peixoto committed
92
                       src, sink,  placeholders::_2,  placeholders::_3),
93
         writable_edge_scalar_properties(), writable_edge_scalar_properties())
Tiago Peixoto's avatar
Tiago Peixoto committed
94
95
        (capacity,res);
}