Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA > Class Template Reference

Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g using the Floyd-Warshall algorithm. More...

#include <Floyd_Warshall.H>

Collaboration diagram for Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >:
[legend]

Public Member Functions

constexpr bool has_negative_cycle () const noexcept
 Check if the graph contains a negative cycle.
 
const DynMatrix< long > & get_path_mat () const noexcept
 Get the path reconstruction matrix.
 
const DynMatrix< Distance_Type > & get_dist_mat () const noexcept
 Get the distance matrix.
 
DynArray< Node * > get_nodes () const
 Get an owning copy of the graph nodes array.
 
const DynArray< Node * > & get_nodes_ref () const noexcept
 Get the array of graph nodes as a constant reference.
 
DynArray< Node * > get_nodes_copy () const
 Get an owning copy of the graph nodes array.
 
Node * select_node (long i) const noexcept
 Get the graph node at a given matrix index.
 
long index_node (Node *p) const
 Return the adjacency-matrix index corresponding to node p.
 
 Floyd_All_Shortest_Paths (const GT &__g, SA &__sa)
 Construct Floyd-Warshall solver for all-pairs shortest paths.
 
 Floyd_All_Shortest_Paths (const GT &g, SA &&sa=SA())
 Construct Floyd-Warshall solver with rvalue arc filter.
 
std::string entry (const Distance_Type &e) const
 Convert a distance value to string representation.
 
Path< GT > get_min_path (const long src_idx, const long tgt_idx) const
 Reconstruct the shortest path between two nodes by matrix indices.
 
Path< GT > get_min_path (typename GT::Node *src, typename GT::Node *tgt) const
 Reconstruct the shortest path between two nodes.
 

Static Public Member Functions

static void print (const DynMatrix< Distance_Type > &dist)
 Print a distance matrix to standard output.
 

Private Types

typedef GT::Node Node
 
typedef GT::Arc Arc
 
typedef Distance::Distance_Type Distance_Type
 

Private Attributes

DynArray< Node * > nodes
 
GT & g
 
const long n
 
const Distance_Type Inf
 
bool negative_cycle = false
 
DynMatrix< long > path_mat
 
DynMatrix< Distance_Type > dist
 
SA & sa
 

Detailed Description

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
class Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >

Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g using the Floyd-Warshall algorithm.

This class computes two matrices:

  1. dist: matrix of shortest-path costs between all node pairs. Each entry dist(i,j) stores the minimum total cost to go from the node with index i to the node with index j.
  2. path: path reconstruction matrix. Each entry path(i,j) stores the next node index on a shortest path from i to j. By iterating through path(k,j) you can reconstruct the path. The helper get_min_path() performs this reconstruction.

Floyd-Warshall supports negative edge weights, but it does not work correctly if the graph contains negative cycles. Use Bellman-Ford based algorithms if you suspect negative cycles.

Template parameters:

  1. GT: graph type.
  2. Distance<GT>: arc weight accessor that must provide:
    1. Distance<GT>::Distance_Type: the weight type.
    2. Distance_Type operator()(typename GT::Arc *a): returns the weight of arc a.
    3. Distance<GT>::Max_Distance: a sentinel maximum value that is treated as infinity.
    4. Distance<GT>::Zero_Distance: additive identity (usually 0).
  3. SA: arc filter.
See also
dijkstra_min_spanning_tree() dijkstra_min_path() find_path_depth_first() find_min_path() bellman_ford_min_spanning_tree() q_bellman_ford_min_spanning_tree()

Definition at line 152 of file Floyd_Warshall.H.

Member Typedef Documentation

◆ Arc

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
typedef GT::Arc Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Arc
private

Definition at line 155 of file Floyd_Warshall.H.

◆ Distance_Type

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
typedef Distance::Distance_Type Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Distance_Type
private

Definition at line 156 of file Floyd_Warshall.H.

◆ Node

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
typedef GT::Node Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Node
private

Definition at line 154 of file Floyd_Warshall.H.

Constructor & Destructor Documentation

◆ Floyd_All_Shortest_Paths() [1/2]

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Floyd_All_Shortest_Paths ( const GT &  __g,
SA &  __sa 
)
inline

Construct Floyd-Warshall solver for all-pairs shortest paths.

Computes shortest distances and path reconstruction matrices for all pairs of nodes in the graph using the Floyd-Warshall algorithm. Handles negative weights but detects negative cycles.

Parameters
[in]__gInput graph (will be treated as const).
[in]__saArc filter for considering which arcs to include.
Exceptions
std::invalid_argumentif graph has no nodes.
std::bad_allocif memory allocation fails.

Definition at line 287 of file Floyd_Warshall.H.

References Aleph::DynArray< T >::access(), ah_invalid_argument_if, Aleph::DynMatrix< T >::allocate(), arcs, Aleph::blossom_maximum_cardinality_matching(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::g, Aleph::in_place_sort(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Inf, k, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::n, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::negative_cycle, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::path_mat, Aleph::DynArray< T >::reserve(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::sa.

◆ Floyd_All_Shortest_Paths() [2/2]

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Floyd_All_Shortest_Paths ( const GT &  g,
SA &&  sa = SA() 
)
inline

Construct Floyd-Warshall solver with rvalue arc filter.

Parameters
[in]gInput graph.
[in]saArc filter (moved).

Definition at line 396 of file Floyd_Warshall.H.

Member Function Documentation

◆ entry()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
std::string Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::entry ( const Distance_Type &  e) const
inline

Convert a distance value to string representation.

Parameters
[in]eDistance value to convert.
Returns
String representation ("Inf" for infinite distances).

Definition at line 405 of file Floyd_Warshall.H.

References Aleph::blossom_maximum_cardinality_matching(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Inf.

◆ get_dist_mat()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const DynMatrix< Distance_Type > & Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_dist_mat ( ) const
inlinenoexcept

Get the distance matrix.

The distance matrix contains shortest distances between all pairs of nodes. Entry (i,j) contains the shortest distance from node i to node j.

Returns
Const reference to the distance matrix.

Definition at line 193 of file Floyd_Warshall.H.

References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist.

Referenced by compare_with_floyd().

◆ get_min_path() [1/2]

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Path< GT > Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path ( const long  src_idx,
const long  tgt_idx 
) const
inline

Reconstruct the shortest path between two nodes by matrix indices.

Uses the path reconstruction matrix to build the shortest path from source to target node.

Parameters
[in]src_idxMatrix index of source node.
[in]tgt_idxMatrix index of target node.
Returns
Path object containing the shortest path.
Exceptions
std::out_of_rangeif indices are invalid.

Definition at line 450 of file Floyd_Warshall.H.

References ah_out_of_range_error_if, Aleph::Path< GT >::append_directed(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::g, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Inf, k, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::n, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes, and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::path_mat.

Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path().

◆ get_min_path() [2/2]

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Path< GT > Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path ( typename GT::Node *  src,
typename GT::Node *  tgt 
) const
inline

Reconstruct the shortest path between two nodes.

Convenience method that accepts node pointers instead of indices.

Parameters
[in]srcPointer to source node.
[in]tgtPointer to target node.
Returns
Path object containing the shortest path.
Exceptions
std::invalid_argumentif either node pointer is null.
std::domain_errorif either node is not found in the graph.

Definition at line 491 of file Floyd_Warshall.H.

References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::index_node().

◆ get_nodes()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
DynArray< Node * > Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_nodes ( ) const
inline

Get an owning copy of the graph nodes array.

Returns a copy of the sorted array of node pointers. This method is equivalent to get_nodes_copy() and provides a safe snapshot for long-term storage or when the Floyd_All_Shortest_Paths instance may be destroyed.

Note
This method returns a copy of the DynArray container; the Node* elements are borrowed from the graph and caller does not gain ownership of the nodes themselves. Pointer validity depends on the graph nodes remaining alive.
Returns
Owning copy of the node pointer array container.
See also
get_nodes_copy() for the same functionality with an explicit name
get_nodes_ref() for a constant reference (no copy)

Definition at line 215 of file Floyd_Warshall.H.

References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_nodes_copy().

◆ get_nodes_copy()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
DynArray< Node * > Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_nodes_copy ( ) const
inline

Get an owning copy of the graph nodes array.

Returns a copy of the sorted array of node pointers. This is safer for long-term storage or if the Floyd_All_Shortest_Paths instance is destroyed, but incurs an O(n) copy cost.

Warning
This method returns a copy of the node array. Modifying the returned array does not affect the internal state of the Floyd_All_Shortest_Paths instance.
The pointers are valid as long as the received graph is alive.
Returns
Owning copy of the node pointer array.

Definition at line 247 of file Floyd_Warshall.H.

References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes.

Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_nodes().

◆ get_nodes_ref()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const DynArray< Node * > & Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_nodes_ref ( ) const
inlinenoexcept

Get the array of graph nodes as a constant reference.

Returns the sorted array of node pointers used for matrix indexing. This provides O(1) access to the underlying storage but requires the caller to ensure the Floyd_All_Shortest_Paths instance remains alive.

Warning
The returned reference and its contained pointers are valid only as long as the Floyd_All_Shortest_Paths instance and the underlying graph nodes exist. Not thread-safe if the container is mutated.
Returns
Const reference to the node pointer array.

Definition at line 230 of file Floyd_Warshall.H.

References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes.

◆ get_path_mat()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const DynMatrix< long > & Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_path_mat ( ) const
inlinenoexcept

Get the path reconstruction matrix.

The path matrix contains intermediate nodes for shortest path reconstruction. Entry (i,j) contains the next node index on the shortest path from i to j.

Returns
Const reference to the path reconstruction matrix.

Definition at line 183 of file Floyd_Warshall.H.

References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::path_mat.

◆ has_negative_cycle()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
constexpr bool Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::has_negative_cycle ( ) const
inlineconstexprnoexcept

Check if the graph contains a negative cycle.

Returns
true if a negative cycle was detected, false otherwise.

Definition at line 173 of file Floyd_Warshall.H.

References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::negative_cycle.

◆ index_node()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
long Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::index_node ( Node *  p) const
inline

Return the adjacency-matrix index corresponding to node p.

Find the matrix index corresponding to a graph node.

Parameters
[in]pPointer to the node to find.
Returns
Matrix index of the node.
Exceptions
std::domain_errorif the node is not found.

Definition at line 265 of file Floyd_Warshall.H.

References ah_domain_error_if, ah_invalid_argument_if, Aleph::binary_search(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes, and Aleph::DynArray< T >::size().

Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path().

◆ print()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
static void Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::print ( const DynMatrix< Distance_Type > &  dist)
inlinestatic

Print a distance matrix to standard output.

Prints the distance matrix in a readable format, showing "inf" for infinite distances and numeric values otherwise.

Parameters
[in]distDistance matrix to print.

Definition at line 423 of file Floyd_Warshall.H.

References Aleph::DynMatrix< T >::access(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Inf, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::n, and Aleph::DynMatrix< T >::rows().

◆ select_node()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::select_node ( long  i) const
inlinenoexcept

Get the graph node at a given matrix index.

Parameters
[in]iMatrix index (must be in range [0, n)).
Returns
Pointer to the node at index i.

Definition at line 255 of file Floyd_Warshall.H.

References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes.

Member Data Documentation

◆ dist

◆ g

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
GT& Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::g
private

◆ Inf

◆ n

◆ negative_cycle

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
bool Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::negative_cycle = false
private

◆ nodes

◆ path_mat

◆ sa

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
SA& Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::sa
private

The documentation for this class was generated from the following file: