|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Dijkstra shortest paths in Aleph-w (single path, shortest-path tree, and heap trade-offs). More...
#include <iostream>#include <iomanip>#include <string>#include <vector>#include <chrono>#include <random>#include <limits>#include <tpl_graph.H>#include <Dijkstra.H>Go to the source code of this file.
Classes | |
| struct | RoadDistance |
| Distance accessor functor for Dijkstra algorithm. More... | |
Typedefs | |
| using | CityNode = Graph_Node< string > |
| Node type: city name. | |
| using | RoadArc = Graph_Arc< double > |
| Arc type: distance in km. | |
| using | CityGraph = List_Digraph< CityNode, RoadArc > |
| Graph type: directed graph of cities connected by roads. | |
Functions | |
| CityGraph | build_city_graph (vector< CityGraph::Node * > &nodes) |
| Build a sample graph representing a city road network. | |
| CityGraph | build_random_graph (int num_nodes, double edge_probability, double max_weight, unsigned seed=42) |
| Build a random graph for performance testing. | |
| void | print_graph (CityGraph &g) |
| Print the graph structure. | |
| void | print_path_detailed (CityGraph &g, Path< CityGraph > &path) |
| Print a path with detailed step-by-step information. | |
| template<typename Func > | |
| double | measure_time_ms (Func &&f) |
| int | main () |
Dijkstra shortest paths in Aleph-w (single path, shortest-path tree, and heap trade-offs).
This example demonstrates how to compute shortest paths with Aleph-w's Dijkstra_Min_Paths on a weighted graph with non-negative arc weights. It focuses on two common usage modes:
It also compares two priority-queue backends used internally by Dijkstra: ArcHeap (binary heap) vs ArcFibonacciHeap.
CityGraph = List_Digraph<Graph_Node<string>, Graph_Arc<double>>string)double)Note: The demo builds bidirectional roads by inserting arcs in both directions, even though the container type is a directed graph.
This example has no command-line options; all parameters (graph sizes, densities) are hard-coded.
find_min_path(g, src, dst, path) computes a shortest path and writes it into path.compute_min_paths_tree(g, src, tree) builds an explicit shortest-path tree graph.paint_min_paths_tree(g, src) marks the original graph so you can query later.get_min_path(tree, dst, path) extracts the path to dst from a previously built tree.get_min_path(dst, path) extracts the path to dst after paint_min_paths_tree().Let V be the number of nodes and E the number of arcs.
ArcHeap)**: O((V + E) log V)ArcFibonacciHeap)**: O(E + V log V) (amortized)Notes:
find_min_path(g, s, s, path) may return Inf and an empty path; handle the trivial case explicitly if you need distance 0 and path [s].Dijkstra.H (implementation)bellman_ford_example.cc / Bellman_Ford.H (negative weights)johnson_example.cc (all-pairs shortest paths with negative weights but no negative cycles)astar_example.cc / AStar.H (the heuristic-guided shortest path)Definition in file dijkstra_example.cc.
| using CityGraph = List_Digraph<CityNode, RoadArc> |
Graph type: directed graph of cities connected by roads.
Definition at line 136 of file dijkstra_example.cc.
| using CityNode = Graph_Node<string> |
Node type: city name.
Definition at line 130 of file dijkstra_example.cc.
Arc type: distance in km.
Definition at line 133 of file dijkstra_example.cc.
| CityGraph build_city_graph | ( | vector< CityGraph::Node * > & | nodes | ) |
Build a sample graph representing a city road network.
Creates the following network:
| [out] | nodes | Vector to store node pointers for later reference |
Definition at line 184 of file dijkstra_example.cc.
References Aleph::maps(), and nodes.
Referenced by main().
| CityGraph build_random_graph | ( | int | num_nodes, |
| double | edge_probability, | ||
| double | max_weight, | ||
| unsigned | seed = 42 |
||
| ) |
Build a random graph for performance testing.
| num_nodes | Number of nodes |
| edge_probability | Probability of edge between any two nodes (0.0 to 1.0) |
| max_weight | Maximum edge weight |
| seed | Random seed for reproducibility |
Definition at line 228 of file dijkstra_example.cc.
References Aleph::maps(), nodes, num_nodes, rng, Aleph::to_string(), and w.
Referenced by main().
| int main | ( | ) |
Definition at line 349 of file dijkstra_example.cc.
References build_city_graph(), build_random_graph(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::maps(), measure_time_ms(), nodes, print_graph(), print_path_detailed(), and Aleph::Path< GT >::size().
| double measure_time_ms | ( | Func && | f | ) |
Definition at line 337 of file dijkstra_example.cc.
Referenced by main().
| void print_graph | ( | CityGraph & | g | ) |
Print the graph structure.
Definition at line 262 of file dijkstra_example.cc.
References Aleph::maps().
Referenced by main().
Print a path with detailed step-by-step information.
Definition at line 294 of file dijkstra_example.cc.
References Aleph::Path< GT >::for_each_arc(), Aleph::Path< GT >::for_each_node(), Aleph::maps(), and Aleph::Path< GT >::size().
Referenced by main().