58 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
60 auto arc = it.get_curr_ne();
68 auto a =
ait.get_curr_ne();
71 if ((s == src && t == tgt) || (s == tgt && t == src))
84 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
86 auto node = it.get_curr_ne();
100 for (
auto it1 = g.
get_node_it(); it1.has_curr(); it1.next_ne())
102 auto u = it1.get_curr_ne();
103 for (
auto it2 = g.
get_node_it(); it2.has_curr(); it2.next_ne())
105 auto v = it2.get_curr_ne();
113 auto arc =
ait.get_curr_ne();
139 cout <<
"Test: Random_Graph sparse basic creation... ";
143 auto g =
gen(
static_cast<size_t>(10),
static_cast<size_t>(15));
145 assert(g.get_num_nodes() == 10);
146 assert(g.get_num_arcs() <= 15);
149 cout <<
"PASSED (nodes=" << g.get_num_nodes()
150 <<
", arcs=" << g.get_num_arcs() <<
")" <<
endl;
155 cout <<
"Test: Random_Graph sparse connected... ";
159 auto g =
gen(
static_cast<size_t>(20),
static_cast<size_t>(30),
true);
161 assert(g.get_num_nodes() == 20);
170 cout <<
"Test: Random_Graph sparse disconnected... ";
175 const auto g =
gen(
static_cast<size_t>(20),
static_cast<size_t>(5),
false);
177 assert(g.get_num_nodes() == 20);
180 cout <<
"PASSED (nodes=" << g.get_num_nodes()
181 <<
", arcs=" << g.get_num_arcs() <<
")" <<
endl;
186 cout <<
"Test: Random_Graph with probability p... ";
190 auto g =
gen(15, 0.3);
192 assert(g.get_num_nodes() == 15);
198 cout <<
"PASSED (nodes=" << g.get_num_nodes()
199 <<
", arcs=" << g.get_num_arcs() <<
")" <<
endl;
204 cout <<
"Test: Random_Graph with high probability (dense)... ";
208 auto g =
gen(10, 0.8);
210 assert(g.get_num_nodes() == 10);
217 cout <<
"PASSED (arcs=" << g.get_num_arcs()
223 cout <<
"Test: Random_Graph invalid probability... ";
230 }
catch (
const std::domain_error &) {
239 }
catch (
const std::domain_error &) {
248 }
catch (
const std::domain_error &) {
259 cout <<
"Test: Random_Graph Eulerian (sparse)... ";
263 auto g =
gen.eulerian(
size_t(12),
size_t(20));
265 assert(g.get_num_nodes() == 12);
269 cout <<
"PASSED (all degrees even)" <<
endl;
274 cout <<
"Test: Random_Graph Eulerian (probability)... ";
278 auto g =
gen.eulerian(10, 0.4);
280 assert(g.get_num_nodes() == 10);
284 cout <<
"PASSED (all degrees even)" <<
endl;
289 cout <<
"Test: Random_Graph sufficient Hamiltonian... ";
293 auto g =
gen.sufficient_hamiltonian(8, 0.5);
295 assert(g.get_num_nodes() == 8);
299 cout <<
"PASSED (Ore condition satisfied)" <<
endl;
304 cout <<
"Test: Random_Graph deterministic with same seed... ";
309 auto g1 =
gen1(
static_cast<size_t>(10),
static_cast<size_t>(20));
310 auto g2 =
gen2(
static_cast<size_t>(10),
static_cast<size_t>(20));
312 assert(
g1.get_num_nodes() ==
g2.get_num_nodes());
313 assert(
g1.get_num_arcs() ==
g2.get_num_arcs());
315 cout <<
"PASSED (same seed => same graph structure)" <<
endl;
320 cout <<
"Test: Random_Graph different seeds produce different graphs... ";
325 auto g1 =
gen1(
static_cast<size_t>(15),
static_cast<size_t>(30));
326 auto g2 =
gen2(
static_cast<size_t>(15),
static_cast<size_t>(30));
331 cout <<
"PASSED (g1 arcs=" <<
g1.get_num_arcs()
332 <<
", g2 arcs=" <<
g2.get_num_arcs() <<
")" <<
endl;
351 cout <<
"Test: Single node graph... ";
355 auto g =
gen(
static_cast<size_t>(1),
static_cast<size_t>(0));
357 assert(g.get_num_nodes() == 1);
358 assert(g.get_num_arcs() == 0);
365 cout <<
"Test: Zero nodes rejected... ";
371 gen(
static_cast<size_t>(0),
static_cast<size_t>(10));
372 }
catch (
const std::domain_error &) {
383 cout <<
"Test: Two node graph... ";
387 auto g =
gen(
static_cast<size_t>(2),
static_cast<size_t>(1),
true);
389 assert(g.get_num_nodes() == 2);
390 assert(g.get_num_arcs() >= 1);
398 cout <<
"Test: Requesting more arcs than possible... ";
403 auto g =
gen(
static_cast<size_t>(5),
static_cast<size_t>(100));
405 assert(g.get_num_nodes() == 5);
406 assert(g.get_num_arcs() <= 10);
408 cout <<
"PASSED (arcs capped at " << g.get_num_arcs() <<
")" <<
endl;
436 cout <<
"Test: Custom node and arc initializers... ";
443 auto g =
gen(
static_cast<size_t>(5),
static_cast<size_t>(6));
447 for (
auto it = g.get_node_it(); it.has_curr(); it.next_ne())
448 if (
auto node = it.get_curr_ne(); node->get_info() < 0 || node->get_info() >= 5)
456 for (
auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
458 if (
auto arc = it.get_curr_ne(); arc->get_info() < 1)
473 cout <<
"======================================" <<
endl;
474 cout <<
" Random Graph Generator Tests" <<
endl;
475 cout <<
"======================================" <<
endl;
478 cout <<
"--- Random_Graph (undirected) ---" <<
endl;
493 cout <<
"--- Edge Cases ---" <<
endl;
500 cout <<
"--- Custom Initializers ---" <<
endl;
504 cout <<
"======================================" <<
endl;
505 cout <<
" All tests PASSED!" <<
endl;
506 cout <<
"======================================" <<
endl;
Random undirected graph generator.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
constexpr size_t get_num_arcs() const noexcept
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
bool test_connectivity(const GT &g)
Connectivity test for undirected graphs.
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Random graph generation utilities.
void test_random_graph_probability_dense()
void test_custom_initializers()
void test_random_graph_sparse_disconnected()
void test_random_graph_probability()
void test_random_graph_deterministic_seed()
void test_single_node_graph()
void test_random_graph_hamiltonian()
bool all_nodes_have_even_degree(const GT &g)
Verify that all nodes have even degree (necessary for Eulerian)
void test_zero_nodes_rejected()
void test_random_graph_invalid_probability()
bool is_simple_graph(const GT &g)
Verify that a graph has no parallel arcs (simple graph)
void test_random_graph_sparse_basic()
void test_random_graph_sparse_connected()
void test_random_graph_eulerian_sparse()
void test_complete_graph_limit()
bool satisfies_ore_condition(const GT &g)
Verify Ore's theorem condition for Hamiltonian graphs: For all non-adjacent pairs u,...
void test_two_node_graph()
void test_random_graph_different_seeds()
void test_random_graph_eulerian_probability()
Arc of graph implemented with double-linked adjacency lists.
void operator()(Graph &, Graph::Arc *arc)
void operator()(Graph &, Graph::Node *node)
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.