Skip to main content

bellman_ford_paths

Function bellman_ford_paths 

Source
pub fn bellman_ford_paths(
    graph: &Graph,
    source: VertexId,
    targets: &[VertexId],
    weights: &[f64],
) -> IgraphResult<Vec<BellmanFordPathEntry>>
Expand description

Shortest paths from source to each vertex in targets using Bellman-Ford (handles negative edge weights).

Counterpart of igraph_get_shortest_paths_bellman_ford in references/igraph/src/paths/bellman_ford.c:296. Runs the SSSP computation once and reconstructs a path for each target.

Returns a Vec with one entry per target. Each entry is Some((vertices, edges)) if the target is reachable from source, or None if it is not.

§Errors

§Examples

use rust_igraph::{Graph, bellman_ford_paths};

let mut g = Graph::new(4, true).unwrap();
g.add_edge(0, 1).unwrap(); // edge 0, weight 3
g.add_edge(1, 2).unwrap(); // edge 1, weight -1
g.add_edge(0, 2).unwrap(); // edge 2, weight 5
g.add_edge(2, 3).unwrap(); // edge 3, weight 1
let results = bellman_ford_paths(&g, 0, &[2, 3], &[3.0, -1.0, 5.0, 1.0]).unwrap();
// Path to vertex 2: 0→1→2 (cost 2, via negative edge)
let (vs, es) = results[0].as_ref().unwrap();
assert_eq!(*vs, vec![0, 1, 2]);
assert_eq!(*es, vec![0, 1]);
// Path to vertex 3: 0→1→2→3 (cost 3)
let (vs, es) = results[1].as_ref().unwrap();
assert_eq!(*vs, vec![0, 1, 2, 3]);
assert_eq!(*es, vec![0, 1, 3]);