首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用花瓣图的随机游动

利用花瓣图的随机游动
EN

Stack Overflow用户
提问于 2018-10-18 11:53:33
回答 1查看 373关注 0票数 0

我试图使用petgraph机箱在有向图上实现随机游走。

到目前为止,我已经定义了一个RandomWalk结构,它实现了Walker特性:

代码语言:javascript
复制
extern crate petgraph; // 0.4.13
use petgraph::visit::{GraphBase, Walker};
use petgraph::Direction;

pub struct RandomWalk<G> 
    where G: GraphBase
{
   next: G::NodeId, 
}

impl<G> Walker<G> for RandomWalk<G>
    where G: GraphBase
{
   type Item = G::NodeId; 

   fn walk_next(&mut self, graph: G) -> Option<Self::Item> {
       // Even this deterministic walk does not work:
       graph.neighbors_directed(self.next, Direction::Incoming).next()
   }
}

但是,我得到了错误:

代码语言:javascript
复制
error[E0599]: no method named `neighbors_directed` found for type `G` in the current scope
  --> src/lib.rs:50:11
   |
50 |     graph.neighbors_directed(self.next, Direction::Incoming).next()
   |           ^^^^^^^^^^^^^^^^^^
   |
   = note: the method `neighbors_directed` exists but the following trait bounds were not satisfied:
           `&G : petgraph::visit::IntoNeighborsDirected`
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `neighbors_directed`, perhaps you need to implement it:
           candidate #1: `petgraph::visit::IntoNeighborsDirected`

我真的不明白petgraph API是如何工作的,GraphBase不是正确的类型吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-18 12:54:55

如果您理解编译器,那么解决方案是非常清晰的。

锈蚀不假定任何关于类型的东西,除非您指定它,例如,您不能将两种类型的T一起添加,除非实现Add特性。然后你可以写T + T

你的问题很相似。

您正在尝试使用函数neighbors_directed,它不是为G实现的(在您的示例中绑定到GraphBase )。相反,您必须指定G还必须通过将特征IntoNeighborsDirected添加到impl块来实现它。

代码语言:javascript
复制
impl<G> Walker<G> for RandomWalk<G> where G: GraphBase + IntoNeighborsDirected

这将告诉编译器,G已经实现了neighbors_directed方法,您可以使用它(游乐场)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52873390

复制
相关文章

相似问题

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