Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
random_graph.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
46# ifndef RANDOM_GRAPH_H
47# define RANDOM_GRAPH_H
48
49# include <ah-graph-concepts.H>
50
51# include <concepts>
52# include <memory>
53# include <optional>
54# include <tpl_indexArc.H>
55# include <tpl_dynSetHash.H>
56# include <tpl_sort_utils.H>
57# include <tpl_graph_utils.H>
58# include <tpl_components.H>
59# include <single_graph.H>
60# include <Tarjan.H>
61# include <ah-errors.H>
62# include <ah_init_guard.H>
63# include <ah-random.H>
64
65namespace Aleph
66{
76 template <class GT>
78 {
79 void operator ()(GT &, typename GT::Node *) const noexcept
80 {
81 // empty
82 }
83 };
84
85
95 template <class GT>
97 {
98 void operator ()(GT &, typename GT::Arc *) const noexcept
99 {
100 // empty
101 }
102 };
103
104 // TODO: consider replacing Init_Node and Init_Arc classes with lambdas
105
114 template <class F>
116 {
117 std::optional<F> own; // the initializer, if it was not a modifiable lvalue
118 F * f; // the initializer that is used
119
120 public:
122 Kept_Initializer(F & x) noexcept : f(&x) {}
123
125 Kept_Initializer(const F & x) : own(x), f(&*own) {}
126
128 Kept_Initializer(F && x) : own(std::move(x)), f(&*own) {}
129
132
134 F & get() const noexcept { return *f; }
135 };
136
137 template <AlephGraph GT, class Init_Node, class Init_Arc>
139 {
140 protected:
141 typedef typename GT::Node GT_Node;
142 typedef typename GT::Arc GT_Arc;
143
146
147 Gsl_Compatible_Rng_Owner r; // the generator, in dynamic memory
148
149 Init_Node & init_node;
151
152 std::unique_ptr<DynArray<GT_Node *>> nodes; // pointer to save directory
153 // space when not used
154
155 std::unique_ptr<IndexArc<GT>> idx_arc; // pointer because constructor
156 // requires the graph
157 mutable size_t num_nodes;
158 mutable size_t num_arcs;
159 mutable unsigned long rand_max;
160
162
163 bool save_parity; // indicates whether to track parity relations
164 // among nodes (only while eulerian() builds
165 // its graph)
166
167 // The order in which the nodes were created. The sets of nodes and the
168 // components follow it instead of the addresses of the nodes, which are
169 // another order in each run, so that the same seed gives the same graph
171
172 size_t order_of(GT_Node *p) const { return creation.find(p); }
173
176 {
177 const Random_Graph_Base *base = nullptr;
178
179 bool operator()(GT_Node *a, GT_Node *b) const
180 {
181 return base->order_of(a) < base->order_of(b);
182 }
183 };
184
186
187 // Puts the nodes of each component, and the components by their first
188 // node, in the order of creation
190 {
191 auto before = [this] (GT_Node *a, GT_Node *b)
192 {
193 return order_of(a) < order_of(b);
194 };
195 for (auto it = comps.get_it(); it.has_curr(); it.next_ne())
196 mergesort(it.get_curr(), before);
197 mergesort(comps, [this] (const DynList<GT_Node *> & a, const DynList<GT_Node *> & b)
198 {
199 return order_of(a.get_first()) < order_of(b.get_first());
200 });
201 }
202
203 virtual void
205
207 {
208 auto a = idx_arc->insert(g.insert_arc(src, tgt));
209 init_arc(g, a);
211 return a;
212 }
213
215 GT_Node * select_random_node(GT_Node *excluded = nullptr) noexcept
216 {
217 assert(nodes.get() != nullptr);
218 assert(num_nodes > 0);
219 assert(excluded == nullptr or num_nodes > 1); // avoid infinite loop
220
221 GT_Node *ret_val = nullptr;
222 while (true)
223 {
224 unsigned long idx = r->uniform_int(num_nodes);
225 ret_val = nodes->access(idx);
226 if (excluded == nullptr or ret_val != excluded)
227 break;
228 }
229
230 return ret_val;
231 }
232
235 {
236 const unsigned long k = r->uniform_int(list.size());
237 typename DynList<GT_Node *>::Iterator it(list);
238 for (unsigned long i = 0; i < k; ++i, it.next_ne()) {}
239
240 return it.get_curr_ne();
241 }
242
244
245 virtual void connect() = 0;
246
247 // What a generator resets before each graph: the graph that it gives
248 // is g moved out, which leaves the rest of its state behind
249 virtual void prepare_new_graph()
250 {
251 // empty
252 }
253
254 void initialize_and_create_nodes(const size_t & __num_nodes,
255 const size_t & __num_arcs)
256 {
257 ah_domain_error_if(__num_nodes == 0)
258 << "Number of nodes must be greater than 0";
259
260 clear_graph(g); // what a former call left, if it threw
261 creation.empty();
263
264 num_nodes = __num_nodes;
265
266 const size_t num_nodes_2 = num_nodes * num_nodes;
267 if (g.is_digraph())
268 num_arcs = std::min(__num_arcs, num_nodes_2 - num_nodes);
269 else
270 num_arcs = std::min(__num_arcs, (num_nodes_2 - num_nodes) / 2);
271
273 }
274
275 template <class N, class A>
276 Random_Graph_Base(const unsigned long seed, N && __init_node, A && __init_arc)
277 : kept_node(std::forward<N>(__init_node)),
278 kept_arc(std::forward<A>(__init_arc)),
280 init_node(kept_node.get()),
281 init_arc(kept_arc.get()),
282 num_nodes(0), num_arcs(0),
284 {
285 // empty
286 }
287
288 virtual ~Random_Graph_Base() = default;
289
291 GT create(const size_t & __num_nodes, const size_t & __num_arcs,
292 const bool connected)
293 {
294 initialize_and_create_nodes(__num_nodes, __num_arcs);
295
296 // randomly insert arcs by selecting random node pairs
297 for (size_t i = 0; i < num_arcs; ++i)
298 {
299 auto src = select_random_node();
300 auto tgt = select_random_node(src);
301 if (idx_arc->search(src, tgt) == nullptr) // arc already exists?
302 insert_arc(src, tgt);
303 }
304
305 if (connected)
306 connect();
307
308 return std::move(g);
309 }
310
311 virtual GT create_p(const size_t & __num_nodes, const double & p,
312 bool connected) = 0;
313
314 virtual void make_eulerian() = 0;
315
316 virtual void make_hamiltonian() = 0;
317
318 public:
335 GT eulerian(const size_t & __num_nodes, const size_t & __num_arcs)
336 {
337 save_parity = true;
338 Init_Guard parity_off([this] { save_parity = false; });
339 g = this->create(__num_nodes, __num_arcs, true);
341
342 return std::move(g);
343 }
344
364 GT eulerian(const size_t & __num_nodes, const double & p)
365 {
366 save_parity = true;
367 Init_Guard parity_off([this] { save_parity = false; });
368 g = this->create_p(__num_nodes, p, true);
370
371 return std::move(g);
372 }
373
401 GT sufficient_hamiltonian(const size_t & __num_nodes,
402 const double & p = 0.5)
403 {
404 g = this->create_p(__num_nodes, p, true);
406
407 return std::move(g);
408 }
409 };
410
411
456 template <AlephGraph GT,
457 class Init_Node = Dft_Init_Rand_Node<GT>,
459 class Random_Graph : public Random_Graph_Base<GT, Init_Node, Init_Arc>
460 {
461 typedef typename GT::Node GT_Node;
462 typedef typename GT::Arc GT_Arc;
463
465
466 typename Base::Node_Set odd_nodes{typename Base::By_Creation{this}}; // odd degree
467 typename Base::Node_Set even_nodes{typename Base::By_Creation{this}}; // even degree
468
469 // The arcs that the random joins of make_eulerian() inserted
471
473 {
474 auto a = this->insert_arc(src, tgt);
475 joined.append(a);
476 return a;
477 }
478
480 {
481 if (not this->save_parity)
482 return;
483
484 if (is_even(this->g.get_num_arcs(src)))
485 { // was odd before the insertion
486 this->odd_nodes.remove(src);
487 this->even_nodes.insert(src);
488 }
489 else
490 {
491 this->even_nodes.remove(src);
492 this->odd_nodes.insert(src);
493 }
494
495 if (is_even(this->g.get_num_arcs(tgt)))
496 { // was odd before the insertion
497 this->odd_nodes.remove(tgt);
498 this->even_nodes.insert(tgt);
499 }
500 else
501 {
502 this->even_nodes.remove(tgt);
503 this->odd_nodes.insert(tgt);
504 }
505 }
506
507 // The parities of the nodes of a former graph
508 void prepare_new_graph() override
509 {
512 }
513
515 {
516 this->nodes = std::unique_ptr<DynArray<GT_Node *>>
517 (new DynArray<GT_Node *>(this->num_nodes));
518
519 this->nodes->reserve(this->num_nodes);
520
521 for (size_t i = 0; i < this->num_nodes; ++i)
522 {
523 auto p = this->g.insert_node(new GT_Node);
524 this->nodes->access(i) = p;
525 this->creation.insert(p, i);
526 this->init_node(this->g, p);
527 if (this->save_parity)
528 {
529 this->even_nodes.insert(p);
530 NODE_COUNTER(p) = 0;
531 }
532 }
533
534 this->idx_arc = std::unique_ptr<IndexArc<GT>>(new IndexArc<GT>(this->g));
535 }
536
537 void connect() override
538 {
539 DynList<DynList<GT_Node *>> subgraphs; // list of subgraphs
540
541 Unconnected_Components<GT>()(this->g, subgraphs);
542 this->sort_components(subgraphs);
543
544 const size_t & num_subs = subgraphs.size();
545
546 if (num_subs == 1)
547 return;
548
549 DynArray<GT_Node *> block_nodes;
550
551 for (typename DynList<DynList<GT_Node *>>::Iterator it(subgraphs);
552 it.has_curr(); it.next_ne())
553 block_nodes.append(this->select_random_node(it.get_curr_ne()));
554
555 for (size_t i = 1; i < num_subs; ++i)
556 {
557 auto src = block_nodes.access(i - 1);
558 auto tgt = block_nodes.access(i);
559 this->insert_arc(src, tgt);
560 }
561 }
562
563 // Create a random graph where the probability of an arc between
564 // any pair of nodes is p
565 GT create_p(const size_t & __num_nodes, const double & p,
566 const bool connected) override
567 {
568 ah_domain_error_if(p > 1.0 or p <= 0.0) << "Invalid value for p";
569
570 this->initialize_and_create_nodes(__num_nodes, __num_nodes);
571
572 for (size_t i = 0; i + 1 < this->num_nodes; ++i)
573 {
574 auto src = this->nodes->access(i);
575 for (size_t j = i + 1; j < this->num_nodes; ++j)
576 if (this->r->uniform() <= p) // random draw
577 {
578 auto tgt = this->nodes->access(j);
579 assert(src != tgt);
580 this->insert_arc(src, tgt);
581 }
582 }
583
584 if (connected)
585 connect();
586
587 return std::move(this->g);
588 }
589
590 public:
597 template <class N = Init_Node, class A = Init_Arc>
598 requires (std::constructible_from<Kept_Initializer<Init_Node>, N> and
599 std::constructible_from<Kept_Initializer<Init_Arc>, A>)
600 Random_Graph(unsigned long seed = time(nullptr),
601 N && __init_node = Init_Node(),
602 A && __init_arc = Init_Arc())
603 : Random_Graph_Base<GT, Init_Node, Init_Arc>
604 (seed, std::forward<N>(__init_node), std::forward<A>(__init_arc))
605 {
607 << "Building of random digraph through a graph";
608 }
609
610
627 GT operator ()(const size_t & __num_nodes, const size_t & __num_arcs,
628 bool connected = true)
629 {
630 return this->create(__num_nodes, __num_arcs, connected);
631 }
632
656 GT operator ()(const size_t & __num_nodes, const double & p,
657 bool connected = true)
658 {
659 return create_p(__num_nodes, p, connected);
660 }
661
662 private:
663 // Join the odd nodes src and tgt, which have an arc, through a node
664 // adjacent to neither, whose degree keeps its parity with an arc to
665 // each: a random even node, as many times as there are even nodes, and
666 // then any node. The search had no end when there was no such node.
668 {
669 auto fits = [&] (GT_Node *p)
670 {
671 return p != src and p != tgt and
672 this->idx_arc->search(src, p) == nullptr and
673 this->idx_arc->search(tgt, p) == nullptr;
674 };
675
676 GT_Node *p = nullptr;
677 for (size_t tries = this->even_nodes.size(); tries > 0 and p == nullptr;
678 --tries)
679 {
680 auto q = this->even_nodes.select
681 (this->r->uniform_int(this->even_nodes.size()));
682 if (fits(q))
683 p = q;
684 }
685
686 for (size_t i = 0; p == nullptr and i < this->nodes->size(); ++i)
687 if (fits(this->nodes->access(i)))
688 p = this->nodes->access(i);
689
690 if (p == nullptr)
691 return false;
692
693 join_arc(src, p);
694 join_arc(p, tgt);
695
696 return true;
697 }
698
699 // Random pairs of odd nodes until one without an arc, which the arc
700 // joins, as many as there are odd nodes; two odd nodes with an arc
701 // are joined through a node. The pairs had no end when all the odd
702 // nodes were adjacent.
704 {
705 for (size_t tries = this->odd_nodes.size(); tries > 0; --tries)
706 {
707 GT_Node *src = this->odd_nodes.select
708 (this->r->uniform_int(this->odd_nodes.size()));
709 GT_Node *tgt = nullptr;
710 do
711 tgt = this->odd_nodes.select
712 (this->r->uniform_int(this->odd_nodes.size()));
713 while (tgt == src);
714
715 if (this->idx_arc->search(src, tgt) == nullptr)
716 {
717 join_arc(src, tgt);
718 return true;
719 }
720
721 if (this->odd_nodes.size() == 2)
722 return join_through_a_node(src, tgt);
723 }
724
725 return false;
726 }
727
728 // Any pair of odd nodes without an arc, as the random pairs would find
729 // in the end; or else any pair that a node joins
731 {
732 DynArray<GT_Node *> odd; // the set changes when the arcs go in
733 for (typename Base::Node_Set::Iterator it(this->odd_nodes);
734 it.has_curr(); it.next_ne())
735 odd.append(it.get_curr_ne());
736
737 for (size_t i = 0; i < odd.size(); ++i)
738 for (size_t j = i + 1; j < odd.size(); ++j)
739 if (this->idx_arc->search(odd.access(i), odd.access(j)) == nullptr)
740 {
741 join_arc(odd.access(i), odd.access(j));
742 return true;
743 }
744
745 for (size_t i = 0; i < odd.size(); ++i)
746 for (size_t j = i + 1; j < odd.size(); ++j)
747 if (join_through_a_node(odd.access(i), odd.access(j)))
748 return true;
749
750 return false;
751 }
752
753 // The exact construction, from the graph that create() gave: the random
754 // joins could leave a graph that no arcs make Eulerian, so their arcs
755 // go. Then, in each component of the complement (the arcs the graph
756 // lacks), a breadth-first spanning tree; from its leaves up, a node of
757 // odd degree takes the arc to its parent, which changes the parity of
758 // the parent. A root left odd means that its component has an odd
759 // number of odd nodes, and then no arcs make the graph Eulerian.
761 {
762 for (typename DynList<GT_Arc *>::Iterator it(joined); it.has_curr();
763 it.next_ne())
764 this->idx_arc->remove_from_graph(it.get_curr_ne());
765 joined.empty();
766
767 const size_t n = this->nodes->size();
768 this->odd_nodes.empty();
769 this->even_nodes.empty();
770 for (size_t i = 0; i < n; ++i)
771 {
772 auto p = this->nodes->access(i);
773 if (this->g.get_num_arcs(p) % 2 == 0)
774 this->even_nodes.insert(p);
775 else
776 this->odd_nodes.insert(p);
777 }
778
779 DynArray<long> parent;
780 parent.reserve(n);
781 DynArray<size_t> order; // breadth-first order, filled by append()
782 DynArray<size_t> unvisited, rest;
783 unvisited.reserve(n);
784 for (size_t i = 0; i < n; ++i)
785 unvisited.access(i) = i;
786
787 while (unvisited.size() > 0)
788 { // a node not yet visited is the root of a new tree
789 size_t head = order.size();
790 const size_t root = unvisited.access(unvisited.size() - 1);
791 unvisited.cut(unvisited.size() - 1);
792 parent.access(root) = -1;
793 order.append(root);
794 while (head < order.size())
795 { // each check either visits a node or meets an arc of the graph
796 const size_t x = order.access(head++);
797 auto px = this->nodes->access(x);
798 rest.cut();
799 for (size_t k = 0; k < unvisited.size(); ++k)
800 {
801 const size_t y = unvisited.access(k);
802 if (this->idx_arc->search(px, this->nodes->access(y)) == nullptr)
803 {
804 parent.access(y) = static_cast<long>(x);
805 order.append(y);
806 }
807 else
808 rest.append(y);
809 }
810 std::swap(unvisited, rest);
811 }
812 }
813
814 DynArray<char> odd;
815 odd.reserve(n);
816 for (size_t i = 0; i < n; ++i)
817 odd.access(i) = this->g.get_num_arcs(this->nodes->access(i)) % 2;
818
819 DynList<size_t> tree_arcs; // the nodes whose arc to the parent goes in
820 for (size_t k = order.size(); k-- > 0; )
821 {
822 const size_t x = order.access(k);
823 if (not odd.access(x))
824 continue;
825 ah_domain_error_if(parent.access(x) < 0)
826 << "The graph cannot be made Eulerian by adding arcs to it";
827 tree_arcs.append(x);
828 odd.access(parent.access(x)) ^= 1;
829 }
830
831 for (typename DynList<size_t>::Iterator it(tree_arcs); it.has_curr();
832 it.next_ne())
833 {
834 const size_t x = it.get_curr_ne();
835 this->insert_arc(this->nodes->access(x),
836 this->nodes->access(parent.access(x)));
837 }
838 }
839
840 void make_eulerian() override
841 {
842 joined.empty();
843 while (this->odd_nodes.size() > 1)
845 {
847 break;
848 }
849 joined.empty();
850
851 assert(all_even());
852 }
853
854 bool all_even() const
855 {
856 for (typename GT::Node_Iterator it(this->g); it.has_curr(); it.next_ne())
857 if (this->g.get_num_arcs(it.get_curr_ne()) % 2 != 0)
858 return false;
859 return true;
860 }
861
863 {
864 if (this->idx_arc->search(src, tgt) == nullptr)
865 this->insert_arc(src, tgt);
866
867 const size_t & n = this->g.get_num_nodes();
868
869 while (this->g.get_num_arcs(src) + this->g.get_num_arcs(tgt) < n)
870 {
871 auto p = this->nodes->access(this->r->uniform_int(n));
872 if (p == src or p == tgt)
873 continue;
874
875 if (this->idx_arc->search(src, p) == nullptr)
876 this->insert_arc(src, p);
877
878 if (this->g.get_num_arcs(src) + this->g.get_num_arcs(tgt) == n)
879 break;
880
881 if (this->idx_arc->search(tgt, p) == nullptr)
882 this->insert_arc(tgt, p);
883 }
884 }
885
886 void make_hamiltonian() override
887 {
888 const size_t & n = this->g.get_num_nodes();
889 for (size_t i = 0; i + 1 < n; ++i)
890 {
891 auto src = this->nodes->access(i);
892 for (size_t j = i + 1; j < n; ++j)
893 balance_graph_nodes_degree(src, this->nodes->access(j));
894 }
895 }
896
897 public:
915 GT eulerian(const size_t & __num_nodes, const size_t & __num_arcs)
916 {
917 this->save_parity = true;
918 Init_Guard parity_off([this] { this->save_parity = false; });
919 this->g = this->create(__num_nodes, __num_arcs, true);
921
922 return std::move(this->g);
923 }
924
945 GT eulerian(const size_t & __num_nodes, const double & p)
946 {
947 this->save_parity = true;
948 Init_Guard parity_off([this] { this->save_parity = false; });
949 this->g = this->create_p(__num_nodes, p, true);
951
952 return std::move(this->g);
953 }
954
982 GT sufficient_hamiltonian(const size_t & __num_nodes,
983 const double & p = 0.5)
984 {
985 this->g = this->create_p(__num_nodes, p, true);
987
988 return std::move(this->g);
989 }
990 };
991
992
1013 template <AlephGraph GT,
1014 class Init_Node = Dft_Init_Rand_Node<GT>,
1016 class Random_Digraph : public Random_Graph_Base<GT, Init_Node, Init_Arc>
1017 {
1018 typedef typename GT::Node GT_Node;
1019 typedef typename GT::Arc GT_Arc;
1020
1022
1023 // nodes with out-degree > in-degree, < in-degree, and == in-degree
1024 typename Base::Node_Set greater{typename Base::By_Creation{this}};
1025 typename Base::Node_Set smaller{typename Base::By_Creation{this}};
1026 typename Base::Node_Set equal{typename Base::By_Creation{this}};
1027
1029 {
1030 const size_t & n = this->nodes->size();
1031
1032 if (n != this->g.get_num_nodes())
1033 std::cout << "Warning num of nodes of graph does not match with array "
1034 << this->g.get_num_nodes() << "!=" << n << std::endl;
1035
1036 size_t total = greater.size() + smaller.size() + equal.size();
1037 if (total != this->g.get_num_nodes())
1038 std::cout << "Inconsistency with nodes parity" << std::endl
1039 << "greater = " << greater.size() << std::endl
1040 << "smaller = " << smaller.size() << std::endl
1041 << "equal = " << equal.size() << std::endl
1042 << "total = " << total << std::endl
1043 << "|V| = " << this->g.get_num_nodes();
1044
1045 for (size_t i = 0; i < n; ++i)
1046 {
1047 auto p = this->nodes->access(i);
1048
1049 const long & in_sz = NODE_COUNTER(p);
1050 const size_t & out_sz = this->g.get_num_arcs(p);
1051
1052 if (in_sz == out_sz)
1053 {
1054 if (smaller.search(p) != nullptr)
1055 std::cout << "Inconsistency " << in_sz << "/" << out_sz << " found "
1056 << " in smaller table" << std::endl;
1057
1058 if (greater.search(p) != nullptr)
1059 std::cout << "Inconsistency " << in_sz << "/" << out_sz << " found "
1060 << " in greater table" << std::endl;
1061
1062 if (equal.search(p) == nullptr)
1063 {
1064 std::cout << "node of same in/out degree is not in equal table"
1065 << std::endl;
1066
1067 return false;
1068 }
1069 }
1070 else if (in_sz > out_sz)
1071 {
1072 if (greater.search(p) != nullptr)
1073 std::cout << "Inconsistency " << in_sz << "/" << out_sz << " found "
1074 << " in greater table" << std::endl;
1075
1076 if (equal.search(p) != nullptr)
1077 std::cout << "Inconsistency " << in_sz << "/" << out_sz << " found "
1078 << std::endl;
1079
1080 if (smaller.search(p) == nullptr)
1081 {
1082 std::cout << "node with " << in_sz << "/" << out_sz << " not found "
1083 << "smaller table" << std::endl;
1084
1085 return false;
1086 }
1087 }
1088 else
1089 {
1090 if (smaller.search(p) != nullptr)
1091 std::cout << "Inconsistency " << in_sz << "/" << out_sz << " found "
1092 << " in smaller table" << std::endl;
1093
1094 if (equal.search(p) != nullptr)
1095 std::cout << "Inconsistency " << in_sz << "/" << out_sz << " found "
1096 << std::endl;
1097
1098 if (greater.search(p) == nullptr)
1099 {
1100 std::cout << "node with " << in_sz << "/" << out_sz << " not found "
1101 << "greater table" << std::endl;
1102
1103 return false;
1104 }
1105 }
1106 }
1107
1108 return true;
1109 }
1110
1111 // This call is made right after inserting a new arc src-->tgt.
1112 // This implies that out(src) is updated, but in(tgt) is not.
1114 {
1115 if (not this->save_parity)
1116 return;
1117
1118 const size_t & src_out_degree = this->g.get_num_arcs(src);
1119 const long & src_in_degree = NODE_COUNTER(src);
1120
1121 if (src_out_degree == src_in_degree)
1122 { // src is in greater ==> remove it and insert into equal
1123 assert(this->smaller.search(src) != nullptr);
1124 this->smaller.remove(src);
1125 this->equal.insert(src);
1126 }
1127 else if (src_out_degree > src_in_degree)
1128 if (src_out_degree == src_in_degree + 1)
1129 {
1130 assert(this->equal.search(src) != nullptr);
1131 this->equal.remove(src);
1132 this->greater.insert(src);
1133 }
1134 else
1135 assert(this->greater.search(src) != nullptr);
1136 else // src_out_degree < src_in_degree
1137 assert(this->smaller.search(src) != nullptr);
1138
1139 const size_t & tgt_out_degree = this->g.get_num_arcs(tgt);
1140 const long tgt_in_degree = ++NODE_COUNTER(tgt);
1141
1142 if (tgt_out_degree == tgt_in_degree)
1143 {
1144 assert(this->greater.search(tgt));
1145 this->greater.remove(tgt);
1146 this->equal.insert(tgt);
1147 }
1148 else if (tgt_out_degree > tgt_in_degree)
1149 assert(this->greater.search(tgt));
1150 else // (tgt_out_degree < tgt_in_degree)
1151 {
1152 if (tgt_in_degree - 1 == tgt_out_degree)
1153 { // tgt is in equal ==> remove it
1154 assert(this->equal.search(tgt) != nullptr);
1155 this->smaller.insert(tgt);
1156 this->equal.remove(tgt);
1157 }
1158 else
1159 assert(this->smaller.search(tgt) != nullptr);
1160 }
1161 }
1162
1163 // The former graph took the flag of g with it when it was moved out:
1164 // g says again that it is directed, also if its type is undirected
1165 // (see set_digraph()). The parities of the nodes of that graph go.
1166 void prepare_new_graph() override
1167 {
1168 this->g.set_digraph(true);
1169 greater.empty();
1170 smaller.empty();
1171 equal.empty();
1172 }
1173
1175 {
1176 this->nodes = std::unique_ptr<DynArray<GT_Node *>>
1177 (new DynArray<GT_Node *>(this->num_nodes));
1178
1179 this->nodes->reserve(this->num_nodes);
1180
1181 for (size_t i = 0; i < this->num_nodes; ++i)
1182 {
1183 typename GT::Node *p = this->g.insert_node(new GT_Node);
1184 this->nodes->access(i) = p;
1185 this->creation.insert(p, i);
1186 this->init_node(this->g, p);
1187
1188 if (this->save_parity)
1189 {
1190 NODE_COUNTER(p) = 0;
1191 this->equal.insert(p);
1192 }
1193 }
1194
1195 this->idx_arc = std::unique_ptr<IndexArc<GT>>(new IndexArc<GT>(this->g));
1196 }
1197
1198 void connect() override
1199 {
1200 DynList<DynList<typename GT::Node *>> blk_list; // disconnected subgraphs
1201
1202 { // save in-degrees since Tarjan's algorithm will modify them
1205
1206 typename GT::Node_Iterator it(this->g);
1207 for (int i = 0; it.has_curr(); it.next_ne(), ++i)
1208 in_degree.access(i) = NODE_COUNTER(it.get_curr_ne());
1209
1210 Tarjan_Connected_Components<GT>()(this->g, blk_list);
1211
1212 it.reset_first(); // restore in-degrees
1213 for (size_t i = 0; it.has_curr(); it.next_ne(), ++i)
1214 NODE_COUNTER(it.get_curr_ne()) = in_degree.access(i);
1215 }
1216 this->sort_components(blk_list);
1217
1218 const size_t & num_blocks = blk_list.size();
1219
1220 if (num_blocks == 1)
1221 return;
1222
1223 // each node in this list is a randomly selected node from block i
1225 b1.reserve(num_blocks);
1227 b2.reserve(num_blocks); {
1228 typename DynList<DynList<GT_Node *>>::Iterator it(blk_list);
1229 for (size_t i = 0; it.has_curr(); it.next_ne(), ++i)
1230 { // select two random nodes from the current component
1231 DynList<typename GT::Node *> & list = it.get_curr_ne();
1232 b1.access(i) = this->select_random_node(list);
1233 b2.access(i) = this->select_random_node(list);
1234 }
1235 }
1236
1237 for (size_t i = 0; i + 1 < num_blocks; ++i)
1238 {
1239 auto src = b1.access(i); // node in block i
1240 auto tgt = b1.access((i + 1) % num_blocks); // node in block i + 1
1241
1242 if (this->idx_arc->search_directed(src, tgt) == nullptr)
1243 this->insert_arc(src, tgt);
1244
1245 src = b2.access(i); // node in block i
1246 tgt = b2.access((i + 1) % num_blocks); // node in block i + 1
1247
1248 if (this->idx_arc->search_directed(tgt, src) == nullptr)
1249 this->insert_arc(tgt, src);
1250 }
1251 }
1252
1253 // Create a random graph where the probability of an arc between
1254 // any pair of nodes is p
1255 GT create_p(const size_t & __num_nodes, const double & p,
1256 bool connected) override
1257 {
1258 ah_domain_error_if(p > 1.0 or p <= 0.0) << "Invalid value for p";
1259
1260 this->initialize_and_create_nodes(__num_nodes, __num_nodes);
1261
1262 for (size_t i = 0; i < this->num_nodes; ++i)
1263 {
1264 auto src = this->nodes->access(i);
1265 for (size_t j = 0; j < this->num_nodes; ++j)
1266 if (i != j and this->r->uniform() <= p)
1267 {
1268 auto tgt = this->nodes->access(j);
1269 assert(this->idx_arc->search_directed(src, tgt) == nullptr);
1270 this->insert_arc(src, tgt);
1271 }
1272 }
1273
1274 if (connected)
1275 connect();
1276
1277 return std::move(this->g);
1278 }
1279
1280 public:
1287 template <class N = Init_Node, class A = Init_Arc>
1288 requires (std::constructible_from<Kept_Initializer<Init_Node>, N> and
1289 std::constructible_from<Kept_Initializer<Init_Arc>, A>)
1290 Random_Digraph(unsigned long seed = time(nullptr),
1291 N && __init_node = Init_Node(),
1292 A && __init_arc = Init_Arc())
1293 : Random_Graph_Base<GT, Init_Node, Init_Arc>
1294 (seed, std::forward<N>(__init_node), std::forward<A>(__init_arc))
1295 {
1296 // empty
1297 }
1298
1299 template <class N, class A>
1300 requires (std::constructible_from<Kept_Initializer<Init_Node>, N> and
1301 std::constructible_from<Kept_Initializer<Init_Arc>, A>)
1302 Random_Digraph(N && __init_node, A && __init_arc)
1303 : Random_Digraph(time(nullptr), std::forward<N>(__init_node),
1304 std::forward<A>(__init_arc))
1305 {
1306 // empty
1307 }
1308
1325 GT operator ()(const size_t & __num_nodes, const size_t & __num_arcs,
1326 bool connected = true)
1327 {
1328 return this->create(__num_nodes, __num_arcs, connected);
1329 }
1330
1355 GT operator ()(const size_t & __num_nodes, const double & p,
1356 bool connected = true)
1357 {
1358 return this->create_p(__num_nodes, p, connected);
1359 }
1360
1361 private:
1362 // The arc src --> tgt, from a node with more arcs in than out to one
1363 // with more out than in; or, if it is there, a node mid with the arcs
1364 // src --> mid and mid --> tgt, which keep its balance: a random node
1365 // of equal, as many times as there are, and then any node. equal could
1366 // be empty, and the search had no end when no node was free.
1367 bool join(GT_Node *src, GT_Node *tgt)
1368 {
1369 if (this->idx_arc->search_directed(src, tgt) == nullptr)
1370 {
1371 this->insert_arc(src, tgt);
1372 return true;
1373 }
1374
1375 auto fits = [&] (GT_Node *p)
1376 {
1377 return p != src and p != tgt and
1378 this->idx_arc->search_directed(src, p) == nullptr and
1379 this->idx_arc->search_directed(p, tgt) == nullptr;
1380 };
1381
1382 GT_Node *mid = nullptr;
1383 for (size_t tries = this->equal.size(); tries > 0 and mid == nullptr;
1384 --tries)
1385 {
1386 auto p = this->equal.select
1387 (this->r->uniform_int(this->equal.size()));
1388 if (fits(p))
1389 mid = p;
1390 }
1391
1392 for (size_t i = 0; mid == nullptr and i < this->nodes->size(); ++i)
1393 if (fits(this->nodes->access(i)))
1394 mid = this->nodes->access(i);
1395
1396 if (mid == nullptr)
1397 return false;
1398
1399 this->insert_arc(src, mid);
1400 this->insert_arc(mid, tgt);
1401
1402 return true;
1403 }
1404
1405 // Any pair of unbalanced nodes without the arc; or else any pair that
1406 // join() joins through a node
1408 {
1409 DynArray<GT_Node *> srcs, tgts; // the sets change when the arcs go in
1410 for (typename Base::Node_Set::Iterator it(this->smaller);
1411 it.has_curr(); it.next_ne())
1412 srcs.append(it.get_curr_ne());
1413 for (typename Base::Node_Set::Iterator it(this->greater);
1414 it.has_curr(); it.next_ne())
1415 tgts.append(it.get_curr_ne());
1416
1417 for (size_t i = 0; i < srcs.size(); ++i)
1418 for (size_t j = 0; j < tgts.size(); ++j)
1419 if (this->idx_arc->search_directed(srcs.access(i), tgts.access(j))
1420 == nullptr)
1421 {
1422 this->insert_arc(srcs.access(i), tgts.access(j));
1423 return true;
1424 }
1425
1426 for (size_t i = 0; i < srcs.size(); ++i)
1427 for (size_t j = 0; j < tgts.size(); ++j)
1428 if (join(srcs.access(i), tgts.access(j)))
1429 return true;
1430
1431 return false;
1432 }
1433
1434 // A path of arcs that the digraph lacks, from src, which has more arcs
1435 // in than out, to the nearest node with more out than in; its inner
1436 // nodes keep their balance. There always is one: if the nodes that such
1437 // paths reach from src had no arcs in excess, all the arcs from them to
1438 // the rest would be in the digraph, and they would have more arcs out
1439 // than in, while src has more in than out.
1441 {
1442 const size_t n = this->nodes->size();
1443 DynMapTree<GT_Node *, long> parent; // also the visited nodes
1445 parent.insert(src, -1);
1446 q.put(src);
1448 GT_Node *tgt = nullptr;
1449 while (not q.is_empty() and tgt == nullptr)
1450 {
1451 auto x = q.get();
1452 for (size_t i = 0; i < n; ++i)
1453 {
1454 auto y = this->nodes->access(i);
1455 if (y == x or parent.search(y) != nullptr or
1456 this->idx_arc->search_directed(x, y) != nullptr)
1457 continue;
1458 parent.insert(y, 0);
1459 from.insert(y, x);
1460 if (this->greater.search(y) != nullptr)
1461 {
1462 tgt = y;
1463 break;
1464 }
1465 q.put(y);
1466 }
1467 }
1468 ah_domain_error_if(tgt == nullptr) // it cannot happen
1469 << "The digraph cannot be made Eulerian by adding arcs to it";
1470
1471 DynList<GT_Node *> path; // from tgt back to src
1472 for (auto y = tgt; y != src; y = from.find(y))
1473 path.insert(y);
1474 GT_Node *x = src;
1475 for (typename DynList<GT_Node *>::Iterator it(path); it.has_curr();
1476 it.next_ne())
1477 {
1478 auto y = it.get_curr_ne();
1479 this->insert_arc(x, y);
1480 x = y;
1481 }
1482 }
1483
1484 void make_eulerian() override
1485 {
1486 GT_Node *src = nullptr;
1487 GT_Node *tgt = nullptr;
1488
1489 while (this->greater.size() > 0 and this->smaller.size() > 0)
1490 {
1491 do
1492 {
1493 tgt = this->greater.select
1494 (this->r->uniform_int(this->greater.size()));
1495 src = this->smaller.select
1496 (this->r->uniform_int(this->smaller.size()));
1497 }
1498 while (src == tgt);
1499
1500 if (not (join(src, tgt) or join_any_unbalanced_nodes()))
1502 }
1503 }
1504
1506 {
1507 const size_t & n = this->g.get_num_nodes();
1508 const size_t n2 = n / 2;
1509
1510 while (not (this->g.get_num_arcs(p) >= n2 and NODE_COUNTER(p) >= n2))
1511 {
1512 auto q = this->nodes->access(this->r->uniform_int(n));
1513 if (q == p)
1514 continue;
1515
1516 if (this->idx_arc->search_directed(p, q) == nullptr)
1517 {
1518 this->insert_arc(p, q);
1519 NODE_COUNTER(q)++;
1520 }
1521
1522 if (this->idx_arc->search_directed(q, p) == nullptr)
1523 {
1524 this->insert_arc(q, p);
1525 NODE_COUNTER(p)++;
1526 }
1527 }
1528 }
1529
1530 // Balance both nodes so they satisfy the Hamiltonian condition.
1531 // If arc src-->tgt exists, balance each node independently.
1533 {
1534 if (this->idx_arc->search_directed(src, tgt) != nullptr)
1535 {
1538
1539 return;
1540 }
1541
1542 const size_t & n = this->g.get_num_nodes();
1543
1544 while (this->g.get_num_arcs(src) + NODE_COUNTER(tgt) < n)
1545 {
1546 auto p = this->nodes->access(this->r->uniform_int(n));
1547 if (p == src or p == tgt)
1548 continue;
1549
1550 if (this->idx_arc->search_directed(src, p) == nullptr)
1551 {
1552 this->insert_arc(src, p);
1553 NODE_COUNTER(p)++;
1554
1555 if (this->g.get_num_arcs(src) + NODE_COUNTER(tgt) == n)
1556 break;
1557 }
1558
1559 if (this->idx_arc->search_directed(p, tgt) == nullptr)
1560 {
1561 this->insert_arc(p, tgt);
1562 NODE_COUNTER(tgt)++;
1563 }
1564 }
1565
1566 assert(this->g.get_num_arcs(src) + NODE_COUNTER(tgt) >= n);
1567 }
1568
1569 void make_hamiltonian() override
1570 {
1571 this->g.reset_counter_nodes();
1572
1573 // compute in-degree for each node
1574 for (typename GT::Arc_Iterator it(this->g); it.has_curr(); it.next_ne())
1575 NODE_COUNTER(it.get_tgt_node_ne())++;
1576
1577 const size_t & n = this->g.get_num_nodes();
1578
1579 for (size_t i = 0; i < n; ++i)
1580 {
1581 auto src = this->nodes->access(i);
1582 for (size_t j = 0; j < n; ++j)
1583 {
1584 if (i == j)
1585 continue;
1586
1587 auto tgt = this->nodes->access(j);
1589 }
1590 }
1591 }
1592 };
1593}
1594
1595# endif // RANDOM_GRAPH_H
Tarjan's algorithm for strongly connected components.
Exception handling system with formatted messages for Aleph-w.
#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.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
RAII guard for exception-safe resource cleanup.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
void cut(const size_t new_dim=0)
Cut the array to a new dimension; that is, it reduces the dimension of array and frees the remaining ...
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
bool is_empty() const noexcept
Return true if this is empty.
Iterator on the items of list.
Definition htlist.H:1614
T & get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition htlist.H:1669
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
T & append(const T &item)
Definition htlist.H:1362
T & insert(const T &item)
Definition htlist.H:1308
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
void empty() noexcept
empty the list
Definition htlist.H:1576
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
Dynamic set implemented using randomized binary search trees of type Rand_Tree<Key>.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
size_t remove(const Key &key)
Removes a key from the dynamic set.
Key * search(const Key &key)
Find an element in the set.
Key & select(size_t i)
Returns the ith node in infix position.
void empty()
Remove all the elements of the set.
A Gsl_Compatible_Rng in dynamic memory, so that what holds it grows only by a pointer,...
Definition ah-random.H:494
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
Definition ah-random.H:302
double uniform() noexcept
A real number in [0, 1), as gsl_rng_uniform(r).
Definition ah-random.H:334
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
Definition htlist.H:1012
bool has_curr() const noexcept
Definition htlist.H:969
Index for fast arc lookup by its endpoint nodes.
RAII guard for graph algorithm initialization.
An initializer that a random generator keeps: one given as a modifiable lvalue is used where it is,...
Kept_Initializer(const Kept_Initializer &)=delete
std::optional< F > own
F & get() const noexcept
The initializer.
Kept_Initializer(F &&x)
Keep x, moved.
Kept_Initializer & operator=(const Kept_Initializer &)=delete
Kept_Initializer(F &x) noexcept
Use x where it is.
Kept_Initializer(const F &x)
Keep a copy of x.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Random directed graph (digraph) generator.
Random_Digraph(N &&__init_node, A &&__init_arc)
Base::Node_Set smaller
void update_parity_after_arc_insertion(GT_Node *src, GT_Node *tgt) override
Random_Digraph(unsigned long seed=time(nullptr), N &&__init_node=Init_Node(), A &&__init_arc=Init_Arc())
Constructor.
bool join(GT_Node *src, GT_Node *tgt)
GT create_p(const size_t &__num_nodes, const double &p, bool connected) override
GT operator()(const size_t &__num_nodes, const size_t &__num_arcs, bool connected=true)
Create a sparse random digraph.
void balance_digraph_node(GT_Node *p)
void make_hamiltonian() override
void balance_digraph_nodes_degree(GT_Node *src, GT_Node *tgt)
void connect() override
void prepare_new_graph() override
void make_eulerian() override
void create_nodes_and_initialize_arc_index() override
void join_through_a_path(GT_Node *src)
Random_Graph_Base(const unsigned long seed, N &&__init_node, A &&__init_arc)
virtual void create_nodes_and_initialize_arc_index()=0
std::unique_ptr< DynArray< GT_Node * > > nodes
std::unique_ptr< IndexArc< GT > > idx_arc
size_t order_of(GT_Node *p) const
void initialize_and_create_nodes(const size_t &__num_nodes, const size_t &__num_arcs)
GT create(const size_t &__num_nodes, const size_t &__num_arcs, const bool connected)
Create a sparse random graph.
GT eulerian(const size_t &__num_nodes, const size_t &__num_arcs)
Create a random Eulerian graph (sparse version).
virtual void make_hamiltonian()=0
Kept_Initializer< Init_Node > kept_node
virtual void connect()=0
DynMapHash< GT_Node *, size_t > creation
Gsl_Compatible_Rng_Owner r
GT_Arc * insert_arc(GT_Node *src, GT_Node *tgt)
virtual void update_parity_after_arc_insertion(GT_Node *src, GT_Node *tgt)=0
virtual void prepare_new_graph()
GT eulerian(const size_t &__num_nodes, const double &p)
Create a random Eulerian graph (dense version).
virtual ~Random_Graph_Base()=default
Kept_Initializer< Init_Arc > kept_arc
virtual GT create_p(const size_t &__num_nodes, const double &p, bool connected)=0
GT_Node * select_random_node(DynList< GT_Node * > &list) noexcept
Select a random node from the given list.
void sort_components(DynList< DynList< GT_Node * > > &comps) const
virtual void make_eulerian()=0
GT_Node * select_random_node(GT_Node *excluded=nullptr) noexcept
Select a random node different from excluded.
Random undirected graph generator.
bool all_even() const
Base::Node_Set even_nodes
void make_hamiltonian() override
void join_through_the_complement()
GT operator()(const size_t &__num_nodes, const size_t &__num_arcs, bool connected=true)
Create a sparse random graph.
Random_Graph(unsigned long seed=time(nullptr), N &&__init_node=Init_Node(), A &&__init_arc=Init_Arc())
Constructor.
void create_nodes_and_initialize_arc_index() override
GT create_p(const size_t &__num_nodes, const double &p, const bool connected) override
GT_Arc * join_arc(GT_Node *src, GT_Node *tgt)
void update_parity_after_arc_insertion(GT_Node *src, GT_Node *tgt) override
void balance_graph_nodes_degree(GT_Node *src, GT_Node *tgt)
void prepare_new_graph() override
GT eulerian(const size_t &__num_nodes, const size_t &__num_arcs)
Create a random Eulerian graph (sparse version).
void connect() override
GT eulerian(const size_t &__num_nodes, const double &p)
Create a random Eulerian graph (dense version).
DynList< GT_Arc * > joined
void make_eulerian() override
Base::Node_Set odd_nodes
bool join_through_a_node(GT_Node *src, GT_Node *tgt)
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
Definition Tarjan.H:174
Compute the connected components of a graph.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
void set_digraph(bool val)
Temporal indication for preventing to other algorithms that an graph must be treated as a directed gr...
Definition graph-dry.H:972
void reset_counter_nodes() const noexcept
Reset all the counters to zero for all the nodes of graph.
Definition graph-dry.H:1350
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
#define NODE_COUNTER(p)
Get the counter of a node.
GT sufficient_hamiltonian(const size_t &__num_nodes, const double &p=0.5)
Create a random Hamiltonian graph.
size_t in_degree(typename GT::Node *p, SA sa=SA())
Compute the filtered in degree of node p.
Definition tpl_graph.H:2806
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
GT sufficient_hamiltonian(const size_t &__num_nodes, const double &p=0.5)
Create a random Hamiltonian graph.
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
bool is_even(const long n)
Return true if n is even.
Definition ahUtils.H:106
void mergesort(T *a, const long l, const long r, Array< T > &buf, Compare cmp)
Sort an array using merge sort with a reusable buffer.
STL namespace.
Single graph utilities.
Default arc initializer for random graph generation.
void operator()(GT &, typename GT::Arc *) const noexcept
Default node initializer for random graph generation.
void operator()(GT &, typename GT::Node *) const noexcept
Comparison of nodes by the order in which they were created.
const Random_Graph_Base * base
bool operator()(GT_Node *a, GT_Node *b) const
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
Graph connectivity and connected components.
Dynamic set implementations based on hash tables.
Utility algorithms and operations for graphs.
Arc indexing for fast lookup by endpoint nodes.
Comprehensive sorting algorithms and search utilities for Aleph-w.