scirs2-graph 0.4.1

Graph processing module for SciRS2 (scirs2-graph)
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
//! Streaming graph processing for dynamic and large-scale graphs
//!
//! This module provides data structures and algorithms for processing graph
//! streams where edges arrive (and optionally depart) over time. It supports:
//!
//! - **Edge stream processing**: Incremental edge additions and deletions
//! - **Approximate degree distribution**: Maintained incrementally
//! - **Streaming triangle counting**: Doulion-style sampling and MASCOT-style
//!   edge sampling for approximate triangle counts
//! - **Sliding window model**: Maintain a graph over the most recent W edges
//! - **Memory-bounded processing**: Configurable memory limits with eviction
//!
//! # Design
//!
//! The streaming model assumes edges arrive one at a time. Algorithms maintain
//! approximate statistics without storing the entire graph, making them suitable
//! for graphs that do not fit in memory.

use crate::compressed::CsrGraph;
use crate::error::{GraphError, Result};
use scirs2_core::random::prelude::*;
use std::collections::{HashMap, HashSet, VecDeque};

// ────────────────────────────────────────────────────────────────────────────
// StreamEdge
// ────────────────────────────────────────────────────────────────────────────

/// A single edge in a graph stream.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StreamEdge {
    /// Source node
    pub src: usize,
    /// Destination node
    pub dst: usize,
    /// Edge weight
    pub weight: f64,
    /// Timestamp (monotonically increasing)
    pub timestamp: u64,
}

/// Type of stream operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamOp {
    /// Add an edge
    Insert,
    /// Remove an edge
    Delete,
}

/// A stream event: an edge with an operation type.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StreamEvent {
    /// The edge
    pub edge: StreamEdge,
    /// Whether this is an insertion or deletion
    pub op: StreamOp,
}

// ────────────────────────────────────────────────────────────────────────────
// StreamingGraph
// ────────────────────────────────────────────────────────────────────────────

/// A streaming graph that supports incremental edge additions and deletions.
///
/// Maintains an adjacency set representation that is updated incrementally.
/// Tracks basic statistics like degree distribution, edge count, and node count.
#[derive(Debug)]
pub struct StreamingGraph {
    /// Adjacency sets: node -> set of (neighbor, weight)
    adjacency: HashMap<usize, HashMap<usize, f64>>,
    /// Total number of edges currently in the graph
    num_edges: usize,
    /// Number of stream events processed
    events_processed: u64,
    /// Whether the graph is directed
    directed: bool,
    /// Maximum node ID seen
    max_node_id: usize,
}

impl StreamingGraph {
    /// Create a new empty streaming graph.
    pub fn new(directed: bool) -> Self {
        Self {
            adjacency: HashMap::new(),
            num_edges: 0,
            events_processed: 0,
            directed,
            max_node_id: 0,
        }
    }

    /// Process a stream event (insert or delete an edge).
    pub fn process_event(&mut self, event: &StreamEvent) {
        self.events_processed += 1;
        match event.op {
            StreamOp::Insert => self.insert_edge(event.edge.src, event.edge.dst, event.edge.weight),
            StreamOp::Delete => self.delete_edge(event.edge.src, event.edge.dst),
        }
    }

    /// Insert an edge into the graph.
    pub fn insert_edge(&mut self, src: usize, dst: usize, weight: f64) {
        self.max_node_id = self.max_node_id.max(src).max(dst);

        self.adjacency.entry(src).or_default().insert(dst, weight);

        if !self.directed {
            self.adjacency.entry(dst).or_default().insert(src, weight);
        }
        self.num_edges += 1;
    }

    /// Delete an edge from the graph.
    pub fn delete_edge(&mut self, src: usize, dst: usize) {
        if let Some(neighbors) = self.adjacency.get_mut(&src) {
            if neighbors.remove(&dst).is_some() {
                self.num_edges = self.num_edges.saturating_sub(1);
            }
        }
        if !self.directed {
            if let Some(neighbors) = self.adjacency.get_mut(&dst) {
                neighbors.remove(&src);
            }
        }
    }

    /// Check if an edge exists.
    pub fn has_edge(&self, src: usize, dst: usize) -> bool {
        self.adjacency
            .get(&src)
            .is_some_and(|neighbors| neighbors.contains_key(&dst))
    }

    /// Get the degree of a node.
    pub fn degree(&self, node: usize) -> usize {
        self.adjacency
            .get(&node)
            .map_or(0, |neighbors| neighbors.len())
    }

    /// Get the number of nodes.
    pub fn num_nodes(&self) -> usize {
        self.adjacency.len()
    }

    /// Get the number of edges.
    pub fn num_edges(&self) -> usize {
        self.num_edges
    }

    /// Get the number of events processed.
    pub fn events_processed(&self) -> u64 {
        self.events_processed
    }

    /// Get neighbors of a node.
    pub fn neighbors(&self, node: usize) -> Vec<(usize, f64)> {
        self.adjacency
            .get(&node)
            .map_or_else(Vec::new, |neighbors| {
                neighbors.iter().map(|(&n, &w)| (n, w)).collect()
            })
    }

    /// Get the degree distribution as a histogram.
    pub fn degree_distribution(&self) -> DegreeDistribution {
        let mut dist = HashMap::new();
        let mut max_degree = 0;
        let mut total_degree = 0;

        for neighbors in self.adjacency.values() {
            let deg = neighbors.len();
            *dist.entry(deg).or_insert(0usize) += 1;
            max_degree = max_degree.max(deg);
            total_degree += deg;
        }

        let n = self.adjacency.len();
        let avg_degree = if n > 0 {
            total_degree as f64 / n as f64
        } else {
            0.0
        };

        DegreeDistribution {
            histogram: dist,
            max_degree,
            avg_degree,
            num_nodes: n,
        }
    }

    /// Snapshot the current graph state as a CSR graph.
    pub fn to_csr(&self) -> Result<CsrGraph> {
        let num_nodes = if self.adjacency.is_empty() {
            0
        } else {
            self.max_node_id + 1
        };

        let mut edges = Vec::with_capacity(self.num_edges);
        for (&src, neighbors) in &self.adjacency {
            for (&dst, &weight) in neighbors {
                if self.directed || src <= dst {
                    edges.push((src, dst, weight));
                }
            }
        }

        CsrGraph::from_edges(num_nodes, edges, self.directed)
    }
}

/// Degree distribution statistics.
#[derive(Debug, Clone)]
pub struct DegreeDistribution {
    /// Histogram: degree -> count
    pub histogram: HashMap<usize, usize>,
    /// Maximum degree observed
    pub max_degree: usize,
    /// Average degree
    pub avg_degree: f64,
    /// Number of nodes
    pub num_nodes: usize,
}

// ────────────────────────────────────────────────────────────────────────────
// Streaming Triangle Counter (Doulion-style)
// ────────────────────────────────────────────────────────────────────────────

/// Approximate streaming triangle counter using edge sampling (Doulion algorithm).
///
/// The Doulion algorithm samples each edge with probability `p` and counts
/// triangles in the sampled subgraph. The triangle count is then scaled by `1/p^3`
/// to estimate the total.
///
/// # Reference
/// Tsourakakis et al., "Doulion: Counting Triangles in Massive Graphs with
/// a Coin", KDD 2009.
#[derive(Debug)]
pub struct DoulionTriangleCounter {
    /// Sampling probability
    sample_prob: f64,
    /// Sampled edges as adjacency sets
    sampled_adj: HashMap<usize, HashSet<usize>>,
    /// Number of triangles found in sampled subgraph
    sampled_triangles: usize,
    /// Number of edges processed
    edges_processed: u64,
    /// Number of edges sampled
    edges_sampled: u64,
    /// RNG for sampling
    rng: StdRng,
}

impl DoulionTriangleCounter {
    /// Create a new Doulion triangle counter with given sampling probability.
    ///
    /// Lower `sample_prob` uses less memory but gives less accurate estimates.
    /// A value of 0.1 is reasonable for graphs with millions of edges.
    pub fn new(sample_prob: f64, seed: u64) -> Result<Self> {
        if !(0.0..=1.0).contains(&sample_prob) {
            return Err(GraphError::InvalidGraph(
                "sample_prob must be in [0, 1]".to_string(),
            ));
        }
        Ok(Self {
            sample_prob,
            sampled_adj: HashMap::new(),
            sampled_triangles: 0,
            edges_processed: 0,
            edges_sampled: 0,
            rng: StdRng::seed_from_u64(seed),
        })
    }

    /// Process a new edge from the stream.
    ///
    /// With probability `p`, the edge is sampled. If both endpoints are already
    /// in the sample, we check for triangles formed.
    pub fn process_edge(&mut self, src: usize, dst: usize) {
        self.edges_processed += 1;

        // Sample this edge with probability p
        if self.rng.random::<f64>() >= self.sample_prob {
            return;
        }

        self.edges_sampled += 1;

        // Before adding the edge, count new triangles formed
        // A triangle is formed if there exists a node w such that
        // w is a neighbor of both src and dst in the sampled graph
        let neighbors_src: HashSet<usize> = self.sampled_adj.get(&src).cloned().unwrap_or_default();
        let neighbors_dst: HashSet<usize> = self.sampled_adj.get(&dst).cloned().unwrap_or_default();

        // Count common neighbors
        let common = neighbors_src.intersection(&neighbors_dst).count();
        self.sampled_triangles += common;

        // Add the edge to the sample
        self.sampled_adj.entry(src).or_default().insert(dst);
        self.sampled_adj.entry(dst).or_default().insert(src);
    }

    /// Get the estimated total number of triangles.
    pub fn estimated_triangles(&self) -> f64 {
        if self.sample_prob <= 0.0 {
            return 0.0;
        }
        // Scale by 1/p^3
        let p3 = self.sample_prob * self.sample_prob * self.sample_prob;
        self.sampled_triangles as f64 / p3
    }

    /// Get the number of sampled triangles (unscaled).
    pub fn sampled_triangles(&self) -> usize {
        self.sampled_triangles
    }

    /// Get statistics about the counter.
    pub fn stats(&self) -> TriangleCounterStats {
        TriangleCounterStats {
            edges_processed: self.edges_processed,
            edges_sampled: self.edges_sampled,
            sampled_triangles: self.sampled_triangles,
            estimated_triangles: self.estimated_triangles(),
            sample_prob: self.sample_prob,
            memory_nodes: self.sampled_adj.len(),
        }
    }
}

/// Statistics from a streaming triangle counter.
#[derive(Debug, Clone)]
pub struct TriangleCounterStats {
    /// Total edges processed from the stream
    pub edges_processed: u64,
    /// Number of edges retained in the sample
    pub edges_sampled: u64,
    /// Triangles found in the sample
    pub sampled_triangles: usize,
    /// Estimated total triangles (scaled)
    pub estimated_triangles: f64,
    /// Sampling probability used
    pub sample_prob: f64,
    /// Number of nodes stored in memory
    pub memory_nodes: usize,
}

// ────────────────────────────────────────────────────────────────────────────
// MASCOT-style Triangle Counter
// ────────────────────────────────────────────────────────────────────────────

/// MASCOT (Memory-efficient Accurate Sampling for Counting Local Triangles)
/// streaming triangle counter.
///
/// Maintains a fixed-size edge reservoir sample and updates triangle counts
/// as new edges arrive. More memory-efficient than Doulion for fixed memory budgets.
///
/// # Reference
/// Lim & Kang, "MASCOT: Memory-efficient and Accurate Sampling for Counting
/// Local Triangles in Graph Streams", KDD 2015.
#[derive(Debug)]
pub struct MascotTriangleCounter {
    /// Maximum number of edges to store
    max_edges: usize,
    /// Current edge reservoir
    edges: Vec<(usize, usize)>,
    /// Adjacency sets for quick triangle checks
    adj: HashMap<usize, HashSet<usize>>,
    /// Global triangle count estimate (with scaling)
    triangle_estimate: f64,
    /// Number of edges processed
    edges_processed: u64,
    /// RNG for reservoir sampling
    rng: StdRng,
}

impl MascotTriangleCounter {
    /// Create a new MASCOT counter with a fixed edge budget.
    pub fn new(max_edges: usize, seed: u64) -> Self {
        Self {
            max_edges,
            edges: Vec::with_capacity(max_edges),
            adj: HashMap::new(),
            triangle_estimate: 0.0,
            edges_processed: 0,
            rng: StdRng::seed_from_u64(seed),
        }
    }

    /// Process a new edge from the stream.
    pub fn process_edge(&mut self, src: usize, dst: usize) {
        self.edges_processed += 1;

        // Count triangles formed with current sample
        let neighbors_src: Vec<usize> = self
            .adj
            .get(&src)
            .map_or_else(Vec::new, |s| s.iter().copied().collect());
        let neighbors_dst: HashSet<usize> = self.adj.get(&dst).cloned().unwrap_or_default();

        let common_count = neighbors_src
            .iter()
            .filter(|w| neighbors_dst.contains(w))
            .count();

        // Scale factor: probability that both other edges are in the sample
        let t = self.edges_processed;
        let m = self.max_edges;
        if t <= m as u64 {
            // All edges are stored, no scaling needed
            self.triangle_estimate += common_count as f64;
        } else {
            // Reservoir sampling probability for an edge to be in sample
            let p = m as f64 / t as f64;
            // Both other edges of triangle must be in sample: scale by 1/p^2
            if p > 0.0 {
                self.triangle_estimate += common_count as f64 / (p * p);
            }
        }

        // Reservoir sampling: decide whether to include this edge
        if self.edges.len() < self.max_edges {
            // Sample not full yet, always include
            self.edges.push((src, dst));
            self.adj.entry(src).or_default().insert(dst);
            self.adj.entry(dst).or_default().insert(src);
        } else {
            // Replace a random edge with probability max_edges / edges_processed
            let j = self.rng.random_range(0..self.edges_processed as usize);
            if j < self.max_edges {
                // Remove old edge
                let (old_src, old_dst) = self.edges[j];
                if let Some(set) = self.adj.get_mut(&old_src) {
                    set.remove(&old_dst);
                }
                if let Some(set) = self.adj.get_mut(&old_dst) {
                    set.remove(&old_src);
                }

                // Insert new edge
                self.edges[j] = (src, dst);
                self.adj.entry(src).or_default().insert(dst);
                self.adj.entry(dst).or_default().insert(src);
            }
        }
    }

    /// Get the estimated triangle count.
    pub fn estimated_triangles(&self) -> f64 {
        self.triangle_estimate
    }

    /// Get counter statistics.
    pub fn stats(&self) -> TriangleCounterStats {
        TriangleCounterStats {
            edges_processed: self.edges_processed,
            edges_sampled: self.edges.len() as u64,
            sampled_triangles: 0, // MASCOT tracks scaled estimate directly
            estimated_triangles: self.triangle_estimate,
            sample_prob: if self.edges_processed > 0 {
                self.edges.len() as f64 / self.edges_processed as f64
            } else {
                1.0
            },
            memory_nodes: self.adj.len(),
        }
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Sliding Window Graph
// ────────────────────────────────────────────────────────────────────────────

/// A sliding window graph that maintains only the most recent `W` edges.
///
/// As new edges arrive, old edges beyond the window are automatically evicted.
/// This is useful for maintaining a graph over a time-bounded stream.
#[derive(Debug)]
pub struct SlidingWindowGraph {
    /// Window size (maximum number of edges to retain)
    window_size: usize,
    /// Ordered queue of edges (front = oldest)
    edge_queue: VecDeque<(usize, usize, f64)>,
    /// Current adjacency representation
    adjacency: HashMap<usize, HashMap<usize, f64>>,
    /// Edge count for each (src, dst) pair to handle multi-edges
    edge_counts: HashMap<(usize, usize), usize>,
    /// Whether the graph is directed
    directed: bool,
    /// Total events processed
    events_processed: u64,
}

impl SlidingWindowGraph {
    /// Create a new sliding window graph.
    pub fn new(window_size: usize, directed: bool) -> Self {
        Self {
            window_size,
            edge_queue: VecDeque::with_capacity(window_size),
            adjacency: HashMap::new(),
            edge_counts: HashMap::new(),
            directed,
            events_processed: 0,
        }
    }

    /// Process a new edge. If the window is full, the oldest edge is evicted.
    pub fn process_edge(&mut self, src: usize, dst: usize, weight: f64) {
        self.events_processed += 1;

        // Evict oldest edge if window is full
        if self.edge_queue.len() >= self.window_size {
            if let Some((old_src, old_dst, _old_weight)) = self.edge_queue.pop_front() {
                self.remove_edge_internal(old_src, old_dst);
            }
        }

        // Add new edge
        self.edge_queue.push_back((src, dst, weight));
        self.add_edge_internal(src, dst, weight);
    }

    fn add_edge_internal(&mut self, src: usize, dst: usize, weight: f64) {
        self.adjacency.entry(src).or_default().insert(dst, weight);
        *self.edge_counts.entry((src, dst)).or_insert(0) += 1;

        if !self.directed {
            self.adjacency.entry(dst).or_default().insert(src, weight);
            *self.edge_counts.entry((dst, src)).or_insert(0) += 1;
        }
    }

    fn remove_edge_internal(&mut self, src: usize, dst: usize) {
        let key = (src, dst);
        if let Some(count) = self.edge_counts.get_mut(&key) {
            *count = count.saturating_sub(1);
            if *count == 0 {
                self.edge_counts.remove(&key);
                if let Some(neighbors) = self.adjacency.get_mut(&src) {
                    neighbors.remove(&dst);
                    if neighbors.is_empty() {
                        self.adjacency.remove(&src);
                    }
                }
            }
        }

        if !self.directed {
            let rev_key = (dst, src);
            if let Some(count) = self.edge_counts.get_mut(&rev_key) {
                *count = count.saturating_sub(1);
                if *count == 0 {
                    self.edge_counts.remove(&rev_key);
                    if let Some(neighbors) = self.adjacency.get_mut(&dst) {
                        neighbors.remove(&src);
                        if neighbors.is_empty() {
                            self.adjacency.remove(&dst);
                        }
                    }
                }
            }
        }
    }

    /// Get the current number of edges in the window.
    pub fn num_edges(&self) -> usize {
        self.edge_queue.len()
    }

    /// Get the current number of active nodes.
    pub fn num_nodes(&self) -> usize {
        self.adjacency.len()
    }

    /// Get the window size.
    pub fn window_size(&self) -> usize {
        self.window_size
    }

    /// Get neighbors of a node in the current window.
    pub fn neighbors(&self, node: usize) -> Vec<(usize, f64)> {
        self.adjacency
            .get(&node)
            .map_or_else(Vec::new, |neighbors| {
                neighbors.iter().map(|(&n, &w)| (n, w)).collect()
            })
    }

    /// Get the degree of a node.
    pub fn degree(&self, node: usize) -> usize {
        self.adjacency
            .get(&node)
            .map_or(0, |neighbors| neighbors.len())
    }

    /// Check if an edge exists in the current window.
    pub fn has_edge(&self, src: usize, dst: usize) -> bool {
        self.adjacency
            .get(&src)
            .is_some_and(|n| n.contains_key(&dst))
    }

    /// Get total events processed.
    pub fn events_processed(&self) -> u64 {
        self.events_processed
    }

    /// Take a snapshot of the current window as a CSR graph.
    pub fn to_csr(&self) -> Result<CsrGraph> {
        let max_node = self
            .adjacency
            .keys()
            .chain(self.adjacency.values().flat_map(|n| n.keys()))
            .copied()
            .max()
            .map_or(0, |m| m + 1);

        let mut edges = Vec::new();
        for (&src, neighbors) in &self.adjacency {
            for (&dst, &weight) in neighbors {
                if self.directed || src <= dst {
                    edges.push((src, dst, weight));
                }
            }
        }

        CsrGraph::from_edges(max_node, edges, self.directed)
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Memory-Bounded Stream Processor
// ────────────────────────────────────────────────────────────────────────────

/// Configuration for memory-bounded stream processing.
#[derive(Debug, Clone)]
pub struct MemoryBoundedConfig {
    /// Maximum memory budget in bytes
    pub max_memory_bytes: usize,
    /// Eviction strategy when memory is exceeded
    pub eviction_strategy: EvictionStrategy,
    /// Whether to track degree distribution
    pub track_degrees: bool,
    /// Whether to count triangles
    pub count_triangles: bool,
    /// Triangle counting sample probability
    pub triangle_sample_prob: f64,
}

impl Default for MemoryBoundedConfig {
    fn default() -> Self {
        Self {
            max_memory_bytes: 100 * 1024 * 1024, // 100 MB
            eviction_strategy: EvictionStrategy::LeastRecentEdge,
            track_degrees: true,
            count_triangles: false,
            triangle_sample_prob: 0.1,
        }
    }
}

/// Strategy for evicting data when memory budget is exceeded.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EvictionStrategy {
    /// Remove the oldest edges first (FIFO)
    LeastRecentEdge,
    /// Remove edges from the lowest-degree nodes first
    LowestDegreeNode,
    /// Random edge removal
    RandomEdge,
}

/// A memory-bounded stream processor that enforces a memory budget.
///
/// Processes edge events and maintains approximate graph statistics
/// while staying within a configurable memory limit.
#[derive(Debug)]
pub struct MemoryBoundedProcessor {
    /// Configuration
    config: MemoryBoundedConfig,
    /// The underlying streaming graph
    graph: StreamingGraph,
    /// Edge insertion order (for LeastRecentEdge eviction)
    insertion_order: VecDeque<(usize, usize)>,
    /// Approximate memory usage in bytes
    estimated_memory: usize,
    /// Edges evicted
    edges_evicted: u64,
    /// RNG for random eviction
    rng: StdRng,
}

impl MemoryBoundedProcessor {
    /// Create a new memory-bounded processor.
    pub fn new(config: MemoryBoundedConfig) -> Self {
        Self {
            graph: StreamingGraph::new(false),
            insertion_order: VecDeque::new(),
            estimated_memory: 0,
            edges_evicted: 0,
            rng: StdRng::seed_from_u64(42),
            config,
        }
    }

    /// Process an edge event, evicting old edges if memory budget is exceeded.
    pub fn process_edge(&mut self, src: usize, dst: usize, weight: f64) {
        // Estimate memory for this edge (~80 bytes for HashMap entries + overhead)
        let edge_memory_estimate = 80;

        // Evict edges if over budget
        while self.estimated_memory + edge_memory_estimate > self.config.max_memory_bytes
            && !self.insertion_order.is_empty()
        {
            self.evict_one();
        }

        // Insert the new edge
        self.graph.insert_edge(src, dst, weight);
        self.insertion_order.push_back((src, dst));
        self.estimated_memory += edge_memory_estimate;
    }

    fn evict_one(&mut self) {
        match self.config.eviction_strategy {
            EvictionStrategy::LeastRecentEdge => {
                if let Some((src, dst)) = self.insertion_order.pop_front() {
                    self.graph.delete_edge(src, dst);
                    self.estimated_memory = self.estimated_memory.saturating_sub(80);
                    self.edges_evicted += 1;
                }
            }
            EvictionStrategy::LowestDegreeNode => {
                // Find the node with the lowest degree
                if let Some((&node, _)) = self
                    .graph
                    .adjacency
                    .iter()
                    .min_by_key(|(_, neighbors)| neighbors.len())
                {
                    let neighbors: Vec<usize> = self
                        .graph
                        .adjacency
                        .get(&node)
                        .map_or_else(Vec::new, |n| n.keys().copied().collect());
                    for neighbor in neighbors {
                        self.graph.delete_edge(node, neighbor);
                        self.estimated_memory = self.estimated_memory.saturating_sub(80);
                        self.edges_evicted += 1;
                    }
                }
            }
            EvictionStrategy::RandomEdge => {
                if !self.insertion_order.is_empty() {
                    let idx = self.rng.random_range(0..self.insertion_order.len());
                    if let Some((src, dst)) = self.insertion_order.remove(idx) {
                        self.graph.delete_edge(src, dst);
                        self.estimated_memory = self.estimated_memory.saturating_sub(80);
                        self.edges_evicted += 1;
                    }
                }
            }
        }
    }

    /// Get the current streaming graph.
    pub fn graph(&self) -> &StreamingGraph {
        &self.graph
    }

    /// Get the number of edges evicted.
    pub fn edges_evicted(&self) -> u64 {
        self.edges_evicted
    }

    /// Get estimated memory usage in bytes.
    pub fn estimated_memory(&self) -> usize {
        self.estimated_memory
    }

    /// Get the degree distribution of the current graph.
    pub fn degree_distribution(&self) -> DegreeDistribution {
        self.graph.degree_distribution()
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Tests
// ────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // ── StreamingGraph Tests ──

    #[test]
    fn test_streaming_graph_insert() {
        let mut g = StreamingGraph::new(false);
        g.insert_edge(0, 1, 1.0);
        g.insert_edge(1, 2, 2.0);

        assert_eq!(g.num_edges(), 2);
        assert_eq!(g.num_nodes(), 3);
        assert!(g.has_edge(0, 1));
        assert!(g.has_edge(1, 0)); // undirected
        assert!(g.has_edge(1, 2));
    }

    #[test]
    fn test_streaming_graph_delete() {
        let mut g = StreamingGraph::new(false);
        g.insert_edge(0, 1, 1.0);
        g.insert_edge(1, 2, 2.0);
        g.delete_edge(0, 1);

        assert_eq!(g.num_edges(), 1);
        assert!(!g.has_edge(0, 1));
        assert!(!g.has_edge(1, 0)); // undirected deletion
        assert!(g.has_edge(1, 2));
    }

    #[test]
    fn test_streaming_graph_directed() {
        let mut g = StreamingGraph::new(true);
        g.insert_edge(0, 1, 1.0);

        assert!(g.has_edge(0, 1));
        assert!(!g.has_edge(1, 0)); // directed
    }

    #[test]
    fn test_streaming_graph_process_event() {
        let mut g = StreamingGraph::new(false);
        let event = StreamEvent {
            edge: StreamEdge {
                src: 0,
                dst: 1,
                weight: 1.0,
                timestamp: 0,
            },
            op: StreamOp::Insert,
        };
        g.process_event(&event);
        assert_eq!(g.num_edges(), 1);
        assert_eq!(g.events_processed(), 1);

        let del_event = StreamEvent {
            edge: StreamEdge {
                src: 0,
                dst: 1,
                weight: 1.0,
                timestamp: 1,
            },
            op: StreamOp::Delete,
        };
        g.process_event(&del_event);
        assert_eq!(g.num_edges(), 0);
        assert_eq!(g.events_processed(), 2);
    }

    #[test]
    fn test_streaming_graph_degree() {
        let mut g = StreamingGraph::new(false);
        g.insert_edge(0, 1, 1.0);
        g.insert_edge(0, 2, 1.0);
        g.insert_edge(0, 3, 1.0);

        assert_eq!(g.degree(0), 3);
        assert_eq!(g.degree(1), 1);
        assert_eq!(g.degree(4), 0); // non-existent node
    }

    #[test]
    fn test_streaming_graph_neighbors() {
        let mut g = StreamingGraph::new(false);
        g.insert_edge(0, 1, 1.0);
        g.insert_edge(0, 2, 2.0);

        let mut neighbors = g.neighbors(0);
        neighbors.sort_by_key(|&(n, _)| n);
        assert_eq!(neighbors.len(), 2);
        assert_eq!(neighbors[0].0, 1);
        assert_eq!(neighbors[1].0, 2);
    }

    #[test]
    fn test_streaming_graph_degree_distribution() {
        let mut g = StreamingGraph::new(false);
        // Star graph: center=0, spokes=1,2,3,4
        g.insert_edge(0, 1, 1.0);
        g.insert_edge(0, 2, 1.0);
        g.insert_edge(0, 3, 1.0);
        g.insert_edge(0, 4, 1.0);

        let dist = g.degree_distribution();
        assert_eq!(dist.max_degree, 4);
        assert_eq!(dist.num_nodes, 5);
        // Node 0 has degree 4, nodes 1-4 have degree 1
        assert_eq!(dist.histogram.get(&4), Some(&1));
        assert_eq!(dist.histogram.get(&1), Some(&4));
    }

    #[test]
    fn test_streaming_graph_to_csr() {
        let mut g = StreamingGraph::new(false);
        g.insert_edge(0, 1, 1.0);
        g.insert_edge(1, 2, 2.0);

        let csr = g.to_csr().expect("to_csr");
        assert_eq!(csr.num_nodes(), 3);
        assert!(csr.has_edge(0, 1));
        assert!(csr.has_edge(1, 0));
        assert!(csr.has_edge(1, 2));
    }

    // ── Doulion Triangle Counter Tests ──

    #[test]
    fn test_doulion_basic() {
        // Complete graph K4 has 4 triangles
        let mut counter = DoulionTriangleCounter::new(1.0, 42).expect("new");

        // Add all edges of K4
        counter.process_edge(0, 1);
        counter.process_edge(0, 2);
        counter.process_edge(0, 3);
        counter.process_edge(1, 2);
        counter.process_edge(1, 3);
        counter.process_edge(2, 3);

        // With p=1.0, the estimate should be exact
        let est = counter.estimated_triangles();
        assert!((est - 4.0).abs() < 1e-6, "expected 4 triangles, got {est}");

        let stats = counter.stats();
        assert_eq!(stats.edges_processed, 6);
        assert_eq!(stats.edges_sampled, 6);
    }

    #[test]
    fn test_doulion_no_triangles() {
        let mut counter = DoulionTriangleCounter::new(1.0, 42).expect("new");

        // Path graph: no triangles
        counter.process_edge(0, 1);
        counter.process_edge(1, 2);
        counter.process_edge(2, 3);

        assert!(counter.estimated_triangles().abs() < 1e-6);
    }

    #[test]
    fn test_doulion_sampling() {
        // With p=0.5, the estimate may differ from exact
        let mut counter = DoulionTriangleCounter::new(0.5, 42).expect("new");

        // K4 edges
        counter.process_edge(0, 1);
        counter.process_edge(0, 2);
        counter.process_edge(0, 3);
        counter.process_edge(1, 2);
        counter.process_edge(1, 3);
        counter.process_edge(2, 3);

        // The estimate should be non-negative
        assert!(counter.estimated_triangles() >= 0.0);
    }

    #[test]
    fn test_doulion_invalid_prob() {
        assert!(DoulionTriangleCounter::new(1.5, 42).is_err());
        assert!(DoulionTriangleCounter::new(-0.1, 42).is_err());
    }

    // ── MASCOT Triangle Counter Tests ──

    #[test]
    fn test_mascot_basic() {
        let mut counter = MascotTriangleCounter::new(100, 42);

        // K4 edges
        counter.process_edge(0, 1);
        counter.process_edge(0, 2);
        counter.process_edge(0, 3);
        counter.process_edge(1, 2);
        counter.process_edge(1, 3);
        counter.process_edge(2, 3);

        // With budget=100 and only 6 edges, all are stored -> exact count
        let est = counter.estimated_triangles();
        assert!((est - 4.0).abs() < 1e-6, "expected 4 triangles, got {est}");
    }

    #[test]
    fn test_mascot_stats() {
        let mut counter = MascotTriangleCounter::new(100, 42);
        counter.process_edge(0, 1);
        counter.process_edge(1, 2);

        let stats = counter.stats();
        assert_eq!(stats.edges_processed, 2);
        assert_eq!(stats.edges_sampled, 2);
    }

    // ── Sliding Window Tests ──

    #[test]
    fn test_sliding_window_basic() {
        let mut sw = SlidingWindowGraph::new(3, false);

        sw.process_edge(0, 1, 1.0);
        sw.process_edge(1, 2, 2.0);
        sw.process_edge(2, 3, 3.0);

        assert_eq!(sw.num_edges(), 3);
        assert!(sw.has_edge(0, 1));
        assert!(sw.has_edge(1, 2));
        assert!(sw.has_edge(2, 3));

        // Add one more: oldest (0-1) should be evicted
        sw.process_edge(3, 4, 4.0);
        assert_eq!(sw.num_edges(), 3);
        assert!(!sw.has_edge(0, 1)); // evicted
        assert!(sw.has_edge(1, 2));
        assert!(sw.has_edge(2, 3));
        assert!(sw.has_edge(3, 4));
    }

    #[test]
    fn test_sliding_window_directed() {
        let mut sw = SlidingWindowGraph::new(5, true);

        sw.process_edge(0, 1, 1.0);
        assert!(sw.has_edge(0, 1));
        assert!(!sw.has_edge(1, 0)); // directed
    }

    #[test]
    fn test_sliding_window_degree() {
        let mut sw = SlidingWindowGraph::new(10, false);
        sw.process_edge(0, 1, 1.0);
        sw.process_edge(0, 2, 1.0);
        sw.process_edge(0, 3, 1.0);

        assert_eq!(sw.degree(0), 3);
        assert_eq!(sw.degree(1), 1);
    }

    #[test]
    fn test_sliding_window_events_processed() {
        let mut sw = SlidingWindowGraph::new(2, false);
        sw.process_edge(0, 1, 1.0);
        sw.process_edge(1, 2, 1.0);
        sw.process_edge(2, 3, 1.0); // evicts 0-1

        assert_eq!(sw.events_processed(), 3);
        assert_eq!(sw.num_edges(), 2);
    }

    #[test]
    fn test_sliding_window_to_csr() {
        let mut sw = SlidingWindowGraph::new(10, false);
        sw.process_edge(0, 1, 1.0);
        sw.process_edge(1, 2, 2.0);

        let csr = sw.to_csr().expect("to_csr");
        assert_eq!(csr.num_nodes(), 3);
        assert!(csr.has_edge(0, 1));
    }

    // ── Memory-Bounded Processor Tests ──

    #[test]
    fn test_memory_bounded_basic() {
        let config = MemoryBoundedConfig {
            max_memory_bytes: 400, // Very small: ~5 edges
            eviction_strategy: EvictionStrategy::LeastRecentEdge,
            track_degrees: true,
            count_triangles: false,
            triangle_sample_prob: 0.1,
        };
        let mut proc = MemoryBoundedProcessor::new(config);

        for i in 0..20 {
            proc.process_edge(i, i + 1, 1.0);
        }

        // Some edges should have been evicted
        assert!(proc.edges_evicted() > 0);
        assert!(proc.estimated_memory() <= 400);
    }

    #[test]
    fn test_memory_bounded_degree_dist() {
        let config = MemoryBoundedConfig {
            max_memory_bytes: 10_000,
            ..Default::default()
        };
        let mut proc = MemoryBoundedProcessor::new(config);

        proc.process_edge(0, 1, 1.0);
        proc.process_edge(0, 2, 1.0);
        proc.process_edge(0, 3, 1.0);

        let dist = proc.degree_distribution();
        assert!(dist.num_nodes > 0);
    }

    #[test]
    fn test_memory_bounded_eviction_strategies() {
        for strategy in &[
            EvictionStrategy::LeastRecentEdge,
            EvictionStrategy::RandomEdge,
        ] {
            let config = MemoryBoundedConfig {
                max_memory_bytes: 200,
                eviction_strategy: *strategy,
                ..Default::default()
            };
            let mut proc = MemoryBoundedProcessor::new(config);

            for i in 0..10 {
                proc.process_edge(i, i + 1, 1.0);
            }

            // Should not crash and should evict
            assert!(proc.edges_evicted() > 0 || proc.estimated_memory() <= 200);
        }
    }

    #[test]
    fn test_streaming_graph_empty() {
        let g = StreamingGraph::new(false);
        assert_eq!(g.num_nodes(), 0);
        assert_eq!(g.num_edges(), 0);
        assert!(g.neighbors(0).is_empty());
        assert_eq!(g.degree(0), 0);
    }

    #[test]
    fn test_streaming_graph_delete_nonexistent() {
        let mut g = StreamingGraph::new(false);
        g.insert_edge(0, 1, 1.0);
        g.delete_edge(5, 6); // should not crash
        assert_eq!(g.num_edges(), 1);
    }

    #[test]
    fn test_sliding_window_single_capacity() {
        let mut sw = SlidingWindowGraph::new(1, false);
        sw.process_edge(0, 1, 1.0);
        assert_eq!(sw.num_edges(), 1);
        assert!(sw.has_edge(0, 1));

        sw.process_edge(2, 3, 2.0);
        assert_eq!(sw.num_edges(), 1);
        assert!(!sw.has_edge(0, 1)); // evicted
        assert!(sw.has_edge(2, 3));
    }
}