首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用petgraph中的Bellman-Ford算法

使用petgraph中的Bellman-Ford算法
EN

Stack Overflow用户
提问于 2017-05-25 02:22:20
回答 1查看 336关注 0票数 2

我想使用petgraph板条箱中的Bellman-Ford算法。下面是一个不能编译的非常简单的示例程序:

代码语言:javascript
复制
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));
}

当我编译这个程序时,我得到了这个错误消息:

代码语言:javascript
复制
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`
EN

回答 1

Stack Overflow用户

发布于 2017-05-25 06:30:35

据我所知,实现的Bellman-Ford算法适用于浮点数,而不是整数。

使用浮点数而不是u64,并在以后引用deps可以做到这一点:

代码语言:javascript
复制
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);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44165951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档