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 noexcept
 Get the array of graph nodes.
 
Nodeselect_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< GTget_min_path (const long src_idx, const long tgt_idx) const
 Reconstruct the shortest path between two nodes by matrix indices.
 
Path< GTget_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
 
GTg
 
const long n
 
const Distance_Type Inf
 
bool negative_cycle = false
 
DynMatrix< longpath_mat
 
DynMatrix< Distance_Typedist
 
SAsa
 

Detailed Description

template<class GT, class Distance = Dft_Dist<GT>, class 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 149 of file Floyd_Warshall.H.

Member Typedef Documentation

◆ Arc

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

Definition at line 152 of file Floyd_Warshall.H.

◆ Distance_Type

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

Definition at line 153 of file Floyd_Warshall.H.

◆ Node

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

Definition at line 151 of file Floyd_Warshall.H.

Constructor & Destructor Documentation

◆ Floyd_All_Shortest_Paths() [1/2]

template<class GT , class Distance = Dft_Dist<GT>, class 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 242 of file Floyd_Warshall.H.

References Aleph::DynArray< T >::access(), ah_invalid_argument_if, Aleph::DynMatrix< T >::allocate(), arcs, 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, Aleph::maps(), 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<class GT , class Distance = Dft_Dist<GT>, class 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 351 of file Floyd_Warshall.H.

Member Function Documentation

◆ entry()

template<class GT , class Distance = Dft_Dist<GT>, class 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 360 of file Floyd_Warshall.H.

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

◆ get_dist_mat()

template<class GT , class Distance = Dft_Dist<GT>, class 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 190 of file Floyd_Warshall.H.

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

◆ get_min_path() [1/2]

template<class GT , class Distance = Dft_Dist<GT>, class 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 405 of file Floyd_Warshall.H.

References ah_out_of_range_error_if, Aleph::Path< GT >::append_directed(), 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, Aleph::maps(), 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<class GT , class Distance = Dft_Dist<GT>, class 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 446 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<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
DynArray< Node * > Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_nodes ( ) const
inlinenoexcept

Get the array of graph nodes.

Returns the sorted array of node pointers used for matrix indexing.

Returns
Const copy of the node pointer array.

Definition at line 202 of file Floyd_Warshall.H.

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

◆ get_path_mat()

template<class GT , class Distance = Dft_Dist<GT>, class 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 180 of file Floyd_Warshall.H.

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

◆ has_negative_cycle()

template<class GT , class Distance = Dft_Dist<GT>, class 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 170 of file Floyd_Warshall.H.

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

◆ index_node()

template<class GT , class Distance = Dft_Dist<GT>, class 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 220 of file Floyd_Warshall.H.

References ah_domain_error_if, ah_invalid_argument_if, Aleph::binary_search(), Aleph::maps(), 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<class GT , class Distance = Dft_Dist<GT>, class 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 378 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<class GT , class Distance = Dft_Dist<GT>, class 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 210 of file Floyd_Warshall.H.

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

Member Data Documentation

◆ dist

◆ g

◆ Inf

◆ n

◆ negative_cycle

◆ nodes

◆ path_mat

◆ sa

template<class GT , class Distance = Dft_Dist<GT>, class 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: