graph_augment.hh 3.6 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
// Copyright (C) 2008  Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//
// 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,
27
28
                   ReversedMap rmap, ResidualMap res,
                   bool detect_reversed = false)
Tiago Peixoto's avatar
Tiago Peixoto committed
29
30
31
{
    typename graph_traits<Graph>::edge_iterator e, e_end;
    for (tie(e,e_end) = edges(g); e != e_end; ++e)
32
        augmented[*e] = 0;
Tiago Peixoto's avatar
Tiago Peixoto committed
33
34
35
36
37
38
39
40
41
42

    typename graph_traits<Graph>::vertex_iterator v, v_end;
    vector<typename graph_traits<Graph>::edge_descriptor> e_list;
    for (tie(v,v_end) = vertices(g); v != v_end; ++v)
    {
        e_list.clear();

        typename graph_traits<Graph>::out_edge_iterator e, e_end;
        for (tie(e, e_end) = out_edges(*v, g); e != e_end; ++e)
        {
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
            if (detect_reversed && augmented[*e] != 0)
            {
                typename graph_traits<Graph>::out_edge_iterator e2, e2_end;
                for (tie(e2, e2_end) = out_edges(target(*e, g), g);
                     e2 != e2_end; ++e2)
                {
                    if (augmented[*e2] != 0)
                        continue;

                    if (target(*e2, g) == *v)
                    {
                        augmented[*e] = 2;
                        augmented[*e2] = 2;
                        rmap[*e] = *e2;
                        rmap[*e2] = *e;
                        break;
                    }
                }
            }

            if (augmented[*e] == 0)
Tiago Peixoto's avatar
Tiago Peixoto committed
64
65
66
67
68
69
70
                e_list.push_back(*e);
        }

        for (size_t i = 0; i < e_list.size(); ++i)
        {
            typename graph_traits<Graph>::edge_descriptor ae;
            ae = add_edge(target(e_list[i],g), source(e_list[i],g), g).first;
71
            augmented[ae] = 1;
Tiago Peixoto's avatar
Tiago Peixoto committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
            capacity[ae] = 0;
            rmap[e_list[i]] = ae;
            rmap[ae] = e_list[i];
            res[ae] = 0;
        }
    }
}

template <class Graph, class AugmentedMap>
void deaugment_graph(Graph& g, AugmentedMap augmented)
{
    vector<typename graph_traits<Graph>::edge_descriptor> e_list;
    typename graph_traits<Graph>::vertex_iterator v, v_end;
    for (tie(v,v_end) = vertices(g); v != v_end; ++v)
    {
        e_list.clear();
        typename graph_traits<Graph>::out_edge_iterator e, e_end;
        for (tie(e, e_end) = out_edges(*v, g); e != e_end; ++e)
        {
91
            if (augmented[*e] == 1)
Tiago Peixoto's avatar
Tiago Peixoto committed
92
93
94
95
                e_list.push_back(*e);
        }

        for (size_t i = 0; i < e_list.size(); ++i)
96
            remove_edge(e_list[i], g);
Tiago Peixoto's avatar
Tiago Peixoto committed
97
98
99
100
101
    }
}


template <class Type, class Index>
Tiago Peixoto's avatar
Tiago Peixoto committed
102
103
unchecked_vector_property_map<Type, Index>
get_unchecked(checked_vector_property_map<Type, Index> prop)
Tiago Peixoto's avatar
Tiago Peixoto committed
104
{
Tiago Peixoto's avatar
Tiago Peixoto committed
105
    return prop.get_unchecked();
Tiago Peixoto's avatar
Tiago Peixoto committed
106
107
108
109
}

template <class Prop>
Prop
Tiago Peixoto's avatar
Tiago Peixoto committed
110
get_unchecked(Prop prop)
Tiago Peixoto's avatar
Tiago Peixoto committed
111
112
113
114
115
116
117
{
    return prop;
}

} // graph_tool namespace

#endif // GRAPH_AUGMENT_HH