175#include <tclap/CmdLine.h>
183using namespace Aleph;
197 cout <<
"\n" << string(60,
'=') <<
"\n";
198 cout <<
" " << title <<
"\n";
199 cout << string(60,
'=') <<
"\n\n";
204 cout <<
"\n--- " << title <<
" ---\n";
210 cout << label <<
":" << endl;
215 double total_degree = 0;
216 size_t min_deg = numeric_limits<size_t>::max();
219 for (
auto it = g.
get_node_it(); it.has_curr(); it.next())
225 min_deg =
min(min_deg, deg);
226 max_deg =
max(max_deg, deg);
230 cout <<
" Avg degree: " << fixed << setprecision(2) << avg_deg << endl;
231 cout <<
" Min degree: " << min_deg << endl;
232 cout <<
" Max degree: " << max_deg << endl;
243 cout <<
"Generate a graph with n vertices and m random edges.\n";
244 cout <<
"Edges are placed uniformly at random.\n\n";
249 cout <<
"Parameters: n=" << n <<
" vertices, m=" <<
m <<
" edges\n";
265 components(g, subgraphs);
267 cout <<
"Number of connected components: " << subgraphs.
size() << endl;
269 if (subgraphs.
size() == 1)
270 cout <<
"Graph is CONNECTED\n";
273 cout <<
"Graph is DISCONNECTED\n";
274 cout <<
"Component sizes: ";
275 for (
auto it = subgraphs.
get_it(); it.has_curr(); it.next())
276 cout << it.get_curr().get_num_nodes() <<
" ";
281 double density = 2.0 *
m / (n * (n - 1));
282 cout <<
"\nGraph density: " << fixed << setprecision(4) << density << endl;
283 cout <<
"(1.0 = complete graph, 0.0 = no edges)\n";
294 cout <<
"Generate a random graph and then force connectivity (if needed).\n";
295 cout <<
"This demo requests a connected graph from the generator.\n\n";
300 cout <<
"Parameters: n=" << n <<
" vertices, m=" <<
m <<
" edges\n";
301 cout <<
"(Reference threshold for connectivity in G(n,m): ~n*ln(n)/2 = "
302 << (size_t)(n *
log(n) / 2) <<
")\n";
315 components(g, subgraphs);
317 cout <<
"\nConnected components: " << subgraphs.
size() << endl;
318 if (subgraphs.
size() == 1)
319 cout <<
"Graph is CONNECTED (as expected for dense graphs)\n";
321 cout <<
"Graph is disconnected (rare for this density)\n";
332 cout <<
"Generate a random graph where all vertices have even degree.\n";
333 cout <<
"Such a graph has an Eulerian cycle.\n\n";
338 cout <<
"Parameters: n=" << n <<
" vertices, m=" <<
m <<
" edges\n";
351 bool all_even =
true;
352 cout <<
"Vertex degrees: ";
353 for (
auto it = g.
get_node_it(); it.has_curr(); it.next())
359 if (deg % 2 != 0) all_even =
false;
361 cout <<
"(checking...)" << endl;
363 cout <<
"All degrees even? " << (all_even ?
"YES" :
"NO") << endl;
367 cout <<
"Is Eulerian (Test_Eulerian)? " << (
test(g) ?
"YES" :
"NO") << endl;
378 cout <<
"Generate random directed graphs (digraphs).\n\n";
383 cout <<
"Parameters: n=" << n <<
" vertices, m=" <<
m <<
" arcs\n";
391 cout <<
"Digraph statistics:" << endl;
392 cout <<
" Vertices: " << g.get_num_nodes() << endl;
393 cout <<
" Arcs: " << g.get_num_arcs() << endl;
398 size_t total_out = 0;
401 for (
auto it = g.get_node_it(); it.has_curr(); it.next())
406 total_out += out_deg;
407 max_out =
max(max_out, out_deg);
410 cout <<
"Average out-degree: " << fixed << setprecision(2)
411 << (double)total_out / n << endl;
412 cout <<
"Max out-degree: " << max_out << endl;
423 cout <<
"Generate a random digraph where in-degree = out-degree for all.\n";
424 cout <<
"(Has an Eulerian cycle)\n\n";
429 cout <<
"Parameters: n=" << n <<
" vertices, m=" <<
m <<
" arcs\n";
437 cout <<
"Digraph statistics:" << endl;
438 cout <<
" Vertices: " << g.get_num_nodes() << endl;
439 cout <<
" Arcs: " << g.get_num_arcs() << endl;
445 cout <<
"Is Eulerian (Test_Eulerian)? " << (
test(g) ?
"YES" :
"NO") << endl;
456 cout <<
"How does edge count affect connectivity?\n\n";
461 cout <<
"n = " << n <<
" vertices, " << trials <<
" trials each\n\n";
463 cout << setw(10) <<
"Edges" << setw(15) <<
"Density"
464 << setw(20) <<
"Avg Components" << setw(15) <<
"% Connected" << endl;
465 cout << string(60,
'-') << endl;
469 for (
size_t m = n/2;
m <= n*3;
m += n/2)
471 size_t total_components = 0;
472 size_t connected_count = 0;
474 for (
size_t t = 0; t < trials; t++)
482 total_components += comps.
size();
483 if (comps.
size() == 1) connected_count++;
486 double density = 2.0 *
m / (n * (n - 1));
487 double avg_comps = (double)total_components / trials;
488 double pct_connected = 100.0 * connected_count / trials;
490 cout << setw(10) <<
m
491 << setw(15) << fixed << setprecision(3) << density
492 << setw(20) << setprecision(1) << avg_comps
493 << setw(14) << setprecision(0) << pct_connected <<
"%" << endl;
496 cout <<
"\nNote: Connectivity threshold is around m ≈ n*ln(n)/2 ≈ "
497 << (size_t)(n *
log(n) / 2) <<
" edges\n";
504int main(
int argc,
char* argv[])
509 "Random graph generation example for Aleph-w.\n"
510 "Demonstrates various random graph models.",
514 TCLAP::ValueArg<string> sectionArg(
516 "Run only specific section: erdos, connected, eulerian, digraph, "
517 "eulerian_dig, params, or 'all'",
518 false,
"all",
"section",
cmd
521 cmd.parse(argc, argv);
523 string section = sectionArg.getValue();
526 cout <<
"============================================================\n";
527 cout <<
" ALEPH-W RANDOM GRAPH GENERATION EXAMPLE\n";
528 cout <<
"============================================================\n";
548 cout <<
"\n" << string(60,
'=') <<
"\n";
549 cout <<
"Random graph generation demo completed!\n";
550 cout << string(60,
'=') <<
"\n\n";
554 catch (TCLAP::ArgException& e)
556 cerr <<
"Error: " << e.error() <<
" for argument " << e.argId() << endl;
561 cerr <<
"Error: " << e.what() << endl;
WeightedDigraph::Node Node
Generic directed graph (digraph) wrapper template.
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
void next()
Advances the iterator to the next filtered element.
Graph implemented with double-linked adjacency lists.
Random directed graph (digraph) generator.
GT eulerian(const size_t &__num_nodes, const size_t &__num_arcs)
Create a random Eulerian graph (sparse version).
Random undirected graph generator.
GT eulerian(const size_t &__num_nodes, const size_t &__num_arcs)
Create a random Eulerian graph (sparse version).
Tests whether a graph or digraph is Eulerian.
Compute the connected components of a graph.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Eulerian graph detection and path/cycle finding.
__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_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
static void section(const string &title)
Random graph generation utilities.
void print_subsection(const string &title)
void print_section(const string &title)
void demo_eulerian_digraph()
void print_graph_stats(const string &label, G &g)
Arc of graph implemented with double-linked adjacency lists.
Node belonging to a graph implemented with a double linked adjacency list.
Filtered iterator of adjacent arcs of a node.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Graph connectivity and connected components.
Generic graph and digraph implementations.