1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#![deny(missing_docs)]

//! This crate implements several pathfinding, flow, and graph algorithms.

extern crate fixedbitset;
#[macro_use]
extern crate itertools;
extern crate ordermap;
pub extern crate num_traits;

mod astar;
mod bfs;
mod connected_components;
mod dfs;
mod dijkstra;
mod edmonds_karp;
mod fringe;
mod grid;
mod idastar;
mod kuhn_munkres;
mod matrix;
mod topological_sort;

pub use astar::*;
pub use bfs::*;
pub use connected_components::*;
pub use dfs::*;
pub use dijkstra::*;
pub use edmonds_karp::*;
pub use fringe::*;
pub use grid::*;
pub use idastar::*;
pub use kuhn_munkres::*;
pub use matrix::*;
pub use topological_sort::*;

use ordermap::OrderMap;
use std::hash::Hash;

fn reverse_path<N, V, F>(parents: &OrderMap<N, V>, mut parent: F, start: usize) -> Vec<N>
where
    N: Eq + Hash + Clone,
    F: FnMut(&V) -> usize,
{
    let path = itertools::unfold(start, |i| {
        parents.get_index(*i).map(|(node, value)| {
            *i = parent(value);
            node
        })
    }).collect::<Vec<&N>>();

    path.into_iter().rev().cloned().collect()
}