95# ifndef FLOYD_WARSHALL_H
96# define FLOYD_WARSHALL_H
100# include <type_traits>
164 template <AlephGraph
GT,
189 if constexpr (std::is_invocable_v<const Distance &, Arc *>)
200 Arc *lightest =
nullptr;
202 if (it.get_tgt_node_ne() == q
and
203 (lightest ==
nullptr or d(it.get_current_arc_ne()) < d(lightest)))
204 lightest = it.get_current_arc_ne();
206 assert(lightest !=
nullptr);
216 for (
long i = 0; i <
n; ++i)
330 <<
"Floyd_All_Shortest_Paths::index_node() node not found";
361 std::enable_if_t<std::is_convertible_v<D, Distance>,
int> = 0>
363 :
g(const_cast<
GT &>(__g)),
n(
g.get_num_nodes()),
372 for (
typename GT::Node_Iterator it(
g); it.has_curr(); it.next_ne())
378 for (
long i = 0; i <
n; ++i)
379 for (
long j = 0; j <
n; ++j)
390 Arc *arc = it.get_curr_ne();
399 if (not directed
and w <
dist(j, i))
417 for (
long i = 0; i <
n; ++i)
420 for (
long j = 0; j <
n; ++j)
434 if constexpr (std::numeric_limits<Distance_Type>::is_integer)
436 if constexpr (std::numeric_limits<Distance_Type>::is_signed)
438 const Distance_Type maxv = std::numeric_limits<Distance_Type>::max();
439 const Distance_Type minv = std::numeric_limits<Distance_Type>::lowest();
441 can_add = (dik <= maxv - dkj);
444 <<
"Floyd_All_Shortest_Paths: a distance is below "
445 <<
"the range of the distance type";
449 can_add = (dik <= std::numeric_limits<Distance_Type>::max() - dkj);
459 dist(i, j) = new_dist;
515 noexcept(std::is_nothrow_move_constructible_v<SA>
516 and std::is_nothrow_move_constructible_v<Distance>
517 and std::is_nothrow_copy_constructible_v<Distance_Type>)
535 std::stringstream ss;
552 for (
int i = 0; i <
n; ++i)
554 for (
int j = 0; j <
n; ++j)
581 <<
"Source index out of range";
583 <<
"Target index out of range";
585 <<
"Floyd_All_Shortest_Paths::get_min_path(): the graph has a "
588 if (
dist(src_idx, tgt_idx) ==
Inf)
591 auto src =
nodes(src_idx);
594 if (src_idx == tgt_idx)
635 template <AlephGraph GT, ArcDistance<GT> Distance,
636 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
Standard functor implementations and comparison objects.
High-level sorting functions for Aleph containers.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Default distance accessor for arc weights.
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Dynamic matrix with sparse storage.
void allocate()
Pre-allocate memory for the entire matrix.
constexpr size_t rows() const noexcept
Get the number of rows.
T & access(const size_t i, const size_t j)
Direct access to an allocated entry.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g ...
Floyd_All_Shortest_Paths(const GT &__g, D &&__distance, SA __sa=SA())
Construct Floyd-Warshall solver for all-pairs shortest paths.
DynMatrix< Distance_Type > dist
DynArray< Node * > get_nodes() const
Get an owning copy of the graph nodes array.
DynArray< Node * > get_nodes_copy() const
Get an owning copy of the graph nodes array.
Path< GT > get_min_path(const long src_idx, const long tgt_idx) const
Reconstruct the shortest path between two nodes by matrix indices.
DynMatrix< long > path_mat
const DynMatrix< Distance_Type > & get_dist_mat() const noexcept
Get the distance matrix.
Node * select_node(long i) const noexcept
Get the graph node at a given matrix index.
constexpr bool has_negative_cycle() const noexcept
Check if the graph contains a negative cycle.
void append_lightest_arc(Path< GT > &path, Node *p, Node *q, D &d) const
Floyd_All_Shortest_Paths(const GT &, Distance, SA=SA()) -> Floyd_All_Shortest_Paths< GT, Distance, SA >
The type of a distance given to the constructor is deduced from it: that of the constructor is not,...
decltype(auto) path_distance() const
Floyd_All_Shortest_Paths(const Floyd_All_Shortest_Paths &other)=default
Copy the matrices of another solver, on the same graph.
std::string entry(const Distance_Type &e) const
Convert a distance value to string representation.
Path< GT > get_min_path(typename GT::Node *src, typename GT::Node *tgt) const
Reconstruct the shortest path between two nodes.
Floyd_All_Shortest_Paths(Floyd_All_Shortest_Paths &&other) noexcept(std::is_nothrow_move_constructible_v< SA > and std::is_nothrow_move_constructible_v< Distance > and std::is_nothrow_copy_constructible_v< Distance_Type >)
Take the matrices of another solver, which is left without nodes, on the same graph.
Distance::Distance_Type Distance_Type
const DynArray< Node * > & get_nodes_ref() const noexcept
Get the array of graph nodes as a constant reference.
Floyd_All_Shortest_Paths(const GT &g, SA &&sa=SA())
Construct Floyd-Warshall solver with rvalue arc filter and a distance accessor built by Distance().
Floyd_All_Shortest_Paths(const GT &__g, SA &__sa)
Construct Floyd-Warshall solver with a distance accessor built by Distance().
bool has_negative_diagonal() const
const DynMatrix< long > & get_path_mat() const noexcept
Get the path reconstruction matrix.
long index_node(Node *p) const
Return the adjacency-matrix index corresponding to node p.
static void print(const DynMatrix< Distance_Type > &dist)
Print a distance matrix to standard output.
void append(Arc *arc)
Append an arc to the path.
void append_directed(Node *p)
Append a node to a directed path.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
bool is_digraph() const noexcept
Return true if the graph this is directed.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
and
Check uniqueness with explicit hash + equality functors.
bool binary_search(Itor beg, Itor end, const T &value)
Binary search for a value.
Filtered iterator on all the arcs of a graph.
Default filter for filtered iterators on arcs.
Filtered iterator of adjacent arcs of a node.
Dynamic matrix with lazy allocation.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
Arc indexing for fast lookup by endpoint nodes.