use crate::{
adjacency_list_graph_old::AdjacencyList,
graph::edge::{
From,
To,
Weight,
},
negative_cycle::NegativeCycleError,
spfa::spfa,
};
pub trait ShortestPathPotentialEdge:
From<V = usize>
+ To<V = usize>
+ Weight<i64>
+ std::convert::From<(usize, usize, i64)>
{
}
impl<T> ShortestPathPotentialEdge for T where
T: From<V = usize>
+ To<V = usize>
+ Weight<i64>
+ std::convert::From<(usize, usize, i64)>
{
}
pub fn shortest_path_potential<E>(
v_size: usize,
mut directed_edges: Vec<E>,
) -> Result<Vec<i64>, NegativeCycleError>
where
E: ShortestPathPotentialEdge,
{
directed_edges.extend((0..v_size).map(|i| (v_size, i, 0).into()));
let g = AdjacencyList::<E>::from((v_size + 1, directed_edges));
match spfa(g.edges(), v_size) {
Ok(mut potential) => {
potential.pop();
Ok(potential.into_iter().map(|x| x.unwrap()).collect())
}
Err(e) => Err(e),
}
}
#[cfg(test)]
mod tests {
#[test]
fn test() {}
}