dugong 0.8.0

Dagre-compatible graph layout algorithms (port of dagrejs/dagre).
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
use super::EMPTY_LAYER_SLOT;
use super::OrderEdgeWeight;
use crate::graphlib::Graph;
use crate::work::{ceil_log2, checked_add, checked_mul, checked_n_log_n};
use crate::{WorkControl, WorkError};
use rustc_hash::FxHashMap as HashMap;

#[derive(Debug, Clone, Copy)]
struct SouthEntry {
    pos: usize,
    weight: f64,
}

fn accumulate_crossings(
    entries: &[SouthEntry],
    tree: &mut [f64],
    first_index: usize,
    cc: &mut f64,
) {
    for entry in entries {
        let mut index = entry.pos + first_index;
        tree[index] += entry.weight;
        let mut weight_sum: f64 = 0.0;
        while index > 0 {
            if index % 2 == 1 {
                weight_sum += tree[index + 1];
            }
            index = (index - 1) >> 1;
            tree[index] += entry.weight;
        }
        *cc += entry.weight * weight_sum;
    }
}

pub fn cross_count<N, E, G>(g: &Graph<N, E, G>, layering: &[Vec<String>]) -> f64
where
    N: Default + 'static,
    E: Default + OrderEdgeWeight + 'static,
    G: Default,
{
    let mut cc: f64 = 0.0;
    for i in 1..layering.len() {
        cc += two_layer_cross_count(g, &layering[i - 1], &layering[i]);
    }
    cc
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct ControlledCrossCount {
    pub value: f64,
    pub is_proven_minimum: bool,
}

#[derive(Debug, Clone, Copy)]
struct ZeroCrossingDomain {
    all_weights_are_non_negative_and_finite: bool,
    all_layer_weight_sums_are_finite: bool,
    minimum_positive_weight: Option<f64>,
}

impl Default for ZeroCrossingDomain {
    fn default() -> Self {
        Self {
            all_weights_are_non_negative_and_finite: true,
            all_layer_weight_sums_are_finite: true,
            minimum_positive_weight: None,
        }
    }
}

impl ZeroCrossingDomain {
    fn observe_weight(&mut self, weight: f64) {
        self.all_weights_are_non_negative_and_finite &= weight.is_finite() && weight >= 0.0;
        if weight > 0.0 {
            self.minimum_positive_weight = Some(
                self.minimum_positive_weight
                    .map_or(weight, |minimum| minimum.min(weight)),
            );
        }
    }

    fn finish_layer(&mut self, edge_count: Option<usize>, maximum_weight: f64) {
        let Some(edge_count) = edge_count else {
            self.all_layer_weight_sums_are_finite = false;
            return;
        };
        if edge_count == 0 || maximum_weight == 0.0 {
            return;
        }

        // A finite total weight keeps every non-negative accumulator-tree cell and every
        // `weight_sum` finite in every ordering. Bound the total conservatively with the next
        // power of two so a zero-weight edge can never observe JavaScript's `0 * Infinity = NaN`.
        let Some(capacity) = edge_count.checked_next_power_of_two() else {
            self.all_layer_weight_sums_are_finite = false;
            return;
        };
        self.all_layer_weight_sums_are_finite &= maximum_weight <= f64::MAX / capacity as f64;
    }

    fn proves_zero_is_global_minimum(self, value: f64) -> bool {
        value == 0.0
            && self.all_weights_are_non_negative_and_finite
            && self.all_layer_weight_sums_are_finite
            && self
                .minimum_positive_weight
                .is_none_or(|minimum| minimum * minimum > 0.0)
    }
}

pub(crate) fn cross_count_ix_controlled<N, E, G>(
    g: &Graph<N, E, G>,
    layering: &[Vec<usize>],
    work_control: &mut dyn WorkControl,
) -> Result<ControlledCrossCount, WorkError>
where
    N: Default + 'static,
    E: Default + OrderEdgeWeight + 'static,
    G: Default,
{
    charge_nonzero(work_control, layering.len().saturating_sub(1))?;
    let mut cc: f64 = 0.0;
    let mut zero_domain = ZeroCrossingDomain::default();
    for i in 1..layering.len() {
        cc += two_layer_cross_count_ix_controlled(
            g,
            &layering[i - 1],
            &layering[i],
            &mut zero_domain,
            work_control,
        )?;
    }
    Ok(ControlledCrossCount {
        value: cc,
        is_proven_minimum: zero_domain.proves_zero_is_global_minimum(cc),
    })
}

fn two_layer_cross_count<N, E, G>(g: &Graph<N, E, G>, north: &[String], south: &[String]) -> f64
where
    N: Default + 'static,
    E: Default + OrderEdgeWeight + 'static,
    G: Default,
{
    if south.is_empty() {
        return 0.0;
    }

    let mut south_pos: HashMap<&str, usize> = HashMap::default();
    for (i, v) in south.iter().enumerate() {
        south_pos.insert(v.as_str(), i);
    }

    let mut first_index: usize = 1;
    while first_index < south.len() {
        first_index <<= 1;
    }
    let tree_size = 2 * first_index - 1;
    first_index -= 1;
    let mut tree: Vec<f64> = vec![0.0; tree_size];

    let mut cc: f64 = 0.0;
    let mut entries: Vec<SouthEntry> = Vec::new();
    for v in north {
        entries.clear();
        g.for_each_out_edge(v, None, |ek, lbl| {
            let target = if g.is_directed() || ek.v.as_str() == v.as_str() {
                ek.w.as_str()
            } else {
                ek.v.as_str()
            };
            let Some(&pos) = south_pos.get(target) else {
                return;
            };
            entries.push(SouthEntry {
                pos,
                weight: lbl.weight(),
            });
        });
        if entries.len() > 1 {
            // Dagre's per-north stable sort preserves the graph's edge order for equal positions.
            entries.sort_by_key(|entry| entry.pos);
        }
        accumulate_crossings(&entries, &mut tree, first_index, &mut cc);
    }

    cc
}

fn charge_nonzero(work_control: &mut dyn WorkControl, units: usize) -> Result<(), WorkError> {
    if units == 0 {
        return Ok(());
    }
    work_control.charge(units)
}

fn two_layer_cross_count_ix_controlled<N, E, G>(
    g: &Graph<N, E, G>,
    north: &[usize],
    south: &[usize],
    zero_domain: &mut ZeroCrossingDomain,
    work_control: &mut dyn WorkControl,
) -> Result<f64, WorkError>
where
    N: Default + 'static,
    E: Default + OrderEdgeWeight + 'static,
    G: Default,
{
    if south.is_empty() {
        return Ok(0.0);
    }

    charge_nonzero(work_control, south.len())?;
    let mut south_pos: HashMap<usize, usize> = HashMap::default();
    for (i, &v_ix) in south.iter().enumerate() {
        if v_ix == EMPTY_LAYER_SLOT {
            continue;
        }
        south_pos.insert(v_ix, i);
    }

    let mut first_index: usize = 1;
    while first_index < south.len() {
        first_index <<= 1;
    }
    let tree_size = checked_mul(first_index, 2)?
        .checked_sub(1)
        .ok_or(WorkError::ArithmeticOverflow)?;
    charge_nonzero(work_control, tree_size)?;
    first_index -= 1;
    let mut tree: Vec<f64> = vec![0.0; tree_size];

    let tree_depth = ceil_log2(south.len());
    let mut cc: f64 = 0.0;
    let mut entries: Vec<SouthEntry> = Vec::new();
    let mut participating_edge_count = Some(0usize);
    let mut maximum_weight = 0.0f64;
    charge_nonzero(work_control, north.len())?;
    for &v_ix in north {
        if v_ix == EMPTY_LAYER_SLOT {
            continue;
        }
        entries.clear();
        let mut push_entry = |target_ix: usize, lbl: &E| {
            let Some(&pos) = south_pos.get(&target_ix) else {
                return;
            };
            let weight = lbl.weight();
            zero_domain.observe_weight(weight);
            participating_edge_count =
                participating_edge_count.and_then(|count| count.checked_add(1));
            if weight.is_finite() && weight >= 0.0 {
                maximum_weight = maximum_weight.max(weight);
            }
            entries.push(SouthEntry { pos, weight });
        };
        if g.is_directed() {
            charge_nonzero(work_control, g.out_edge_count_ix(v_ix))?;
            g.for_each_out_edge_ix(v_ix, None, |_u_ix, target_ix, _ek, lbl| {
                push_entry(target_ix, lbl);
            });
        } else {
            charge_nonzero(work_control, g.undirected_edge_count_ix(v_ix))?;
            g.for_each_undirected_edge_ix(v_ix, |_node_ix, target_ix, _ek, lbl| {
                push_entry(target_ix, lbl);
            });
        }
        if entries.len() > 1 {
            charge_nonzero(work_control, checked_n_log_n(entries.len())?)?;
            // Dagre's per-north stable sort preserves the graph's edge order for equal positions.
            entries.sort_by_key(|entry| entry.pos);
        }
        let entry_work = checked_mul(entries.len(), checked_add(tree_depth, 2)?)?;
        charge_nonzero(work_control, entry_work)?;
        accumulate_crossings(&entries, &mut tree, first_index, &mut cc);
    }
    zero_domain.finish_layer(participating_edge_count, maximum_weight);

    Ok(cc)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graphlib::GraphOptions;
    use crate::order::WeightLabel;

    #[derive(Default)]
    struct RecordingWorkControl {
        used: usize,
        max: usize,
    }

    impl WorkControl for RecordingWorkControl {
        fn charge(&mut self, units: usize) -> Result<(), WorkError> {
            let next = self
                .used
                .checked_add(units)
                .ok_or(WorkError::ArithmeticOverflow)?;
            if next > self.max {
                return Err(WorkError::Interrupted);
            }
            self.used = next;
            Ok(())
        }
    }

    fn weighted_edge(weight: f64) -> WeightLabel {
        WeightLabel { weight }
    }

    #[test]
    fn streaming_entries_preserve_dagre_edge_and_float_order() {
        let mut graph: Graph<(), WeightLabel, ()> = Graph::new(GraphOptions {
            multigraph: true,
            ..GraphOptions::default()
        });
        for (name, weight) in [("first", 1e16), ("second", 1.0), ("third", 1.0)] {
            graph.set_edge_named(
                "north-a",
                "south-b",
                Some(name),
                Some(weighted_edge(weight)),
            );
        }
        graph.set_edge_with_label("north-b", "south-a", weighted_edge(1.0));

        let north = vec!["north-a".to_string(), "north-b".to_string()];
        let south = vec!["south-a".to_string(), "south-b".to_string()];
        let public = cross_count(&graph, &[north.clone(), south.clone()]);
        let layering = vec![
            north
                .iter()
                .map(|node| graph.node_ix(node).unwrap())
                .collect(),
            south
                .iter()
                .map(|node| graph.node_ix(node).unwrap())
                .collect(),
        ];
        let mut work_control = RecordingWorkControl {
            max: usize::MAX,
            ..Default::default()
        };

        let actual = cross_count_ix_controlled(&graph, &layering, &mut work_control).unwrap();

        // Moving the large equal-position edge after both unit edges changes the rounded sum.
        assert_eq!(public.to_bits(), 1e16f64.to_bits());
        assert_eq!(actual.value.to_bits(), public.to_bits());
    }

    #[test]
    fn streaming_work_excludes_flatten_copy_and_trivial_sorts() {
        let mut graph: Graph<(), WeightLabel, ()> = Graph::new(GraphOptions::default());
        graph.set_edge_with_label("north-a", "south-c", weighted_edge(3.0));
        graph.set_edge_with_label("north-a", "south-a", weighted_edge(2.0));
        graph.set_edge_with_label("north-b", "outside", weighted_edge(7.0));
        graph.set_edge_with_label("north-c", "south-b", weighted_edge(5.0));
        let layering = vec![
            ["north-a", "north-b", "north-c"]
                .map(|node| graph.node_ix(node).unwrap())
                .to_vec(),
            ["south-a", "south-b", "south-c"]
                .map(|node| graph.node_ix(node).unwrap())
                .to_vec(),
        ];
        let mut exact = RecordingWorkControl {
            max: 32,
            ..Default::default()
        };

        let crossing_count = cross_count_ix_controlled(&graph, &layering, &mut exact).unwrap();

        assert_eq!(crossing_count.value, 15.0);
        assert_eq!(exact.used, 32);

        let mut below = RecordingWorkControl {
            max: 31,
            ..Default::default()
        };
        assert_eq!(
            cross_count_ix_controlled(&graph, &layering, &mut below),
            Err(WorkError::Interrupted)
        );
        assert_eq!(below.used, 28);
    }

    #[test]
    fn zero_minimum_proof_rejects_underflowing_positive_weights() {
        let mut graph: Graph<(), WeightLabel, ()> = Graph::new(GraphOptions::default());
        graph.set_edge_with_label("north-a", "south-b", weighted_edge(f64::MIN_POSITIVE));
        graph.set_edge_with_label("north-b", "south-a", weighted_edge(f64::MIN_POSITIVE));
        let layering = vec![
            ["north-a", "north-b"]
                .map(|node| graph.node_ix(node).unwrap())
                .to_vec(),
            ["south-a", "south-b"]
                .map(|node| graph.node_ix(node).unwrap())
                .to_vec(),
        ];
        let mut work_control = RecordingWorkControl {
            max: usize::MAX,
            ..Default::default()
        };

        let result = cross_count_ix_controlled(&graph, &layering, &mut work_control).unwrap();

        assert_eq!(result.value, 0.0);
        assert!(!result.is_proven_minimum);
    }

    #[test]
    fn overflowing_layer_disables_the_shortcut_before_a_nan_reordering() {
        let mut graph: Graph<(), WeightLabel, ()> = Graph::new(GraphOptions::default());
        graph.set_edge_with_label("zero", "south-a", weighted_edge(0.0));
        graph.set_edge_with_label("huge-a", "south-b", weighted_edge(f64::MAX));
        graph.set_edge_with_label("huge-b", "south-b", weighted_edge(f64::MAX));
        let south = ["south-a", "south-b"]
            .map(|node| graph.node_ix(node).unwrap())
            .to_vec();
        let non_crossing = ["zero", "huge-a", "huge-b"]
            .map(|node| graph.node_ix(node).unwrap())
            .to_vec();
        let crossing = ["huge-a", "huge-b", "zero"]
            .map(|node| graph.node_ix(node).unwrap())
            .to_vec();
        let mut work_control = RecordingWorkControl {
            max: usize::MAX,
            ..Default::default()
        };

        let initial =
            cross_count_ix_controlled(&graph, &[non_crossing, south.clone()], &mut work_control)
                .unwrap();
        let reordered =
            cross_count_ix_controlled(&graph, &[crossing, south], &mut work_control).unwrap();

        assert_eq!(initial.value, 0.0);
        assert!(!initial.is_proven_minimum);
        assert!(reordered.value.is_nan());
    }

    #[test]
    fn undirected_crossings_use_the_incident_endpoint_in_both_scorers() {
        let mut graph: Graph<(), WeightLabel, ()> = Graph::new(GraphOptions {
            directed: false,
            ..GraphOptions::default()
        });
        // Both queried north nodes are the canonical `w` endpoint of their undirected edge.
        graph.set_edge_with_label("b", "z", weighted_edge(1.0));
        graph.set_edge_with_label("a", "y", weighted_edge(1.0));
        let north = ["z", "y"];
        let south = ["a", "b"];
        let public_layering = vec![
            north.map(str::to_string).to_vec(),
            south.map(str::to_string).to_vec(),
        ];
        let indexed_layering = vec![
            north.map(|node| graph.node_ix(node).unwrap()).to_vec(),
            south.map(|node| graph.node_ix(node).unwrap()).to_vec(),
        ];
        let mut work_control = RecordingWorkControl {
            max: usize::MAX,
            ..Default::default()
        };

        let controlled =
            cross_count_ix_controlled(&graph, &indexed_layering, &mut work_control).unwrap();

        assert_eq!(cross_count(&graph, &public_layering), 1.0);
        assert_eq!(controlled.value, 1.0);
        assert!(!controlled.is_proven_minimum);
    }
}