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
//! DFS (深度优先搜索) 算法插件实现

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

/// DFS traversal state
struct DfsState<'a> {
    visited: &'a mut Vec<bool>,
    order: &'a mut Vec<usize>,
    discovery_time: &'a mut Vec<usize>,
    finish_time: &'a mut Vec<usize>,
    parent: &'a mut Vec<Option<usize>>,
    time: &'a mut usize,
}

/// DFS (深度优先搜索) 算法插件
///
/// 提供基于深度优先搜索的图遍历功能,支持:
/// - 图遍历并记录访问顺序
/// - 环检测(有向图)
/// - 连通分量计算(无向图)
///
/// # 示例
///
/// ```
/// use god_graph::plugins::algorithms::dfs::DfsPlugin;
/// use god_graph::graph::Graph;
///
/// // 创建 DFS 插件,从节点 0 开始遍历
/// let plugin = DfsPlugin::new(0);
///
/// // 或者使用默认配置
/// let plugin = DfsPlugin::new_unspecified();
///
/// // 可选:不记录访问顺序
/// let plugin = DfsPlugin::new(0).with_record_order(false);
/// ```
pub struct DfsPlugin {
    start_node: usize,
    record_order: bool,
}

impl DfsPlugin {
    /// 创建新的 DFS 插件实例
    ///
    /// # 参数
    ///
    /// * `start_node` - 起始节点索引
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::dfs::DfsPlugin;
    ///
    /// let plugin = DfsPlugin::new(0);
    /// ```
    pub fn new(start_node: usize) -> Self {
        Self {
            start_node,
            record_order: true,
        }
    }

    /// 创建默认配置的 DFS 插件实例
    ///
    /// 默认从节点 0 开始遍历,并记录访问顺序。
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::dfs::DfsPlugin;
    ///
    /// let plugin = DfsPlugin::new_unspecified();
    /// ```
    pub fn new_unspecified() -> Self {
        Self {
            start_node: 0,
            record_order: true,
        }
    }

    /// 设置是否记录访问顺序
    ///
    /// # 参数
    ///
    /// * `record` - `true` 记录访问顺序,`false` 不记录
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::dfs::DfsPlugin;
    ///
    /// let plugin = DfsPlugin::new(0).with_record_order(false);
    /// ```
    pub fn with_record_order(mut self, record: bool) -> Self {
        self.record_order = record;
        self
    }

    /// 从指定节点开始遍历图
    ///
    /// 执行深度优先搜索并返回遍历结果。
    ///
    /// # 参数
    ///
    /// * `graph` - 要遍历的图
    /// * `start` - 起始节点索引
    ///
    /// # 返回
    ///
    /// 返回包含访问节点数、访问顺序、发现时间、完成时间和父节点信息的 `DfsResult`。
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::dfs::DfsPlugin;
    /// use god_graph::graph::Graph;
    ///
    /// let mut graph = Graph::<i32, f64>::directed();
    /// graph.add_node(1).unwrap();
    /// graph.add_node(2).unwrap();
    /// graph.add_edge(0, 1, 1.0).unwrap();
    ///
    /// let plugin = DfsPlugin::new(0);
    /// let result = plugin.traverse(&graph, 0).unwrap();
    ///
    /// assert!(result.visited_count >= 1);
    /// ```
    pub fn traverse<G>(&self, graph: &G, start: usize) -> VgiResult<DfsResult>
    where
        G: VirtualGraph + ?Sized,
    {
        let n = graph.node_count();
        if n == 0 {
            return Ok(DfsResult {
                visited_count: 0,
                order: vec![],
                discovery_time: vec![],
                finish_time: vec![],
                parent: vec![],
            });
        }

        // 收集所有节点索引并创建位置映射
        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
        };

        if start >= node_indices.len() {
            return Err(crate::vgi::VgiError::Internal {
                message: format!("Start node {} out of range", start),
            });
        }

        let start_pos = node_id_to_pos.get(start).copied().unwrap_or(usize::MAX);
        if start_pos == usize::MAX || start_pos >= n {
            return Err(crate::vgi::VgiError::Internal {
                message: format!("Start node {} not found", start),
            });
        }

        // 初始化 DFS 状态 (Vec instead of HashMap/HashSet)
        let mut visited: Vec<bool> = vec![false; n];
        let mut order: Vec<usize> = Vec::with_capacity(n);
        let mut discovery_time: Vec<usize> = vec![0; n];
        let mut finish_time: Vec<usize> = vec![0; n];
        let mut parent: Vec<Option<usize>> = vec![None; n];
        let mut time = 0;
        let mut state = DfsState {
            visited: &mut visited,
            order: &mut order,
            discovery_time: &mut discovery_time,
            finish_time: &mut finish_time,
            parent: &mut parent,
            time: &mut time,
        };

        self.dfs_visit(graph, start_pos, &node_id_to_pos, 0, &mut state);

        Ok(DfsResult {
            visited_count: visited.iter().filter(|&&v| v).count(),
            order,
            discovery_time,
            finish_time,
            parent,
        })
    }

    fn dfs_visit<G>(
        &self,
        graph: &G,
        node_pos: usize,
        node_id_to_pos: &Vec<usize>,
        depth: usize,
        state: &mut DfsState<'_>,
    ) where
        G: VirtualGraph + ?Sized,
    {
        if depth > graph.node_count() {
            return; // 防止无限递归
        }

        state.visited[node_pos] = true;
        *state.time += 1;
        state.discovery_time[node_pos] = *state.time;

        if self.record_order {
            state.order.push(node_pos);
        }

        // 获取邻居
        let node_indices: Vec<NodeIndex> = graph.nodes().map(|n| n.index()).collect();
        for neighbor_idx in graph.neighbors(node_indices[node_pos]) {
            let neighbor_pos = node_id_to_pos[neighbor_idx.index()];
            if neighbor_pos != usize::MAX && !state.visited[neighbor_pos] {
                state.parent[neighbor_pos] = Some(node_pos);
                self.dfs_visit(graph, neighbor_pos, node_id_to_pos, depth + 1, state);
            }
        }

        *state.time += 1;
        state.finish_time[node_pos] = *state.time;
    }

    /// 检测有向图中是否存在环
    ///
    /// 使用 DFS 遍历检测图中是否存在环(cycle)。
    ///
    /// # 参数
    ///
    /// * `graph` - 要检测的图
    ///
    /// # 返回
    ///
    /// 如果图中存在环则返回 `Ok(true)`,否则返回 `Ok(false)`。
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::dfs::DfsPlugin;
    /// use god_graph::graph::Graph;
    ///
    /// let mut graph = Graph::<i32, f64>::directed();
    /// graph.add_node(1).unwrap();
    /// graph.add_node(2).unwrap();
    /// graph.add_edge(0, 1, 1.0).unwrap();
    /// graph.add_edge(1, 0, 1.0).unwrap();
    ///
    /// let plugin = DfsPlugin::new(0);
    /// let has_cycle = plugin.has_cycle(&graph).unwrap();
    ///
    /// assert!(has_cycle);
    /// ```
    pub fn has_cycle<G>(&self, graph: &G) -> VgiResult<bool>
    where
        G: VirtualGraph + ?Sized,
    {
        let n = graph.node_count();
        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 mut visited: Vec<bool> = vec![false; n];
        let mut rec_stack: Vec<bool> = vec![false; n];

        for (pos, _idx) in node_indices.iter().enumerate() {
            if !visited[pos]
                && self.has_cycle_util(graph, pos, &node_id_to_pos, &mut visited, &mut rec_stack)?
            {
                return Ok(true);
            }
        }

        Ok(false)
    }

    fn has_cycle_util<G>(
        &self,
        graph: &G,
        node_pos: usize,
        node_id_to_pos: &Vec<usize>,
        visited: &mut Vec<bool>,
        rec_stack: &mut Vec<bool>,
    ) -> VgiResult<bool>
    where
        G: VirtualGraph + ?Sized,
    {
        visited[node_pos] = true;
        rec_stack[node_pos] = true;

        let node_indices: Vec<NodeIndex> = graph.nodes().map(|n| n.index()).collect();
        for neighbor_idx in graph.neighbors(node_indices[node_pos]) {
            let neighbor_pos = node_id_to_pos[neighbor_idx.index()];
            if neighbor_pos == usize::MAX {
                continue;
            }
            if !visited[neighbor_pos] {
                if self.has_cycle_util(graph, neighbor_pos, node_id_to_pos, visited, rec_stack)? {
                    return Ok(true);
                }
            } else if rec_stack[neighbor_pos] {
                return Ok(true);
            }
        }

        rec_stack[node_pos] = false;
        Ok(false)
    }

    /// 计算无向图的连通分量
    ///
    /// 使用 DFS 遍历找出图中所有的连通分量。
    ///
    /// # 参数
    ///
    /// * `graph` - 要计算的图
    ///
    /// # 返回
    ///
    /// 返回包含所有连通分量的向量,每个连通分量是一个节点索引向量。
    ///
    /// # 示例
    ///
    /// ```
    /// use god_graph::plugins::algorithms::dfs::DfsPlugin;
    /// use god_graph::graph::Graph;
    ///
    /// let mut graph = Graph::<i32, f64>::undirected();
    /// graph.add_node(1).unwrap();
    /// graph.add_node(2).unwrap();
    /// graph.add_node(3).unwrap();
    /// graph.add_edge(0, 1, 1.0).unwrap();
    ///
    /// let plugin = DfsPlugin::new(0);
    /// let components = plugin.connected_components(&graph).unwrap();
    ///
    /// assert_eq!(components.len(), 2);
    /// ```
    pub fn connected_components<G>(&self, graph: &G) -> VgiResult<Vec<Vec<usize>>>
    where
        G: VirtualGraph + ?Sized,
    {
        let n = graph.node_count();
        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 mut visited: Vec<bool> = vec![false; n];
        let mut components: Vec<Vec<usize>> = Vec::with_capacity(n / 4 + 1);

        for (pos, _idx) in node_indices.iter().enumerate() {
            if !visited[pos] {
                let mut component = Vec::with_capacity(n / 2 + 1);
                self.dfs_component(graph, pos, &node_id_to_pos, &mut visited, &mut component);
                components.push(component);
            }
        }

        Ok(components)
    }

    fn dfs_component<G>(
        &self,
        graph: &G,
        node_pos: usize,
        node_id_to_pos: &Vec<usize>,
        visited: &mut Vec<bool>,
        component: &mut Vec<usize>,
    ) where
        G: VirtualGraph + ?Sized,
    {
        visited[node_pos] = true;
        component.push(node_pos);

        let node_indices: Vec<NodeIndex> = graph.nodes().map(|n| n.index()).collect();
        for neighbor_idx in graph.neighbors(node_indices[node_pos]) {
            let neighbor_pos = node_id_to_pos[neighbor_idx.index()];
            if neighbor_pos != usize::MAX && !visited[neighbor_pos] {
                self.dfs_component(graph, neighbor_pos, node_id_to_pos, visited, component);
            }
        }
    }
}

/// DFS 遍历结果
///
/// 包含深度优先搜索的完整结果信息。
///
/// # 字段
///
/// * `visited_count` - 访问的节点总数
/// * `order` - 节点访问顺序(节点索引列表)
/// * `discovery_time` - 每个节点的发现时间 (position-based)
/// * `finish_time` - 每个节点的完成时间 (position-based)
/// * `parent` - 每个节点在 DFS 树中的父节点 (position-based)
///
/// # 示例
///
/// ```
/// use god_graph::plugins::algorithms::dfs::DfsResult;
///
/// // DfsResult 通常由 DfsPlugin::traverse() 返回
/// // 包含完整的 DFS 遍历信息
/// ```
#[derive(Debug, Clone)]
pub struct DfsResult {
    /// 访问的节点总数
    pub visited_count: usize,
    /// 节点访问顺序(节点索引列表)
    pub order: Vec<usize>,
    /// 每个节点的发现时间 (position-based)
    pub discovery_time: Vec<usize>,
    /// 每个节点的完成时间 (position-based)
    pub finish_time: Vec<usize>,
    /// 每个节点在 DFS 树中的父节点 (position-based)
    pub parent: Vec<Option<usize>>,
}

impl GraphAlgorithm for DfsPlugin {
    fn info(&self) -> PluginInfo {
        PluginInfo::new("dfs", "1.0.0", "深度优先搜索算法")
            .with_author("God-Graph Team")
            .with_required_capabilities(&[Capability::IncrementalUpdate])
            .with_supported_graph_types(&[GraphType::Directed, GraphType::Undirected])
            .with_tags(&["traversal", "search", "cycle-detection"])
    }

    fn execute<G>(&self, ctx: &mut PluginContext<G>) -> VgiResult<AlgorithmResult>
    where
        G: VirtualGraph + ?Sized,
    {
        let start = self.start_node;

        if start >= ctx.graph.node_count() {
            return Err(crate::vgi::VgiError::Internal {
                message: format!("Start node {} out of range", start),
            });
        }

        ctx.report_progress(0.1);
        let result = self.traverse(ctx.graph, start)?;
        ctx.report_progress(1.0);

        Ok(
            AlgorithmResult::new("dfs", AlgorithmData::NodeList(result.order))
                .with_metadata("start_node", start.to_string())
                .with_metadata("visited_count", result.visited_count.to_string()),
        )
    }

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

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

    #[test]
    fn test_dfs_basic() {
        let mut graph = Graph::<String, f64>::directed();
        graph.add_node("node_0".to_string()).unwrap();
        graph.add_node("node_1".to_string()).unwrap();
        graph.add_node("node_2".to_string()).unwrap();
        graph.add_node("node_3".to_string()).unwrap();

        let plugin = DfsPlugin::new(0);
        let result = plugin.traverse(&graph, 0).unwrap();

        assert!(result.visited_count >= 1);
        assert!(!result.order.is_empty());
    }

    #[test]
    fn test_dfs_cycle_detection() {
        let mut graph_with_cycle = Graph::<i32, f64>::directed();
        graph_with_cycle.add_node(1).unwrap();
        graph_with_cycle.add_node(2).unwrap();
        graph_with_cycle.add_node(3).unwrap();
        // 注意:需要正确的边设置来形成环

        let plugin = DfsPlugin::new(0);
        // 简单测试
        let result = plugin.has_cycle(&graph_with_cycle);
        assert!(result.is_ok());
    }

    #[test]
    fn test_dfs_plugin_info() {
        let plugin = DfsPlugin::new(0);
        let info = plugin.info();

        assert_eq!(info.name, "dfs");
        assert!(info.tags.contains(&"traversal".to_string()));
    }
}