Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
random_graph.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33
38#include <gtest/gtest.h>
39#include <random_graph.H>
40#include <tpl_graph.H>
41#include <tpl_graph_utils.H>
42
43using namespace std;
44using namespace Aleph;
45
46// Simple graph type for testing (undirected)
48
49// ============================================================================
50// Test helper functions
51// ============================================================================
52
54template <class GT>
55bool is_simple_graph(const GT & g)
56{
57 for (auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
58 {
59 auto arc = it.get_curr_ne();
60 auto src = g.get_src_node(arc);
61 auto tgt = g.get_tgt_node(arc);
62
63 // Count arcs between src and tgt
64 size_t count = 0;
65 for (auto ait = g.get_arc_it(); ait.has_curr(); ait.next_ne())
66 {
67 auto a = ait.get_curr_ne();
68 auto s = g.get_src_node(a);
69 auto t = g.get_tgt_node(a);
70 if ((s == src && t == tgt) || (s == tgt && t == src))
71 ++count;
72 }
73 if (count > 1)
74 return false;
75 }
76 return true;
77}
78
80template <class GT>
82{
83 for (auto it = g.get_node_it(); it.has_curr(); it.next_ne())
84 {
85 auto node = it.get_curr_ne();
86 if (g.get_num_arcs(node) % 2 != 0)
87 return false;
88 }
89 return true;
90}
91
94template <class GT>
96{
97 const size_t n = g.get_num_nodes();
98
99 for (auto it1 = g.get_node_it(); it1.has_curr(); it1.next_ne())
100 {
101 auto u = it1.get_curr_ne();
102 for (auto it2 = g.get_node_it(); it2.has_curr(); it2.next_ne())
103 {
104 auto v = it2.get_curr_ne();
105 if (u == v)
106 continue;
107
108 // Check if u and v are adjacent
109 bool adjacent = false;
110 for (auto ait = g.get_arc_it(u); ait.has_curr(); ait.next_ne())
111 {
112 auto arc = ait.get_curr_ne();
113 if (g.get_connected_node(arc, u) == v)
114 {
115 adjacent = true;
116 break;
117 }
118 }
119
120 if (!adjacent)
121 {
122 size_t deg_u = g.get_num_arcs(u);
123 size_t deg_v = g.get_num_arcs(v);
124 if (deg_u + deg_v < n)
125 return false;
126 }
127 }
128 }
129 return true;
130}
131
132// ============================================================================
133// Random_Graph tests (undirected graphs)
134// ============================================================================
135
136TEST(RandomGraph, SparseBasic)
137{
138
139 Random_Graph<Graph> gen(42);
140
141 auto g = gen(static_cast<size_t>(10), static_cast<size_t>(15));
142
143 EXPECT_TRUE(g.get_num_nodes() == 10);
144 EXPECT_TRUE(g.get_num_arcs() <= 15); // May be less due to duplicate avoidance
145 EXPECT_TRUE(is_simple_graph(g));
146
147}
148
149TEST(RandomGraph, SparseConnected)
150{
151
152 Random_Graph<Graph> gen(123);
153
154 auto g = gen(static_cast<size_t>(20), static_cast<size_t>(30), true);
155
156 EXPECT_TRUE(g.get_num_nodes() == 20);
157 EXPECT_TRUE(test_connectivity(g));
158 EXPECT_TRUE(is_simple_graph(g));
159
160}
161
162TEST(RandomGraph, SparseDisconnected)
163{
164
165 Random_Graph<Graph> gen(456);
166
167 // With few arcs and no connectivity requirement, may be disconnected
168 const auto g = gen(static_cast<size_t>(20), static_cast<size_t>(5), false);
169
170 EXPECT_TRUE(g.get_num_nodes() == 20);
171 EXPECT_TRUE(is_simple_graph(g));
172
173}
174
175TEST(RandomGraph, Probability)
176{
177
178 Random_Graph<Graph> gen(789);
179
180 auto g = gen(15, 0.3);
181
182 EXPECT_TRUE(g.get_num_nodes() == 15);
183 EXPECT_TRUE(is_simple_graph(g));
184
185 // With p=0.3 we expect roughly 0.3 * (15*14/2) = 31.5 arcs on average
186 // but due to randomness it can vary significantly
187
188}
189
190TEST(RandomGraph, ProbabilityDense)
191{
192
193 Random_Graph<Graph> gen(101);
194
195 auto g = gen(10, 0.8);
196
197 EXPECT_TRUE(g.get_num_nodes() == 10);
198 EXPECT_TRUE(is_simple_graph(g));
199
200 // With p=0.8 we expect a fairly dense graph
201 size_t max_arcs = 10 * 9 / 2; // 45
202 EXPECT_TRUE(g.get_num_arcs() > max_arcs / 2); // Should have more than half
203
204}
205
206TEST(RandomGraph, InvalidProbability)
207{
208
209 Random_Graph<Graph> gen(999);
210
211 bool caught_p_zero = false;
212 try {
213 gen(10, 0.0);
214 } catch (const std::domain_error &) {
215 caught_p_zero = true;
216 }
217 EXPECT_TRUE(caught_p_zero);
218 (void)caught_p_zero;
219
220 bool caught_p_negative = false;
221 try {
222 gen(10, -0.5);
223 } catch (const std::domain_error &) {
224 caught_p_negative = true;
225 }
226 EXPECT_TRUE(caught_p_negative);
227 (void)caught_p_negative;
228
229 bool caught_p_over_one = false;
230 try {
231 gen(10, 1.5);
232 } catch (const std::domain_error &) {
233 caught_p_over_one = true;
234 }
235 EXPECT_TRUE(caught_p_over_one);
236 (void)caught_p_over_one;
237
238}
239
240TEST(RandomGraph, EulerianSparse)
241{
242
243 Random_Graph<Graph> gen(2024);
244
245 auto g = gen.eulerian(size_t(12), size_t(20));
246
247 EXPECT_TRUE(g.get_num_nodes() == 12);
248 EXPECT_TRUE(test_connectivity(g));
249 EXPECT_TRUE(all_nodes_have_even_degree(g));
250
251}
252
253TEST(RandomGraph, EulerianProbability)
254{
255
256 Random_Graph<Graph> gen(2025);
257
258 auto g = gen.eulerian(10, 0.4);
259
260 EXPECT_TRUE(g.get_num_nodes() == 10);
261 EXPECT_TRUE(test_connectivity(g));
262 EXPECT_TRUE(all_nodes_have_even_degree(g));
263
264}
265
266TEST(RandomGraph, Hamiltonian)
267{
268
269 Random_Graph<Graph> gen(3000);
270
271 auto g = gen.sufficient_hamiltonian(8, 0.5);
272
273 EXPECT_TRUE(g.get_num_nodes() == 8);
274 EXPECT_TRUE(test_connectivity(g));
275 EXPECT_TRUE(satisfies_ore_condition(g));
276
277}
278
279TEST(RandomGraph, DeterministicSeed)
280{
281
282 Random_Graph<Graph> gen1(12345);
283 Random_Graph<Graph> gen2(12345);
284
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));
287
288 EXPECT_TRUE(g1.get_num_nodes() == g2.get_num_nodes());
289 EXPECT_TRUE(g1.get_num_arcs() == g2.get_num_arcs());
290
291}
292
293TEST(RandomGraph, DifferentSeeds)
294{
295
296 Random_Graph<Graph> gen1(11111);
297 Random_Graph<Graph> gen2(22222);
298
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));
301
302 // With different seeds, graphs should (almost certainly) differ
303 // We can't guarantee they differ, but it's extremely unlikely they're identical
304 EXPECT_EQ(g1.get_num_nodes(), 15u);
305 EXPECT_EQ(g2.get_num_nodes(), 15u);
306 EXPECT_TRUE(is_simple_graph(g1));
307 EXPECT_TRUE(is_simple_graph(g2));
308
309}
310
311// Random_Digraph, and Random_Graph on every kind of graph, are checked by
312// graph_exhaustive_generators_test.cc
313
314// ============================================================================
315// Edge case tests
316// ============================================================================
317
318TEST(RandomGraph, SingleNodeGraph)
319{
320
321 Random_Graph<Graph> gen(1);
322
323 auto g = gen(static_cast<size_t>(1), static_cast<size_t>(0));
324
325 EXPECT_TRUE(g.get_num_nodes() == 1);
326 EXPECT_TRUE(g.get_num_arcs() == 0);
327
328}
329
330TEST(RandomGraph, ZeroNodesRejected)
331{
332
333 Random_Graph<Graph> gen(42);
334
335 bool caught = false;
336 try {
337 gen(static_cast<size_t>(0), static_cast<size_t>(10));
338 } catch (const std::domain_error &) {
339 caught = true;
340 }
341 EXPECT_TRUE(caught);
342 (void)caught;
343
344}
345
346TEST(RandomGraph, TwoNodeGraph)
347{
348
349 Random_Graph<Graph> gen(2);
350
351 auto g = gen(static_cast<size_t>(2), static_cast<size_t>(1), true);
352
353 EXPECT_TRUE(g.get_num_nodes() == 2);
354 EXPECT_TRUE(g.get_num_arcs() >= 1); // At least 1 arc to be connected
355 EXPECT_TRUE(test_connectivity(g));
356
357}
358
359TEST(RandomGraph, CompleteGraphLimit)
360{
361
362 Random_Graph<Graph> gen(100);
363
364 // For 5 nodes, max arcs = 5*4/2 = 10
365 auto g = gen(static_cast<size_t>(5), static_cast<size_t>(100)); // Request 100, but can only have 10
366
367 EXPECT_TRUE(g.get_num_nodes() == 5);
368 EXPECT_TRUE(g.get_num_arcs() <= 10);
369
370}
371
372
373// ============================================================================
374// Custom initializer tests
375// ============================================================================
376
378{
379 int counter = 0;
380 void operator()(Graph & /* g */, Graph::Node * node)
381 {
382 node->get_info() = counter++;
383 }
384};
385
387{
388 int weight = 1;
389 void operator()(Graph & /* g */, Graph::Arc * arc)
390 {
391 arc->get_info() = weight++;
392 }
393};
394
395TEST(RandomGraph, CustomInitializers)
396{
397
398 NodeInitializer node_init;
399 ArcInitializer arc_init;
400
401 Random_Graph<Graph, NodeInitializer, ArcInitializer> gen(5555, node_init, arc_init);
402
403 auto g = gen(static_cast<size_t>(5), static_cast<size_t>(6));
404
405 // Verify nodes were initialized with sequential values
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;
410
411 EXPECT_TRUE(nodes_initialized);
412
413 // Verify arcs were initialized with sequential weights
414 bool arcs_initialized = true;
415 for (auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
416 {
417 if (auto arc = it.get_curr_ne(); arc->get_info() < 1)
418 arcs_initialized = false;
419 }
420 EXPECT_TRUE(arcs_initialized);
421
422}
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.
Definition graph-dry.H:854
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:3144
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:1058
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:3122
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
#define TEST(name)
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.
Definition ahAlgo.H:127
STL namespace.
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.
Definition tpl_graph.H:231
void operator()(Graph &, Graph::Arc *arc)
void operator()(Graph &, Graph::Node *node)
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.