48#include <gtest/gtest.h>
69 std::vector<Graph::Node *> make_nodes(
Graph &g,
int n)
71 std::vector<Graph::Node *>
nodes;
73 for (
int i = 0; i < n; ++i)
78 std::vector<TestDigraph::Node *> make_nodes(
TestDigraph &g,
int n)
80 std::vector<TestDigraph::Node *>
nodes;
82 for (
int i = 0; i < n; ++i)
83 nodes.push_back(g.insert_node(i));
87 std::vector<WGraph::Node *> make_nodes(
WGraph &g,
int n)
89 std::vector<WGraph::Node *>
nodes;
91 for (
int i = 0; i < n; ++i)
92 nodes.push_back(g.insert_node(i));
121 struct CollectVisitedInfos
123 std::vector<int> *infos =
nullptr;
133 struct EvenArcInfoOnly
135 bool operator()(
typename GT::Arc *a)
const noexcept
137 return (a->get_info() % 2) == 0;
145 int bfs_distance(
const std::vector<std::vector<int>> &adj,
int s,
int t)
150 std::vector<int> dist(adj.size(), -1);
163 dist[v] = dist[u] + 1;
176 std::vector<typename GT::Node *>
nodes;
190 auto nodes = make_nodes(g, 5);
201 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
208 auto nodes = make_nodes(g, 3);
219 auto nodes = make_nodes(g, 3);
230 auto nodes = make_nodes(g, 5);
241 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
248 auto nodes = make_nodes(g, 3);
259 auto nodes = make_nodes(g, 3);
274 auto nodes = make_nodes(g, 3);
294 auto nodes = make_nodes(g, 3);
318 auto nodes = make_nodes(g, 3);
332 auto nodes = make_nodes(g, 4);
344 auto nodes = make_nodes(g, 4);
363 std::mt19937
rng(123456);
364 std::uniform_int_distribution<int>
n_dist(2, 10);
365 std::bernoulli_distribution
edge_dist(0.25);
366 std::uniform_int_distribution<int>
pick_dist(0, 9);
373 auto nodes = make_nodes(g, n);
374 std::vector<std::vector<int>> adj(n);
376 for (
int u = 0; u < n; ++u)
377 for (
int v = u + 1; v < n; ++v)
403 for (
size_t i = 0; i + 1 <
seq.
size(); ++i)
423 auto nodes = make_nodes(g, 4);
435 auto nodes = make_nodes(dg, 2);
443 auto nodes = make_nodes(g, 3);
461 auto nodes = make_nodes(tree, 4);
476 auto nodes = make_nodes(dg, 2);
486 auto nodes = make_nodes(g, 6);
511 auto nodes = make_nodes(g, 4);
517 std::multiset<std::pair<size_t, size_t>>
sizes;
519 for (
auto it =
comps.
get_it(); it.has_curr(); it.next_ne())
522 auto &sg = it.get_curr();
523 sizes.
insert({sg.get_num_nodes(), sg.get_num_arcs()});
527 EXPECT_EQ(
sizes, (std::multiset<std::pair<size_t, size_t>>{{3U, 2U}, {1U, 0
U}}));
530 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
532 auto gp = it.get_curr();
548 auto nodes = make_nodes(g, 4);
572 auto nodes = make_nodes(g, 5);
592 auto nodes = make_nodes(g, 5);
614 auto nodes = make_nodes(g, 4);
625 arcs.append(
nullptr);
634 for (
auto it = tree.get_node_it(); it.has_curr(); it.next_ne())
636 auto tp = it.get_curr();
642 for (
auto it = tree.get_arc_it(); it.has_curr(); it.next_ne())
644 auto ta = it.get_curr();
651 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
662 auto nodes = make_nodes(g, 4);
670 for (
auto it = cut_nodes.get_it(); it.has_curr(); it.next_ne())
671 infos.insert(it.get_curr()->get_info());
675 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
680 auto dn = make_nodes(dg, 2);
681 dg.insert_arc(
dn[0],
dn[1], 1);
688 auto nodes = make_nodes(g, 4);
714 auto nodes = make_nodes(g, 6);
716 for (
int i = 1; i < 6; ++i)
750 auto nodes = make_nodes(g, 3);
784 bool operator()(
TestDigraph::Arc *a)
const noexcept {
return a->get_info() == 2; }
785 void set_cookie(
void *)
noexcept {}
789 auto nodes = make_nodes(g, 3);
822 auto nodes = make_nodes(g, 3);
855 auto nodes = make_nodes(g, 4);
Stateful breadth-first traversal functor.
Stateful depth-first traversal functor.
Default distance accessor for arc weights.
Generic directed graph (digraph) wrapper template.
typename BaseGraph::Arc Arc
T & append()
Allocate a new entry to the end of array.
T & insert(const T &item)
Insert a new item by copy.
size_t size() const noexcept
Count the number of elements of the list.
Functor for computing the transposed digraph, filtering arcs.
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)
void for_each_node(Operation op=Operation()) const
Execute an operation on each node of path.
size_t size() const noexcept
Return the path length in nodes.
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
bool is_empty() const noexcept
Return true if the path is empty.
bool inside_graph(const GT &gr) const noexcept
Return true if this is on graph gr
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Compute the total cost (sum of arc weights) of a graph.
Distance::Distance_Type total_cost(GT &g)
Compute the total cost.
Distance::Distance_Type value() const noexcept
Return the accumulated value (after using operator()(Arc*)).
void reset() noexcept
Reset the internal accumulator used by operator()(Arc*).
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
void reset_arcs() const
Reset all the arcs of graph (the control bits, the state, the counter and the cookie)
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
constexpr size_t get_num_arcs() const noexcept
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
size_t depth_first_traversal(const GT &g, typename GT::Node *start_node, bool(*visit)(const GT &g, typename GT::Node *, typename GT::Arc *))
Depth-first traversal starting from a given node.
#define ARC_COOKIE(p)
Return the arc cookie
bool has_cycle(const GT &g)
Return true if an undirected graph has at least one cycle.
DynList< GT > inconnected_components(const GT &g)
Forward declaration for inconnected_components().
bool test_for_cycle(const GT &g, typename GT::Node *src)
Search for a cycle reachable from a given node.
GT find_breadth_first_spanning_tree(GT &g, typename GT::Node *gp)
Build a breadth-first spanning tree (mapped to the original graph).
bool traverse_arcs(typename GT::Node *p, Operation op=Operation())
Generic arcs traverse of a node.
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
GT invert_digraph(const GT &g)
Compute the transpose (arc-reversed) digraph.
DynList< typename GT::Node * > compute_cut_nodes(const GT &g, typename GT::Node *start)
Compute articulation points (cut vertices) of an undirected graph.
bool test_connectivity(const GT &g)
Connectivity test for undirected graphs.
#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.
Path< GT > find_path_breadth_first(const GT &g, typename GT::Node *start, typename GT::Node *end)
Breadth-first search of a (shortest-by-edges) path between two nodes.
bool test_for_path(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Return true if there is a path between two nodes.
size_t breadth_first_traversal(const GT &g, typename GT::Node *start, bool(*visit)(const GT &, typename GT::Node *, typename GT::Arc *))
Breadth-first traversal starting from a given node.
void build_subgraph(const GT &g, GT &sg, typename GT::Node *g_src, size_t &node_count)
Forward declaration for build_subgraph().
GT find_depth_first_spanning_tree(const GT &g, typename GT::Node *gnode)
Build a depth-first spanning tree (mapped to the original graph).
GT::Arc * search_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept
Arc filtered searching given two nodes.
bool is_graph_acyclique(const GT &g, typename GT::Node *start_node)
Return true if an undirected graph is acyclic.
GT::Arc * search_directed_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept
Searching of directed arc linking two nodes.
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.
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Filtered iterator of adjacent arcs of a node.
Lazy and scalable dynamic array implementation.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.