I have a radiation pattern and you want to find the shortest path from node A to node B. I searched on crates.io and found a petgraph that looks like the most popular box. It implements a number of algorithms , but none of them solves my problem. Did I miss something?
For example, the Dijkstra algorithm returns the costs of a path, but which path has a minimum cost? The Bellman-Ford algorithm returns the path cost and nodes, but there are no paths.
This is the easiest way to find the path from the graph:
extern crate petgraph;
use petgraph::prelude::*;
use petgraph::algo::dijkstra;
fn main() {
let mut graph = Graph::<&str, i32>::new();
let a = graph.add_node("a");
let b = graph.add_node("b");
let c = graph.add_node("c");
let d = graph.add_node("d");
graph.extend_with_edges(&[(a, b, 1), (b, c, 1), (c, d, 1), (a, b, 1), (b, d, 1)]);
let paths_cost = dijkstra(&graph, a, Some(d), |e| *e.weight());
println!("dijkstra {:?}", paths_cost);
let mut path = vec![d.index()];
let mut cur_node = d;
while cur_node != a {
let m = graph
.edges_directed(cur_node, Direction::Incoming)
.map(|edge| paths_cost.get(&edge.source()).unwrap())
.min()
.unwrap();
let v = *m as usize;
path.push(v);
cur_node = NodeIndex::new(v);
}
for i in path.iter().rev().map(|v| graph[NodeIndex::new(*v)]) {
println!("path: {}", i);
}
}
As far as I can see, I need to calculate the path myself, based on the result
dijkstra.
, dijkstra ( dijkstra.rs) predecessor return predecessor, , - predecessor[predecessor[d]].