Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
FloydWarshall.hpp
Go to the documentation of this file.
1
6// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved.
7
8#pragma once
9
10#include <functional>
11#include <alib/map>
12#include <alib/set>
13#include <limits>
14#include <stdexcept>
15
17
18namespace graph {
19
20namespace shortest_path {
21
23// ---------------------------------------------------------------------------------------------------------------------
24
25 public:
37 template<typename TGraph>
38 static
40 run(const TGraph &graph);
41
42// =====================================================================================================================
43
44};
45
46// =====================================================================================================================
47
48template<typename TGraph>
50FloydWarshall::run(const TGraph &graph) {
51 using weight_type = typename TGraph::edge_type::weight_type;
52 using node_type = typename TGraph::node_type;
53
55
56 ext::set<node_type> nodes = graph.getNodes();
57
58 // Init distances matrix
59 for (auto &first : nodes) {
61 for (auto &second: nodes) {
62 if (first == second) {
63 dist[first].insert(ext::make_pair(second, 0));
64 } else {
66 }
67 }
68 }
69
70 for (auto &first : nodes) {
71 for (auto &s_edge : graph.successorEdges(first)) {
72 const node_type &s = common::SupportFunction::other(s_edge, first); // successor
73 dist[first][s] = std::min(dist[s_edge.first][s_edge.second], s_edge.weight());
74 }
75 }
76
77 // Run FloydWarshall
78 for (auto &k : nodes) {
79 for (auto &i : nodes) {
80 if (dist[i][k] != std::numeric_limits<weight_type>::max()) { // Optimization
81 for (auto &j : nodes) {
82 if (dist[k][j] != std::numeric_limits<weight_type>::max()) { // Optimization
83 dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);
84 }
85 }
86 }
87 }
88 }
89
90 // Check for negative cycle
91 for (auto &n : nodes)
92 if (dist[n][n] < 0)
93 throw std::out_of_range("FloydWarshall: Detected negative weight cycle.");
94
95 return dist;
96}
97
98// ---------------------------------------------------------------------------------------------------------------------
99
100} // namespace shortest_path
101
102} // namespace graph
103
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
std::pair< iterator, bool > insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:118
Definition: set.hpp:44
static TNode const & other(const TEdge &e, const TNode &n)
Definition: FloydWarshall.hpp:22
static ext::map< typename TGraph::node_type, ext::map< typename TGraph::node_type, typename TGraph::edge_type::weight_type > > run(const TGraph &graph)
Definition: FloydWarshall.hpp:50
p second
Definition: ToRegExpAlgebraic.h:126
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 const T & min(const T &a)
Definition: algorithm.hpp:310
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ReconstructPath.hpp:14