|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Random undirected graph generator. More...
#include <random_graph.H>
Public Member Functions | |
| template<class N = Init_Node, class A = Init_Arc> requires (std::constructible_from<Kept_Initializer<Init_Node>, N> and std::constructible_from<Kept_Initializer<Init_Arc>, A>) | |
| Random_Graph (unsigned long seed=time(nullptr), N &&__init_node=Init_Node(), A &&__init_arc=Init_Arc()) | |
| Constructor. | |
| GT | operator() (const size_t &__num_nodes, const size_t &__num_arcs, bool connected=true) |
| Create a sparse random graph. | |
| GT | operator() (const size_t &__num_nodes, const double &p, bool connected=true) |
| Create a random graph with arc probability. | |
| 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). | |
| GT | sufficient_hamiltonian (const size_t &__num_nodes, const double &p=0.5) |
| Create a random Hamiltonian graph. | |
Public Member Functions inherited from Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc > | |
| 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). | |
| GT | sufficient_hamiltonian (const size_t &__num_nodes, const double &p=0.5) |
| Create a random Hamiltonian graph. | |
Private Types | |
| typedef GT::Node | GT_Node |
| typedef GT::Arc | GT_Arc |
| using | Base = Random_Graph_Base< GT, Init_Node, Init_Arc > |
Private Member Functions | |
| GT_Arc * | join_arc (GT_Node *src, GT_Node *tgt) |
| void | update_parity_after_arc_insertion (GT_Node *src, GT_Node *tgt) override |
| void | prepare_new_graph () override |
| void | create_nodes_and_initialize_arc_index () override |
| void | connect () override |
| GT | create_p (const size_t &__num_nodes, const double &p, const bool connected) override |
| bool | join_through_a_node (GT_Node *src, GT_Node *tgt) |
| bool | join_random_odd_nodes () |
| bool | join_any_odd_nodes () |
| void | join_through_the_complement () |
| void | make_eulerian () override |
| bool | all_even () const |
| void | balance_graph_nodes_degree (GT_Node *src, GT_Node *tgt) |
| void | make_hamiltonian () override |
Private Attributes | |
| Base::Node_Set | odd_nodes {typename Base::By_Creation{this}} |
| Base::Node_Set | even_nodes {typename Base::By_Creation{this}} |
| DynList< GT_Arc * > | joined |
Additional Inherited Members | |
Protected Types inherited from Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc > | |
| typedef GT::Node | GT_Node |
| typedef GT::Arc | GT_Arc |
| using | Node_Set = DynSetRandTree< GT_Node *, By_Creation > |
Protected Member Functions inherited from Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc > | |
| size_t | order_of (GT_Node *p) const |
| void | sort_components (DynList< DynList< GT_Node * > > &comps) const |
| GT_Arc * | insert_arc (GT_Node *src, GT_Node *tgt) |
| GT_Node * | select_random_node (GT_Node *excluded=nullptr) noexcept |
Select a random node different from excluded. | |
| GT_Node * | select_random_node (DynList< GT_Node * > &list) noexcept |
| Select a random node from the given list. | |
| void | initialize_and_create_nodes (const size_t &__num_nodes, const size_t &__num_arcs) |
| template<class N , class A > | |
| Random_Graph_Base (const unsigned long seed, N &&__init_node, A &&__init_arc) | |
| virtual | ~Random_Graph_Base ()=default |
| GT | create (const size_t &__num_nodes, const size_t &__num_arcs, const bool connected) |
| Create a sparse random graph. | |
Protected Attributes inherited from Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc > | |
| Kept_Initializer< Init_Node > | kept_node |
| Kept_Initializer< Init_Arc > | kept_arc |
| Gsl_Compatible_Rng_Owner | r |
| Init_Node & | init_node |
| Init_Arc & | init_arc |
| std::unique_ptr< DynArray< GT_Node * > > | nodes |
| std::unique_ptr< IndexArc< GT > > | idx_arc |
| size_t | num_nodes |
| size_t | num_arcs |
| unsigned long | rand_max |
| GT | g |
| bool | save_parity |
| DynMapHash< GT_Node *, size_t > | creation |
Random undirected graph generator.
Random_Graph is a class for generating random undirected graphs with a specified number of nodes and, depending on the invoked operation, a specified number of arcs or arc probability.
The class exports two generation methods via the function-call operator, intended for generating sparse and dense graphs respectively.
For this class to compile, GT::Node_Type and GT::Arc_Type must have default constructors.
Template parameters:
The Init_Node class must have the following structure:
The Init_Arc class must have the following structure:
The generation routines include a boolean parameter that determines whether the graph should be connected. By default, this parameter is true. Consider setting it to false if connectivity is not required, since ensuring connectivity requires computing connected components, which adds time and memory overhead.
Definition at line 459 of file random_graph.H.
|
private |
Definition at line 464 of file random_graph.H.
|
private |
Definition at line 462 of file random_graph.H.
|
private |
Definition at line 461 of file random_graph.H.
|
inline |
Constructor.
| [in] | seed | Seed for the random number generator. |
| [in] | __init_node | Node initialization functor. |
| [in] | __init_arc | Arc initialization functor. |
Definition at line 600 of file random_graph.H.
References ah_domain_error_if, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, and GraphCommon< GT, Node, Arc >::is_digraph().
|
inlineprivate |
Definition at line 854 of file random_graph.H.
References Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, and GraphCommon< GT, Node, Arc >::get_num_arcs().
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::make_eulerian().
|
inlineprivate |
Definition at line 862 of file random_graph.H.
References Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::idx_arc, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::insert_arc(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::nodes, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::r, and Aleph::Gsl_Compatible_Rng::uniform_int().
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::make_hamiltonian().
|
inlineoverrideprivatevirtual |
Implements Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >.
Definition at line 537 of file random_graph.H.
References Aleph::DynArray< T >::access(), Aleph::DynArray< T >::append(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::insert_arc(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::select_random_node(), Aleph::DynList< T >::size(), and Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::sort_components().
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::create_p().
|
inlineoverrideprivatevirtual |
Implements Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >.
Definition at line 514 of file random_graph.H.
References Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::creation, Aleph::Random_Graph< GT, Init_Node, Init_Arc >::even_nodes, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::idx_arc, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::init_node, Aleph::DynSetTree< Key, Tree, Compare >::insert(), Aleph::Dlink::insert(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), NODE_COUNTER, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::nodes, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::num_nodes, and Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::save_parity.
|
inlineoverrideprivatevirtual |
Implements Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >.
Definition at line 565 of file random_graph.H.
References ah_domain_error_if, Aleph::Random_Graph< GT, Init_Node, Init_Arc >::connect(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::initialize_and_create_nodes(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::insert_arc(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::nodes, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::num_nodes, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::r, and Aleph::Gsl_Compatible_Rng::uniform().
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::eulerian(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::operator()(), and Aleph::Random_Graph< GT, Init_Node, Init_Arc >::sufficient_hamiltonian().
|
inline |
Create a random Eulerian graph (dense version).
This version builds a random Eulerian graph with probability p of an arc existing between any pair of nodes.
Use this routine to generate dense graphs. In this case, use a value of p greater than or equal to 0.5.
__num_nodes increases.| [in] | __num_nodes | Number of nodes the graph should have. |
| [in] | p | Probability of an arc existing between any pair of nodes. |
| bad_alloc | if there is not enough memory. |
| domain_error | if p is not in the range (0, 1]. |
| domain_error | if the graph cannot be made Eulerian by adding arcs to it, as a complete graph of an even number of nodes. |
Definition at line 945 of file random_graph.H.
References Aleph::Random_Graph< GT, Init_Node, Init_Arc >::create_p(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, Aleph::Random_Graph< GT, Init_Node, Init_Arc >::make_eulerian(), and Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::save_parity.
|
inline |
Create a random Eulerian graph (sparse version).
This version builds a sparse random graph that is guaranteed to be Eulerian; that is, it contains Eulerian cycles.
The process first generates a sparse random graph, then examines the result and creates new arcs so that the output contains an Eulerian cycle.
| [in] | __num_nodes | Number of nodes the graph should have. |
| [in] | __num_arcs | Number of arcs the graph should have. |
| bad_alloc | if there is not enough memory. |
| domain_error | if the graph cannot be made Eulerian by adding arcs to it, as a complete graph of an even number of nodes. |
Definition at line 915 of file random_graph.H.
References Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::create(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, Aleph::Random_Graph< GT, Init_Node, Init_Arc >::make_eulerian(), and Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::save_parity.
Referenced by demo_eulerian(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 730 of file random_graph.H.
References Aleph::DynArray< T >::access(), Aleph::DynArray< T >::append(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::idx_arc, Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_arc(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_through_a_node(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::odd_nodes, and Aleph::DynArray< T >::size().
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::make_eulerian().
|
inlineprivate |
Definition at line 472 of file random_graph.H.
References Aleph::DynList< T >::append(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::insert_arc(), and Aleph::Random_Graph< GT, Init_Node, Init_Arc >::joined.
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_any_odd_nodes(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_random_odd_nodes(), and Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_through_a_node().
|
inlineprivate |
Definition at line 703 of file random_graph.H.
References Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::idx_arc, Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_arc(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_through_a_node(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::odd_nodes, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::r, Aleph::DynSetTree< Key, Tree, Compare >::select(), Aleph::DynSetTree< Key, Tree, Compare >::size(), and Aleph::Gsl_Compatible_Rng::uniform_int().
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::make_eulerian().
|
inlineprivate |
Definition at line 667 of file random_graph.H.
References Aleph::and, Aleph::Random_Graph< GT, Init_Node, Init_Arc >::even_nodes, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::idx_arc, Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_arc(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::nodes, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::r, Aleph::DynSetTree< Key, Tree, Compare >::select(), Aleph::DynSetTree< Key, Tree, Compare >::size(), and Aleph::Gsl_Compatible_Rng::uniform_int().
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_any_odd_nodes(), and Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_random_odd_nodes().
|
inlineprivate |
Definition at line 760 of file random_graph.H.
References Aleph::DynArray< T >::access(), ah_domain_error_if, Aleph::DynArray< T >::append(), Aleph::DynList< T >::append(), Aleph::DynArray< T >::cut(), Aleph::DynSetTree< Key, Tree, Compare >::empty(), Aleph::DynList< T >::empty(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::even_nodes, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, GraphCommon< GT, Node, Arc >::get_num_arcs(), Aleph::HTList::Iterator::has_curr(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::idx_arc, Aleph::DynSetTree< Key, Tree, Compare >::insert(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::insert_arc(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::joined, k, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::nodes, Aleph::Random_Graph< GT, Init_Node, Init_Arc >::odd_nodes, Aleph::DynArray< T >::reserve(), root(), Aleph::DynArray< T >::size(), and y.
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::make_eulerian().
|
inlineoverrideprivatevirtual |
Implements Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >.
Definition at line 840 of file random_graph.H.
References Aleph::Random_Graph< GT, Init_Node, Init_Arc >::all_even(), Aleph::DynList< T >::empty(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_any_odd_nodes(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_random_odd_nodes(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_through_the_complement(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::joined, Aleph::Random_Graph< GT, Init_Node, Init_Arc >::odd_nodes, and Aleph::DynSetTree< Key, Tree, Compare >::size().
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::eulerian(), and Aleph::Random_Graph< GT, Init_Node, Init_Arc >::eulerian().
|
inlineoverrideprivatevirtual |
Implements Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >.
Definition at line 886 of file random_graph.H.
References Aleph::Random_Graph< GT, Init_Node, Init_Arc >::balance_graph_nodes_degree(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, GraphCommon< GT, Node, Arc >::get_num_nodes(), and Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::nodes.
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::sufficient_hamiltonian().
|
inline |
Create a random graph with arc probability.
This version builds a random graph with probability p of an arc existing between any pair of nodes.
Use this routine to generate dense graphs. In this case, use a value of p greater than or equal to 0.5.
The procedure is quadratic: all node pairs are enumerated, and for each pair (i, j) a random draw with probability p determines whether an arc is created.
__num_nodes increases.| [in] | __num_nodes | Number of nodes the graph should have. |
| [in] | p | Probability of an arc existing between any pair of nodes. |
| [in] | connected | Whether the generated graph should be connected. Defaults to true. |
| bad_alloc | if there is not enough memory. |
| domain_error | if p is not in the range (0, 1]. |
Definition at line 656 of file random_graph.H.
References Aleph::Random_Graph< GT, Init_Node, Init_Arc >::create_p().
|
inline |
Create a sparse random graph.
This version builds a random graph with approximately __num_arcs arcs. The graph contains no parallel arcs.
The randomization procedure selects two nodes at random and inserts an arc between them if one does not already exist. The routine is linear, proportional to __num_arcs.
| [in] | __num_nodes | Number of nodes the graph should have. |
| [in] | __num_arcs | Number of arcs the graph should have. |
| [in] | connected | Whether the generated graph should be connected. Defaults to true. |
| bad_alloc | if there is not enough memory. |
Definition at line 627 of file random_graph.H.
References Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::create().
|
inlineoverrideprivatevirtual |
Reimplemented from Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >.
Definition at line 508 of file random_graph.H.
References Aleph::DynSetTree< Key, Tree, Compare >::empty(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::even_nodes, and Aleph::Random_Graph< GT, Init_Node, Init_Arc >::odd_nodes.
|
inlineoverrideprivatevirtual |
Implements Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >.
Definition at line 479 of file random_graph.H.
References Aleph::Random_Graph< GT, Init_Node, Init_Arc >::even_nodes, Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::g, GraphCommon< GT, Node, Arc >::get_num_arcs(), Aleph::DynSetTree< Key, Tree, Compare >::insert(), Aleph::is_even(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::odd_nodes, Aleph::DynSetTree< Key, Tree, Compare >::remove(), and Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::save_parity.
|
private |
Definition at line 467 of file random_graph.H.
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::create_nodes_and_initialize_arc_index(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_through_a_node(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_through_the_complement(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::prepare_new_graph(), and Aleph::Random_Graph< GT, Init_Node, Init_Arc >::update_parity_after_arc_insertion().
|
private |
Definition at line 466 of file random_graph.H.
Referenced by Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_any_odd_nodes(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_random_odd_nodes(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_through_the_complement(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::make_eulerian(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::prepare_new_graph(), and Aleph::Random_Graph< GT, Init_Node, Init_Arc >::update_parity_after_arc_insertion().