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_netcapgraph.H
1 # ifndef TPL_NETCAPGRAPH_H
2 # define TPL_NETCAPGRAPH_H
3 # include <tpl_netgraph.H>
4 
5 namespace Aleph {
6 
14  template <typename Node_Info, typename F_Type = double>
15 class Net_Cap_Node : public Net_Node<Node_Info, F_Type>
16 {
17 public:
19  typedef F_Type Flow_Type;
20 
21  Flow_Type max_cap;
22 
23  Net_Cap_Node() : max_cap(numeric_limits<Flow_Type>::max()) {}
24 
27  Net_Cap_Node(const Node_Info & node_info)
28  : Net_Node<Node_Info, F_Type>(node_info),
29  max_cap(numeric_limits<Flow_Type>::max())
30  { /* empty */ }
31 
35  : Net_Node<Node_Info, F_Type>(*node), max_cap(node->max_cap) { /* empty */ }
36 };
37 
61  template <class NodeT, class ArcT>
62 class Net_Cap_Graph : public Net_Graph<NodeT, ArcT>
63 {
64 public:
68  typedef ArcT Arc;
70  typedef NodeT Node;
72  typedef typename Arc::Flow_Type Flow_Type;
74  typedef typename Node::Node_Type Node_Type;
76  typedef typename Arc::Arc_Type Arc_Type;
77 
88  Node * insert_node(const Node_Type & node_info,
89  const Flow_Type & cap = numeric_limits<Flow_Type>::max())
90  {
91  Node * p = Net_Class::insert_node(node_info);
92  p->max_cap = cap;
93  return p;
94  }
117  private:
118  Aux_Net * aux_net;
119  public:
120  Net_Cap_Graph() : aux_net(NULL) { /* empty */ }
123  Aux_Net * get_aux_net() { return aux_net; }
138  {
139  if (aux_net != NULL)
140  throw std::domain_error("aux_net has already computed");
141  aux_net = new Aux_Net;
142  try
143  {
144  for (Node_Iterator<Net_Cap_Graph> it(*this); it.has_current(); it.next())
145  {
146  Node * p = it.get_current();
147  typename Aux_Net::Node * src = aux_net->insert_node();
148  typename Aux_Net::Node * tgt = aux_net->insert_node();
149  typename Aux_Net::Arc * arc =
150  aux_net->insert_arc(src, tgt, true, p->max_cap, 0);
151  NODE_COOKIE(p) = arc;
152  ARC_COOKIE(arc) = p;
153  }
154  for (Arc_Iterator<Net_Cap_Graph> it(*this); it.has_current(); it.next())
155  {
156  Arc * arc = it.get_current();
157  typename Aux_Net::Arc * src_arc =
158  (typename Aux_Net::Arc *) NODE_COOKIE(get_src_node(arc));
159  typename Aux_Net::Arc * tgt_arc =
160  (typename Aux_Net::Arc *) NODE_COOKIE(get_tgt_node(arc));
161  typename Aux_Net::Arc * a =
162  aux_net->insert_arc(aux_net->get_tgt_node(src_arc),
163  aux_net->get_src_node(tgt_arc), false,
164  arc->cap, arc->flow);
165  ARC_COOKIE(arc) = a;
166  ARC_COOKIE(a) = arc;
167  }
168  return aux_net;
169  }
170  catch (...)
171  {
172  free_aux_net();
173  }
174  }
189  void update()
190  {
191  if (aux_net == NULL)
192  throw std::domain_error("Auxiliar net has not been generated");
193 
194  for (Arc_Iterator<Aux_Net, No_Res_Arc<Aux_Net> > it(*aux_net);
195  it.has_current(); it.next())
196  {
197  typename Aux_Net::Arc * arc = it.get_current();
198  if (arc->get_info())
199  {
200  Node * p = (Node*) ARC_COOKIE(arc);
201  p->in_flow = p->out_flow = arc->flow;
202  }
203  else
204  {
205  Arc * a = (Arc*) ARC_COOKIE(arc);
206  a->flow = arc->flow;
207  }
208  }
209  }
213  {
214  if (aux_net == NULL)
215  throw std::domain_error("Auxiliar net has not been generated");
216 
217  clear_graph(*aux_net);
218  delete aux_net;
219  aux_net = NULL;
220  }
221 
222  ~Net_Cap_Graph()
223  {
224  if (aux_net != NULL)
225  free_aux_net();
226  }
227 };
228 
229 } // end namespace Aleph
230 
231 # endif // endif
Node * insert_node(const Node_Type &node_info, const Flow_Type &cap=numeric_limits< Flow_Type >::max())
Definition: tpl_netcapgraph.H:88
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
Net_Cap_Node(const Node_Info &node_info)
Definition: tpl_netcapgraph.H:27
Definition: tpl_netgraph.H:66
#define NODE_COOKIE(p)
Definition: aleph-graph.H:248
Definition: tpl_netcapgraph.H:15
Definition: tpl_netcapgraph.H:62
void free_aux_net()
Definition: tpl_netcapgraph.H:212
F_Type Flow_Type
Tipo que representa el flujo.
Definition: tpl_netcapgraph.H:19
NodeT Node
Tipo de node.
Definition: tpl_netgraph.H:215
Definition: tpl_graph.H:751
ArcT Arc
Tipo de arco.
Definition: tpl_netcapgraph.H:68
ArcT Arc
Tipo de arco.
Definition: tpl_netgraph.H:212
Aux_Net * compute_aux_net()
Definition: tpl_netcapgraph.H:137
Net_Graph< NodeT, ArcT > Net_Class
La clase de red tradicional.
Definition: tpl_netcapgraph.H:66
#define ARC_COOKIE(p)
Definition: aleph-graph.H:281
Net_Graph< Net_Node< Empty_Class, Flow_Type >, Net_Arc< bool, Flow_Type > > Aux_Net
Definition: tpl_netcapgraph.H:116
Net_Cap_Node(Net_Cap_Node *node)
Definition: tpl_netcapgraph.H:34
void clear_graph(GT &g)
Definition: tpl_graph.H:2486
void update()
Definition: tpl_netcapgraph.H:189
Node::Node_Type Node_Type
Tipo de atributo que almacena un nodo.
Definition: tpl_netcapgraph.H:74
Definition: tpl_netgraph.H:2208
Arc::Flow_Type Flow_Type
Tipo que representa la capacidad y el flujo.
Definition: tpl_netcapgraph.H:72
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
NodeT Node
Tipo de node.
Definition: tpl_netcapgraph.H:70
Definition: tpl_netgraph.H:205
Definition: tpl_graph.H:814
Aux_Net * get_aux_net()
Definition: tpl_netcapgraph.H:123
Node * insert_node()
Definition: tpl_netgraph.H:461
Node * insert_node(const Node_Type &node_info)
Definition: tpl_netgraph.H:434
Definition: tpl_netgraph.H:141
Arc::Arc_Type Arc_Type
Tipo de atributo que almacena un arco.
Definition: tpl_netcapgraph.H:76

Leandro Rabindranath León