46# ifndef RANDOM_GRAPH_H
47# define RANDOM_GRAPH_H
134 F &
get() const noexcept {
return *
f; }
137 template <AlephGraph GT,
class Init_Node,
class Init_Arc>
152 std::unique_ptr<DynArray<GT_Node *>>
nodes;
195 for (
auto it = comps.get_it(); it.has_curr(); it.next_ne())
217 assert(
nodes.get() !=
nullptr);
219 assert(excluded ==
nullptr or
num_nodes > 1);
225 ret_val =
nodes->access(idx);
226 if (excluded ==
nullptr or ret_val != excluded)
238 for (
unsigned long i = 0; i <
k; ++i, it.
next_ne()) {}
255 const size_t & __num_arcs)
258 <<
"Number of nodes must be greater than 0";
275 template <
class N,
class A>
291 GT create(
const size_t & __num_nodes,
const size_t & __num_arcs,
292 const bool connected)
297 for (
size_t i = 0; i <
num_arcs; ++i)
301 if (
idx_arc->search(src, tgt) ==
nullptr)
311 virtual GT create_p(
const size_t & __num_nodes,
const double & p,
335 GT eulerian(
const size_t & __num_nodes,
const size_t & __num_arcs)
339 g = this->
create(__num_nodes, __num_arcs,
true);
368 g = this->
create_p(__num_nodes, p,
true);
402 const double & p = 0.5)
404 g = this->
create_p(__num_nodes, p,
true);
456 template <AlephGraph
GT,
516 this->
nodes = std::unique_ptr<DynArray<GT_Node *>>
521 for (
size_t i = 0; i < this->
num_nodes; ++i)
524 this->
nodes->access(i) = p;
544 const size_t & num_subs = subgraphs.
size();
552 it.has_curr(); it.next_ne())
555 for (
size_t i = 1; i < num_subs; ++i)
557 auto src = block_nodes.
access(i - 1);
558 auto tgt = block_nodes.
access(i);
566 const bool connected)
override
572 for (
size_t i = 0; i + 1 < this->
num_nodes; ++i)
574 auto src = this->
nodes->access(i);
575 for (
size_t j = i + 1; j < this->
num_nodes; ++j)
578 auto tgt = this->
nodes->access(j);
587 return std::move(this->
g);
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>)
601 N && __init_node = Init_Node(),
604 (
seed,
std::forward<
N>(__init_node),
std::forward<A>(__init_arc))
607 <<
"Building of random digraph through a graph";
627 GT
operator ()(
const size_t & __num_nodes,
const size_t & __num_arcs,
628 bool connected =
true)
630 return this->
create(__num_nodes, __num_arcs, connected);
657 bool connected =
true)
659 return create_p(__num_nodes, p, connected);
671 return p != src
and p != tgt
and
673 this->
idx_arc->search(tgt, p) ==
nullptr;
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);
705 for (
size_t tries = this->
odd_nodes.
size(); tries > 0; --tries)
715 if (this->
idx_arc->search(src, tgt) ==
nullptr)
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());
737 for (
size_t i = 0; i < odd.
size(); ++i)
738 for (
size_t j = i + 1; j < odd.
size(); ++j)
745 for (
size_t i = 0; i < odd.
size(); ++i)
746 for (
size_t j = i + 1; j < odd.
size(); ++j)
764 this->
idx_arc->remove_from_graph(it.get_curr_ne());
767 const size_t n = this->
nodes->size();
770 for (
size_t i = 0; i < n; ++i)
772 auto p = this->
nodes->access(i);
784 for (
size_t i = 0; i < n; ++i)
787 while (unvisited.
size() > 0)
789 size_t head = order.
size();
791 unvisited.
cut(unvisited.
size() - 1);
794 while (head < order.
size())
796 const size_t x = order.
access(head++);
797 auto px = this->
nodes->access(x);
799 for (
size_t k = 0;
k < unvisited.
size(); ++
k)
801 const size_t y = unvisited.
access(
k);
802 if (this->
idx_arc->search(px, this->nodes->access(
y)) ==
nullptr)
804 parent.
access(
y) =
static_cast<long>(x);
810 std::swap(unvisited, rest);
816 for (
size_t i = 0; i < n; ++i)
820 for (
size_t k = order.
size();
k-- > 0; )
822 const size_t x = order.
access(
k);
826 <<
"The graph cannot be made Eulerian by adding arcs to it";
834 const size_t x = it.get_curr_ne();
836 this->nodes->access(parent.
access(x)));
856 for (
typename GT::Node_Iterator it(this->
g); it.has_curr(); it.next_ne())
864 if (this->
idx_arc->search(src, tgt) ==
nullptr)
869 while (this->
g.
get_num_arcs(src) + this->g.get_num_arcs(tgt) < n)
872 if (p == src or p == tgt)
875 if (this->
idx_arc->search(src, p) ==
nullptr)
878 if (this->
g.
get_num_arcs(src) + this->g.get_num_arcs(tgt) == n)
881 if (this->
idx_arc->search(tgt, p) ==
nullptr)
889 for (
size_t i = 0; i + 1 < n; ++i)
891 auto src = this->
nodes->access(i);
892 for (
size_t j = i + 1; j < n; ++j)
915 GT eulerian(
const size_t & __num_nodes,
const size_t & __num_arcs)
919 this->
g = this->
create(__num_nodes, __num_arcs,
true);
922 return std::move(this->g);
949 this->
g = this->
create_p(__num_nodes, p,
true);
952 return std::move(this->g);
983 const double & p = 0.5)
985 this->
g = this->
create_p(__num_nodes, p,
true);
988 return std::move(this->
g);
1013 template <AlephGraph
GT,
1030 const size_t & n = this->
nodes->size();
1033 std::cout <<
"Warning num of nodes of graph does not match with array "
1038 std::cout <<
"Inconsistency with nodes parity" << std::endl
1039 <<
"greater = " <<
greater.size() << std::endl
1041 <<
"equal = " <<
equal.
size() << std::endl
1042 <<
"total = " << total << std::endl
1045 for (
size_t i = 0; i < n; ++i)
1047 auto p = this->
nodes->access(i);
1052 if (in_sz == out_sz)
1055 std::cout <<
"Inconsistency " << in_sz <<
"/" << out_sz <<
" found "
1056 <<
" in smaller table" << std::endl;
1058 if (
greater.search(p) !=
nullptr)
1059 std::cout <<
"Inconsistency " << in_sz <<
"/" << out_sz <<
" found "
1060 <<
" in greater table" << std::endl;
1064 std::cout <<
"node of same in/out degree is not in equal table"
1070 else if (in_sz > out_sz)
1072 if (
greater.search(p) !=
nullptr)
1073 std::cout <<
"Inconsistency " << in_sz <<
"/" << out_sz <<
" found "
1074 <<
" in greater table" << std::endl;
1077 std::cout <<
"Inconsistency " << in_sz <<
"/" << out_sz <<
" found "
1082 std::cout <<
"node with " << in_sz <<
"/" << out_sz <<
" not found "
1083 <<
"smaller table" << std::endl;
1091 std::cout <<
"Inconsistency " << in_sz <<
"/" << out_sz <<
" found "
1092 <<
" in smaller table" << std::endl;
1095 std::cout <<
"Inconsistency " << in_sz <<
"/" << out_sz <<
" found "
1098 if (
greater.search(p) ==
nullptr)
1100 std::cout <<
"node with " << in_sz <<
"/" << out_sz <<
" not found "
1101 <<
"greater table" << std::endl;
1121 if (src_out_degree == src_in_degree)
1127 else if (src_out_degree > src_in_degree)
1128 if (src_out_degree == src_in_degree + 1)
1135 assert(this->
greater.search(src) !=
nullptr);
1142 if (tgt_out_degree == tgt_in_degree)
1144 assert(this->
greater.search(tgt));
1148 else if (tgt_out_degree > tgt_in_degree)
1149 assert(this->
greater.search(tgt));
1152 if (tgt_in_degree - 1 == tgt_out_degree)
1176 this->
nodes = std::unique_ptr<DynArray<GT_Node *>>
1181 for (
size_t i = 0; i < this->
num_nodes; ++i)
1184 this->
nodes->access(i) = p;
1206 typename GT::Node_Iterator it(this->
g);
1207 for (
int i = 0; it.has_curr(); it.next_ne(), ++i)
1213 for (
size_t i = 0; it.has_curr(); it.next_ne(), ++i)
1218 const size_t & num_blocks = blk_list.
size();
1220 if (num_blocks == 1)
1229 for (
size_t i = 0; it.has_curr(); it.next_ne(), ++i)
1237 for (
size_t i = 0; i + 1 < num_blocks; ++i)
1240 auto tgt = b1.
access((i + 1) % num_blocks);
1242 if (this->
idx_arc->search_directed(src, tgt) ==
nullptr)
1246 tgt = b2.
access((i + 1) % num_blocks);
1248 if (this->
idx_arc->search_directed(tgt, src) ==
nullptr)
1256 bool connected)
override
1262 for (
size_t i = 0; i < this->
num_nodes; ++i)
1264 auto src = this->
nodes->access(i);
1265 for (
size_t j = 0; j < this->
num_nodes; ++j)
1268 auto tgt = this->
nodes->access(j);
1269 assert(this->
idx_arc->search_directed(src, tgt) ==
nullptr);
1277 return std::move(this->
g);
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>)
1291 N && __init_node = Init_Node(),
1294 (
seed,
std::forward<
N>(__init_node),
std::forward<A>(__init_arc))
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>)
1304 std::forward<A>(__init_arc))
1326 bool connected =
true)
1328 return this->
create(__num_nodes, __num_arcs, connected);
1356 bool connected =
true)
1358 return this->
create_p(__num_nodes, p, connected);
1369 if (this->
idx_arc->search_directed(src, tgt) ==
nullptr)
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;
1383 for (
size_t tries = this->
equal.
size(); tries > 0
and mid ==
nullptr;
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);
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());
1417 for (
size_t i = 0; i < srcs.
size(); ++i)
1418 for (
size_t j = 0; j < tgts.
size(); ++j)
1426 for (
size_t i = 0; i < srcs.
size(); ++i)
1427 for (
size_t j = 0; j < tgts.
size(); ++j)
1442 const size_t n = this->
nodes->size();
1452 for (
size_t i = 0; i < n; ++i)
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)
1460 if (this->
greater.search(
y) !=
nullptr)
1469 <<
"The digraph cannot be made Eulerian by adding arcs to it";
1472 for (
auto y = tgt;
y != src;
y = from.
find(
y))
1478 auto y = it.get_curr_ne();
1489 while (this->
greater.size() > 0
and this->smaller.size() > 0)
1508 const size_t n2 = n / 2;
1516 if (this->
idx_arc->search_directed(p, q) ==
nullptr)
1522 if (this->
idx_arc->search_directed(q, p) ==
nullptr)
1534 if (this->
idx_arc->search_directed(src, tgt) !=
nullptr)
1547 if (p == src or p == tgt)
1550 if (this->
idx_arc->search_directed(src, p) ==
nullptr)
1559 if (this->
idx_arc->search_directed(p, tgt) ==
nullptr)
1574 for (
typename GT::Arc_Iterator it(this->
g); it.has_curr(); it.next_ne())
1579 for (
size_t i = 0; i < n; ++i)
1581 auto src = this->
nodes->access(i);
1582 for (
size_t j = 0; j < n; ++j)
1587 auto tgt = this->
nodes->access(j);
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.
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
constexpr Dlink *& get_first() const noexcept
If this is a header node, it return the first node of this
void insert(Dlink *node) noexcept
Insert node after this.
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.
T & get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Doubly-linked list (defined in tpl_dynList.H).
T & get_first()
Return the first item of the list.
T & append(const T &item)
T & insert(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
void empty() noexcept
empty the list
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,...
A random number generator with the sequences of GSL's gsl_rng_mt19937.
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).
double uniform() noexcept
A real number in [0, 1), as gsl_rng_uniform(r).
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
bool has_curr() const noexcept
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
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.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Number Theoretic Transform over Z / MOD Z.
Random directed graph (digraph) generator.
Random_Digraph(N &&__init_node, A &&__init_arc)
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)
bool join_any_unbalanced_nodes()
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
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.
Base::Node_Set even_nodes
bool join_random_odd_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).
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
bool join_through_a_node(GT_Node *src, GT_Node *tgt)
bool join_any_odd_nodes()
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
Compute the connected components of a graph.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
bool is_digraph() const noexcept
Return true if the graph this is directed.
void set_digraph(bool val)
Temporal indication for preventing to other algorithms that an graph must be treated as a directed gr...
void reset_counter_nodes() const noexcept
Reset all the counters to zero for all the nodes of graph.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
__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)
__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)
#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.
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
GT sufficient_hamiltonian(const size_t &__num_nodes, const double &p=0.5)
Create a random Hamiltonian graph.
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.
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.
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
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.