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
//! VGI 核心 trait 定义 - 分层 API 设计
//!
//! 提供虚拟图接口的分层抽象,降低学习成本
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                    GraphAdvanced                            │
//! │  (可选:高级操作 - reserve, clear, update)                   │
//! ├─────────────────────────────────────────────────────────────┤
//! │                    GraphUpdate                              │
//! │  (增量更新 - add_node, add_edge, remove_node, remove_edge)  │
//! ├─────────────────────────────────────────────────────────────┤
//! │                    GraphRead                                │
//! │  (只读查询 - node_count, get_node, neighbors, etc.)         │
//! └─────────────────────────────────────────────────────────────┘
//!//!//! ┌─────────────────────────────────────────────────────────────┐
//! │                    VirtualGraph                             │
//! │  (完整 trait = GraphRead + GraphUpdate + GraphAdvanced)     │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! # 使用指南
//!
//! ## 场景 1: 只需要读取图数据
//!
//! ```rust
//! use god_graph::vgi::traits::GraphRead;
//!
//! fn analyze_graph<G: GraphRead>(graph: &G) {
//!     println!("Nodes: {}", graph.node_count());
//!     for node in graph.nodes() {
//!         println!("Node: {:?}", node.data());
//!     }
//! }
//! ```
//!
//! ## 场景 2: 需要修改图结构
//!
//! ```rust
//! use god_graph::vgi::traits::GraphUpdate;
//!
//! fn build_graph<G: GraphUpdate>(graph: &mut G) {
//!     let n1 = graph.add_node("A".to_string()).unwrap();
//!     let n2 = graph.add_node("B".to_string()).unwrap();
//!     graph.add_edge(n1, n2, 1.0).unwrap();
//! }
//! ```
//!
//! ## 场景 3: 需要完整功能
//!
//! ```rust
//! use god_graph::vgi::traits::VirtualGraph;
//!
//! fn process_graph<G: VirtualGraph>(graph: &mut G) {
//!     // 使用所有功能
//! }
//! ```

use crate::edge::{EdgeIndex, EdgeRef};
use crate::node::{NodeIndex, NodeRef};
use crate::vgi::error::{VgiError, VgiResult};
use crate::vgi::metadata::{Capability, GraphMetadata, GraphType};

// ========== Layer 1: GraphRead (只读查询) ==========

/// 图只读查询 trait
///
/// 提供基础的图查询操作,不修改图结构
///
/// # 使用场景
///
/// - 图算法分析(如 PageRank、BFS)
/// - 图属性计算(如密度、直径)
/// - 图可视化导出
///
/// # 示例
///
/// ```
/// use god_graph::vgi::traits::GraphRead;
///
/// fn print_graph_info<G: GraphRead>(graph: &G) {
///     println!("Nodes: {}", graph.node_count());
///     println!("Edges: {}", graph.edge_count());
/// }
/// ```
pub trait GraphRead {
    /// 节点数据类型
    type NodeData;
    /// 边数据类型
    type EdgeData;

    /// 获取图元数据
    ///
    /// 返回图的描述信息和能力列表
    fn metadata(&self) -> GraphMetadata;

    /// 检查是否支持特定能力
    ///
    /// 默认实现检查元数据中的能力列表
    fn has_capability(&self, capability: Capability) -> bool {
        self.metadata().supports(capability)
    }

    /// 检查是否支持所有指定能力
    ///
    /// 默认实现检查元数据中的能力列表
    fn has_capabilities(&self, capabilities: &[Capability]) -> bool {
        self.metadata().supports_all(capabilities)
    }

    // ========== 基础统计 ==========

    /// 获取节点数量
    fn node_count(&self) -> usize;

    /// 获取边数量
    fn edge_count(&self) -> usize;

    /// 检查是否为空图
    fn is_empty(&self) -> bool {
        self.node_count() == 0
    }

    /// 获取图的类型
    fn graph_type(&self) -> GraphType {
        self.metadata().graph_type
    }

    // ========== 节点查询 ==========

    /// 获取节点引用
    ///
    /// # Arguments
    ///
    /// * `index` - 节点索引
    ///
    /// # Returns
    ///
    /// 如果节点存在,返回节点引用
    fn get_node(&self, index: NodeIndex) -> VgiResult<&Self::NodeData>;

    /// 检查节点是否存在
    fn contains_node(&self, index: NodeIndex) -> bool;

    /// 获取所有节点迭代器
    fn nodes(&self) -> impl Iterator<Item = NodeRef<'_, Self::NodeData>>;

    // ========== 边查询 ==========

    /// 获取边引用
    ///
    /// # Arguments
    ///
    /// * `index` - 边索引
    ///
    /// # Returns
    ///
    /// 如果边存在,返回边引用
    fn get_edge(&self, index: EdgeIndex) -> VgiResult<&Self::EdgeData>;

    /// 获取边的端点
    ///
    /// # Arguments
    ///
    /// * `index` - 边索引
    ///
    /// # Returns
    ///
    /// 返回边的起始和目标节点索引
    fn edge_endpoints(&self, index: EdgeIndex) -> VgiResult<(NodeIndex, NodeIndex)>;

    /// 检查边是否存在
    fn contains_edge(&self, index: EdgeIndex) -> bool;

    /// 检查两个节点之间是否存在边
    fn has_edge(&self, from: NodeIndex, to: NodeIndex) -> bool;

    /// 获取所有边迭代器
    fn edges(&self) -> impl Iterator<Item = EdgeRef<'_, Self::EdgeData>>;

    // ========== 邻接查询 ==========

    /// 获取节点的出边邻居迭代器
    ///
    /// # Arguments
    ///
    /// * `node` - 节点索引
    ///
    /// # Returns
    ///
    /// 返回邻居节点索引的迭代器
    fn neighbors(&self, node: NodeIndex) -> impl Iterator<Item = NodeIndex>;

    /// 获取节点的关联边迭代器
    ///
    /// # Arguments
    ///
    /// * `node` - 节点索引
    ///
    /// # Returns
    ///
    /// 返回边索引的迭代器
    fn incident_edges(&self, node: NodeIndex) -> impl Iterator<Item = EdgeIndex>;

    /// 获取节点的出度
    fn out_degree(&self, node: NodeIndex) -> VgiResult<usize>;

    /// 获取节点的入度
    ///
    /// 默认实现对无向图返回出度
    fn in_degree(&self, node: NodeIndex) -> VgiResult<usize> {
        self.out_degree(node)
    }

    /// 获取节点的总度数
    ///
    /// 默认实现对无向图返回出度
    fn degree(&self, node: NodeIndex) -> VgiResult<usize> {
        self.out_degree(node)
    }
}

// ========== Layer 2: GraphUpdate (增量更新) ==========

/// 图增量更新 trait
///
/// 提供添加和删除节点/边的操作
///
/// # 设计原则
///
/// 1. **增量更新**: 只支持添加/删除,不支持批量替换
/// 2. **高效实现**: 避免不必要的内存重新分配
/// 3. **错误处理**: 统一的错误类型
///
/// # 使用场景
///
/// - 动态图构建
/// - 在线图更新
/// - 图编辑操作
///
/// # 示例
///
/// ```
/// use god_graph::vgi::traits::GraphUpdate;
///
/// fn add_triangle<G: GraphUpdate>(graph: &mut G, data: G::NodeData, edge_weight: G::EdgeData) {
///     let n1 = graph.add_node(data.clone()).unwrap();
///     let n2 = graph.add_node(data.clone()).unwrap();
///     let n3 = graph.add_node(data).unwrap();
///     
///     graph.add_edge(n1, n2, edge_weight.clone()).unwrap();
///     graph.add_edge(n2, n3, edge_weight.clone()).unwrap();
///     graph.add_edge(n3, n1, edge_weight).unwrap();
/// }
/// ```
pub trait GraphUpdate: GraphRead {
    /// 添加节点
    ///
    /// # Arguments
    ///
    /// * `data` - 节点数据
    ///
    /// # Returns
    ///
    /// 返回新添加节点的索引
    fn add_node(&mut self, data: Self::NodeData) -> VgiResult<NodeIndex>;

    /// 删除节点
    ///
    /// # Arguments
    ///
    /// * `index` - 节点索引
    ///
    /// # Returns
    ///
    /// 返回被删除节点的数据
    fn remove_node(&mut self, index: NodeIndex) -> VgiResult<Self::NodeData>;

    /// 添加边
    ///
    /// # Arguments
    ///
    /// * `from` - 起始节点索引
    /// * `to` - 目标节点索引
    /// * `data` - 边数据
    ///
    /// # Returns
    ///
    /// 返回新添加边的索引
    fn add_edge(
        &mut self,
        from: NodeIndex,
        to: NodeIndex,
        data: Self::EdgeData,
    ) -> VgiResult<EdgeIndex>;

    /// 删除边
    ///
    /// # Arguments
    ///
    /// * `index` - 边索引
    ///
    /// # Returns
    ///
    /// 返回被删除边的数据
    fn remove_edge(&mut self, index: EdgeIndex) -> VgiResult<Self::EdgeData>;
}

// ========== Layer 3: GraphAdvanced (高级操作) ==========

/// 图高级操作 trait
///
/// 提供批量操作、原地更新等高级功能
///
/// # 设计原则
///
/// 1. **可选功能**: 不是所有后端都需要实现
/// 2. **性能优化**: 提供比基础操作更高效的实现
/// 3. **向后兼容**: 保留给需要高级功能的用户
///
/// # 使用场景
///
/// - 大规模图预分配
/// - 原地节点/边数据更新
/// - 图清理操作
///
/// # 示例
///
/// ```
/// use god_graph::vgi::traits::GraphAdvanced;
///
/// fn optimize_graph<G: GraphAdvanced>(graph: &mut G) {
///     // 预分配容量
///     graph.reserve(10000, 50000);
///     
///     // 原地更新节点数据
///     let node = graph.get_node_mut(0.into()).unwrap();
///     *node = "updated".to_string();
/// }
/// ```
pub trait GraphAdvanced: GraphRead {
    /// 获取节点可变引用
    ///
    /// # Arguments
    ///
    /// * `index` - 节点索引
    ///
    /// # Returns
    ///
    /// 如果节点存在,返回节点可变引用
    fn get_node_mut(&mut self, index: NodeIndex) -> VgiResult<&mut Self::NodeData>;

    /// 预分配容量
    ///
    /// # Arguments
    ///
    /// * `additional_nodes` - 额外节点容量
    /// * `additional_edges` - 额外边容量
    fn reserve(&mut self, additional_nodes: usize, additional_edges: usize);

    /// 清空图
    fn clear(&mut self);

    /// 更新节点数据(高效实现)
    ///
    /// # Arguments
    ///
    /// * `index` - 节点索引
    /// * `data` - 新节点数据
    ///
    /// # Returns
    ///
    /// 返回旧的节点数据
    ///
    /// # Note
    ///
    /// 默认实现使用 `get_node_mut()`,高效且 O(1)。
    /// 子类可以重写以提供更优化的实现。
    fn update_node(&mut self, index: NodeIndex, data: Self::NodeData) -> VgiResult<Self::NodeData> {
        let node = self.get_node_mut(index)?;
        Ok(std::mem::replace(node, data))
    }

    /// 更新边数据(高效实现)
    ///
    /// # Arguments
    ///
    /// * `index` - 边索引
    /// * `data` - 新边数据
    ///
    /// # Returns
    ///
    /// 返回旧的边数据
    ///
    /// # Note
    ///
    /// 默认实现先获取端点再删除添加(O(n))。
    /// 子类可以重写以提供 O(1) 实现。
    fn update_edge(&mut self, index: EdgeIndex, data: Self::EdgeData) -> VgiResult<Self::EdgeData> {
        // 默认实现:先删除再添加(低效,后端可以重写)
        if !self.contains_edge(index) {
            return Err(VgiError::Internal {
                message: format!("Edge {:?} not found", index),
            });
        }
        let old_data = self.remove_edge(index)?;
        let (from, to) = self.edge_endpoints(index)?;
        self.add_edge(from, to, data)?;
        Ok(old_data)
    }
}

// ========== VirtualGraph: 完整 trait ==========

/// 虚拟图核心 trait
///
/// 提供统一的图操作接口,屏蔽底层后端实现细节
///
/// # Trait Hierarchy
///
/// `VirtualGraph` = `GraphRead` + `GraphUpdate` + `GraphAdvanced`
///
/// # 设计原则
///
/// 1. **最小接口**: 只定义必要的核心操作
/// 2. **泛型设计**: 支持任意节点和边数据类型
/// 3. **错误处理**: 统一的错误类型,便于上层处理
/// 4. **能力查询**: 支持运行时查询后端能力
///
/// # 使用示例
///
/// ```
/// use god_graph::vgi::{VirtualGraph, GraphMetadata, GraphType};
/// use god_graph::graph::Graph;
///
/// // Graph<T, E> 实现 VirtualGraph
/// let mut graph = Graph::<String, f64>::directed();
///
/// // 查询元数据
/// let metadata = graph.metadata();
/// assert_eq!(metadata.graph_type, GraphType::Directed);
///
/// // 检查能力
/// assert!(graph.has_capability(Capability::IncrementalUpdate));
/// ```
pub trait VirtualGraph: GraphRead + GraphUpdate + GraphAdvanced {}

// 为所有实现 GraphRead + GraphUpdate + GraphAdvanced 的类型自动实现 VirtualGraph
impl<T> VirtualGraph for T where T: GraphRead + GraphUpdate + GraphAdvanced {}

// ========== 向后兼容的默认实现 ==========

// 为 GraphRead 提供默认的 update_node/update_edge 实现(低效但兼容)
impl<T> GraphAdvanced for T
where
    T: GraphRead + GraphUpdate,
{
    fn get_node_mut(&mut self, index: NodeIndex) -> VgiResult<&mut Self::NodeData> {
        // 默认实现:通过 remove + add 模拟(低效)
        if !self.contains_node(index) {
            return Err(VgiError::Internal {
                message: format!("Node {:?} not found", index),
            });
        }
        // 注意:这个实现效率低下,实现 GraphUpdate 的类型应该重写此方法
        // 这里只是为了编译通过,实际使用需要单独实现 GraphAdvanced
        unimplemented!("get_node_mut requires direct mutable access, which this default implementation cannot provide. Please implement GraphAdvanced explicitly.")
    }

    fn reserve(&mut self, _additional_nodes: usize, _additional_edges: usize) {
        // 默认空实现
    }

    fn clear(&mut self) {
        // 默认实现:逐个删除节点
        let nodes: Vec<_> = self.nodes().map(|n| n.index()).collect();
        for node in nodes {
            let _ = self.remove_node(node);
        }
    }
}

// ========== Backend Trait ==========

/// 图后端 trait
///
/// 定义图后端的基本操作,包括初始化、配置、生命周期管理等。
///
/// **注意**: `Backend` trait 继承自 `VirtualGraph`,实现 `Backend` 的类型
/// 自动支持所有 `VirtualGraph` 的图操作方法。
///
/// # 设计原则
///
/// 1. **统一接口**: Backend 继承 VirtualGraph,避免双重 API
/// 2. **可插拔**: 支持动态切换后端
/// 3. **能力发现**: 支持查询后端支持的能力
///
/// # 生命周期
///
/// ```text
/// create() → configure() → initialize() → ready
//////                              execute()
//////                              shutdown() → drop
/// ```
///
/// # 示例
///
/// ```
/// use god_graph::vgi::{Backend, BackendConfig, BackendType, VirtualGraph, GraphType};
/// use god_graph::backend::single_machine::SingleMachineBackend;
///
/// let mut backend: SingleMachineBackend<String, f64> = SingleMachineBackend::default();
/// let config = BackendConfig::new(GraphType::Directed);
/// backend.initialize(config).unwrap();
///
/// // Backend 继承 VirtualGraph 所有方法
/// let n1 = backend.add_node("A".to_string()).unwrap();
/// assert!(backend.has_capability(Capability::IncrementalUpdate));
/// ```
pub trait Backend: VirtualGraph + Send + Sync {
    /// 获取后端名称
    fn name(&self) -> &'static str;

    /// 获取后端版本
    fn version(&self) -> &'static str;

    /// 获取后端元数据
    ///
    /// 注意:此方法返回后端本身的元数据(名称、版本、类型),
    /// 与 `VirtualGraph::metadata()` 返回的图元数据不同。
    fn backend_metadata(&self) -> GraphMetadata;

    /// 获取后端类型
    fn backend_type(&self) -> BackendType;

    /// 检查后端是否已初始化
    fn is_initialized(&self) -> bool;

    /// 检查后端是否健康
    fn is_healthy(&self) -> bool;

    /// 初始化后端
    ///
    /// # Arguments
    ///
    /// * `config` - 后端配置
    ///
    /// # Returns
    ///
    /// 初始化成功返回 Ok,失败返回错误
    fn initialize(&mut self, config: BackendConfig) -> VgiResult<()>;

    /// 关闭后端
    ///
    /// 清理资源,释放内存
    fn shutdown(&mut self) -> VgiResult<()>;
}

/// 后端类型枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BackendType {
    /// 单机后端
    SingleMachine,
    /// 分布式后端
    Distributed,
    /// 外部数据库后端
    ExternalDatabase,
    /// 内存后端
    InMemory,
    /// 持久化后端
    Persistent,
    /// 混合后端
    Hybrid,
}

impl std::fmt::Display for BackendType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BackendType::SingleMachine => write!(f, "SingleMachine"),
            BackendType::Distributed => write!(f, "Distributed"),
            BackendType::ExternalDatabase => write!(f, "ExternalDatabase"),
            BackendType::InMemory => write!(f, "InMemory"),
            BackendType::Persistent => write!(f, "Persistent"),
            BackendType::Hybrid => write!(f, "Hybrid"),
        }
    }
}

/// 后端配置
///
/// 用于配置后端的行为
#[derive(Debug, Clone)]
pub struct BackendConfig {
    /// 图类型
    pub graph_type: GraphType,
    /// 初始节点容量
    pub initial_node_capacity: usize,
    /// 初始边容量
    pub initial_edge_capacity: usize,
    /// 是否启用并行
    pub enable_parallel: bool,
    /// 并行线程数
    pub parallel_threads: Option<usize>,
    /// 自定义配置项
    pub custom: std::collections::HashMap<String, String>,
}

impl Default for BackendConfig {
    fn default() -> Self {
        Self {
            graph_type: GraphType::Directed,
            initial_node_capacity: 1024,
            initial_edge_capacity: 4096,
            enable_parallel: false,
            parallel_threads: None,
            custom: std::collections::HashMap::new(),
        }
    }
}

impl BackendConfig {
    /// 创建新的配置
    pub fn new(graph_type: GraphType) -> Self {
        Self {
            graph_type,
            ..Default::default()
        }
    }

    /// 设置节点容量
    pub fn with_node_capacity(mut self, capacity: usize) -> Self {
        self.initial_node_capacity = capacity;
        self
    }

    /// 设置边容量
    pub fn with_edge_capacity(mut self, capacity: usize) -> Self {
        self.initial_edge_capacity = capacity;
        self
    }

    /// 启用并行
    pub fn with_parallel(mut self, threads: Option<usize>) -> Self {
        self.enable_parallel = true;
        self.parallel_threads = threads;
        self
    }

    /// 添加自定义配置
    pub fn with_custom(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.custom.insert(key.into(), value.into());
        self
    }

    /// 获取自定义配置值
    pub fn get_custom(&self, key: &str) -> Option<&String> {
        self.custom.get(key)
    }
}

/// 后端构建器 trait
///
/// 用于构建和配置后端
pub trait BackendBuilder: Sized {
    /// 构建的后端类型
    type Backend: Backend;

    /// 创建新的构建器
    fn new() -> Self;

    /// 设置配置
    fn with_config(self, config: BackendConfig) -> Self;

    /// 构建后端
    fn build(self) -> VgiResult<Self::Backend>;
}

/// 图构建器 trait
///
/// 提供图的构建和初始化操作
pub trait GraphBuilder {
    /// 节点数据类型
    type NodeData;
    /// 边数据类型
    type EdgeData;
    /// 构建的图类型
    type Graph: VirtualGraph<NodeData = Self::NodeData, EdgeData = Self::EdgeData>;

    /// 创建有向图
    fn directed() -> Self::Graph;

    /// 创建无向图
    fn undirected() -> Self::Graph;

    /// 创建指定类型的图
    fn with_type(graph_type: GraphType) -> Self::Graph;

    /// 创建带容量的图
    fn with_capacity(nodes: usize, edges: usize) -> Self::Graph;

    /// 创建带元数据的图
    fn with_metadata(metadata: GraphMetadata) -> Self::Graph;
}

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

    #[test]
    fn test_graph_type_display() {
        assert_eq!(GraphType::Directed.to_string(), "Directed");
        assert_eq!(GraphType::Undirected.to_string(), "Undirected");
        assert_eq!(GraphType::Mixed.to_string(), "Mixed");
    }

    #[test]
    fn test_graph_read() {
        let mut graph = Graph::<String, f64>::directed();
        let n1 = graph.add_node("A".to_string()).unwrap();
        let n2 = graph.add_node("B".to_string()).unwrap();
        graph.add_edge(n1, n2, 1.0).unwrap();

        // 测试 GraphRead trait
        assert_eq!(graph.node_count(), 2);
        assert_eq!(graph.edge_count(), 1);
        assert_eq!(graph.get_node(n1).unwrap(), "A");
        assert!(graph.contains_node(n1));
        
        let neighbors: Vec<_> = graph.neighbors(n1).collect();
        assert_eq!(neighbors, vec![n2]);
    }

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

        // 测试 GraphUpdate trait
        let n1 = graph.add_node("A".to_string()).unwrap();
        let n2 = graph.add_node("B".to_string()).unwrap();
        let e1 = graph.add_edge(n1, n2, 1.0).unwrap();

        assert_eq!(graph.node_count(), 2);
        assert_eq!(graph.edge_count(), 1);

        let removed = graph.remove_edge(e1).unwrap();
        assert_eq!(removed, 1.0);
    }

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

        // 测试 VirtualGraph trait
        let metadata = graph.metadata();
        assert_eq!(metadata.graph_type, GraphType::Directed);

        let n1 = graph.add_node("A".to_string()).unwrap();
        let n2 = graph.add_node("B".to_string()).unwrap();

        assert_eq!(graph.node_count(), 2);
        assert!(graph.contains_node(n1));
        assert!(graph.contains_node(n2));

        assert_eq!(graph.get_node(n1).unwrap(), "A");
        assert_eq!(graph.get_node(n2).unwrap(), "B");

        let e1 = graph.add_edge(n1, n2, 1.0).unwrap();
        assert_eq!(graph.edge_count(), 1);
        assert!(graph.contains_edge(e1));

        let neighbors: Vec<_> = graph.neighbors(n1).collect();
        assert_eq!(neighbors, vec![n2]);
        assert_eq!(graph.out_degree(n1).unwrap(), 1);
    }

    #[test]
    fn test_virtual_graph_remove() {
        let mut graph = Graph::<i32, f64>::undirected();

        let n1 = graph.add_node(1).unwrap();
        let n2 = graph.add_node(2).unwrap();
        let e1 = graph.add_edge(n1, n2, 1.0).unwrap();

        // 删除边
        let edge_data = graph.remove_edge(e1).unwrap();
        assert_eq!(edge_data, 1.0);
        assert_eq!(graph.edge_count(), 0);

        // 删除节点
        let node_data = graph.remove_node(n1).unwrap();
        assert_eq!(node_data, 1);
        assert_eq!(graph.node_count(), 1);
    }
}