Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
negative_cycles_test_support.H
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
44# ifndef NEGATIVE_CYCLES_TEST_SUPPORT_H
45# define NEGATIVE_CYCLES_TEST_SUPPORT_H
46
47# include <cmath>
48# include <cstddef>
49# include <tuple>
50# include <vector>
51
52# include <tpl_agraph.H>
53# include <tpl_graph.H>
54
56{
57 using namespace Aleph;
58
64 using Arc = Graph::Arc;
65 using Edge_Def = std::tuple<size_t, size_t, long long>;
66
67 template <class GT>
69 {
71 std::vector<typename GT::Node *> nodes;
72 std::vector<typename GT::Arc *> arcs;
73 };
74
75 template <class GT, typename Weight_Type>
77 build_graph_generic(const size_t n,
78 const std::vector<std::tuple<size_t, size_t, Weight_Type>> & edges)
79 {
81 built.nodes.reserve(n);
82 built.arcs.reserve(edges.size());
83
84 for (size_t i = 0; i < n; ++i)
85 built.nodes.push_back(built.g.insert_node(static_cast<int>(i)));
86
87 for (const auto & [u, v, w] : edges)
88 built.arcs.push_back(built.g.insert_arc(built.nodes[u], built.nodes[v], w));
89
90 return built;
91 }
92
93 inline Built_Graph_T<Graph> build_graph(const size_t n, const std::vector<Edge_Def> & edges)
94 {
96 }
97
98 struct Hide_Arc
99 {
100 Arc * blocked = nullptr;
101
102 bool operator()(Arc * arc) const noexcept
103 {
104 return arc != blocked;
105 }
106 };
107
108 // Counts how many times the distance accessor is called.
110 {
111 using Distance_Type = long long;
112 int * calls = nullptr;
113
115 {
116 ++*calls;
117 return arc->get_info();
118 }
119 };
120
121 // Counts how many times the filter is asked, and hides one arc.
123 {
124 int * calls = nullptr;
125 Arc * blocked = nullptr;
126
127 bool operator()(Arc * arc) const
128 {
129 ++*calls;
130 return arc != blocked;
131 }
132 };
133
134 // Rejects the arcs whose weight is not finite.
136 {
137 bool operator()(Float_Graph::Arc * arc) const noexcept
138 {
139 return std::isfinite(arc->get_info());
140 }
141 };
142
143 // Non-null cookies and control bits planted on a graph before a search,
144 // checked afterwards: the searches must not read, use or clear them.
145 template <class GT>
147 {
148 std::vector<int> cells;
149
151 {
152 cells.assign(built.nodes.size() + built.arcs.size(), 0);
153 size_t k = 0;
154 for (size_t i = 0; i < built.nodes.size(); ++i, ++k)
155 {
156 NODE_COOKIE(built.nodes[i]) = &cells[k];
157 NODE_BITS(built.nodes[i]).set_bit(Aleph::Spanning_Tree, i % 2 == 0);
158 NODE_BITS(built.nodes[i]).set_bit(Aleph::Find_Path, true);
159 }
160 for (size_t i = 0; i < built.arcs.size(); ++i, ++k)
161 {
162 ARC_COOKIE(built.arcs[i]) = &cells[k];
163 ARC_BITS(built.arcs[i]).set_bit(Aleph::Spanning_Tree, true);
164 ARC_BITS(built.arcs[i]).set_bit(Aleph::Find_Path, i % 2 == 1);
165 }
166 }
167
168 bool intact(const Built_Graph_T<GT> & built) const
169 {
170 size_t k = 0;
171 for (size_t i = 0; i < built.nodes.size(); ++i, ++k)
172 if (NODE_COOKIE(built.nodes[i]) != &cells[k]
173 or IS_NODE_VISITED(built.nodes[i], Aleph::Spanning_Tree) != (i % 2 == 0)
175 return false;
176 for (size_t i = 0; i < built.arcs.size(); ++i, ++k)
177 if (ARC_COOKIE(built.arcs[i]) != &cells[k]
179 or IS_ARC_VISITED(built.arcs[i], Aleph::Find_Path) != (i % 2 == 1))
180 return false;
181 return true;
182 }
183 };
184} // namespace Negative_Cycles_Test_Support
185
186# endif // NEGATIVE_CYCLES_TEST_SUPPORT_H
long double w
Definition btreepic.C:153
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
typename BaseGraph::Arc Arc
Definition graph-dry.H:4381
typename BaseGraph::Node Node
Definition graph-dry.H:4380
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:439
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
#define ARC_COOKIE(p)
Return the arc cookie
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define ARC_BITS(p)
Return the control bits of arc p.
#define NODE_COOKIE(p)
Return the node cookie
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
#define NODE_BITS(p)
Get the control bits of a node.
@ Spanning_Tree
Definition aleph-graph.H:79
@ Find_Path
Definition aleph-graph.H:76
Main namespace for Aleph-w library functions.
std::tuple< size_t, size_t, long long > Edge_Def
Built_Graph_T< GT > build_graph_generic(const size_t n, const std::vector< std::tuple< size_t, size_t, Weight_Type > > &edges)
Built_Graph_T< Graph > build_graph(const size_t n, const std::vector< Edge_Def > &edges)
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
bool operator()(Float_Graph::Arc *arc) const noexcept
bool intact(const Built_Graph_T< GT > &built) const
static int * k
Array-based graph implementation.
Generic graph and digraph implementations.