6

我有一个方向图,想找到从节点 A 到节点 B 的最短路径。我在crates.io上搜索并找到了看起来像最受欢迎的 crate 的petgraph 。实现了许多算法,但没有一个能解决我的任务。我错过了什么?

例如,Dijkstra 算法返回路径成本,但哪条路径的成本最小?Bellman-Ford 算法返回路径成本和节点,但没有路径。

这是我发现从图中打印路径的最简单方法:

extern crate petgraph;
use petgraph::prelude::*;
use petgraph::algo::dijkstra;

fn main() {
    let mut graph = Graph::<&str, i32>::new();
    let a = graph.add_node("a");
    let b = graph.add_node("b");
    let c = graph.add_node("c");
    let d = graph.add_node("d");

    graph.extend_with_edges(&[(a, b, 1), (b, c, 1), (c, d, 1), (a, b, 1), (b, d, 1)]);
    let paths_cost = dijkstra(&graph, a, Some(d), |e| *e.weight());
    println!("dijkstra {:?}", paths_cost);

    let mut path = vec![d.index()];
    let mut cur_node = d;

    while cur_node != a {
        let m = graph
            .edges_directed(cur_node, Direction::Incoming)
            .map(|edge| paths_cost.get(&edge.source()).unwrap())
            .min()
            .unwrap();
        let v = *m as usize;
        path.push(v);
        cur_node = NodeIndex::new(v);
    }

    for i in path.iter().rev().map(|v| graph[NodeIndex::new(*v)]) {
        println!("path: {}", i);
    }
}

据我所知,我需要自己根据 dijkstra.

我相信,如果我dijkstra自己实现(基于dijkstra.rspredecessor的实现),并用和 return取消注释行predecessor,最终的变体会更快,因为答案将类似于predecessor[predecessor[d]].

4

1 回答 1

7

正如评论中提到的(由库的主要作者,不少于),您可以使用 A* ( astar) 算法:

use petgraph::{algo, prelude::*}; // 0.5.1

fn main() {
    let mut graph = Graph::new();

    let a = graph.add_node("a");
    let b = graph.add_node("b");
    let c = graph.add_node("c");
    let d = graph.add_node("d");

    graph.extend_with_edges(&[(a, b, 1), (b, c, 1), (c, d, 1), (a, b, 1), (b, d, 1)]);

    let path = algo::astar(
        &graph,
        a,               // start
        |n| n == d,      // is_goal
        |e| *e.weight(), // edge_cost
        |_| 0,           // estimate_cost
    );

    match path {
        Some((cost, path)) => {
            println!("The total cost was {}: {:?}", cost, path);
        }
        None => println!("There was no path"),
    }
}
The total cost was 2: [NodeIndex(0), NodeIndex(1), NodeIndex(3)]
于 2017-10-22T14:09:37.303 回答