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
use super::{
    Assignment, AssignmentCertificate, AssignmentCost, AssignmentOperation, AssignmentPolicy,
    CostMatrix, add, certificate_error, less, sub,
};
use crate::{AlgorithmReceipt, GraphError, control::WorkMeter};

#[derive(Copy, Clone, Debug)]
struct ArcRef {
    from: usize,
    index: usize,
}

#[derive(Clone, Debug)]
struct Arc<C> {
    to: usize,
    reverse: usize,
    capacity: usize,
    initial_capacity: usize,
    cost: C,
}

#[derive(Clone, Debug)]
struct Network<C> {
    adjacency: Vec<Vec<Arc<C>>>,
}

impl<C: AssignmentCost> Network<C> {
    fn new(nodes: usize) -> Self {
        Self {
            adjacency: vec![Vec::new(); nodes],
        }
    }

    fn add_arc(&mut self, from: usize, to: usize, capacity: usize, cost: C) -> ArcRef {
        let forward_index = self.adjacency[from].len();
        let reverse_index = self.adjacency[to].len();
        let reverse_cost = C::zero()
            .checked_sub(&cost)
            .expect("validated non-negative costs and derived residual costs are negatable");
        self.adjacency[from].push(Arc {
            to,
            reverse: reverse_index,
            capacity,
            initial_capacity: capacity,
            cost,
        });
        self.adjacency[to].push(Arc {
            to: from,
            reverse: forward_index,
            capacity: 0,
            initial_capacity: 0,
            cost: reverse_cost,
        });
        ArcRef {
            from,
            index: forward_index,
        }
    }

    fn flow(&self, arc: ArcRef) -> usize {
        let edge = &self.adjacency[arc.from][arc.index];
        edge.initial_capacity - edge.capacity
    }

    fn send(&mut self, arc: ArcRef, amount: usize) -> Result<(), GraphError> {
        let (to, reverse, capacity) = {
            let edge = &self.adjacency[arc.from][arc.index];
            (edge.to, edge.reverse, edge.capacity)
        };
        if capacity < amount {
            return certificate_error("assignment operation exceeds network capacity");
        }
        self.adjacency[arc.from][arc.index].capacity -= amount;
        self.adjacency[to][reverse].capacity = self.adjacency[to][reverse]
            .capacity
            .checked_add(amount)
            .ok_or_else(|| GraphError::WeightOverflow("residual capacity".to_owned()))?;
        Ok(())
    }

    fn augment_path(
        &mut self,
        source: usize,
        sink: usize,
        predecessors: &[Option<ArcRef>],
    ) -> Result<(), GraphError> {
        let mut node = sink;
        while node != source {
            let arc = predecessors[node].ok_or_else(|| {
                GraphError::InvalidAssignment(
                    "assignment network has no augmenting path".to_owned(),
                )
            })?;
            self.send(arc, 1)?;
            node = arc.from;
        }
        Ok(())
    }
}

struct Layout<C> {
    network: Network<C>,
    source: usize,
    sink: usize,
    source_first: Vec<ArcRef>,
    source_double: Vec<Option<ArcRef>>,
    insertion: Vec<ArcRef>,
    pairs: Vec<Vec<Option<ArcRef>>>,
    target_sink: Vec<ArcRef>,
    deletion_base: C,
}

type ResidualPath<C> = (Vec<Option<C>>, Vec<Option<ArcRef>>);

fn build<C: AssignmentCost>(
    costs: &CostMatrix<C>,
    policy: &AssignmentPolicy<C>,
) -> Result<Layout<C>, GraphError> {
    let source = 0;
    let row_base = 1;
    let target_base = row_base + costs.rows();
    let sink = target_base + costs.columns();
    let mut network = Network::new(sink + 1);
    let mut source_first = Vec::with_capacity(costs.rows());
    let mut source_double = Vec::with_capacity(costs.rows());
    let mut deletion_base = C::zero();

    for row in 0..costs.rows() {
        let deletion = &policy.deletion_costs[row];
        deletion_base = add(&deletion_base, deletion, "assignment deletion base")?;
        let credit = sub(&C::zero(), deletion, "assignment deletion credit")?;
        source_first.push(network.add_arc(source, row_base + row, 1, credit));
        source_double.push(policy.doubling_cost(row).map(|doubling| {
            network.add_arc(
                source,
                row_base + row,
                costs.columns().saturating_sub(1),
                doubling.clone(),
            )
        }));
    }

    let insertion = (0..costs.columns())
        .map(|target| {
            network.add_arc(
                source,
                target_base + target,
                1,
                policy.insertion_costs[target].clone(),
            )
        })
        .collect::<Vec<_>>();

    let mut pairs = Vec::with_capacity(costs.rows());
    for row in 0..costs.rows() {
        pairs.push(
            (0..costs.columns())
                .map(|target| {
                    costs.allowed(row, target).then(|| {
                        network.add_arc(
                            row_base + row,
                            target_base + target,
                            1,
                            costs.value(row, target).clone(),
                        )
                    })
                })
                .collect(),
        );
    }
    let target_sink = (0..costs.columns())
        .map(|target| network.add_arc(target_base + target, sink, 1, C::zero()))
        .collect();

    Ok(Layout {
        network,
        source,
        sink,
        source_first,
        source_double,
        insertion,
        pairs,
        target_sink,
        deletion_base,
    })
}

pub(super) fn solve<C: AssignmentCost>(
    costs: &CostMatrix<C>,
    policy: &AssignmentPolicy<C>,
    meter: &mut WorkMeter<'_>,
) -> Result<Assignment<C>, GraphError> {
    let mut layout = build(costs, policy)?;
    let mut flow_cost = C::zero();
    for _ in 0..costs.columns() {
        let (distances, predecessors) =
            shortest_residual_path(&layout.network, layout.source, Some(meter))?;
        let distance = distances[layout.sink].as_ref().ok_or_else(|| {
            GraphError::InvalidAssignment("assignment network cannot cover every target".to_owned())
        })?;
        flow_cost = add(&flow_cost, distance, "assignment flow total")?;
        layout
            .network
            .augment_path(layout.source, layout.sink, &predecessors)?;
    }

    let operations = operations_from_flow(costs, policy, &layout)?;
    let total_cost = add(
        &layout.deletion_base,
        &flow_cost,
        "assignment objective total",
    )?;
    let potentials = residual_potentials(&layout.network, Some(meter))?;
    Ok(Assignment {
        operations,
        total_cost,
        certificate: AssignmentCertificate::MinCostFlow { potentials },
        receipt: empty_receipt(),
    })
}

fn shortest_residual_path<C: AssignmentCost>(
    network: &Network<C>,
    source: usize,
    mut meter: Option<&mut WorkMeter<'_>>,
) -> Result<ResidualPath<C>, GraphError> {
    let nodes = network.adjacency.len();
    let mut distances = vec![None; nodes];
    let mut predecessors = vec![None; nodes];
    distances[source] = Some(C::zero());
    for _ in 0..nodes.saturating_sub(1) {
        let mut changed = false;
        for from in 0..nodes {
            let Some(distance) = distances[from].clone() else {
                continue;
            };
            for (index, edge) in network.adjacency[from].iter().enumerate() {
                charge_edge(&mut meter)?;
                if edge.capacity == 0 {
                    continue;
                }
                let candidate = add(&distance, &edge.cost, "assignment path relaxation")?;
                if distances[edge.to]
                    .as_ref()
                    .map(|current| less(&candidate, current, "assignment path relaxation ordering"))
                    .transpose()?
                    .unwrap_or(true)
                {
                    distances[edge.to] = Some(candidate);
                    predecessors[edge.to] = Some(ArcRef { from, index });
                    changed = true;
                }
            }
        }
        if !changed {
            break;
        }
    }
    Ok((distances, predecessors))
}

fn residual_potentials<C: AssignmentCost>(
    network: &Network<C>,
    mut meter: Option<&mut WorkMeter<'_>>,
) -> Result<Vec<C>, GraphError> {
    let nodes = network.adjacency.len();
    let mut potentials = vec![C::zero(); nodes];
    for iteration in 0..nodes {
        let mut changed = false;
        for from in 0..nodes {
            for edge in &network.adjacency[from] {
                charge_edge(&mut meter)?;
                if edge.capacity == 0 {
                    continue;
                }
                let candidate = add(&potentials[from], &edge.cost, "assignment dual relaxation")?;
                if less(
                    &candidate,
                    &potentials[edge.to],
                    "assignment dual relaxation ordering",
                )? {
                    potentials[edge.to] = candidate;
                    changed = true;
                    if iteration + 1 == nodes {
                        return certificate_error(
                            "assignment residual network has a negative cycle",
                        );
                    }
                }
            }
        }
        if !changed {
            break;
        }
    }
    Ok(potentials)
}

fn operations_from_flow<C: AssignmentCost>(
    costs: &CostMatrix<C>,
    policy: &AssignmentPolicy<C>,
    layout: &Layout<C>,
) -> Result<Vec<AssignmentOperation<C>>, GraphError> {
    let mut operations = Vec::new();
    for source in 0..costs.rows() {
        let targets = (0..costs.columns())
            .filter(|target| {
                layout.pairs[source][*target].is_some_and(|arc| layout.network.flow(arc) == 1)
            })
            .collect::<Vec<_>>();
        if let Some((&first, rest)) = targets.split_first() {
            operations.push(AssignmentOperation::Match {
                source,
                target: first,
                cost: costs.value(source, first).clone(),
            });
            let doubling = policy.doubling_cost(source);
            for &target in rest {
                let cost = add(
                    costs.value(source, target),
                    doubling.ok_or_else(|| {
                        GraphError::InvalidAssignment(
                            "flow doubled a source under a forbid policy".to_owned(),
                        )
                    })?,
                    "doubling operation",
                )?;
                operations.push(AssignmentOperation::Double {
                    source,
                    target,
                    cost,
                });
            }
        } else {
            operations.push(AssignmentOperation::Delete {
                source,
                cost: policy.deletion_costs[source].clone(),
            });
        }
    }
    for target in 0..costs.columns() {
        if layout.network.flow(layout.insertion[target]) == 1 {
            operations.push(AssignmentOperation::Insert {
                target,
                cost: policy.insertion_costs[target].clone(),
            });
        }
    }
    Ok(operations)
}

pub(super) fn verify<C: AssignmentCost>(
    costs: &CostMatrix<C>,
    policy: &AssignmentPolicy<C>,
    assignment: &Assignment<C>,
    potentials: &[C],
) -> Result<(), GraphError> {
    let mut layout = build(costs, policy)?;
    let mut source_counts = vec![0usize; costs.rows()];
    let mut inserted = vec![false; costs.columns()];
    for operation in &assignment.operations {
        match operation {
            AssignmentOperation::Match { source, target, .. }
            | AssignmentOperation::Double { source, target, .. } => {
                source_counts[*source] += 1;
                let arc = layout.pairs[*source][*target].ok_or_else(|| {
                    GraphError::CertificateInvalid(
                        "assignment uses a forbidden pair edge".to_owned(),
                    )
                })?;
                layout.network.send(arc, 1)?;
            }
            AssignmentOperation::Insert { target, .. } => inserted[*target] = true,
            AssignmentOperation::Delete { .. } => {}
        }
    }
    for (source, count) in source_counts.into_iter().enumerate() {
        if count == 0 {
            continue;
        }
        layout.network.send(layout.source_first[source], 1)?;
        if count > 1 {
            let arc = layout.source_double[source].ok_or_else(|| {
                GraphError::CertificateInvalid("doubling network arc is absent".to_owned())
            })?;
            layout.network.send(arc, count - 1)?;
        }
    }
    for (target, is_inserted) in inserted.into_iter().enumerate() {
        if is_inserted {
            layout.network.send(layout.insertion[target], 1)?;
        }
        layout.network.send(layout.target_sink[target], 1)?;
    }

    if potentials.len() != layout.network.adjacency.len() {
        return certificate_error("dual potential count does not match assignment network");
    }
    for (from, edges) in layout.network.adjacency.iter().enumerate() {
        for edge in edges {
            if edge.capacity == 0 {
                continue;
            }
            let with_source = add(&edge.cost, &potentials[from], "assignment reduced cost")?;
            let reduced = sub(
                &with_source,
                &potentials[edge.to],
                "assignment reduced cost",
            )?;
            if less(&reduced, &C::zero(), "assignment reduced-cost ordering")? {
                return certificate_error("dual potential admits a negative reduced-cost edge");
            }
        }
    }
    Ok(())
}

fn charge_edge(meter: &mut Option<&mut WorkMeter<'_>>) -> Result<(), GraphError> {
    if let Some(meter) = meter.as_deref_mut() {
        meter.edge()?;
    }
    Ok(())
}

fn empty_receipt() -> AlgorithmReceipt {
    AlgorithmReceipt {
        work_used: 0,
        cells: 0,
        edges: 0,
        peak_memory_cells: 0,
        cell_work: 1,
        edge_work: 1,
    }
}