use std::collections::VecDeque;
use indexmap::IndexMap;
use crate::Hypergraph;
pub fn single_source_shortest_path_length<N, E, M>(
h: &Hypergraph<N, E, M>,
source: &str,
) -> Option<IndexMap<String, f64>> {
if !h.has_node(source) {
return None;
}
let mut dist: IndexMap<String, f64> = IndexMap::new();
for n in h.node_ids() {
dist.insert(n, f64::INFINITY);
}
dist.insert(source.to_string(), 0.0);
let mut queue: VecDeque<String> = VecDeque::new();
queue.push_back(source.to_string());
while let Some(cur) = queue.pop_front() {
let cur_d = dist[&cur];
for nb in h.node_neighbors(&cur).unwrap_or_default() {
if dist[&nb].is_infinite() {
dist.insert(nb.clone(), cur_d + 1.0);
queue.push_back(nb);
}
}
}
Some(dist)
}
pub fn shortest_path_length<N, E, M>(
h: &Hypergraph<N, E, M>,
) -> Vec<(String, IndexMap<String, f64>)> {
h.node_ids()
.into_iter()
.filter_map(|src| single_source_shortest_path_length(h, &src).map(|d| (src, d)))
.collect()
}