graph_augment.hh 3.1 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-2017 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
//
// 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/>.

#ifndef GRAPH_AUGMENT_HH
#define GRAPH_AUGMENT_HH

namespace graph_tool
{
using namespace std;
using namespace boost;

template <class Graph, class AugmentedMap, class CapacityMap,
          class ReversedMap,  class ResidualMap>
void augment_graph(Graph& g, AugmentedMap augmented, CapacityMap capacity,
29
                   ReversedMap rmap, ResidualMap res,
30
                   bool detect_reversed=false)
Tiago Peixoto's avatar
Tiago Peixoto committed
31
32
{
    vector<typename graph_traits<Graph>::edge_descriptor> e_list;
33
34
35
    for (auto e : edges_range(g))
        augmented[e] = false;
    for (auto e : edges_range(g))
Tiago Peixoto's avatar
Tiago Peixoto committed
36
    {
37
        if (!detect_reversed)
Tiago Peixoto's avatar
Tiago Peixoto committed
38
        {
39
40
41
42
43
            e_list.push_back(e);
        }
        else
        {
            for (auto ae : out_edges_range(target(e, g), g))
44
            {
45
                if (target(ae, g) == source(e, g) && augmented[e] == false)
46
                {
47
48
49
50
                    augmented[e] = augmented[ae] = 2;
                    rmap[e] = ae;
                    rmap[ae] = e;
                    break;
51
52
                }
            }
53
            if (augmented[e] == false)
54
                e_list.push_back(e);
Tiago Peixoto's avatar
Tiago Peixoto committed
55
        }
56
    }
Tiago Peixoto's avatar
Tiago Peixoto committed
57

58
59
60
61
62
63
64
65
    for (auto& e : e_list)
    {
        auto ae = add_edge(target(e, g), source(e, g), g).first;
        augmented[ae] = true;
        capacity[ae] = 0;
        rmap[e] = ae;
        rmap[ae] = e;
        res[ae] = 0;
Tiago Peixoto's avatar
Tiago Peixoto committed
66
67
68
69
70
71
72
    }
}

template <class Graph, class AugmentedMap>
void deaugment_graph(Graph& g, AugmentedMap augmented)
{
    vector<typename graph_traits<Graph>::edge_descriptor> e_list;
73
    for (auto v : vertices_range(g))
Tiago Peixoto's avatar
Tiago Peixoto committed
74
75
    {
        e_list.clear();
76
        for (auto e : out_edges_range(v, g))
Tiago Peixoto's avatar
Tiago Peixoto committed
77
        {
78
            if (augmented[e] == true)
79
                e_list.push_back(e);
Tiago Peixoto's avatar
Tiago Peixoto committed
80
81
        }

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        for (auto& e : e_list)
            remove_edge(e, g);
    }
}


template <class Graph, class CapacityMap, class ResidualMap,
          class AugmentedMap>
void residual_graph(Graph& g, CapacityMap capacity, ResidualMap res,
                    AugmentedMap augmented)
{
    vector<typename graph_traits<Graph>::edge_descriptor> e_list;
    for (auto e : edges_range(g))
    {
        if (capacity[e] - res[e] > 0)
            e_list.push_back(e);
    }

    for (auto& e : e_list)
    {
        auto ne = add_edge(target(e, g), source(e, g), g);
        augmented[ne.first] = true;
Tiago Peixoto's avatar
Tiago Peixoto committed
104
105
106
107
108
109
    }
}

} // graph_tool namespace

#endif // GRAPH_AUGMENT_HH