Skip to main content

a_star_path

Function a_star_path 

Source
pub fn a_star_path<H: Fn(VertexId, VertexId) -> f64>(
    graph: &Graph,
    from: VertexId,
    to: VertexId,
    weights: Option<&[f64]>,
    mode: DijkstraMode,
    heuristic: H,
) -> IgraphResult<Option<(Vec<VertexId>, Vec<u32>)>>
Expand description

Single-source single-target shortest path using A*.

weights is None for unweighted (unit-weight BFS-equivalent) or Some(&[f64]) for weighted; non-negative finite values are required (NaN / negative values return IgraphError::InvalidArgument). Edges of weight f64::INFINITY are skipped during relaxation.

heuristic(v, target) -> f64 must return a non-negative admissible estimate of the remaining distance from v to target. With |_, _| 0.0 (the “null heuristic”), A* reduces to Dijkstra and is guaranteed correct. Inadmissible heuristics may return a path shorter than the true geodesic.

Returns Ok(None) when target is unreachable; otherwise Ok(Some((vs, es))) with vertex chain (including source and target) and parallel edge id chain.

Counterpart of igraph_get_shortest_path_astar(_, _, _, from, to, &weights, mode, heuristic, NULL).

§Examples

use rust_igraph::{Graph, a_star_path, DijkstraMode};

// Path 0-1-2-3 unit weights with null heuristic (≡ Dijkstra):
// shortest path 0→3 is the chain through every vertex.
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
let (vs, es) = a_star_path(&g, 0, 3, None, DijkstraMode::Out, |_, _| 0.0)
    .unwrap()
    .unwrap();
assert_eq!(vs, vec![0, 1, 2, 3]);
assert_eq!(es, vec![0, 1, 2]);