arcweight 0.3.0

A high-performance, modular library for weighted finite state transducers with comprehensive examples and benchmarks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//! $`O(V+E)`$ shortest distance for acyclic FSTs.
//!
//! This module provides specialized shortest distance algorithms for acyclic (DAG)
//! FSTs, achieving optimal $`O(V+E)`$ complexity by exploiting the topological structure.
//! Since most FSTs in speech/NLP applications are acyclic, this is the preferred
//! algorithm when applicable.
//!
//! # Complexity Comparison
//!
//! | Algorithm | Time | Space | Requirements |
//! |-----------|------|-------|--------------|
//! | Acyclic (this) | $`O(V+E)`$ | $`O(V)`$ | Acyclic FST |
//! | Dijkstra | $`O((V+E)\log V)`$ | $`O(V)`$ | Non-negative weights |
//! | Bellman-Ford | $`O(VE)`$ | $`O(V)`$ | General |
//!
//! # Algorithm
//!
//! The acyclic algorithm uses Kahn's topological sort followed by single-pass relaxation:
//!
//! 1. **Topological Sort:** Order states so all arcs go forward — $`O(V+E)`$
//! 2. **Relaxation:** Process states in order, relax outgoing arcs — $`O(V+E)`$
//!
//! For state $`q`$ in topological order with distance $`d[q]`$:
//! ```text
//! for each arc q --w--> r:
//!     d[r] ← d[r] ⊕ (d[q] ⊗ w)
//! ```
//!
//! # When to Use
//!
//! Most FSTs in practice are acyclic:
//!
//! - **Lexicon FSTs:** Dictionary lookups
//! - **Pronunciation models:** Grapheme-to-phoneme mappings
//! - **N-gram LMs:** Finite-order language models
//! - **Linear-chain CRFs:** Sequence labeling models
//! - **Morphological analyzers:** Word decomposition
//!
//! Use [`is_acyclic`] to check, or [`shortest_distance_auto`] for automatic selection.
//!
//! # Example
//!
//! ```rust
//! use arcweight::prelude::*;
//! use arcweight::algorithms::{shortest_distance_acyclic, is_acyclic};
//!
//! let mut fst = VectorFst::<TropicalWeight>::new();
//! let s0 = fst.add_state();
//! let s1 = fst.add_state();
//! let s2 = fst.add_state();
//! fst.set_start(s0);
//! fst.set_final(s2, TropicalWeight::one());
//!
//! // Two paths to s2
//! fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(3.0), s2));  // Direct: 3.0
//! fst.add_arc(s0, Arc::new(2, 2, TropicalWeight::new(1.0), s1));  // Via s1: 1.0 + 0.5 = 1.5
//! fst.add_arc(s1, Arc::new(3, 3, TropicalWeight::new(0.5), s2));
//!
//! // Verify acyclic and compute distances
//! assert!(is_acyclic(&fst));
//! let d = shortest_distance_acyclic(&fst)?;
//!
//! assert_eq!(*d[0].value(), 0.0);  // Start
//! assert_eq!(*d[1].value(), 1.0);  // Via s0->s1
//! assert_eq!(*d[2].value(), 1.5);  // min(3.0, 1.0+0.5) = 1.5
//! # Ok::<(), arcweight::Error>(())
//! ```
//!
//! # References
//!
//! \[1\] Mohri, M. 2002. Semiring frameworks and algorithms for shortest-distance
//!     problems. *Journal of Automata, Languages and Combinatorics* 7, 3, 321-350.
//!
//! \[2\] Cormen, T. H., Leiserson, C. E., Rivest, R. L., and Stein, C. 2009.
//!     *Introduction to Algorithms* (3rd ed.). MIT Press. Chapter 24: Single-Source
//!     Shortest Paths.
//!
//! \[3\] Kahn, A. B. 1962. Topological sorting of large networks. *Communications
//!     of the ACM* 5, 11 (November 1962), 558-562. DOI: <https://doi.org/10.1145/368996.369025>

use crate::fst::{Fst, StateId};
use crate::semiring::Semiring;
use crate::{Error, Result};

/// Computes shortest distances from the start state for acyclic FSTs.
///
/// Uses topological sorting to process states in dependency order, achieving
/// optimal $`O(V+E)`$ time complexity. This is the preferred algorithm for
/// acyclic FSTs, which are common in speech and NLP applications.
///
/// # Type Parameters
///
/// * `W` - Weight type implementing [`Semiring`]
/// * `F` - FST type implementing [`Fst`]
///
/// # Arguments
///
/// * `fst` - Input FST (must be acyclic)
///
/// # Returns
///
/// Vector of weights where `result[s]` is the shortest distance from the start
/// state to state `s`. Returns `W::zero()` (semiring zero) for unreachable states.
///
/// # Complexity
///
/// - **Time:** $`O(V + E)`$
/// - **Space:** $`O(V)`$
///
/// # Algorithm
///
/// 1. Compute topological order using Kahn's algorithm
/// 2. Initialize $`d[q_0] = \bar{1}`$, $`d[q] = \bar{0}`$ otherwise
/// 3. For each state $`q`$ in topological order:
///    - For each arc $`(q, w, r)`$: $`d[r] \leftarrow d[r] \oplus (d[q] \otimes w)`$
///
/// # Errors
///
/// Returns [`Error::InvalidOperation`] if the FST
/// contains cycles (detected during topological sort).
///
/// # Example
///
/// ```rust
/// use arcweight::prelude::*;
/// use arcweight::algorithms::shortest_distance_acyclic;
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
/// fst.set_final(s1, TropicalWeight::one());
/// fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(2.5), s1));
///
/// let distances = shortest_distance_acyclic(&fst)?;
/// assert_eq!(*distances[0].value(), 0.0);  // Start state (tropical one)
/// assert_eq!(*distances[1].value(), 2.5);  // Via single arc
/// # Ok::<(), arcweight::Error>(())
/// ```
///
/// # References
///
/// \[1\] Mohri, M. 2002. Semiring frameworks and algorithms for shortest-distance
///     problems. *Journal of Automata, Languages and Combinatorics* 7, 3, 321-350.
///
/// # See Also
///
/// - [`shortest_distance`](crate::algorithms::shortest_distance()) - General algorithm for cyclic FSTs
/// - [`is_acyclic`] - Check if FST is acyclic
/// - [`shortest_distance_auto`] - Automatic algorithm selection
pub fn shortest_distance_acyclic<W, F>(fst: &F) -> Result<Vec<W>>
where
    W: Semiring,
    F: Fst<W>,
{
    let num_states = fst.num_states();
    if num_states == 0 {
        return Ok(Vec::new());
    }

    let start = match fst.start() {
        Some(s) => s,
        None => return Ok(vec![W::zero(); num_states]),
    };

    // Compute topological order using Kahn's algorithm
    let topo_order = topological_sort(fst)?;

    // Initialize distances
    let mut distances = vec![W::zero(); num_states];
    distances[start as usize] = W::one();

    // Process states in topological order
    for &state in &topo_order {
        let dist = distances[state as usize].clone();
        // Skip unreachable states: if distance is the semiring zero (additive identity),
        // no paths can reach this state. For tropical/log semirings, zero = infinity.
        // This optimization is safe because zero.times(w) = zero for any weight w.
        if Semiring::is_zero(&dist) {
            continue;
        }

        // Relax all outgoing arcs
        for arc in fst.arcs(state) {
            let new_dist = dist.times(&arc.weight);
            distances[arc.nextstate as usize] = distances[arc.nextstate as usize].plus(&new_dist);
        }
    }

    Ok(distances)
}

/// Computes reverse shortest distances (to final states) for acyclic FSTs.
///
/// Computes the shortest distance from each state to any final state in
/// $`O(V+E)`$ time. This is the "backward" version of shortest distance,
/// useful for:
///
/// - Weight pushing (toward final states)
/// - A* search heuristics
/// - Pruning based on total path weights
///
/// # Type Parameters
///
/// * `W` - Weight type implementing [`Semiring`]
/// * `F` - FST type implementing [`Fst`]
///
/// # Arguments
///
/// * `fst` - Input FST (must be acyclic)
///
/// # Returns
///
/// Vector of weights where `result[s]` is the shortest distance from state
/// `s` to any final state (including final weights).
///
/// # Complexity
///
/// - **Time:** $`O(V + E)`$
/// - **Space:** $`O(V + E)`$ (for reverse adjacency list)
///
/// # Algorithm
///
/// 1. Build reverse adjacency list
/// 2. Compute topological order and reverse it
/// 3. Initialize with final weights
/// 4. Process states in reverse topological order, propagating distances backward
///
/// # Example
///
/// ```rust
/// use arcweight::prelude::*;
/// use arcweight::algorithms::shortest_distance_acyclic_reverse;
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// let s2 = fst.add_state();
/// fst.set_start(s0);
/// fst.set_final(s2, TropicalWeight::new(0.5));
///
/// fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(1.0), s1));
/// fst.add_arc(s1, Arc::new(2, 2, TropicalWeight::new(2.0), s2));
///
/// let d = shortest_distance_acyclic_reverse(&fst)?;
/// // d[s2] = 0.5 (final weight)
/// // d[s1] = 2.0 + 0.5 = 2.5
/// // d[s0] = 1.0 + 2.0 + 0.5 = 3.5
/// # Ok::<(), arcweight::Error>(())
/// ```
pub fn shortest_distance_acyclic_reverse<W, F>(fst: &F) -> Result<Vec<W>>
where
    W: Semiring,
    F: Fst<W>,
{
    let num_states = fst.num_states();
    if num_states == 0 {
        return Ok(Vec::new());
    }

    // Compute topological order and reverse it
    let mut topo_order = topological_sort(fst)?;
    topo_order.reverse();

    // Initialize distances from final states
    let mut distances = vec![W::zero(); num_states];
    for state in fst.states() {
        if let Some(w) = fst.final_weight(state) {
            distances[state as usize] = w.clone();
        }
    }

    // Build reverse adjacency list for efficient backward traversal
    let mut incoming: Vec<Vec<(StateId, W)>> = vec![Vec::new(); num_states];
    for state in fst.states() {
        for arc in fst.arcs(state) {
            incoming[arc.nextstate as usize].push((state, arc.weight.clone()));
        }
    }

    // Process states in reverse topological order
    for &state in &topo_order {
        let dist = distances[state as usize].clone();
        if Semiring::is_zero(&dist) {
            continue;
        }

        // Propagate distances backward
        for (prev_state, weight) in &incoming[state as usize] {
            let new_dist = weight.times(&dist);
            distances[*prev_state as usize] = distances[*prev_state as usize].plus(&new_dist);
        }
    }

    Ok(distances)
}

/// Computes topological order using Kahn's algorithm.
///
/// Returns states in topological order where all arcs go from earlier to
/// later states. Returns an error if the FST contains cycles.
///
/// # Complexity
///
/// - **Time:** $`O(V + E)`$
/// - **Space:** $`O(V)`$
///
/// # References
///
/// \[1\] Kahn, A. B. 1962. Topological sorting of large networks. *Communications
///     of the ACM* 5, 11 (November 1962), 558-562. DOI: <https://doi.org/10.1145/368996.369025>
fn topological_sort<W, F>(fst: &F) -> Result<Vec<StateId>>
where
    W: Semiring,
    F: Fst<W>,
{
    let num_states = fst.num_states();
    if num_states == 0 {
        return Ok(Vec::new());
    }

    // Compute in-degrees
    let mut in_degree = vec![0usize; num_states];
    for state in fst.states() {
        for arc in fst.arcs(state) {
            in_degree[arc.nextstate as usize] += 1;
        }
    }

    // Initialize queue with states that have no incoming edges
    let mut queue: Vec<StateId> = Vec::new();
    for state in fst.states() {
        if in_degree[state as usize] == 0 {
            queue.push(state);
        }
    }

    // Process queue
    let mut result = Vec::with_capacity(num_states);
    while let Some(state) = queue.pop() {
        result.push(state);

        for arc in fst.arcs(state) {
            in_degree[arc.nextstate as usize] -= 1;
            if in_degree[arc.nextstate as usize] == 0 {
                queue.push(arc.nextstate);
            }
        }
    }

    // Check for cycles
    if result.len() != num_states {
        return Err(Error::InvalidOperation(
            "FST contains cycles - use general shortest_distance instead".to_string(),
        ));
    }

    Ok(result)
}

/// Checks if an FST is acyclic (contains no cycles).
///
/// Uses topological sort to detect cycles in $`O(V+E)`$ time. This can be used
/// to determine whether [`shortest_distance_acyclic`] is applicable.
///
/// # Example
///
/// ```rust
/// use arcweight::prelude::*;
/// use arcweight::algorithms::is_acyclic;
///
/// // Acyclic FST
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
/// fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::one(), s1));
/// assert!(is_acyclic(&fst));
///
/// // Cyclic FST
/// fst.add_arc(s1, Arc::new(2, 2, TropicalWeight::one(), s0)); // Creates cycle
/// assert!(!is_acyclic(&fst));
/// ```
pub fn is_acyclic<W, F>(fst: &F) -> bool
where
    W: Semiring,
    F: Fst<W>,
{
    topological_sort(fst).is_ok()
}

/// Automatically selects the optimal shortest distance algorithm.
///
/// Checks if the FST is acyclic and uses the $`O(V+E)`$ algorithm if possible,
/// otherwise falls back to the general iterative algorithm for cyclic FSTs.
///
/// This is the recommended entry point when the FST structure is unknown.
///
/// # Example
///
/// ```rust
/// use arcweight::prelude::*;
/// use arcweight::algorithms::shortest_distance_auto;
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
/// fst.set_final(s1, TropicalWeight::one());
/// fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(1.0), s1));
///
/// // Automatically uses acyclic algorithm for this FST
/// let distances = shortest_distance_auto(&fst)?;
/// # Ok::<(), arcweight::Error>(())
/// ```
///
/// # See Also
///
/// - [`shortest_distance_acyclic`] - Explicit acyclic algorithm
/// - [`shortest_distance`](crate::algorithms::shortest_distance()) - General algorithm
/// - [`is_acyclic`] - Check FST structure
pub fn shortest_distance_auto<W, F>(fst: &F) -> Result<Vec<W>>
where
    W: Semiring,
    F: Fst<W>,
{
    // Try acyclic algorithm first (will fail if cycles exist)
    match shortest_distance_acyclic(fst) {
        Ok(distances) => Ok(distances),
        Err(_) => {
            // Fall back to general algorithm
            crate::algorithms::shortest_distance(fst)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fst::{MutableFst, VectorFst};
    use crate::semiring::{Semiring, TropicalWeight};
    use num_traits::One;

    #[test]
    fn test_acyclic_shortest_distance_simple() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();

        fst.set_start(s0);
        fst.set_final(s2, TropicalWeight::one());

        // Two paths: s0 -> s2 (weight 3.0) or s0 -> s1 -> s2 (weight 1.0 + 0.5 = 1.5)
        fst.add_arc(s0, crate::arc::Arc::new(1, 1, TropicalWeight::new(3.0), s2));
        fst.add_arc(s0, crate::arc::Arc::new(2, 2, TropicalWeight::new(1.0), s1));
        fst.add_arc(s1, crate::arc::Arc::new(3, 3, TropicalWeight::new(0.5), s2));

        let distances = shortest_distance_acyclic(&fst).unwrap();

        assert_eq!(*distances[0].value(), 0.0); // Start state
        assert_eq!(*distances[1].value(), 1.0); // Via s0 -> s1
        assert_eq!(*distances[2].value(), 1.5); // min(3.0, 1.0 + 0.5) = 1.5
    }

    #[test]
    fn test_acyclic_shortest_distance_chain() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let mut states = Vec::new();

        for _ in 0..5 {
            states.push(fst.add_state());
        }

        fst.set_start(states[0]);
        fst.set_final(states[4], TropicalWeight::one());

        for i in 0..4 {
            fst.add_arc(
                states[i],
                crate::arc::Arc::new(1, 1, TropicalWeight::new(1.0), states[i + 1]),
            );
        }

        let distances = shortest_distance_acyclic(&fst).unwrap();

        for (i, dist) in distances.iter().enumerate() {
            assert_eq!(*dist.value(), i as f32);
        }
    }

    #[test]
    fn test_acyclic_shortest_distance_unreachable() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let _s2 = fst.add_state(); // Unreachable

        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(s0, crate::arc::Arc::new(1, 1, TropicalWeight::new(1.0), s1));

        let distances = shortest_distance_acyclic(&fst).unwrap();

        assert_eq!(*distances[0].value(), 0.0);
        assert_eq!(*distances[1].value(), 1.0);
        assert!(Semiring::is_zero(&distances[2])); // Unreachable
    }

    #[test]
    fn test_cyclic_fst_detection() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();

        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());

        // Create a cycle
        fst.add_arc(s0, crate::arc::Arc::new(1, 1, TropicalWeight::new(1.0), s1));
        fst.add_arc(s1, crate::arc::Arc::new(2, 2, TropicalWeight::new(1.0), s0));

        assert!(shortest_distance_acyclic::<TropicalWeight, _>(&fst).is_err());
        assert!(!is_acyclic::<TropicalWeight, _>(&fst));
    }

    #[test]
    fn test_is_acyclic() {
        // Acyclic FST
        let mut fst1 = VectorFst::<TropicalWeight>::new();
        let s0 = fst1.add_state();
        let s1 = fst1.add_state();
        fst1.set_start(s0);
        fst1.set_final(s1, TropicalWeight::one());
        fst1.add_arc(s0, crate::arc::Arc::new(1, 1, TropicalWeight::one(), s1));

        assert!(is_acyclic(&fst1));

        // Cyclic FST
        let mut fst2 = VectorFst::<TropicalWeight>::new();
        let t0 = fst2.add_state();
        let t1 = fst2.add_state();
        fst2.set_start(t0);
        fst2.add_arc(t0, crate::arc::Arc::new(1, 1, TropicalWeight::one(), t1));
        fst2.add_arc(t1, crate::arc::Arc::new(2, 2, TropicalWeight::one(), t0));

        assert!(!is_acyclic(&fst2));
    }

    #[test]
    fn test_reverse_shortest_distance() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();

        fst.set_start(s0);
        fst.set_final(s2, TropicalWeight::new(0.5));

        fst.add_arc(s0, crate::arc::Arc::new(1, 1, TropicalWeight::new(1.0), s1));
        fst.add_arc(s1, crate::arc::Arc::new(2, 2, TropicalWeight::new(2.0), s2));

        let distances = shortest_distance_acyclic_reverse(&fst).unwrap();

        // s2 -> final: 0.5 (final weight)
        assert_eq!(*distances[2].value(), 0.5);
        // s1 -> s2 -> final: 2.0 + 0.5 = 2.5
        assert_eq!(*distances[1].value(), 2.5);
        // s0 -> s1 -> s2 -> final: 1.0 + 2.0 + 0.5 = 3.5
        assert_eq!(*distances[0].value(), 3.5);
    }

    #[test]
    fn test_empty_fst() {
        let fst = VectorFst::<TropicalWeight>::new();
        let distances = shortest_distance_acyclic(&fst).unwrap();
        assert!(distances.is_empty());
    }

    #[test]
    fn test_auto_selects_acyclic() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(s0, crate::arc::Arc::new(1, 1, TropicalWeight::new(1.0), s1));

        // Auto should work for acyclic FSTs
        let distances = shortest_distance_auto(&fst).unwrap();
        assert_eq!(*distances[1].value(), 1.0);
    }
}