|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Bellman-Ford shortest paths in Aleph-w (negative weights, negative-cycle detection, SPFA). More...
#include <iostream>#include <iomanip>#include <string>#include <vector>#include <chrono>#include <cstring>#include <tpl_graph.H>#include <Bellman_Ford.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 | |
| Node * | find_node (WeightedDigraph &g, const string &name) |
| void | print_graph (WeightedDigraph &g) |
| void | print_path (Path< WeightedDigraph > &path, WeightedDigraph &g) |
| template<typename Func > | |
| double | measure_ms (Func &&f) |
| void | example_basic_negative_weights () |
| void | example_negative_cycle () |
| void | example_spfa_comparison () |
| void | example_comparison_dijkstra () |
| static void | usage (const char *prog) |
| static bool | has_flag (int argc, char *argv[], const char *flag) |
| int | main (int argc, char *argv[]) |
Bellman-Ford shortest paths in Aleph-w (negative weights, negative-cycle detection, SPFA).
This example demonstrates Aleph-w's Bellman-Ford implementation for single-source shortest paths on directed graphs that may contain negative arc weights. It also demonstrates:
Bellman-Ford is the “safe default” when you cannot guarantee non-negative weights, and it is a key building block for Johnson's all-pairs algorithm.
WeightedDigraph = List_Digraph<Graph_Node<string>, Graph_Arc<double>>string)double)If no flags are given, or if you pass no “specific” flags, the program runs all demos.
Bellman-Ford repeatedly relaxes all edges. In a graph with no negative cycles reachable from the source, shortest paths have at most |V|-1 edges, so |V|-1 relaxation rounds suffice.
After the |V|-1 rounds, if any edge can still be relaxed, there exists a negative cycle reachable from the source, and shortest paths are not well-defined (cost can be decreased indefinitely by looping).
The example also shows a queue-driven approach that only relaxes outgoing edges of nodes whose distance changed. This is often faster in practice, but retains Bellman-Ford's worst-case behavior.
Let V be the number of nodes and E the number of arcs.
O(V * E) time, O(V) extra space.O(E) on many inputs (no guarantee).O(V * E).double weights, comparisons can be sensitive to rounding; be careful if you adapt this to real-world numeric data.Bellman_Ford.H (implementation)dijkstra_example.cc / Dijkstra.H (faster when all weights are non-negative)johnson_example.cc (all-pairs shortest paths using Bellman-Ford + Dijkstra)Definition in file bellman_ford_example.cc.
| using Arc = WeightedDigraph::Arc |
Definition at line 141 of file bellman_ford_example.cc.
| using Node = WeightedDigraph::Node |
Definition at line 140 of file bellman_ford_example.cc.
| using WeightedDigraph = List_Digraph<Graph_Node<string>, Graph_Arc<double> > |
Definition at line 139 of file bellman_ford_example.cc.
| void example_basic_negative_weights | ( | ) |
Definition at line 226 of file bellman_ford_example.cc.
References Aleph::maps(), print_graph(), and print_path().
| void example_comparison_dijkstra | ( | ) |
Definition at line 437 of file bellman_ford_example.cc.
References Aleph::maps().
| void example_negative_cycle | ( | ) |
Definition at line 303 of file bellman_ford_example.cc.
References Aleph::maps(), and print_graph().
| void example_spfa_comparison | ( | ) |
Definition at line 372 of file bellman_ford_example.cc.
References Aleph::maps(), measure_ms(), N, nodes, and Aleph::to_string().
| Node * find_node | ( | WeightedDigraph & | g, |
| const string & | name | ||
| ) |
Definition at line 157 of file bellman_ford_example.cc.
|
static |
Definition at line 456 of file bellman_ford_example.cc.
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 464 of file bellman_ford_example.cc.
| double measure_ms | ( | Func && | f | ) |
Definition at line 214 of file bellman_ford_example.cc.
Referenced by example_spfa_comparison().
| void print_graph | ( | WeightedDigraph & | g | ) |
Definition at line 165 of file bellman_ford_example.cc.
References Aleph::maps().
Referenced by example_basic_negative_weights(), and example_negative_cycle().
| void print_path | ( | Path< WeightedDigraph > & | path, |
| WeightedDigraph & | g | ||
| ) |
Definition at line 185 of file bellman_ford_example.cc.
References Aleph::Path< GT >::for_each_arc(), Aleph::Path< GT >::for_each_node(), Aleph::maps(), and Aleph::Path< GT >::size().
Referenced by example_basic_negative_weights().
|
static |
Definition at line 450 of file bellman_ford_example.cc.