petgraph/petgraph

GitHub: petgraph/petgraph

一个用 Rust 编写的高性能图数据结构与算法库,提供多种图类型和丰富的内置算法,支持可视化导出和 no_std 环境。

Stars: 3875 | Forks: 445

Petgraph Logo

# petgraph Petgraph 为 Rust 提供了快速、灵活的图数据结构和算法。支持带有任意节点和边数据的有向图和无向图。它具有以下特性: * **多种图类型**:Graph、StableGraph、GraphMap 和 MatrixGraph,以适应各种使用场景。 * **内置且可扩展的算法**:用于寻路、最小生成树、图同构等任务,并暴露了 trait 以实现自定义算法。 * **图可视化支持**:支持从 [DOT][dot-url] 格式导入/导出图,以便使用 [Graphviz][graphviz-url] 进行可视化。 支持 Rust 1.64 及更高版本。此要求仅会在主要版本发布时更改。 [![Crates.io](https://img.shields.io/crates/v/petgraph.svg)][crates-url] [![docs.rs](https://img.shields.io/docsrs/petgraph)][docsrs] ![MSRV](https://img.shields.io/badge/rustc-1.64+-blue.svg) [![Discord chat](https://img.shields.io/discord/1166289348384280616?logo=discord&style=flat)][discord-url] [![Build Status](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/04d94d60b5113725.svg)][ci-url] ## 示例 有关更多示例,请参阅 [docs.rs 上的文档][docsrs-examples]。 ``` use petgraph::graph::UnGraph; use petgraph::algo::{dijkstra, min_spanning_tree}; use petgraph::data::FromElements; use petgraph::dot::{Dot, Config}; use petgraph::visit::NodeIndexable; fn main() { // Create an undirected graph with associated data // of type `i32` for the nodes and `()` for the edges. let g = UnGraph::::from_edges(&[ (0, 1), (1, 2), (2, 3), (0, 3) ]); // The graph looks like this: // 0 -- 1 // | | // 3 -- 2 // Find the shortest path from `0` to `2` using `1` as the cost for every edge. let node_map = dijkstra(&g, 0.into(), Some(2.into()), |_| 1); assert_eq!(&2i32, node_map.get(&g.from_index(2)).unwrap()); // Get the minimum spanning tree of the graph as a new graph, and check that // one edge was trimmed. let mst = UnGraph::<_, _>::from_elements(min_spanning_tree(&g)); assert_eq!(g.raw_edges().len() - 1, mst.raw_edges().len()); // Output the tree to `graphviz` `DOT` format println!("{:?}", Dot::with_config(&mst, &[Config::EdgeNoLabel])); // graph { // 0 [ label = "0" ] // 1 [ label = "0" ] // 2 [ label = "0" ] // 3 [ label = "0" ] // 0 -- 1 [ ] // 2 -- 3 [ ] // 1 -- 2 [ ] // } } ``` ## 文档 * [docs.rs 上的 API 文档][docsrs] * [docs.rs 上的示例][docsrs-examples] * [更新日志][changelog] ### Crate 特性 petgraph 默认启用以下特性进行构建: - `graphmap` - 启用 [`GraphMap`][docsrs-graph-map]。 - `stable_graph` - 启用 [`StableGraph`][docsrs-stable-graph]。 - `matrix_graph` - 启用 [`MatrixGraph`][docsrs-matrix-graph]。 - `std` - 启用 Rust 标准库。 禁用 `std` 特性使得在 `no_std` 环境中使用 `petgraph` 成为可能。 可选择启用以下特性: - `serde-1` - 使用 [serde 1.0][docsrs-serde] 为 `Graph, StableGraph, GraphMap` 启用序列化功能。需要满足 serde 要求的 Rust 版本。 - `rayon` - 为 `GraphMap` 中的底层数据启用并行迭代器。需要满足 [rayon][docsrs-rayon] 要求的 Rust 版本。 - `dot_parser` - 启用从 [DOT/Graphviz][dot-url] 字符串和文件解析图的功能。 - `generate` - 启用图生成器。 - `unstable` - 启用不稳定的 crate 特性(目前仅有 `generate`)。此标志后功能的 API 随时可能发生更改。 ## 获取帮助 首先,请查看 [API 文档][docsrs] 中能否找到您问题的答案。如果找不到答案,请随时在 [讨论页面][github-discussions] 上提问。我们将很乐意为您解答。如果您发现错误或有功能建议,请[创建一个 issue][github-new-issue]。 ## Logo 吉祥物的名字叫“Sir Paul Rustory Graphosaurus”(好朋友都叫他 Paul)。 该 Logo 由才华横溢的 Aren 创作。 ## 许可证 采用双重许可,以兼容 Rust 项目。 根据您的选择,在 [Apache License, Version 2.0][apache-license] 或 [MIT 许可证][mit-license] 下获得许可。除非按照这些条款,否则不得复制、修改或分发此文件。
标签:Dijkstra, DOT格式, Graphviz, Petgraph, Rust, SEO关键词, 可视化界面, 图可视化, 图同构, 图数据结构, 图算法, 开源库, 搜索引擎爬虫, 数据结构与算法, 数据结构库, 无向图, 最小生成树, 最短路径, 有向图, 网络流量审计, 路径查找, 通知系统