Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_contract_matrix_paths_cycles_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
49#include <set>
50#include <stdexcept>
51#include <string>
52#include <tuple>
53#include <type_traits>
54#include <utility>
55
56#include <gtest/gtest.h>
57
58#include <tpl_graph.H>
59#include <Bellman_Ford.H>
60#include <Min_Mean_Cycle.H>
62#include <Negative_Cycles.H>
63
64#include "graph_harness.H"
67
68using namespace Aleph;
69using namespace graph_harness;
70using namespace graph_contract_matrix;
71
72namespace
73{
75 template <class List>
76 std::set<int> arc_numbers(const List & l)
77 {
78 std::set<int> r;
79 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
80 r.insert(it.get_curr()->get_info());
81 return r;
82 }
83} // namespace
84
85namespace
86{
91 Spec ladder()
92 {
93 Spec s;
94 s.n = 4;
95 s.arcs = {{0, 1}, {2, 3}, {1, 2}, {1, 2}};
96 return s;
97 }
98
101 template <class GT, class SA>
102 constexpr int ladder_cost = hides<GT, SA> ? 4 : 3;
103
106 template <class GT, class SA>
107 std::set<int> ladder_arcs()
108 {
109 return hides<GT, SA> ? std::set<int>{0, 1, 3} : std::set<int>{0, 1, 2};
110 }
111
113 Spec ring()
114 {
115 Spec s;
116 s.n = 3;
117 s.arcs = {{0, 1}, {1, 2}, {2, 0}};
118 return s;
119 }
120
125 template <class GT>
126 void make_negative(Built<GT> & b)
127 {
128 b.arcs[0]->get_info() = -3;
129 }
130
131 Spec negative()
132 {
133 Spec s;
134 s.n = 3;
135 s.arcs = {{0, 1}, {1, 2}, {1, 0}};
136 return s;
137 }
138
140 template <class GT>
141 std::set<int> path_arcs(const Path<GT> & p)
142 {
143 std::set<int> r;
144 p.for_each_arc([&r] (typename GT::Arc * a) { r.insert(a->get_info()); });
145 return r;
146 }
147
150 template <class GT>
151 std::set<int> graph_arcs(const GT & g)
152 {
153 std::set<int> r;
154 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
155 r.insert(it.get_curr_ne()->get_info());
156 return r;
157 }
158} // namespace
159
160// On the digraphs
161
162template <class GT>
163class ContractPathsCycles : public ::testing::Test {};
164
166
168{
169 using GT = TypeParam;
170
171 for_each_filter<GT>([&] (auto sa, const char * name)
172 {
173 using SA = decltype(sa);
175 Built<GT> l(ladder());
176 Built<GT> neg(negative());
177 make_negative(neg);
178 SCOPED_TRACE(name);
179 constexpr bool hidden = hides<GT, SA>;
180 const int cost = ladder_cost<GT, SA>;
181 const auto arcs = ladder_arcs<GT, SA>();
182
183 // The constructor takes the graph as GT &. build_tree() goes last,
184 // since it maps the cookies through which get_distance() walks
185 {
186 BF x(l.g, Dft_Dist<GT>(), sa);
187 EXPECT_FALSE(x.has_computation());
188 EXPECT_EQ(&x.get_graph(), &l.g);
189 EXPECT_FALSE(x.paint_spanning_tree(l.nodes[0]));
190 EXPECT_TRUE(x.is_painted());
191 EXPECT_EQ(x.get_start_node(), l.nodes[0]);
192 EXPECT_EQ(x.get_distance(l.nodes[3]), cost);
193 Path<GT> path(l.g);
194 EXPECT_EQ(x.get_min_path(l.nodes[3], path), cost);
195 EXPECT_EQ(path_arcs(path), arcs);
196 std::set<int> tree_arcs;
197 const auto tree_array = x.extract_min_spanning_tree();
198 for (size_t i = 0; i < tree_array.size(); ++i)
199 if (tree_array(i) != nullptr)
200 tree_arcs.insert(tree_array(i)->get_info());
201 EXPECT_EQ(tree_arcs, arcs);
202 GT tree;
203 x.build_tree(tree, true);
204 EXPECT_EQ(graph_arcs(tree), arcs);
205 }
206 {
207 BF x(l.g, Dft_Dist<GT>(), sa);
208 EXPECT_FALSE(x.faster_paint_spanning_tree(l.nodes[0]));
209 EXPECT_EQ(x.get_distance(l.nodes[3]), cost);
210 x.clear();
211 EXPECT_FALSE(x.is_painted());
212 EXPECT_EQ(x.compute_nodes_weights().size(), 4U);
213 }
214
215 // A negative cycle only through the hidden arc
216 BF x(neg.g, Dft_Dist<GT>(), sa);
217 EXPECT_EQ(x.has_negative_cycle(neg.nodes[0]), not hidden);
218 EXPECT_EQ(x.has_negative_cycle(), not hidden);
219 if constexpr (hidden)
220 {
221 EXPECT_FALSE(x.paint_spanning_tree(neg.nodes[0]));
222 EXPECT_EQ(x.get_distance(neg.nodes[2]), -2);
223 }
224
225 EXPECT_EQ(x.test_negative_cycle(neg.nodes[0]).is_empty(), hidden);
226 EXPECT_EQ(x.test_negative_cycle().is_empty(), hidden);
227 EXPECT_EQ(std::get<0>(x.search_negative_cycle(neg.nodes[0], 0.7, 1)).is_empty(), hidden);
228 EXPECT_EQ(x.search_negative_cycle(neg.nodes[0]).is_empty(), hidden);
229 EXPECT_EQ(std::get<0>(x.search_negative_cycle(0.7, 1)).is_empty(), hidden);
230 EXPECT_EQ(x.search_negative_cycle().is_empty(), hidden);
231 Path<GT> cycle(neg.g);
232 EXPECT_EQ(x.test_negative_cycle(neg.nodes[0], cycle), not hidden);
233 EXPECT_EQ(x.test_negative_cycle(cycle), not hidden);
234 EXPECT_EQ(path_arcs(cycle).count(Hidden), hidden ? 0U : 1U);
235 });
236}
237
238TYPED_TEST(ContractPathsCycles, BellmanFordNegativeCycle)
239{
240 using GT = TypeParam;
241
242 for_each_filter<GT>([&] (auto sa, const char * name)
243 {
244 using SA = decltype(sa);
245 Built<GT> neg(negative());
246 make_negative(neg);
247 auto * s = neg.nodes[0];
248 SCOPED_TRACE(name);
249 constexpr bool hidden = hides<GT, SA>;
250
252 Node_Arc_Iterator, SA> x;
253 Path<GT> path(neg.g);
254
255 // The distance and the filter as lvalues
256 Dft_Dist<GT> d;
257 EXPECT_EQ(x(neg.g, path, d, sa), not hidden);
258 EXPECT_EQ(x(neg.g, s, path, d, sa), not hidden);
259 EXPECT_EQ(x(neg.g, s, d, sa).is_empty(), hidden);
260 EXPECT_EQ(x(neg.g, d, sa).is_empty(), hidden);
261
262 // And as rvalues
263 EXPECT_EQ(x(neg.g, path, Dft_Dist<GT>(), SA(sa)), not hidden);
264 EXPECT_EQ(x(neg.g, s, path, Dft_Dist<GT>(), SA(sa)), not hidden);
265 EXPECT_EQ(x(neg.g, s, Dft_Dist<GT>(), SA(sa)).is_empty(), hidden);
266 EXPECT_EQ(x(neg.g, Dft_Dist<GT>(), SA(sa)).is_empty(), hidden);
267 });
268}
269
270TYPED_TEST(ContractPathsCycles, KarpMinimumMeanCycle)
271{
272 using GT = TypeParam;
273
274 for_each_filter<GT>([&] (auto sa, const char * name)
275 {
276 using SA = decltype(sa);
277 Built<GT> b(ring());
278 SCOPED_TRACE(name);
279 constexpr bool hidden = hides<GT, SA>;
280 for_each_constness(b.g, [&] (auto & g)
281 {
282 const auto r = karp_minimum_mean_cycle(g, Dft_Dist<GT>(), sa);
283 EXPECT_EQ(r.has_cycle, not hidden);
284 if (not hidden)
285 {
286 EXPECT_EQ(r.minimum_mean, 1.0L);
287 EXPECT_EQ(arc_numbers(r.cycle_arcs), (std::set<int>{0, 1, 2}));
288 }
289 EXPECT_EQ(minimum_mean_cycle(g, Dft_Dist<GT>(), sa).has_cycle, not hidden);
290 EXPECT_EQ(karp_minimum_mean_cycle_value(g, Dft_Dist<GT>(), sa).has_cycle, not hidden);
291 EXPECT_EQ(minimum_mean_cycle_value(g, Dft_Dist<GT>(), sa).has_cycle, not hidden);
292
294 EXPECT_EQ(karp(g).has_cycle, not hidden);
296 EXPECT_EQ(value(g).has_cycle, not hidden);
297 });
298 });
299}
300
301TYPED_TEST(ContractPathsCycles, HowardMinimumMeanCycle)
302{
303 using GT = TypeParam;
304
305 for_each_filter<GT>([&] (auto sa, const char * name)
306 {
307 using SA = decltype(sa);
308 Built<GT> b(ring());
309 SCOPED_TRACE(name);
310 constexpr bool hidden = hides<GT, SA>;
311 for_each_constness(b.g, [&] (auto & g)
312 {
313 const auto r = howard_minimum_mean_cycle(g, Dft_Dist<GT>(), sa);
314 EXPECT_EQ(r.has_cycle, not hidden);
315 if (not hidden)
316 {
317 EXPECT_EQ(r.minimum_mean, 1.0L);
318 EXPECT_EQ(arc_numbers(r.cycle_arcs), (std::set<int>{0, 1, 2}));
319 }
320 EXPECT_EQ(howard_minimum_mean_cycle(g, Dft_Dist<GT>(), sa, size_t(1) << 20).has_cycle,
321 not hidden);
322 EXPECT_EQ(howard_minimum_mean_cycle_value(g, Dft_Dist<GT>(), sa).has_cycle, not hidden);
323 EXPECT_EQ(howard_minimum_mean_cycle_value(g, Dft_Dist<GT>(), sa, size_t(1) << 20).has_cycle,
324 not hidden);
325 });
326 });
327}
328
330{
331 using GT = TypeParam;
332
333 for_each_filter<GT>([&] (auto sa, const char * name)
334 {
335 using SA = decltype(sa);
336 Built<GT> neg(negative());
337 make_negative(neg);
338 SCOPED_TRACE(name);
339 constexpr bool hidden = hides<GT, SA>;
340 for_each_constness(neg.g, [&] (auto & g)
341 {
342 const auto cycles = find_disjoint_negative_cycles(g, 2, Negative_Cycle_Exclusion::Min_Weight_Arc,
343 Dft_Dist<GT>(), sa);
344 ASSERT_EQ(cycles.size(), hidden ? 0U : 1U);
345 if (not hidden)
346 {
347 EXPECT_EQ(cycles.get_first().total_cost, -1);
348 }
349 EXPECT_EQ(most_negative_cycle_bounded(g, 3, Dft_Dist<GT>(), sa).is_negative(), not hidden);
350 EXPECT_EQ(most_negative_cycle_up_to_3(g, 3, Dft_Dist<GT>(), sa).is_negative(), not hidden);
351 });
352 });
353}
Bellman-Ford algorithm for single-source shortest paths.
Minimum mean cycle by Howard's policy iteration.
Minimum mean cycle in directed graphs (Karp algorithm).
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
size_t size_t int32_t value
Definition ca-c-api.h:116
Bellman-Ford algorithm for shortest paths with negative weights.
Default distance accessor for arc weights.
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
Functor wrapper for value-only minimum mean cycle.
Functor wrapper for Karp minimum mean cycle.
Filtered iterator for outcoming arcs of a node.
Definition tpl_graph.H:1942
Path on a graph.
Definition tpl_graph.H:3832
void for_each_arc(Operation op=Operation()) const
Execute an operation on each arc of path.
Definition tpl_graph.H:4540
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
What the files of the contract matrix of the graph templates, graph_contract_matrix_*_test....
TYPED_TEST(ContractPathsCycles, BellmanFord)
TYPED_TEST_SUITE(ContractPathsCycles, Directed_Kinds)
The kinds of arc filter and of distance that the graph algorithms must accept.
Every small multigraph, on every kind of graph, for the tests.
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
Min_Mean_Cycle_Value_Result minimum_mean_cycle_value(const GT &g, Distance distance=Distance(), SA sa=SA())
Alias for karp_minimum_mean_cycle_value().
bool has_cycle(const GT &g)
Return true if an undirected graph has at least one cycle.
Bounded_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_bounded(const GT &g, const size_t max_length, Distance distance=Distance(), SA sa=SA())
Most negative simple cycle of at most max_length arcs, ranked by total cost (not by mean cost as Karp...
Min_Mean_Cycle_Result< GT, typename Distance::Distance_Type > minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA())
Alias for karp_minimum_mean_cycle().
Min_Mean_Cycle_Value_Result karp_minimum_mean_cycle_value(const GT &g, Distance distance=Distance(), SA sa=SA())
Compute only the minimum mean value (without witness walk).
Short_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_up_to_3(const GT &g, const size_t max_length=3, Distance distance=Distance(), SA sa=SA())
Minimum-total-cost simple cycle with at most one, two or three arcs.
Howard_Mean_Cycle_Result< GT, typename Distance::Distance_Type > howard_minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA(), const size_t max_fallback_bytes=std::numeric_limits< size_t >::max())
Minimum mean cycle by Howard's policy iteration.
Min_Mean_Cycle_Value_Result howard_minimum_mean_cycle_value(const GT &g, Distance distance=Distance(), SA sa=SA(), const size_t max_fallback_bytes=std::numeric_limits< size_t >::max())
Minimum mean cycle value by Howard's policy iteration, without the witness.
Main namespace for Aleph-w library functions.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
void for_each_constness(GT &g, F &&f)
Calls f(g) with g as it is and as a const graph.
constexpr int Hidden
The number of the arc that the filters hide.
::testing::Types< Aleph::List_Digraph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SDigraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Digraph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Directed_Kinds
The directed kinds, with int infos.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Detects if a negative cycle exists and eventually computes it.
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
std::vector< typename GT::Arc * > arcs
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
Gsl_Compatible_Rng r
Generic graph and digraph implementations.
DynList< int > l