23namespace shortest_path {
45 typename F = std::function<void(
const TNode &,
46 const typename TGraph::edge_type::weight_type &)> >
51 F f_user = [](
const TNode &,
52 const typename TGraph::edge_type::weight_type &) ->
void {});
72 template<
typename TGraph,
typename TNode,
typename F = std::function<void(
const TNode &,
73 const typename TGraph::edge_type::weight_type &)>>
79 F f_user = [](
const TNode &,
80 const typename TGraph::edge_type::weight_type &) {});
82 template<
typename TGraph,
typename TNode>
94 template<
typename TNode,
typename TWeight>
104 template<
typename TGraph,
typename TNode,
typename F>
106 Data<TNode, typename TGraph::edge_type::weight_type>
107 impl(
const TGraph &
graph,
113 template<
typename TGraph,
typename TNode,
typename F>
116 relaxation(
const TGraph &
graph,
118 BellmanFord::Data<TNode, typename TGraph::edge_type::weight_type> &data,
125 template<
typename TNode,
typename TWeight>
126 inline static void init(BellmanFord::Data<TNode, TWeight> &data,
const TNode &
start);
134template<
typename TGraph,
typename TNode,
typename F>
141template<
typename TGraph,
typename TNode,
typename F>
147 using weight_type =
typename TGraph::edge_type::weight_type;
149 Data<TNode, weight_type> data = impl(
graph,
start, f_user);
151 if (data.g.find(goal) == data.g.end()) {
160template<
typename TGraph,
typename TNode,
typename F>
161BellmanFord::Data<TNode, typename TGraph::edge_type::weight_type>
162BellmanFord::impl(
const TGraph &
graph,
const TNode &
start, F f_user) {
164 using weight_type =
typename TGraph::edge_type::weight_type;
166 Data<TNode, weight_type> data;
174 auto nodes =
graph.getNodes();
175 size_t nodes_cnt = nodes.size();
177 for (
size_t i = 1;
i < nodes_cnt; ++
i) {
180 relaxation(
graph, nodes, data, data.state1, data.state2, f_user);
183 relaxation(
graph, nodes, data, data.state2, data.state1, f_user);
186 if (((
i % 2 == 1) && data.state2.empty()) ||
187 ((
i % 2 == 0) && data.state1.empty()))
191 for (
const auto &n: nodes) {
192 for (
const auto &s_edge:
graph.successorEdges(n)) {
195 auto search = data.g.find(s);
197 if (search != data.g.end() && data.g.at(n) + s_edge.weight() < data.g.at(s)) {
198 throw std::out_of_range(
"BellmanFord: Detected negative weight cycle.");
208template<
typename TGraph,
typename TNode,
typename F>
209void BellmanFord::relaxation(
const TGraph &
graph,
211 BellmanFord::Data<TNode, typename TGraph::edge_type::weight_type> &data,
215 using weight_type =
typename TGraph::edge_type::weight_type;
217 for (
const auto &n: nodes) {
219 if (state1.find(n) == state1.
end()) {
223 for (
const auto &s_edge:
graph.successorEdges(n)) {
226 auto search = data.g.find(s);
229 if (search == data.g.end() || data.g.at(n) + s_edge.weight() < data.g.at(s)) {
230 weight_type gscore = data.g.at(n) + s_edge.weight();
236 data.p.insert_or_assign(s, n);
248template<
typename TNode,
typename TWeight>
249void BellmanFord::init(BellmanFord::Data<TNode, TWeight> &data,
const TNode &
start) {
252 data.state1.insert(
start);
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
static ext::vector< TNode > reconstructPath(const ext::map< TNode, TNode > &p, const TNode &start, const TNode &goal)
Definition: ReconstructPath.hpp:68
static TNode const & other(const TEdge &e, const TNode &n)
Definition: BellmanFord.hpp:25
static ext::pair< ext::vector< TNode >, typename TGraph::edge_type::weight_type > findPathRegistration(const TGraph &graph, const TNode &start, const TNode &goal)
Definition: BellmanFord.hpp:85
static ext::pair< ext::vector< TNode >, typename TGraph::edge_type::weight_type > findPath(const TGraph &graph, const TNode &start, const TNode &goal, F f_user=[](const TNode &, const typename TGraph::edge_type::weight_type &) {})
Definition: BellmanFord.hpp:143
static void run(const TGraph &graph, const TNode &start, F f_user=[](const TNode &, const typename TGraph::edge_type::weight_type &) -> void {})
Definition: BellmanFord.hpp:135
int i
Definition: AllEpsilonClosure.h:118
constexpr const T & max(const T &a)
Root case of maximum computation. The maximum from one value is the value itself.
Definition: algorithm.hpp:278
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ReconstructPath.hpp:14
void start(measurements::stealth_string name, measurements::Type type)
Definition: measurements.cpp:14