Skip to main content

bellman_ford_path_to

Function bellman_ford_path_to 

Source
pub fn bellman_ford_path_to(
    graph: &Graph,
    source: VertexId,
    target: VertexId,
    weights: &[f64],
) -> IgraphResult<Option<(Vec<VertexId>, Vec<u32>)>>
Expand description

Returns the shortest path from source to target using Bellman-Ford, following out-edges on directed graphs.

Returns Some((vertices, edges)) if a finite-weight path exists, None if target is unreachable. When source == target, returns Some((vec![source], vec![])).

Supports negative edge weights; detects negative cycles reachable from the source.

Counterpart of igraph_get_shortest_path_bellman_ford(_, vertices, edges, from, to, weights, IGRAPH_OUT).

ยงExamples

use rust_igraph::{Graph, bellman_ford_path_to};

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 (vs, es) = bellman_ford_path_to(&g, 0, 3, &[3.0, -1.0, 5.0, 1.0])
    .unwrap()
    .unwrap();
assert_eq!(vs, vec![0, 1, 2, 3]);
assert_eq!(es, vec![0, 1, 3]);