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_net_sup_dem.H
1 #line 620 "reducciones.nw"
2 # ifndef TPL_NET_SUP_DEM_H
3 # define TPL_NET_SUP_DEM_H
4 
5 # include <tpl_netgraph.H>
6 
7 namespace Aleph {
8 
9 #line 586 "reducciones.nw"
10 
14  template <typename Node_Info, typename F_Type = long>
15 class Net_Sup_Dem_Node : public Net_Node<Node_Info, F_Type>
16 {
17 public:
19  typedef F_Type Flow_Type;
20 
21  Flow_Type supply_flow;
22  Flow_Type & get_supply_flow() { return supply_flow; }
24 
25  Net_Sup_Dem_Node() : supply_flow(0) { /* empty */ }
26 
29  Net_Sup_Dem_Node(const Node_Info & node_info)
30  : Net_Node<Node_Info, F_Type>(node_info), supply_flow(0) { /* empty */ }
31 
35  : Net_Node<Node_Info, F_Type>(*node), supply_flow(node->supply_flow) { /* empty */ }
36 };
37 
38 #line 629 "reducciones.nw"
39 
55  template <class NodeT = Net_Sup_Dem_Node<Empty_Class, double>,
56  class ArcT = Net_Arc<Empty_Class, double> >
57 class Net_Sup_Dem_Graph : public Net_Graph<NodeT, ArcT>
58 {
59 public:
63  typedef ArcT Arc;
65  typedef NodeT Node;
67  typedef typename Arc::Flow_Type Flow_Type;
69  typedef typename Node::Node_Type Node_Type;
71  typedef typename Arc::Arc_Type Arc_Type;
72 
73 
74 #line 675 "reducciones.nw"
75 
85 Node * insert_node(const Node_Type & node_info, const Flow_Type & supply = 0)
86 {
87  Node * p = Net_Class::insert_node(node_info);
88  p->supply_flow = supply;
89  return p;
90 }
100 Node * insert_node(const Flow_Type & supply = 0)
101 {
102  return insert_node(Node_Type(), supply);
103 }
104 #line 719 "reducciones.nw"
105 private:
106 
107  Node * super_source;
108  Node * super_sink;
109 
110 public:
111  Net_Sup_Dem_Graph() : super_source(NULL), super_sink(NULL) { /* empty */ }
113  bool exist_aux_net() const
114  {
115  return super_source != NULL or super_sink != NULL;
116  }
117 
118 #line 739 "reducciones.nw"
119 typedef Net_Sup_Dem_Graph Aux_Net;
121 
144 {
145  if (exist_aux_net())
146  throw std::domain_error("Auxiliar net is already computed");
147  if (this->residual_net)
148  throw std::domain_error("Residual net has been already computed");
149 
150  super_source = insert_node(); // fuente auxiliar
151  super_sink = insert_node(); // sumidero auxiliar
152  // recorrer nodos y atar arcos a fuente o sumidero según signo provisión
153  for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_current(); it.next())
154  {
155  Node * p = it.get_current();
156  if (p->supply_flow > 0) // ¿nodo proveedor?
157  {
158  if (p->out_cap < p->supply_flow)
159  throw std::range_error(gnu::autosprintf("Supply flow in node at %p is "
160  "greater than out capacity", p));
161  insert_arc(super_source, p, p->supply_flow);
162  }
163  else if (p->supply_flow < 0) // ¿nodo demandante?
164  {
165  if (p->in_cap < -p->supply_flow)
166  throw std::range_error(gnu::autosprintf("Supply flow in node at %p is "
167  "smaller than in capacity", p));
168  insert_arc(p, super_sink, -p->supply_flow);
169  }
170  }
171  if (this->get_out_degree(super_source) == 0)
172  {
173  remove_node(super_source);
174  super_source = NULL;
175  }
176  if (this->get_in_degree(super_sink) == 0)
177  {
178  remove_node(super_sink);
179  super_sink = NULL;
180  }
181  return this;
182 }
183 #line 825 "reducciones.nw"
184  Net_Sup_Dem_Graph * get_aux_net() { return exist_aux_net() ? this : NULL; }
185 #line 833 "reducciones.nw"
186 
193 bool is_feasible() const
194 {
195  if (not exist_aux_net())
196  throw std::domain_error("Auxiliar net has not been computed");
197  for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_current(); it.next())
198  {
199  Node * p = it.get_current();
200  Flow_Type & supply_flow = p->supply_flow;
201  if (supply_flow == 0)
202  continue;
203  if (supply_flow > 0 and p->out_flow < supply_flow)
204  return false;
205  else if (supply_flow < 0 and p->in_flow < -supply_flow)
206  return true;
207  }
208  return true;
209 }
210 
226 void non_feasible_nodes(DynDlist<Node *> & supply_list, DynDlist<Node *> & demand_list)
227 {
228  for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_current(); it.next())
229  {
230  Node * p = it.get_current();
231  Flow_Type & supply_flow = p->supply_flow;
232  if (supply_flow == 0)
233  continue;
234  if (supply_flow > 0 and p->out_flow < supply_flow)
235  supply_list.append(p);
236  else if (supply_flow < 0 and p->in_flow < -supply_flow)
237  demand_list.append(p);
238  }
239 }
240 #line 895 "reducciones.nw"
241 
254 void set_supply_flow(Node * p, const Flow_Type & supply)
255 {
256  if (supply > 0 and p->out_cap < supply)
257  throw std::range_error(gnu::autosprintf("Supply flow in node at %p is "
258  "greater than out capacity", p));
259  else if (supply < 0 and p->in_cap < -supply)
260  throw std::range_error(gnu::autosprintf("Supply flow in node at %p is "
261  "smaller than in capacity", p));
262  p->supply_flow = supply;
263 }
264 #line 922 "reducciones.nw"
265 void free_aux_net()
267 {
268  if (not exist_aux_net())
269  throw std::domain_error("Auxiliar net has not been computed");
270 
271  if (this->residual_net)
272  {
273  this->unmake_residual_net();
274  this->unmake_super_nodes();
275  }
276  if (super_source != NULL)
277  {
278  remove_node(super_source);
279  super_source = NULL;
280  }
281  if (super_sink != NULL)
282  {
283  remove_node(super_sink);
284  super_sink = NULL;
285  }
286 }
287 #line 664 "reducciones.nw"
288 };
289 
290 } // end namespace Aleph
291 
292 # endif // TPL_NET_SUP_DEM_H
bool exist_aux_net() const
Retorna true si la red auxiliar ya ha sido creada.
Definition: tpl_net_sup_dem.H:113
void unmake_residual_net()
Definition: tpl_netgraph.H:919
Definition: tpl_net_sup_dem.H:57
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
F_Type Flow_Type
Tipo que representa el flujo.
Definition: tpl_net_sup_dem.H:19
Net_Sup_Dem_Graph Aux_Net
El tipo de red capacitada auxiliar equivalente.
Definition: tpl_net_sup_dem.H:120
NodeT Node
Tipo de node.
Definition: tpl_net_sup_dem.H:65
Net_Sup_Dem_Node(Net_Sup_Dem_Node *node)
Definition: tpl_net_sup_dem.H:34
void non_feasible_nodes(DynDlist< Node * > &supply_list, DynDlist< Node * > &demand_list)
Definition: tpl_net_sup_dem.H:226
Definition: tpl_dynDlist.H:26
Definition: tpl_net_sup_dem.H:15
void free_aux_net()
Libera memoria de la red extendida.
Definition: tpl_net_sup_dem.H:266
Node * insert_node(const Node_Type &node_info, const Flow_Type &supply=0)
Definition: tpl_net_sup_dem.H:85
Node::Node_Type Node_Type
Tipo de atributo que almacena un nodo.
Definition: tpl_net_sup_dem.H:69
Node * insert_node(const Flow_Type &supply=0)
Definition: tpl_net_sup_dem.H:100
size_t get_in_degree(Node *node) const
Definition: tpl_netgraph.H:238
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
Net_Sup_Dem_Graph * compute_aux_net()
Definition: tpl_net_sup_dem.H:143
size_t get_out_degree(Node *node) const
Definition: tpl_netgraph.H:242
Net_Sup_Dem_Node(const Node_Info &node_info)
Definition: tpl_net_sup_dem.H:29
Definition: tpl_netgraph.H:205
Definition: tpl_graph.H:814
void set_supply_flow(Node *p, const Flow_Type &supply)
Definition: tpl_net_sup_dem.H:254
Net_Graph< NodeT, ArcT > Net_Class
La clase de red tradicional.
Definition: tpl_net_sup_dem.H:61
Flow_Type & get_supply_flow()
Retorna el valor de provisión del nodo.
Definition: tpl_net_sup_dem.H:23
Node * insert_node()
Definition: tpl_netgraph.H:461
virtual void remove_node(Node *p)
Elimina un nodo de una red de flujo junto con todos sus arcos.
Definition: tpl_netgraph.H:697
ArcT Arc
Tipo de arco.
Definition: tpl_net_sup_dem.H:63
void unmake_super_nodes()
Definition: tpl_netgraph.H:412
Arc::Flow_Type Flow_Type
Tipo que representa la capacidad y el flujo.
Definition: tpl_net_sup_dem.H:67
bool is_feasible() const
Definition: tpl_net_sup_dem.H:193
Definition: tpl_netgraph.H:141
T & append(const T &item)
Definition: tpl_dynDlist.H:106
Arc::Arc_Type Arc_Type
Tipo de atributo que almacena un arco.
Definition: tpl_net_sup_dem.H:71

Leandro Rabindranath León