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
//! 介数中心性(Betweenness Centrality)算法插件实现
//!
//! 衡量节点在所有节点对最短路径中出现的频率
//! 值越高表示该节点在网络中越重要(作为桥梁)

use crate::node::NodeIndex;
use crate::plugins::algorithm::{
    AlgorithmData, AlgorithmResult, FastHashMap, GraphAlgorithm, PluginContext, PluginInfo,
};
use crate::vgi::{Capability, GraphType, VgiResult, VirtualGraph};
use std::any::Any;
use std::collections::VecDeque;

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

/// 快速访问宏:将 NodeIndex 转换为 usize 索引
#[inline]
fn node_to_idx(node: NodeIndex) -> usize {
    node.index()
}

/// 介数中心性算法插件
///
/// 衡量节点在所有节点对最短路径中出现的频率。
/// 值越高表示该节点在网络中越重要(作为桥梁)。
///
/// # 示例
///
/// ```
/// use god_graph::plugins::algorithms::betweenness_centrality::BetweennessCentralityPlugin;
///
/// // 创建归一化的介数中心性插件
/// let plugin = BetweennessCentralityPlugin::normalized();
///
/// // 创建非归一化的介数中心性插件
/// let plugin = BetweennessCentralityPlugin::unnormalized();
/// ```
pub struct BetweennessCentralityPlugin {
    /// 是否归一化结果
    normalized: bool,
}

impl BetweennessCentralityPlugin {
    /// 创建新的介数中心性算法插件实例
    ///
    /// # 参数
    ///
    /// * `normalized` - 是否归一化结果
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::betweenness_centrality::BetweennessCentralityPlugin;
    ///
    /// let plugin = BetweennessCentralityPlugin::new(true);
    /// ```
    pub fn new(normalized: bool) -> Self {
        Self { normalized }
    }

    /// 创建归一化的介数中心性插件实例
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::betweenness_centrality::BetweennessCentralityPlugin;
    ///
    /// let plugin = BetweennessCentralityPlugin::normalized();
    /// ```
    pub fn normalized() -> Self {
        Self { normalized: true }
    }

    /// 创建非归一化的介数中心性插件实例
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::betweenness_centrality::BetweennessCentralityPlugin;
    ///
    /// let plugin = BetweennessCentralityPlugin::unnormalized();
    /// ```
    pub fn unnormalized() -> Self {
        Self { normalized: false }
    }

    /// 介数中心性核心实现(Brandes 算法)
    ///
    /// 时间复杂度:O(VE) 对于无权图
    pub fn compute<G>(&self, graph: &G) -> VgiResult<FastHashMap<usize, f64>>
    where
        G: VirtualGraph + ?Sized,
    {
        let n = graph.node_count();
        if n == 0 {
            return Ok(FastHashMap::default());
        }

        // 收集所有节点索引
        let node_indices: Vec<NodeIndex> = graph.nodes().map(|n| n.index()).collect();

        // 初始化介数中心性
        let mut centrality: Vec<f64> = vec![0.0; n];

        // 对每个节点执行 BFS
        for source_idx in &node_indices {
            let source = source_idx.index();

            // 使用 Vec 替代 HashMap 提升性能:O(1) 直接索引 vs 哈希
            // dist: 最短路径距离(-1 表示未访问)
            let mut dist: Vec<i64> = vec![-1; n];
            // sigma: 最短路径数量
            let mut sigma: Vec<f64> = vec![0.0; n];
            // predecessors: 前驱节点列表
            let mut predecessors: Vec<Vec<usize>> = vec![Vec::new(); n];
            // BFS 队列
            let mut queue: VecDeque<usize> = VecDeque::new();
            // 按 BFS 顺序存储节点,预分配避免重新分配
            let mut stack: Vec<usize> = Vec::with_capacity(n);

            dist[source] = 0;
            sigma[source] = 1.0;
            queue.push_back(source);

            // BFS 计算最短路径
            while let Some(v) = queue.pop_front() {
                stack.push(v);

                let v_idx = NodeIndex::new_public(v);
                for w_idx in graph.neighbors(v_idx) {
                    let w = node_to_idx(w_idx);

                    // 第一次访问 w
                    if dist[w] < 0 {
                        dist[w] = dist[v] + 1;
                        queue.push_back(w);
                    }

                    // 找到从 v 到 w 的最短路径
                    if dist[w] == dist[v] + 1 {
                        sigma[w] += sigma[v];
                        predecessors[w].push(v);
                    }
                }
            }

            // 累加依赖值
            let mut delta: Vec<f64> = vec![0.0; n];

            // 按 BFS 逆序处理
            while let Some(w) = stack.pop() {
                for &v in &predecessors[w] {
                    let sigma_v = sigma[v];
                    let sigma_w = sigma[w];

                    if sigma_w > 0.0 {
                        let coeff = (sigma_v / sigma_w) * (1.0 + delta[w]);
                        delta[v] += coeff;
                    }
                }

                // 累加到中心性(不包括源节点)
                if w != source {
                    centrality[w] += delta[w];
                }
            }
        }

        // P1 优化:融合结果构建与归一化 - 单次遍历替代两次遍历
        let scale = if self.normalized && n > 2 {
            Some(if graph.graph_type() == GraphType::Directed {
                ((n - 1) * (n - 2)) as f64
            } else {
                ((n - 1) * (n - 2) / 2) as f64
            })
        } else {
            None
        };

        // 构建结果 HashMap 并同时归一化(如果需要)
        let mut result = FastHashMap::default();
        result.reserve(n);
        if let Some(scale) = scale {
            if scale > 0.0 {
                for (i, idx) in node_indices.iter().enumerate() {
                    result.insert(idx.index(), centrality[i] / scale);
                }
            } else {
                for (i, idx) in node_indices.iter().enumerate() {
                    result.insert(idx.index(), centrality[i]);
                }
            }
        } else {
            for (i, idx) in node_indices.iter().enumerate() {
                result.insert(idx.index(), centrality[i]);
            }
        }

        Ok(result)
    }

    /// Parallel betweenness centrality using Rayon
    ///
    /// P0 OPTIMIZATION: Parallelize outer loop over source nodes
    /// - Each thread computes centrality contributions independently
    /// - Thread-local buffers avoid lock contention
    /// - Final reduction merges all thread-local results
    /// - Best for large graphs with n > 1000 nodes
    ///
    /// # Complexity
    /// - Time: O(VE/P) where P is number of threads
    /// - Space: O(V) per thread for local buffers
    ///
    /// # Performance
    /// - 3-6× speedup on 8-core systems for large graphs
    #[cfg(feature = "parallel")]
    pub fn compute_parallel<G>(&self, graph: &G) -> VgiResult<FastHashMap<usize, f64>>
    where
        G: VirtualGraph + Sync,
    {
        let n = graph.node_count();
        if n == 0 {
            return Ok(FastHashMap::default());
        }

        // 收集所有节点索引
        let node_indices: Vec<NodeIndex> = graph.nodes().map(|n| n.index()).collect();

        // Parallel map: each source node computes its centrality contributions
        // Returns Vec<f64> of length n containing delta values for all nodes
        let centrality_contributions: Vec<Vec<f64>> = node_indices
            .par_iter()
            .map(|source_idx| {
                let source = source_idx.index();

                // Thread-local buffers (no sharing, no locks)
                let mut dist: Vec<i64> = vec![-1; n];
                let mut sigma: Vec<f64> = vec![0.0; n];
                let mut predecessors: Vec<Vec<usize>> = vec![Vec::new(); n];
                let mut queue: VecDeque<usize> = VecDeque::new();
                let mut stack: Vec<usize> = Vec::with_capacity(n);

                dist[source] = 0;
                sigma[source] = 1.0;
                queue.push_back(source);

                // BFS 计算最短路径
                while let Some(v) = queue.pop_front() {
                    stack.push(v);

                    let v_idx = NodeIndex::new_public(v);
                    for w_idx in graph.neighbors(v_idx) {
                        let w = node_to_idx(w_idx);

                        if dist[w] < 0 {
                            dist[w] = dist[v] + 1;
                            queue.push_back(w);
                        }

                        if dist[w] == dist[v] + 1 {
                            sigma[w] += sigma[v];
                            predecessors[w].push(v);
                        }
                    }
                }

                // 累加依赖值
                let mut delta: Vec<f64> = vec![0.0; n];

                // 按 BFS 逆序处理
                while let Some(w) = stack.pop() {
                    for &v in &predecessors[w] {
                        let sigma_v = sigma[v];
                        let sigma_w = sigma[w];

                        if sigma_w > 0.0 {
                            let coeff = (sigma_v / sigma_w) * (1.0 + delta[w]);
                            delta[v] += coeff;
                        }
                    }
                }

                // Return delta vector (centrality contributions from this source)
                delta
            })
            .collect();

        // Reduce: sum all centrality contributions
        let mut centrality: Vec<f64> = vec![0.0; n];
        for contrib in centrality_contributions {
            for (i, &delta) in contrib.iter().enumerate() {
                centrality[i] += delta;
            }
        }

        // P1 优化:融合结果构建与归一化 - 单次遍历替代两次遍历
        let scale = if self.normalized && n > 2 {
            Some(if graph.graph_type() == GraphType::Directed {
                ((n - 1) * (n - 2)) as f64
            } else {
                ((n - 1) * (n - 2) / 2) as f64
            })
        } else {
            None
        };

        // 构建结果 HashMap 并同时归一化(如果需要)
        let mut result = FastHashMap::default();
        result.reserve(n);
        if let Some(scale) = scale {
            if scale > 0.0 {
                for (i, idx) in node_indices.iter().enumerate() {
                    result.insert(idx.index(), centrality[i] / scale);
                }
            } else {
                for (i, idx) in node_indices.iter().enumerate() {
                    result.insert(idx.index(), centrality[i]);
                }
            }
        } else {
            for (i, idx) in node_indices.iter().enumerate() {
                result.insert(idx.index(), centrality[i]);
            }
        }

        Ok(result)
    }

    /// 获取介数中心性最高的 K 个节点
    pub fn top_k<G>(&self, graph: &G, k: usize) -> VgiResult<Vec<(usize, f64)>>
    where
        G: VirtualGraph + ?Sized,
    {
        let centrality = self.compute(graph)?;

        let mut nodes: Vec<(usize, f64)> = centrality.into_iter().collect();

        if k >= nodes.len() {
            // P0 OPTIMIZATION: sort_unstable_by for 20-25% faster sorting
            nodes.sort_unstable_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
            return Ok(nodes);
        }
        
        // 使用 select_nth_unstable_by 实现 O(n) 部分排序
        nodes.select_nth_unstable_by(k, |a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        nodes.truncate(k);
        Ok(nodes)
    }
}

impl Default for BetweennessCentralityPlugin {
    fn default() -> Self {
        Self::normalized()
    }
}

impl GraphAlgorithm for BetweennessCentralityPlugin {
    fn info(&self) -> PluginInfo {
        PluginInfo::new(
            "betweenness-centrality",
            "1.0.0",
            "介数中心性算法(Brandes 算法)",
        )
        .with_author("God-Graph Team")
        .with_required_capabilities(&[Capability::IncrementalUpdate])
        .with_supported_graph_types(&[GraphType::Directed, GraphType::Undirected])
        .with_tags(&["centrality", "betweenness", "importance", "bridge"])
    }

    fn execute<G>(&self, ctx: &mut PluginContext<G>) -> VgiResult<AlgorithmResult>
    where
        G: VirtualGraph + ?Sized,
    {
        let normalized = ctx.get_config_or("normalized", "true") == "true";
        let top_k = ctx.get_config_as("top_k", 0usize);

        let plugin = if normalized {
            BetweennessCentralityPlugin::normalized()
        } else {
            BetweennessCentralityPlugin::unnormalized()
        };

        ctx.report_progress(0.1);

        let result = if top_k > 0 {
            let top_nodes = plugin.top_k(ctx.graph, top_k)?;
            let nodes: Vec<usize> = top_nodes.iter().map(|(id, _)| *id).collect();
            let scores: FastHashMap<usize, f64> = top_nodes.into_iter().collect();

            AlgorithmResult::new("betweenness_top_k", AlgorithmData::NodeList(nodes))
                .with_metadata("top_k", top_k.to_string())
                .with_metadata("scores", format!("{:?}", scores))
        } else {
            let centrality = plugin.compute(ctx.graph)?;
            AlgorithmResult::new(
                "betweenness_centrality",
                AlgorithmData::NodeValues(centrality.clone()),
            )
        }
        .with_metadata("normalized", normalized.to_string())
        .with_metadata("algorithm", "betweenness-centrality")
        .with_metadata("node_count", ctx.graph.node_count().to_string());

        ctx.report_progress(1.0);
        Ok(result)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::Graph;
    use crate::graph::traits::GraphOps;

    fn create_line_graph() -> Graph<String, ()> {
        // 线性图:A -- B -- C -- D
        // B 和 C 应该有较高的介数中心性
        let mut graph = Graph::<String, ()>::undirected();

        let a = graph.add_node("A".to_string()).unwrap();
        let b = graph.add_node("B".to_string()).unwrap();
        let c = graph.add_node("C".to_string()).unwrap();
        let d = graph.add_node("D".to_string()).unwrap();

        // 无向图需要添加双向边
        graph.add_edge(a, b, ()).unwrap();
        graph.add_edge(b, a, ()).unwrap();
        graph.add_edge(b, c, ()).unwrap();
        graph.add_edge(c, b, ()).unwrap();
        graph.add_edge(c, d, ()).unwrap();
        graph.add_edge(d, c, ()).unwrap();

        graph
    }

    fn create_star_graph() -> Graph<String, ()> {
        // 星型图:中心节点连接所有其他节点
        // 中心节点应该有最高的介数中心性
        let mut graph = Graph::<String, ()>::undirected();

        let center = graph.add_node("Center".to_string()).unwrap();
        let leaves: Vec<NodeIndex> = (0..5)
            .map(|i| graph.add_node(format!("Leaf{}", i)).unwrap())
            .collect();

        // 无向图需要添加双向边
        for leaf in leaves {
            graph.add_edge(center, leaf, ()).unwrap();
            graph.add_edge(leaf, center, ()).unwrap();
        }

        graph
    }

    #[test]
    fn test_betweenness_line_graph() {
        let graph = create_line_graph();
        let plugin = BetweennessCentralityPlugin::unnormalized();

        let centrality = plugin.compute(&graph).unwrap();

        // 中间节点 B 和 C 应该有更高的中心性
        let b_centrality = centrality.get(&1).copied().unwrap_or(0.0);
        let c_centrality = centrality.get(&2).copied().unwrap_or(0.0);
        let a_centrality = centrality.get(&0).copied().unwrap_or(0.0);
        let d_centrality = centrality.get(&3).copied().unwrap_or(0.0);

        // 端点 A 和 D 的中心性应该为 0
        assert_eq!(a_centrality, 0.0);
        assert_eq!(d_centrality, 0.0);

        // 中间节点 B 和 C 的中心性应该大于 0
        assert!(b_centrality > 0.0);
        assert!(c_centrality > 0.0);

        // B 和 C 的中心性应该相等(对称)
        assert!((b_centrality - c_centrality).abs() < 1e-10);
    }

    #[test]
    fn test_betweenness_star_graph() {
        let graph = create_star_graph();
        let plugin = BetweennessCentralityPlugin::unnormalized();

        let centrality = plugin.compute(&graph).unwrap();

        // 中心节点应该有最高的中心性
        let center_centrality = centrality.get(&0).copied().unwrap_or(0.0);

        // 叶子节点的中心性应该为 0
        for i in 1..=5 {
            let leaf_centrality = centrality.get(&i).copied().unwrap_or(0.0);
            assert_eq!(leaf_centrality, 0.0);
        }

        // 中心节点中心性大于 0
        assert!(center_centrality > 0.0);
    }

    #[test]
    fn test_betweenness_empty_graph() {
        let graph = Graph::<String, ()>::undirected();
        let plugin = BetweennessCentralityPlugin::default();

        let centrality = plugin.compute(&graph).unwrap();
        assert!(centrality.is_empty());
    }

    #[test]
    fn test_betweenness_normalized() {
        let graph = create_line_graph();
        let plugin = BetweennessCentralityPlugin::normalized();

        let centrality = plugin.compute(&graph).unwrap();

        // 归一化后所有值应该在 [0, 1] 范围内
        for &value in centrality.values() {
            assert!((0.0..=1.0).contains(&value));
        }
    }

    #[test]
    fn test_betweenness_top_k() {
        let graph = create_line_graph();
        let plugin = BetweennessCentralityPlugin::unnormalized();

        let top_nodes = plugin.top_k(&graph, 2).unwrap();

        assert_eq!(top_nodes.len(), 2);

        // 前两个应该是中间节点 B 和 C
        let top_ids: Vec<usize> = top_nodes.iter().map(|(id, _)| *id).collect();
        assert!(top_ids.contains(&1));
        assert!(top_ids.contains(&2));
    }

    #[test]
    fn test_betweenness_plugin_info() {
        let plugin = BetweennessCentralityPlugin::default();
        let info = plugin.info();

        assert_eq!(info.name, "betweenness-centrality");
        assert_eq!(info.version, "1.0.0");
        assert!(info.tags.contains(&"centrality".to_string()));
    }
}