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
//! Rank helpers (longest-path, slack).

use crate::graphlib::{EdgeKey, Graph};
use crate::work::{checked_add, checked_mul};
use crate::{EdgeLabel, GraphLabel, NodeLabel};
use rustc_hash::FxHashMap as HashMap;

pub fn longest_path(g: &mut Graph<NodeLabel, EdgeLabel, GraphLabel>) {
    let mut work_control = crate::NoopWorkControl;
    longest_path_controlled(g, &mut work_control)
        .expect("rank arithmetic must fit the public longest-path compatibility API");
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct LongestPathWorkPlan {
    work_units: usize,
    prepare_adjacency_cache: bool,
}

fn longest_path_work_plan(
    g: &Graph<NodeLabel, EdgeLabel, GraphLabel>,
) -> Result<LongestPathWorkPlan, crate::WorkError> {
    let prepare_adjacency_cache = !g.is_adjacency_cache_current();
    // The admitted algorithm scans Graphlib node order for sources, validates every edge slot,
    // visits each live node and outgoing edge, then commits the staged rank of each live node.
    // A stale CSR additionally initializes and clones its node-slot offset/cursor arrays, scans
    // edge slots twice, and materializes both directed endpoint arrays. Keep that cold-only work
    // distinct so warm callers are not charged for historical node tombstones that they do not
    // inspect.
    let node_work = checked_mul(g.node_count(), 2)?;
    let base_work = checked_add(
        checked_add(g.node_order_slot_count(), node_work)?,
        checked_add(g.edge_slot_count(), checked_mul(g.edge_count(), 2)?)?,
    )?;
    let cold_csr_work = if prepare_adjacency_cache {
        checked_add(
            checked_mul(g.node_slot_count(), 6)?,
            checked_add(
                checked_mul(g.edge_slot_count(), 2)?,
                checked_mul(g.edge_count(), 2)?,
            )?,
        )?
    } else {
        0
    };
    Ok(LongestPathWorkPlan {
        work_units: checked_add(base_work, cold_csr_work)?,
        prepare_adjacency_cache,
    })
}

pub(crate) fn longest_path_controlled(
    g: &mut Graph<NodeLabel, EdgeLabel, GraphLabel>,
    work_control: &mut dyn crate::WorkControl,
) -> Result<(), crate::WorkError> {
    let plan = longest_path_work_plan(g)?;
    work_control.charge(plan.work_units)?;
    crate::rank::validate_rank_arithmetic(g)?;
    if plan.prepare_adjacency_cache {
        g.prepare_adjacency_cache();
    }
    struct Frame {
        v: String,
        edges: Vec<EdgeKey>,
        next_edge: usize,
        rank: Option<i128>,
        incoming_minlen: Option<i128>,
    }

    fn apply_candidate(rank: &mut Option<i128>, candidate: i128) {
        *rank = Some(match *rank {
            Some(current) => current.min(candidate),
            None => candidate,
        });
    }

    let sources: Vec<String> = g.sources().into_iter().map(|s| s.to_string()).collect();
    let mut visited: HashMap<String, i128> = HashMap::default();
    let mut staged_ranks = Vec::with_capacity(g.node_count());
    for v in sources {
        if visited.contains_key(&v) {
            continue;
        }

        let mut stack = vec![Frame {
            edges: g.out_edges(&v, None),
            v,
            next_edge: 0,
            rank: None,
            incoming_minlen: None,
        }];

        while let Some(frame) = stack.last_mut() {
            if let Some(rank) = visited.get(frame.v.as_str()).copied() {
                let incoming_minlen = frame.incoming_minlen;
                let _ = stack.pop();
                if let (Some(parent), Some(minlen)) = (stack.last_mut(), incoming_minlen) {
                    apply_candidate(&mut parent.rank, rank - minlen);
                }
                continue;
            }

            if frame.next_edge < frame.edges.len() {
                let edge = frame.edges[frame.next_edge].clone();
                frame.next_edge += 1;
                let minlen = g
                    .edge_by_key(&edge)
                    .map(|lbl| lbl.minlen as i128)
                    .unwrap_or(1);
                if let Some(child_rank) = visited.get(edge.w.as_str()).copied() {
                    apply_candidate(&mut frame.rank, child_rank - minlen);
                } else {
                    stack.push(Frame {
                        edges: g.out_edges(&edge.w, None),
                        v: edge.w,
                        next_edge: 0,
                        rank: None,
                        incoming_minlen: Some(minlen),
                    });
                }
                continue;
            }

            let Some(frame) = stack.pop() else {
                break;
            };
            let rank = frame.rank.unwrap_or(0);
            let rank_i32 = i32::try_from(rank).map_err(|_| crate::WorkError::ArithmeticOverflow)?;
            if let Some(node_ix) = g.node_ix(&frame.v) {
                staged_ranks.push((node_ix, rank_i32));
            }
            visited.insert(frame.v, rank);
            if let (Some(parent), Some(minlen)) = (stack.last_mut(), frame.incoming_minlen) {
                apply_candidate(&mut parent.rank, rank - minlen);
            }
        }
    }

    for (node_ix, rank) in staged_ranks {
        if let Some(label) = g.node_label_mut_by_ix(node_ix) {
            label.rank = Some(rank);
        }
    }
    Ok(())
}

pub fn slack(g: &Graph<NodeLabel, EdgeLabel, GraphLabel>, e: &EdgeKey) -> i32 {
    slack_checked(g, e).expect("rank slack must fit the public compatibility API")
}

pub(crate) fn slack_checked(
    g: &Graph<NodeLabel, EdgeLabel, GraphLabel>,
    e: &EdgeKey,
) -> Result<i32, crate::WorkError> {
    // Be defensive: callers can provide arbitrary graphs. Missing nodes/ranks are treated
    // as `0` so layout can degrade gracefully instead of panicking.
    let w_rank = g.node(&e.w).and_then(|n| n.rank).unwrap_or(0);
    let v_rank = g.node(&e.v).and_then(|n| n.rank).unwrap_or(0);
    let minlen = g.edge_by_key(e).map_or(1, |lbl| lbl.minlen) as i128;
    let slack = i128::from(w_rank) - i128::from(v_rank) - minlen;
    i32::try_from(slack).map_err(|_| crate::WorkError::ArithmeticOverflow)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graphlib::GraphOptions;
    use crate::{WorkControl, WorkError};

    #[derive(Debug, Default)]
    struct RecordingWorkControl {
        charges: Vec<usize>,
        remaining: Option<usize>,
    }

    impl RecordingWorkControl {
        fn with_limit(limit: usize) -> Self {
            Self {
                remaining: Some(limit),
                ..Self::default()
            }
        }
    }

    impl WorkControl for RecordingWorkControl {
        fn charge(&mut self, units: usize) -> Result<(), WorkError> {
            self.charges.push(units);
            let Some(remaining) = self.remaining else {
                return Ok(());
            };
            let Some(next) = remaining.checked_sub(units) else {
                return Err(WorkError::Interrupted);
            };
            self.remaining = Some(next);
            Ok(())
        }
    }

    fn graph_with_nodes(nodes: &[&str]) -> Graph<NodeLabel, EdgeLabel, GraphLabel> {
        let mut graph = Graph::new(GraphOptions {
            multigraph: true,
            ..GraphOptions::default()
        });
        graph.set_graph(GraphLabel::default());
        graph.set_default_node_label(NodeLabel::default);
        graph.set_default_edge_label(|| EdgeLabel {
            minlen: 1,
            weight: 1.0,
            ..EdgeLabel::default()
        });
        for node in nodes {
            graph.set_node(*node, NodeLabel::default());
        }
        graph
    }

    fn chain_graph() -> Graph<NodeLabel, EdgeLabel, GraphLabel> {
        let mut graph = graph_with_nodes(&["a", "b", "c"]);
        graph.set_edge("a", "b");
        graph.set_edge("b", "c");
        graph
    }

    fn sparse_chain_graph() -> Graph<NodeLabel, EdgeLabel, GraphLabel> {
        let mut graph = graph_with_nodes(&["a", "b", "c"]);
        graph.set_edge_named("a", "c", Some("removed"), None);
        graph.set_edge("a", "b");
        graph.set_edge("b", "c");
        assert!(graph.remove_edge("a", "c", Some("removed")));
        graph
    }

    fn overflowing_chain_graph() -> Graph<NodeLabel, EdgeLabel, GraphLabel> {
        let mut graph = graph_with_nodes(&["a", "b", "c"]);
        graph.set_edge_with_label(
            "a",
            "b",
            EdgeLabel {
                minlen: 2,
                ..EdgeLabel::default()
            },
        );
        graph.set_edge_with_label(
            "b",
            "c",
            EdgeLabel {
                minlen: i32::MAX as usize,
                ..EdgeLabel::default()
            },
        );
        graph
    }

    fn rank_snapshot(
        graph: &Graph<NodeLabel, EdgeLabel, GraphLabel>,
    ) -> Vec<(String, Option<i32>)> {
        graph
            .nodes()
            .map(|id| (id.to_string(), graph.node(id).and_then(|node| node.rank)))
            .collect()
    }

    #[test]
    fn longest_path_rejects_before_cold_csr_or_rank_mutation() {
        let mut rejected_graph = chain_graph();
        rejected_graph.for_each_node_mut(|_id, node| node.rank = Some(17));
        let source_ranks = rank_snapshot(&rejected_graph);
        let plan = longest_path_work_plan(&rejected_graph).unwrap();
        assert!(plan.prepare_adjacency_cache);
        assert!(!rejected_graph.is_adjacency_cache_current());

        let mut rejected = RecordingWorkControl::with_limit(plan.work_units - 1);
        assert_eq!(
            longest_path_controlled(&mut rejected_graph, &mut rejected),
            Err(WorkError::Interrupted)
        );
        assert_eq!(rejected.charges, [plan.work_units]);
        assert_eq!(rejected.remaining, Some(plan.work_units - 1));
        assert_eq!(rank_snapshot(&rejected_graph), source_ranks);
        assert!(!rejected_graph.is_adjacency_cache_current());

        for limit in [plan.work_units, plan.work_units + 1] {
            let mut graph = chain_graph();
            let mut admitted = RecordingWorkControl::with_limit(limit);
            longest_path_controlled(&mut graph, &mut admitted)
                .expect("equal and above longest-path budgets succeed");
            assert_eq!(admitted.charges, [plan.work_units]);
            assert_eq!(admitted.remaining, Some(limit - plan.work_units));
            assert!(graph.is_adjacency_cache_current());
            assert_eq!(
                rank_snapshot(&graph),
                [
                    ("a".to_string(), Some(-2)),
                    ("b".to_string(), Some(-1)),
                    ("c".to_string(), Some(0)),
                ]
            );
        }
    }

    #[test]
    fn longest_path_distinguishes_cold_and_warm_csr() {
        let mut cold_graph = sparse_chain_graph();
        let mut warm_graph = sparse_chain_graph();
        warm_graph.prepare_adjacency_cache();

        let cold_plan = longest_path_work_plan(&cold_graph).unwrap();
        let warm_plan = longest_path_work_plan(&warm_graph).unwrap();
        let expected_cold_delta = checked_add(
            checked_mul(cold_graph.node_slot_count(), 6).unwrap(),
            checked_add(
                checked_mul(cold_graph.edge_slot_count(), 2).unwrap(),
                checked_mul(cold_graph.edge_count(), 2).unwrap(),
            )
            .unwrap(),
        )
        .unwrap();
        assert_eq!(
            cold_plan.work_units,
            checked_add(warm_plan.work_units, expected_cold_delta).unwrap()
        );

        let mut cold_control = RecordingWorkControl::default();
        longest_path_controlled(&mut cold_graph, &mut cold_control)
            .expect("the cold longest-path run succeeds");
        let mut warm_control = RecordingWorkControl::default();
        longest_path_controlled(&mut warm_graph, &mut warm_control)
            .expect("the warm longest-path run succeeds");
        assert_eq!(cold_control.charges, [cold_plan.work_units]);
        assert_eq!(warm_control.charges, [warm_plan.work_units]);
        assert_eq!(rank_snapshot(&cold_graph), rank_snapshot(&warm_graph));
    }

    #[test]
    fn longest_path_overflow_preserves_every_existing_rank() {
        let mut graph = overflowing_chain_graph();
        for (rank, id) in [11, 12, 13].into_iter().zip(["a", "b", "c"]) {
            graph.node_mut(id).unwrap().rank = Some(rank);
        }
        let source_ranks = rank_snapshot(&graph);
        let plan = longest_path_work_plan(&graph).unwrap();
        let mut work_control = RecordingWorkControl::with_limit(plan.work_units);

        assert_eq!(
            longest_path_controlled(&mut graph, &mut work_control),
            Err(WorkError::ArithmeticOverflow)
        );
        assert_eq!(work_control.charges, [plan.work_units]);
        assert_eq!(work_control.remaining, Some(0));
        assert_eq!(rank_snapshot(&graph), source_ranks);
    }

    #[test]
    fn longest_path_accounts_for_each_slot_domain() {
        let dense_edges = chain_graph();
        let sparse_edges = sparse_chain_graph();
        let expected_dense_warm = checked_add(
            checked_add(
                dense_edges.node_order_slot_count(),
                checked_mul(dense_edges.node_count(), 2).unwrap(),
            )
            .unwrap(),
            checked_add(
                dense_edges.edge_slot_count(),
                checked_mul(dense_edges.edge_count(), 2).unwrap(),
            )
            .unwrap(),
        )
        .unwrap();
        assert_eq!(
            sparse_edges.edge_slot_count(),
            dense_edges.edge_slot_count() + 1
        );
        let dense_edges_cold = longest_path_work_plan(&dense_edges).unwrap().work_units;
        let sparse_edges_cold = longest_path_work_plan(&sparse_edges).unwrap().work_units;
        assert_eq!(sparse_edges_cold, dense_edges_cold + 3);
        dense_edges.prepare_adjacency_cache();
        sparse_edges.prepare_adjacency_cache();
        assert_eq!(
            longest_path_work_plan(&dense_edges).unwrap().work_units,
            expected_dense_warm
        );
        assert_eq!(
            longest_path_work_plan(&sparse_edges).unwrap().work_units,
            longest_path_work_plan(&dense_edges).unwrap().work_units + 1
        );

        let mut dense_node_slots = graph_with_nodes(&["0", "2", "3"]);
        dense_node_slots.set_edge("0", "2");
        dense_node_slots.set_edge("2", "3");
        let mut sparse_node_slots = graph_with_nodes(&["0", "1", "2", "3"]);
        assert!(sparse_node_slots.remove_node("1"));
        sparse_node_slots.set_edge("0", "2");
        sparse_node_slots.set_edge("2", "3");
        assert_eq!(
            sparse_node_slots.node_slot_count(),
            dense_node_slots.node_slot_count() + 1
        );
        assert_eq!(
            sparse_node_slots.node_order_slot_count(),
            dense_node_slots.node_order_slot_count()
        );
        assert_eq!(
            longest_path_work_plan(&sparse_node_slots)
                .unwrap()
                .work_units,
            longest_path_work_plan(&dense_node_slots)
                .unwrap()
                .work_units
                + 6
        );
        dense_node_slots.prepare_adjacency_cache();
        sparse_node_slots.prepare_adjacency_cache();
        assert_eq!(
            longest_path_work_plan(&sparse_node_slots)
                .unwrap()
                .work_units,
            longest_path_work_plan(&dense_node_slots)
                .unwrap()
                .work_units
        );

        let mut sparse_order = graph_with_nodes(&["a", "b", "c", "d"]);
        assert!(sparse_order.remove_node("b"));
        sparse_order.set_edge("a", "c");
        sparse_order.set_edge("c", "d");
        sparse_order.prepare_adjacency_cache();
        assert_eq!(
            sparse_order.node_slot_count(),
            sparse_node_slots.node_slot_count()
        );
        assert_eq!(
            sparse_order.node_order_slot_count(),
            sparse_node_slots.node_order_slot_count() + 1
        );
        assert_eq!(
            longest_path_work_plan(&sparse_order).unwrap().work_units,
            longest_path_work_plan(&sparse_node_slots)
                .unwrap()
                .work_units
                + 1
        );
    }
}