|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Johnson all-pairs shortest paths in Aleph-w (negative weights, Floyd comparison, benchmark flags). More...
#include <iostream>#include <iomanip>#include <string>#include <vector>#include <limits>#include <chrono>#include <cmath>#include <random>#include <unordered_set>#include <cstring>#include <tpl_graph.H>#include <Johnson.H>#include <Floyd_Warshall.H>Go to the source code of this file.
Classes | |
| struct | Distance |
| Distance accessor. More... | |
Typedefs | |
| using | WeightedDigraph = List_Digraph< Graph_Node< string >, Graph_Arc< double > > |
| using | Node = WeightedDigraph::Node |
| using | Arc = WeightedDigraph::Arc |
Functions | |
| template<typename Func > | |
| double | measure_ms (Func &&f) |
| static void | usage (const char *prog) |
| static WeightedDigraph | build_random_graph (int n, int e, unsigned seed) |
| static void | compare_with_floyd (WeightedDigraph &g) |
| Node * | find_node (WeightedDigraph &g, const string &name) |
| void | print_graph (WeightedDigraph &g) |
| void | example_basic_all_pairs () |
| void | example_negative_cycle () |
| void | example_reweighting () |
| void | example_complexity_comparison () |
| void | example_currency_arbitrage () |
| int | main (int argc, char *argv[]) |
Johnson all-pairs shortest paths in Aleph-w (negative weights, Floyd comparison, benchmark flags).
This example demonstrates Johnson's algorithm for all-pairs shortest paths (APSP) on a weighted directed graph.
Johnson is especially useful when:
Johnson combines Bellman-Ford (for reweighting) and Dijkstra (for efficient shortest paths once all weights are non-negative).
WeightedDigraph = List_Digraph<Graph_Node<string>, Graph_Arc<double>>string)double)Notes:
-n without -e, the program uses e = 5*n.[1, 10].q and connect q -> v with weight 0 for all vertices v.q to compute potentials h(v).Run Dijkstra as needed on the reweighted graph.Convert distances back: -d(u,v) = d'(u,v) - h(u) + h(v)`In Aleph-w, constructing Johnson<...> performs the Bellman-Ford step and reweighting internally.
Let V be the number of nodes and E the number of arcs.
O(V*E + V*(E+V) log V) with a binary heap.johnson.get_distance(src,tgt) for every pair, which is significantly more expensive if each get_distance() triggers a Dijkstra run.-n/-e)** intentionally performs only a single sample query to avoid an O(V^2) all-pairs loop.Johnson<...> construction throws std::domain_error.O(V^3)) can be competitive or simpler to reason about.get_distance(src,tgt) for all pairs on large graphs.Johnson.H (implementation)Bellman_Ford.H (used internally by Johnson)Dijkstra.H / dijkstra_example.cc (used after reweighting)Floyd_Warshall.H / write_floyd.C (APSP alternative)Definition in file johnson_example.cc.
| using Arc = WeightedDigraph::Arc |
Definition at line 152 of file johnson_example.cc.
| using Node = WeightedDigraph::Node |
Definition at line 151 of file johnson_example.cc.
| using WeightedDigraph = List_Digraph<Graph_Node<string>, Graph_Arc<double> > |
Definition at line 150 of file johnson_example.cc.
|
static |
Definition at line 174 of file johnson_example.cc.
References StlAlephIterator< SetName >::end(), Aleph::DynList< T >::insert(), Aleph::maps(), nodes, rng, and Aleph::to_string().
|
static |
Definition at line 216 of file johnson_example.cc.
References Aleph::maps(), measure_ms(), and nodes.
| void example_basic_all_pairs | ( | ) |
Definition at line 323 of file johnson_example.cc.
References h, Aleph::maps(), nodes, and print_graph().
| void example_complexity_comparison | ( | ) |
Definition at line 510 of file johnson_example.cc.
| void example_currency_arbitrage | ( | ) |
Definition at line 523 of file johnson_example.cc.
| void example_negative_cycle | ( | ) |
Definition at line 415 of file johnson_example.cc.
References Aleph::maps(), and print_graph().
| void example_reweighting | ( | ) |
Definition at line 466 of file johnson_example.cc.
References Aleph::maps().
| Node * find_node | ( | WeightedDigraph & | g, |
| const string & | name | ||
| ) |
Definition at line 279 of file johnson_example.cc.
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 568 of file johnson_example.cc.
| double measure_ms | ( | Func && | f | ) |
Definition at line 311 of file johnson_example.cc.
Referenced by compare_with_floyd().
| void print_graph | ( | WeightedDigraph & | g | ) |
Definition at line 287 of file johnson_example.cc.
References Aleph::maps().
Referenced by example_basic_all_pairs(), and example_negative_cycle().
|
static |
Definition at line 166 of file johnson_example.cc.
References Aleph::maps().