我想使用petgraph箱中的Bellman-Ford算法。这是一个非常简单的示例程序,无法编译:
extern crate petgraph;
use petgraph::prelude::*;
use petgraph::dot::{Dot, Config};
use petgraph::algo::bellman_ford;
fn main() {
println!("Hello, world!");
let mut deps = Graph::<&str, u64>::new();
let a = deps.add_node("later");
let b = deps.add_node("hello");
deps.update_edge(a, b, 5);
deps.update_edge(b, a, 10);
let result = bellman_ford(deps, NodeIndex::new(0));
}
编译此程序时,我收到此错误消息:
error[E0277]: the trait bound `petgraph::Graph<&str, f64>: petgraph::visit::IntoNodeIdentifiers` is not satisfied
--> src/main.rs:16:18
|
16 | let result = bellman_ford(deps, NodeIndex::new(0));
| ^^^^^^^^^^^^ the trait `petgraph::visit::IntoNodeIdentifiers` is not implemented for `petgraph::Graph<&str, f64>`
|
= help: the following implementations were found:
<&'a petgraph::Graph<N, E, Ty, Ix> as petgraph::visit::IntoNodeIdentifiers>
= note: required by `petgraph::algo::bellman_ford`
error[E0277]: the trait bound `petgraph::Graph<&str, f64>: petgraph::visit::IntoEdges` is not satisfied
--> src/main.rs:16:18
|
16 | let result = bellman_ford(deps, NodeIndex::new(0));
| ^^^^^^^^^^^^ the trait `petgraph::visit::IntoEdges` is not implemented for `petgraph::Graph<&str, f64>`
|
= help: the following implementations were found:
<&'a petgraph::Graph<N, E, Ty, Ix> as petgraph::visit::IntoEdges>
= note: required by `petgraph::algo::bellman_ford`
答案 0 :(得分:2)
根据我的收集,实施的Bellman-Ford算法适用于浮点数,而不是整数。
使用浮点数代替u64
并稍后引用deps
可以解决问题:
use petgraph::algo::bellman_ford;
fn main() {
let mut deps = Graph::<&str, f64>::new();
let a = deps.add_node("later");
let b = deps.add_node("hello");
deps.update_edge(a, b, 5.0);
deps.update_edge(b, a, 10.0);
let result = bellman_ford(&deps, NodeIndex::new(0));
println!("{:?}", result);
}