Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_cookie_mapping_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
46#include <gtest/gtest.h>
47
48#include <vector>
49
50#include <Bellman_Ford.H>
51#include <Dominators.H>
52#include <Tarjan.H>
53#include <kosaraju.H>
54#include <tpl_cut_nodes.H>
55#include <tpl_graph.H>
56#include <tpl_graph_utils.H>
57#include <tpl_spanning_tree.H>
58
59using namespace Aleph;
60
61namespace
62{
63 using Mapped_Graph = List_Graph<Graph_Node<int>, Graph_Arc<int>>;
64 using Mapped_Digraph = List_Digraph<Graph_Node<int>, Graph_Arc<int>>;
65
66 // Whether p is one of the nodes of g
67 template <class GT>
68 bool has_node(const GT & g, typename GT::Node * p)
69 {
70 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
71 if (it.get_curr() == p)
72 return true;
73 return false;
74 }
75
76 // Whether a is one of the arcs of g
77 template <class GT>
78 bool has_arc(const GT & g, typename GT::Arc * a)
79 {
80 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
81 if (it.get_curr() == a)
82 return true;
83 return false;
84 }
85
86 // Every node and arc of c maps to one of g that maps back to it; the
87 // cookie of what the copy maps to is read only if it is in g
88 template <class GT>
89 void expect_mapped(const GT & g, const GT & c)
90 {
91 for (typename GT::Node_Iterator it(c); it.has_curr(); it.next_ne())
92 {
93 auto q = it.get_curr();
94 auto p = static_cast<typename GT::Node *>(NODE_COOKIE(q));
95 ASSERT_TRUE(has_node(g, p)) << "node " << q->get_info();
96 EXPECT_EQ(NODE_COOKIE(p), q) << "node " << q->get_info();
97 }
98 for (typename GT::Arc_Iterator it(c); it.has_curr(); it.next_ne())
99 {
100 auto b = it.get_curr();
101 auto a = static_cast<typename GT::Arc *>(ARC_COOKIE(b));
102 ASSERT_TRUE(has_arc(g, a)) << "arc " << b->get_info();
103 EXPECT_EQ(ARC_COOKIE(a), b) << "arc " << b->get_info();
104 }
105 }
106
107 // A cycle 0..5, a tail 5-6-7 and a triangle 7-8-9; the cut nodes are 5,
108 // 6 and 7
109 template <class GT>
110 std::vector<typename GT::Node *> build(GT & g)
111 {
112 std::vector<typename GT::Node *> v;
113 for (int i = 0; i < 10; ++i)
114 v.push_back(g.insert_node(i));
115 int k = 0;
116 for (int i = 0; i < 6; ++i)
117 g.insert_arc(v[i], v[(i + 1) % 6], k++);
118 g.insert_arc(v[5], v[6], k++);
119 g.insert_arc(v[6], v[7], k++);
120 g.insert_arc(v[7], v[8], k++);
121 g.insert_arc(v[8], v[9], k++);
122 g.insert_arc(v[9], v[7], k++);
123 return v;
124 }
125}
126
127// The cookies of the nodes held their low values when Compute_Cut_Nodes
128// copied them, so that the copies mapped to those values
129TEST(CookieMapping, ComputeCutNodesMapsItsCopies)
130{
131 Mapped_Graph g;
132 auto v = build(g);
135 cc(v[0], cuts);
136 EXPECT_EQ(cuts.size(), 3u);
137 cc.paint_subgraphs();
138 {
139 Mapped_Graph block;
140 cc.map_subgraph(block, NODE_COUNTER(v[0]));
141 EXPECT_EQ(block.get_num_nodes(), 5u);
142 expect_mapped(g, block);
143 }
144 Mapped_Graph block; // again, once the first copy is gone
145 cc.map_subgraph(block, NODE_COUNTER(v[0]));
146 EXPECT_EQ(block.get_num_nodes(), 5u);
147 expect_mapped(g, block);
148 {
149 Mapped_Graph cut_graph;
151 cc.map_cut_graph(cut_graph, cross);
152 EXPECT_EQ(cut_graph.get_num_nodes(), 3u);
153 expect_mapped(g, cut_graph);
154 }
155
156 // compute_blocks() maps the cut graph again, and all the blocks
158 Mapped_Graph cut_graph;
160 cc.compute_blocks(blocks, cut_graph, cross);
161 size_t nodes = 0;
162 for (auto it = blocks.get_it(); it.has_curr(); it.next_ne())
163 {
164 nodes += it.get_curr().get_num_nodes();
165 expect_mapped(g, it.get_curr());
166 }
167 EXPECT_EQ(nodes, 7u); // all but the cut nodes
168 EXPECT_EQ(cut_graph.get_num_nodes(), 3u);
169 expect_mapped(g, cut_graph);
170}
171
172// map_subgraph() reset the bits but not the cookies, and map_cut_graph()
173// gave a copy of the graph that it had mapped
174TEST(CookieMapping, CutNodeFunctionsMapTheirCopies)
175{
176 Mapped_Graph g;
177 auto v = build(g);
178 auto cuts = compute_cut_nodes(g, v[0]);
179 EXPECT_EQ(cuts.size(), 3u);
180 paint_subgraphs(g, cuts);
181 const long color = NODE_COUNTER(v[0]);
182 for (int run = 0; run < 2; ++run)
183 {
184 SCOPED_TRACE(run);
185 Mapped_Graph block = map_subgraph(g, color);
186 EXPECT_EQ(block.get_num_nodes(), 5u);
187 expect_mapped(g, block);
188 auto [cut_graph, cross] = map_cut_graph(g, cuts);
189 EXPECT_EQ(cut_graph.get_num_nodes(), 3u);
190 expect_mapped(g, cut_graph);
191 }
192}
193
194// The breadth-first trees reset the bits but not the cookies
195TEST(CookieMapping, BreadthFirstTreesMapTheirCopies)
196{
197 Mapped_Graph g;
198 auto v = build(g);
199 for (int run = 0; run < 2; ++run)
200 {
201 SCOPED_TRACE(run);
202 Mapped_Graph tree;
204 EXPECT_EQ(tree.get_num_arcs(), 9u);
205 expect_mapped(g, tree);
206 Mapped_Graph tree2 = find_breadth_first_spanning_tree(g, v[0]);
207 EXPECT_EQ(tree2.get_num_arcs(), 9u);
208 expect_mapped(g, tree2);
209 }
210}
211
212// Tarjan_Connected_Components did not clear the cookies of the arcs
213TEST(CookieMapping, TarjanMapsItsBlocks)
214{
215 Mapped_Digraph g;
216 build(g);
217 for (int run = 0; run < 2; ++run)
218 {
219 SCOPED_TRACE(run);
223 EXPECT_EQ(blocks.size(), 3u);
224 for (auto it = blocks.get_it(); it.has_curr(); it.next_ne())
225 expect_mapped(g, it.get_curr());
226 }
227}
228
229// kosaraju_connected_components() mapped the nodes of g, and the blocks of
230// the arcs, to the transposed graph, which it destroys
231TEST(CookieMapping, KosarajuMapsItsBlocks)
232{
233 Mapped_Digraph g;
234 build(g);
235 for (int run = 0; run < 2; ++run)
236 {
237 SCOPED_TRACE(run);
241 EXPECT_EQ(blocks.size(), 3u);
242 for (auto it = blocks.get_it(); it.has_curr(); it.next_ne())
243 expect_mapped(g, it.get_curr());
244 for (auto it = between.get_it(); it.has_curr(); it.next_ne())
245 EXPECT_EQ(ARC_COOKIE(it.get_curr()), nullptr); // not to the transposed graph
246 }
247 auto lists = kosaraju_connected_components(g);
248 EXPECT_EQ(lists.size(), 3u);
249 for (Mapped_Digraph::Node_Iterator it(g); it.has_curr(); it.next_ne())
250 EXPECT_EQ(NODE_COOKIE(it.get_curr()), nullptr); // not to the transposed graph
251}
252
253// Bellman_Ford::build_tree() mapped the nodes when their cookies still
254// held their parents in the tree
255TEST(CookieMapping, BellmanFordMapsItsTree)
256{
257 Mapped_Digraph g;
258 auto v = build(g);
260 ASSERT_FALSE(bf.paint_spanning_tree(v[0])); // no negative cycle
261 Mapped_Digraph tree;
262 bf.build_tree(tree);
263 EXPECT_EQ(tree.get_num_arcs(), 9u);
264 expect_mapped(g, tree);
265}
266
267// build_dominator_tree() did not clear the cookies, unlike the tree of the
268// post-dominators
269TEST(CookieMapping, DominatorTreeMapsItsNodes)
270{
271 Mapped_Digraph g;
272 auto v = build(g);
273 for (int run = 0; run < 2; ++run)
274 {
275 SCOPED_TRACE(run);
276 Mapped_Digraph tree;
277 build_dominator_tree(g, v[0], tree);
278 EXPECT_EQ(tree.get_num_nodes(), 10u);
279 for (Mapped_Digraph::Node_Iterator it(tree); it.has_curr(); it.next_ne())
280 {
281 auto q = it.get_curr();
282 auto p = static_cast<Mapped_Digraph::Node *>(NODE_COOKIE(q));
283 ASSERT_TRUE(has_node(g, p));
284 EXPECT_EQ(NODE_COOKIE(p), q);
285 }
286 }
287}
Bellman-Ford algorithm for single-source shortest paths.
Dominator and post-dominator tree computation via the Lengauer-Tarjan algorithm.
Tarjan's algorithm for strongly connected components.
Bellman-Ford algorithm for shortest paths with negative weights.
bool paint_spanning_tree(Node *start)
Paint the shortest paths tree from a start node.
void build_tree(GT &tree, bool with_map=true)
Extract from the graph a previously painted shortest paths tree.
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.
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
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
Compute a breadth-first spanning tree of a graph.
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:439
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
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
Definition Tarjan.H:174
void connected_components(const GT &g, DynList< GT > &blk_list, DynList< typename GT::Arc * > &arc_list)
Computes the strongly connected components (SCCs) of a digraph.
Definition Tarjan.H:721
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
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
#define TEST(name)
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
#define ARC_COOKIE(p)
Return the arc cookie
void kosaraju_connected_components(const GT &g, DynList< GT > &blk_list, DynList< typename GT::Arc * > &arc_list)
Compute strongly connected components using Kosaraju's algorithm.
Definition kosaraju.H:267
#define NODE_COUNTER(p)
Get the counter of a node.
GT find_breadth_first_spanning_tree(GT &g, typename GT::Node *gp)
Build a breadth-first spanning tree (mapped to the original graph).
void build_dominator_tree(GT &g, typename GT::Node *root, GT &tree, SA sa=SA())
Build the dominator tree of a digraph.
Definition Dominators.H:678
#define NODE_COOKIE(p)
Return the node cookie
long paint_subgraphs(const GT &g, const DynList< typename GT::Node * > &cut_node_list)
Paint connected blocks around articulation points.
DynList< typename GT::Node * > compute_cut_nodes(const GT &g, typename GT::Node *start)
Compute articulation points (cut vertices) of an undirected graph.
std::tuple< GT, DynList< typename GT::Arc * > > map_cut_graph(const GT &g, const DynList< typename GT::Node * > &cut_node_list)
Extract the cut graph and cross-arc list.
GT map_subgraph(const GT &g, const long color)
Extract a mapped subgraph containing a given color.
Kosaraju's algorithm for finding strongly connected components.
void build(Tree_Node< Key > *troot, Convert &&convert, typename GT::Node *groot, Args &&... args)
Main namespace for Aleph-w library functions.
bool between(const Point &a, const Point &b, const Point &c)
static long & color(typename GT::Node *p)
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
static int * k
Articulation points (cut nodes), bridges, and biconnected components.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
Spanning tree generation algorithms.