Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_components_test.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
50#include <gtest/gtest.h>
51#include <tpl_components.H>
52#include <tpl_graph.H>
53#include <tpl_sgraph.H>
54#include <tpl_agraph.H>
55
56using namespace Aleph;
57
58//==============================================================================
59// Graph Type Definitions - All 6 combinations
60//==============================================================================
61
62// List-based graphs (tpl_graph.H)
65
66// Sparse graphs (tpl_sgraph.H)
69
70// Array-based graphs (tpl_agraph.H)
73
74//==============================================================================
75// Test Fixtures
76//==============================================================================
77
78class BuildSubgraphTest : public ::testing::Test
79{
80protected:
84};
85
86class InconnectedComponentsTest : public ::testing::Test
87{
88protected:
91};
92
93//==============================================================================
94// Build_Subgraph Basic Tests
95//==============================================================================
96
98{
99 [[maybe_unused]] auto n1 = g.insert_node(1);
100 g.reset_nodes();
101 g.reset_arcs();
102
103 Build_Subgraph<Graph> builder;
104 builder(g, sg, n1);
105
106 EXPECT_EQ(sg.get_num_nodes(), 1);
107 EXPECT_EQ(sg.get_num_arcs(), 0);
108}
109
110TEST_F(BuildSubgraphTest, TwoNodesConnected)
111{
112 [[maybe_unused]] auto n1 = g.insert_node(1);
113 auto n2 = g.insert_node(2);
114 g.insert_arc(n1, n2);
115 g.reset_nodes();
116 g.reset_arcs();
117
118 Build_Subgraph<Graph> builder;
119 builder(g, sg, n1);
120
121 EXPECT_EQ(sg.get_num_nodes(), 2);
122 EXPECT_EQ(sg.get_num_arcs(), 1);
123}
124
126{
127 [[maybe_unused]] auto n1 = g.insert_node(1);
128 auto n2 = g.insert_node(2);
129 auto n3 = g.insert_node(3);
130 g.insert_arc(n1, n2);
131 g.insert_arc(n2, n3);
132 g.insert_arc(n3, n1);
133 g.reset_nodes();
134 g.reset_arcs();
135
136 Build_Subgraph<Graph> builder;
137 builder(g, sg, n1);
138
139 EXPECT_EQ(sg.get_num_nodes(), 3);
140 EXPECT_EQ(sg.get_num_arcs(), 3);
141}
142
143TEST_F(BuildSubgraphTest, DisconnectedGraph_BuildsOneComponent)
144{
145 // Create two disconnected components
146 [[maybe_unused]] auto n1 = g.insert_node(1);
147 auto n2 = g.insert_node(2);
148 g.insert_arc(n1, n2);
149
150 auto n3 = g.insert_node(3);
151 auto n4 = g.insert_node(4);
152 g.insert_arc(n3, n4);
153
154 g.reset_nodes();
155 g.reset_arcs();
156
157 Build_Subgraph<Graph> builder;
158 builder(g, sg, n1);
159
160 // Should only build the component containing n1
161 EXPECT_EQ(sg.get_num_nodes(), 2);
162 EXPECT_EQ(sg.get_num_arcs(), 1);
163}
164
166{
167 [[maybe_unused]] auto n1 = g.insert_node(10);
168 auto n2 = g.insert_node(20);
169 g.insert_arc(n1, n2);
170 g.reset_nodes();
171 g.reset_arcs();
172
173 Build_Subgraph<Graph> builder;
174 builder(g, sg, n1);
175
176 // Verify mappings
177 auto sg_n1 = mapped_node<Graph>(n1);
178 auto sg_n2 = mapped_node<Graph>(n2);
179
180 EXPECT_NE(sg_n1, nullptr);
181 EXPECT_NE(sg_n2, nullptr);
182 EXPECT_EQ(sg_n1->get_info(), 10);
183 EXPECT_EQ(sg_n2->get_info(), 20);
184}
185
186TEST_F(BuildSubgraphTest, ReturnSubgraph)
187{
188 [[maybe_unused]] auto n1 = g.insert_node(1);
189 auto n2 = g.insert_node(2);
190 auto n3 = g.insert_node(3);
191 g.insert_arc(n1, n2);
192 g.insert_arc(n2, n3);
193 g.reset_nodes();
194 g.reset_arcs();
195
196 Build_Subgraph<Graph> builder;
197 auto result = builder(g, n1);
198
199 EXPECT_EQ(result.get_num_nodes(), 3);
200 EXPECT_EQ(result.get_num_arcs(), 2);
201}
202
204{
205 [[maybe_unused]] auto n1 = g.insert_node(1);
206 auto n2 = g.insert_node(2);
207 auto n3 = g.insert_node(3);
208 g.insert_arc(n1, n2);
209 g.insert_arc(n2, n3);
210 g.reset_nodes();
211 g.reset_arcs();
212
213 DynList<Graph::Node*> node_list;
214 Build_Subgraph<Graph> builder;
215 builder(g, node_list, n1);
216
217 EXPECT_EQ(node_list.size(), 3);
218}
219
220TEST_F(BuildSubgraphTest, IgnoresTheCookiesThatTheGraphHad)
221{
222 auto n1 = g.insert_node(1);
223 auto n2 = g.insert_node(2);
224 auto n3 = g.insert_node(3);
225 auto a12 = g.insert_arc(n1, n2);
226 auto a23 = g.insert_arc(n2, n3);
227 auto a31 = g.insert_arc(n3, n1);
228
229 // cookies toward a node and an arc of a graph that went, as those that
230 // Tarjan leaves toward its blocks; the bits are clear
231 {
232 Graph other;
233 auto o = other.insert_node(0);
234 auto oa = other.insert_arc(o, o);
235 for (auto p : {n1, n2, n3})
236 NODE_COOKIE(p) = o;
237 for (auto a : {a12, a23, a31})
238 ARC_COOKIE(a) = oa;
239 }
240
241 Build_Subgraph<Graph>()(g, sg, n1);
242 ASSERT_EQ(sg.get_num_nodes(), 3);
243 ASSERT_EQ(sg.get_num_arcs(), 3);
244
245 // each node and arc maps to its copy, and the copy to it
246 for (auto p : {n1, n2, n3})
247 {
248 auto q = mapped_node<Graph>(p);
249 ASSERT_NE(q, nullptr);
250 EXPECT_EQ(q->get_info(), p->get_info());
251 EXPECT_EQ(mapped_node<Graph>(q), p);
252 }
253 for (auto a : {a12, a23, a31})
254 {
255 auto b = mapped_arc<Graph>(a);
256 ASSERT_NE(b, nullptr);
257 EXPECT_EQ(mapped_arc<Graph>(b), a);
258 auto s = mapped_node<Graph>(sg.get_src_node(b));
259 auto t = mapped_node<Graph>(sg.get_tgt_node(b));
260 EXPECT_TRUE((s == g.get_src_node(a) and t == g.get_tgt_node(a))
261 or (s == g.get_tgt_node(a) and t == g.get_src_node(a)));
262 }
263}
264
266{
267 [[maybe_unused]] auto n1 = g.insert_node(1);
268
269 Build_Subgraph<Graph> builder;
270
271 EXPECT_THROW(builder(g, sg, nullptr), std::invalid_argument);
272}
273
274//==============================================================================
275// Inconnected_Components Basic Tests
276//==============================================================================
277
279{
280 [[maybe_unused]] auto n1 = g.insert_node(1);
281 auto n2 = g.insert_node(2);
282 auto n3 = g.insert_node(3);
283 g.insert_arc(n1, n2);
284 g.insert_arc(n2, n3);
285
286 DynList<Graph> components;
288 cc(g, components);
289
290 EXPECT_EQ(components.size(), 1);
291 EXPECT_EQ(components.get_first().get_num_nodes(), 3);
292}
293
295{
296 // Component 1
297 [[maybe_unused]] auto n1 = g.insert_node(1);
298 auto n2 = g.insert_node(2);
299 g.insert_arc(n1, n2);
300
301 // Component 2 (disconnected)
302 auto n3 = g.insert_node(3);
303 auto n4 = g.insert_node(4);
304 g.insert_arc(n3, n4);
305
306 DynList<Graph> components;
308 cc(g, components);
309
310 EXPECT_EQ(components.size(), 2);
311
312 // Each component should have 2 nodes
313 size_t total_nodes = 0;
314 for (auto & comp : components)
315 total_nodes += comp.get_num_nodes();
316 EXPECT_EQ(total_nodes, 4);
317}
318
320{
321 // Three isolated nodes = three components
322 g.insert_node(1);
323 g.insert_node(2);
324 g.insert_node(3);
325
326 DynList<Graph> components;
328 cc(g, components);
329
330 EXPECT_EQ(components.size(), 3);
331}
332
334{
335 DynList<Graph> components;
337 cc(g, components);
338
339 EXPECT_EQ(components.size(), 0);
340}
341
343{
344 // Two components
345 [[maybe_unused]] auto n1 = g.insert_node(1);
346 auto n2 = g.insert_node(2);
347 g.insert_arc(n1, n2);
348
349 [[maybe_unused]] auto n3 = g.insert_node(3);
350
353 cc(g, node_lists);
354
355 EXPECT_EQ(node_lists.size(), 2);
356}
357
359{
360 [[maybe_unused]] auto n1 = g.insert_node(1);
361 auto n2 = g.insert_node(2);
362 g.insert_arc(n1, n2);
363
364 g.insert_node(3); // Isolated
365 g.insert_node(4); // Isolated
366
368
369 EXPECT_EQ(cc.count_components(g), 3);
370}
371
373{
374 [[maybe_unused]] auto n1 = g.insert_node(1);
375 auto n2 = g.insert_node(2);
376 auto n3 = g.insert_node(3);
377 g.insert_arc(n1, n2);
378 g.insert_arc(n2, n3);
379
381
382 EXPECT_TRUE(cc.is_connected(g));
383}
384
386{
387 [[maybe_unused]] auto n1 = g.insert_node(1);
388 auto n2 = g.insert_node(2);
389 g.insert_arc(n1, n2);
390
391 g.insert_node(3); // Isolated
392
394
395 EXPECT_FALSE(cc.is_connected(g));
396}
397
398TEST_F(InconnectedComponentsTest, IsConnected_EmptyGraph)
399{
401
402 EXPECT_TRUE(cc.is_connected(g)); // Empty graph is considered connected
403}
404
405TEST_F(InconnectedComponentsTest, IsConnected_SingleNode)
406{
407 g.insert_node(1);
408
410
411 EXPECT_TRUE(cc.is_connected(g));
412}
413
414//==============================================================================
415// Stress Tests
416//==============================================================================
417
419{
420 // Create 50 isolated nodes = 50 components
421 for (int i = 0; i < 50; ++i)
422 g.insert_node(i);
423
425
426 EXPECT_EQ(cc.count_components(g), 50);
427}
428
430{
431 const int N = 100;
433
434 for (int i = 0; i < N; ++i)
435 nodes.append(g.insert_node(i));
436
437 // Create a connected chain
438 for (int i = 0; i < N - 1; ++i)
439 g.insert_arc(nodes(i), nodes(i + 1));
440
442
443 EXPECT_TRUE(cc.is_connected(g));
444 EXPECT_EQ(cc.count_components(g), 1);
445}
446
447//==============================================================================
448// Typed Tests for All Graph Types
449//==============================================================================
450
451template <typename GraphType>
452class ComponentsAllGraphs : public ::testing::Test
453{
454protected:
455 GraphType g;
456};
457
458using GraphTypes = ::testing::Types<
459 ListGraph,
465>;
466
468
469// Undirected graphs only - for connectivity tests
470// (Inconnected_Components is designed for undirected graphs)
471template <typename GraphType>
472class ComponentsUndirectedGraphs : public ::testing::Test
473{
474protected:
475 GraphType g;
476};
477
478using UndirectedGraphTypes = ::testing::Types<
479 ListGraph,
482>;
483
485
486template <typename GraphType>
487class ComponentsDigraphs : public ::testing::Test
488{
489protected:
490 GraphType g;
491};
492
493using DigraphTypes = ::testing::Types<
497>;
498
500
501// Following the arcs that leave each node, a -> b, c -> b with a first gave
502// {a, b} and {c}, and the copy of c -> b, in the subgraph of c, pointed to
503// the image of b in that of a: every operation rejects a digraph
504TYPED_TEST(ComponentsDigraphs, EveryOperationRejectsADigraph)
505{
506 using Graph = TypeParam;
507 Graph & g = this->g;
508 auto * a = g.insert_node(0);
509 auto * b = g.insert_node(1);
510 auto * c = g.insert_node(2);
511 g.insert_arc(a, b, 10);
512 g.insert_arc(c, b, 20);
513
515 DynList<Graph> blocks;
517 EXPECT_THROW(cc(g, blocks), std::domain_error);
518 EXPECT_THROW(cc(g, lists), std::domain_error);
519 EXPECT_THROW((void) cc.count_components(g), std::domain_error);
520 EXPECT_THROW((void) cc.is_connected(g), std::domain_error);
521 EXPECT_TRUE(blocks.is_empty());
522 EXPECT_TRUE(lists.is_empty());
523
524 Graph one;
525 one.insert_node(0);
526 EXPECT_THROW((void) cc.is_connected(one), std::domain_error);
527}
528
529TYPED_TEST(ComponentsAllGraphs, BuildSubgraphSingleNode)
530{
531 using Graph = TypeParam;
532 Graph & g = this->g;
533
534 [[maybe_unused]] auto n1 = g.insert_node(1);
535 g.reset_nodes();
536 g.reset_arcs();
537
538 Graph sg;
539 Build_Subgraph<Graph> builder;
540 builder(g, sg, n1);
541
542 EXPECT_EQ(sg.get_num_nodes(), 1);
543 EXPECT_EQ(sg.get_num_arcs(), 0);
544}
545
546TYPED_TEST(ComponentsAllGraphs, BuildSubgraphTriangle)
547{
548 using Graph = TypeParam;
549 Graph & g = this->g;
550
551 [[maybe_unused]] auto n1 = g.insert_node(1);
552 auto n2 = g.insert_node(2);
553 auto n3 = g.insert_node(3);
554 g.insert_arc(n1, n2);
555 g.insert_arc(n2, n3);
556 g.insert_arc(n3, n1);
557 g.reset_nodes();
558 g.reset_arcs();
559
560 Graph sg;
561 Build_Subgraph<Graph> builder;
562 builder(g, sg, n1);
563
564 EXPECT_EQ(sg.get_num_nodes(), 3);
565 EXPECT_EQ(sg.get_num_arcs(), 3);
566}
567
568TYPED_TEST(ComponentsAllGraphs, BuildSubgraphNullptrThrows)
569{
570 using Graph = TypeParam;
571 Graph & g = this->g;
572
573 [[maybe_unused]] auto n1 = g.insert_node(1);
574
575 Graph sg;
576 Build_Subgraph<Graph> builder;
577
578 EXPECT_THROW(builder(g, sg, nullptr), std::invalid_argument);
579}
580
582{
583 using Graph = TypeParam;
584 Graph & g = this->g;
585
586 [[maybe_unused]] auto n1 = g.insert_node(1);
587 auto n2 = g.insert_node(2);
588 auto n3 = g.insert_node(3);
589 g.insert_arc(n1, n2);
590 g.insert_arc(n2, n3);
591
592 DynList<Graph> components;
594 cc(g, components);
595
596 EXPECT_EQ(components.size(), 1);
597}
598
600{
601 using Graph = TypeParam;
602 Graph & g = this->g;
603
604 [[maybe_unused]] auto n1 = g.insert_node(1);
605 auto n2 = g.insert_node(2);
606 g.insert_arc(n1, n2);
607
608 auto n3 = g.insert_node(3);
609 auto n4 = g.insert_node(4);
610 g.insert_arc(n3, n4);
611
612 DynList<Graph> components;
614 cc(g, components);
615
616 EXPECT_EQ(components.size(), 2);
617}
618
620{
621 using Graph = TypeParam;
622 Graph & g = this->g;
623
624 g.insert_node(1);
625 g.insert_node(2);
626 g.insert_node(3);
627
629
630 EXPECT_EQ(cc.count_components(g), 3);
631}
632
634{
635 using Graph = TypeParam;
636 Graph & g = this->g;
637
638 [[maybe_unused]] auto n1 = g.insert_node(1);
639 auto n2 = g.insert_node(2);
640 g.insert_arc(n1, n2);
641
643
644 EXPECT_TRUE(cc.is_connected(g));
645}
646
648{
649 using Graph = TypeParam;
650 Graph & g = this->g;
651
652 [[maybe_unused]] auto n1 = g.insert_node(1);
653 auto n2 = g.insert_node(2);
654 g.insert_arc(n1, n2);
655
656 g.insert_node(3);
657
659
660 EXPECT_FALSE(cc.is_connected(g));
661}
662
664{
665 using Graph = TypeParam;
666 Graph & g = this->g;
667
669
670 EXPECT_TRUE(cc.is_connected(g));
671}
672
673// Main
674int main(int argc, char **argv)
675{
676 ::testing::InitGoogleTest(&argc, argv);
677 return RUN_ALL_TESTS();
678}
int main()
Build a mapped subgraph from a graph starting at a given node.
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
T & append()
Allocate a new entry to the end of array.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Arc for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:197
constexpr bool is_empty() const noexcept
Definition htlist.H:434
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Graph class implemented with singly-linked adjacency lists.
Definition tpl_sgraph.H:274
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Compute the connected components of a graph.
size_t count_components(const GT &g)
Count the number of connected components in a graph.
bool is_connected(const GT &g)
Check if a graph is connected.
void reset_arcs() const
Reset all the arcs of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:1213
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:1206
#define N
Definition fib.C:294
::testing::Types< ListGraph, SparseGraph, ArrayGraph > UndirectedGraphTypes
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
#define ARC_COOKIE(p)
Return the arc cookie
#define NODE_COOKIE(p)
Return the node cookie
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Array-based graph implementation.
Graph connectivity and connected components.
::testing::Types< ListGraph, ListDigraph, SparseGraph, SparseDigraph, ArrayGraph, ArrayDigraph > GraphTypes
List_Digraph< Graph_Node< int >, Graph_Arc< int > > ListDigraph
::testing::Types< ListDigraph, SparseDigraph, ArrayDigraph > DigraphTypes
TYPED_TEST_SUITE(ComponentsAllGraphs, GraphTypes)
List_Graph< Graph_Node< int >, Graph_Arc< int > > ListGraph
Array_Graph< Graph_Anode< int >, Graph_Aarc< int > > ArrayGraph
TYPED_TEST(ComponentsDigraphs, EveryOperationRejectsADigraph)
List_SDigraph< Graph_Snode< int >, Graph_Sarc< int > > SparseDigraph
TEST_F(BuildSubgraphTest, SingleNode)
List_SGraph< Graph_Snode< int >, Graph_Sarc< int > > SparseGraph
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.