Function rustfst::algorithms::shortest_distance
source · pub fn shortest_distance<W: Semiring, F: ExpandedFst<W>>(
fst: &F,
reverse: bool
) -> Result<Vec<W>>
Expand description
Compute the shortest distance from the initial state to every state.
The shortest distance from p
to q
is the ⊕-sum of the weights
of all the paths between p
and q
.
§Example
fn main() -> Result<()> {
let mut fst = VectorFst::<IntegerWeight>::new();
let s0 = fst.add_state();
let s1 = fst.add_state();
let s2 = fst.add_state();
fst.set_start(s0).unwrap();
fst.add_tr(s0, Tr::new(32, 23, 18, s1));
fst.add_tr(s0, Tr::new(32, 23, 21, s2));
fst.add_tr(s1, Tr::new(32, 23, 55, s2));
let dists = shortest_distance(&fst, false)?;
assert_eq!(dists, vec![
IntegerWeight::one(),
IntegerWeight::new(18),
IntegerWeight::new(21 + 18*55),
]);