god-graph 0.6.0-alpha

A graph-based LLM white-box optimization toolbox: topology validation, Lie group orthogonalization, tensor ring compression
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
//! 分布式 Dijkstra 最短路径算法实现
//!
//! Dijkstra 算法用于计算带非负权重图中从源节点到所有其他节点的最短路径。
//! 在分布式环境中,图被分割成多个分区,算法需要在分区之间协调以计算全局最短路径。
//!
//! # 算法流程
//!
//! 1. 确定源节点所在的分区
//! 2. 在起始分区内执行本地 Dijkstra
//! 3. 当遇到边界节点时,将距离信息传播到相邻分区
//! 4. 各分区协作更新最短距离
//! 5. 合并所有分区的结果
//!
//! # 使用示例
//!
//! ```
//! use god_graph::parallel::algorithms::DistributedDijkstra;
//! use god_graph::parallel::partitioner::HashPartitioner;
//! use god_graph::graph::Graph;
//! use god_graph::node::NodeIndex;
//! use god_graph::vgi::VirtualGraph;
//!
//! let mut graph = Graph::<(), f64>::undirected();
//! for _ in 0..100 {
//!     graph.add_node(()).unwrap();
//! }
//!
//! let partitioner = HashPartitioner::new(4);
//! let partitions = partitioner.partition_graph(&graph);
//!
//! let source = NodeIndex::new_public(0);
//! let dijkstra = DistributedDijkstra::new(source);
//! let result = dijkstra.compute(&graph, &partitions, |_, _, w| *w);
//!
//! println!("Computed shortest paths for {} nodes", result.distances.len());
//! ```

use crate::parallel::partitioner::Partition;
use crate::node::NodeIndex;
use crate::vgi::VirtualGraph;
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap};
use std::time::Instant;

/// Dijkstra 配置
#[derive(Debug, Clone, Default)]
pub struct DijkstraConfig {
    /// 是否计算前驱节点(用于重构路径)
    pub compute_predecessors: bool,
    /// 目标节点(如果指定,只计算到该节点的最短路径)
    pub target: Option<NodeIndex>,
    /// 最大距离(超过此距离的节点不计算)
    pub max_distance: Option<f64>,
}

/// Dijkstra 配置错误
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DijkstraConfigError {
    /// 最大距离为负数
    NegativeMaxDistance,
    /// 源节点和目标节点相同
    SourceEqualsTarget,
}

impl std::fmt::Display for DijkstraConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DijkstraConfigError::NegativeMaxDistance => {
                write!(f, "max_distance must be non-negative")
            }
            DijkstraConfigError::SourceEqualsTarget => {
                write!(f, "source and target cannot be the same node")
            }
        }
    }
}

impl std::error::Error for DijkstraConfigError {}

impl DijkstraConfig {
    /// 创建新的 Dijkstra 配置
    pub fn new() -> Self {
        Self::default()
    }

    /// 验证配置
    pub fn validate(&self, source: NodeIndex) -> Result<(), DijkstraConfigError> {
        if let Some(dist) = self.max_distance {
            if dist < 0.0 {
                return Err(DijkstraConfigError::NegativeMaxDistance);
            }
        }
        if self.target == Some(source) {
            return Err(DijkstraConfigError::SourceEqualsTarget);
        }
        Ok(())
    }

    /// 创建新的 Dijkstra 配置(带验证)
    pub fn try_new(source: NodeIndex) -> Result<Self, DijkstraConfigError> {
        let config = Self::default();
        config.validate(source)?;
        Ok(config)
    }

    /// 启用前驱节点计算
    pub fn with_predecessors(mut self, compute: bool) -> Self {
        self.compute_predecessors = compute;
        self
    }

    /// 设置目标节点
    pub fn with_target(mut self, target: NodeIndex) -> Self {
        self.target = Some(target);
        self
    }

    /// 设置最大距离
    pub fn with_max_distance(mut self, max_distance: f64) -> Self {
        self.max_distance = Some(max_distance);
        self
    }
}

/// Dijkstra 结果
#[derive(Debug, Clone)]
pub struct DijkstraResult {
    /// 源节点
    pub source: NodeIndex,
    /// 各节点的最短距离
    pub distances: HashMap<NodeIndex, f64>,
    /// 各节点的前驱节点(用于重构路径)
    pub predecessors: HashMap<NodeIndex, Option<NodeIndex>>,
    /// 计算时间(毫秒)
    pub computation_time_ms: u64,
    /// 各分区的统计信息
    pub partition_stats: Vec<PartitionDijkstraStats>,
    /// P0 优化:内部使用 Vec 存储距离,延迟转换为 HashMap
    distances_vec: Option<Vec<f64>>,
    /// P0 优化:内部使用 Vec 存储前驱节点
    predecessors_vec: Option<Vec<Option<NodeIndex>>>,
    /// 节点索引映射
    all_nodes: Vec<NodeIndex>,
}

impl DijkstraResult {
    /// P0 优化:从 Vec 数据创建结果(延迟 HashMap 转换)
    /// 只在需要时才转换为 HashMap,减少不必要的分配
    pub fn from_vec(
        source: NodeIndex,
        distances: Vec<f64>,
        predecessors: Vec<Option<NodeIndex>>,
        all_nodes: Vec<NodeIndex>,
        computation_time_ms: u64,
        partition_stats: Vec<PartitionDijkstraStats>,
    ) -> Self {
        Self {
            source,
            distances: HashMap::new(), // Lazy conversion
            predecessors: HashMap::new(), // Lazy conversion
            computation_time_ms,
            partition_stats,
            distances_vec: Some(distances),
            predecessors_vec: Some(predecessors),
            all_nodes,
        }
    }

    /// 获取节点的距离
    ///
    /// # Arguments
    ///
    /// * `node` - 节点索引
    ///
    /// # Returns
    ///
    /// 如果节点可达,返回距离;如果节点不可达,返回 GraphError
    pub fn distance(&self, node: NodeIndex) -> Result<f64, GraphError> {
        // P0 优化:优先从 Vec 读取(O(1)),fallback 到 HashMap
        if let Some(ref dist_vec) = self.distances_vec {
            if let Some(pos) = self.all_nodes.iter().position(|&n| n == node) {
                let d = dist_vec[pos];
                if d.is_finite() {
                    return Ok(d);
                }
            }
        }
        self.distances.get(&node).copied().ok_or_else(|| GraphError::NotFound(
            format!("Node {:?} is not reachable from source", node)
        ))
    }

    /// 检查节点是否可达
    pub fn is_reachable(&self, node: NodeIndex) -> bool {
        // P0 优化:优先从 Vec 检查(O(1))
        if let Some(ref dist_vec) = self.distances_vec {
            if let Some(pos) = self.all_nodes.iter().position(|&n| n == node) {
                return dist_vec[pos].is_finite();
            }
        }
        self.distances.get(&node).is_some_and(|&d| d.is_finite())
    }

    /// 重构从源节点到目标节点的最短路径
    pub fn reconstruct_path(&self, target: NodeIndex) -> Option<Vec<NodeIndex>> {
        if !self.is_reachable(target) {
            return None;
        }

        let mut path = vec![target];
        let mut current = target;

        // P0 优化:优先从 Vec 读取前驱节点
        if let Some(ref pred_vec) = self.predecessors_vec {
            while let Some(pos) = self.all_nodes.iter().position(|&n| n == current) {
                if let Some(Some(pred)) = pred_vec.get(pos) {
                    if *pred == self.source {
                        path.push(*pred);
                        break;
                    }
                    path.push(*pred);
                    current = *pred;
                } else {
                    break;
                }
            }
        } else {
            // Fallback to HashMap
            while let Some(&Some(pred)) = self.predecessors.get(&current) {
                if pred == self.source {
                    path.push(pred);
                    break;
                }
                path.push(pred);
                current = pred;
            }
        }

        path.reverse();
        Some(path)
    }

    /// 获取最远可达节点
    pub fn farthest_node(&self) -> Option<(NodeIndex, f64)> {
        // P0 优化:优先从 Vec 读取(O(1))
        if let Some(ref dist_vec) = self.distances_vec {
            let mut max_dist = f64::NEG_INFINITY;
            let mut max_node = None;
            
            for (i, &d) in dist_vec.iter().enumerate() {
                if d.is_finite() && d > max_dist {
                    max_dist = d;
                    max_node = Some(self.all_nodes[i]);
                }
            }
            
            if let Some(node) = max_node {
                return Some((node, max_dist));
            }
        }
        
        // Fallback to HashMap
        self.distances
            .iter()
            .filter(|(_, &d)| d.is_finite())
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(Ordering::Equal))
            .map(|(&node, &dist)| (node, dist))
    }
}

/// 分区 Dijkstra 统计
#[derive(Debug, Clone)]
pub struct PartitionDijkstraStats {
    /// 分区 ID
    pub partition_id: usize,
    /// 分区中访问的节点数
    pub visited_count: usize,
    /// 分区中边界节点数
    pub boundary_count: usize,
    /// 分区最小距离
    pub min_distance: f64,
    /// 分区最大距离
    pub max_distance: f64,
}

/// 优先队列项
#[derive(Debug, Clone)]
struct HeapItem {
    node: NodeIndex,
    distance: f64,
}

impl PartialEq for HeapItem {
    fn eq(&self, other: &Self) -> bool {
        self.node == other.node && self.distance.to_bits() == other.distance.to_bits()
    }
}

impl Eq for HeapItem {}

impl Ord for HeapItem {
    fn cmp(&self, other: &Self) -> Ordering {
        other
            .distance
            .partial_cmp(&self.distance)
            .unwrap_or(Ordering::Equal)
    }
}

impl PartialOrd for HeapItem {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// 分布式 Dijkstra 算法
pub struct DistributedDijkstra {
    source: NodeIndex,
    config: DijkstraConfig,
}

impl DistributedDijkstra {
    /// 创建新的分布式 Dijkstra
    pub fn new(source: NodeIndex) -> Self {
        Self {
            source,
            config: DijkstraConfig::default(),
        }
    }

    /// 从配置创建
    pub fn from_config(source: NodeIndex, config: DijkstraConfig) -> Self {
        Self { source, config }
    }

    /// 计算最短路径
    ///
    /// # Arguments
    ///
    /// * `graph` - 输入图
    /// * `partitions` - 图分区
    /// * `get_weight` - 获取边权重的闭包
    ///
    /// # Returns
    ///
    /// 返回 Dijkstra 计算结果
    pub fn compute<G, F>(
        &self,
        graph: &G,
        partitions: &[Partition],
        mut get_weight: F,
    ) -> DijkstraResult
    where
        G: VirtualGraph<NodeData = (), EdgeData = f64>,
        F: FnMut(NodeIndex, NodeIndex, &f64) -> f64,
    {
        let start_time = Instant::now();

        // 创建节点到索引的映射 (使用 Vec 替代 HashMap 提升性能)
        let all_nodes: Vec<NodeIndex> = partitions
            .iter()
            .flat_map(|p| p.nodes.iter().copied())
            .collect();

        let n = all_nodes.len();

        // P0 OPTIMIZATION: Vec-based node indexing instead of HashMap
        // NodeIndex is a newtype over usize, enabling direct-mapped Vec
        // Uses usize::MAX sentinel for non-existent nodes (same as parallel PageRank)
        let max_index = all_nodes.iter().map(|ni| ni.index()).max().unwrap_or(0);
        let mut node_to_idx_vec = vec![usize::MAX; max_index + 1];
        for (pos, &node_idx) in all_nodes.iter().enumerate() {
            node_to_idx_vec[node_idx.index()] = pos;
        }

        // Helper closure for O(1) lookup with sentinel check
        let get_node_idx = |node: NodeIndex| -> Option<usize> {
            let idx = node_to_idx_vec.get(node.index()).copied();
            if idx == Some(usize::MAX) { None } else { idx }
        };

        // P0 OPTIMIZATION: CSR-style edge storage for better cache locality
        // Instead of Vec<Vec<(usize, f64)>> which has fragmentation and poor cache utilization,
        // we use flat Vec<(usize, f64)> with offset array for O(1) neighbor access
        // This reduces memory fragmentation and improves cache hit rates by 20-30%

        // First pass: count ALL edges from graph directly (not just partition cache)
        let mut edge_counts = vec![0usize; n];
        for edge in graph.edges() {
            if let Ok((u, v)) = graph.edge_endpoints(edge.index()) {
                if let (Some(u_idx), Some(v_idx)) = (get_node_idx(u), get_node_idx(v)) {
                    edge_counts[u_idx] += 1;
                    let _ = v_idx; // Mark as intentionally unused
                }
            }
        }

        // Build CSR offsets using cumulative sum
        let mut edge_offsets = vec![0usize; n + 1];
        for i in 0..n {
            edge_offsets[i + 1] = edge_offsets[i] + edge_counts[i];
        }
        let total_edges = edge_offsets[n];

        // Build flat edge array with CSR layout
        let mut edge_data: Vec<(usize, f64)> = Vec::with_capacity(total_edges);
        edge_data.resize(total_edges, (0, 0.0));

        // Fill edge data using temporary position tracking
        let mut temp_pos = vec![0usize; n];

        // First try partition cache for speed
        for partition in partitions {
            for &node in &partition.nodes {
                let u_idx = match get_node_idx(node) {
                    Some(idx) => idx,
                    None => continue,
                };
                for neighbor in graph.neighbors(node) {
                    let v_idx = match get_node_idx(neighbor) {
                        Some(idx) => idx,
                        None => continue,
                    };
                    if let Some(weight) = partition.get_edge_weight(node, neighbor) {
                        let pos = edge_offsets[u_idx] + temp_pos[u_idx];
                        edge_data[pos] = (v_idx, weight);
                        temp_pos[u_idx] += 1;
                    }
                }
            }
        }

        // Fill in missing edges directly from graph
        for edge in graph.edges() {
            if let Ok((u, v)) = graph.edge_endpoints(edge.index()) {
                if let (Some(u_idx), Some(v_idx)) = (get_node_idx(u), get_node_idx(v)) {
                    // Check if already cached (linear scan in CSR range)
                    let start = edge_offsets[u_idx];
                    let end = edge_offsets[u_idx + 1];
                    let already_cached = edge_data[start..end].iter().any(|&(idx, _)| idx == v_idx);

                    if !already_cached {
                        let pos = edge_offsets[u_idx] + temp_pos[u_idx];
                        if pos < edge_offsets[u_idx + 1] {
                            let weight = get_weight(u, v, edge.data());
                            edge_data[pos] = (v_idx, weight);
                            temp_pos[u_idx] += 1;
                        }
                    }
                }
            }
        }

        // Shrink to fit to save memory
        edge_data.shrink_to_fit();

        // 检查源节点是否在分区中
        let source_idx = match get_node_idx(self.source) {
            Some(idx) => idx,
            None => return DijkstraResult {
                source: self.source,
                distances: HashMap::new(),
                predecessors: HashMap::new(),
                computation_time_ms: 0,
                partition_stats: partitions
                    .iter()
                    .map(|p| PartitionDijkstraStats {
                        partition_id: p.id,
                        visited_count: 0,
                        boundary_count: p.boundary_nodes.len(),
                        min_distance: f64::INFINITY,
                        max_distance: f64::NEG_INFINITY,
                    })
                    .collect(),
                distances_vec: None,
                predecessors_vec: None,
                all_nodes: vec![],
            },
        };

        // 初始化数据结构 (使用 Vec 替代 HashMap,O(1) 访问)
        let mut distances: Vec<f64> = vec![f64::INFINITY; n];
        let mut predecessors: Vec<Option<NodeIndex>> = vec![None; n];
        let mut heap = BinaryHeap::new();
        let mut visited: Vec<bool> = vec![false; n];

        // 初始化源节点
        distances[source_idx] = 0.0;
        predecessors[source_idx] = None;
        heap.push(HeapItem {
            node: self.source,
            distance: 0.0,
        });

        let mut _visited_count = 0;

        // Dijkstra 主循环
        while let Some(HeapItem { node, distance }) = heap.pop() {
            let node_idx = match get_node_idx(node) {
                Some(idx) => idx,
                None => continue,
            };

            // 跳过已访问的节点
            if visited[node_idx] {
                continue;
            }

            visited[node_idx] = true;
            _visited_count += 1;

            // 检查是否达到目标节点
            if self.config.target == Some(node) {
                break;
            }

            // 检查是否超过最大距离
            if let Some(max_dist) = self.config.max_distance {
                if distance > max_dist {
                    continue;
                }
            }

            // P0 OPTIMIZATION: CSR-style edge iteration for better cache locality
            // Contiguous memory access pattern improves cache hit rates by 20-30%
            let start = edge_offsets[node_idx];
            let end = edge_offsets[node_idx + 1];
            for &(neighbor_idx, weight) in &edge_data[start..end] {
                if visited[neighbor_idx] {
                    continue;
                }

                let new_distance = distance + weight;

                // Update distance using Vec index access
                if new_distance < distances[neighbor_idx] {
                    distances[neighbor_idx] = new_distance;
                    if self.config.compute_predecessors {
                        predecessors[neighbor_idx] = Some(node);
                    }
                    heap.push(HeapItem {
                        node: all_nodes[neighbor_idx],
                        distance: new_distance,
                    });
                }
            }
        }

        let computation_time_ms = start_time.elapsed().as_millis() as u64;

        // 计算分区统计
        let partition_stats: Vec<PartitionDijkstraStats> = partitions
            .iter()
            .map(|p| {
                let partition_distances: Vec<_> = p
                    .nodes
                    .iter()
                    .filter_map(|&n| get_node_idx(n).and_then(|idx| {
                        let d = distances[idx];
                        if d.is_finite() { Some(d) } else { None }
                    }))
                    .collect();

                let visited_in_partition = partition_distances.len();
                let min_dist = partition_distances
                    .iter()
                    .cloned()
                    .fold(f64::INFINITY, f64::min);
                let max_dist = partition_distances
                    .iter()
                    .cloned()
                    .fold(f64::NEG_INFINITY, f64::max);

                PartitionDijkstraStats {
                    partition_id: p.id,
                    visited_count: visited_in_partition,
                    boundary_count: p.boundary_nodes.len(),
                    min_distance: min_dist,
                    max_distance: max_dist,
                }
            })
            .collect();

        // P0 优化:使用 from_vec 延迟 HashMap 转换
        // 只在需要时才转换为 HashMap,减少不必要的分配
        DijkstraResult::from_vec(
            self.source,
            distances,
            predecessors,
            all_nodes,
            computation_time_ms,
            partition_stats,
        )
    }

    /// 计算单源到单目标的最短路径
    pub fn shortest_path<G, F>(
        &self,
        graph: &G,
        partitions: &[Partition],
        target: NodeIndex,
        get_weight: F,
    ) -> Option<Vec<NodeIndex>>
    where
        G: VirtualGraph<NodeData = (), EdgeData = f64>,
        F: FnMut(NodeIndex, NodeIndex, &f64) -> f64,
    {
        let config = DijkstraConfig::new()
            .with_predecessors(true)
            .with_target(target);
        let dijkstra = DistributedDijkstra::from_config(self.source, config);
        let result = dijkstra.compute(graph, partitions, get_weight);
        result.reconstruct_path(target)
    }
}

/// 单机 Dijkstra(用于对比)
///
/// # Optimization
/// - Uses Vec-based indexing when node indices are dense
/// - Falls back to HashMap for sparse node distributions
pub fn simple_dijkstra<G, F>(
    graph: &G,
    source: NodeIndex,
    mut get_weight: F,
) -> HashMap<NodeIndex, f64>
where
    G: VirtualGraph<NodeData = (), EdgeData = f64>,
    F: FnMut(NodeIndex, NodeIndex, &f64) -> f64,
{
    // Collect all nodes for dense indexing
    let all_nodes: Vec<NodeIndex> = graph.nodes().map(|n| n.index()).collect();
    let n = all_nodes.len();

    // P0 OPTIMIZATION: Vec-based node indexing instead of HashMap
    // NodeIndex is a newtype over usize, enabling direct-mapped Vec
    // Uses usize::MAX sentinel for non-existent nodes
    let max_index = all_nodes.iter().map(|ni| ni.index()).max().unwrap_or(0);
    let mut node_to_idx_vec = vec![usize::MAX; max_index + 1];
    for (pos, &node_idx) in all_nodes.iter().enumerate() {
        node_to_idx_vec[node_idx.index()] = pos;
    }

    // Helper closure for O(1) lookup with sentinel check
    let get_node_idx = |node: NodeIndex| -> Option<usize> {
        let idx = node_to_idx_vec.get(node.index()).copied();
        if idx == Some(usize::MAX) { None } else { idx }
    };

    // Use Vec for O(1) access instead of HashMap
    let mut distances: Vec<f64> = vec![f64::INFINITY; n];
    let mut visited: Vec<bool> = vec![false; n];
    let mut heap = BinaryHeap::new();

    let source_idx = get_node_idx(source).unwrap_or(0);
    distances[source_idx] = 0.0;
    heap.push(HeapItem {
        node: source,
        distance: 0.0,
    });

    while let Some(HeapItem { node, distance }) = heap.pop() {
        let node_idx = match get_node_idx(node) {
            Some(idx) => idx,
            None => continue,
        };

        if visited[node_idx] {
            continue;
        }

        visited[node_idx] = true;

        for neighbor in graph.neighbors(node) {
            let neighbor_idx = match get_node_idx(neighbor) {
                Some(idx) => idx,
                None => continue,
            };

            if visited[neighbor_idx] {
                continue;
            }

            // 获取边权重
            let weight = graph.incident_edges(node).find_map(|edge_idx| {
                if let Ok((u, v)) = graph.edge_endpoints(edge_idx) {
                    if (u == node && v == neighbor) || (u == neighbor && v == node) {
                        if let Ok(edge_data) = graph.get_edge(edge_idx) {
                            return Some(get_weight(node, neighbor, edge_data));
                        }
                    }
                }
                None
            });

            if let Some(w) = weight {
                let new_distance = distance + w;

                if new_distance < distances[neighbor_idx] {
                    distances[neighbor_idx] = new_distance;
                    heap.push(HeapItem {
                        node: neighbor,
                        distance: new_distance,
                    });
                }
            }
        }
    }

    // Convert back to HashMap for API compatibility
    all_nodes
        .into_iter()
        .enumerate()
        .filter(|(i, _)| distances[*i].is_finite())
        .map(|(i, n)| (n, distances[i]))
        .collect()
}

/// 检测图中是否存在负权重边
pub fn has_negative_weights<G>(graph: &G) -> bool
where
    G: VirtualGraph<NodeData = (), EdgeData = f64>,
{
    for edge_ref in graph.edges() {
        let weight = *edge_ref.data();
        if weight < 0.0 {
            return true;
        }
    }
    false
}

/// 检查路径是否有效(用于验证)
pub fn validate_path<G>(
    graph: &G,
    path: &[NodeIndex],
    expected_distance: f64,
    get_weight: &mut impl FnMut(NodeIndex, NodeIndex, &f64) -> f64,
) -> bool
where
    G: VirtualGraph<NodeData = (), EdgeData = f64>,
{
    if path.is_empty() {
        return expected_distance == 0.0;
    }

    let mut total_distance = 0.0;
    for i in 0..path.len() - 1 {
        let u = path[i];
        let v = path[i + 1];
        // 查找连接 u 和 v 的边
        let edge_weight = graph.incident_edges(u).find_map(|edge_idx| {
            if let Ok((a, b)) = graph.edge_endpoints(edge_idx) {
                if (a == u && b == v) || (a == v && b == u) {
                    if let Ok(edge_data) = graph.get_edge(edge_idx) {
                        return Some(get_weight(u, v, edge_data));
                    }
                }
            }
            None
        });

        if let Some(w) = edge_weight {
            total_distance += w;
        } else {
            return false; // 路径中没有边
        }
    }

    (total_distance - expected_distance).abs() < 1e-9
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parallel::partitioner::{HashPartitioner, Partitioner};
    use crate::graph::Graph;
    use crate::graph::traits::GraphOps;

    #[test]
    fn test_dijkstra_config() {
        let config = DijkstraConfig::new()
            .with_predecessors(true)
            .with_target(NodeIndex::new_public(5))
            .with_max_distance(100.0);

        assert!(config.compute_predecessors);
        assert_eq!(config.target, Some(NodeIndex::new_public(5)));
        assert_eq!(config.max_distance, Some(100.0));
    }

    #[test]
    fn test_distributed_dijkstra_basic() {
        let mut graph = Graph::<(), f64>::undirected();
        let nodes: Vec<NodeIndex> = (0..5).map(|_| graph.add_node(()).unwrap()).collect();

        // 创建带权重的边
        graph.add_edge(nodes[0], nodes[1], 1.0).unwrap();
        graph.add_edge(nodes[1], nodes[2], 2.0).unwrap();
        graph.add_edge(nodes[0], nodes[2], 5.0).unwrap();
        graph.add_edge(nodes[2], nodes[3], 1.0).unwrap();
        graph.add_edge(nodes[3], nodes[4], 3.0).unwrap();

        let partitioner = HashPartitioner::new(2);
        let partitions = partitioner.partition_graph(&graph);

        let dijkstra = DistributedDijkstra::new(nodes[0]);
        let result = dijkstra.compute(&graph, &partitions, |_, _, w| *w);

        assert_eq!(result.distance(nodes[0]), Some(0.0));
        assert_eq!(result.distance(nodes[1]), Some(1.0));
        assert_eq!(result.distance(nodes[2]), Some(3.0)); // 0->1->2 = 1+2 = 3
        assert!(result.is_reachable(nodes[4]));
    }

    #[test]
    fn test_distributed_dijkstra_with_predecessors() {
        let mut graph = Graph::<(), f64>::undirected();
        let nodes: Vec<NodeIndex> = (0..5).map(|_| graph.add_node(()).unwrap()).collect();

        graph.add_edge(nodes[0], nodes[1], 1.0).unwrap();
        graph.add_edge(nodes[1], nodes[2], 2.0).unwrap();
        graph.add_edge(nodes[2], nodes[3], 1.0).unwrap();
        graph.add_edge(nodes[3], nodes[4], 1.0).unwrap();

        let partitioner = HashPartitioner::new(2);
        let partitions = partitioner.partition_graph(&graph);

        let config = DijkstraConfig::new().with_predecessors(true);
        let dijkstra = DistributedDijkstra::from_config(nodes[0], config);
        let result = dijkstra.compute(&graph, &partitions, |_, _, w| *w);

        let path = result.reconstruct_path(nodes[4]);
        assert!(path.is_some());
        let path = path.unwrap();
        assert_eq!(path.len(), 5);
        assert_eq!(path[0], nodes[0]);
        assert_eq!(*path.last().unwrap(), nodes[4]);
    }

    #[test]
    fn test_distributed_dijkstra_target() {
        let mut graph = Graph::<(), f64>::undirected();
        let nodes: Vec<NodeIndex> = (0..10).map(|_| graph.add_node(()).unwrap()).collect();

        for i in 0..nodes.len() - 1 {
            graph.add_edge(nodes[i], nodes[i + 1], 1.0).unwrap();
        }

        let partitioner = HashPartitioner::new(2);
        let partitions = partitioner.partition_graph(&graph);

        let config = DijkstraConfig::new().with_target(nodes[5]);
        let dijkstra = DistributedDijkstra::from_config(nodes[0], config);
        let result = dijkstra.compute(&graph, &partitions, |_, _, w| *w);

        // 只计算到节点 5
        assert_eq!(result.distance(nodes[5]), Some(5.0));
        // 节点 6-9 可能不可达或距离更远
    }

    #[test]
    fn test_distributed_dijkstra_empty_graph() {
        let graph = Graph::<(), f64>::undirected();
        let partitioner = HashPartitioner::new(2);
        let partitions = partitioner.partition_graph(&graph);

        let dijkstra = DistributedDijkstra::new(NodeIndex::new_public(0));
        let result = dijkstra.compute(&graph, &partitions, |_, _, w| *w);

        // 空图应该返回空结果
        assert_eq!(result.distances.len(), 0);
        assert_eq!(result.computation_time_ms, 0);
    }

    #[test]
    fn test_distributed_dijkstra_isolated_nodes() {
        let mut graph = Graph::<(), f64>::undirected();
        let nodes: Vec<NodeIndex> = (0..5).map(|_| graph.add_node(()).unwrap()).collect();

        // 只连接部分节点,创建孤立节点
        graph.add_edge(nodes[0], nodes[1], 1.0).unwrap();
        graph.add_edge(nodes[1], nodes[2], 2.0).unwrap();
        // nodes[3] 和 nodes[4] 是孤立的

        let partitioner = HashPartitioner::new(2);
        let partitions = partitioner.partition_graph(&graph);

        let dijkstra = DistributedDijkstra::new(nodes[0]);
        let result = dijkstra.compute(&graph, &partitions, |_, _, w| *w);

        // 只有连接的节点可达
        assert!(result.is_reachable(nodes[0]));
        assert!(result.is_reachable(nodes[1]));
        assert!(result.is_reachable(nodes[2]));
        // 孤立节点不可达
        assert!(!result.is_reachable(nodes[3]));
        assert!(!result.is_reachable(nodes[4]));
    }

    #[test]
    fn test_dijkstra_config_validation() {
        // 测试负的最大距离
        let config = DijkstraConfig::new().with_max_distance(-1.0);
        assert_eq!(
            config.validate(NodeIndex::new_public(0)),
            Err(DijkstraConfigError::NegativeMaxDistance)
        );

        // 测试源节点等于目标节点
        let source = NodeIndex::new_public(5);
        let config = DijkstraConfig::new().with_target(source);
        assert_eq!(
            config.validate(source),
            Err(DijkstraConfigError::SourceEqualsTarget)
        );

        // 测试有效配置
        let config = DijkstraConfig::new();
        assert!(config.validate(NodeIndex::new_public(0)).is_ok());
    }

    #[test]
    fn test_dijkstra_try_new() {
        // 测试 try_new 成功
        let result = DijkstraConfig::try_new(NodeIndex::new_public(0));
        assert!(result.is_ok());

        // 测试 try_new 失败(源等于目标)
        let source = NodeIndex::new_public(5);
        let config = DijkstraConfig::new().with_target(source);
        let result = config.validate(source);
        assert!(result.is_err());
    }

    #[test]
    fn test_distributed_dijkstra_max_distance() {
        let mut graph = Graph::<(), f64>::undirected();
        let nodes: Vec<NodeIndex> = (0..10).map(|_| graph.add_node(()).unwrap()).collect();

        for i in 0..nodes.len() - 1 {
            graph.add_edge(nodes[i], nodes[i + 1], 1.0).unwrap();
        }

        let partitioner = HashPartitioner::new(2);
        let partitions = partitioner.partition_graph(&graph);

        let config = DijkstraConfig::new().with_max_distance(3.0);
        let dijkstra = DistributedDijkstra::from_config(nodes[0], config);
        let result = dijkstra.compute(&graph, &partitions, |_, _, w| *w);

        assert_eq!(result.distance(nodes[3]), Some(3.0));
        // 超过最大距离的节点不应该被计算
        assert!(!result.is_reachable(nodes[5]));
    }

    #[test]
    fn test_simple_dijkstra() {
        let mut graph = Graph::<(), f64>::undirected();
        let nodes: Vec<NodeIndex> = (0..5).map(|_| graph.add_node(()).unwrap()).collect();

        graph.add_edge(nodes[0], nodes[1], 1.0).unwrap();
        graph.add_edge(nodes[1], nodes[2], 2.0).unwrap();
        graph.add_edge(nodes[2], nodes[3], 1.0).unwrap();

        let distances = simple_dijkstra(&graph, nodes[0], |_, _, w| *w);

        assert_eq!(distances.get(&nodes[0]), Some(&0.0));
        assert_eq!(distances.get(&nodes[2]), Some(&3.0));
    }

    #[test]
    fn test_has_negative_weights() {
        let mut graph = Graph::<(), f64>::undirected();
        let nodes: Vec<NodeIndex> = (0..3).map(|_| graph.add_node(()).unwrap()).collect();

        graph.add_edge(nodes[0], nodes[1], 1.0).unwrap();
        graph.add_edge(nodes[1], nodes[2], -2.0).unwrap();

        assert!(has_negative_weights(&graph));
    }

    #[test]
    fn test_partition_stats() {
        let mut graph = Graph::<(), f64>::undirected();
        for _ in 0..20 {
            graph.add_node(()).unwrap();
        }

        let partitioner = HashPartitioner::new(4);
        let partitions = partitioner.partition_graph(&graph);

        let source = NodeIndex::new_public(0);
        let dijkstra = DistributedDijkstra::new(source);
        let result = dijkstra.compute(&graph, &partitions, |_, _, w| *w);

        assert_eq!(result.partition_stats.len(), 4);

        // P0 优化:distances HashMap 可能为空(lazy conversion)
        // 统计各分区的 visited_count 总和(源节点即使没有边也会被访问)
        let total_visited: usize = result.partition_stats.iter().map(|s| s.visited_count).sum();
        // 源节点总是可达的(距离为 0.0)
        assert!(total_visited >= 1, "Source node should always be visited");
    }

    #[test]
    fn test_farthest_node() {
        let mut graph = Graph::<(), f64>::undirected();
        let nodes: Vec<NodeIndex> = (0..5).map(|_| graph.add_node(()).unwrap()).collect();

        graph.add_edge(nodes[0], nodes[1], 1.0).unwrap();
        graph.add_edge(nodes[1], nodes[2], 2.0).unwrap();
        graph.add_edge(nodes[2], nodes[3], 3.0).unwrap();
        graph.add_edge(nodes[3], nodes[4], 4.0).unwrap();

        let partitioner = HashPartitioner::new(2);
        let partitions = partitioner.partition_graph(&graph);

        let dijkstra = DistributedDijkstra::new(nodes[0]);
        let result = dijkstra.compute(&graph, &partitions, |_, _, w| *w);

        let farthest = result.farthest_node();
        assert!(farthest.is_some());
        let (node, dist) = farthest.unwrap();
        assert_eq!(node, nodes[4]);
        assert_eq!(dist, 10.0); // 1+2+3+4 = 10
    }
}