2017-05-24 3 views
1

私はpetgraphの箱からBellman-Fordアルゴリズムを使いたいと思います。ここではコンパイルできない非常に簡単なサンプルプログラムは次のとおりです。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

'以下の実装が発見された:<& 'petgraph ...' '&deps'で' deps'を交換してください – red75prime

答えて

2

私は、実装ベルマン - フォード法の作品を集めたものから、整数ではなく浮動小数点数を使用します。

代わり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); 
} 
関連する問題