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_netgraph.H
1 # ifndef TPL_NETGRAPH_H
2 # define TPL_NETGRAPH_H
3 
4 # include <limits>
5 # include <tpl_dynDlist.H>
6 # include <tpl_dynListStack.H>
7 # include <tpl_dynBinHeap.H>
8 # include <tpl_dynSetTree.H>
9 # include <tpl_random_queue.H>
10 # include <Set.H>
11 # include <tpl_graph_utils.H>
12 # include <tpl_find_path.H>
13 
45 using namespace Aleph;
46 
47 namespace Aleph {
48 
49 template <class N> class Res_F;
50 
51 
65  template <typename Arc_Info, typename F_Type = double>
66 class Net_Arc : public Graph_Aarc<Arc_Info>
67 {
68 public:
69 
71  typedef F_Type Flow_Type;
72 
75 
78 
82  bool check_arc() const { return flow >= 0 and flow <= cap; }
83 
86 
88  bool is_residual;
89 
90  Net_Arc() : img_arc(NULL), is_residual(false) { /* empty */ }
91 
92  Net_Arc(Net_Arc * net_arc)
93  : Graph_Aarc<Arc_Info>(*net_arc), cap(net_arc->cap), flow(net_arc->flow),
94  img_arc(NULL), is_residual(false)
95  {
96  /* empty */
97  }
98 
99  Net_Arc(const Arc_Info & info)
100  : Graph_Aarc<Arc_Info>(info), img_arc(NULL), is_residual(false)
101  { /* empty */ }
102 
103  Net_Arc & operator = (Net_Arc & arc)
104  {
105  if (this == &arc)
106  return *this;
107 
108  this->get_info() = arc.get_info();
109  cap = arc.cap;
110  flow = arc.flow;
111  img_arc = NULL;
112  is_residual = false;
113 
114  return *this;
115  }
116 };
117 
140  template <typename Node_Info, typename F_Type = double>
141 class Net_Node : public Graph_Anode<Node_Info>
142 {
143 public:
144 
146  typedef F_Type Flow_Type;
147 
149  size_t in_degree;
150 
153 
156 
159 
162 
163  Net_Node() : in_degree(0), out_cap(0), in_cap(0), out_flow(0), in_flow(0)
164  {
165  // empty
166  }
167 
170  Net_Node(const Node_Info & node_info)
171  : Graph_Anode<Node_Info>(node_info), in_degree(0),
172  out_cap(0), in_cap(0), out_flow(0), in_flow(0)
173  {
174  /* empty */
175  }
176 
179  Net_Node(Net_Node * net_node)
180  : Graph_Anode<Node_Info>(*net_node), in_degree(net_node->in_degree),
181  out_cap(net_node->out_cap), in_cap(net_node->in_cap),
182  out_flow(net_node->out_flow), in_flow(net_node->in_flow)
183  {
184  /* empty */
185  }
186 };
187 
203  template <class NodeT = Net_Node<Empty_Class, double>,
204  class ArcT = Net_Arc<Empty_Class, double> >
205 class Net_Graph : public Array_Digraph<NodeT, ArcT>
206 {
207 public:
208 
210 
212  typedef ArcT Arc;
213 
215  typedef NodeT Node;
216 
218  typedef typename Arc::Flow_Type Flow_Type;
219 
221  typedef typename Node::Node_Type Node_Type;
222 
224  typedef typename Arc::Arc_Type Arc_Type;
225 
226 public:
227 
228  mutable Flow_Type Infinity;
229 
231  Flow_Type get_in_cap(Node * node) const { return node->in_cap; }
232 
234  Flow_Type get_out_cap(Node * node) const { return node->out_cap; }
235 
238  size_t get_in_degree(Node * node) const { return node->in_degree; }
239 
242  size_t get_out_degree(Node * node) const { return get_num_arcs(node); }
243 
245  Flow_Type get_out_flow(Node * node) const { return node->out_flow; }
246 
248  Flow_Type get_in_flow(Node * node) const { return node->in_flow; }
249 
251  bool is_source(Node * node) { return src_nodes.count(node) > 0; }
252 
254  bool is_sink(Node * node) { return sink_nodes.count(node) > 0; }
255 
259  bool is_connected(Node * node) const
260  {
261  return node->in_degree != 0 or node->num_arcs != 0;
262  }
263 
266  bool check_node(Node * node)
267  {
268  if (not is_connected(node))
269  return false;
270 
271  if (is_sink(node))
272  return node->out_flow == 0 and node->in_flow >= 0;
273 
274  if (is_source(node))
275  return node->in_flow == 0 and node->out_flow >= 0;
276 
277  Flow_Type sum_out_cap = 0;
278  Flow_Type sum_out_flow = 0;
279  for (Node_Arc_Iterator<Net_Graph> it(node); it.has_curr(); it.next())
280  {
281  Arc * arc = it.get_current();
282  if (arc->is_residual)
283  continue;
284 
285  if (not arc->check_arc())
286  return false;
287 
288  sum_out_cap += arc->cap;
289  sum_out_flow += arc->flow;
290  }
291 
292  if (sum_out_cap != node->out_cap or sum_out_flow != node->out_flow)
293  return false;
294 
295  if (is_source(node))
296  return true;
297 
298  return node->out_flow == node->in_flow;
299  }
300 
301 private:
302 
303  Aleph::set<Node*> src_nodes;
304  Aleph::set<Node*> sink_nodes;
305 
306 public:
307 
309  Aleph::set<Node*> & get_src_nodes() { return src_nodes; }
310 
312  Aleph::set<Node*> & get_sink_nodes() { return sink_nodes; }
313 
314 public:
315 
320  {
321  if (src_nodes.size() == 1)
322  return;
323 
324  if (src_nodes.size() == 0)
325  throw std::domain_error("network has not source node (it has cicles)");
326 
327  DynDlist<Node*> src_list;
328  for (typename Aleph::set<Node*>::iterator it = src_nodes.begin();
329  it != src_nodes.end(); ++it)
330  src_list.append(*it);
331 
332  Node * super_source = insert_node();
333  while (not src_list.is_empty())
334  {
335  Node * p = src_list.remove_first();
336  insert_arc(super_source, p, get_out_cap(p));
337  }
338 
339  with_super_source = true;
340  }
341 
345  {
346  if (not with_super_source)
347  return;
348 
349  I(src_nodes.size() == 1);
350 
351  remove_node(*src_nodes.begin());
352  with_super_source = false;
353  }
354 
359  {
360  if (sink_nodes.size() == 1)
361  return;
362 
363  if (sink_nodes.size() == 0)
364  throw std::domain_error("network has not sink node (it has cicles)");
365 
366  DynDlist<Node*> sink_list;
367  for (typename Aleph::set<Node*>::iterator it = sink_nodes.begin();
368  it != sink_nodes.end(); ++it)
369  sink_list.append(*it);
370 
371  Node * super_sink = insert_node();
372  while (not sink_list.is_empty())
373  {
374  Node * p = sink_list.remove_first();
375  insert_arc(p, super_sink, get_in_cap(p));
376  }
377 
378  with_super_sink = true;
379  }
380 
384  {
385  if (not with_super_sink)
386  return;
387 
388  I(sink_nodes.size() == 1);
389 
390  remove_node(*sink_nodes.begin());
391  with_super_sink = false;
392  }
393 
397  {
399  try
400  {
401  make_super_sink();
402  }
403  catch (std::bad_alloc)
404  {
406  }
407  }
408 
413  {
416  }
417 
419  Node * get_source() { return *get_src_nodes().begin(); }
420 
422  Node * get_sink() { return *get_sink_nodes().begin(); }
423 
434  Node * insert_node(const Node_Type & node_info)
435  {
436  Node * p = Digraph::insert_node(node_info);
437  try
438  {
439  src_nodes.insert(p);
440  try
441  {
442  sink_nodes.insert(p);
443  return p;
444  }
445  catch (bad_alloc)
446  {
447  src_nodes.erase(p);
448  Digraph::remove_node(p);
449  throw;
450  }
451  }
452  catch (bad_alloc)
453  {
454  Digraph::remove_node(p);
455  throw; // propagar excepción
456  }
457  }
458 
462 
475  {
476  Digraph::insert_node(p);
477  try
478  {
479  src_nodes.insert(p);
480  try
481  {
482  sink_nodes.insert(p);
483  return p;
484  }
485  catch (bad_alloc)
486  {
487  src_nodes.erase(p);
488  Digraph::remove_node(p);
489  throw;
490  }
491  }
492  catch (bad_alloc)
493  {
494  Digraph::remove_node(p);
495  throw; // propagar excepción
496  }
497  }
498 
517  virtual Arc * insert_arc(Node * src_node, Node * tgt_node,
518  const typename Arc::Arc_Type & arc_info,
519  const Flow_Type & cap, const Flow_Type & flow)
520  { // inserción en clase base
521  Arc * arc = Digraph::insert_arc(src_node, tgt_node, arc_info);
522 
523  src_nodes.erase(tgt_node); // actualización de source/sink
524  sink_nodes.erase(src_node);
525 
526  arc->cap = cap; // ajuste capacidad y flujo de arco
527  arc->flow = flow;
528 
529  if (not arc->check_arc())
530  throw std::overflow_error("flow is greater than capacity");
531 
532  tgt_node->in_degree++; // actualizar info de control de nodo
533  src_node->out_cap += cap;
534  tgt_node->in_cap += cap;
535  src_node->out_flow += flow;
536  tgt_node->in_flow += flow;
537 
538  return arc;
539  }
540 
558  Arc * connect_arc(Arc * arc)
559  {
560  Digraph::connect_arc(arc);
561 
562  Node * src = this->get_src_node(arc);
563  Node * tgt = this->get_tgt_node(arc);
564 
565  src_nodes.erase(tgt); // elimina destino de src_nodes
566  sink_nodes.erase(src); // elimina fuente de sink_nodes
567 
568  tgt->in_degree++; // actualiza información de control de nodo
569  src->out_cap += arc->cap;
570  tgt->in_cap += arc->cap;
571  src->out_flow += arc->flow;
572  tgt->in_flow += arc->flow;
573 
574  return arc;
575  }
576 
594  virtual Arc *
595  insert_arc(Node * src_node, Node * tgt_node, const Flow_Type & cap)
596  {
597  return insert_arc(src_node, tgt_node, Arc_Type(), cap, 0);
598  }
599 
617  virtual Arc * insert_arc(Node * src_node, Node * tgt_node,
618  const typename Arc::Arc_Type & arc_info)
619  {
620  return insert_arc(src_node, tgt_node, arc_info, 0, 0);
621  }
622 
642  virtual Arc * insert_arc(Node * src_node, Node * tgt_node)
643  {
644  return insert_arc(src_node, tgt_node, Arc_Type(), 0, 0);
645  }
646 
648  virtual void remove_arc(Arc * arc)
649  {
650  Node * src = this->get_src_node(arc);
651  Node * tgt = this->get_tgt_node(arc);
652 
653  if (--(tgt->in_degree) == 0)
654  src_nodes.insert(tgt); // tgt deviene un nodo fuente
655 
656  src->out_cap -= arc->cap; // actualizar caps y flujos en src y tgt
657  src->out_flow -= arc->flow;
658  tgt->in_cap -= arc->cap;
659  tgt->in_flow -= arc->flow;
660 
661  Digraph::remove_arc(arc); // eliminar en clase base
662 
663  if (this->get_num_arcs(src) == 0)
664  sink_nodes.insert(src); // src deviene un nodo sumidero
665  }
666 
678  void disconnect_arc(Arc * arc)
679  {
680  Node * src = this->get_src_node(arc);
681  Node * tgt = this->get_tgt_node(arc);
682  if (--(tgt->in_degree) == 0)
683  src_nodes.insert(tgt); // tgt deviene un nodo fuente
684 
685  src->out_cap -= arc->cap; // actualizar caps y flujos en src y tgt
686  src->out_flow -= arc->flow;
687  tgt->in_cap -= arc->cap;
688  tgt->in_flow -= arc->flow;
689 
690  Digraph::disconnect_arc(arc); // desconeción en clase base
691 
692  if (this->get_num_arcs(src) == 0)
693  sink_nodes.insert(src); // src deviene un nodo sumidero
694  }
695 
697  virtual void remove_node(Node * p)
698  {
699  Digraph::remove_node(p); // eliminación en clase base
700  src_nodes.erase(p);
701  sink_nodes.erase(p);
702  }
703 
706  Net_Graph(Digraph & digraph)
707  : with_super_source(false), with_super_sink(false), residual_net(false)
708  {
709  Net_Graph::Net_Graph(); // inicializa atributos
710  copy_graph(*this, digraph, true); // copia mapeada
711  }
712 
716  : Array_Digraph<NodeT, ArcT>::Array_Digraph(),
717  Infinity(numeric_limits<typename Arc::Flow_Type>::max()),
719  with_super_sink(net.with_super_sink), residual_net(net.residual_net)
720  {
721  copy_graph(*this, net, false); // copia sin mapear
722  }
723 
724  ~Net_Graph()
725  {
726  if (residual_net)
728  }
729 
731  void set_cap(Arc * arc, const Flow_Type & cap)
732  {
733  if (cap < arc->flow)
734  throw std::out_of_range("capacity value is smaller than flow");
735 
736  const Flow_Type old_cap = arc->cap;
737  arc->cap = cap;
738 
739  Node * src_node = get_src_node(arc);
740  src_node->out_cap -= old_cap;
741  src_node->out_cap += cap;
742 
743  Node * tgt_node = get_tgt_node(arc);
744  tgt_node->in_cap -= old_cap;
745  tgt_node->in_cap += cap;
746  }
747 
750  void set_flow(Arc * arc, const Flow_Type & flow)
751  {
752  if (flow > arc->cap)
753  throw std::out_of_range("flow value is greater than capacity");
754 
755  const Flow_Type old_flow = arc->flow;
756  arc->flow = flow;
757 
758  Node * src_node = get_src_node(arc);
759  src_node->out_flow -= old_flow;
760  src_node->out_flow += flow;
761 
762  Node * tgt_node = get_tgt_node(arc);
763  tgt_node->in_flow -= old_flow;
764  tgt_node->in_flow += flow;
765  }
766 
768  const Flow_Type & get_flow(Arc * arc) const { return arc->flow; }
769 
771  const Flow_Type & get_cap(Arc * arc) const { return arc->cap; }
772 
773  void reset()
774  {
775  for (Arc_Iterator<Net_Graph> it(*this); it.has_current(); it.next())
776  it.get_current()->flow = 0;
777 
778  for (Node_Iterator<Net_Graph> it(*this); it.has_current(); it.next())
779  {
780  Node * p = it.get_current();
781  p->in_flow = p->out_flow = 0;
782  }
783  }
784 
798  {
799  for (Node_Iterator<Net_Graph> it(*this); it.has_current(); it.next())
800  if (not check_node(it.get_current()))
801  return false;
802 
803  return true; // todos los nodos son válidos
804  }
805 
809  Flow_Type flow_value() { return get_source()->out_flow; }
810 
813 
816 
817  bool residual_net;
818 
819  Net_Graph()
820  : Infinity(numeric_limits<typename Arc::Flow_Type>::max()),
821  with_super_source(false), with_super_sink(false), residual_net(false)
822  { /* empty */ }
823 
824  void insert_residual_arc(Arc * arc)
825  {
826  Arc * res_arc = Digraph::insert_arc(this->get_tgt_node(arc),
827  this->get_src_node(arc));
828 
829  res_arc->is_residual = true;
830  res_arc->img_arc = arc;
831  res_arc->cap = arc->cap;
832  res_arc->flow = arc->cap - arc->flow;
833 
834  arc->img_arc = res_arc;
835  }
836 
837 private:
838 
839  void remove_residual_arc(Arc * arc)
840  {
841  I(arc->is_residual);
842 
843  Digraph::remove_arc(arc);
844  }
845 
846 public:
847 
890  {
891  try
892  {
893  if (residual_net)
894  throw std::domain_error("Residual net is currently computed");
895 
896  size_t n = this->get_num_arcs(); // num arcos residuales a insertar
897  for (Arc_Iterator<Net_Graph> it(*this); it.has_curr() and n > 0;
898  it.next())
899  {
900  Arc * arc = it.get_current_arc();
901  if (not arc->is_residual)
902  {
903  insert_residual_arc(arc);
904  --n;
905  }
906  }
907 
908  residual_net = true;
909  }
910  catch (...)
911  {
913  }
914  }
915 
920  {
921  for (Arc_Iterator<Net_Graph> it(*this); it.has_current(); )
922  {
923  Arc * arc = it.get_current_arc();
924  if (arc->is_residual)
925  {
926  it.next();
927  remove_residual_arc(arc);
928  }
929  else
930  it.next();
931  }
932  residual_net = false;
933  }
934 
935 public:
936 
938  bool is_there_residual_net() const { return residual_net; }
939 
942  {
943  if (not residual_net)
944  throw std::domain_error("Residual net is not computed");
945 
946  return a->img_arc;
947  }
948 
949  void increase_out_flow(Node * p, const Flow_Type & flow)
950  {
951  if (is_sink(p))
952  return;
953 
954  p->out_flow += flow;
955  }
956 
957  void decrease_out_flow(Node * p, const Flow_Type & flow)
958  {
959  if (is_sink(p))
960  return;
961 
962  p->out_flow -= flow;
963  }
964 
965  void increase_in_flow(Node * p, const Flow_Type & flow)
966  {
967  if (is_source(p))
968  return;
969 
970  p->in_flow += flow;
971  }
972 
973  void decrease_in_flow(Node * p, const Flow_Type & flow)
974  {
975  if (is_source(p))
976  return;
977 
978  p->in_flow -= flow;
979  }
980 };
981 
996  template <class N>
997 class Res_F
998 {
999 public:
1000 
1001  bool operator () (typename N::Arc * a) const
1002  {
1003  return (a->cap - a->flow) != 0;
1004  }
1005 };
1006 
1032  template <class Net>
1033 typename Net::Flow_Type increase_flow(Net & net, Path<Net> & path)
1034 {
1035  if (not net.residual_net)
1036  throw std::domain_error("Residual net is not created");
1037 
1038  typename Net::Flow_Type slack = net.Infinity; // eslabón mínimo
1039 
1040  // calcular el eslabón mínimo del camino de aumento
1041  for (typename Path<Net>::Iterator it(path); it.has_current_arc(); it.next())
1042  {
1043  typename Net::Arc * arc = it.get_current_arc();
1044  const typename Net::Flow_Type w = arc->cap - arc->flow;
1045 
1046  I(w >= 0);
1047 
1048  if (w < slack)
1049  slack = w;
1050  }
1051 
1052  if (slack == 0)
1053  return slack;
1054 
1055  // aumentar el flujo de la red por el camino de aumento
1056  for (typename Path<Net>::Iterator it(path); it.has_current_arc(); it.next())
1057  {
1058  typename Net::Arc * arc = it.get_current_arc();
1059  typename Net::Arc * img = (typename Net::Arc*) arc->img_arc;
1060  arc->flow += slack;
1061  img->flow -= slack;
1062 
1063  if (not arc->is_residual)
1064  {
1065  net.increase_out_flow(net.get_src_node(arc), slack);
1066  net.increase_in_flow(net.get_tgt_node(arc), slack);
1067  }
1068  else
1069  {
1070  net.decrease_in_flow(net.get_src_node(arc), slack);
1071  net.decrease_out_flow(net.get_tgt_node(arc), slack);
1072  }
1073 
1074  I(arc->check_arc());
1075  }
1076 
1077  return slack;
1078 }
1079 
1103  template <class Net>
1104 bool has_aumenting_path(Net & net)
1105 {
1106  if (not net.residual_net)
1107  throw std::domain_error("Residual net has not been generated");
1108 
1109  return test_for_path<Net, Res_F<Net> >(net, net.get_source(), net.get_sink());
1110 }
1111 
1145  template <class Net, template <class, class> class Find_Path>
1146  typename Net::Flow_Type
1147 aumenting_path_maximum_flow(Net & net, const bool & leave_residual = false)
1148 {
1149  net.make_super_nodes();
1150  try
1151  {
1152  net.make_residual_net();
1153  }
1154  catch (std::bad_alloc)
1155  {
1156  net.unmake_super_nodes();
1157  throw;
1158  }
1159 
1160  typename Net::Node * source = net.get_source();
1161  typename Net::Node * sink = net.get_sink();
1162  try
1163  {
1164  while (true) // mientras exista un camino de aumento
1165  {
1166  Path<Net> path(net);
1167  if (not Find_Path<Net, Res_F<Net> > () (net, source, sink, path))
1168  break;
1169 
1170  increase_flow <Net> (net, path);
1171  }
1172  }
1173  catch (std::bad_alloc)
1174  {
1175  net.unmake_residual_net();
1176  net.unmake_super_nodes();
1177  throw;
1178  }
1179 
1180  const typename Net::Flow_Type ret_val = source->out_flow;
1181  if (not leave_residual)
1182  {
1183  net.unmake_residual_net();
1184  net.unmake_super_nodes();
1185  }
1186 
1187  return ret_val;
1188 }
1189 
1219  template <class Net> typename Net::Flow_Type
1220 ford_fulkerson_maximum_flow(Net & net, const bool & leave_residual = false)
1221 {
1222  return aumenting_path_maximum_flow<Net, Find_Path_Depth_First>
1223  (net, leave_residual);
1224 }
1225 
1231  template <class Net> class
1233 {
1234 public:
1235 
1249  typename Net::Flow_Type
1250  operator () (Net & net, const bool & leave_residual = false) const
1251  {
1252  return ford_fulkerson_maximum_flow(net, leave_residual);
1253  }
1254 };
1255 
1285  template <class Net> typename Net::Flow_Type
1286 edmonds_karp_maximum_flow(Net & net, const bool & leave_residual = false)
1287 {
1288  return aumenting_path_maximum_flow<Net, Find_Path_Breadth_First>
1289  (net, leave_residual);
1290 }
1291 
1297  template <class Net>
1299 {
1300 public:
1301 
1315  typename Net::Flow_Type
1316  operator () (Net & net,
1317  const bool & leave_residual = false) const
1318  {
1319  return edmonds_karp_maximum_flow(net, leave_residual);
1320  }
1321 };
1322 
1323  template <class Net> static
1324 bool is_node_active(typename Net::Node * p)
1325 {
1326  return p->in_flow > p->out_flow;
1327 }
1328 
1329  template <class Net> static
1330 long & node_height(typename Net::Node * p) { return NODE_COUNTER(p); }
1331 
1340  template <class N>
1341 class Res_Arc
1342 {
1343 public:
1344 
1345  bool operator () (typename N::Arc * a) const
1346  {
1347  return a->is_residual;
1348  }
1349 };
1350 
1351  template <class Net> static
1352 bool initial_height(Net & net, typename Net::Node * p, typename Net::Arc * a)
1353 {
1354  node_height<Net>(p) = node_height<Net>(net.get_src_node(a)) + 1;
1355  return false;
1356 }
1357 
1358  template <class Net> static
1359 void init_height_in_nodes(Net & net)
1360 {
1361  breadth_first_traversal <Net, Res_Arc<Net>>
1362  (net, net.get_sink(), &initial_height);
1363 }
1364 
1365  template <class Q_Type> static
1366 void put_in_active_queue(Q_Type & q, typename Q_Type::Item_Type & p)
1367 {
1368  if (NODE_BITS(p).get_bit(Aleph::Maximum_Flow))
1369  return;
1370 
1371  NODE_BITS(p).set_bit(Aleph::Maximum_Flow, true);
1372  q.put(p);
1373 }
1374 
1375  template <class Q_Type> static
1376 typename Q_Type::Item_Type get_from_active_queue(Q_Type & q)
1377 {
1378  typename Q_Type::Item_Type p = q.get();
1379 
1380  I(NODE_BITS(p).get_bit(Aleph::Maximum_Flow));
1381 
1382  NODE_BITS(p).set_bit(Aleph::Maximum_Flow, false);
1383 
1384  return p;
1385 }
1386 
1387 
1388  template <class Q_Type> static
1389 void remove_from_active_queue(Q_Type & q, typename Q_Type::Item_Type & p)
1390 {
1391  NODE_BITS(p).set_bit(Aleph::Maximum_Flow, false);
1392  q.remove(p);
1393 }
1394 
1429  template <class Net, class Q_Type> typename Net::Flow_Type
1431  (Net & net, const bool & leave_residual = false)
1432 {
1433  net.make_super_nodes();
1434  try
1435  {
1436  net.make_residual_net();
1437  net.reset_counter_nodes();
1438  }
1439  catch (std::bad_alloc)
1440  {
1441  net.unmake_super_nodes();
1442  throw;
1443  }
1444 
1445  init_height_in_nodes(net);
1446  typename Net::Node * source = net.get_source();
1447  typename Net::Node * sink = net.get_sink();
1448 
1449  Q_Type q; // instancia el conjunto de nodos activos
1450  try
1451  {
1452  for (Node_Arc_Iterator<Net, Res_F<Net> > it(source); it.has_curr();
1453  it.next())
1454  {
1455  typename Net::Arc * arc = it.get_current_arc();
1456  typename Net::Node * tgt = net.get_tgt_node(arc);
1457  arc->flow = tgt->in_flow = arc->cap - arc->flow; // inunde arco
1458  arc->img_arc->flow = 0;
1459  if (tgt != sink)
1460  put_in_active_queue(q, tgt); // tgt deviene activo
1461  }
1462 
1463  source->out_flow = source->out_cap;
1464 
1465  while (not q.is_empty()) // mientras haya nodos activos
1466  {
1467  typename Net::Node * src = get_from_active_queue(q);
1468  typename Net::Flow_Type excess = src->in_flow - src->out_flow;
1469 
1470  for (Node_Arc_Iterator<Net, Res_F<Net> > it(src);
1471  it.has_curr() and excess > 0; it.next())
1472  {
1473  typename Net::Arc * arc = it.get_current_arc();
1474  typename Net::Node * tgt = net.get_tgt_node(arc);
1475  if (node_height<Net>(src) != node_height<Net>(tgt) + 1)
1476  continue; // el nodo no es elegible
1477 
1478  const typename Net::Flow_Type flow_avail_in_arc =
1479  arc->cap - arc->flow;
1480  typename Net::Flow_Type flow_to_push =
1481  std::min(flow_avail_in_arc, excess);
1482 
1483  arc->flow += flow_to_push;
1484  arc->img_arc->flow -= flow_to_push;
1485  if (arc->is_residual)
1486  {
1487  net.decrease_out_flow(tgt, flow_to_push);
1488  net.decrease_in_flow(src, flow_to_push);
1489  }
1490  else
1491  {
1492  net.increase_out_flow(src, flow_to_push);
1493  net.increase_in_flow(tgt, flow_to_push);
1494  }
1495 
1496  excess -= flow_to_push;
1497 
1498  if (is_node_active<Net>(tgt) and tgt != sink and tgt != source)
1499  put_in_active_queue(q, tgt);
1500  }
1501 
1502  if (excess > 0) // ¿src aún sigue activo?
1503  { // sí ==> incremente h(src) y re-inserte en q
1504  node_height<Net>(src)++;
1505  put_in_active_queue(q, src);
1506  }
1507  }
1508 
1509  const typename Net::Flow_Type ret_val = source->out_flow;
1510 
1511  if (not leave_residual)
1512  {
1513  net.unmake_residual_net();
1514  net.unmake_super_nodes();
1515  }
1516 
1517  return ret_val;
1518  }
1519  catch (...)
1520  {
1521  net.unmake_residual_net();
1522  net.unmake_super_nodes();
1523  throw;
1524  }
1525 }
1526 
1554  template <class Net> typename Net::Flow_Type
1556  (Net & net, const bool & leave_residual = false)
1557 {
1559  <Net,DynListQueue<typename Net::Node*> >(net, leave_residual);
1560 }
1561 
1567  template <class Net>
1569 {
1570 public:
1584  typename Net::Flow_Type
1585  operator () (Net & net,
1586  const bool & leave_residual = false) const
1587  {
1588  return fifo_preflow_maximum_flow(net, leave_residual);
1589  }
1590 };
1591 
1592  template <class Net>
1594 {
1595  bool operator () (typename Net::Node * n1, typename Net::Node * n2) const
1596  {
1597  return node_height<Net>(n1) > node_height<Net>(n2);
1598  }
1599 };
1600 
1632  template <class Net> typename Net::Flow_Type
1633 heap_preflow_maximum_flow(Net & net, const bool & leave_residual = false)
1634 {
1637  (net, leave_residual);
1638 }
1639 
1645  template <class Net>
1647 {
1648 public:
1649 
1662  typename Net::Flow_Type
1663  operator () (Net & net,
1664  const bool & leave_residual = false) const
1665  {
1666  return heap_preflow_maximum_flow(net, leave_residual);
1667  }
1668 };
1669 
1696  template <class Net> typename Net::Flow_Type
1697 random_preflow_maximum_flow(Net & net, const bool & leave_residual = false)
1698 {
1700  <Net, Random_Set<typename Net::Node*> > (net, leave_residual);
1701 }
1702 
1708 template <class Net> class Random_Preflow_Maximum_Flow
1709 {
1710 public:
1723  typename Net::Flow_Type
1724  operator () (Net & net,
1725  const bool & leave_residual = false) const
1726  {
1727  return random_preflow_maximum_flow(net, leave_residual);
1728  }
1729 };
1730 
1731 
1732  template <class Net> static
1733 bool has_arcs_in_active_queue(typename Net::Node * p)
1734 {
1735  for (Node_Arc_Iterator<Net, Res_F<Net> > it(p); it.has_current(); it.next())
1736  if (IS_ARC_VISITED(it.get_current(), Aleph::Maximum_Flow))
1737  return true;
1738 
1739  return false;
1740 }
1741 
1784  template <class Net, class QN_Type, class QA_Type>
1785  typename Net::Flow_Type
1787  const bool & leave_residual = false)
1788 {
1789  net.make_super_nodes();
1790  try
1791  {
1792  net.make_residual_net();
1793  net.reset_counter_nodes();
1794  }
1795  catch (std::bad_alloc)
1796  {
1797  net.unmake_super_nodes();
1798  throw;
1799  }
1800 
1801  QA_Type q; // conjunto de arcos elegibles
1802  QN_Type p; // conjunto de nodos activos
1803  typename Net::Node * source = net.get_source();
1804  typename Net::Node * sink = net.get_sink();
1805  init_height_in_nodes(net);
1806 
1807  for (Node_Arc_Iterator<Net, Res_F<Net> > i(source); i.has_curr(); i.next())
1808  {
1809  typename Net::Arc * arc = i.get_current_arc();
1810  typename Net::Node * tgt = net.get_tgt_node(arc);
1811  arc->flow = tgt->in_flow = arc->cap - arc->flow; // inundar arco
1812  arc->img_arc->flow = 0;
1813 
1814  // meter arcos elegibles de tgt
1815  for (Node_Arc_Iterator<Net, Res_F<Net> > j(tgt); j.has_curr(); j.next())
1816  {
1817  typename Net::Arc * a = j.get_current_arc();
1818  if (node_height<Net>(tgt) ==
1819  node_height<Net>(net.get_tgt_node(a)) + 1)
1820  put_in_active_queue(q, a);
1821  }
1822  put_in_active_queue(p, tgt); // mete en cola de nodos activos
1823  }
1824  source->out_flow = source->out_cap;
1825 
1826  while (true)
1827  {
1828  while (not q.is_empty()) // mientras haya arcos elegibles
1829  {
1830  typename Net::Arc * arc = get_from_active_queue(q);
1831  typename Net::Node * src = net.get_src_node(arc);
1832  typename Net::Node * tgt = net.get_tgt_node(arc);
1833  if (node_height<Net>(src) != node_height<Net>(tgt) + 1)
1834  continue; // No ==> ignore arco
1835 
1836  typename Net::Flow_Type excess = src->in_flow - src->out_flow;
1837  if (excess == 0) // ¿fuente descargado en iteración anterior?
1838  { // sí ==> ignore arco
1839  remove_from_active_queue(p, src);
1840  continue; // avance al siguiente arco elegible
1841  }
1842 
1843  const typename Net::Flow_Type push =
1844  std::min(excess, arc->cap - arc->flow);
1845  arc->flow += push;
1846  arc->img_arc->flow -= push;
1847  if (arc->is_residual)
1848  {
1849  net.decrease_out_flow(net.get_tgt_node(arc), push);
1850  net.decrease_in_flow(net.get_src_node(arc), push);
1851  }
1852  else
1853  {
1854  net.increase_in_flow(net.get_tgt_node(arc), push);
1855  net.increase_out_flow(net.get_src_node(arc), push);
1856  }
1857  excess -= push;
1858 
1859  if (is_node_active<Net>(tgt) and tgt != source and tgt != sink)
1860  {
1861  for (Node_Arc_Iterator<Net, Res_F<Net> > it(tgt);
1862  it.has_curr(); it.next())
1863  {
1864  typename Net::Arc * a = it.get_current_arc();
1865  if (node_height<Net>(tgt) ==
1866  node_height<Net>(net.get_tgt_node(a)) + 1)
1867  put_in_active_queue(q, a);
1868  }
1869 
1870  put_in_active_queue(p, tgt);
1871  }
1872 
1873  if (excess == 0) // ¿se descargó el fuente?
1874  { // sí ==> fuente deviene inactivo
1875  remove_from_active_queue(p, src);
1876  continue;
1877  }
1878 
1879  if (src != source and src != sink and // ¿empuje no saturante?
1880  not has_arcs_in_active_queue<Net>(src))
1881  { // sí ==> incremente h(active) y re-inserte arc en q
1882  remove_from_active_queue(p, src);
1883  node_height<Net>(src)++;
1884  put_in_active_queue(p, src);
1885 
1886  for (Node_Arc_Iterator<Net> it(src); it.has_current(); it.next())
1887  {
1888  typename Net::Arc * a = it.get_current_arc(); // sale de src
1889  if ((node_height<Net>(src) ==
1890  node_height<Net>(net.get_tgt_node(a)) + 1)
1891  and (a->cap - a->flow > 0))
1892  put_in_active_queue(q, a);
1893 
1894  typename Net::Arc * i = a->img_arc; // arco entrante a src
1895  if ((i->cap - i->flow > 0) and // ¿i es elegible?
1896  (node_height<Net>(net.get_src_node(i)) ==
1897  node_height<Net>(src) + 1))
1898  put_in_active_queue(q, i);
1899  }
1900  }
1901  }
1902 
1903  if (p.is_empty()) // ¿quedan nodos activos?
1904  break; // ya no hay nodos activos
1905 
1906  typename Net::Node * src = get_from_active_queue(p);
1907  node_height<Net>(src)++;
1908  put_in_active_queue(p, src);
1909 
1910  for (Node_Arc_Iterator<Net> it(src); it.has_current(); it.next())
1911  {
1912  typename Net::Arc * a = it.get_current_arc(); // sale de src
1913  if ((node_height<Net>(src) ==
1914  node_height<Net>(net.get_tgt_node(a)) + 1)
1915  and (a->cap - a->flow > 0))
1916  put_in_active_queue(q, a);
1917 
1918  typename Net::Arc * i = a->img_arc; // arco entrante a src
1919  if ((i->cap - i->flow > 0) and // ¿i es elegible?
1920  (node_height<Net>(net.get_src_node(i)) ==
1921  node_height<Net>(src) + 1))
1922  put_in_active_queue(q, i);
1923  }
1924  } // fin while (true)
1925 
1926  if (not leave_residual)
1927  {
1928  net.unmake_residual_net();
1929  I(source->out_flow == sink->in_flow);
1930  net.unmake_super_nodes();
1931  }
1932 
1933  return source->out_flow;
1934 }
1953  template <class Net> typename Net::Flow_Type
1955  (Net & net, const bool & leave_residual = false)
1956 {
1957  typedef DynSetTreap<typename Net::Node*> Active;
1958  typedef DynListStack<typename Net::Arc*> Stack;
1959 
1960  return generic_preflow_edge_maximum_flow <Net,Active,Stack>
1961  (net, leave_residual);
1962 }
1963 
1969 template <class Net> class Depth_First_Preflow_Maximum_Flow
1970 {
1971 public:
1972 
1985  typename Net::Flow_Type
1986  operator () (Net & net,
1987  const bool & leave_residual = false) const
1988  {
1989  return depth_first_preflow_edge_maximum_flow(net, leave_residual);
1990  }
1991 };
1992 
2013  template <class Net> typename Net::Flow_Type
2015  (Net & net, const bool leave_residual = false)
2016 {
2017  typedef DynSetTreap<typename Net::Node*> Active;
2018  typedef DynListQueue<typename Net::Arc*> Queue;
2019  return generic_preflow_edge_maximum_flow<Net, Active, Queue>
2020  (net, leave_residual);
2021 }
2022 
2028 template <class Net> class Breadth_First_Preflow_Maximum_Flow
2029 {
2030 public:
2043  typename Net::Flow_Type
2044  operator () (Net & net,
2045  const bool & leave_residual = false) const
2046  {
2047  return breadth_first_preflow_edge_maximum_flow(net, leave_residual);
2048  }
2049 };
2050 
2051 template <class Net> struct Compare_Arc
2052 {
2053  bool operator () (typename Net::Arc * a1, typename Net::Arc * a2) const
2054  {
2055  typename Net::Node * src1 = (typename Net::Node *) a1->src_node;
2056  typename Net::Node * src2 = (typename Net::Node *) a2->src_node;
2057  if (src1->counter == src2->counter)
2058  return a1->cap - a1->flow > a2->cap - a2->flow;
2059 
2060  return src1->counter > src2->counter;
2061  }
2062 };
2089  template <class Net> typename Net::Flow_Type
2091  (Net & net, const bool & leave_residual = false)
2092 {
2093  typedef DynSetTreap<typename Net::Node*> Active;
2095  return generic_preflow_edge_maximum_flow <Net, Active, Prio_Queue>
2096  (net, leave_residual);
2097 }
2098 
2104 template <class Net> class Priority_First_Preflow_Maximum_Flow
2105 {
2106 public:
2119  typename Net::Flow_Type
2120  operator () (Net & net,
2121  const bool & leave_residual = false) const
2122  {
2123  return priority_first_preflow_edge_maximum_flow(net, leave_residual);
2124  }
2125 };
2126 
2146  template <class Net> typename Net::Flow_Type
2148  (Net & net, const bool & leave_residual = false)
2149 {
2150  typedef DynSetTreap<typename Net::Node*> Active;
2151  typedef Random_Set <typename Net::Arc*> Random_Queue;
2152  return generic_preflow_edge_maximum_flow <Net, Active, Random_Queue>
2153  (net, leave_residual);
2154 }
2155 
2161 template <class Net> class Random_First_Preflow_Maximum_Flow
2162 {
2163 public:
2183  typename Net::Flow_Type
2184  operator () (Net & net,
2185  const bool & leave_residual = false) const
2186  {
2187  return random_first_preflow_edge_maximum_flow(net, leave_residual);
2188  }
2189 };
2190 
2191 
2192 static void * vs_ptr = NULL;
2193 
2194  template <class Net> static bool
2195 add_vertex_to_vs(Net &, typename Net::Node * node, typename Net::Arc*)
2196 {
2197  ((Aleph::set<typename Net::Node*>*)vs_ptr)->insert(node);
2198  return false; // indica que debe seguir explorando
2199 }
2200 
2208 template <class N> class No_Res_Arc
2209 {
2210 public:
2211 
2212  bool operator () (typename N::Node *, typename N::Arc * a) const
2213  {
2214  return not a->is_residual;
2215  }
2216 
2217  bool operator () (N&, typename N::Arc * a) const
2218  {
2219  return not a->is_residual;
2220  }
2221 
2222  bool operator () (typename N::Arc * a) const
2223  {
2224  return not a->is_residual;
2225  }
2226 };
2227 
2260  template <class Net, template <class> class Maxflow>
2261 typename Net::Flow_Type min_cut(Net & net,
2266  const bool leave_residual = false)
2267 {
2268  Maxflow <Net> () (net, true); // calcula flujo máximo y red residual
2269 
2270  typename Net::Node * source = net.get_source();
2271 
2272  vs_ptr = &vs;
2273  depth_first_traversal <Net, Res_F<Net>>
2274  (net, source, &add_vertex_to_vs);
2275  const size_t size_vt = net.get_num_nodes() - vs.size();
2276  for (Node_Iterator<Net> it(net); it.has_current() and
2277  vt.size() < size_vt; it.next())
2278  {
2279  typename Net::Node * p = it.get_current();
2280  if (not IS_NODE_VISITED(p, Aleph::Depth_First))
2281  vt.insert(p);
2282  }
2283 
2284  for (Arc_Iterator<Net, No_Res_Arc<Net> > it(net); it.has_curr(); it.next())
2285  {
2286  typename Net::Arc * arc = it.get_current();
2287  if (arc->flow == 0) // ¿candidato a arco de retroceso?
2288  if (vt.count(net.get_src_node(arc)) > 0 and // ¿de vt hacia vs?
2289  vs.count(net.get_tgt_node(arc)) > 0)
2290  {
2291  cutt.insert(arc);
2292  continue;
2293  }
2294 
2295  if (arc->flow == arc->cap) // ¿candidato a arco de cruce?
2296  if (vs.count(net.get_src_node(arc)) > 0 and // ¿de vs hacia vt?
2297  vt.count(net.get_tgt_node(arc)) > 0)
2298  cuts.insert(arc);
2299  }
2300 
2301  if (not leave_residual)
2302  {
2303  net.unmake_residual_net();
2304  net.unmake_super_nodes();
2305  }
2306 
2307  return source->out_flow;
2308 }
2309 
2324  template <class Net,
2325  template <class> class Maxflow = Heap_Preflow_Maximum_Flow>
2326 class Min_Cut
2327 {
2328 public:
2342  typename Net::Flow_Type operator () (Net & net,
2347  {
2348  return min_cut <Net, Maxflow> (net, vs, vt, cuts, cutt);
2349  }
2350 };
2351 
2361 template <class N> class Flow_Filter
2362 {
2363 
2364 public:
2365 
2367  static typename N::Flow_Type min;
2368 
2369  typename N::Arc * operator () (typename N::Node *, typename N::Arc * arc)
2370  {
2371  return arc->cap - arc->flow >= min ? arc : NULL;
2372  }
2373 
2374  typename N::Arc * operator () (typename N::Arc * arc)
2375  {
2376  return arc->cap - arc->flow >= min ? arc : NULL;
2377  }
2378 };
2379 
2380 template <class N> typename N::Flow_Type Flow_Filter<N>::min = 0;
2381 
2402  template <class Net, template <class, class> class Find_Path>
2403 bool find_aumenting_path(Net & net, Path<Net> & path,
2404  const typename Net::Flow_Type & min_slack)
2405 {
2406  Flow_Filter<Net>::min = min_slack;
2407  typename Net::Node * src = net.get_source();
2408  typename Net::Node * sink = net.get_sink();
2409 
2410  if (not net.residual_net)
2411  net.make_residual_net();
2412 
2413  return Find_Path<Net, Flow_Filter<Net>> () (net, src, sink, path);
2414 }
2415 
2428 template <class Net,
2429  template <class, class> class Find_Path = Find_Path_Breadth_First>
2431 {
2432 public:
2445  bool operator () (Net & net, Path<Net> & path,
2446  const typename Net::Flow_Type & slack)
2447  {
2448  return find_aumenting_path <Net, Find_Path> (net, path, slack);
2449  }
2450 };
2451 
2472  template <class Net, template <class, class> class Find_Path>
2473 bool find_decrementing_path(Net & net, Path<Net> & path,
2474  const typename Net::Flow_Type & min_slack)
2475 {
2476  Flow_Filter<Net>::min = min_slack;
2477  typename Net::Node * src = net.get_source();
2478  typename Net::Node * sink = net.get_sink();
2479 
2480  if (not net.residual_net)
2481  net.make_residual_net();
2482 
2483  return Find_Path<Net, Flow_Filter<Net> > () (net, sink, src, path);
2484 }
2485 
2498 template <class Net,
2499  template <class, class> class Find_Path = Find_Path_Breadth_First>
2501 {
2502 public:
2515  bool operator () (Net & net, Path<Net> & path,
2516  const typename Net::Flow_Type & slack)
2517  {
2518  return find_decrementing_path <Net, Find_Path> (net, path, slack);
2519  }
2520 };
2521 
2551  template <class Net>
2552 void increase_flow(Net & net, Path<Net> & path,
2553  const typename Net::Flow_Type & val)
2554 {
2555  if (not net.residual_net)
2556  throw std::domain_error("Residual net is not created");
2557 
2558  // aumentar flujo de red por el camino de aumento
2559  for (typename Path<Net>::Iterator it(path); it.has_current_arc(); it.next())
2560  {
2561  typename Net::Arc * arc = it.get_current_arc();
2562  typename Net::Arc * img = (typename Net::Arc *) arc->img_arc;
2563  if (arc->cap - arc->flow < val)
2564  throw std::domain_error("minimum slack of aumenting path es"
2565  " smaller than val");
2566 
2567  arc->flow += val;
2568  img->flow -= val;
2569  if (not arc->is_residual)
2570  {
2571  net.increase_out_flow(net.get_src_node(arc), val);
2572  net.increase_in_flow(net.get_tgt_node(arc), val);
2573  }
2574  else
2575  {
2576  net.decrease_in_flow(net.get_src_node(arc), val);
2577  net.decrease_out_flow(net.get_tgt_node(arc), val);
2578  }
2579  }
2580 }
2581 
2612  template <class Net>
2613 void decrease_flow(Net & net, Path<Net> & path,
2614  const typename Net::Flow_Type & val)
2615 {
2616  if (not net.residual_net)
2617  throw std::domain_error("Residual net is not created");
2618 
2619  // decrementar flujo de red por camino de decremento
2620  for (typename Path<Net>::Iterator it(path); it.has_current_arc(); it.next())
2621  {
2622  typename Net::Arc * arc = it.get_current_arc();
2623  typename Net::Arc * img = (typename Net::Arc *) arc->img_arc;
2624 
2625  if (arc->cap - arc->flow < val)
2626  throw std::domain_error("minimum slack of decrementing path "
2627  " is smaller than val");
2628 
2629  arc->flow -= val;
2630  img->flow += val;
2631  if (not arc->is_residual)
2632  {
2633  net.decrease_out_flow(get_src_node(arc), val);
2634  net.decrease_in_flow(net.get_tgt_node(arc), val);
2635  }
2636  else
2637  {
2638  net.increase_out_flow(get_tgt_node(arc), val);
2639  net.increase_in_flow(net.get_src_node(arc), val);
2640  }
2641  }
2642 }
2643 
2644 } // end namespace Aleph
2645 
2646 # endif // TPL_NETGRAPH_H
Node::Node_Type Node_Type
Tipo de atributo que almacena un nodo.
Definition: tpl_netgraph.H:221
Definition: tpl_netgraph.H:2051
Definition: tpl_agraph.H:554
void unmake_residual_net()
Definition: tpl_netgraph.H:919
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const typename Arc::Arc_Type &arc_info)
Definition: tpl_netgraph.H:617
Definition: tpl_netgraph.H:2161
Net::Flow_Type generic_preflow_vertex_push_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:1431
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
Definition: tpl_netgraph.H:2361
Definition: tpl_netgraph.H:2028
Flow_Type get_out_cap(Node *node) const
Retorna la capacidad total de salida del nodo node.
Definition: tpl_netgraph.H:234
Arc * connect_arc(Arc *arc)
Definition: tpl_netgraph.H:558
Definition: tpl_netgraph.H:1298
Node * insert_node(Node *p)
Definition: tpl_netgraph.H:474
static N::Flow_Type min
Valor de flujo cota a filtrar.
Definition: tpl_netgraph.H:2367
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap)
Definition: tpl_netgraph.H:595
Definition: tpl_netgraph.H:66
Net::Flow_Type random_first_preflow_edge_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:2148
Net::Flow_Type ford_fulkerson_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:1220
void unmake_super_sink()
Definition: tpl_netgraph.H:383
NodeT Node
Tipo de node.
Definition: tpl_netgraph.H:215
F_Type Flow_Type
Tipo que representa el flujo.
Definition: tpl_netgraph.H:146
Definition: tpl_netgraph.H:2104
Definition: tpl_dynDlist.H:26
Definition: tpl_graph.H:751
Definition: tpl_netgraph.H:2430
Node * get_sink()
Retorna un nodo sumidero de la red.
Definition: tpl_netgraph.H:422
Arc * get_residual_arc(Arc *a)
Obtiene el arco residual de a.
Definition: tpl_netgraph.H:941
Definition: tpl_netgraph.H:1568
Net::Flow_Type operator()(Net &net, const bool &leave_residual=false) const
Definition: tpl_netgraph.H:2044
#define IS_NODE_VISITED(p, bit)
Definition: aleph-graph.H:242
Definition: tpl_netgraph.H:1708
Net::Flow_Type operator()(Net &net, const bool &leave_residual=false) const
Definition: tpl_netgraph.H:2120
bool is_sink(Node *node)
Retorna true si node es sumidero.
Definition: tpl_netgraph.H:254
void disconnect_arc(Arc *arc)
Definition: tpl_netgraph.H:678
Definition: tpl_find_path.H:158
Definition: tpl_dynListStack.H:20
Net_Arc * img_arc
apunta al arco reflejo
Definition: tpl_netgraph.H:85
void decrease_flow(Net &net, Path< Net > &path, const typename Net::Flow_Type &val)
Definition: tpl_netgraph.H:2613
ArcT Arc
Tipo de arco.
Definition: tpl_netgraph.H:212
Net_Graph(Digraph &digraph)
Definition: tpl_netgraph.H:706
Aleph::set< Node * > & get_sink_nodes()
Retorna el conjunto de nodos sumidero que contiene la red.
Definition: tpl_netgraph.H:312
Definition: tpl_dynListQueue.H:22
iterator begin()
Retorna un iterador posicionado al primer elemento del conjunto.
Definition: Set.H:459
Definition: tpl_netgraph.H:1341
Definition: tpl_netgraph.H:49
#define NODE_COUNTER(p)
Definition: aleph-graph.H:226
size_type erase(const T &value)
Definition: Set.H:604
Net::Flow_Type priority_first_preflow_edge_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:2091
bool is_connected(Node *node) const
Definition: tpl_netgraph.H:259
bool check_node(Node *node)
Definition: tpl_netgraph.H:266
virtual void remove_arc(Arc *arc)
Elimina de la red el arco arc.
Definition: tpl_netgraph.H:648
Net_Graph(Net_Graph &net)
Definition: tpl_netgraph.H:715
Net::Flow_Type operator()(Net &net, const bool &leave_residual=false) const
Definition: tpl_netgraph.H:1663
bool is_source(Node *node)
Retorna true si node es fuente.
Definition: tpl_netgraph.H:251
void increase_flow(Net &net, Path< Net > &path, const typename Net::Flow_Type &val)
Definition: tpl_netgraph.H:2552
void make_super_sink()
Definition: tpl_netgraph.H:358
bool find_decrementing_path(Net &net, Path< Net > &path, const typename Net::Flow_Type &min_slack)
Definition: tpl_netgraph.H:2473
#define IS_ARC_VISITED(p, bit)
Definition: aleph-graph.H:275
Flow_Type flow_value()
Definition: tpl_netgraph.H:809
void unmake_super_source()
Definition: tpl_netgraph.H:344
void make_residual_net()
Definition: tpl_netgraph.H:889
Net::Flow_Type depth_first_preflow_edge_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:1955
Definition: tpl_dynBinHeap.H:26
Node * get_source()
Retorna un nodo fuente de la red.
Definition: tpl_netgraph.H:419
bool has_aumenting_path(Net &net)
Definition: tpl_netgraph.H:1104
Definition: tpl_netgraph.H:2208
Definition: tpl_graph.H:26
Definition: tpl_agraph.H:13
Net::Flow_Type operator()(Net &net, const bool &leave_residual=false) const
Definition: tpl_netgraph.H:1316
bool is_residual
indica si el arco es o no residual
Definition: tpl_netgraph.H:88
virtual Arc * insert_arc(Node *src_node, Node *tgt_node)
Definition: tpl_netgraph.H:642
void set_flow(Arc *arc, const Flow_Type &flow)
Definition: tpl_netgraph.H:750
void make_super_source()
Definition: tpl_netgraph.H:319
std::pair< iterator, bool > insert(const T &value)
Definition: Set.H:488
const Flow_Type & get_flow(Arc *arc) const
retorna el valor de capacidad del arco.
Definition: tpl_netgraph.H:768
Net::Flow_Type edmonds_karp_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:1286
Arc::Flow_Type Flow_Type
Tipo que representa la capacidad y el flujo.
Definition: tpl_netgraph.H:218
size_t get_in_degree(Node *node) const
Definition: tpl_netgraph.H:238
Arc::Arc_Type Arc_Type
Tipo de atributo que almacena un arco.
Definition: tpl_netgraph.H:224
Net::Flow_Type operator()(Net &net, Aleph::set< typename Net::Node * > &vs, Aleph::set< typename Net::Node * > &vt, DynDlist< typename Net::Arc * > &cuts, DynDlist< typename Net::Arc * > &cutt)
Definition: tpl_netgraph.H:2342
Flow_Type out_cap
Capacidad total de salida (suma de caps arcos salida)
Definition: tpl_netgraph.H:152
Definition: tpl_random_queue.H:23
bool find_aumenting_path(Net &net, Path< Net > &path, const typename Net::Flow_Type &min_slack)
Definition: tpl_netgraph.H:2403
const Flow_Type & get_cap(Arc *arc) const
Retorna el valor de flujo de un arco.
Definition: tpl_netgraph.H:771
bool with_super_source
true si a la red se le ha añadido un supra fuente.
Definition: tpl_netgraph.H:812
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
bool with_super_sink
true si a la red se le ha añadido un supra sumidero.
Definition: tpl_netgraph.H:815
Flow_Type get_out_flow(Node *node) const
Retorna el valor de flujo de salida del nodo.
Definition: tpl_netgraph.H:245
Net::Flow_Type operator()(Net &net, const bool &leave_residual=false) const
Definition: tpl_netgraph.H:1724
Net::Flow_Type random_preflow_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:1697
Net::Flow_Type min_cut(Net &net, Aleph::set< typename Net::Node * > &vs, Aleph::set< typename Net::Node * > &vt, DynDlist< typename Net::Arc * > &cuts, DynDlist< typename Net::Arc * > &cutt, const bool leave_residual=false)
Definition: tpl_netgraph.H:2261
Flow_Type get_in_flow(Node *node) const
Retorna el valor de flujo de entrada del nodo.
Definition: tpl_netgraph.H:248
Definition: tpl_netgraph.H:2500
void make_super_nodes()
Definition: tpl_netgraph.H:396
Flow_Type in_cap
Capacidad total de entrada (suma de caps arcos entrada)
Definition: tpl_netgraph.H:155
Definition: tpl_agraph.H:130
Net::Flow_Type generic_preflow_edge_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:1786
Net_Node(Net_Node *net_node)
Definition: tpl_netgraph.H:179
bool has_current_arc() const
Definition: tpl_graph.H:1908
#define NODE_BITS(p)
Definition: aleph-graph.H:221
Definition: tpl_netgraph.H:2326
void set_cap(Arc *arc, const Flow_Type &cap)
Coloca valor de capacidad a un arco.
Definition: tpl_netgraph.H:731
Aleph::set< Node * > & get_src_nodes()
Retorna el conjunto de nodos fuente que contiene la red.
Definition: tpl_netgraph.H:309
Net::Flow_Type operator()(Net &net, const bool &leave_residual=false) const
Definition: tpl_netgraph.H:1986
size_t get_out_degree(Node *node) const
Definition: tpl_netgraph.H:242
Net::Flow_Type operator()(Net &net, const bool &leave_residual=false) const
Definition: tpl_netgraph.H:1585
Net::Flow_Type operator()(Net &net, const bool &leave_residual=false) const
Definition: tpl_netgraph.H:1250
Net::Flow_Type fifo_preflow_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:1556
bool operator()(Net &net, Path< Net > &path, const typename Net::Flow_Type &slack)
Definition: tpl_netgraph.H:2445
Net::Flow_Type aumenting_path_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:1147
size_type count(const T &value)
Definition: Set.H:373
bool operator()(Net &net, Path< Net > &path, const typename Net::Flow_Type &slack)
Definition: tpl_netgraph.H:2515
Definition: tpl_netgraph.H:205
Definition: tpl_netgraph.H:1593
Definition: tpl_graph.H:814
Definition: tpl_netgraph.H:1231
bool check_arc() const
Definition: tpl_netgraph.H:82
size_t in_degree
Cantidad de arcos entrantes.
Definition: tpl_netgraph.H:149
Flow_Type in_flow
Valor de flujo entrante.
Definition: tpl_netgraph.H:161
Definition: tpl_netgraph.H:1969
void copy_graph(GT &gtgt, GT &gsrc, const bool cookie_map=false)
Definition: tpl_graph.H:2505
Definition: tpl_netgraph.H:1646
F_Type Flow_Type
Tipo que representa el flujo.
Definition: tpl_netgraph.H:71
Node * insert_node()
Definition: tpl_netgraph.H:461
bool is_there_residual_net() const
Retorna true si la red residual está creada; false de lo contrario.
Definition: tpl_netgraph.H:938
Node * insert_node(const Node_Type &node_info)
Definition: tpl_netgraph.H:434
virtual void remove_node(Node *p)
Elimina un nodo de una red de flujo junto con todos sus arcos.
Definition: tpl_netgraph.H:697
Flow_Type get_in_cap(Node *node) const
Retorna la capacidad total de entrada del nodo node.
Definition: tpl_netgraph.H:231
Net::Flow_Type operator()(Net &net, const bool &leave_residual=false) const
Definition: tpl_netgraph.H:2184
Definition: Set.H:69
size_type size()
Retorna la cantidad de elementos que contiene el conjunto.
Definition: Set.H:275
Flow_Type cap
valor de capacidad
Definition: tpl_netgraph.H:74
void unmake_super_nodes()
Definition: tpl_netgraph.H:412
iterator end()
Retorna un iterador posicionado al último elemento del conjunto.
Definition: Set.H:465
Net_Node(const Node_Info &node_info)
Definition: tpl_netgraph.H:170
Definition: tpl_graph.H:694
Definition: tpl_dynSetTree.H:1174
bool check_network()
Definition: tpl_netgraph.H:797
Net::Flow_Type breadth_first_preflow_edge_maximum_flow(Net &net, const bool leave_residual=false)
Definition: tpl_netgraph.H:2015
T remove_first()
Definition: tpl_dynDlist.H:218
Definition: tpl_graph.H:1868
Net::Flow_Type heap_preflow_maximum_flow(Net &net, const bool &leave_residual=false)
Definition: tpl_netgraph.H:1633
T & insert(const T &item)
Definition: tpl_dynDlist.H:86
Definition: tpl_netgraph.H:141
T & append(const T &item)
Definition: tpl_dynDlist.H:106
Flow_Type flow
valor de flujo
Definition: tpl_netgraph.H:77
Flow_Type out_flow
Valor de flujo saliente.
Definition: tpl_netgraph.H:158

Leandro Rabindranath León