我有一个有向图,想找到从节点 A 到节点 B 的最短路径。我在crates.io上搜索并找到了petgraph,它看起来像是最受欢迎的 crate。它实现了许多算法,但没有一个能解决我的任务。我错过了什么?
例如,Dijkstra 算法返回路径成本,但哪条路径的成本最低?该Bellman-Ford算法返回路径成本和节点,但没有路径。
这是我发现从图中打印路径的最简单方法:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,我需要根据
dijkstra
.
我相信,如果我dijkstra
自己实现(基于我的实现基于dijkstra.rs),并使用predecessor
和 return取消注释行predecessor
,最终变体会更快,因为答案将类似于predecessor[predecessor[d]]
.
正如评论中提到的(由库的主要作者,不少),您可以使用 A* ( astar
) 算法:
use petgraph::{algo, prelude::*}; // 0.5.1
fn main() {
let mut graph = Graph::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 path = algo::astar(
&graph,
a, // start
|n| n == d, // is_goal
|e| *e.weight(), // edge_cost
|_| 0, // estimate_cost
);
match path {
Some((cost, path)) => {
println!("The total cost was {}: {:?}", cost, path);
}
None => println!("There was no path"),
}
}
Run Code Online (Sandbox Code Playgroud)
use petgraph::{algo, prelude::*}; // 0.5.1
fn main() {
let mut graph = Graph::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 path = algo::astar(
&graph,
a, // start
|n| n == d, // is_goal
|e| *e.weight(), // edge_cost
|_| 0, // estimate_cost
);
match path {
Some((cost, path)) => {
println!("The total cost was {}: {:?}", cost, path);
}
None => println!("There was no path"),
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1215 次 |
最近记录: |