4#include <gtest/gtest.h>
45 int operator()(LG::Arc * a)
const {
return a->
get_info(); }
50 struct Move_Only_Filter
52 Move_Only_Filter() =
default;
53 Move_Only_Filter(Move_Only_Filter &&) =
default;
54 Move_Only_Filter(
const Move_Only_Filter &) =
delete;
55 bool operator()(LG::Arc *)
const {
return true; }
59 struct Reference_Filter
62 bool operator()(LG::Arc * a)
const {
return a->get_info() < limit; }
65 struct NonArithmeticCost
71 using Distance_Type = Cost;
72 Cost operator()(LG::Arc *)
const {
return {}; }
78static_assert(AlephGraph<LG>
and AlephGraph<LD>
and AlephGraph<AG>
and AlephGraph<AD>);
79static_assert(AlephGraph<SG>
and AlephGraph<SD>
and AlephGraph<NG>);
80static_assert(AlephGraph<const LG &>);
88static_assert(has_basic_in_iterator<LG>
and has_basic_in_iterator<AG>
and
89 has_basic_in_iterator<SG>
and has_basic_in_iterator<NG>);
90static_assert(not has_basic_in_iterator<LD>
and not has_basic_in_iterator<AD>
and
91 not has_basic_in_iterator<SD>);
92static_assert(not AlephGraph<NotAGraph>
and not AlephGraph<DynList<int>>
and not AlephGraph<int>);
94static_assert(ArcFilter<Dft_Show_Arc<LG>, LG>);
95static_assert(NodeFilter<Dft_Show_Node<LG>, LG>);
96static_assert(not ArcFilter<Dft_Show_Node<LG>, LG>);
97static_assert(not NodeFilter<Dft_Show_Arc<LG>, LG>);
99static_assert(not ArcFilter<Move_Only_Filter, LG>);
100static_assert(ArcFilter<Reference_Filter, LG>);
102static_assert(ArcDistance<Dft_Dist<LG>, LG>);
103static_assert(not ArcDistance<PlainWeight, LG>);
105static_assert(FlowNetwork<NG>);
106static_assert(not FlowNetwork<LG>);
119concept can_yen =
requires(
const LG & g, LG::Node * n, D d) {
120 yen_k_shortest_paths<LG, D>(g, n, n, 1, d);
168TEST(AhGraphConceptsTest, InOutArcHelpersCompileAndAgree)
173 auto * a = g.insert_node(1);
174 auto * b = g.insert_node(2);
175 g.insert_arc(a, b, 4);
176 g.insert_arc(a, b, 7);
177 auto even = [](LG::Arc * arc) {
return arc->get_info() % 2 == 0; };
179 EXPECT_FALSE(all_out_arc<LG>(a, even));
180 EXPECT_TRUE(exists_out_arc<LG>(a, even));
181 EXPECT_EQ(search_in_arc<LG>(b, even)->get_info(), 4);
182 EXPECT_EQ(filter_in_arcs<LG>(b, even).
size(), 1u);
183 EXPECT_EQ((map_out_arcs<LG, int>(a, [](LG::Arc * arc) {
return arc->get_info(); }).
size()), 2u);
184 EXPECT_EQ((foldl_in_arcs<LG, int>(b, 0, [](
const int & s, LG::Arc * arc)
185 {
return s + arc->get_info(); })), 11);
188 EXPECT_EQ(out_degree<LG>(a), 2u);
189 EXPECT_EQ(in_degree<LG>(b), 2u);
190 EXPECT_EQ(in_degree<LG>(a), 0u);
195TEST(AhGraphConceptsTest, BasicInIteratorSeesTheArcsIntoTheNode)
198 auto * a = g.insert_node(1);
199 auto * b = g.insert_node(2);
200 auto * c = g.insert_node(3);
201 g.insert_arc(a, b, 4);
202 g.insert_arc(c, b, 5);
203 g.insert_arc(b, c, 6);
204 int sum = 0, sum_of_g = 0;
205 size_t n = 0, n_of_g = 0;
207 sum += it.get_curr()->get_info();
209 sum_of_g += it.get_curr()->get_info();
212 EXPECT_EQ(n_of_g, n);
213 EXPECT_EQ(sum_of_g,
sum);
216 auto * s = net.insert_node();
217 auto *
m = net.insert_node();
219 net.insert_arc(s,
m, 2);
220 net.insert_arc(
m, t, 3);
221 net.insert_arc(s, t, 4);
225 EXPECT_EQ(net.get_tgt_node(it.get_curr()), t);
228 EXPECT_EQ(into_t, 2u);
229 EXPECT_EQ(into_t, net.get_in_degree(t));
232TEST(AhGraphConceptsTest, ConstrainedAlgorithmsStillRun)
235 auto * a = g.insert_node(1);
236 auto * b = g.insert_node(2);
237 auto * c = g.insert_node(3);
238 g.insert_arc(a, b, 2);
239 g.insert_arc(b, c, 3);
240 g.insert_arc(a, c, 9);
248 EXPECT_EQ(tree.get_num_arcs(), 2u);
250 const auto paths = yen_k_shortest_paths<LG>(g, a, c, 2);
251 EXPECT_EQ(paths.size(), 2u);
258 struct May_Throw_Filter
260 bool operator()(
typename GT::Arc *)
const {
return true; }
267 const bool * armed =
nullptr;
269 bool operator()(
typename GT::Arc *)
const
272 throw std::runtime_error(
"Armed_Filter");
282TEST(AhGraphConceptsTest, WhatCallsTheFilterIsNoexceptAsTheFilter)
284 static_assert(nothrow_arc_filter_v<LG, Dft_Show_Arc<LG>>);
285 static_assert(not nothrow_arc_filter_v<LG, May_Throw_Filter<LG>>);
286 static_assert(not nothrow_arc_filter_v<LG, Reference_Filter>);
288 static_assert(
noexcept(search_arc<LG>(std::declval<const LG &>(),
nullptr,
nullptr)));
289 static_assert(not
noexcept(search_arc<LG, May_Throw_Filter<LG>>(std::declval<const LG &>(),
291 static_assert(
noexcept(search_directed_arc<LD>(std::declval<const LD &>(),
nullptr,
nullptr)));
292 static_assert(not
noexcept(search_directed_arc<LD, May_Throw_Filter<LD>>(
293 std::declval<const LD &>(),
nullptr,
nullptr)));
294 static_assert(
noexcept(search_spanning_tree_arc<LG>(std::declval<const LG &>(),
nullptr,
296 static_assert(not
noexcept(search_spanning_tree_arc<LG, May_Throw_Filter<LG>>(
297 std::declval<const LG &>(),
nullptr,
nullptr)));
299 static_assert(
noexcept(std::declval<Out_Iterator<LD> &>().
next_ne()));
300 static_assert(not
noexcept(std::declval<Out_Iterator<LD, May_Throw_Filter<LD>> &>().
next_ne()));
301 static_assert(
noexcept(std::declval<In_Iterator<LG> &>().
next_ne()));
302 static_assert(not
noexcept(std::declval<In_Iterator<LG, May_Throw_Filter<LG>> &>().
next_ne()));
303 static_assert(
noexcept(std::declval<In_Iterator<AD> &>().
next_ne()));
304 static_assert(not
noexcept(std::declval<In_Iterator<AD, May_Throw_Filter<AD>> &>().
next_ne()));
308 static_assert(
noexcept(std::declval<Frame &>().next_arc()));
309 static_assert(
noexcept(std::declval<Frame &>().template next_target<Aleph::Depth_First>()));
310 static_assert(not
noexcept(std::declval<May_Throw_Frame &>().next_arc()));
311 static_assert(not
noexcept(std::declval<May_Throw_Frame &>()
312 .template next_target<Aleph::Depth_First>()));
315 auto * a = g.insert_node(1);
316 auto * b = g.insert_node(2);
317 auto * c = g.insert_node(3);
318 auto * ab = g.insert_arc(a, b, 1);
319 g.insert_arc(a, c, 2);
322 const Armed_Filter<LG>
filter{&armed};
325 EXPECT_THROW((
search_arc<LG, Armed_Filter<LG>>(g, a, b,
filter)), std::runtime_error);
332 ASSERT_TRUE(it.has_curr());
334 EXPECT_THROW(it.
next_ne(), std::runtime_error);
Dijkstra's shortest path algorithm.
Johnson's algorithm for all-pairs shortest paths.
K-shortest path algorithms (Yen and Eppstein-style API).
Kruskal's minimum spanning tree algorithm.
C++20 concepts for the protocol shared by graph algorithms.
constexpr bool has_basic_in_iterator
WeightedDigraph::Node Node
bool has_curr() const noexcept
Return true the iterator has an current arc.
Generic directed graph (digraph) wrapper template.
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
Distance::Distance_Type find_min_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &min_path)
Computes the shortest path between start and end by painting the graph.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Arc for graphs implemented with simple adjacency lists.
Graph serialization and deserialization class.
Filtered iterator for incoming arcs of a node.
Computes the minimum spanning tree of a graph using Kruskal's algorithm.
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
_Graph_Node Node
The graph type.
Graph class implemented with singly-linked adjacency lists.
Number Theoretic Transform over Z / MOD Z.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
GT::Arc * search_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept(nothrow_arc_filter_v< GT, SA >)
Arc filtered searching given two nodes.
void for_each_node(const GT &g, Op operation, SN sn=SN())
Traverse all the nodes of graph filtering some ones according to a condition and executing an operati...
void for_each_arc(const GT &g, Op operation, SA sa=SA())
Traverse all the arcs of graph filtering some ones according to a condition and executing an operatio...
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
void copy_graph(GT >gt, const GT &gsrc, bool cookie_map=false)
Explicit copy of graph.
Graph serialization and deserialization utilities.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
Net::Flow_Type dinic_maximum_flow(Net &net)
Compute maximum flow using Dinic's algorithm.
and
Check uniqueness with explicit hash + equality functors.
bool are_equal(const GT &g1, const GT &g2)
Fast graph comparison.
GT::Arc * search_spanning_tree_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept(nothrow_arc_filter_v< GT, SA >)
Helper to find the spanning tree arc between two nodes in a multigraph.
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
The next arc of a node to look at, for a depth-first search that keeps only that in a frame.
Default arc loading functor for binary and text modes.
Default arc storage functor for binary and text modes.
Default node storage functor for binary and text modes.
Arc of graph implemented with double-linked adjacency lists.
Arc of a flow network implemented with adjacency lists.
Flow network implemented with adjacency lists.
Filtered iterator of adjacent arcs of a node.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Array-based graph implementation.
Generic graph and digraph implementations.
Graph indexing utilities for O(log n) node/arc lookup.
Advanced maximum flow algorithms.
Network flow graph structures.
Simple graph implementation with adjacency lists.