46#include <gtest/gtest.h>
68 bool has_node(
const GT & g,
typename GT::Node * p)
70 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
71 if (it.get_curr() == p)
78 bool has_arc(
const GT & g,
typename GT::Arc * a)
80 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
81 if (it.get_curr() == a)
89 void expect_mapped(
const GT & g,
const GT & c)
91 for (
typename GT::Node_Iterator it(c); it.has_curr(); it.next_ne())
93 auto q = it.get_curr();
95 ASSERT_TRUE(has_node(g, p)) <<
"node " << q->
get_info();
96 EXPECT_EQ(
NODE_COOKIE(p), q) <<
"node " << q->get_info();
98 for (
typename GT::Arc_Iterator it(c); it.has_curr(); it.next_ne())
100 auto b = it.get_curr();
102 ASSERT_TRUE(has_arc(g, a)) <<
"arc " << b->
get_info();
103 EXPECT_EQ(
ARC_COOKIE(a), b) <<
"arc " << b->get_info();
110 std::vector<typename GT::Node *>
build(
GT & g)
112 std::vector<typename GT::Node *> v;
113 for (
int i = 0; i < 10; ++i)
116 for (
int i = 0; i < 6; ++i)
129TEST(CookieMapping, ComputeCutNodesMapsItsCopies)
136 EXPECT_EQ(cuts.
size(), 3u);
141 EXPECT_EQ(block.get_num_nodes(), 5u);
142 expect_mapped(g, block);
146 EXPECT_EQ(block.get_num_nodes(), 5u);
147 expect_mapped(g, block);
149 Mapped_Graph cut_graph;
152 EXPECT_EQ(cut_graph.get_num_nodes(), 3u);
153 expect_mapped(g, cut_graph);
158 Mapped_Graph cut_graph;
162 for (
auto it = blocks.
get_it(); it.has_curr(); it.next_ne())
164 nodes += it.get_curr().get_num_nodes();
165 expect_mapped(g, it.get_curr());
167 EXPECT_EQ(
nodes, 7u);
168 EXPECT_EQ(cut_graph.get_num_nodes(), 3u);
169 expect_mapped(g, cut_graph);
174TEST(CookieMapping, CutNodeFunctionsMapTheirCopies)
179 EXPECT_EQ(cuts.size(), 3u);
182 for (
int run = 0; run < 2; ++run)
186 EXPECT_EQ(block.get_num_nodes(), 5u);
187 expect_mapped(g, block);
189 EXPECT_EQ(cut_graph.get_num_nodes(), 3u);
190 expect_mapped(g, cut_graph);
195TEST(CookieMapping, BreadthFirstTreesMapTheirCopies)
199 for (
int run = 0; run < 2; ++run)
204 EXPECT_EQ(tree.get_num_arcs(), 9u);
205 expect_mapped(g, tree);
207 EXPECT_EQ(tree2.get_num_arcs(), 9u);
208 expect_mapped(g, tree2);
213TEST(CookieMapping, TarjanMapsItsBlocks)
217 for (
int run = 0; run < 2; ++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());
231TEST(CookieMapping, KosarajuMapsItsBlocks)
235 for (
int run = 0; run < 2; ++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);
248 EXPECT_EQ(lists.size(), 3u);
249 for (Mapped_Digraph::Node_Iterator it(g); it.has_curr(); it.next_ne())
255TEST(CookieMapping, BellmanFordMapsItsTree)
263 EXPECT_EQ(tree.get_num_arcs(), 9u);
264 expect_mapped(g, tree);
269TEST(CookieMapping, DominatorTreeMapsItsNodes)
273 for (
int run = 0; run < 2; ++run)
278 EXPECT_EQ(tree.get_num_nodes(), 10u);
279 for (Mapped_Digraph::Node_Iterator it(tree); it.has_curr(); it.next_ne())
281 auto q = it.get_curr();
282 auto p =
static_cast<Mapped_Digraph::Node *
>(
NODE_COOKIE(q));
283 ASSERT_TRUE(has_node(g, p));
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.
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).
size_t size() const noexcept
Return the number of items of the list, in constant time.
Compute a breadth-first spanning tree of a graph.
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
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.
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.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
DynArray< Graph::Node * > nodes
#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.
#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.
#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.
Articulation points (cut nodes), bridges, and biconnected components.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
Spanning tree generation algorithms.