Aleph-w  1.5a.2
Biblioteca general de algoritmos y estructuras de datos
 Todo Clases Archivos Funciones Variables 'typedefs' Enumeraciones Amigas Grupos Páginas
tpl_maxflow_mincost.H
1 # ifndef TPL_MAXFLOW_MINCOST_H
2 # define TPL_MAXFLOW_MINCOST_H
3 
4 # include <Bellman_Ford.H>
5 # include <tpl_netgraph.H>
6 
7 namespace Aleph {
8 
9 
24  template <typename Arc_Info, typename F_Type = double>
25 class Net_Cost_Arc : public Net_Arc<Arc_Info, F_Type>
26 {
27 public:
28 
30  typedef F_Type Flow_Type;
33 
35  Flow_Type flow_cost() const { return this->flow*cost; }
36 
37  Net_Cost_Arc() : cost(0) { /* empty */ }
38 
39  Net_Cost_Arc(const Arc_Info & info)
40  : Net_Arc<Arc_Info, F_Type>(info), cost(0)
41  { /* empty */ }
42 };
43 
62  template <class NodeT, class ArcT>
63 class Net_Max_Flow_Min_Cost : public Net_Graph<NodeT, ArcT>
64 {
65 public:
66 
70 
72  typedef typename Net::Digraph Digraph;
73 
75 
77  typedef ArcT Arc;
78 
80  typedef NodeT Node;
81 
83  typedef typename Arc::Flow_Type Flow_Type;
84 
86  typedef typename Node::Node_Type Node_Type;
87 
89  typedef typename Arc::Arc_Type Arc_Type;
90 
92  Flow_Type & get_cost(Arc * a) { return a->cost; }
93 
95  const Flow_Type & flow_cost(Arc * a) const { return a->flow_cost(); }
96 
107  virtual Arc * insert_arc(Node * src_node, Node * tgt_node,
108  const Flow_Type & cap, const Flow_Type & cost)
109  {
110  Arc * a = Net::insert_arc(src_node, tgt_node, Arc_Type(), cap, 0);
111  a->cost = cost;
112  return a;
113  }
114 
116  virtual Arc * insert_arc(Node * src_node, Node * tgt_node)
117  {
118  Arc * a = Net::insert_arc(src_node, tgt_node, Arc_Type());
119  a->cost = 0;
120  return a;
121  }
122 
124  virtual Arc * insert_arc(Node * src_node, Node * tgt_node,
125  const Arc_Type & arc_info)
126  {
127  Arc * a = Net::insert_arc(src_node, tgt_node, arc_info);
128  a->cost = 0;
129  return a;
130  }
131 
134  {
135  Flow_Type total = 0;
136  for (Arc_Iterator<Net_MFMC> it(*this); it.has_current(); it.next())
137  {
138  Arc * a = it.get_current();
139  if (not a->is_residual)
140  total += a->flow_cost();
141  }
142 
143  return total;
144  }
145 };
146 
147  template <class Net>
149 {
150 public:
151 
152  typedef typename Net::Flow_Type Distance_Type;
153 
154  static const Distance_Type Max_Distance;
155 
156  static const Distance_Type Zero_Distance;
157 
158  typename Net::Flow_Type operator () (typename Net::Arc * a) const
159  {
160  return a->is_residual ? -((typename Net::Arc *) a->img_arc)->cost : a->cost;
161  }
162 };
163 
164  template <class Net>
165 const typename Access_Cost<Net>::Distance_Type
167 
168 
169  template <class Net>
170  const typename Access_Cost<Net>::Distance_Type
172  numeric_limits<typename Access_Cost::Distance_Type>::max();
173 
174 
201  template <class Net, template <class> class Max_Flow_Algo>
203  (Net & net, bool leave_residual = false)
204 { // tipos sinónimos para hacer más simple la lectura
207  typedef Res_F<Net> Filter;
208 
209  Max_Flow_Algo <Net> () (net, true); // obtiene flujo máximo inicial
210 
211  Path<Net> cycle(net); // aquí se guardan ciclos negativos
212  // eliminar ciclos negativos mientras éstos existan
213  while (Bellman_Ford <Net, Access_Cost<Net>, Filter> (net)(cycle))
214  if (increase_flow <Net> (net, cycle) == 0)
215  break;
216 
217  if (not leave_residual)
218  net.unmake_residual_net();
219 }
220 
221  template <class Net, template <class> class Cost> static
222 typename Net::Flow_Type search_max_arc_cost(Net & net)
223 {
224  Arc_Iterator<Net> it(net);
225  typename Net::Flow_Type max_cost = Cost <Net> () (it.get_current());
226  for (it.next(); it.has_current(); it.next())
227  {
228  typename Net::Flow_Type curr_cost = Cost <Net> () (it.get_current());
229  if (curr_cost > max_cost)
230  max_cost = curr_cost;
231  }
232 
233  return max_cost;
234 }
235 
236  template <class Net> static
237 typename Net::Flow_Type compute_max_possible_flow(Net & net)
238 {
239  typename Net::Node * source = net.get_source();
240  typename Net::Node * sink = net.get_sink();
241 
242  return std::min(net.get_out_cap(source), net.get_in_cap(sink));
243 }
244 
245  template <class Net, template <class> class Cost> static
246 typename Net::Arc * create_dummy_arc(Net & net)
247 {
248  const typename Net::Flow_Type max_cost =
249  net.get_num_nodes() * search_max_arc_cost <Net, Cost> (net);
250 
251  net.make_residual_net();
252 
253  typename Net::Node * src = net.get_source();
254  typename Net::Node * tgt = net.get_sink();
255  const typename Net::Flow_Type max_flow =
256  std::min(net.get_out_cap(src), net.get_in_cap(tgt));
257  typename Net::Net::Digraph * digraph = &net;
258  typename Net::Arc * a = digraph->insert_arc(src, tgt);
259  net.get_cookie() = a;
260 
261  try
262  {
263  typename Net::Arc * img = digraph->insert_arc(tgt, src);
264 
265  digraph->disconnect_arc(a); // no supra-arco, dejarlo por coste
266 
267  a->is_residual = false;
268  a->img_arc = img;
269  a->cap = max_flow;
270  a->cost = max_cost;
271  a->flow = 0;
272 
273  img->is_residual = true;
274  img->img_arc = a;
275  img->cap = max_flow;
276  img->cost = max_cost;
277  img->flow = 0;
278 
279  return img;
280 
281  }
282  catch (...)
283  {
284  digraph->remove_arc(a);
285  throw;
286  }
287 }
288 
289  template <class Net> static
290 void destroy_dummy_arc(Net & net)
291 {
292  typename Net::Arc * a = (typename Net::Arc *) net.get_cookie();
293  typename Net::Arc * img = (typename Net::Arc *) a->img_arc;
294  typename Net::Net::Digraph * digraph = &net;
295 
296  digraph->connect_arc(a);
297  digraph->remove_arc(img);
298  digraph->remove_arc(a);
299 
300  net.unmake_residual_net();
301 }
302 
326  template <class Net>
328 {
331  typedef Res_F<Net> Res_Filter;
332 
333  create_dummy_arc <Net, Access_Cost> (net);
334 
335  Path<Net> cycle(net); // aquí se guardan ciclos negativos
337  <Net, Access_Cost<Net>, Cmp,Plus, Res_Filter> () (net, cycle))
338  if (increase_flow <Net> (net, cycle) == 0)
339 
340  {
341  ((typename Net::Arc *) net.get_cookie())->cost = 0;
342  break;
343  }
344 
345  destroy_dummy_arc(net);
346 }
347 
356  template <class Net>
358 {
359 public:
360 
382  void by_cycle_canceling_and_dummy_arc(Net & net) const
383  {
385  }
386 
411  template <template <class> class Max_Flow_Algo>
412  void by_cycle_canceling(Net & net, bool leave_residual = false)
413  {
414  max_flow_min_cost_by_cycle_canceling<Net, Max_Flow_Algo>
415  (net, leave_residual);
416  }
417 };
418 
419 
420 } // end namespace Aleph
421 
422 # endif // TPL_MAXFLOW_MINCOST_H
void by_cycle_canceling_and_dummy_arc(Net &net) const
Definition: tpl_maxflow_mincost.H:382
Definition: tpl_maxflow_mincost.H:25
Definition: Bellman_Ford.H:1359
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const typename Arc::Arc_Type &arc_info, const Flow_Type &cap, const Flow_Type &flow)
Definition: tpl_netgraph.H:517
NodeT Node
Tipo de node.
Definition: tpl_maxflow_mincost.H:80
Definition: tpl_maxflow_mincost.H:357
Definition: tpl_netgraph.H:66
F_Type Flow_Type
Tipo que representa el flujo.
Definition: tpl_maxflow_mincost.H:30
Definition: ahFunction.H:145
Definition: tpl_maxflow_mincost.H:148
Definition: tpl_graph.H:751
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &cost)
Definition: tpl_maxflow_mincost.H:107
const Flow_Type & flow_cost(Arc *a) const
Retorna una referencia al coste de un arco:
Definition: tpl_maxflow_mincost.H:95
Definition: tpl_netgraph.H:49
ArcT Arc
Tipo de arco.
Definition: tpl_maxflow_mincost.H:77
Definition: tpl_graph.H:26
virtual Arc * insert_arc(Node *src_node, Node *tgt_node)
Usado por algorítmica interna. NO UTILIZAR.
Definition: tpl_maxflow_mincost.H:116
Definition: Bellman_Ford.H:1439
Arc::Flow_Type Flow_Type
Tipo que representa la capacidad y el flujo.
Definition: tpl_maxflow_mincost.H:83
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
Net::Digraph Digraph
El tipo de grafo dirigido.
Definition: tpl_maxflow_mincost.H:72
void by_cycle_canceling(Net &net, bool leave_residual=false)
Definition: tpl_maxflow_mincost.H:412
Flow_Type cost
coste por unidad de flujo
Definition: tpl_maxflow_mincost.H:32
Node::Node_Type Node_Type
Tipo de atributo que almacena un nodo.
Definition: tpl_maxflow_mincost.H:86
Definition: tpl_netgraph.H:205
Flow_Type & get_cost(Arc *a)
Retorna una referencia modificable al coste de un arco.
Definition: tpl_maxflow_mincost.H:92
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const Arc_Type &arc_info)
Usado por algorítmica interna. NO UTILIZAR.
Definition: tpl_maxflow_mincost.H:124
Definition: tpl_maxflow_mincost.H:63
Net_Graph< NodeT, ArcT > Net
Definition: tpl_maxflow_mincost.H:69
Definition: ahFunction.H:54
Flow_Type compute_flow_cost()
Calcula el coste total del flujo circulante por la red.
Definition: tpl_maxflow_mincost.H:133
Arc::Arc_Type Arc_Type
Tipo de atributo que almacena un arco.
Definition: tpl_maxflow_mincost.H:89
Flow_Type flow_cost() const
Retorna el coste del flujo circulante.
Definition: tpl_maxflow_mincost.H:35
Flow_Type flow
valor de flujo
Definition: tpl_netgraph.H:77
void max_flow_min_cost_by_cycle_canceling(Net &net)
Definition: tpl_maxflow_mincost.H:327

Leandro Rabindranath León