pub fn bellman_ford_distances(
graph: &Graph,
source: VertexId,
weights: &[f64],
) -> IgraphResult<Vec<Option<f64>>>Expand description
Single-source Bellman-Ford shortest distances on graph from
source, following out-edges on directed graphs.
Returns distances[v]: Some(d) if v is reachable from
source, None otherwise. distances[source] == Some(0.0).
weights[e] is the weight of edge e; length must equal
graph.ecount(). Negative weights are allowed; NaN weights are
rejected (IgraphError::InvalidArgument). If a negative cycle
is reachable from the source, returns
IgraphError::InvalidArgument with a “negative cycle”
message (matches upstream’s IGRAPH_ENEGCYCLE semantics).
Use this instead of crate::dijkstra_distances when edge
weights can be negative. For non-negative weights Dijkstra is
asymptotically faster (O((V+E) log V) vs Bellman-Ford’s
O(V·E)).
Counterpart of igraph_distances_bellman_ford(_, _, vss(source), vss_all(), weights, IGRAPH_OUT).
§Examples
use rust_igraph::{Graph, bellman_ford_distances};
// Directed graph 0 → 1 → 2 with weights 3, -1 — Bellman-Ford
// handles the negative edge that would break Dijkstra's
// monotonicity assumption.
let mut g = Graph::new(3, true).unwrap();
g.add_edge(0, 1).unwrap(); // edge 0, weight 3
g.add_edge(1, 2).unwrap(); // edge 1, weight -1
let d = bellman_ford_distances(&g, 0, &[3.0, -1.0]).unwrap();
assert_eq!(d, vec![Some(0.0), Some(3.0), Some(2.0)]);