ruvector-mincut 2.0.6

World's first subpolynomial dynamic min-cut: self-healing networks, AI optimization, real-time graph analysis
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
//! Parallel Level Updates with Work-Stealing
//!
//! Provides efficient parallel computation for j-tree levels:
//! - Rayon-based parallel iteration
//! - Work-stealing for load balancing
//! - Lock-free result aggregation
//! - Adaptive parallelism based on workload
//!
//! Target: Near-linear speedup for independent level updates

use crate::graph::VertexId;
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, RwLock};

#[cfg(feature = "rayon")]
use rayon::prelude::*;

/// Configuration for parallel level updates
#[derive(Debug, Clone)]
pub struct ParallelConfig {
    /// Minimum workload to use parallelism
    pub min_parallel_size: usize,
    /// Number of threads (0 = auto-detect)
    pub num_threads: usize,
    /// Enable work-stealing
    pub work_stealing: bool,
    /// Chunk size for parallel iteration
    pub chunk_size: usize,
    /// Enable adaptive parallelism
    pub adaptive: bool,
}

impl Default for ParallelConfig {
    fn default() -> Self {
        Self {
            min_parallel_size: 100,
            num_threads: 0, // Auto-detect
            work_stealing: true,
            chunk_size: 64,
            adaptive: true,
        }
    }
}

/// Work item for parallel processing
#[derive(Debug, Clone)]
pub struct WorkItem {
    /// Level index
    pub level: usize,
    /// Vertices to process
    pub vertices: Vec<VertexId>,
    /// Priority (lower = higher priority)
    pub priority: u32,
    /// Estimated work units
    pub estimated_work: usize,
}

/// Result from parallel level update
#[derive(Debug, Clone)]
pub struct LevelUpdateResult {
    /// Level index
    pub level: usize,
    /// Computed cut value
    pub cut_value: f64,
    /// Partition (vertices on one side)
    pub partition: HashSet<VertexId>,
    /// Time taken in microseconds
    pub time_us: u64,
}

/// Work-stealing scheduler for parallel level processing
pub struct WorkStealingScheduler {
    config: ParallelConfig,
    /// Work queue
    work_queue: RwLock<Vec<WorkItem>>,
    /// Completed results
    results: RwLock<HashMap<usize, LevelUpdateResult>>,
    /// Active workers count
    active_workers: AtomicUsize,
    /// Total work processed
    total_work: AtomicU64,
    /// Steal count
    steals: AtomicU64,
}

impl WorkStealingScheduler {
    /// Create new scheduler with default config
    pub fn new() -> Self {
        Self::with_config(ParallelConfig::default())
    }

    /// Create with custom config
    pub fn with_config(config: ParallelConfig) -> Self {
        Self {
            config,
            work_queue: RwLock::new(Vec::new()),
            results: RwLock::new(HashMap::new()),
            active_workers: AtomicUsize::new(0),
            total_work: AtomicU64::new(0),
            steals: AtomicU64::new(0),
        }
    }

    /// Submit work item
    pub fn submit(&self, item: WorkItem) {
        let mut queue = self.work_queue.write().unwrap();
        let estimated_work = item.estimated_work;
        queue.push(item);

        // Sort by priority (ascending)
        queue.sort_by_key(|w| w.priority);

        self.total_work
            .fetch_add(estimated_work as u64, Ordering::Relaxed);
    }

    /// Submit multiple work items
    pub fn submit_batch(&self, items: Vec<WorkItem>) {
        let mut queue = self.work_queue.write().unwrap();

        for item in items {
            self.total_work
                .fetch_add(item.estimated_work as u64, Ordering::Relaxed);
            queue.push(item);
        }

        // Sort by priority (ascending)
        queue.sort_by_key(|w| w.priority);
    }

    /// Try to steal work from queue
    pub fn steal(&self) -> Option<WorkItem> {
        let mut queue = self.work_queue.write().unwrap();

        if queue.is_empty() {
            return None;
        }

        self.steals.fetch_add(1, Ordering::Relaxed);

        // Steal from front (highest priority)
        Some(queue.remove(0))
    }

    /// Record result
    pub fn complete(&self, result: LevelUpdateResult) {
        let mut results = self.results.write().unwrap();
        results.insert(result.level, result);
    }

    /// Get all results
    pub fn get_results(&self) -> HashMap<usize, LevelUpdateResult> {
        self.results.read().unwrap().clone()
    }

    /// Clear results
    pub fn clear_results(&self) {
        self.results.write().unwrap().clear();
    }

    /// Check if queue is empty
    pub fn is_empty(&self) -> bool {
        self.work_queue.read().unwrap().is_empty()
    }

    /// Get queue size
    pub fn queue_size(&self) -> usize {
        self.work_queue.read().unwrap().len()
    }

    /// Get total steals
    pub fn steal_count(&self) -> u64 {
        self.steals.load(Ordering::Relaxed)
    }
}

impl Default for WorkStealingScheduler {
    fn default() -> Self {
        Self::new()
    }
}

/// Parallel level updater using Rayon
pub struct ParallelLevelUpdater {
    config: ParallelConfig,
    /// Scheduler for work-stealing
    scheduler: Arc<WorkStealingScheduler>,
    /// Global minimum cut found
    global_min: AtomicU64,
    /// Level with global minimum
    best_level: AtomicUsize,
}

impl ParallelLevelUpdater {
    /// Create new parallel updater with default config
    pub fn new() -> Self {
        Self::with_config(ParallelConfig::default())
    }

    /// Create with custom config
    pub fn with_config(config: ParallelConfig) -> Self {
        Self {
            scheduler: Arc::new(WorkStealingScheduler::with_config(config.clone())),
            config,
            global_min: AtomicU64::new(f64::INFINITY.to_bits()),
            best_level: AtomicUsize::new(usize::MAX),
        }
    }

    /// Update global minimum atomically
    pub fn try_update_min(&self, value: f64, level: usize) -> bool {
        let value_bits = value.to_bits();
        let mut current = self.global_min.load(Ordering::Acquire);

        loop {
            let current_value = f64::from_bits(current);
            if value >= current_value {
                return false;
            }

            match self.global_min.compare_exchange_weak(
                current,
                value_bits,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => {
                    self.best_level.store(level, Ordering::Release);
                    return true;
                }
                Err(c) => current = c,
            }
        }
    }

    /// Get current global minimum
    pub fn global_min(&self) -> f64 {
        f64::from_bits(self.global_min.load(Ordering::Acquire))
    }

    /// Get best level
    pub fn best_level(&self) -> Option<usize> {
        let level = self.best_level.load(Ordering::Acquire);
        if level == usize::MAX {
            None
        } else {
            Some(level)
        }
    }

    /// Reset global minimum
    pub fn reset_min(&self) {
        self.global_min
            .store(f64::INFINITY.to_bits(), Ordering::Release);
        self.best_level.store(usize::MAX, Ordering::Release);
    }

    /// Process levels in parallel using Rayon
    #[cfg(feature = "rayon")]
    pub fn process_parallel<F>(&self, levels: &[usize], mut process_fn: F) -> Vec<LevelUpdateResult>
    where
        F: FnMut(usize) -> LevelUpdateResult + Send + Sync + Clone,
    {
        let size = levels.len();

        if size < self.config.min_parallel_size {
            // Sequential processing for small workloads
            return levels
                .iter()
                .map(|&level| {
                    let result = process_fn.clone()(level);
                    self.try_update_min(result.cut_value, level);
                    result
                })
                .collect();
        }

        // Parallel processing with Rayon
        levels
            .par_iter()
            .map(|&level| {
                let result = process_fn.clone()(level);
                self.try_update_min(result.cut_value, level);
                result
            })
            .collect()
    }

    /// Process levels in parallel (scalar fallback)
    #[cfg(not(feature = "rayon"))]
    pub fn process_parallel<F>(&self, levels: &[usize], mut process_fn: F) -> Vec<LevelUpdateResult>
    where
        F: FnMut(usize) -> LevelUpdateResult + Clone,
    {
        levels
            .iter()
            .map(|&level| {
                let result = process_fn.clone()(level);
                self.try_update_min(result.cut_value, level);
                result
            })
            .collect()
    }

    /// Process work items with work-stealing
    #[cfg(feature = "rayon")]
    pub fn process_with_stealing<F>(
        &self,
        work_items: Vec<WorkItem>,
        process_fn: F,
    ) -> Vec<LevelUpdateResult>
    where
        F: Fn(&WorkItem) -> LevelUpdateResult + Send + Sync,
    {
        if work_items.len() < self.config.min_parallel_size {
            // Sequential
            return work_items
                .iter()
                .map(|item| {
                    let result = process_fn(item);
                    self.try_update_min(result.cut_value, item.level);
                    result
                })
                .collect();
        }

        // Parallel with work-stealing
        work_items
            .par_iter()
            .map(|item| {
                let result = process_fn(item);
                self.try_update_min(result.cut_value, item.level);
                result
            })
            .collect()
    }

    /// Process work items (scalar fallback)
    #[cfg(not(feature = "rayon"))]
    pub fn process_with_stealing<F>(
        &self,
        work_items: Vec<WorkItem>,
        process_fn: F,
    ) -> Vec<LevelUpdateResult>
    where
        F: Fn(&WorkItem) -> LevelUpdateResult,
    {
        work_items
            .iter()
            .map(|item| {
                let result = process_fn(item);
                self.try_update_min(result.cut_value, item.level);
                result
            })
            .collect()
    }

    /// Batch vertex processing within a level
    #[cfg(feature = "rayon")]
    pub fn process_vertices_parallel<F, R>(&self, vertices: &[VertexId], process_fn: F) -> Vec<R>
    where
        F: Fn(VertexId) -> R + Send + Sync,
        R: Send,
    {
        if vertices.len() < self.config.min_parallel_size {
            return vertices.iter().map(|&v| process_fn(v)).collect();
        }

        vertices.par_iter().map(|&v| process_fn(v)).collect()
    }

    /// Batch vertex processing (scalar fallback)
    #[cfg(not(feature = "rayon"))]
    pub fn process_vertices_parallel<F, R>(&self, vertices: &[VertexId], process_fn: F) -> Vec<R>
    where
        F: Fn(VertexId) -> R,
    {
        vertices.iter().map(|&v| process_fn(v)).collect()
    }

    /// Parallel reduction for aggregating results
    #[cfg(feature = "rayon")]
    pub fn parallel_reduce<T, F, R>(
        &self,
        items: &[T],
        identity: R,
        map_fn: F,
        reduce_fn: fn(R, R) -> R,
    ) -> R
    where
        T: Sync,
        F: Fn(&T) -> R + Send + Sync,
        R: Send + Clone,
    {
        if items.len() < self.config.min_parallel_size {
            return items
                .iter()
                .map(|item| map_fn(item))
                .fold(identity.clone(), reduce_fn);
        }

        items
            .par_iter()
            .map(|item| map_fn(item))
            .reduce(|| identity.clone(), reduce_fn)
    }

    /// Parallel reduction (scalar fallback)
    #[cfg(not(feature = "rayon"))]
    pub fn parallel_reduce<T, F, R>(
        &self,
        items: &[T],
        identity: R,
        map_fn: F,
        reduce_fn: fn(R, R) -> R,
    ) -> R
    where
        F: Fn(&T) -> R,
        R: Clone,
    {
        items
            .iter()
            .map(|item| map_fn(item))
            .fold(identity, reduce_fn)
    }

    /// Get scheduler reference
    pub fn scheduler(&self) -> &Arc<WorkStealingScheduler> {
        &self.scheduler
    }
}

impl Default for ParallelLevelUpdater {
    fn default() -> Self {
        Self::new()
    }
}

/// Parallel cut computation helpers
pub struct ParallelCutOps;

impl ParallelCutOps {
    /// Compute boundary size in parallel
    #[cfg(feature = "rayon")]
    pub fn boundary_size_parallel(
        partition: &HashSet<VertexId>,
        adjacency: &HashMap<VertexId, Vec<(VertexId, f64)>>,
    ) -> f64 {
        let partition_vec: Vec<_> = partition.iter().copied().collect();

        if partition_vec.len() < 100 {
            return Self::boundary_size_sequential(partition, adjacency);
        }

        partition_vec
            .par_iter()
            .map(|&v| {
                adjacency
                    .get(&v)
                    .map(|neighbors| {
                        neighbors
                            .iter()
                            .filter(|(n, _)| !partition.contains(n))
                            .map(|(_, w)| w)
                            .sum::<f64>()
                    })
                    .unwrap_or(0.0)
            })
            .sum()
    }

    /// Compute boundary size sequentially
    #[cfg(not(feature = "rayon"))]
    pub fn boundary_size_parallel(
        partition: &HashSet<VertexId>,
        adjacency: &HashMap<VertexId, Vec<(VertexId, f64)>>,
    ) -> f64 {
        Self::boundary_size_sequential(partition, adjacency)
    }

    /// Sequential boundary computation
    pub fn boundary_size_sequential(
        partition: &HashSet<VertexId>,
        adjacency: &HashMap<VertexId, Vec<(VertexId, f64)>>,
    ) -> f64 {
        partition
            .iter()
            .map(|&v| {
                adjacency
                    .get(&v)
                    .map(|neighbors| {
                        neighbors
                            .iter()
                            .filter(|(n, _)| !partition.contains(n))
                            .map(|(_, w)| w)
                            .sum::<f64>()
                    })
                    .unwrap_or(0.0)
            })
            .sum()
    }

    /// Find minimum degree vertex in parallel
    #[cfg(feature = "rayon")]
    pub fn min_degree_vertex_parallel(
        vertices: &[VertexId],
        adjacency: &HashMap<VertexId, Vec<(VertexId, f64)>>,
    ) -> Option<(VertexId, usize)> {
        if vertices.len() < 100 {
            return Self::min_degree_vertex_sequential(vertices, adjacency);
        }

        vertices
            .par_iter()
            .map(|&v| {
                let degree = adjacency.get(&v).map(|n| n.len()).unwrap_or(0);
                (v, degree)
            })
            .filter(|(_, d)| *d > 0)
            .min_by_key(|(_, d)| *d)
    }

    /// Find minimum degree vertex sequentially
    #[cfg(not(feature = "rayon"))]
    pub fn min_degree_vertex_parallel(
        vertices: &[VertexId],
        adjacency: &HashMap<VertexId, Vec<(VertexId, f64)>>,
    ) -> Option<(VertexId, usize)> {
        Self::min_degree_vertex_sequential(vertices, adjacency)
    }

    /// Sequential minimum degree
    pub fn min_degree_vertex_sequential(
        vertices: &[VertexId],
        adjacency: &HashMap<VertexId, Vec<(VertexId, f64)>>,
    ) -> Option<(VertexId, usize)> {
        vertices
            .iter()
            .map(|&v| {
                let degree = adjacency.get(&v).map(|n| n.len()).unwrap_or(0);
                (v, degree)
            })
            .filter(|(_, d)| *d > 0)
            .min_by_key(|(_, d)| *d)
    }
}

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

    #[test]
    fn test_work_item_submission() {
        let scheduler = WorkStealingScheduler::new();

        scheduler.submit(WorkItem {
            level: 0,
            vertices: vec![1, 2, 3],
            priority: 1,
            estimated_work: 100,
        });

        scheduler.submit(WorkItem {
            level: 1,
            vertices: vec![4, 5, 6],
            priority: 0, // Higher priority
            estimated_work: 50,
        });

        assert_eq!(scheduler.queue_size(), 2);

        // Should steal highest priority first
        let stolen = scheduler.steal().unwrap();
        assert_eq!(stolen.level, 1); // Priority 0 comes first
    }

    #[test]
    fn test_parallel_updater_min() {
        let updater = ParallelLevelUpdater::new();

        assert!(updater.global_min().is_infinite());

        assert!(updater.try_update_min(10.0, 0));
        assert_eq!(updater.global_min(), 10.0);
        assert_eq!(updater.best_level(), Some(0));

        assert!(updater.try_update_min(5.0, 1));
        assert_eq!(updater.global_min(), 5.0);
        assert_eq!(updater.best_level(), Some(1));

        // Should not update with higher value
        assert!(!updater.try_update_min(7.0, 2));
        assert_eq!(updater.global_min(), 5.0);
    }

    #[test]
    fn test_process_parallel() {
        let updater = ParallelLevelUpdater::new();

        let levels = vec![0, 1, 2, 3, 4];

        let results = updater.process_parallel(&levels, |level| LevelUpdateResult {
            level,
            cut_value: level as f64 * 2.0,
            partition: HashSet::new(),
            time_us: 0,
        });

        assert_eq!(results.len(), 5);
        assert_eq!(updater.global_min(), 0.0);
        assert_eq!(updater.best_level(), Some(0));
    }

    #[test]
    fn test_boundary_size() {
        let partition: HashSet<_> = vec![1, 2].into_iter().collect();

        let mut adjacency: HashMap<VertexId, Vec<(VertexId, f64)>> = HashMap::new();
        adjacency.insert(1, vec![(2, 1.0), (3, 2.0)]);
        adjacency.insert(2, vec![(1, 1.0), (4, 3.0)]);
        adjacency.insert(3, vec![(1, 2.0)]);
        adjacency.insert(4, vec![(2, 3.0)]);

        let boundary = ParallelCutOps::boundary_size_sequential(&partition, &adjacency);

        // Edges crossing: 1-3 (2.0) + 2-4 (3.0) = 5.0
        assert_eq!(boundary, 5.0);
    }

    #[test]
    fn test_min_degree_vertex() {
        let vertices: Vec<_> = vec![1, 2, 3, 4];

        let mut adjacency: HashMap<VertexId, Vec<(VertexId, f64)>> = HashMap::new();
        adjacency.insert(1, vec![(2, 1.0), (3, 1.0), (4, 1.0)]);
        adjacency.insert(2, vec![(1, 1.0)]);
        adjacency.insert(3, vec![(1, 1.0), (4, 1.0)]);
        adjacency.insert(4, vec![(1, 1.0), (3, 1.0)]);

        let (min_v, min_deg) =
            ParallelCutOps::min_degree_vertex_sequential(&vertices, &adjacency).unwrap();

        assert_eq!(min_v, 2);
        assert_eq!(min_deg, 1);
    }

    #[test]
    fn test_scheduler_steal_count() {
        let scheduler = WorkStealingScheduler::new();

        scheduler.submit(WorkItem {
            level: 0,
            vertices: vec![1],
            priority: 0,
            estimated_work: 10,
        });

        assert_eq!(scheduler.steal_count(), 0);
        let _ = scheduler.steal();
        assert_eq!(scheduler.steal_count(), 1);
    }

    #[test]
    fn test_batch_submit() {
        let scheduler = WorkStealingScheduler::new();

        let items = vec![
            WorkItem {
                level: 0,
                vertices: vec![],
                priority: 2,
                estimated_work: 100,
            },
            WorkItem {
                level: 1,
                vertices: vec![],
                priority: 0,
                estimated_work: 50,
            },
            WorkItem {
                level: 2,
                vertices: vec![],
                priority: 1,
                estimated_work: 75,
            },
        ];

        scheduler.submit_batch(items);

        assert_eq!(scheduler.queue_size(), 3);

        // Should be sorted by priority
        let first = scheduler.steal().unwrap();
        assert_eq!(first.level, 1); // Priority 0
    }
}