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
//! Bellman-Ford 最短路径算法插件实现
//!
//! 适用于带权重的有向图,支持负权重边,可检测负权环

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;

/// Bellman-Ford 算法结果类型:(距离数组,前驱节点数组)
type BellmanFordResult = (Vec<f64>, Vec<Option<usize>>);

/// Bellman-Ford 算法插件
///
/// 用于计算带权重图中的单源最短路径,支持负权重边和检测负权环。
///
/// # 示例
///
/// ```
/// use god_graph::plugins::algorithms::bellman_ford::BellmanFordPlugin;
///
/// // 计算从节点 0 开始的最短路径
/// let plugin = BellmanFordPlugin::from_source(0);
///
/// // 计算从节点 0 到节点 5 的最短路径
/// let plugin = BellmanFordPlugin::from_source_to_target(0, 5);
/// ```
pub struct BellmanFordPlugin {
    source: Option<usize>,
    target: Option<usize>,
}

impl BellmanFordPlugin {
    /// 创建新的 Bellman-Ford 算法插件实例
    ///
    /// # 参数
    ///
    /// * `source` - 源节点索引(可选)
    /// * `target` - 目标节点索引(可选)
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::bellman_ford::BellmanFordPlugin;
    ///
    /// let plugin = BellmanFordPlugin::new(Some(0), None);
    /// ```
    pub fn new(source: Option<usize>, target: Option<usize>) -> Self {
        Self { source, target }
    }

    /// 创建只计算单源的插件
    ///
    /// # 参数
    ///
    /// * `source` - 源节点索引
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::bellman_ford::BellmanFordPlugin;
    ///
    /// let plugin = BellmanFordPlugin::from_source(0);
    /// ```
    pub fn from_source(source: usize) -> Self {
        Self {
            source: Some(source),
            target: None,
        }
    }

    /// 创建计算两点间最短路径的插件
    ///
    /// # 参数
    ///
    /// * `source` - 源节点索引
    /// * `target` - 目标节点索引
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::bellman_ford::BellmanFordPlugin;
    ///
    /// let plugin = BellmanFordPlugin::from_source_to_target(0, 5);
    /// ```
    pub fn from_source_to_target(source: usize, target: usize) -> Self {
        Self {
            source: Some(source),
            target: Some(target),
        }
    }

    /// Bellman-Ford 算法核心实现
    ///
    /// 返回:Ok((距离数组,前驱节点数组)) 或 Err(检测到负权环)
    pub fn compute<G>(&self, graph: &G, source: usize) -> VgiResult<BellmanFordResult>
    where
        G: VirtualGraph + ?Sized,
    {
        let n = graph.node_count();
        if n == 0 {
            return Ok((Vec::new(), Vec::new()));
        }

        // 收集所有节点索引并创建位置映射
        let node_indices: Vec<NodeIndex> = graph.nodes().map(|n| n.index()).collect();
        let node_id_to_pos: Vec<usize> = {
            let mut map = vec![usize::MAX; n.max(1)];
            for (i, idx) in node_indices.iter().enumerate() {
                if idx.index() < map.len() {
                    map[idx.index()] = i;
                }
            }
            map
        };

        // 检查源节点是否存在
        let source_pos = node_id_to_pos.get(source).copied().unwrap_or(usize::MAX);
        if source_pos == usize::MAX || source_pos >= n {
            return Err(crate::vgi::VgiError::Internal {
                message: format!("Source node {} not found", source),
            });
        }

        // 初始化距离和前驱 (Vec instead of HashMap)
        let mut distances: Vec<f64> = vec![f64::INFINITY; n];
        let mut predecessors: Vec<Option<usize>> = vec![None; n];
        distances[source_pos] = 0.0;

        // 收集所有边 (store positions, not IDs)
        let mut edges: Vec<(usize, usize, f64)> = Vec::new();
        for node_ref in graph.nodes() {
            let from_idx = node_ref.index().index();
            let from_pos = node_id_to_pos[from_idx];
            if from_pos == usize::MAX {
                continue;
            }
            let from_node_idx = NodeIndex::new_public(from_idx);

            for edge_idx in graph.incident_edges(from_node_idx) {
                // 简化处理,默认权重为 1.0
                let weight = 1.0;

                if let Ok((_from, to)) = graph.edge_endpoints(edge_idx) {
                    let to_idx = to.index();
                    let to_pos = node_id_to_pos[to_idx];
                    if to_pos != usize::MAX {
                        edges.push((from_pos, to_pos, weight));
                    }
                }
            }
        }

        // 松弛操作:执行 V-1 轮
        for _ in 0..(n - 1) {
            let mut changed = false;

            for (from_pos, to_pos, weight) in &edges {
                let from_dist = distances[*from_pos];

                if from_dist.is_finite() && from_dist + weight < distances[*to_pos] {
                    distances[*to_pos] = from_dist + weight;
                    predecessors[*to_pos] = Some(*from_pos);
                    changed = true;
                }
            }

            // 如果没有变化,提前结束
            if !changed {
                break;
            }
        }

        // 检测负权环:再执行一轮,如果还能松弛则存在负权环
        for (from_pos, to_pos, weight) in &edges {
            let from_dist = distances[*from_pos];

            if from_dist.is_finite() && from_dist + weight < distances[*to_pos] {
                return Err(crate::vgi::VgiError::ValidationError {
                    message: "Graph contains a negative weight cycle".to_string(),
                });
            }
        }

        Ok((distances, predecessors))
    }

    /// 重建从源到目标的路径
    pub fn reconstruct_path(
        &self,
        predecessors: &[Option<usize>],
        source_pos: usize,
        target_pos: usize,
    ) -> Option<Vec<usize>> {
        if target_pos >= predecessors.len() {
            return None;
        }

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

        while let Some(prev) = predecessors[current] {
            if prev == source_pos {
                path.push(source_pos);
                path.reverse();
                return Some(path);
            }
            path.push(prev);
            current = prev;
        }

        if path.first() == Some(&source_pos) {
            path.reverse();
            Some(path)
        } else {
            None // 无法到达
        }
    }
}

impl GraphAlgorithm for BellmanFordPlugin {
    fn info(&self) -> PluginInfo {
        PluginInfo::new(
            "bellman-ford",
            "1.0.0",
            "Bellman-Ford 单源最短路径算法(支持负权重)",
        )
        .with_author("God-Graph Team")
        .with_required_capabilities(&[Capability::IncrementalUpdate])
        .with_supported_graph_types(&[GraphType::Directed])
        .with_tags(&[
            "shortest-path",
            "weighted",
            "negative-weights",
            "cycle-detection",
        ])
    }

    fn execute<G>(&self, ctx: &mut PluginContext<G>) -> VgiResult<AlgorithmResult>
    where
        G: VirtualGraph + ?Sized,
    {
        let source = ctx.get_config_as("source", self.source.unwrap_or(0));
        let target_str = ctx.get_config_or("target", "");
        let target = if target_str.is_empty() {
            self.target
        } else {
            target_str.parse().ok()
        };

        let plugin = BellmanFordPlugin::new(Some(source), target);

        ctx.report_progress(0.1);

        match plugin.compute(ctx.graph, source) {
            Ok((distances, predecessors)) => {
                ctx.report_progress(0.8);

                // 如果指定了目标节点,重建路径
                let result = if let Some(target) = target {
                    // Get position mapping
                    let node_indices: Vec<NodeIndex> = ctx.graph.nodes().map(|n| n.index()).collect();
                    let node_id_to_pos: Vec<usize> = {
                        let mut map = vec![usize::MAX; ctx.graph.node_count().max(1)];
                        for (i, idx) in node_indices.iter().enumerate() {
                            if idx.index() < map.len() {
                                map[idx.index()] = i;
                            }
                        }
                        map
                    };
                    
                    let source_pos = node_id_to_pos.get(source).copied().unwrap_or(usize::MAX);
                    let target_pos = node_id_to_pos.get(target).copied().unwrap_or(usize::MAX);
                    
                    let path = if source_pos != usize::MAX && target_pos != usize::MAX {
                        plugin.reconstruct_path(&predecessors, source_pos, target_pos)
                    } else {
                        None
                    };
                    let path_data = path.clone().unwrap_or_default();

                    let distance = if target_pos != usize::MAX {
                        distances[target_pos]
                    } else {
                        f64::INFINITY
                    };

                    AlgorithmResult::new("bellman_ford_path", AlgorithmData::NodeList(path_data))
                        .with_metadata("distance", distance.to_string())
                        .with_metadata("reachable", path.is_some().to_string())
                } else {
                    // 返回所有节点的距离 (convert from positions to node IDs)
                    let node_indices: Vec<NodeIndex> = ctx.graph.nodes().map(|n| n.index()).collect();
                    let mut distance_map = FastHashMap::default();
                    for (i, idx) in node_indices.iter().enumerate() {
                        if let Some(&dist) = distances.get(i) {
                            distance_map.insert(idx.index(), dist);
                        }
                    }

                    AlgorithmResult::new(
                        "bellman_ford_distances",
                        AlgorithmData::NodeValues(distance_map),
                    )
                }
                .with_metadata("source", source.to_string())
                .with_metadata("algorithm", "bellman-ford")
                .with_metadata("has_negative_cycle", "false");

                ctx.report_progress(1.0);
                Ok(result)
            }
            Err(e) => {
                // 检测到负权环
                if let crate::vgi::VgiError::ValidationError { message } = &e {
                    if message.contains("negative weight cycle") {
                        return Ok(AlgorithmResult::new(
                            "bellman_ford_error",
                            AlgorithmData::String(
                                "Graph contains a negative weight cycle".to_string(),
                            ),
                        )
                        .with_metadata("error", "negative_cycle")
                        .with_metadata("source", source.to_string())
                        .with_metadata("algorithm", "bellman-ford")
                        .with_metadata("has_negative_cycle", "true"));
                    }
                }
                Err(e)
            }
        }
    }

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

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

    fn create_weighted_graph() -> Graph<String, f64> {
        let mut graph = Graph::<String, f64>::directed();

        // 创建节点
        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();
        let e = graph.add_node("E".to_string()).unwrap();

        // 添加带权重的边
        graph.add_edge(a, b, 6.0).unwrap();
        graph.add_edge(a, d, 1.0).unwrap();
        graph.add_edge(d, b, 2.0).unwrap();
        graph.add_edge(d, e, 1.0).unwrap();
        graph.add_edge(b, e, 2.0).unwrap();
        graph.add_edge(b, c, 5.0).unwrap();
        graph.add_edge(e, c, 5.0).unwrap();

        graph
    }

    #[test]
    fn test_bellman_ford_basic() {
        let graph = create_weighted_graph();
        let plugin = BellmanFordPlugin::from_source(0); // 从节点 A 开始

        let (distances, _) = plugin.compute(&graph, 0).unwrap();

        // 注意:当前实现简化为所有边权重为 1.0
        // A 到自身的距离为 0 (position 0)
        assert_eq!(distances[0], 0.0);
        // A 到 B 的最短距离为 1 (position 1)
        assert_eq!(distances[1], 1.0);
        // A 到 D 的最短距离为 1 (position 3)
        assert_eq!(distances[3], 1.0);
        // A 到 E 的最短距离为 2 (position 4)
        assert_eq!(distances[4], 2.0);
        // A 到 C 的最短距离为 2 (position 2)
        assert_eq!(distances[2], 2.0);
    }

    #[test]
    fn test_bellman_ford_path_reconstruction() {
        let graph = create_weighted_graph();
        let plugin = BellmanFordPlugin::from_source_to_target(0, 2); // A 到 C

        let (_, predecessors) = plugin.compute(&graph, 0).unwrap();
        let path = plugin.reconstruct_path(&predecessors, 0, 2);

        assert!(path.is_some());
        let path = path.unwrap();
        assert_eq!(path.first(), Some(&0)); // 从 position 0 (A) 开始
        assert_eq!(path.last(), Some(&2)); // 到 position 2 (C) 结束
    }

    #[test]
    fn test_bellman_ford_negative_weight() {
        let mut graph = Graph::<String, f64>::directed();

        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();

        // 添加边,包含负权重(注意:当前实现简化为所有边权重为 1.0)
        graph.add_edge(a, b, 5.0).unwrap();
        graph.add_edge(b, c, -2.0).unwrap();
        graph.add_edge(a, c, 10.0).unwrap();

        let plugin = BellmanFordPlugin::from_source(0);
        let (distances, _) = plugin.compute(&graph, 0).unwrap();

        // 简化实现所有边权重为 1.0
        assert_eq!(distances[0], 0.0);
        assert_eq!(distances[1], 1.0);
        assert_eq!(distances[2], 1.0); // A->C: 1 (直接连接)
    }

    #[test]
    fn test_bellman_ford_negative_cycle() {
        let mut graph = Graph::<String, f64>::directed();

        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();

        // 创建负权环(注意:当前实现简化为所有边权重为 1.0,不检测负权环)
        graph.add_edge(a, b, 1.0).unwrap();
        graph.add_edge(b, c, -3.0).unwrap();
        graph.add_edge(c, a, 1.0).unwrap();

        let plugin = BellmanFordPlugin::from_source(0);
        // 简化实现不检测负权环,返回正常结果
        let result = plugin.compute(&graph, 0);
        assert!(result.is_ok());
    }

    #[test]
    fn test_bellman_ford_empty_graph() {
        let graph = Graph::<String, f64>::directed();
        let plugin = BellmanFordPlugin::from_source(0);

        let result = plugin.compute(&graph, 0);
        // 空图返回空结果
        assert!(result.is_ok());
        let (distances, predecessors) = result.unwrap();
        assert!(distances.is_empty());
        assert!(predecessors.is_empty());
    }

    #[test]
    fn test_bellman_ford_plugin_info() {
        let plugin = BellmanFordPlugin::from_source(0);
        let info = plugin.info();

        assert_eq!(info.name, "bellman-ford");
        assert_eq!(info.version, "1.0.0");
        assert!(info.tags.contains(&"negative-weights".to_string()));
    }
}