lling-llang 0.1.0

WFST framework for text normalization and grammar correction
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! Weighted minimization algorithm for WFSTs.
//!
//! Minimization produces a WFST with the minimum number of states that
//! accepts the same weighted language. This is achieved by:
//!
//! 1. Weight pushing (to normalize weight distribution)
//! 2. Partition refinement (treating (label, weight) as atomic symbols)
//!
//! # Algorithm
//!
//! The algorithm follows Mohri's approach:
//! 1. Push weights to ensure canonical form
//! 2. Compute equivalence classes using Hopcroft-style partition refinement
//! 3. Merge equivalent states
//!
//! # Complexity
//!
//! - Acyclic: O(|Q| + |E|)
//! - General: O(|E| log |Q|) with Hopcroft's algorithm
//!
//! # Requirements
//!
//! - Input must be deterministic
//! - Semiring must be divisible (for weight pushing)
//!
//! # References
//!
//! - Mohri, M. (2009). "Weighted Automata Algorithms"
//! - Hopcroft, J. (1971). "An n log n algorithm for minimizing states in a finite automaton"

use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::hash::Hash;

use crate::semiring::{DivisibleSemiring, QuantizableSemiring, Semiring};
use crate::wfst::{MutableWfst, StateId, WeightedTransition, Wfst, NO_STATE};

/// Default epsilon for floating-point weight comparison during minimization.
/// Weights within this tolerance are considered equal for partition refinement.
/// This addresses floating-point artifacts introduced by weight pushing's division operations.
const MINIMIZE_EPSILON: f64 = 1e-10;

/// A wrapper for quantized weight values for hashable approximate comparison.
///
/// This enables HashMap-based partition refinement with floating-point weights
/// by using the `QuantizableSemiring::quantize()` method. This addresses the issue
/// where weight pushing introduces tiny floating-point differences that incorrectly
/// separate equivalent states.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct QuantizedWeight {
    /// Weight value quantized as integer via QuantizableSemiring::quantize.
    quantized: i64,
}

impl QuantizedWeight {
    /// Create a quantized weight from a QuantizableSemiring value.
    ///
    /// # Arguments
    /// * `weight` - The weight to quantize
    /// * `epsilon` - The quantization precision (values within epsilon are equal)
    fn from_weight<W: QuantizableSemiring>(weight: &W, epsilon: f64) -> Self {
        Self {
            quantized: weight.quantize(epsilon),
        }
    }
}

/// Configuration for minimization.
#[derive(Clone, Debug)]
pub struct MinimizeConfig {
    /// Push weights before minimizing (recommended).
    pub push_weights: bool,
    /// Direction for weight pushing.
    pub push_direction: crate::algorithms::PushDirection,
    /// Whether to connect (trim) before minimization.
    pub connect_first: bool,
    /// Epsilon for weight comparison during partition refinement.
    /// Weights within this tolerance are considered equal.
    /// This addresses floating-point artifacts from weight pushing.
    pub weight_epsilon: f64,
}

impl Default for MinimizeConfig {
    fn default() -> Self {
        Self {
            push_weights: true,
            push_direction: crate::algorithms::PushDirection::Forward,
            connect_first: true,
            weight_epsilon: MINIMIZE_EPSILON,
        }
    }
}

impl MinimizeConfig {
    /// Standard minimization with weight pushing.
    pub fn standard() -> Self {
        Self::default()
    }

    /// Minimize without weight pushing (input must already be pushed).
    pub fn no_push() -> Self {
        Self {
            push_weights: false,
            push_direction: crate::algorithms::PushDirection::Forward,
            connect_first: true,
            weight_epsilon: MINIMIZE_EPSILON,
        }
    }

    /// Create config with custom weight epsilon.
    pub fn with_epsilon(epsilon: f64) -> Self {
        Self {
            weight_epsilon: epsilon,
            ..Self::default()
        }
    }
}

/// Error during minimization.
#[derive(Clone, Debug, PartialEq)]
pub enum MinimizeError {
    /// No start state defined.
    NoStartState,
    /// Input WFST is not deterministic.
    NotDeterministic,
    /// Weight pushing failed.
    PushError(String),
}

impl std::fmt::Display for MinimizeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MinimizeError::NoStartState => write!(f, "WFST has no start state"),
            MinimizeError::NotDeterministic => {
                write!(f, "WFST must be deterministic before minimization")
            }
            MinimizeError::PushError(msg) => write!(f, "Weight pushing failed: {}", msg),
        }
    }
}

impl std::error::Error for MinimizeError {}

/// A signature for a state, used for partition refinement.
///
/// Uses QuantizedWeight instead of raw weights to enable approximate comparison,
/// addressing floating-point artifacts from weight pushing.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct StateSignature<L: Ord + Hash> {
    /// Quantized final weight (or None if not final)
    final_weight: Option<QuantizedWeight>,
    /// Sorted list of (input_label, output_label, quantized_weight, target_partition_id)
    transitions: Vec<(Option<L>, Option<L>, QuantizedWeight, usize)>,
}

impl<L: Ord + Hash + Clone> StateSignature<L> {
    fn new() -> Self {
        Self {
            final_weight: None,
            transitions: Vec::new(),
        }
    }
}

/// Minimize a deterministic WFST.
///
/// Produces a WFST with the minimum number of states accepting the same
/// weighted language.
///
/// # Requirements
///
/// - Input must be deterministic (use `determinize` first if needed)
/// - Semiring must be divisible for weight pushing
/// - Semiring must implement `QuantizableSemiring` for approximate weight comparison
///
/// # Returns
///
/// A new minimized WFST, or an error if minimization fails.
///
/// # Example
///
/// ```ignore
/// use lling_llang::algorithms::{minimize, MinimizeConfig, determinize, DeterminizeConfig};
///
/// let fst = build_some_wfst();
/// let det_fst = determinize(&fst, DeterminizeConfig::standard())?;
/// let min_fst = minimize(&det_fst, MinimizeConfig::standard())?;
/// ```
pub fn minimize<L, W, F>(fst: &F, config: MinimizeConfig) -> Result<F, MinimizeError>
where
    L: Clone + Eq + Hash + Ord + Debug,
    W: DivisibleSemiring + QuantizableSemiring + PartialOrd + Clone + Debug,
    F: MutableWfst<L, W> + Wfst<L, W> + Default + Clone,
{
    let n = fst.num_states();
    if n == 0 {
        return Ok(F::default());
    }

    let start = fst.start();
    if start == NO_STATE {
        return Err(MinimizeError::NoStartState);
    }

    // Check that input is deterministic
    if !super::determinize::is_deterministic(fst) {
        return Err(MinimizeError::NotDeterministic);
    }

    // Clone and preprocess
    let mut working = fst.clone();

    // Optionally connect first
    if config.connect_first {
        use crate::algorithms::{connect, ConnectConfig};
        connect(&mut working, ConnectConfig::trim());
    }

    // Optionally push weights
    if config.push_weights {
        use crate::algorithms::{push_weights, PushConfig, ShortestDistanceConfig};
        let push_config = PushConfig {
            direction: config.push_direction.clone(),
            remove_non_coaccessible: false, // We already connected if needed
            distance_config: ShortestDistanceConfig::default(),
        };
        push_weights(&mut working, push_config)
            .map_err(|e| MinimizeError::PushError(e.to_string()))?;
    }

    // Partition refinement to find equivalent states
    let partitions = compute_partitions(&working, config.weight_epsilon)?;

    // Build minimized WFST from partitions
    build_minimized(&working, &partitions)
}

/// Compute state partitions using iterative refinement.
///
/// Uses `QuantizableSemiring::quantize()` for approximate comparison to handle
/// floating-point artifacts from weight pushing.
fn compute_partitions<L, W, F>(fst: &F, epsilon: f64) -> Result<Vec<usize>, MinimizeError>
where
    L: Clone + Eq + Hash + Ord + Debug,
    W: QuantizableSemiring + Clone + Debug,
    F: Wfst<L, W>,
{
    let n = fst.num_states();
    if n == 0 {
        return Ok(Vec::new());
    }

    // Initial partition: separate by quantized final weight
    let mut partition: Vec<usize> = vec![0; n];
    let mut num_partitions = 0;

    // Separate by quantized final weight
    let mut final_weight_to_partition: HashMap<Option<QuantizedWeight>, usize> = HashMap::new();

    for state in 0..n {
        let state_id = state as StateId;
        let fw = if fst.is_final(state_id) {
            Some(QuantizedWeight::from_weight(
                &fst.final_weight(state_id),
                epsilon,
            ))
        } else {
            None
        };

        if let Some(&p) = final_weight_to_partition.get(&fw) {
            partition[state] = p;
        } else {
            let p = num_partitions;
            num_partitions += 1;
            final_weight_to_partition.insert(fw, p);
            partition[state] = p;
        }
    }

    // Iterative refinement
    let mut changed = true;
    while changed {
        changed = false;

        // Compute signatures for each state based on current partition
        let mut signature_to_partition: HashMap<StateSignature<L>, usize> = HashMap::new();
        let mut new_partition: Vec<usize> = vec![0; n];
        let mut new_num_partitions = 0;

        for state in 0..n {
            let state_id = state as StateId;

            // Build signature with quantized weights
            let mut sig = StateSignature::new();

            if fst.is_final(state_id) {
                sig.final_weight = Some(QuantizedWeight::from_weight(
                    &fst.final_weight(state_id),
                    epsilon,
                ));
            }

            let mut trans_sigs: Vec<(Option<L>, Option<L>, QuantizedWeight, usize)> = Vec::new();
            for trans in fst.transitions(state_id) {
                let target_partition = partition[trans.to as usize];
                trans_sigs.push((
                    trans.input.clone(),
                    trans.output.clone(),
                    QuantizedWeight::from_weight(&trans.weight, epsilon),
                    target_partition,
                ));
            }

            // Sort for canonical form
            trans_sigs.sort_by(|a, b| {
                a.0.cmp(&b.0)
                    .then_with(|| a.1.cmp(&b.1))
                    .then_with(|| a.3.cmp(&b.3))
            });
            sig.transitions = trans_sigs;

            // Look up or create partition for this signature
            if let Some(&p) = signature_to_partition.get(&sig) {
                new_partition[state] = p;
            } else {
                let p = new_num_partitions;
                new_num_partitions += 1;
                signature_to_partition.insert(sig, p);
                new_partition[state] = p;
            }
        }

        // Check if partitions changed
        if new_num_partitions > num_partitions {
            changed = true;
            partition = new_partition;
            num_partitions = new_num_partitions;
        }
    }

    Ok(partition)
}

/// Build minimized WFST from partition assignments.
fn build_minimized<L, W, F>(fst: &F, partitions: &[usize]) -> Result<F, MinimizeError>
where
    L: Clone + Eq + Hash + Ord + Debug,
    W: Semiring + Clone + Debug,
    F: MutableWfst<L, W> + Wfst<L, W> + Default,
{
    let n = fst.num_states();
    if n == 0 {
        return Ok(F::default());
    }

    // Find number of partitions (new states)
    let num_new_states = partitions.iter().max().map(|&m| m + 1).unwrap_or(0);

    // Find representative state for each partition
    let mut partition_to_rep: HashMap<usize, StateId> = HashMap::new();
    for state in 0..n {
        let p = partitions[state];
        partition_to_rep.entry(p).or_insert(state as StateId);
    }

    // Create new WFST
    let mut result = F::default();
    for _ in 0..num_new_states {
        result.add_state();
    }

    // Set start state
    let old_start = fst.start();
    if old_start != NO_STATE {
        let new_start = partitions[old_start as usize];
        result.set_start(new_start as StateId);
    }

    // Add transitions and final weights from representatives
    let mut added_transitions: HashSet<(usize, Option<L>, Option<L>, usize)> = HashSet::new();

    for (partition, &rep) in &partition_to_rep {
        let new_state = *partition as StateId;

        // Set final weight if representative is final
        if fst.is_final(rep) {
            result.set_final(new_state, fst.final_weight(rep));
        }

        // Add transitions (avoiding duplicates)
        for trans in fst.transitions(rep) {
            let target_partition = partitions[trans.to as usize];
            let key = (
                *partition,
                trans.input.clone(),
                trans.output.clone(),
                target_partition,
            );

            if !added_transitions.contains(&key) {
                added_transitions.insert(key);

                let new_trans = WeightedTransition {
                    from: new_state,
                    to: target_partition as StateId,
                    input: trans.input.clone(),
                    output: trans.output.clone(),
                    weight: trans.weight.clone(),
                };
                result.add_transition(new_trans);
            }
        }
    }

    Ok(result)
}

/// Count the number of states that can be removed by minimization.
///
/// This is a quick estimate without actually performing minimization.
/// Uses the default weight epsilon for comparison.
pub fn estimate_reduction<L, W, F>(fst: &F) -> usize
where
    L: Clone + Eq + Hash + Ord + Debug,
    W: QuantizableSemiring + Clone + Debug,
    F: Wfst<L, W>,
{
    estimate_reduction_with_epsilon(fst, MINIMIZE_EPSILON)
}

/// Count the number of states that can be removed by minimization.
///
/// Uses a custom epsilon for weight comparison.
pub fn estimate_reduction_with_epsilon<L, W, F>(fst: &F, epsilon: f64) -> usize
where
    L: Clone + Eq + Hash + Ord + Debug,
    W: QuantizableSemiring + Clone + Debug,
    F: Wfst<L, W>,
{
    let n = fst.num_states();
    if n == 0 {
        return 0;
    }

    if let Ok(partitions) = compute_partitions(fst, epsilon) {
        let num_new_states = partitions.iter().max().map(|&m| m + 1).unwrap_or(0);
        n.saturating_sub(num_new_states)
    } else {
        0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::semiring::TropicalWeight;
    use crate::wfst::{VectorWfst, VectorWfstBuilder};

    // Property-based tests
    mod property_tests {
        use super::*;
        use crate::test_utils::arb_deterministic_wfst_tropical;
        use proptest::prelude::*;

        proptest! {
            /// Minimization should never increase state count.
            ///
            /// This property was previously disabled due to a bug where weight pushing
            /// introduced floating-point artifacts that caused incorrect state separation.
            /// Fixed by using QuantizedWeight for approximate comparison.
            #[test]
            fn minimize_reduces_or_maintains_states(
                fst in arb_deterministic_wfst_tropical(8, 3)
            ) {
                if fst.num_states() == 0 {
                    return Ok(());
                }

                let original_states = fst.num_states();
                let result = minimize(&fst, MinimizeConfig::standard());

                if let Ok(min_fst) = result {
                    prop_assert!(
                        min_fst.num_states() <= original_states,
                        "Minimization increased states from {} to {}",
                        original_states,
                        min_fst.num_states()
                    );
                }
            }

            /// Minimization is idempotent: min(min(F)) ≈ min(F).
            ///
            /// Applying minimization twice should produce the same result as once.
            #[test]
            fn minimize_idempotent(
                fst in arb_deterministic_wfst_tropical(6, 2)
            ) {
                if fst.num_states() == 0 {
                    return Ok(());
                }

                let result1 = minimize(&fst, MinimizeConfig::standard());
                if let Ok(min1) = result1 {
                    let result2 = minimize(&min1, MinimizeConfig::standard());
                    if let Ok(min2) = result2 {
                        prop_assert_eq!(
                            min1.num_states(),
                            min2.num_states(),
                            "Minimization not idempotent: first pass {} states, second pass {} states",
                            min1.num_states(),
                            min2.num_states()
                        );
                    }
                }
            }

            /// Minimized FST should still be deterministic.
            #[test]
            fn minimize_preserves_determinism(
                fst in arb_deterministic_wfst_tropical(8, 3)
            ) {
                if fst.num_states() == 0 {
                    return Ok(());
                }

                let result = minimize(&fst, MinimizeConfig::standard());
                if let Ok(min_fst) = result {
                    prop_assert!(
                        super::super::super::determinize::is_deterministic(&min_fst),
                        "Minimized FST should be deterministic"
                    );
                }
            }

            /// Estimate reduction provides a reasonable bound.
            /// Note: estimate_reduction doesn't do weight pushing, so it may differ
            /// significantly from actual reduction. We only check it's non-negative
            /// and doesn't exceed the state count.
            #[test]
            fn estimate_reduction_bounds(
                fst in arb_deterministic_wfst_tropical(6, 2)
            ) {
                if fst.num_states() <= 1 {
                    return Ok(());
                }

                let estimated = estimate_reduction(&fst);
                let original_states = fst.num_states();

                // Estimate should be bounded by state count
                prop_assert!(
                    estimated <= original_states,
                    "Estimated reduction {} exceeds state count {}",
                    estimated,
                    original_states
                );

                // Just verify minimize works (actual comparison is unreliable
                // because weight pushing changes state signatures)
                let result = minimize(&fst, MinimizeConfig::standard());
                prop_assert!(
                    result.is_ok() || matches!(result, Err(MinimizeError::PushError(_))),
                    "Minimize failed unexpectedly: {:?}",
                    result
                );
            }
        }
    }

    fn build_redundant_fst() -> VectorWfst<char, TropicalWeight> {
        // Two equivalent branches that should be merged:
        // 0 --a--> 1 --b--> 3 (final)
        //    ` --> 2 --b--> 4 (final)
        // States 1,2 and 3,4 are equivalent pairs
        let mut fst = VectorWfst::new();
        fst.add_states(5);
        fst.set_start(0);
        fst.add_arc(0, Some('a'), Some('a'), 1, TropicalWeight::new(1.0));
        fst.add_arc(0, Some('c'), Some('c'), 2, TropicalWeight::new(1.0));
        fst.add_arc(1, Some('b'), Some('b'), 3, TropicalWeight::new(1.0));
        fst.add_arc(2, Some('b'), Some('b'), 4, TropicalWeight::new(1.0));
        fst.set_final(3, TropicalWeight::one());
        fst.set_final(4, TropicalWeight::one());
        fst
    }

    fn build_minimal_fst() -> VectorWfst<char, TropicalWeight> {
        // Already minimal: 0 --a--> 1 --b--> 2 (final)
        VectorWfstBuilder::new()
            .add_states(3)
            .start(0)
            .arc(0, Some('a'), Some('a'), 1, TropicalWeight::new(1.0))
            .arc(1, Some('b'), Some('b'), 2, TropicalWeight::new(2.0))
            .final_state(2, TropicalWeight::one())
            .build()
    }

    fn build_chain_with_equiv_states() -> VectorWfst<char, TropicalWeight> {
        // 0 --a--> 1 --b--> 2 --c--> 3 (final)
        //                   ` --c--> 4 (final)
        // States 3 and 4 are equivalent (same transitions, same final weight)
        let mut fst = VectorWfst::new();
        fst.add_states(5);
        fst.set_start(0);
        fst.add_arc(0, Some('a'), Some('a'), 1, TropicalWeight::new(1.0));
        fst.add_arc(1, Some('b'), Some('b'), 2, TropicalWeight::new(1.0));
        fst.add_arc(2, Some('c'), Some('c'), 3, TropicalWeight::new(1.0));
        fst.add_arc(2, Some('d'), Some('d'), 4, TropicalWeight::new(1.0));
        fst.set_final(3, TropicalWeight::one());
        fst.set_final(4, TropicalWeight::one());
        fst
    }

    #[test]
    fn test_minimize_empty() {
        let fst: VectorWfst<char, TropicalWeight> = VectorWfst::new();
        let result = minimize(&fst, MinimizeConfig::standard())
            .expect("algorithms/minimize.rs: required value was None/Err");
        assert_eq!(result.num_states(), 0);
    }

    #[test]
    fn test_minimize_already_minimal() {
        let fst = build_minimal_fst();
        let result = minimize(&fst, MinimizeConfig::standard())
            .expect("algorithms/minimize.rs: required value was None/Err");

        // Should have same or fewer states
        assert!(result.num_states() <= fst.num_states());
    }

    #[test]
    fn test_minimize_redundant() {
        let fst = build_redundant_fst();
        let initial_states = fst.num_states();

        let result = minimize(&fst, MinimizeConfig::standard())
            .expect("algorithms/minimize.rs: required value was None/Err");

        // Should have fewer states (3,4 merged into one)
        assert!(
            result.num_states() < initial_states,
            "Expected fewer than {} states, got {}",
            initial_states,
            result.num_states()
        );
    }

    #[test]
    fn test_minimize_non_deterministic_fails() {
        // Create a non-deterministic FST
        let mut fst = VectorWfst::new();
        fst.add_states(3);
        fst.set_start(0);
        fst.add_arc(0, Some('a'), Some('a'), 1, TropicalWeight::new(1.0));
        fst.add_arc(0, Some('a'), Some('a'), 2, TropicalWeight::new(2.0)); // Same label!
        fst.set_final(1, TropicalWeight::one());
        fst.set_final(2, TropicalWeight::one());

        let result = minimize(&fst, MinimizeConfig::standard());
        assert!(matches!(result, Err(MinimizeError::NotDeterministic)));
    }

    #[test]
    fn test_minimize_chain_equiv() {
        let fst = build_chain_with_equiv_states();
        let initial_states = fst.num_states();

        let result = minimize(&fst, MinimizeConfig::standard())
            .expect("algorithms/minimize.rs: required value was None/Err");

        // Check we got a valid result
        assert!(result.num_states() > 0);
        assert!(result.num_states() <= initial_states);
    }

    #[test]
    fn test_estimate_reduction() {
        let redundant = build_redundant_fst();
        let reduction = estimate_reduction(&redundant);

        // Should estimate at least 1 state can be removed
        // (since states 3,4 are equivalent)
        assert!(reduction >= 1, "Expected reduction >= 1, got {}", reduction);
    }

    #[test]
    fn test_minimize_preserves_determinism() {
        let fst = build_redundant_fst();
        assert!(super::super::determinize::is_deterministic(&fst));

        let result = minimize(&fst, MinimizeConfig::standard())
            .expect("algorithms/minimize.rs: required value was None/Err");
        assert!(super::super::determinize::is_deterministic(&result));
    }

    #[test]
    fn test_minimize_no_push_config() {
        let fst = build_minimal_fst();

        // With no push should still work for already-pushed FSTs
        let result = minimize(&fst, MinimizeConfig::no_push())
            .expect("algorithms/minimize.rs: required value was None/Err");
        assert!(result.num_states() <= fst.num_states());
    }
}