简体   繁体   English

你如何使用 Rust 的 petgraph 执行过滤搜索?

[英]How do you perform a filtered search with Rust's petgraph?

Rust's Petgraph library has a bunch of filter 'adaptors' , but I haven't been able to find any examples or tutorials for how to use them. Rust 的 Petgraph 库有一堆过滤器“适配器” ,但我找不到任何示例或教程来说明如何使用它们。 Some (but not all) have constructors, such as EdgeFiltered.from_fn() which takes a graph and a function, but it's not clear how you'd then use it with the search methods like Dfs or astar as they don't have filter parameters.一些(但不是全部)具有构造函数,例如EdgeFiltered.from_fn() ,它接受一个图形和一个 function,但不清楚你如何将它与Dfsastar等搜索方法一起使用,因为它们没有过滤器参数。

So how do you use these filters?那么如何使用这些过滤器呢? For example, if I had a graph with integer edge weights how would you:例如,如果我有一个带有 integer 边权重的图表,您将如何:

  1. Perform a depth first search but excluding edges with a negative weight,执行深度优先搜索,但排除具有负权重的边缘,
  2. Perform an astar search excluding edges with a negative weight?执行 astar 搜索,排除具有负权重的边?

The adapters implement GraphBase , you can just wrap your graph in them and pass the adapter struct to eg Dfs :适配器实现GraphBase ,您可以将图形包装在其中并将适配器结构传递给例如Dfs

use petgraph::Graph;
use petgraph::visit::Dfs;
use petgraph::visit::EdgeFiltered;

fn main() {
    let mut graph = Graph::<(), i64>::new();
    let a = graph.add_node(());
    let b = graph.add_node(());
    let e = graph.add_edge(a, b, -1);
    let filtered = EdgeFiltered::from_fn(&graph, |edge_ref| *edge_ref.weight() > 0 );
    let dfs = Dfs::new(&filtered, a);
}

Playground Link 游乐场链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM