sim-lib-discrete-graph 0.2.0

Discrete graph algorithms.
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
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
//! Certified dynamic-time-warp and edit alignment.

use core::cmp::Ordering;

use crate::{
    AlgorithmControl, AlgorithmInterrupt, AlgorithmReceipt, FiniteCost, GraphError, NeverInterrupt,
    control::WorkMeter,
    cost::{add, compare, validate},
};

mod verify;

pub use verify::verify_alignment;

/// Window limiting which prefix-pair cells an alignment may visit.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum AlignmentWindow {
    /// Evaluate the full prefix grid.
    #[default]
    Unbounded,
    /// Permit cells whose prefix indices differ by at most `radius`.
    Radius(usize),
}

impl AlignmentWindow {
    fn contains(self, left: usize, right: usize) -> bool {
        match self {
            Self::Unbounded => true,
            Self::Radius(radius) => left.abs_diff(right) <= radius,
        }
    }
}

/// Endpoint semantics for sequence alignment.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum AlignmentBoundary {
    /// Align both complete sequences.
    #[default]
    Global,
    /// Align the complete left query to a contiguous region of the right
    /// sequence. Right-side prefix and suffix costs are free.
    Subsequence,
}

/// Whether to retain the full proof table or only the final rolling row.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum AlignmentMemory {
    /// Retain backpointers and return the full alignment path.
    #[default]
    Full,
    /// Retain two score rows while solving and return score-only evidence.
    RollingScoreOnly,
}

/// Costs for advancing only one side of an edit alignment.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GapPolicy<C> {
    /// Cost of consuming a left item without a right item.
    pub delete: C,
    /// Cost of consuming a right item without a left item.
    pub insert: C,
}

impl<C> GapPolicy<C> {
    /// Builds a gap policy with explicit deletion and insertion costs.
    pub fn new(delete: C, insert: C) -> Self {
        Self { delete, insert }
    }
}

/// Window, gap, boundary, and memory policy for dynamic alignment.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DtwPolicy<C> {
    /// Admissible prefix-grid cells.
    pub window: AlignmentWindow,
    /// One-sided step costs.
    pub gaps: GapPolicy<C>,
    /// Required start/end coverage.
    pub boundary: AlignmentBoundary,
    /// Retained proof material.
    pub memory: AlignmentMemory,
}

impl<C> DtwPolicy<C> {
    /// Builds a global, unbounded, full-memory alignment policy.
    pub fn new(gaps: GapPolicy<C>) -> Self {
        Self {
            window: AlignmentWindow::Unbounded,
            gaps,
            boundary: AlignmentBoundary::Global,
            memory: AlignmentMemory::Full,
        }
    }

    /// Returns a copy with a different alignment window.
    pub fn with_window(mut self, window: AlignmentWindow) -> Self {
        self.window = window;
        self
    }

    /// Returns a copy with different endpoint semantics.
    pub fn with_boundary(mut self, boundary: AlignmentBoundary) -> Self {
        self.boundary = boundary;
        self
    }

    /// Returns a copy with a different memory policy.
    pub fn with_memory(mut self, memory: AlignmentMemory) -> Self {
        self.memory = memory;
        self
    }
}

/// Stable predecessor move in a full alignment certificate.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AlignmentMove {
    /// Consume one item from each sequence.
    Match,
    /// Consume one left item.
    Delete,
    /// Consume one right item.
    Insert,
}

/// One reachable prefix-grid cell in a full alignment certificate.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AlignmentCell<C> {
    /// Minimum cost of reaching this cell.
    pub total_cost: C,
    /// Stable predecessor move, or `None` at a permitted free start.
    pub predecessor: Option<AlignmentMove>,
    /// Cost charged by the predecessor move.
    pub step_cost: Option<C>,
}

/// Optimality evidence retained under the selected memory policy.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AlignmentCertificate<C> {
    /// Full prefix-grid Bellman table with backpointers.
    Full {
        /// Row-major `(left prefix, right prefix)` cells.
        cells: Vec<Vec<Option<AlignmentCell<C>>>>,
    },
    /// Final score row reproduced by rolling-memory evaluation.
    Rolling {
        /// Cost at every right prefix after consuming the complete left input.
        final_row: Vec<Option<C>>,
        /// Stable selected right endpoint.
        endpoint: usize,
    },
}

/// One operation in a full alignment path.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AlignmentStep<C> {
    /// Pair one left and one right item.
    Match {
        /// Left input index.
        left: usize,
        /// Right input index.
        right: usize,
        /// Local pair cost.
        cost: C,
    },
    /// Consume an unmatched left item.
    Delete {
        /// Left input index.
        left: usize,
        /// Gap cost.
        cost: C,
    },
    /// Consume an unmatched right item.
    Insert {
        /// Right input index.
        right: usize,
        /// Gap cost.
        cost: C,
    },
}

/// Minimum-cost alignment plus proof and deterministic accounting.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Alignment<C> {
    /// Exact minimum score.
    pub score: C,
    /// Stable full path, absent in rolling score-only mode.
    pub steps: Option<Vec<AlignmentStep<C>>>,
    /// Proof material selected by the memory policy.
    pub certificate: AlignmentCertificate<C>,
    /// Cell/edge work accounting.
    pub receipt: AlgorithmReceipt,
}

/// Aligns two sequences under a window, gap, boundary, and memory policy.
pub fn dynamic_time_warp<T, C: FiniteCost>(
    left: &[T],
    right: &[T],
    local_cost: impl Fn(&T, &T) -> C,
    policy: DtwPolicy<C>,
) -> Result<Alignment<C>, GraphError> {
    dynamic_time_warp_with_control(
        left,
        right,
        local_cost,
        policy,
        &AlgorithmControl::default(),
        &NeverInterrupt,
    )
}

/// Aligns two sequences under explicit work and cancellation control.
pub fn dynamic_time_warp_with_control<T, C: FiniteCost>(
    left: &[T],
    right: &[T],
    local_cost: impl Fn(&T, &T) -> C,
    policy: DtwPolicy<C>,
    control: &AlgorithmControl,
    interrupt: &dyn AlgorithmInterrupt,
) -> Result<Alignment<C>, GraphError> {
    validate_policy(&policy)?;
    let memory = peak_memory(left.len(), right.len(), policy.memory)?;
    let mut meter = WorkMeter::new(control, interrupt, memory)?;
    let computed = match policy.memory {
        AlignmentMemory::Full => {
            let (cells, stats) = full_table(left, right, &local_cost, &policy, Some(&mut meter))?;
            let endpoint = select_endpoint(&cells, policy.boundary)?;
            let score = cells[left.len()][endpoint]
                .as_ref()
                .expect("selected endpoint is reachable")
                .total_cost
                .clone();
            let steps = reconstruct(&cells, left.len(), endpoint, policy.boundary)?;
            Computed {
                score,
                steps: Some(steps),
                certificate: AlignmentCertificate::Full { cells },
                stats,
            }
        }
        AlignmentMemory::RollingScoreOnly => {
            let (final_row, stats) =
                rolling_row(left, right, &local_cost, &policy, Some(&mut meter))?;
            let endpoint = select_rolling_endpoint(&final_row, policy.boundary, right.len())?;
            let score = final_row[endpoint]
                .as_ref()
                .expect("selected endpoint is reachable")
                .clone();
            Computed {
                score,
                steps: None,
                certificate: AlignmentCertificate::Rolling {
                    final_row,
                    endpoint,
                },
                stats,
            }
        }
    };
    let receipt = meter.finish();
    debug_assert_eq!(receipt.cells, computed.stats.cells);
    debug_assert_eq!(receipt.edges, computed.stats.edges);
    Ok(Alignment {
        score: computed.score,
        steps: computed.steps,
        certificate: computed.certificate,
        receipt,
    })
}

struct Computed<C> {
    score: C,
    steps: Option<Vec<AlignmentStep<C>>>,
    certificate: AlignmentCertificate<C>,
    stats: Stats,
}

#[derive(Clone, Copy, Debug, Default)]
struct Stats {
    cells: u64,
    edges: u64,
}

type Table<C> = Vec<Vec<Option<AlignmentCell<C>>>>;

fn full_table<T, C: FiniteCost>(
    left: &[T],
    right: &[T],
    local_cost: &impl Fn(&T, &T) -> C,
    policy: &DtwPolicy<C>,
    mut meter: Option<&mut WorkMeter<'_>>,
) -> Result<(Table<C>, Stats), GraphError> {
    let rows = left
        .len()
        .checked_add(1)
        .ok_or_else(|| GraphError::WeightOverflow("alignment rows".to_owned()))?;
    let columns = right
        .len()
        .checked_add(1)
        .ok_or_else(|| GraphError::WeightOverflow("alignment columns".to_owned()))?;
    let mut cells = vec![vec![None; columns]; rows];
    let mut stats = Stats::default();
    for i in 0..rows {
        for j in 0..columns {
            if !policy.window.contains(i, j) {
                continue;
            }
            charge_cell(&mut meter, &mut stats)?;
            cells[i][j] = compute_cell(
                i,
                j,
                left,
                right,
                local_cost,
                policy,
                |row, column| cells[row][column].clone(),
                &mut meter,
                &mut stats,
            )?;
        }
    }
    Ok((cells, stats))
}

#[allow(clippy::too_many_arguments)]
fn compute_cell<C: FiniteCost, T>(
    i: usize,
    j: usize,
    left: &[T],
    right: &[T],
    local_cost: &impl Fn(&T, &T) -> C,
    policy: &DtwPolicy<C>,
    lookup: impl Fn(usize, usize) -> Option<AlignmentCell<C>>,
    meter: &mut Option<&mut WorkMeter<'_>>,
    stats: &mut Stats,
) -> Result<Option<AlignmentCell<C>>, GraphError> {
    if i == 0 && (j == 0 || policy.boundary == AlignmentBoundary::Subsequence) {
        return Ok(Some(AlignmentCell {
            total_cost: C::zero(),
            predecessor: None,
            step_cost: None,
        }));
    }
    let mut best: Option<(C, AlignmentMove, C)> = None;
    if i > 0 && j > 0 {
        charge_edge(meter, stats)?;
        if let Some(previous) = lookup(i - 1, j - 1) {
            let cost = local_cost(&left[i - 1], &right[j - 1]);
            validate_non_negative(&cost, "alignment local cost")?;
            let total = add(&previous.total_cost, &cost, "alignment match")?;
            choose(&mut best, total, AlignmentMove::Match, cost)?;
        }
    }
    if i > 0 {
        charge_edge(meter, stats)?;
        if let Some(previous) = lookup(i - 1, j) {
            let total = add(
                &previous.total_cost,
                &policy.gaps.delete,
                "alignment deletion",
            )?;
            choose(
                &mut best,
                total,
                AlignmentMove::Delete,
                policy.gaps.delete.clone(),
            )?;
        }
    }
    if j > 0 {
        charge_edge(meter, stats)?;
        if let Some(previous) = lookup(i, j - 1) {
            let total = add(
                &previous.total_cost,
                &policy.gaps.insert,
                "alignment insertion",
            )?;
            choose(
                &mut best,
                total,
                AlignmentMove::Insert,
                policy.gaps.insert.clone(),
            )?;
        }
    }
    Ok(
        best.map(|(total_cost, predecessor, step_cost)| AlignmentCell {
            total_cost,
            predecessor: Some(predecessor),
            step_cost: Some(step_cost),
        }),
    )
}

fn rolling_row<T, C: FiniteCost>(
    left: &[T],
    right: &[T],
    local_cost: &impl Fn(&T, &T) -> C,
    policy: &DtwPolicy<C>,
    mut meter: Option<&mut WorkMeter<'_>>,
) -> Result<(Vec<Option<C>>, Stats), GraphError> {
    let columns = right
        .len()
        .checked_add(1)
        .ok_or_else(|| GraphError::WeightOverflow("alignment columns".to_owned()))?;
    let mut previous = vec![None; columns];
    let mut stats = Stats::default();
    for i in 0..=left.len() {
        let mut current = vec![None; columns];
        for j in 0..columns {
            if !policy.window.contains(i, j) {
                continue;
            }
            charge_cell(&mut meter, &mut stats)?;
            if i == 0 && (j == 0 || policy.boundary == AlignmentBoundary::Subsequence) {
                current[j] = Some(C::zero());
                continue;
            }
            let mut best: Option<C> = None;
            if i > 0 && j > 0 {
                charge_edge(&mut meter, &mut stats)?;
                if let Some(prior) = &previous[j - 1] {
                    let cost = local_cost(&left[i - 1], &right[j - 1]);
                    validate_non_negative(&cost, "alignment local cost")?;
                    choose_score(&mut best, add(prior, &cost, "alignment match")?)?;
                }
            }
            if i > 0 {
                charge_edge(&mut meter, &mut stats)?;
                if let Some(prior) = &previous[j] {
                    choose_score(
                        &mut best,
                        add(prior, &policy.gaps.delete, "alignment deletion")?,
                    )?;
                }
            }
            if j > 0 {
                charge_edge(&mut meter, &mut stats)?;
                if let Some(prior) = &current[j - 1] {
                    choose_score(
                        &mut best,
                        add(prior, &policy.gaps.insert, "alignment insertion")?,
                    )?;
                }
            }
            current[j] = best;
        }
        previous = current;
    }
    Ok((previous, stats))
}

fn choose<C: FiniteCost>(
    best: &mut Option<(C, AlignmentMove, C)>,
    total: C,
    movement: AlignmentMove,
    step_cost: C,
) -> Result<(), GraphError> {
    let replace = match best {
        Some((current, _, _)) => {
            compare(&total, current, "alignment candidate ordering")? == Ordering::Less
        }
        None => true,
    };
    if replace {
        *best = Some((total, movement, step_cost));
    }
    Ok(())
}

fn choose_score<C: FiniteCost>(best: &mut Option<C>, total: C) -> Result<(), GraphError> {
    let replace = match best {
        Some(current) => {
            compare(&total, current, "alignment candidate ordering")? == Ordering::Less
        }
        None => true,
    };
    if replace {
        *best = Some(total);
    }
    Ok(())
}

fn select_endpoint<C: FiniteCost>(
    cells: &Table<C>,
    boundary: AlignmentBoundary,
) -> Result<usize, GraphError> {
    let final_row = cells.last().expect("alignment table has a prefix row");
    select_cell_endpoint(final_row, boundary)
}

fn select_cell_endpoint<C: FiniteCost>(
    row: &[Option<AlignmentCell<C>>],
    boundary: AlignmentBoundary,
) -> Result<usize, GraphError> {
    match boundary {
        AlignmentBoundary::Global => row
            .len()
            .checked_sub(1)
            .filter(|endpoint| row[*endpoint].is_some())
            .ok_or(GraphError::Disconnected),
        AlignmentBoundary::Subsequence => {
            let mut best: Option<(usize, &C)> = None;
            for (index, cell) in row.iter().enumerate() {
                let Some(cell) = cell else {
                    continue;
                };
                let replace = match best {
                    Some((_, cost)) => {
                        compare(&cell.total_cost, cost, "alignment endpoint ordering")?
                            == Ordering::Less
                    }
                    None => true,
                };
                if replace {
                    best = Some((index, &cell.total_cost));
                }
            }
            best.map(|(index, _)| index).ok_or(GraphError::Disconnected)
        }
    }
}

fn select_rolling_endpoint<C: FiniteCost>(
    row: &[Option<C>],
    boundary: AlignmentBoundary,
    right_len: usize,
) -> Result<usize, GraphError> {
    match boundary {
        AlignmentBoundary::Global => row
            .get(right_len)
            .and_then(Option::as_ref)
            .map(|_| right_len)
            .ok_or(GraphError::Disconnected),
        AlignmentBoundary::Subsequence => {
            let mut best: Option<(usize, &C)> = None;
            for (index, cost) in row.iter().enumerate() {
                let Some(cost) = cost else {
                    continue;
                };
                let replace = match best {
                    Some((_, current)) => {
                        compare(cost, current, "alignment endpoint ordering")? == Ordering::Less
                    }
                    None => true,
                };
                if replace {
                    best = Some((index, cost));
                }
            }
            best.map(|(index, _)| index).ok_or(GraphError::Disconnected)
        }
    }
}

fn reconstruct<C: FiniteCost>(
    cells: &Table<C>,
    mut i: usize,
    mut j: usize,
    boundary: AlignmentBoundary,
) -> Result<Vec<AlignmentStep<C>>, GraphError> {
    let mut reversed = Vec::new();
    loop {
        let cell = cells[i][j].as_ref().ok_or_else(|| {
            GraphError::CertificateInvalid("alignment path visits an unreachable cell".to_owned())
        })?;
        let Some(movement) = cell.predecessor else {
            if i == 0 && (j == 0 || boundary == AlignmentBoundary::Subsequence) {
                break;
            }
            return Err(GraphError::CertificateInvalid(
                "alignment path ends at an illegal free boundary".to_owned(),
            ));
        };
        let cost = cell.step_cost.clone().ok_or_else(|| {
            GraphError::CertificateInvalid("alignment step has no cost".to_owned())
        })?;
        match movement {
            AlignmentMove::Match => {
                i -= 1;
                j -= 1;
                reversed.push(AlignmentStep::Match {
                    left: i,
                    right: j,
                    cost,
                });
            }
            AlignmentMove::Delete => {
                i -= 1;
                reversed.push(AlignmentStep::Delete { left: i, cost });
            }
            AlignmentMove::Insert => {
                j -= 1;
                reversed.push(AlignmentStep::Insert { right: j, cost });
            }
        }
    }
    reversed.reverse();
    Ok(reversed)
}

fn validate_policy<C: FiniteCost>(policy: &DtwPolicy<C>) -> Result<(), GraphError> {
    validate_non_negative(&policy.gaps.delete, "alignment deletion gap")?;
    validate_non_negative(&policy.gaps.insert, "alignment insertion gap")
}

fn validate_non_negative<C: FiniteCost>(cost: &C, context: &str) -> Result<(), GraphError> {
    validate(cost, context)?;
    if compare(cost, &C::zero(), context)? == Ordering::Less {
        return Err(GraphError::Unsupported(format!(
            "{context} must be non-negative"
        )));
    }
    Ok(())
}

fn peak_memory(
    left_len: usize,
    right_len: usize,
    memory: AlignmentMemory,
) -> Result<usize, GraphError> {
    let columns = right_len
        .checked_add(1)
        .ok_or_else(|| GraphError::WeightOverflow("alignment columns".to_owned()))?;
    match memory {
        AlignmentMemory::Full => left_len
            .checked_add(1)
            .and_then(|rows| rows.checked_mul(columns))
            .ok_or_else(|| GraphError::WeightOverflow("alignment table cells".to_owned())),
        AlignmentMemory::RollingScoreOnly => {
            let rows: usize = if left_len == 0 { 1 } else { 2 };
            rows.checked_mul(columns)
                .ok_or_else(|| GraphError::WeightOverflow("alignment rolling rows".to_owned()))
        }
    }
}

fn charge_cell(
    meter: &mut Option<&mut WorkMeter<'_>>,
    stats: &mut Stats,
) -> Result<(), GraphError> {
    if let Some(meter) = meter.as_deref_mut() {
        meter.cell()?;
    }
    stats.cells = stats
        .cells
        .checked_add(1)
        .ok_or_else(|| GraphError::WeightOverflow("alignment cell count".to_owned()))?;
    Ok(())
}

fn charge_edge(
    meter: &mut Option<&mut WorkMeter<'_>>,
    stats: &mut Stats,
) -> Result<(), GraphError> {
    if let Some(meter) = meter.as_deref_mut() {
        meter.edge()?;
    }
    stats.edges = stats
        .edges
        .checked_add(1)
        .ok_or_else(|| GraphError::WeightOverflow("alignment edge count".to_owned()))?;
    Ok(())
}

#[cfg(test)]
mod tests;