bounded_graph 0.3.0

A thin newtype wrapper for `petgraph` to assist in the creation of graphs with restrictions on their edges
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
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
#[cfg(feature = "serde-1")]
mod serde_tests {
    use crate::{BoundedGraph, BoundedGraphError, BoundedUnGraph, FixedEdgeCount};
    use serde_json;

    #[test]
    fn test_empty_graph_roundtrip() {
        let graph = BoundedGraph::<FixedEdgeCount<2>, ()>::new();

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");
        let deserialized: BoundedGraph<FixedEdgeCount<2>, ()> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        assert_eq!(graph.node_count(), deserialized.node_count());
        assert_eq!(graph.edge_count(), deserialized.edge_count());
    }

    #[test]
    fn test_graph_with_nodes_roundtrip() {
        let mut graph = BoundedGraph::<FixedEdgeCount<3>, i32>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());
        let n3 = graph.add_node(FixedEdgeCount::empty());

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");
        let deserialized: BoundedGraph<FixedEdgeCount<3>, i32> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        assert_eq!(graph.node_count(), deserialized.node_count());
        assert_eq!(graph.edge_count(), deserialized.edge_count());

        // Verify node indices exist in deserialized graph
        assert!(deserialized.node_weight(n1).is_some());
        assert!(deserialized.node_weight(n2).is_some());
        assert!(deserialized.node_weight(n3).is_some());
    }

    #[test]
    fn test_graph_with_edges_roundtrip() {
        let mut graph = BoundedGraph::<FixedEdgeCount<3>, i32>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());
        let n3 = graph.add_node(FixedEdgeCount::empty());

        graph.add_edge(n1, n2, 10).expect("Failed to add edge");
        graph.add_edge(n2, n3, 20).expect("Failed to add edge");
        graph.add_edge(n1, n3, 30).expect("Failed to add edge");

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");
        let deserialized: BoundedGraph<FixedEdgeCount<3>, i32> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        assert_eq!(graph.node_count(), deserialized.node_count());
        assert_eq!(graph.edge_count(), deserialized.edge_count());

        // Verify edges exist and have correct weights
        let edge1 = deserialized
            .find_edge(n1, n2)
            .expect("Edge n1->n2 not found");
        let edge2 = deserialized
            .find_edge(n2, n3)
            .expect("Edge n2->n3 not found");
        let edge3 = deserialized
            .find_edge(n1, n3)
            .expect("Edge n1->n3 not found");

        assert_eq!(*deserialized.edge_weight(edge1).unwrap(), 10);
        assert_eq!(*deserialized.edge_weight(edge2).unwrap(), 20);
        assert_eq!(*deserialized.edge_weight(edge3).unwrap(), 30);
    }

    #[test]
    fn test_undirected_graph_roundtrip() {
        let mut graph = BoundedUnGraph::<FixedEdgeCount<3>, i32>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());

        graph.add_edge(n1, n2, 42).expect("Failed to add edge");

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");
        let deserialized: BoundedUnGraph<FixedEdgeCount<3>, i32> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        assert_eq!(graph.node_count(), deserialized.node_count());
        assert_eq!(graph.edge_count(), deserialized.edge_count());

        let edge = deserialized.find_edge(n1, n2).expect("Edge not found");
        assert_eq!(*deserialized.edge_weight(edge).unwrap(), 42);
    }

    #[test]
    fn test_fixed_edge_count_with_data_roundtrip() {
        #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
        struct NodeData {
            name: String,
            value: i32,
        }

        let mut graph = BoundedGraph::<FixedEdgeCount<2, 2, NodeData>, ()>::new();
        let n1 = graph.add_node(FixedEdgeCount::new(NodeData {
            name: "node1".to_string(),
            value: 42,
        }));
        let n2 = graph.add_node(FixedEdgeCount::new(NodeData {
            name: "node2".to_string(),
            value: 99,
        }));

        graph.add_edge(n1, n2, ()).expect("Failed to add edge");

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");
        let deserialized: BoundedGraph<FixedEdgeCount<2, 2, NodeData>, ()> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        assert_eq!(graph.node_count(), deserialized.node_count());
        assert_eq!(deserialized[n1].data.name, "node1");
        assert_eq!(deserialized[n1].data.value, 42);
        assert_eq!(deserialized[n2].data.name, "node2");
        assert_eq!(deserialized[n2].data.value, 99);
    }

    #[test]
    fn test_asymmetric_edge_limits_roundtrip() {
        let mut graph = BoundedGraph::<FixedEdgeCount<2, 5>, ()>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());
        let n3 = graph.add_node(FixedEdgeCount::empty());

        graph.add_edge(n1, n2, ()).expect("Failed to add edge");
        graph.add_edge(n1, n3, ()).expect("Failed to add edge");

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");
        let deserialized: BoundedGraph<FixedEdgeCount<2, 5>, ()> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        assert_eq!(graph.node_count(), deserialized.node_count());
        assert_eq!(graph.edge_count(), deserialized.edge_count());

        // Verify bounds are preserved
        assert!(deserialized.find_edge(n1, n2).is_some());
        assert!(deserialized.find_edge(n1, n3).is_some());
    }

    #[test]
    fn test_graph_preserves_structure_after_roundtrip() {
        let mut graph = BoundedGraph::<FixedEdgeCount<4>, String>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());
        let n3 = graph.add_node(FixedEdgeCount::empty());
        let n4 = graph.add_node(FixedEdgeCount::empty());

        graph
            .add_edge(n1, n2, "edge1".to_string())
            .expect("Failed to add edge");
        graph
            .add_edge(n2, n3, "edge2".to_string())
            .expect("Failed to add edge");
        graph
            .add_edge(n3, n4, "edge3".to_string())
            .expect("Failed to add edge");
        graph
            .add_edge(n4, n1, "edge4".to_string())
            .expect("Failed to add edge");

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");
        let deserialized: BoundedGraph<FixedEdgeCount<4>, String> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        // Verify all edges exist with correct weights
        let edges = vec![
            (n1, n2, "edge1"),
            (n2, n3, "edge2"),
            (n3, n4, "edge3"),
            (n4, n1, "edge4"),
        ];

        for (src, dst, expected_weight) in edges {
            let edge = deserialized
                .find_edge(src, dst)
                .expect(&format!("Edge {:?}->{:?} not found", src, dst));
            assert_eq!(deserialized.edge_weight(edge).unwrap(), expected_weight);
        }
    }

    #[test]
    fn test_bounded_graph_error_serialization() {
        use petgraph::graph::NodeIndex;

        let errors = vec![
            BoundedGraphError::source_rejected(NodeIndex::new(0), NodeIndex::new(1)),
            BoundedGraphError::target_rejected(NodeIndex::new(0), NodeIndex::new(1)),
            BoundedGraphError::both_rejected(NodeIndex::new(0), NodeIndex::new(1)),
            BoundedGraphError::not_found(Some(NodeIndex::new(0)), None),
        ];

        for error in errors {
            let serialized = serde_json::to_string(&error).expect("Failed to serialize error");
            let deserialized: BoundedGraphError =
                serde_json::from_str(&serialized).expect("Failed to deserialize error");
            assert_eq!(error, deserialized);
        }
    }

    #[test]
    fn test_fixed_edge_count_serialization() {
        let node = FixedEdgeCount::<3, 5, i32>::new(42);

        let serialized = serde_json::to_string(&node).expect("Failed to serialize");
        let deserialized: FixedEdgeCount<3, 5, i32> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        assert_eq!(node, deserialized);
        assert_eq!(deserialized.data, 42);
    }

    #[test]
    fn test_fixed_edge_count_empty_serialization() {
        let node = FixedEdgeCount::<2, 2>::empty();

        let serialized = serde_json::to_string(&node).expect("Failed to serialize");
        let deserialized: FixedEdgeCount<2, 2> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        assert_eq!(node, deserialized);
    }

    #[test]
    fn test_complex_graph_with_string_edges() {
        let mut graph = BoundedGraph::<FixedEdgeCount<10>, String>::new();
        let nodes: Vec<_> = (0..5)
            .map(|_| graph.add_node(FixedEdgeCount::empty()))
            .collect();

        graph
            .add_edge(nodes[0], nodes[1], "first".to_string())
            .expect("Failed to add edge");
        graph
            .add_edge(nodes[1], nodes[2], "second".to_string())
            .expect("Failed to add edge");
        graph
            .add_edge(nodes[2], nodes[3], "third".to_string())
            .expect("Failed to add edge");
        graph
            .add_edge(nodes[3], nodes[4], "fourth".to_string())
            .expect("Failed to add edge");
        graph
            .add_edge(nodes[0], nodes[4], "shortcut".to_string())
            .expect("Failed to add edge");

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");
        let deserialized: BoundedGraph<FixedEdgeCount<10>, String> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        assert_eq!(graph.node_count(), deserialized.node_count());
        assert_eq!(graph.edge_count(), deserialized.edge_count());
        assert_eq!(5, deserialized.edge_count());
    }

    #[test]
    fn test_serde_json_pretty() {
        let mut graph = BoundedGraph::<FixedEdgeCount<2>, ()>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());
        graph.add_edge(n1, n2, ()).expect("Failed to add edge");

        let serialized = serde_json::to_string_pretty(&graph).expect("Failed to serialize");

        // Verify it's valid JSON and can be deserialized
        let deserialized: BoundedGraph<FixedEdgeCount<2>, ()> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        assert_eq!(graph.node_count(), deserialized.node_count());
        assert_eq!(graph.edge_count(), deserialized.edge_count());
    }

    #[test]
    fn test_bounded_acyclic_graph_roundtrip() {
        use crate::BoundedAcyclicDiGraph;

        let mut dag = BoundedAcyclicDiGraph::<FixedEdgeCount<5>, i32>::new();
        let n1 = dag.add_node(FixedEdgeCount::empty());
        let n2 = dag.add_node(FixedEdgeCount::empty());
        let n3 = dag.add_node(FixedEdgeCount::empty());

        // Create a valid DAG: n1 -> n2 -> n3
        dag.add_edge(n1, n2, 10).expect("Failed to add edge");
        dag.add_edge(n2, n3, 20).expect("Failed to add edge");
        dag.add_edge(n1, n3, 30).expect("Failed to add edge");

        // Serialize the DAG
        let serialized = serde_json::to_string(&dag).expect("Failed to serialize");

        // Deserialize back
        let deserialized: BoundedAcyclicDiGraph<FixedEdgeCount<5>, i32> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        // Verify structure is preserved
        assert_eq!(dag.node_count(), deserialized.node_count());
        assert_eq!(dag.edge_count(), deserialized.edge_count());

        // Verify edges exist with correct weights
        let edge1 = deserialized
            .inner()
            .find_edge(n1, n2)
            .expect("Edge n1->n2 not found");
        let edge2 = deserialized
            .inner()
            .find_edge(n2, n3)
            .expect("Edge n2->n3 not found");
        let edge3 = deserialized
            .inner()
            .find_edge(n1, n3)
            .expect("Edge n1->n3 not found");

        assert_eq!(*deserialized.inner().edge_weight(edge1).unwrap(), 10);
        assert_eq!(*deserialized.inner().edge_weight(edge2).unwrap(), 20);
        assert_eq!(*deserialized.inner().edge_weight(edge3).unwrap(), 30);
    }

    #[test]
    fn test_bounded_acyclic_graph_rejects_cycles() {
        use crate::BoundedAcyclicDiGraph;

        // Create a graph with a cycle
        let mut graph = petgraph::Graph::<FixedEdgeCount<5>, i32>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());
        let n3 = graph.add_node(FixedEdgeCount::empty());

        // Create a cycle: n1 -> n2 -> n3 -> n1
        graph.add_edge(n1, n2, 10);
        graph.add_edge(n2, n3, 20);
        graph.add_edge(n3, n1, 30); // This creates a cycle

        // Serialize the cyclic graph
        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");

        // Attempt to deserialize as BoundedAcyclicDiGraph should fail
        let result: Result<BoundedAcyclicDiGraph<FixedEdgeCount<5>, i32>, _> =
            serde_json::from_str(&serialized);

        assert!(result.is_err(), "Deserializing a cyclic graph should fail");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("cycle"),
            "Error should mention cycle: {}",
            err_msg
        );
    }

    #[test]
    fn test_bounded_acyclic_graph_empty_dag() {
        use crate::BoundedAcyclicDiGraph;

        let dag = BoundedAcyclicDiGraph::<FixedEdgeCount<5>, ()>::new();

        let serialized = serde_json::to_string(&dag).expect("Failed to serialize");
        let deserialized: BoundedAcyclicDiGraph<FixedEdgeCount<5>, ()> =
            serde_json::from_str(&serialized).expect("Failed to deserialize empty DAG");

        assert_eq!(0, deserialized.node_count());
        assert_eq!(0, deserialized.edge_count());
    }

    #[test]
    fn test_bounded_acyclic_graph_complex_diamond_dag() {
        use crate::BoundedAcyclicDiGraph;

        // Create a diamond DAG structure
        //      n1
        //     /  \
        //   n2    n3
        //     \  /
        //      n4
        let mut dag = BoundedAcyclicDiGraph::<FixedEdgeCount<10>, String>::new();
        let n1 = dag.add_node(FixedEdgeCount::empty());
        let n2 = dag.add_node(FixedEdgeCount::empty());
        let n3 = dag.add_node(FixedEdgeCount::empty());
        let n4 = dag.add_node(FixedEdgeCount::empty());

        dag.add_edge(n1, n2, "1->2".to_string()).unwrap();
        dag.add_edge(n1, n3, "1->3".to_string()).unwrap();
        dag.add_edge(n2, n4, "2->4".to_string()).unwrap();
        dag.add_edge(n3, n4, "3->4".to_string()).unwrap();

        let serialized = serde_json::to_string(&dag).expect("Failed to serialize");
        let deserialized: BoundedAcyclicDiGraph<FixedEdgeCount<10>, String> =
            serde_json::from_str(&serialized).expect("Failed to deserialize complex DAG");

        assert_eq!(4, deserialized.node_count());
        assert_eq!(4, deserialized.edge_count());
    }

    #[test]
    fn test_bounded_acyclic_graph_rejects_simple_two_node_cycle() {
        use crate::BoundedAcyclicDiGraph;

        // Create a simple 2-node cycle: n1 -> n2 -> n1
        let mut graph = petgraph::Graph::<FixedEdgeCount<5>, i32>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());

        graph.add_edge(n1, n2, 10);
        graph.add_edge(n2, n1, 20);

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");

        let result: Result<BoundedAcyclicDiGraph<FixedEdgeCount<5>, i32>, _> =
            serde_json::from_str(&serialized);

        assert!(result.is_err(), "Should reject graph with simple cycle");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("cycle"),
            "Error should mention 'cycle': {}",
            err
        );
    }

    #[test]
    fn test_bounded_acyclic_graph_rejects_longer_four_node_cycle() {
        use crate::BoundedAcyclicDiGraph;

        // Create a cycle through multiple nodes: n1 -> n2 -> n3 -> n4 -> n1
        let mut graph = petgraph::Graph::<FixedEdgeCount<5>, ()>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());
        let n3 = graph.add_node(FixedEdgeCount::empty());
        let n4 = graph.add_node(FixedEdgeCount::empty());

        graph.add_edge(n1, n2, ());
        graph.add_edge(n2, n3, ());
        graph.add_edge(n3, n4, ());
        graph.add_edge(n4, n1, ()); // Closes the cycle

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");

        let result: Result<BoundedAcyclicDiGraph<FixedEdgeCount<5>, ()>, _> =
            serde_json::from_str(&serialized);

        assert!(result.is_err(), "Should reject graph with longer cycle");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("cycle"),
            "Error should mention 'cycle': {}",
            err
        );
    }

    #[test]
    fn test_bounded_acyclic_graph_rejects_self_loop() {
        use crate::BoundedAcyclicDiGraph;

        // Create a graph with a self-loop
        let mut graph = petgraph::Graph::<FixedEdgeCount<5>, i32>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());

        // Create a self-loop
        graph.add_edge(n1, n1, 42);

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");

        let result: Result<BoundedAcyclicDiGraph<FixedEdgeCount<5>, i32>, _> =
            serde_json::from_str(&serialized);

        assert!(result.is_err(), "Should reject graph with self-loop");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("cycle"),
            "Error should mention 'cycle': {}",
            err
        );
    }

    #[test]
    fn test_bounded_acyclic_graph_with_bounded_violation_outgoing() {
        use crate::BoundedAcyclicDiGraph;

        // Create a graph where a node exceeds its outgoing edge limit
        let mut graph = petgraph::Graph::<FixedEdgeCount<2>, ()>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());
        let n3 = graph.add_node(FixedEdgeCount::empty());
        let n4 = graph.add_node(FixedEdgeCount::empty());

        // n1 has 3 outgoing edges, but limit is 2
        graph.add_edge(n1, n2, ());
        graph.add_edge(n1, n3, ());
        graph.add_edge(n1, n4, ());

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");

        // Currently, deserialization doesn't validate bounded constraints
        // This test documents the current behavior - it will deserialize successfully
        // but operations on the graph will enforce constraints
        let result: Result<BoundedAcyclicDiGraph<FixedEdgeCount<2>, ()>, _> =
            serde_json::from_str(&serialized);

        // Note: This currently succeeds because deserialization only checks for cycles,
        // not bounded constraints. The bounded constraint is enforced at operation time.
        // This test documents this behavior and can be updated if validation is added.
        assert!(
            result.is_ok(),
            "Currently, bounded constraints are not validated during deserialization"
        );

        let deserialized = result.unwrap();
        // The graph was deserialized, but n1 is over capacity
        assert_eq!(3, deserialized.edge_count());

        // Attempting to add more edges should fail
        assert!(!deserialized.can_add_edge(n1, n2));
    }

    #[test]
    fn test_bounded_acyclic_graph_with_bounded_violation_incoming() {
        use crate::BoundedAcyclicDiGraph;

        // Create a graph where a node exceeds its incoming edge limit
        let mut graph = petgraph::Graph::<FixedEdgeCount<2, 1>, ()>::new();
        let n1 = graph.add_node(FixedEdgeCount::empty());
        let n2 = graph.add_node(FixedEdgeCount::empty());
        let n3 = graph.add_node(FixedEdgeCount::empty());
        let n4 = graph.add_node(FixedEdgeCount::empty());

        // n4 has 3 incoming edges, but limit is 1
        graph.add_edge(n1, n4, ());
        graph.add_edge(n2, n4, ());
        graph.add_edge(n3, n4, ());

        let serialized = serde_json::to_string(&graph).expect("Failed to serialize");

        let result: Result<BoundedAcyclicDiGraph<FixedEdgeCount<2, 1>, ()>, _> =
            serde_json::from_str(&serialized);

        // Similar to above - currently succeeds but documents behavior
        assert!(
            result.is_ok(),
            "Currently, bounded constraints are not validated during deserialization"
        );

        let deserialized = result.unwrap();
        assert_eq!(3, deserialized.edge_count());

        // n4 is at capacity for incoming edges
        assert!(!deserialized.can_add_edge(n1, n4));
    }

    #[test]
    fn test_bounded_acyclic_graph_preserves_edge_weights() {
        use crate::BoundedAcyclicDiGraph;

        let mut dag = BoundedAcyclicDiGraph::<FixedEdgeCount<5>, String>::new();
        let n1 = dag.add_node(FixedEdgeCount::empty());
        let n2 = dag.add_node(FixedEdgeCount::empty());
        let n3 = dag.add_node(FixedEdgeCount::empty());

        dag.add_edge(n1, n2, "alpha".to_string()).unwrap();
        dag.add_edge(n2, n3, "beta".to_string()).unwrap();
        dag.add_edge(n1, n3, "gamma".to_string()).unwrap();

        let serialized = serde_json::to_string(&dag).expect("Failed to serialize");
        let deserialized: BoundedAcyclicDiGraph<FixedEdgeCount<5>, String> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        // Verify edge weights are preserved
        let e1 = deserialized
            .inner()
            .find_edge(n1, n2)
            .expect("Edge n1->n2 not found");
        let e2 = deserialized
            .inner()
            .find_edge(n2, n3)
            .expect("Edge n2->n3 not found");
        let e3 = deserialized
            .inner()
            .find_edge(n1, n3)
            .expect("Edge n1->n3 not found");

        assert_eq!(deserialized.inner().edge_weight(e1).unwrap(), "alpha");
        assert_eq!(deserialized.inner().edge_weight(e2).unwrap(), "beta");
        assert_eq!(deserialized.inner().edge_weight(e3).unwrap(), "gamma");
    }

    #[test]
    fn test_bounded_acyclic_graph_with_custom_node_data() {
        use crate::BoundedAcyclicDiGraph;

        #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
        struct NodeData {
            id: u32,
            name: String,
        }

        let mut dag = BoundedAcyclicDiGraph::<FixedEdgeCount<3, 3, NodeData>, i32>::new();
        let n1 = dag.add_node(FixedEdgeCount::new(NodeData {
            id: 1,
            name: "first".to_string(),
        }));
        let n2 = dag.add_node(FixedEdgeCount::new(NodeData {
            id: 2,
            name: "second".to_string(),
        }));

        dag.add_edge(n1, n2, 100).unwrap();

        let serialized = serde_json::to_string(&dag).expect("Failed to serialize");
        let deserialized: BoundedAcyclicDiGraph<FixedEdgeCount<3, 3, NodeData>, i32> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        // Check node data is preserved
        assert_eq!(deserialized.inner()[n1].data.id, 1);
        assert_eq!(deserialized.inner()[n1].data.name, "first");
        assert_eq!(deserialized.inner()[n2].data.id, 2);
        assert_eq!(deserialized.inner()[n2].data.name, "second");

        // Check edge exists
        let e = deserialized
            .inner()
            .find_edge(n1, n2)
            .expect("Edge not found");
        assert_eq!(*deserialized.inner().edge_weight(e).unwrap(), 100);
    }

    #[test]
    fn test_bounded_acyclic_stable_graph_roundtrip() {
        use crate::BoundedAcyclicStableDiGraph;

        let mut dag = BoundedAcyclicStableDiGraph::<FixedEdgeCount<5>, i32>::new();
        let n1 = dag.add_node(FixedEdgeCount::empty());
        let n2 = dag.add_node(FixedEdgeCount::empty());
        let n3 = dag.add_node(FixedEdgeCount::empty());

        dag.add_edge(n1, n2, 10).unwrap();
        dag.add_edge(n2, n3, 20).unwrap();

        let serialized = serde_json::to_string(&dag).expect("Failed to serialize");
        let deserialized: BoundedAcyclicStableDiGraph<FixedEdgeCount<5>, i32> =
            serde_json::from_str(&serialized).expect("Failed to deserialize stable graph");

        assert_eq!(3, deserialized.node_count());
        assert_eq!(2, deserialized.edge_count());
    }

    #[test]
    fn test_bounded_acyclic_stable_graph_after_node_removal() {
        use crate::BoundedAcyclicStableDiGraph;

        // Create a DAG, remove a node, then serialize
        let mut dag = BoundedAcyclicStableDiGraph::<FixedEdgeCount<5>, ()>::new();
        let n1 = dag.add_node(FixedEdgeCount::empty());
        let n2 = dag.add_node(FixedEdgeCount::empty());
        let n3 = dag.add_node(FixedEdgeCount::empty());

        dag.add_edge(n1, n2, ()).unwrap();
        dag.add_edge(n2, n3, ()).unwrap();

        // Remove middle node
        dag.remove_node(n2);

        let serialized = serde_json::to_string(&dag).expect("Failed to serialize");
        let deserialized: BoundedAcyclicStableDiGraph<FixedEdgeCount<5>, ()> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        // After removal, n1 and n3 remain disconnected
        assert_eq!(2, deserialized.node_count());
        assert_eq!(0, deserialized.edge_count());
    }

    #[test]
    fn test_bounded_acyclic_graph_cycle_detection_after_deserialization() {
        use crate::BoundedAcyclicDiGraph;

        // Deserialize a valid DAG, then verify we can't add edges that create cycles
        let mut dag = BoundedAcyclicDiGraph::<FixedEdgeCount<5>, ()>::new();
        let n1 = dag.add_node(FixedEdgeCount::empty());
        let n2 = dag.add_node(FixedEdgeCount::empty());
        let n3 = dag.add_node(FixedEdgeCount::empty());

        dag.add_edge(n1, n2, ()).unwrap();
        dag.add_edge(n2, n3, ()).unwrap();

        let serialized = serde_json::to_string(&dag).expect("Failed to serialize");
        let mut deserialized: BoundedAcyclicDiGraph<FixedEdgeCount<5>, ()> =
            serde_json::from_str(&serialized).expect("Failed to deserialize");

        // Try to add edge that would create cycle
        use crate::BoundedAcyclicGraphError;
        let result = deserialized.add_edge(n3, n1, ());
        assert!(matches!(result, Err(BoundedAcyclicGraphError::Acyclic(_))));

        // Add a valid edge (no cycle)
        assert!(deserialized.add_edge(n1, n3, ()).is_ok());
    }
}