Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Random_Graph< GT, Init_Node, Init_Arc > Class Template Reference

Random undirected graph generator. More...

#include <random_graph.H>

Inheritance diagram for Aleph::Random_Graph< GT, Init_Node, Init_Arc >:
[legend]
Collaboration diagram for Aleph::Random_Graph< GT, Init_Node, Init_Arc >:
[legend]

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
 

Detailed Description

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
class Aleph::Random_Graph< GT, Init_Node, Init_Arc >

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:

  • GT: The graph type to construct (must be undirected).
  • Init_Node: Functor class for initializing node data.
  • Init_Arc: Functor class for initializing arc data.

The Init_Node class must have the following structure:

The Init_Arc class must have the following structure:

struct Init_Arc
{
void operator () (GT & g, GT::Arc * a) { ... }
};

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.

Author
Leandro Rabindranath Leon

Definition at line 459 of file random_graph.H.

Member Typedef Documentation

◆ Base

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
using Aleph::Random_Graph< GT, Init_Node, Init_Arc >::Base = Random_Graph_Base<GT, Init_Node, Init_Arc>
private

Definition at line 464 of file random_graph.H.

◆ GT_Arc

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
typedef GT::Arc Aleph::Random_Graph< GT, Init_Node, Init_Arc >::GT_Arc
private

Definition at line 462 of file random_graph.H.

◆ GT_Node

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
typedef GT::Node Aleph::Random_Graph< GT, Init_Node, Init_Arc >::GT_Node
private

Definition at line 461 of file random_graph.H.

Constructor & Destructor Documentation

◆ Random_Graph()

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
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>)
Aleph::Random_Graph< GT, Init_Node, Init_Arc >::Random_Graph ( unsigned long  seed = time(nullptr),
N &&  __init_node = Init_Node(),
A &&  __init_arc = Init_Arc() 
)
inline

Constructor.

Parameters
[in]seedSeed for the random number generator.
[in]__init_nodeNode initialization functor.
[in]__init_arcArc 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().

Member Function Documentation

◆ all_even()

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
bool Aleph::Random_Graph< GT, Init_Node, Init_Arc >::all_even ( ) const
inlineprivate

◆ balance_graph_nodes_degree()

◆ connect()

◆ create_nodes_and_initialize_arc_index()

◆ create_p()

◆ eulerian() [1/2]

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
GT Aleph::Random_Graph< GT, Init_Node, Init_Arc >::eulerian ( const size_t &  __num_nodes,
const double &  p 
)
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.

Warning
The procedure can be extremely slow and memory-intensive as __num_nodes increases.
Parameters
[in]__num_nodesNumber of nodes the graph should have.
[in]pProbability of an arc existing between any pair of nodes.
Returns
The randomly created graph.
Exceptions
bad_allocif there is not enough memory.
domain_errorif p is not in the range (0, 1].
domain_errorif 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.

◆ eulerian() [2/2]

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
GT Aleph::Random_Graph< GT, Init_Node, Init_Arc >::eulerian ( const size_t &  __num_nodes,
const size_t &  __num_arcs 
)
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.

Parameters
[in]__num_nodesNumber of nodes the graph should have.
[in]__num_arcsNumber of arcs the graph should have.
Returns
The randomly created graph.
Exceptions
bad_allocif there is not enough memory.
domain_errorif 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().

◆ join_any_odd_nodes()

◆ join_arc()

◆ join_random_odd_nodes()

◆ join_through_a_node()

◆ join_through_the_complement()

◆ make_eulerian()

◆ make_hamiltonian()

◆ operator()() [1/2]

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
GT Aleph::Random_Graph< GT, Init_Node, Init_Arc >::operator() ( const size_t &  __num_nodes,
const double &  p,
bool  connected = true 
)
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.

Warning
The procedure can be extremely slow and memory-intensive as __num_nodes increases.
Parameters
[in]__num_nodesNumber of nodes the graph should have.
[in]pProbability of an arc existing between any pair of nodes.
[in]connectedWhether the generated graph should be connected. Defaults to true.
Returns
The randomly created graph.
Exceptions
bad_allocif there is not enough memory.
domain_errorif 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().

◆ operator()() [2/2]

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
GT Aleph::Random_Graph< GT, Init_Node, Init_Arc >::operator() ( const size_t &  __num_nodes,
const size_t &  __num_arcs,
bool  connected = true 
)
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.

Parameters
[in]__num_nodesNumber of nodes the graph should have.
[in]__num_arcsNumber of arcs the graph should have.
[in]connectedWhether the generated graph should be connected. Defaults to true.
Returns
The randomly created graph.
Exceptions
bad_allocif 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().

◆ prepare_new_graph()

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
void Aleph::Random_Graph< GT, Init_Node, Init_Arc >::prepare_new_graph ( )
inlineoverrideprivatevirtual

◆ update_parity_after_arc_insertion()

Member Data Documentation

◆ even_nodes

◆ joined

template<AlephGraph GT, class Init_Node = Dft_Init_Rand_Node<GT>, class Init_Arc = Dft_Init_Rand_Arc<GT>>
DynList<GT_Arc *> Aleph::Random_Graph< GT, Init_Node, Init_Arc >::joined
private

◆ odd_nodes


The documentation for this class was generated from the following file: