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_kgraph.H
1 # ifndef TPL_KGRAPH_H
2 # define TPL_KGRAPH_H
3 
4 # include <tpl_netgraph.H>
5 
6 namespace Aleph {
7 
27 template <class GT,
28  template <class> class Max_Flow = Random_Preflow_Maximum_Flow,
29  class SA = Dft_Show_Arc<GT> >
30 long edge_connectivity(GT & g)
31 {
33  Net net;
34  typename Net::Node * source = NULL;
35  long min_degree = numeric_limits<long>::max();
36  for (Node_Iterator<GT> it(g); it.has_current(); it.next())
37  {
38  typename GT::Node * p = it.get_current();
39  NODE_COOKIE(p) = net.insert_node();
40  if (g.get_num_arcs(p) < min_degree)
41  {
42  source = mapped_node<GT, Net> (p);
43  min_degree = g.get_num_arcs(p);
44  }
45  }
46 
47  if (min_degree <= 1)
48  return min_degree;
49 
50  for (Arc_Iterator<GT, SA> it(g); it.has_current(); it.next())
51  {
52  typename GT::Arc * a = it.get_current();
53  typename Net::Node * src = mapped_node<GT, Net> (g.get_src_node(a));
54  typename Net::Node * tgt = mapped_node<GT, Net> (g.get_tgt_node(a));
55  if (src != source)
56  net.insert_arc(tgt, src, 1);
57 
58  if (tgt != source)
59  net.insert_arc(src, tgt, 1);
60  }
61 
62  I(source->in_degree == 0);
63 
64  long min_k = min_degree;
65  for (Node_Iterator<Net> it(net); it.has_current(); it.next())
66  {
67  typename Net::Node * sink = it.get_current();
68  if (sink == source)
69  continue;
70 
71  DynDlist<typename Net::Arc*> from_sink_arcs;
72 
73  for (Node_Arc_Iterator<Net> it(sink); it.has_current(); it.next())
74  from_sink_arcs.append(it.get_current());
75 
77  it(from_sink_arcs); it.has_current(); it.next())
78  net.disconnect_arc(it.get_current());
79 
80  const typename Net::Flow_Type flow = Max_Flow <Net> () (net);
81  if (flow < min_k)
82  min_k = flow;
83 
84  while (not from_sink_arcs.is_empty())
85  net.connect_arc(from_sink_arcs.get());
86 
87  net.reset(); // colocar flujo en cero
88  }
89 
90  return min_k;
91 }
92 
109  template <class GT,
110  template <class> class Max_Flow = Heap_Preflow_Maximum_Flow>
112 {
113 public:
114 
125  long operator () (GT & g) { return edge_connectivity <GT, Max_Flow> (g); }
126 };
127 
128 
157  template <class GT,
158  template <class> class Max_Flow = Heap_Preflow_Maximum_Flow,
159  class SA = Dft_Show_Arc<GT> >
160 long compute_min_cut(GT & g,
164 {
166  Net net;
167  typename Net::Node * source = NULL;
168  long min_degree = numeric_limits<long>::max();
169  for (Node_Iterator<GT> it(g); it.has_current(); it.next())
170  {
171  typename GT::Node * p = it.get_current();
172  typename Net::Node * q = net.insert_node();
173  GT::map_nodes (p, q);
174 
175  if (g.get_num_arcs(p) < min_degree)
176  {
177  source = mapped_node<GT, Net> (p);
178  min_degree = g.get_num_arcs(p);
179  }
180  }
181 
182  if (min_degree <= 1)
183  return min_degree;
184 
185  for (Arc_Iterator<GT, SA> it(g); it.has_current(); it.next())
186  {
187  typename GT::Arc * a = it.get_current();
188  typename Net::Node * src = mapped_node<GT, Net>(g.get_src_node(a));
189  typename Net::Node * tgt = mapped_node<GT, Net>(g.get_tgt_node(a));
190  if (src != source)
191  {
192  typename Net::Arc * arc = net.insert_arc(tgt, src, 1);
193  ARC_COOKIE(arc) = a;
194  }
195 
196  if (tgt != source)
197  {
198  typename Net::Arc * arc = net.insert_arc(src, tgt, 1);
199  ARC_COOKIE(arc) = a;
200  }
201  }
202 
203  Aleph::set<typename Net::Node*> tmp_vs, tmp_vt;
204  DynDlist<typename Net::Arc*> tmp_cuts, tmp_cutt;
205  long min_k = numeric_limits<long>::max();
206  for (Node_Iterator<Net> it(net); it.has_current(); it.next())
207  {
208  typename Net::Node * sink = it.get_current();
209  if (sink == source)
210  continue;
211 
212  DynDlist<typename Net::Arc*> from_sink_arcs;
213 
214  for (Node_Arc_Iterator<Net> it(sink); it.has_current(); it.next())
215  from_sink_arcs.append(it.get_current());
216 
218  it(from_sink_arcs); it.has_current(); it.next())
219  net.disconnect_arc(it.get_current());
220 
223  const typename Net::Flow_Type flow =
224  Min_Cut <Net, Max_Flow> () (net, vs, vt, cuts, cutt);
225 
226  if (flow < min_k)
227  {
228  min_k = flow;
229  tmp_vs.swap(vs);
230  tmp_vt.swap(vt);
231  tmp_cuts.swap(cuts);
232  tmp_cutt.swap(cutt);
233  }
234 
235  net.reset(); // colocar flujo en cero
236 
237  while (not from_sink_arcs.is_empty())
238  net.connect_arc(from_sink_arcs.get());
239  }
240 
242  it = tmp_vs.begin(); it != tmp_vs.end(); it++)
243  l.insert(mapped_node <Net, GT> (*it));
244 
246  it = tmp_vt.begin(); it != tmp_vt.end(); it++)
247  r.insert(mapped_node <Net, GT> (*it));
248 
249  for (typename DynDlist<typename Net::Arc*>::Iterator it(tmp_cuts);
250  it.has_current(); it.next())
251  {
252  typename Net::Arc* arc = it.get_current();
253  cut.append(mapped_arc<Net, GT> (arc));
254  }
255 
256  return min_k;
257 }
258 
288  template <class GT,
289  template <class> class Max_Flow = Heap_Preflow_Maximum_Flow,
290  class SA = Dft_Show_Arc<GT> >
292 {
293 public:
294  long operator () (GT & g,
298  {
299  return compute_min_cut <GT, Max_Flow, SA> (g, l, r, cut);
300  }
301 };
302 
303 
315  template <class GT,
316  template <class> class Max_Flow = Random_Preflow_Maximum_Flow,
317  class SA = Dft_Show_Arc<GT> >
319 {
321  Net net;
322  for (Node_Iterator<GT> it(g); it.has_current(); it.next())
323  {
324  typename GT::Node * p = it.get_current();
325  NODE_COOKIE(p) = net.insert_node();
326  }
327 
328  for (Arc_Iterator<GT, SA> it(g); it.has_current(); it.next())
329  {
330  typename GT::Arc * a = it.get_current();
331  typename Net::Node * src = mapped_node<GT,Net>(g.get_src_node(a));
332  typename Net::Node * tgt = mapped_node<GT,Net>(g.get_tgt_node(a));
333  net.insert_arc(tgt, src, 1);
334  net.insert_arc(src, tgt, 1);
335  }
336 
337  long min_k = g.get_num_nodes();
338  int i = 1;
339 
340  for (Node_Iterator<Net> k(net); k.has_current() and i < min_k; k.next(), i++)
341  {
342  typename Net::Node * source = k.get_current();
343 
344  DynDlist<typename Net::Arc*> to_source_list;
345  for (Node_Arc_Iterator<Net> it(source); it.has_current(); it.next())
346  {
347  typename Net::Arc * from_arc = it.get_current();
348  typename Net::Arc * to_arc =
349  search_arc <Net> (net, net.get_tgt_node(from_arc), source);
350  I(to_arc != NULL);
351  I(net.get_tgt_node(to_arc) == source);
352  to_source_list.append(to_arc);
353  }
354 
355  for (typename DynDlist<typename Net::Arc *>::Iterator it(to_source_list);
356  it.has_current(); it.next())
357  net.disconnect_arc(it.get_current());
358 
359  {
360  Node_Iterator<Net> j(k);
361  for (j.next(); j.has_current(); j.next())
362  {
363  typename Net::Node * sink = j.get_current();
364  if (search_arc <Net> (net, source, sink) != NULL)
365  continue; // existe arco ==> ignórelo y avance a siguiente
366 
367  DynDlist<typename Net::Arc*> from_sink_arcs;
368 
369  for (Node_Arc_Iterator<Net> it(sink); it.has_current(); it.next())
370  from_sink_arcs.append(it.get_current());
371 
373  it(from_sink_arcs); it.has_current(); it.next())
374  net.disconnect_arc(it.get_current());
375  {
376  Net aux_net;
377  {
379  for (Node_Iterator<Net> it(net); it.has_current(); it.next())
380  {
381  typename Net::Node * p = it.get_current();
382  if (p == source or p == sink)
383  {
384  NODE_COOKIE(p) = aux_net.insert_node();
385  continue;
386  }
387 
388  typename Net::Node * ps =
389  aux_net.insert_node(p->get_info());
390  typename Net::Node * pt =
391  aux_net.insert_node(p->get_info());
392  map.insert(p, aux_net.insert_arc(ps, pt, 1));
393  }
394 
395  for (Arc_Iterator<Net> it(net); it.has_current(); it.next())
396  {
397  typename Net::Arc * a = it.get_current();
398 
399  typename Net::Node * nsrc = net.get_src_node(a);
400  typename Net::Node * ntgt = net.get_tgt_node(a);
401  typename Net::Node * asrc = NULL;
402  typename Net::Node * atgt = NULL;
403 
404  if (nsrc == source)
405  asrc = (typename Net::Node *) NODE_COOKIE(nsrc);
406  else
407  {
408  typename Net::Arc * arc = map.find(nsrc);
409  asrc = aux_net.get_tgt_node(arc);
410  }
411 
412  if (ntgt == sink)
413  atgt = (typename Net::Node *) NODE_COOKIE(ntgt);
414  else
415  {
416  typename Net::Arc * arc = map.find(ntgt);
417  atgt = aux_net.get_src_node(arc);
418  }
419  aux_net.insert_arc(asrc, atgt, 1);
420  }
421  } // fin de bloque que crea el mapeo
422 
423  const typename Net::Flow_Type flow = Max_Flow <Net> () (aux_net);
424  if (flow < min_k)
425  min_k = flow;
426  }
427 
428  while (not from_sink_arcs.is_empty())
429  net.connect_arc(from_sink_arcs.get());
430 
431  net.reset(); // colocar flujo en cero
432  }
433 
434  while (not to_source_list.is_empty())
435  net.connect_arc(to_source_list.get());
436  }
437  }
438 
439  return min_k;
440 }
441 
442 
443 } // end namespace Aleph
444 
445 # endif // TPL_KGRAPH_H
void swap(DynDlist &l)
Definition: tpl_dynDlist.H:295
long compute_min_cut(GT &g, Aleph::set< typename GT::Node * > &l, Aleph::set< typename GT::Node * > &r, DynDlist< typename GT::Arc * > &cut)
Definition: tpl_kgraph.H:160
Definition: tpl_netgraph.H:66
#define NODE_COOKIE(p)
Definition: aleph-graph.H:248
Definition: tpl_dynDlist.H:26
Definition: tpl_graph.H:751
Definition: tpl_netgraph.H:1708
iterator begin()
Retorna un iterador posicionado al primer elemento del conjunto.
Definition: Set.H:459
void swap(set &c)
Definition: Set.H:453
#define ARC_COOKIE(p)
Definition: aleph-graph.H:281
Definition: tpl_dynDlist.H:332
long vertex_connectivity(GT &g)
Definition: tpl_kgraph.H:318
std::pair< iterator, bool > insert(const T &value)
Definition: Set.H:488
Definition: tpl_kgraph.H:291
Definition: tpl_graph.H:634
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
Definition: tpl_netgraph.H:2326
Definition: tpl_dynMapTree.H:289
Definition: tpl_kgraph.H:111
Definition: tpl_netgraph.H:205
Definition: tpl_graph.H:814
Definition: Set.H:39
long operator()(GT &g)
Definition: tpl_kgraph.H:125
T get()
Si this es una cola, entonces extrae el elemento más antiguo.
Definition: tpl_dynDlist.H:250
Definition: tpl_netgraph.H:1646
long edge_connectivity(GT &g)
Definition: tpl_kgraph.H:30
Definition: Set.H:69
iterator end()
Retorna un iterador posicionado al último elemento del conjunto.
Definition: Set.H:465
Definition: Map.H:56
Definition: tpl_graph.H:694
Key * insert(const Key &key, const Type &data)
Definition: tpl_dynMapTree.H:74
T & append(const T &item)
Definition: tpl_dynDlist.H:106
Type & find(const Key &key)
Definition: tpl_dynMapTree.H:223

Leandro Rabindranath León