38#include <gtest/gtest.h>
57 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
59 auto arc = it.get_curr_ne();
65 for (
auto ait = g.
get_arc_it(); ait.has_curr(); ait.next_ne())
67 auto a = ait.get_curr_ne();
70 if ((s == src && t == tgt) || (s == tgt && t == src))
83 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
85 auto node = it.get_curr_ne();
99 for (
auto it1 = g.
get_node_it(); it1.has_curr(); it1.next_ne())
101 auto u = it1.get_curr_ne();
102 for (
auto it2 = g.
get_node_it(); it2.has_curr(); it2.next_ne())
104 auto v = it2.get_curr_ne();
109 bool adjacent =
false;
110 for (
auto ait = g.
get_arc_it(u); ait.has_curr(); ait.next_ne())
112 auto arc = ait.get_curr_ne();
124 if (deg_u + deg_v < n)
141 auto g = gen(
static_cast<size_t>(10),
static_cast<size_t>(15));
143 EXPECT_TRUE(g.get_num_nodes() == 10);
144 EXPECT_TRUE(g.get_num_arcs() <= 15);
149TEST(RandomGraph, SparseConnected)
154 auto g = gen(
static_cast<size_t>(20),
static_cast<size_t>(30),
true);
156 EXPECT_TRUE(g.get_num_nodes() == 20);
162TEST(RandomGraph, SparseDisconnected)
168 const auto g = gen(
static_cast<size_t>(20),
static_cast<size_t>(5),
false);
170 EXPECT_TRUE(g.get_num_nodes() == 20);
180 auto g = gen(15, 0.3);
182 EXPECT_TRUE(g.get_num_nodes() == 15);
190TEST(RandomGraph, ProbabilityDense)
195 auto g = gen(10, 0.8);
197 EXPECT_TRUE(g.get_num_nodes() == 10);
201 size_t max_arcs = 10 * 9 / 2;
202 EXPECT_TRUE(g.get_num_arcs() > max_arcs / 2);
206TEST(RandomGraph, InvalidProbability)
211 bool caught_p_zero =
false;
214 }
catch (
const std::domain_error &) {
215 caught_p_zero =
true;
217 EXPECT_TRUE(caught_p_zero);
220 bool caught_p_negative =
false;
223 }
catch (
const std::domain_error &) {
224 caught_p_negative =
true;
226 EXPECT_TRUE(caught_p_negative);
227 (void)caught_p_negative;
229 bool caught_p_over_one =
false;
232 }
catch (
const std::domain_error &) {
233 caught_p_over_one =
true;
235 EXPECT_TRUE(caught_p_over_one);
236 (void)caught_p_over_one;
240TEST(RandomGraph, EulerianSparse)
245 auto g = gen.
eulerian(
size_t(12),
size_t(20));
247 EXPECT_TRUE(g.get_num_nodes() == 12);
253TEST(RandomGraph, EulerianProbability)
260 EXPECT_TRUE(g.get_num_nodes() == 10);
273 EXPECT_TRUE(g.get_num_nodes() == 8);
279TEST(RandomGraph, DeterministicSeed)
285 auto g1 = gen1(
static_cast<size_t>(10),
static_cast<size_t>(20));
286 auto g2 = gen2(
static_cast<size_t>(10),
static_cast<size_t>(20));
288 EXPECT_TRUE(g1.get_num_nodes() == g2.get_num_nodes());
289 EXPECT_TRUE(g1.get_num_arcs() == g2.get_num_arcs());
293TEST(RandomGraph, DifferentSeeds)
299 auto g1 = gen1(
static_cast<size_t>(15),
static_cast<size_t>(30));
300 auto g2 = gen2(
static_cast<size_t>(15),
static_cast<size_t>(30));
304 EXPECT_EQ(g1.get_num_nodes(), 15u);
305 EXPECT_EQ(g2.get_num_nodes(), 15u);
318TEST(RandomGraph, SingleNodeGraph)
323 auto g = gen(
static_cast<size_t>(1),
static_cast<size_t>(0));
325 EXPECT_TRUE(g.get_num_nodes() == 1);
326 EXPECT_TRUE(g.get_num_arcs() == 0);
330TEST(RandomGraph, ZeroNodesRejected)
337 gen(
static_cast<size_t>(0),
static_cast<size_t>(10));
338 }
catch (
const std::domain_error &) {
351 auto g = gen(
static_cast<size_t>(2),
static_cast<size_t>(1),
true);
353 EXPECT_TRUE(g.get_num_nodes() == 2);
354 EXPECT_TRUE(g.get_num_arcs() >= 1);
359TEST(RandomGraph, CompleteGraphLimit)
365 auto g = gen(
static_cast<size_t>(5),
static_cast<size_t>(100));
367 EXPECT_TRUE(g.get_num_nodes() == 5);
368 EXPECT_TRUE(g.get_num_arcs() <= 10);
395TEST(RandomGraph, CustomInitializers)
403 auto g = gen(
static_cast<size_t>(5),
static_cast<size_t>(6));
406 bool nodes_initialized =
true;
407 for (
auto it = g.get_node_it(); it.has_curr(); it.next_ne())
408 if (
auto node = it.get_curr_ne(); node->get_info() < 0 || node->get_info() >= 5)
409 nodes_initialized =
false;
411 EXPECT_TRUE(nodes_initialized);
414 bool arcs_initialized =
true;
415 for (
auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
417 if (
auto arc = it.get_curr_ne(); arc->get_info() < 1)
418 arcs_initialized =
false;
420 EXPECT_TRUE(arcs_initialized);
Random undirected graph generator.
GT eulerian(const size_t &__num_nodes, const size_t &__num_arcs)
Create a random Eulerian graph (sparse version).
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.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
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)
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
Return the number of arcs of the graph.
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.
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.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Random graph generation utilities.
bool all_nodes_have_even_degree(const GT &g)
Verify that all nodes have even degree (necessary for Eulerian)
bool is_simple_graph(const GT &g)
Verify that a graph has no parallel arcs (simple graph)
bool satisfies_ore_condition(const GT &g)
Verify Ore's theorem condition for Hamiltonian graphs: For all non-adjacent pairs u,...
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.