1 个不稳定版本

0.0.0-a52024年6月20日
0.0.0-a1 2024年6月15日
0.0.0-a0 2024年5月17日

#726Rust 模式

22 每月下载量
用于 pyo3-graphster

BSD-3-Clause

150KB
2.5K SLoC

Graphster logo

无需头痛的图:简单、强大、有效

Graphster 是一个专为快速数据操作、分析和处理设计的 DataGraph 库。


lib.rs:

Graphster

Graphster 是一个通用的图数据结构库,专为快速数据操作、分析和处理而设计。它提供了一组丰富的功能,用于创建、管理和操作具有不同类型节点和边的图,每个节点和边都能持有多个属性。

功能

  • 节点和边:轻松添加、删除和操作节点和边。
  • 属性:每个节点和边都可以有与灵活类型关联的属性,包括整数、浮点数、字符串等。
  • 错误处理:对常见图操作提供全面错误处理。
  • Polars 集成:可选支持 Polars 数据框架以导入图数据。

示例

创建图

use graphster::prelude::*;
use std::collections::HashMap;

let mut graph = DataGraph::new();

// Add nodes with attributes
graph.add_node(0, Attributes::from([("foo", "bar")])).unwrap();
graph.add_node(1, HashMap::from([("bar", "foo")])).unwrap();

// Add an edge between nodes
graph.add_edge(0, 1, Attributes::from([("weight", 10)])).unwrap();

// Get node and edge counts
assert_eq!(graph.node_count(), 2);
assert_eq!(graph.edge_count(), 1);

使用属性

use graphster::prelude::*;

let mut attrs = Attributes::new();
attrs.insert("key1", "value1");
attrs.insert("key2", 42);

assert_eq!(attrs.get("key1"), Some(&AttributeValue::String("value1".to_string())));
assert_eq!(attrs.get("key2"), Some(&AttributeValue::Int32(42)));

处理错误

use graphster::prelude::*;

let mut graph = DataGraph::new();
match graph.add_edge(0, 1, Attributes::new()) {
    Ok(_) => println!("Edge added successfully"),
    Err(e) => println!("Error adding edge: {}", e),
}

使用 Polars 数据框架

use graphster::prelude::*;
use polars::prelude::*;

let s0 = Series::new("index", &[0, 1, 2]);
let s1 = Series::new("values", &[Some(1), None, Some(3)]);
let df = DataFrame::new(vec![s0, s1]).unwrap();

let nodes_df = NodesDataFrame::new(&df, "index");
let graph = DataGraph::from_nodes_dataframes(nodes_df).unwrap();

高级操作示例

邻居

检索所有可以通过从指定节点发出的边到达的相邻节点。

use graphster::prelude::*;

let mut graph = DataGraph::new();
graph.add_node(0, Attributes::new()).unwrap();
graph.add_node(1, Attributes::new()).unwrap();
graph.add_edge(0, 1, Attributes::new()).unwrap();

let neighbors: Vec<_> = graph.neighbors(0).unwrap().collect();
assert_eq!(neighbors, vec![&1.into()]);

无向邻居

检索所有相邻节点,无论边的方向。

use graphster::prelude::*;

let mut graph = DataGraph::new();
graph.add_node(0, Attributes::new()).unwrap();
graph.add_node(1, Attributes::new()).unwrap();
graph.add_edge(0, 1, Attributes::new()).unwrap();
graph.add_edge(1, 0, Attributes::new()).unwrap();

let neighbors: Vec<_> = graph.neighbors_undirected(0).unwrap().collect();
assert_eq!(neighbors, vec![&1.into()]);

连接节点的边

检索连接指定源节点和目标节点的所有边。

use graphster::prelude::*;

let mut graph = DataGraph::new();
graph.add_node(0, Attributes::new()).unwrap();
graph.add_node(1, Attributes::new()).unwrap();
let edge_index = graph.add_edge(0, 1, Attributes::new()).unwrap();

let edges: Vec<_> = graph.edges_connecting(0, 1).unwrap().collect();
assert_eq!(edges, vec![&edge_index]);

无向连接节点的边

检索连接两个节点的所有边,无论边的方向。

use graphster::prelude::*;

let mut graph = DataGraph::new();
graph.add_node(0, Attributes::new()).unwrap();
graph.add_node(1, Attributes::new()).unwrap();
let edge_index_1 = graph.add_edge(0, 1, Attributes::new()).unwrap();
let edge_index_2 = graph.add_edge(1, 0, Attributes::new()).unwrap();

let edges: Vec<_> = graph.edges_connecting_undirected(0, 1).unwrap().collect();
assert_eq!(edges, vec![&edge_index_1, &edge_index_2]);

可选功能

  • Polars:通过指定 polars 功能启用对 Polars 数据框架的支持。

依赖项

~1.5–4MB
~76K SLoC