pub fn distance_map<G, K>(
graph: G,
dist_matrix: &[Vec<K>],
) -> HashMap<(G::NodeId, G::NodeId), K>Expand description
Convert distance matrix into hashmap.
The algorithm takes as input the graph graph and the matrix of shortest distances dist_matrix,
where cell (i, j) stores the shortest distance from node i to node j.
ยงExamples
use std::collections::HashMap;
use graphalgs::shortest_path::{ floyd_warshall, distance_map };
use petgraph::prelude::NodeIndex;
use petgraph::visit::NodeIndexable;
use petgraph::Graph;
let inf = f32::INFINITY;
let graph = Graph::<(), f32>::from_edges(&[
(0, 1, 2.0), (1, 2, 10.0), (1, 3, -5.0),
(3, 2, 2.0), (2, 3, 20.0),
]);
let (n0, n1, n2, n3) = (graph.from_index(0), graph.from_index(1),
graph.from_index(2), graph.from_index(3));
let true_dist_map: HashMap<(NodeIndex, NodeIndex), f32> = [
((n0, n0), 0.0), ((n0, n1), 2.0), ((n0, n2), -1.0), ((n0, n3), -3.0),
((n1, n0), inf), ((n1, n1), 0.0), ((n1, n2), -3.0), ((n1, n3), -5.0),
((n2, n0), inf), ((n2, n1), inf), ((n2, n2), 0.0), ((n2, n3), 20.0),
((n3, n0), inf), ((n3, n1), inf), ((n3, n2), 2.0), ((n3, n3), 0.0),
].iter().cloned().collect();
// Get the distance matrix.
let dist_matrix = floyd_warshall(&graph, |edge| *edge.weight()).unwrap();
// Convert the distance matrix into hashmap.
assert_eq!(distance_map(&graph, &dist_matrix), true_dist_map);