Aleph-w  1.9
General library for algorithms and data structures
tpl_netcapgraph.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef TPL_NETCAPGRAPH_H
28 # define TPL_NETCAPGRAPH_H
29 # include <tpl_netgraph.H>
30 
31 namespace Aleph {
32 
40  template <typename Node_Info, typename F_Type = double>
41 class Net_Cap_Node : public Net_Node<Node_Info, F_Type>
42 {
43 public:
45  typedef F_Type Flow_Type;
46 
47  Flow_Type max_cap;
48 
49  Net_Cap_Node() : max_cap(numeric_limits<Flow_Type>::max()) {}
50 
53  Net_Cap_Node(const Node_Info & node_info)
54  : Net_Node<Node_Info, F_Type>(node_info),
55  max_cap(numeric_limits<Flow_Type>::max())
56  { /* empty */ }
57 
61  : Net_Node<Node_Info, F_Type>(*node), max_cap(node->max_cap) { /* empty */ }
62 };
63 
87  template <class NodeT, class ArcT>
88 class Net_Cap_Graph : public Net_Graph<NodeT, ArcT>
89 {
90 public:
94  typedef ArcT Arc;
96  typedef NodeT Node;
98  typedef typename Arc::Flow_Type Flow_Type;
100  typedef typename Node::Node_Type Node_Type;
102  typedef typename Arc::Arc_Type Arc_Type;
103 
114  Node * insert_node(const Node_Type & node_info,
115  const Flow_Type & cap = numeric_limits<Flow_Type>::max())
116  {
117  Node * p = Net_Class::insert_node(node_info);
118  p->max_cap = cap;
119  return p;
120  }
143  private:
144  Aux_Net * aux_net;
145  public:
146  Net_Cap_Graph() : aux_net(nullptr) { /* empty */ }
149  Aux_Net * get_aux_net() { return aux_net; }
163  Aux_Net * compute_aux_net()
164  {
165  if (aux_net != nullptr)
166  throw std::domain_error("aux_net has already computed");
167  aux_net = new Aux_Net;
168  try
169  {
170  for (Node_Iterator<Net_Cap_Graph> it(*this); it.has_curr();
171  it.next_ne())
172  {
173  Node * p = it.get_curr_ne();
174  typename Aux_Net::Node * src = aux_net->insert_node();
175  typename Aux_Net::Node * tgt = aux_net->insert_node();
176  typename Aux_Net::Arc * arc =
177  aux_net->insert_arc(src, tgt, true, p->max_cap, 0);
178  NODE_COOKIE(p) = arc;
179  ARC_COOKIE(arc) = p;
180  }
181  for (Arc_Iterator<Net_Cap_Graph> it(*this); it.has_curr();
182  it.next_ne())
183  {
184  Arc * arc = it.get_curr_ne();
185  typename Aux_Net::Arc * src_arc =
186  (typename Aux_Net::Arc *) NODE_COOKIE(get_src_node(arc));
187  typename Aux_Net::Arc * tgt_arc =
188  (typename Aux_Net::Arc *) NODE_COOKIE(get_tgt_node(arc));
189  typename Aux_Net::Arc * a =
190  aux_net->insert_arc(aux_net->get_tgt_node(src_arc),
191  aux_net->get_src_node(tgt_arc), false,
192  arc->cap, arc->flow);
193  ARC_COOKIE(arc) = a;
194  ARC_COOKIE(a) = arc;
195  }
196  return aux_net;
197  }
198  catch (...)
199  {
200  free_aux_net();
201  }
202  }
217  void update()
218  {
219  if (aux_net == nullptr)
220  throw std::domain_error("Auxiliar net has not been generated");
221 
222  for (Arc_Iterator<Aux_Net, No_Res_Arc<Aux_Net> > it(*aux_net);
223  it.has_curr(); it.next_ne())
224  {
225  typename Aux_Net::Arc * arc = it.get_curr_ne();
226  if (arc->get_info())
227  {
228  Node * p = (Node*) ARC_COOKIE(arc);
229  p->in_flow = p->out_flow = arc->flow;
230  }
231  else
232  {
233  Arc * a = (Arc*) ARC_COOKIE(arc);
234  a->flow = arc->flow;
235  }
236  }
237  }
241  {
242  if (aux_net == nullptr)
243  throw std::domain_error("Auxiliar net has not been generated");
244 
245  clear_graph(*aux_net);
246  delete aux_net;
247  aux_net = nullptr;
248  }
249 
250  ~Net_Cap_Graph()
251  {
252  if (aux_net != nullptr)
253  free_aux_net();
254  }
255 };
256 
257 } // end namespace Aleph
258 
259 # endif // endif
Node * insert_node(const Node_Type &node_info, const Flow_Type &cap=numeric_limits< Flow_Type >::max())
Definition: tpl_netcapgraph.H:114
void clear_graph(GT &g) noexcept
Definition: tpl_graph.H:3463
Definition: tpl_agraph.H:301
Net_Cap_Node(const Node_Info &node_info)
Definition: tpl_netcapgraph.H:53
#define NODE_COOKIE(p)
Definition: aleph-graph.H:333
Definition: tpl_netcapgraph.H:41
Definition: tpl_netcapgraph.H:88
void free_aux_net()
Definition: tpl_netcapgraph.H:240
F_Type Flow_Type
Tipo que representa el flujo.
Definition: tpl_netcapgraph.H:45
Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &flow, const typename Arc::Arc_Type &arc_info=Arc_Type())
Definition: tpl_net.H:575
Definition: tpl_net.H:82
Definition: tpl_net.H:216
ArcT Arc
Tipo de arco.
Definition: tpl_netcapgraph.H:94
Aux_Net * compute_aux_net()
Definition: tpl_netcapgraph.H:163
Net_Graph< NodeT, ArcT > Net_Class
La clase de red tradicional.
Definition: tpl_netcapgraph.H:92
#define ARC_COOKIE(p)
Definition: aleph-graph.H:366
Net_Graph< Net_Node< Empty_Class, Flow_Type >, Net_Arc< bool, Flow_Type > > Aux_Net
Definition: tpl_netcapgraph.H:142
Net_Cap_Node(Net_Cap_Node *node)
Definition: tpl_netcapgraph.H:60
void update()
Definition: tpl_netcapgraph.H:217
Node::Node_Type Node_Type
Tipo de atributo que almacena un nodo.
Definition: tpl_netcapgraph.H:100
NodeT Node
Tipo de node.
Definition: tpl_net.H:230
Definition: tpl_agraph.H:43
Arc::Flow_Type Flow_Type
Tipo que representa la capacidad y el flujo.
Definition: tpl_netcapgraph.H:98
Definition: ah-comb.H:35
ArcT Arc
Tipo de arco.
Definition: tpl_net.H:227
NodeInfo Node_Type
The node.
Definition: graph-dry.H:233
NodeT Node
Tipo de node.
Definition: tpl_netcapgraph.H:96
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition: graph-dry.H:447
Node * insert_node(const Node_Type &node_info)
Definition: tpl_net.H:481
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition: graph-dry.H:453
Aux_Net * get_aux_net()
Definition: tpl_netcapgraph.H:149
Arc::Arc_Type Arc_Type
Tipo de atributo que almacena un arco.
Definition: tpl_netcapgraph.H:102

Leandro Rabindranath León