Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_kgraph.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
70# ifndef TPL_KGRAPH_H
71# define TPL_KGRAPH_H
72
73# include <ah-graph-concepts.H>
74
75# include <limits>
76# include <tpl_dynSetTree.H>
77# include <tpl_net.H>
78# include <cookie_guard.H>
79
80namespace Aleph
81{
110 template <AlephGraph GT,
111 template <class> class Max_Flow = Random_Preflow_Maximum_Flow,
112 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
113 long edge_connectivity(GT & g, SA sa = SA())
114 {
116 << "edge_connectivity() does not work on digraphs";
117
118 const auto num_nodes = g.get_num_nodes();
119 if (num_nodes == 0)
120 return 0;
121
122 typename GT::Node *source_node = nullptr;
123 long min_degree = std::numeric_limits<long>::max();
124 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
125 {
126 auto p = it.get_curr();
127 long degree = 0;
128 for (Node_Arc_Iterator<GT, SA> it_arc(p, sa); it_arc.has_curr();
129 it_arc.next_ne())
130 ++degree;
131 if (degree < min_degree)
132 {
133 min_degree = degree;
134 source_node = p;
135 }
136 }
137
139 if (dfs(g, source_node) != num_nodes)
140 return 0;
141
142 if (min_degree <= 1)
143 return min_degree;
144
146 Net net;
147
148 // Cookie_Guard clears cookies when function exits (prevents dangling pointers)
149 Cookie_Guard<GT> cookie_guard(g, true, false);
150
151 typename Net::Node *source = nullptr;
152 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
153 {
154 auto p = it.get_curr();
155 auto q = net.insert_node();
156 NODE_COOKIE(p) = q;
157 if (p == source_node)
158 source = q;
159 }
160
161 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
162 {
163 auto a = it.get_curr();
164 auto src = mapped_node<GT, Net>(g.get_src_node(a));
165 auto tgt = mapped_node<GT, Net>(g.get_tgt_node(a));
166 net.insert_arc(tgt, src, typename Net::Flow_Type(1));
167 net.insert_arc(src, tgt, typename Net::Flow_Type(1));
168 }
169
170 long min_k = min_degree;
171 const typename Net::Flow_Type super_cap =
172 static_cast<typename Net::Flow_Type>(min_degree) + 1;
173
175 for (Node_Iterator<Net> it(net); it.has_curr(); it.next_ne())
176 {
177 auto node = it.get_curr();
178 if (node != source)
179 sinks.append(node);
180 }
181
182 for (typename DynDlist<typename Net::Node *>::Iterator it(sinks);
183 it.has_curr(); it.next_ne())
184 {
185 const auto sink = it.get_curr();
186 const auto super_source = net.insert_node();
187 const auto super_sink = net.insert_node();
188 net.insert_arc(super_source, source, super_cap);
189 net.insert_arc(sink, super_sink, super_cap);
190
191 if (const typename Net::Flow_Type flow = Max_Flow<Net>()(net); flow < min_k)
192 min_k = flow;
193
194 net.remove_node(super_source);
195 net.remove_node(super_sink);
196 net.reset(); // reset flow to zero
197 }
198
199 return min_k;
200 }
201
209 template <AlephGraph GT,
210 template <class> class Max_Flow = Heap_Preflow_Maximum_Flow>
212 {
213 public:
221 long operator ()(GT & g) { return edge_connectivity<GT, Max_Flow>(g); }
222 };
223
224
263 template <AlephGraph GT,
264 template <class> class Max_Flow = Heap_Preflow_Maximum_Flow,
265 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
270 SA sa = SA())
271 {
272 l.empty();
273 r.empty();
274 cut.empty();
275
277 << "compute_min_cut() does not work on digraphs";
278
279 const auto num_nodes = g.get_num_nodes();
280 if (num_nodes == 0)
281 return 0;
282
283 typename GT::Node *source_node = nullptr;
284 long min_degree = std::numeric_limits<long>::max();
285 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
286 {
287 auto p = it.get_curr();
288 long degree = 0;
289 for (Node_Arc_Iterator<GT, SA> it_arc(p, sa); it_arc.has_curr();
290 it_arc.next_ne())
291 ++degree;
292 if (degree < min_degree)
293 {
294 min_degree = degree;
295 source_node = p;
296 }
297 }
298
300 if (dfs(g, source_node) != num_nodes)
301 {
302 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
303 {
304 auto p = it.get_curr();
306 l.insert(p);
307 else
308 r.insert(p);
309 }
310 return 0;
311 }
312
313 if (min_degree <= 1)
314 {
315 if (source_node != nullptr)
316 l.insert(source_node);
317
318 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
319 {
320 auto p = it.get_curr();
321 if (p != source_node)
322 r.insert(p);
323 }
324
325 for (Node_Arc_Iterator<GT, SA> it(source_node, sa);
326 it.has_curr(); it.next_ne())
327 {
328 auto arc = it.get_curr();
329 auto other = g.get_connected_node(arc, source_node);
330 if (other != source_node)
331 cut.append(arc);
332 }
333
334 return min_degree;
335 }
336
338 Net net;
341 typename Net::Node *source = nullptr;
342 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
343 {
344 auto p = it.get_curr();
345 auto q = net.insert_node();
346 NODE_COOKIE(p) = nullptr;
347 GT::map_nodes(p, q);
348 net_node_map.insert(q, p);
349 if (p == source_node)
350 source = q;
351 }
352
353 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
354 {
355 auto a = it.get_curr();
356 auto src = mapped_node<GT, Net>(g.get_src_node(a));
357 auto tgt = mapped_node<GT, Net>(g.get_tgt_node(a));
358 auto arc = net.insert_arc(tgt, src, static_cast<typename Net::Flow_Type>(1));
359 ARC_COOKIE(arc) = a;
360 net_arc_map.insert(arc, a);
361
362 arc = net.insert_arc(src, tgt, static_cast<typename Net::Flow_Type>(1));
363 ARC_COOKIE(arc) = a;
364 net_arc_map.insert(arc, a);
365 }
366
368 DynList<typename Net::Arc *> tmp_cuts, tmp_cutt;
369 long min_k = std::numeric_limits<long>::max();
370 const typename Net::Flow_Type super_cap =
371 static_cast<typename Net::Flow_Type>(min_degree) + 1;
372
374 for (Node_Iterator<Net> it(net); it.has_curr(); it.next_ne())
375 if (auto node = it.get_curr(); node != source)
376 sinks.append(node);
377
378 for (typename DynDlist<typename Net::Node *>::Iterator it(sinks);
379 it.has_curr(); it.next_ne())
380 {
381 auto sink = it.get_curr();
382 auto super_source = net.insert_node();
383 auto super_sink = net.insert_node();
384 net.insert_arc(super_source, source, super_cap);
385 net.insert_arc(sink, super_sink, super_cap);
386
389 const auto flow = Min_Cut<Net, Max_Flow>()(net, vs, vt, cuts, cutt);
390
391 if (flow < min_k)
392 {
393 DynSetTree<typename Net::Node *> vs_filtered, vt_filtered;
394 DynList<typename Net::Arc *> cuts_filtered, cutt_filtered;
395
397 it.has_curr(); it.next_ne())
398 if (auto node = it.get_curr(); net_node_map.contains(node))
399 vs_filtered.insert(node);
400
402 it.has_curr(); it.next_ne())
403 if (auto node = it.get_curr(); net_node_map.contains(node))
404 vt_filtered.insert(node);
405
406 for (typename DynList<typename Net::Arc *>::Iterator it(cuts);
407 it.has_curr(); it.next_ne())
408 if (auto arc = it.get_curr(); net_arc_map.contains(arc))
409 cuts_filtered.append(arc);
410
411 for (typename DynList<typename Net::Arc *>::Iterator it(cutt);
412 it.has_curr(); it.next_ne())
413 if (auto arc = it.get_curr(); net_arc_map.contains(arc))
414 cutt_filtered.append(arc);
415
416 min_k = flow;
417 tmp_vs.swap(vs_filtered);
418 tmp_vt.swap(vt_filtered);
419 tmp_cuts.swap(cuts_filtered);
420 tmp_cutt.swap(cutt_filtered);
421 }
422
423 net.remove_node(super_source);
424 net.remove_node(super_sink);
425 net.reset(); // reset flow to zero
426 }
427
428 for (typename DynSetTree<typename Net::Node *>::Iterator it(tmp_vs);
429 it.has_curr(); it.next_ne())
430 l.insert(net_node_map.find(it.get_curr()));
431
432 for (typename DynSetTree<typename Net::Node *>::Iterator it(tmp_vt);
433 it.has_curr(); it.next_ne())
434 r.insert(net_node_map.find(it.get_curr()));
435
436 for (typename DynList<typename Net::Arc *>::Iterator it(tmp_cuts);
437 it.has_curr(); it.next_ne())
438 {
439 typename Net::Arc *arc = it.get_curr();
440 cut.append(net_arc_map.find(arc));
441 }
442
443 return min_k;
444 }
445
454 template <AlephGraph GT,
455 template <class> class Max_Flow = Heap_Preflow_Maximum_Flow,
456 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
458 {
459 SA sa;
460
461 public:
463 explicit Compute_Min_Cut(SA __sa = SA()) : sa(std::move(__sa)) {}
464
466 long operator ()(GT & g,
470 {
471 return compute_min_cut<GT, Max_Flow, SA>(g, l, r, cut, sa);
472 }
473 };
474
475
503 template <AlephGraph GT,
504 template <class> class Max_Flow = Random_Preflow_Maximum_Flow,
505 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
506 long vertex_connectivity(GT & g, SA sa = SA())
507 {
509 << "vertex_connectivity() does not work on digraphs";
510
511 const auto num_nodes = g.get_num_nodes();
512 if (num_nodes <= 1)
513 return 0;
514
515 typename GT::Node *source_node = nullptr;
516 long min_degree = std::numeric_limits<long>::max();
517 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
518 {
519 auto p = it.get_curr();
520 long degree = 0;
521 for (Node_Arc_Iterator<GT, SA> it_arc(p, sa); it_arc.has_curr();
522 it_arc.next_ne())
523 ++degree;
524 if (degree < min_degree)
525 {
526 min_degree = degree;
527 source_node = p;
528 }
529 }
530
532 if (dfs(g, source_node) != num_nodes)
533 return 0;
534
535 if (min_degree <= 1)
536 return min_degree;
537
539 Net net;
540
541 // Cookie_Guard clears cookies when function exits (prevents dangling pointers)
542 Cookie_Guard<GT> cookie_guard(g, true, false);
543
544 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
545 {
546 auto p = it.get_curr();
547 NODE_COOKIE(p) = net.insert_node();
548 }
549
550 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
551 {
552 auto a = it.get_curr();
553 auto src = mapped_node<GT, Net>(g.get_src_node(a));
554 auto tgt = mapped_node<GT, Net>(g.get_tgt_node(a));
555 net.insert_arc(tgt, src, static_cast<typename Net::Flow_Type>(1));
556 net.insert_arc(src, tgt, static_cast<typename Net::Flow_Type>(1));
557 }
558
559 long min_k = min_degree;
560
561 for (Node_Iterator<Net> k(net); k.has_curr() and min_k > 1; k.next_ne())
562 {
563 auto source = k.get_curr();
564 DynDlist<typename Net::Arc *> to_source_list;
565 for (_In_Iterator<Net> it(source); it.has_curr(); it.next_ne())
566 to_source_list.append(it.get_curr());
567
568 for (typename DynDlist<typename Net::Arc *>::Iterator it(to_source_list);
569 it.has_curr(); it.next_ne())
570 net.disconnect_arc(it.get_curr());
571
572 {
574 for (j.next(); j.has_curr(); j.next_ne())
575 {
576 auto sink = j.get_curr();
577 if (search_arc<Net>(net, source, sink) != nullptr)
578 continue; // adjacent nodes: skip to next sink
579
580 DynDlist<typename Net::Arc *> from_sink_arcs;
581
582 for (_Out_Iterator<Net> it(sink); it.has_curr(); it.next_ne())
583 from_sink_arcs.append(it.get_curr());
584
586 it(from_sink_arcs); it.has_curr(); it.next_ne())
587 net.disconnect_arc(it.get_curr());
588
589 {
590 // Build node-splitting network to model vertex capacities.
591 Net aux_net;
592 {
594 for (Node_Iterator<Net> it(net); it.has_curr(); it.next_ne())
595 {
596 auto p = it.get_curr();
597 if (p == source or p == sink)
598 {
599 NODE_COOKIE(p) = aux_net.insert_node();
600 continue;
601 }
602
603 auto ps = aux_net.insert_node(p->get_info());
604 auto pt = aux_net.insert_node(p->get_info());
605 map.insert(p, aux_net.insert_arc(ps, pt, typename Net::Flow_Type(1)));
606 }
607
608 for (Arc_Iterator<Net> it(net); it.has_curr(); it.next_ne())
609 {
610 auto a = it.get_curr();
611 auto nsrc = net.get_src_node(a);
612 auto ntgt = net.get_tgt_node(a);
613 typename Net::Node *asrc = nullptr;
614 typename Net::Node *atgt = nullptr;
615
616 if (nsrc == source)
617 asrc = static_cast<typename Net::Node *>(NODE_COOKIE(nsrc));
618 else
619 {
620 auto arc = map.find(nsrc);
621 asrc = aux_net.get_tgt_node(arc);
622 }
623
624 if (ntgt == sink)
625 atgt = static_cast<typename Net::Node *>(NODE_COOKIE(ntgt));
626 else
627 {
628 auto arc = map.find(ntgt);
629 atgt = aux_net.get_src_node(arc);
630 }
631 aux_net.insert_arc(asrc, atgt, typename Net::Flow_Type(1));
632 }
633 } // end mapping block
634
635 const auto flow = Max_Flow<Net>()(aux_net);
636 if (flow < min_k)
637 min_k = flow;
638 }
639
640 while (not from_sink_arcs.is_empty())
641 net.connect_arc(from_sink_arcs.get());
642
643 net.reset(); // reset flow to zero
644 }
645
646 while (not to_source_list.is_empty())
647 net.connect_arc(to_source_list.get());
648 }
649 }
650
651 return min_k;
652 }
653} // end namespace Aleph
654
655# endif // TPL_KGRAPH_H
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
int num_nodes
Definition btreepic.C:410
Functor wrapper for compute_min_cut().
Definition tpl_kgraph.H:458
Compute_Min_Cut(SA __sa=SA())
Keeps sa, the filter of the arcs.
Definition tpl_kgraph.H:463
long operator()(GT &g, DynSetTree< typename GT::Node * > &l, DynSetTree< typename GT::Node * > &r, DynDlist< typename GT::Arc * > &cut)
Compute a minimum edge cut for g.
Definition tpl_kgraph.H:466
RAII guard that clears graph cookies on destruction.
Stateful depth-first traversal functor.
bool has_curr() const noexcept
Return true the iterator has an current arc.
Definition tpl_graph.H:1822
Iterator dynamic list.
Dynamic doubly linked list with O(1) size and bidirectional access.
T & append(const T &item)
Append a copied item at the end of the list.
void empty() noexcept
@brief Empties the container.
T get()
See remove_first().
Iterator on the items of list.
Definition htlist.H:1614
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
T & insert(const T &item)
Definition htlist.H:1308
void empty() noexcept
empty the list
Definition htlist.H:1576
DynList & swap(DynList &l) noexcept
Definition htlist.H:1253
Dynamic map implemented with a treap.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
bool contains(const Key &key) const noexcept(nothrow_search)
Same as has().
Data & find(const Key &key)
Find the value associated with key.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
void swap(DynSetTree &dset) noexcept(noexcept(tree.swap(dset.tree)) and noexcept(std::swap(num_nodes, dset.num_nodes)) and noexcept(std::swap(arena_allocator, dset.arena_allocator)))
Exchange all elements of this set with dset in constant time (and extremely fast).
Functor wrapper for edge_connectivity().
Definition tpl_kgraph.H:212
long operator()(GT &g)
Compute edge connectivity of g.
Definition tpl_kgraph.H:221
void next()
Advances the iterator to the next filtered element.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
bool has_curr() const noexcept
Definition htlist.H:969
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:1058
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
Definition graph-dry.H:1281
RAII guards for graph node/arc cookies.
#define ARC_COOKIE(p)
Return the arc cookie
long vertex_connectivity(GT &g, SA sa=SA())
Compute vertex connectivity of an undirected graph.
Definition tpl_kgraph.H:506
long edge_connectivity(GT &g, SA sa=SA())
Compute edge connectivity (arc connectivity) of an undirected graph.
Definition tpl_kgraph.H:113
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define NODE_COOKIE(p)
Return the node cookie
long compute_min_cut(GT &g, DynSetTree< typename GT::Node * > &l, DynSetTree< typename GT::Node * > &r, DynDlist< typename GT::Arc * > &cut, SA sa=SA())
Compute a minimum edge cut of an undirected graph.
Definition tpl_kgraph.H:266
@ Depth_First
Definition aleph-graph.H:73
Net_Graph< Net_Node< string >, Net_Arc< Empty_Class, FlowType > > Net
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Functor wrapper for heap_preflow_maximum_flow().
Definition tpl_net.H:2718
Functor wrapper for min_cut().
Definition tpl_net.H:2838
Arc of a flow network implemented with adjacency lists.
Definition tpl_net.H:160
Flow network implemented with adjacency lists.
Definition tpl_net.H:311
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
Definition tpl_net.H:750
Arc * connect_arc(Arc *arc)
Connect a previously disconnected arc.
Definition tpl_net.H:832
virtual void disconnect_arc(Arc *arc)
Disconnect arc arc from the graph without deleting it.
Definition tpl_net.H:921
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())
Insert a capacitated arc with an initial flow.
Definition tpl_net.H:799
void remove_node(Node *p) override
Remove node p and all its arcs from the network.
Definition tpl_net.H:933
ArcT Arc
Arc type.
Definition tpl_net.H:322
typename Arc::Flow_Type Flow_Type
Capacity/flow numeric type.
Definition tpl_net.H:328
NodeT Node
Node type.
Definition tpl_net.H:325
void reset()
Reset all arc flows to zero.
Definition tpl_net.H:1099
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Functor wrapper for random_preflow_maximum_flow().
Definition tpl_net.H:2770
static int * k
Gsl_Compatible_Rng r
Dynamic set implementations based on balanced binary search trees.
Network flow graph structures.
DynList< int > l