38#include <gtest/gtest.h>
40#include <initializer_list>
56 for (
int i = 0; i < n; ++i)
64 for (
auto it = list.
get_it(); it.has_curr(); it.next_ne())
65 s.
insert(it.get_curr()->get_info());
72 const auto got = cut_infos(list);
81 size_t count_nodes_with_color(
const Graph & g,
long color)
84 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
86 auto p = it.get_curr();
87 if (get_color<Graph>(p) ==
color)
93 size_t count_arcs_with_color(
const Graph & g,
long color)
96 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
98 auto a = it.get_curr();
99 if (get_color<Graph>(a) ==
color)
105 size_t count_cut_arcs(
const Graph & g)
108 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
109 if (is_an_cut_arc<Graph>(it.get_curr()))
116TEST(CutNodes, PathGraphHasInternalCutNodes)
119 auto nodes = make_nodes(g, 4);
128 EXPECT_TRUE(cut_infos_eq(cuts, {1, 2}));
131TEST(CutNodes, CycleGraphHasNoCutNodes)
134 auto nodes = make_nodes(g, 4);
147TEST(CutNodes, StarGraphHasCenterAsCutNode)
150 auto nodes = make_nodes(g, 6);
151 const int center = 0;
152 for (
int i = 1; i < 6; ++i)
157 alg(
nodes[center], cuts);
159 EXPECT_TRUE(cut_infos_eq(cuts, {0}));
162TEST(CutNodes, PaintSubgraphsRequiresCutNodesComputed)
165 auto nodes = make_nodes(g, 2);
174TEST(CutNodes, PaintSubgraphsAndMapCutGraphOnStar)
177 auto nodes = make_nodes(g, 5);
178 for (
int i = 1; i < 5; ++i)
186 EXPECT_EQ(next_color, 5L);
188 EXPECT_TRUE(is_a_cut_node<Graph>(
nodes[0]));
189 EXPECT_EQ(get_color<Graph>(
nodes[0]), 0L);
190 for (
int i = 1; i < 5; ++i)
191 EXPECT_GT(get_color<Graph>(
nodes[i]), 0L);
201 for (
auto it = cross.
get_it(); it.has_curr(); it.next_ne())
202 EXPECT_TRUE(is_a_cross_arc<Graph>(it.get_curr()));
205TEST(CutNodes, MapSubgraphMatchesColorCounts)
208 auto nodes = make_nodes(g, 5);
209 for (
int i = 1; i < 5; ++i)
228TEST(CutNodes, MapCutGraphMatchesCutArcCountOnPath)
231 auto nodes = make_nodes(g, 4);
253TEST(CutNodes, CompleteGraphHasNoCutNodes)
256 auto nodes = make_nodes(g, 4);
258 for (
int i = 0; i < 4; ++i)
259 for (
int j = i + 1; j < 4; ++j)
269TEST(CutNodes, TreeAllInternalNodesAreCutNodes)
272 auto nodes = make_nodes(g, 7);
291 EXPECT_TRUE(cut_infos_eq(cuts, {0, 1, 2}));
294TEST(CutNodes, BridgeGraphHasMultipleCutNodes)
297 auto nodes = make_nodes(g, 6);
311 EXPECT_TRUE(cut_infos_eq(cuts, {2, 3}));
314TEST(CutNodes, BiconnectedGraphHasNoCutNodes)
317 auto nodes = make_nodes(g, 4);
331TEST(CutNodes, ArticulationAtRootNode)
334 auto nodes = make_nodes(g, 5);
347 const auto infos = cut_infos(cuts);
348 EXPECT_TRUE(infos.exist(0));
349 EXPECT_TRUE(infos.exist(3));
357TEST(CutNodes, MapSubgraphWithoutPaintingThrows)
360 auto nodes = make_nodes(g, 3);
368 EXPECT_THROW(alg.
map_subgraph(sg, 1L), std::logic_error);
371TEST(CutNodes, MapCutGraphWithoutPaintingThrows)
374 auto nodes = make_nodes(g, 3);
383 EXPECT_THROW(alg.
map_cut_graph(cg, cross), std::logic_error);
386TEST(CutNodes, CorrectSequenceCutNodesPaintMap)
389 auto nodes = make_nodes(g, 3);
402 EXPECT_GT(num_colors, 1L);
409TEST(CutNodes, MultipleCutNodesCallsReinitialize)
412 auto nodes = make_nodes(g, 3);
419 alg(
nodes[0], cuts1);
420 size_t first_count = cuts1.
size();
423 alg(
nodes[0], cuts2);
425 EXPECT_EQ(cuts1.
size(), cuts2.
size());
426 EXPECT_EQ(first_count, cuts2.
size());
433TEST(CutNodes, OperatorWithoutStartUsesFirstNode)
436 auto nodes = make_nodes(g, 4);
445 EXPECT_TRUE(cut_infos_eq(cuts, {1, 2}));
448TEST(CutNodes, OperatorWithSpecificStart)
451 auto nodes = make_nodes(g, 4);
460 EXPECT_TRUE(cut_infos_eq(cuts, {1, 2}));
467TEST(CutNodes, PaintAssignsCorrectColors)
470 auto nodes = make_nodes(g, 5);
472 for (
int i = 1; i < 5; ++i)
480 EXPECT_GT(num_colors, 0);
483 EXPECT_EQ(get_color<Graph>(
nodes[0]), 0L);
487 for (
int i = 1; i < 5; ++i)
489 long c = get_color<Graph>(
nodes[i]);
493 EXPECT_EQ(leaf_colors.
size(), 4U);
496TEST(CutNodes, CrossArcsIdentifiedCorrectly)
499 auto nodes = make_nodes(g, 3);
509 size_t cross_count = 0;
510 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
511 if (is_a_cross_arc<Graph>(it.get_curr()))
517TEST(CutNodes, CutArcsIdentifiedInBridge)
520 auto nodes = make_nodes(g, 6);
535 EXPECT_TRUE(is_an_cut_arc<Graph>(bridge));
538TEST(CutNodes, NumberOfColorsMatchesComponents)
541 auto nodes = make_nodes(g, 7);
557 EXPECT_EQ(num_colors, 5L);
564TEST(CutNodes, MapSubgraphWithInvalidColorThrows)
567 auto nodes = make_nodes(g, 3);
576 EXPECT_THROW(alg.
map_subgraph(sg, 999L), std::domain_error);
579TEST(CutNodes, MappedSubgraphIsCorrect)
582 auto nodes = make_nodes(g, 5);
583 for (
int i = 1; i < 5; ++i)
602TEST(CutNodes, BidirectionalMappingEstablished)
605 auto nodes = make_nodes(g, 3);
618 for (
auto it = sg.
get_node_it(); it.has_curr(); it.next_ne())
620 auto sg_node = it.get_curr();
621 auto orig_node = mapped_node<Graph>(sg_node);
622 EXPECT_NE(orig_node,
nullptr);
630TEST(CutNodes, CutGraphContainsOnlyCutNodes)
633 auto nodes = make_nodes(g, 5);
651TEST(CutNodes, CrossArcsListMatchesGraph)
654 auto nodes = make_nodes(g, 3);
668 size_t expected_cross = 0;
669 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
670 if (is_a_cross_arc<Graph>(it.get_curr()))
673 EXPECT_EQ(cross.
size(), expected_cross);
680TEST(CutNodes, ComputeBlocksWithoutCutNodesThrows)
683 auto nodes = make_nodes(g, 2);
692 EXPECT_THROW(alg.
compute_blocks(blocks, cg, cross), std::logic_error);
695TEST(CutNodes, ComputeBlocksAutoPaints)
698 auto nodes = make_nodes(g, 5);
699 for (
int i = 1; i < 5; ++i)
712 EXPECT_EQ(blocks.
size(), 4U);
715TEST(CutNodes, ComputeBlocksGeneratesAllBlocks)
718 auto nodes = make_nodes(g, 7);
735 EXPECT_EQ(blocks.
size(), 4U);
738TEST(CutNodes, ComputeBlocksGeneratesCutGraph)
741 auto nodes = make_nodes(g, 5);
759TEST(CutNodes, ComputeBlocksGeneratesCrossArcList)
762 auto nodes = make_nodes(g, 3);
780TEST(CutNodes, ComputeBlocksOfAGraphWithoutCutNodesGivesTheGraph)
783 auto nodes = make_nodes(g, 3);
798 ASSERT_EQ(blocks.
size(), 1U);
800 EXPECT_EQ(block.get_num_nodes(), 3U);
801 EXPECT_EQ(block.get_num_arcs(), 3U);
802 for (Graph::Node_Iterator it(block); it.has_curr(); it.next_ne())
813TEST(CutNodes, ComputeBlocksLeavesOutTheComponentsNotReached)
816 auto nodes = make_nodes(g, 6);
826 ASSERT_EQ(cuts.
size(), 1U);
833 ASSERT_EQ(blocks.
size(), 2U);
834 size_t block_nodes = 0;
835 for (
auto it = blocks.
get_it(); it.has_curr(); it.next_ne())
836 block_nodes += it.get_curr().get_num_nodes();
837 EXPECT_EQ(block_nodes, 3U);
839 EXPECT_EQ(cross.
size(), 3U);
846TEST(CutNodes, SingleNodeHasNoCutNodes)
849 auto nodes = make_nodes(g, 1);
858TEST(CutNodes, TwoNodesWithArcNoCutNodes)
861 auto nodes = make_nodes(g, 2);
871TEST(CutNodes, TriangleNoCutNodes)
874 auto nodes = make_nodes(g, 3);
886TEST(CutNodes, SelfLoopDoesNotAffectCutNodes)
889 auto nodes = make_nodes(g, 3);
898 EXPECT_TRUE(cut_infos_eq(cuts, {1}));
901TEST(CutNodes, ParallelArcsDoNotAffectCutNodes)
904 auto nodes = make_nodes(g, 3);
913 EXPECT_TRUE(cut_infos_eq(cuts, {1}));
920TEST(CutNodes, CompleteWorkflowDetectPaintMapAll)
923 auto nodes = make_nodes(g, 5);
924 for (
int i = 1; i < 5; ++i)
936 EXPECT_GT(num_colors, 1L);
951TEST(CutNodes, MultipleIterationsOnSameGraph)
954 auto nodes = make_nodes(g, 4);
961 for (
int iter = 0; iter < 3; ++iter)
965 EXPECT_TRUE(cut_infos_eq(cuts, {1, 2}));
969TEST(CutNodes, LargeGraphStressTest)
973 auto nodes = make_nodes(g,
N);
976 for (
int i = 0; i <
N - 1; ++i)
983 EXPECT_EQ(cuts.
size(),
static_cast<size_t>(
N - 2));
986TEST(CutNodes, ExceptionDuringMapSubgraphClearsGraph)
989 auto nodes = make_nodes(g, 3);
1003 catch (
const std::domain_error &)
1021 for (
auto it = lst.
get_it(); it.has_curr(); it.next_ne())
1027TEST(Bridges, PathGraphAllEdgesAreBridges)
1031 auto nodes = make_nodes(g, 4);
1039 auto bs = bridge_set(bridges);
1040 EXPECT_EQ(bs.size(), 3U);
1041 EXPECT_TRUE(bs.exist(a01));
1042 EXPECT_TRUE(bs.exist(a12));
1043 EXPECT_TRUE(bs.exist(a23));
1050 auto nodes = make_nodes(g, 4);
1057 EXPECT_TRUE(bridges.is_empty());
1060TEST(Bridges, TwoTrianglesOneBridge)
1064 auto nodes = make_nodes(g, 6);
1074 EXPECT_EQ(bridges.size(), 1U);
1075 EXPECT_TRUE(bridge_set(bridges).exist(bridge));
1078TEST(Bridges, TreeAllEdgesAreBridges)
1088 auto nodes = make_nodes(g, 5);
1095 auto bs = bridge_set(bridges);
1096 EXPECT_EQ(bs.size(), 4U);
1097 EXPECT_TRUE(bs.exist(a01));
1098 EXPECT_TRUE(bs.exist(a02));
1099 EXPECT_TRUE(bs.exist(a13));
1100 EXPECT_TRUE(bs.exist(a14));
1106 auto nodes = make_nodes(g, 4);
1107 for (
int i = 0; i < 4; ++i)
1108 for (
int j = i + 1; j < 4; ++j)
1112 EXPECT_TRUE(bridges.is_empty());
1118 auto nodes = make_nodes(g, 1);
1120 EXPECT_TRUE(bridges.is_empty());
1126 auto nodes = make_nodes(g, 2);
1130 EXPECT_EQ(bridges.size(), 1U);
1131 EXPECT_TRUE(bridge_set(bridges).exist(b));
1134TEST(Bridges, ParallelArcsNotBridges)
1138 auto nodes = make_nodes(g, 2);
1143 EXPECT_TRUE(bridges.is_empty());
1146TEST(Bridges, FreeFunctionConsistentWithClass)
1150 auto nodes = make_nodes(g, 4);
1160 EXPECT_EQ(b_class.size(), b_free.size());
1161 EXPECT_EQ(b_class.size(), 1U);
1162 EXPECT_TRUE(bridge_set(b_class).exist(pend));
1165TEST(Bridges, OperatorCallConsistentWithMethod)
1168 auto nodes = make_nodes(g, 4);
1177 EXPECT_EQ(b1.size(), b2.size());
1178 EXPECT_EQ(b1.size(), 3U);
1181TEST(Bridges, MultipleCallsSameResult)
1185 auto nodes = make_nodes(g, 6);
1198 EXPECT_EQ(b1.size(), 1U);
1199 EXPECT_EQ(b2.size(), 1U);
1207 auto nodes = make_nodes(g,
N);
1208 for (
int i = 0; i <
N - 1; ++i)
1212 EXPECT_EQ(bridges.size(),
static_cast<size_t>(
N - 1));
1215TEST(Bridges, ConsistentWithCutArcOnPathGraph)
1219 auto nodes = make_nodes(g, 4);
1226 EXPECT_EQ(bridges.size(), 3U);
1231 alg(
nodes[0], cuts);
1232 EXPECT_EQ(cuts.
size(), 2U);
1235TEST(Bridges, StarGraphAllEdgesBridges)
1239 auto nodes = make_nodes(g, 5);
1241 for (
int i = 1; i < 5; ++i)
1245 auto bs = bridge_set(bridges);
1246 EXPECT_EQ(bs.size(), 4U);
1247 arcs.for_each([&](
Graph::Arc * a) { EXPECT_TRUE(bs.exist(a)); });
1250TEST(Bridges, SelfLoopDoesNotAffectBridges)
1254 auto nodes = make_nodes(g, 3);
1260 auto bs = bridge_set(bridges);
1261 EXPECT_EQ(bs.size(), 2U);
1262 EXPECT_TRUE(bs.exist(a01));
1263 EXPECT_TRUE(bs.exist(a12));
1266TEST(Bridges, ResultIsIndependentOfStartNode)
1271 auto nodes = make_nodes(g, 6);
1280 for (
int start = 0; start < 6; ++start)
1283 auto bs = bridge_set(bridges);
1284 EXPECT_EQ(bs.size(), 1U) <<
"start=" << start;
1285 EXPECT_TRUE(bs.exist(bridge)) <<
"start=" << start;
1289TEST(Bridges, NonBridgeEdgesAbsentFromList)
1294 auto nodes = make_nodes(g, 4);
1301 auto bs = bridge_set(bridges);
1303 EXPECT_EQ(bs.size(), 1U);
1304 EXPECT_TRUE(bs.exist(pend));
1305 EXPECT_FALSE(bs.exist(t01));
1306 EXPECT_FALSE(bs.exist(t12));
1307 EXPECT_FALSE(bs.exist(t20));
1310TEST(Bridges, EmptyGraphNoArgReturnsEmpty)
1317TEST(Bridges, DisconnectedGraphBridgesFromAllComponents)
1323 auto nodes = make_nodes(g, 6);
1331 auto bs = bridge_set(bridges);
1332 EXPECT_EQ(bs.size(), 4U);
1333 EXPECT_TRUE(bs.exist(a01));
1334 EXPECT_TRUE(bs.exist(a12));
1335 EXPECT_TRUE(bs.exist(a34));
1336 EXPECT_TRUE(bs.exist(a45));
Simple dynamic array with automatic resizing and functional operations.
static Array create(const size_t n)
Create an array with n logical elements.
Find bridge edges (isthmuses) of a connected undirected graph.
DynList< typename GT::Arc * > find_bridges(typename GT::Node *start)
Find all bridge edges in the graph, starting from start.
Computation of cut nodes (articulation points) of a graph.
void map_cut_graph(GT &cut_graph, DynDlist< typename GT::Arc * > &cross_arc_list)
Computes the mapped cut graph of a graph.
void map_subgraph(GT &sg, typename GT::Node *gsrc, const long &color)
void compute_blocks(DynDlist< GT > &block_list, GT &cut_graph, DynDlist< typename GT::Arc * > &cross_arc_list)
Build mapped graphs around cut nodes.
long paint_subgraphs()
Paints the connected components around the cut nodes.
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
T & get_first()
Return a modifiable reference to first item in the list.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Dynamic set backed by balanced binary search trees with automatic memory management.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Number Theoretic Transform over Z / MOD Z.
Minimal std::expected-style result type for C++20.
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.
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.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
#define NODE_COOKIE(p)
Return the node cookie
DynList< typename GT::Arc * > find_bridges(const GT &g, typename GT::Node *start, SA sa=SA())
Find all bridge edges in a connected undirected graph.
Main namespace for Aleph-w library functions.
static long & color(typename GT::Node *p)
Arc of graph implemented with double-linked adjacency lists.
Dynamic array container with automatic resizing.
Articulation points (cut nodes), bridges, and biconnected components.
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.