Skip to main content

god_graph/generators/
complete.rs

1//! 完全图生成器
2
3use crate::graph::builders::GraphBuilder;
4use crate::graph::Graph;
5
6/// 生成完全图 K_n
7///
8/// 完全图是每个节点都与其他所有节点相连的图
9///
10/// # 参数
11/// * `n` - 节点数量
12///
13/// # 返回
14/// 生成的完全图
15pub fn complete_graph<T>(n: usize) -> Graph<T, f64>
16where
17    T: Clone + Default,
18{
19    let mut builder = GraphBuilder::undirected().with_nodes((0..n).map(|_| T::default()));
20
21    for i in 0..n {
22        for j in (i + 1)..n {
23            builder = builder.with_edge(i, j, 1.0);
24        }
25    }
26
27    builder.build().unwrap_or_else(|_| Graph::undirected())
28}