Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_scenarios_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
32
38#include <gtest/gtest.h>
39
40#include <Bellman_Ford.H>
41#include <Dijkstra.H>
42#include <Tarjan.H>
43#include <random_graph.H>
44#include <tpl_graph.H>
45
46#include <cstdlib>
47#include <limits>
48#include <random>
49#include <vector>
50
51using namespace Aleph;
52
53namespace
54{
56using DNode = DGraph::Node;
57
58template <class GT, class SA>
59class Out_Iterator_Ne : public Out_Iterator<GT, SA>
60{
61public:
62 using Base = Out_Iterator<GT, SA>;
63 using Base::Base;
64
65 typename GT::Arc * get_current_arc_ne() const noexcept
66 {
67 return const_cast<Out_Iterator_Ne *>(this)->get_current_arc();
68 }
69};
70
71struct BinHeapTag
72{
73 template <class G, class D, class A>
74 using Heap = ArcHeap<G, D, A>;
75};
76
77using DijkstraInt =
79 Dft_Show_Arc<DGraph>, BinHeapTag::template Heap>;
80
83 Dft_Show_Arc<DGraph>, BinHeapTag::template Heap>;
84
85struct InitNodeNoop
86{
87 void operator()(DGraph &, DNode *) const noexcept {}
88};
89
90struct InitArcRandNonNegative
91{
92 std::mt19937 rng;
93 std::uniform_int_distribution<int> dist;
94
95 explicit InitArcRandNonNegative(uint32_t seed, int max_w)
96 : rng(seed), dist(0, max_w)
97 {
98 }
99
100 void operator()(DGraph &, DGraph::Arc *a)
101 {
102 a->get_info() = dist(rng);
103 }
104};
105
106struct InitArcRandAllowNegative
107{
108 std::mt19937 rng;
109 std::uniform_int_distribution<int> dist;
110
111 explicit InitArcRandAllowNegative(uint32_t seed, int min_w, int max_w)
112 : rng(seed), dist(min_w, max_w)
113 {
114 }
115
116 void operator()(DGraph &, DGraph::Arc *a)
117 {
118 a->get_info() = dist(rng);
119 }
120};
121
122static DNode * pick_node_by_index(DGraph & g, size_t idx)
123{
124 size_t i = 0;
125 for (auto it = g.get_node_it(); it.has_curr(); it.next_ne(), ++i)
126 if (i == idx)
127 return it.get_curr();
128 return nullptr;
129}
130
131static std::pair<DNode *, DNode *> pick_two_distinct_nodes(DGraph & g,
132 uint32_t seed)
133{
134 std::mt19937 rng(seed);
135 std::uniform_int_distribution<size_t> dist(0, g.get_num_nodes() - 1);
136
137 const size_t i = dist(rng);
138 size_t j = dist(rng);
139 while (j == i)
140 j = dist(rng);
141
142 auto *s = pick_node_by_index(g, i);
143 auto *t = pick_node_by_index(g, j);
144 return {s, t};
145}
146
148{
149 ASSERT_NE(s, nullptr);
150 ASSERT_NE(t, nullptr);
151
152 // Dijkstra
155 const int dij_dist = dij(g, s, t, dij_path);
156
157 // Bellman-Ford (no negative weights/cycles in this test)
159 const bool neg = bf.paint_spanning_tree(s);
161
163 const int bf_dist = bf.get_min_path(t, bf_path);
164
165 // Both implementations use max() as INF on unreachable.
167
168 // If reachable, paths must be non-empty and end at t.
169 if (bf_dist != std::numeric_limits<int>::max())
170 {
173
174 EXPECT_EQ(dij_path.get_last_node(), t);
175 EXPECT_EQ(bf_path.get_last_node(), t);
176 }
177 else
178 {
181 }
182}
183} // namespace
184
186{
187 DGraph g;
188 auto *a = g.insert_node(1);
189 auto *b = g.insert_node(2);
190 auto *c = g.insert_node(3);
191 auto *d = g.insert_node(4);
192
193 // Component 1: a -> b
194 g.insert_arc(a, b, 5);
195
196 // Component 2: c -> d
197 g.insert_arc(c, d, 7);
198
200 Path<DGraph> path(g);
201 const int dist = dij(g, a, d, path);
202
203 EXPECT_EQ(dist, std::numeric_limits<int>::max());
204 EXPECT_TRUE(path.is_empty());
205}
206
208{
209 DGraph g;
210 auto *s = g.insert_node(0);
211 auto *t = g.insert_node(1);
212
213 const auto arcs_before = g.get_num_arcs();
214
215 g.insert_arc(s, t, 10);
216 g.insert_arc(s, t, 3);
217 g.insert_arc(s, t, 7);
218
219 // Some graph types in Aleph-w behave as simple graphs and may collapse
220 // parallel arcs. If so, skip this multigraph-specific expectation.
221 if (const auto arcs_after = g.get_num_arcs(); arcs_after - arcs_before < 3)
222 GTEST_SKIP() << "Graph type does not appear to support parallel arcs (multigraph).";
223
224 // Important: for multigraphs, use an iterator that visits all arcs.
225 // Some iterators may collapse parallel arcs by target node.
227 Path<DGraph> path(g);
228 const int dist = dij(g, s, t, path);
229
230 EXPECT_EQ(dist, 3);
231 EXPECT_FALSE(path.is_empty());
232}
233
235{
236 DGraph g;
237 auto *n0 = g.insert_node(0);
238 auto *n1 = g.insert_node(1);
239 auto *n2 = g.insert_node(2);
240 auto *n3 = g.insert_node(3);
241
242 // No negative cycle: best path 0->1->2->3 = -2 + 3 + -1 = 0
243 g.insert_arc(n0, n1, -2);
244 g.insert_arc(n1, n2, 3);
245 g.insert_arc(n2, n3, -1);
246 g.insert_arc(n0, n3, 5);
247
249 const bool neg = bf.paint_spanning_tree(n0);
251
252 Path<DGraph> path(g);
253 const int dist = bf.get_min_path(n3, path);
254 EXPECT_EQ(dist, 0);
255 EXPECT_FALSE(path.is_empty());
256}
257
259{
260 // Keep these sizes modest; this is a correctness test, not a perf test.
261 const size_t N = 200;
262
263 // Sparse: ~4N arcs
264 {
265 InitNodeNoop init_node;
266 InitArcRandNonNegative init_arc(1234, 20);
267
269 777, init_node, init_arc);
270
271 auto g = gen(N, size_t(4 * N), false /* strongly connected? */);
272
273 auto [s, t] = pick_two_distinct_nodes(g, 999);
275 }
276
277 // Dense: probability p.
278 {
279 InitNodeNoop init_node;
280 InitArcRandNonNegative init_arc(4321, 20);
281
283 888, init_node, init_arc);
284
285 auto g = gen(N, 0.08, false /* strongly connected? */);
286
287 auto [s, t] = pick_two_distinct_nodes(g, 1001);
289 }
290}
291
293{
294 DGraph g;
295 auto *a = g.insert_node(1);
296 auto *b = g.insert_node(2);
297
298 // Parallel arcs alone do not create a cycle.
299 g.insert_arc(a, b, 1);
300 g.insert_arc(a, b, 2);
301
303 EXPECT_FALSE(tarjan.has_cycle(g));
304
305 // Add one back edge: now there is a cycle.
306 g.insert_arc(b, a, 0);
308 EXPECT_TRUE(tarjan2.has_cycle(g));
309}
Bellman-Ford algorithm for single-source shortest paths.
Dijkstra's shortest path algorithm.
Tarjan's algorithm for strongly connected components.
Bellman-Ford algorithm for shortest paths with negative weights.
auto get_current_arc() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tpl_graph.H:1666
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3848
typename BaseGraph::Arc Arc
Definition graph-dry.H:3852
typename BaseGraph::Node Node
Definition graph-dry.H:3851
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
Definition Dijkstra.H:101
constexpr bool is_empty() const noexcept
Return true if list is empty.
Definition htlist.H:523
Filtered iterator for outcoming arcs of a node.
Definition tpl_graph.H:1750
Digraph_Iterator< GT, __Out_Filt< GT > > Base
Definition tpl_graph.H:1751
Path on a graph.
Definition tpl_graph.H:2669
bool is_empty() const noexcept
Return true if the path is empty.
Definition tpl_graph.H:2813
Random directed graph (digraph) generator.
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
Definition Tarjan.H:171
#define TEST(name)
static mt19937 rng
#define N
Definition fib.C:294
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Random graph generation utilities.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1000
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1119
ArcHeap< G, D, A > Heap
Definition astar_test.cc:60
DGT::Node DNode
Generic graph and digraph implementations.