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

Johnson's algorithm for all-pairs shortest paths. More...

#include <Johnson.H>

Collaboration diagram for Aleph::Johnson< GT, Distance, Ait, NAit, SA >:
[legend]

Classes

struct  Reweighted_Dist
 

Public Types

using Node = typename GT::Node
 
using Arc = typename GT::Arc
 
using Distance_Type = typename Distance::Distance_Type
 

Public Member Functions

 Johnson (const GT &g, Distance d=Distance(), SA __sa=SA())
 Construct a Johnson's all-pairs shortest paths executor.
 
 Johnson ()=default
 Default constructor (uninitialized state)
 
bool is_initialized () const noexcept
 Check if the algorithm has been initialized.
 
Distance_Type get_distance (Node *src, Node *tgt)
 Get the shortest distance from source to target.
 
Path< GTfind_path (Node *src, Node *tgt)
 Find the shortest path from source to target.
 
DynMapTree< std::pair< Node *, Node * >, Distance_Typecompute_all_pairs_distances ()
 Compute the shortest distance for all pairs.
 
Distance_Type get_potential (Node *node) const
 Get the node potential (h value) for a node.
 
GTget_reweighted_graph () noexcept
 Get access to the internal reweighted graph.
 
const GTget_reweighted_graph () const noexcept
 Get const access to the internal reweighted graph.
 
Nodeget_copy_node (Node *orig) const
 Get the copy of a node in the reweighted graph.
 

Private Member Functions

void reweight_arcs ()
 Reweight all arcs using node potentials.
 
void build_reverse_mappings (const GT &orig)
 
Path< GTmap_copy_path_to_original (const Path< GT > &copy_path) const
 

Static Private Member Functions

static Distance_Type checked_add (const Distance_Type &a, const Distance_Type &b)
 
static Distance_Type checked_sub (const Distance_Type &a, const Distance_Type &b)
 

Private Attributes

const GTorig_graph = nullptr
 
GraphCopyWithMapping< GTgraph_copy
 Copy of graph with node mapping.
 
DynMapTree< Node *, Node * > copy_to_orig_node
 
DynMapTree< Arc *, Arc * > copy_to_orig_arc
 
DynMapTree< Node *, Distance_Typeh
 Node potentials from Bellman-Ford.
 
Distance dist
 
SA sa
 
bool initialized = false
 

Detailed Description

template<class GT, class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
class Aleph::Johnson< GT, Distance, Ait, NAit, SA >

Johnson's algorithm for all-pairs shortest paths.

This class implements Johnson's algorithm for computing shortest paths between all pairs of vertices in a weighted directed graph. It handles graphs with negative edge weights (but not negative cycles).

The algorithm is efficient for sparse graphs, with complexity O(V² log V + VE), compared to Floyd-Warshall's O(V³).

Template Parameters
GTGraph type (typically List_Digraph).
DistanceArc weight accessor class. Must provide:
  • Distance_Type: The numeric type for weights
  • operator()(Arc*): Returns the weight of an arc
AitArc iterator template for traversing all arcs.
NAitNode arc iterator template for outgoing arcs.
SAArc filter for internal iterators.

Usage example:

// ... build graph with possibly negative edges ...
try {
// Query shortest path between any pair
auto path = johnson.find_path(src, tgt);
auto dist = johnson.get_distance(src, tgt);
}
catch (const std::domain_error& e) {
// Graph has a negative cycle
}
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3848
Johnson's algorithm for all-pairs shortest paths.
Definition Johnson.H:125
Distance dist
Definition Johnson.H:137
DynList< T > maps(const C &c, Op op)
Classic map operation.
Warning
The graph must be a digraph. For undirected graphs, convert to digraph first (each edge becomes two arcs).
See also
Bellman_Ford Dijkstra_Min_Paths Floyd_All_Shortest_Paths

Definition at line 124 of file Johnson.H.

Member Typedef Documentation

◆ Arc

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
using Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Arc = typename GT::Arc

Definition at line 128 of file Johnson.H.

◆ Distance_Type

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
using Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Distance_Type = typename Distance::Distance_Type

Definition at line 129 of file Johnson.H.

◆ Node

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
using Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Node = typename GT::Node

Definition at line 127 of file Johnson.H.

Constructor & Destructor Documentation

◆ Johnson() [1/2]

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Johnson ( const GT g,
Distance  d = Distance(),
SA  __sa = SA() 
)
inlineexplicit

Construct a Johnson's all-pairs shortest paths executor.

The constructor:

  1. Copies the input graph (preserving node mapping)
  2. Runs Bellman-Ford to compute node potentials
  3. Reweights all edges to be non-negative
Parameters
[in]gThe graph to operate on.
[in]dArc-weight accessor (default constructed).
[in]__saArc filter for internal iterators (default constructed).
Exceptions
std::bad_allocif there is not enough memory.
std::domain_errorif a negative cycle is detected.

Definition at line 287 of file Johnson.H.

References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::build_reverse_mappings(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::dist, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::h, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized, Aleph::maps(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::reweight_arcs(), and Aleph::Johnson< GT, Distance, Ait, NAit, SA >::sa.

◆ Johnson() [2/2]

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Johnson ( )
default

Default constructor (uninitialized state)

Member Function Documentation

◆ build_reverse_mappings()

◆ checked_add()

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
static Distance_Type Aleph::Johnson< GT, Distance, Ait, NAit, SA >::checked_add ( const Distance_Type a,
const Distance_Type b 
)
inlinestaticprivate

◆ checked_sub()

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
static Distance_Type Aleph::Johnson< GT, Distance, Ait, NAit, SA >::checked_sub ( const Distance_Type a,
const Distance_Type b 
)
inlinestaticprivate

◆ compute_all_pairs_distances()

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
DynMapTree< std::pair< Node *, Node * >, Distance_Type > Aleph::Johnson< GT, Distance, Ait, NAit, SA >::compute_all_pairs_distances ( )
inline

◆ find_path()

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
Path< GT > Aleph::Johnson< GT, Distance, Ait, NAit, SA >::find_path ( Node src,
Node tgt 
)
inline

Find the shortest path from source to target.

Parameters
[in]srcSource node (from original graph).
[in]tgtTarget node (from original graph).
Returns
Path in the original graph from src to tgt, or an empty path if unreachable.
Exceptions
std::domain_errorif not initialized or nodes not found.

Definition at line 360 of file Johnson.H.

References ah_domain_error_if, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized, IS_NODE_VISITED, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::map_copy_path_to_original(), Aleph::maps(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::orig_graph, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::sa, and Aleph::Spanning_Tree.

◆ get_copy_node()

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
Node * Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_copy_node ( Node orig) const
inline

Get the copy of a node in the reweighted graph.

Parameters
[in]origOriginal node.
Returns
Corresponding node in the reweighted graph.

Definition at line 459 of file Johnson.H.

References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, and Aleph::maps().

◆ get_distance()

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
Distance_Type Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_distance ( Node src,
Node tgt 
)
inline

Get the shortest distance from source to target.

Parameters
[in]srcSource node (from original graph).
[in]tgtTarget node (from original graph).
Returns
The shortest distance from src to tgt, or infinity if unreachable.
Exceptions
std::domain_errorif not initialized or nodes not found.

Definition at line 320 of file Johnson.H.

References ah_domain_error_if, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::h, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized, IS_NODE_VISITED, Aleph::maps(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::sa, and Aleph::Spanning_Tree.

◆ get_potential()

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
Distance_Type Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_potential ( Node node) const
inline

Get the node potential (h value) for a node.

The potential h(v) represents the shortest distance from the dummy source to v, computed by Bellman-Ford.

Parameters
[in]nodeNode from the original graph.
Returns
The potential h(node).
Exceptions
std::domain_errorif not initialized or node not found.

Definition at line 435 of file Johnson.H.

References ah_domain_error_if, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::h, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized, and Aleph::maps().

◆ get_reweighted_graph() [1/2]

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
const GT & Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_reweighted_graph ( ) const
inlinenoexcept

Get const access to the internal reweighted graph.

Definition at line 452 of file Johnson.H.

References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy.

◆ get_reweighted_graph() [2/2]

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
GT & Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_reweighted_graph ( )
inlinenoexcept

Get access to the internal reweighted graph.

Useful for debugging or advanced operations.

Returns
Reference to the internal graph copy.

Definition at line 448 of file Johnson.H.

References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy.

◆ is_initialized()

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
bool Aleph::Johnson< GT, Distance, Ait, NAit, SA >::is_initialized ( ) const
inlinenoexcept

Check if the algorithm has been initialized.

Returns
true if ready for queries, false otherwise.

Definition at line 311 of file Johnson.H.

References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized.

◆ map_copy_path_to_original()

◆ reweight_arcs()

Member Data Documentation

◆ copy_to_orig_arc

◆ copy_to_orig_node

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
DynMapTree<Node*, Node*> Aleph::Johnson< GT, Distance, Ait, NAit, SA >::copy_to_orig_node
private

◆ dist

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
Distance Aleph::Johnson< GT, Distance, Ait, NAit, SA >::dist
private

◆ graph_copy

◆ h

◆ initialized

◆ orig_graph

template<class GT , class Distance = Dft_Dist<GT>, template< class, class > class Ait = Arc_Iterator, template< class, class > class NAit = Node_Arc_Iterator, class SA = Dft_Show_Arc<GT>>
const GT* Aleph::Johnson< GT, Distance, Ait, NAit, SA >::orig_graph = nullptr
private

◆ sa


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