All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
min_cut.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_MIN_CUT_HPP
16 #define PAAL_MIN_CUT_HPP
17 
18 #include <boost/graph/adjacency_list.hpp>
19 #include <boost/graph/boykov_kolmogorov_max_flow.hpp>
20 
21 #include <numeric>
22 
23 namespace paal {
24 namespace ir {
25 
32  using Traits = boost::adjacency_list_traits<boost::vecS, boost::vecS, boost::directedS>;
33 public:
34  using Edge = Traits::edge_descriptor;
35  using Vertex = Traits::vertex_descriptor;
36 
39  : m_graph(0), m_cap(get(boost::edge_capacity, m_graph)),
40  m_rev(get(boost::edge_reverse, m_graph)),
41  m_colors(get(boost::vertex_color, m_graph)) {}
42 
46  void init(int vertices_num) {
47  m_graph.clear();
48  for (int i = 0; i < vertices_num; ++i) {
50  }
51  m_cap = get(boost::edge_capacity, m_graph);
52  m_rev = get(boost::edge_reverse, m_graph);
53  }
54 
58  Vertex add_vertex_to_graph() { return add_vertex(m_graph); }
59 
69  std::pair<Edge, Edge>
70  add_edge_to_graph(Vertex src, Vertex trg, double cap, double rev_cap = 0.) {
71  bool b, b_rev;
72  Edge e, e_rev;
73 
74  std::tie(e, b) = add_edge(src, trg, m_graph);
75  std::tie(e_rev, b_rev) = add_edge(trg, src, m_graph);
76  assert(b && b_rev);
77 
78  put(m_cap, e, cap);
79  put(m_cap, e_rev, rev_cap);
80 
81  put(m_rev, e, e_rev);
82  put(m_rev, e_rev, e);
83 
84  return std::make_pair(e, e_rev);
85  }
86 
95  double find_min_cut(Vertex src, Vertex trg) {
96  assert(src != trg);
97  double min_cut_val = boost::boykov_kolmogorov_max_flow(m_graph, src, trg);
98  m_colors = get(boost::vertex_color, m_graph);
99  m_src_color = get(m_colors, src);
100  m_last_cut = std::make_pair(src, trg);
101  assert(!is_in_source_set(trg));
102  return min_cut_val;
103  }
104 
109  bool is_in_source_set(Vertex v) const {
110  return (m_src_color == get(m_colors, v));
111  }
112 
117  int source_set_size() const {
118  auto verts = vertices(m_graph);
119  return std::accumulate(verts.first, verts.second, 0,
120  [&](int count, Vertex v) {
121  return count + is_in_source_set(v);
122  });
123  }
124 
128  std::pair<Vertex, Vertex> get_last_cut() const { return m_last_cut; }
129 
133  double get_capacity(Edge e) const { return get(m_cap, e); }
134 
138  void set_capacity(Edge e, double cap) {
139  put(m_cap, e, cap);
140  }
141 
145  void set_capacity(Vertex src, Vertex trg, double cap) {
146  auto e = edge(src, trg, m_graph);
147  assert(e.second);
148  set_capacity(e.first, cap);
149  }
150 
151 private:
152  using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
153  boost::property<boost::vertex_color_t, boost::default_color_type,
154  boost::property<boost::vertex_distance_t, long,
155  boost::property<boost::vertex_predecessor_t, Edge>>>,
156  boost::property<boost::edge_capacity_t, double,
157  boost::property<boost::edge_residual_capacity_t, double,
158  boost::property<boost::edge_reverse_t, Edge>>>>;
159  using EdgeCapacity = boost::property_map<Graph, boost::edge_capacity_t>::type;
160  using EdgeReverse = boost::property_map<Graph, boost::edge_reverse_t>::type;
161  using VertexColors = boost::property_map<Graph, boost::vertex_color_t>::type;
162 
163  Graph m_graph;
164 
165  EdgeCapacity m_cap;
166  EdgeReverse m_rev;
167  VertexColors m_colors;
168 
169  boost::default_color_type m_src_color;
170  std::pair<Vertex, Vertex> m_last_cut;
171 };
172 
173 }
174 }
175 #endif // PAAL_MIN_CUT_HPP
std::pair< Edge, Edge > add_edge_to_graph(Vertex src, Vertex trg, double cap, double rev_cap=0.)
Definition: min_cut.hpp:70
bool is_in_source_set(Vertex v) const
Definition: min_cut.hpp:109
void set_capacity(Edge e, double cap)
Definition: min_cut.hpp:138
Class for creating and modifying directed graphs with edge capacities and finding directed minimum cu...
Definition: min_cut.hpp:31
void set_capacity(Vertex src, Vertex trg, double cap)
Definition: min_cut.hpp:145
double find_min_cut(Vertex src, Vertex trg)
Definition: min_cut.hpp:95
double get_capacity(Edge e) const
Definition: min_cut.hpp:133
min_cut_finder()
Constructor.
Definition: min_cut.hpp:38
Vertex add_vertex_to_graph()
Definition: min_cut.hpp:58
int source_set_size() const
Definition: min_cut.hpp:117
void init(int vertices_num)
Definition: min_cut.hpp:46
std::pair< Vertex, Vertex > get_last_cut() const
Definition: min_cut.hpp:128