{"id":50376954,"url":"https://github.com/twn39/pypetgraph","last_synced_at":"2026-05-30T10:01:50.177Z","repository":{"id":342470502,"uuid":"1172128766","full_name":"twn39/pypetgraph","owner":"twn39","description":"pypetgraph 是基于 Rust 著名图计算库 petgraph 的 Python 高性能封装。它利用 PyO3 实现了近乎原生的 Rust 计算速度，支持在节点和边上存储任意 Python 对象，并针对多核并发和异步 IO 进行了深度优化。","archived":false,"fork":false,"pushed_at":"2026-03-06T09:44:32.000Z","size":140,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-08T19:06:52.638Z","etag":null,"topics":["dijkstra","graph","network","pagerank","petgraph","pyo3","rust"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twn39.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-04T01:09:18.000Z","updated_at":"2026-03-06T09:44:18.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/twn39/pypetgraph","commit_stats":null,"previous_names":["twn39/pypetgraph"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/twn39/pypetgraph","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twn39%2Fpypetgraph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twn39%2Fpypetgraph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twn39%2Fpypetgraph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twn39%2Fpypetgraph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twn39","download_url":"https://codeload.github.com/twn39/pypetgraph/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twn39%2Fpypetgraph/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33687722,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-30T02:00:06.278Z","response_time":92,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dijkstra","graph","network","pagerank","petgraph","pyo3","rust"],"created_at":"2026-05-30T10:01:49.241Z","updated_at":"2026-05-30T10:01:50.172Z","avatar_url":"https://github.com/twn39.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# 🦀 pypetgraph\n\n`pypetgraph` 是基于 Rust 著名图计算库 [petgraph](https://github.com/petgraph/petgraph) 的 Python 高性能封装。它利用 PyO3 实现了近乎原生的 Rust 计算速度，支持在节点和边上存储任意 Python 对象，并针对多核并发和异步 IO 进行了深度优化。\n\n[![PyPI Version](https://img.shields.io/pypi/v/pypetgraph.svg)](https://pypi.org/project/pypetgraph/)\n[![PyPI Downloads](https://img.shields.io/pypi/dm/pypetgraph.svg)](https://pypi.org/project/pypetgraph/)\n[![Build Status](https://github.com/twn39/pypetgraph/actions/workflows/build.yml/badge.svg)](https://github.com/twn39/pypetgraph/actions)\n[![codecov](https://codecov.io/gh/twn39/pypetgraph/branch/main/graph/badge.svg)](https://codecov.io/gh/twn39/pypetgraph)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)\n[![Rust](https://img.shields.io/badge/rust-1.70+-orange.svg)](https://www.rust-lang.org/)\n\n📖 **[详细 API 参阅文档 →](docs/api.md)**\n\n\u003c/div\u003e\n\n---\n\n## ✨ 核心特性\n\n- **极致性能**：核心逻辑由 Rust 编写，比 NetworkX 等纯 Python 库快 10-100 倍。\n- **内存高效**：提供专门的 `FastDiGraph`（固定边权）和 `CsrGraph`（压缩存储）以应对大规模数据。\n- **并发友好**：重型算法在执行时会**释放 GIL**，支持真正的 Python 多线程并行计算。\n- **异步原生**：所有耗时算法均内置 `_async` 版本，无缝集成 `asyncio`。\n- **多种结构**：支持邻接列表、邻接矩阵、CSR 压缩格式以及基于哈希的 `GraphMap`。\n- **类型安全**：提供完整的 `.pyi` 类型提示文件，支持现代 IDE 的自动补全与静态检查。\n\n---\n\n## 🛠 安装 (Installation)\n\n### 1. 使用 pip (Standard)\n你可以直接通过 PyPI 安装已发布的预编译版本：\n\n```bash\npip install pypetgraph\n```\n\n### 2. 使用 uv (Modern)\n推荐使用现代高性能工具 [uv](https://github.com/astral-sh/uv)：\n\n```bash\nuv add pypetgraph\n```\n\n### 3. 从源码构建 (Development)\n如果你需要最新的开发版本，可以从源码本地编译：\n\n```bash\ngit clone https://github.com/twn39/pypetgraph\ncd pypetgraph\nuv run maturin develop --release\n```\n\n---\n\n## 🚀 快速上手\n\n### 1. 通用图 (DiGraph)\n支持挂载任何 Python 对象（字典、自定义类、字符串等）。\n\n```python\nfrom pypetgraph import DiGraph\n\ng = DiGraph()\nn0 = g.add_node({\"name\": \"Beijing\", \"type\": \"city\"})\nn1 = g.add_node({\"name\": \"Shanghai\", \"type\": \"city\"})\ng.add_edge(n0, n1, {\"distance\": 1200, \"traffic\": \"heavy\"})\n\n# 结构化分析\nprint(f\"Has cycle: {g.is_cyclic()}\")\nprint(f\"SCCs: {g.tarjan_scc()}\")\n```\n\n### 2. 异步计算\n利用 `_async` 方法，在不阻塞事件循环的情况下执行复杂图计算：\n\n```python\nimport asyncio\nfrom pypetgraph import FastDiGraph\n\nasync def main():\n    g = FastDiGraph.from_edges([(0, 1, 1.0), (1, 2, 2.0), (0, 2, 5.0)])\n    \n    # 异步计算全源最短路径\n    result = await g.floyd_warshall_async()\n    print(f\"Shortest path 0-\u003e2: {result[0][2]}\") # 输出 3.0\n\nasyncio.run(main())\n```\n\n---\n\n## 🗺 API 概览\n\n### 核心图类\n\n| 类名 | 特性 | 权重类型 | 最佳场景 |\n| :--- | :--- | :---: | :--- |\n| **`DiGraph`** | 有向邻接列表 | `Any` | 通用有向图，需要存储复杂对象 |\n| **`UnGraph`** | 无向邻接列表 | `Any` | 通用无向图 |\n| **`FastDiGraph`** | 数值优化图 | `f64` | **最高算法性能**，纯数值计算 |\n| **`StableDiGraph`**| 索引稳定图 | `Any` | 频繁删除节点但需保持原有索引不变 |\n| **`IntGraphMap`** | 整数哈希图 | `Any` | 节点 ID 为离散整数的极简结构 |\n| **`MatrixDiGraph`**| 邻接矩阵 | `f64` | 密集图 (Dense Graph) |\n| **`CsrGraph`** | 压缩稀疏行 | `f64` | **海量节点**的只读静态图 |\n\n### 支持算法\n\n| 类别 | 算法 | 特性 |\n| :--- | :--- | :--- |\n| **最短路径** | `dijkstra`, `astar`, `floyd_warshall`, `k_shortest_path` | 支持 `_async`，完全释放 GIL |\n| **负权处理** | `bellman_ford` | 仅限 `FastDiGraph`, `MatrixDiGraph`, `CsrGraph` |\n| **遍历** | `bfs`, `dfs` | 高效迭代器实现 |\n| **连通性** | `tarjan_scc`, `kosaraju_scc`, `connected_components` | 强连通分量与弱连通分量 |\n| **拓扑分析** | `toposort`, `is_cyclic`, `is_bipartite`, `is_isomorphic` | 结构完整性检查 |\n| **重要度** | `page_rank` | 用于节点排名与重要性评估 |\n\n---\n\n## 📈 性能表现 (Benchmarks)\n\n在处理大规模图数据时，`pypetgraph` 相比纯 Python 实现（如 NetworkX）具有压倒性优势。\n\n### 1. 内存占用对比\n在存储 **10 万节点** 和 **100 万条边** 的随机图时：\n\n| 库 | 内存占用 | 节省 |\n| :--- | :--- | :--- |\n| **NetworkX** | ~318.5 MB | - |\n| **pypetgraph (FastDiGraph)** | **~20.7 MB** | **93.5%** 📉 |\n\n### 2. 算法速度对比\n基于 5000 节点、5 万边的中型图测试：\n\n| 算法 | NetworkX | pypetgraph | 提升 |\n| :--- | :--- | :--- | :--- |\n| **Dijkstra** (最短路径) | ~6.3 ms | **~0.9 ms** | **7x** 🚀 |\n| **PageRank** (50次迭代) | ~19.5 ms | **~17.5 ms** | **等同/略快** |\n| **SCC** (强连通分量) | 较慢 | **极快** | **10x+** |\n\n---\n\n## 🧪 开发与测试\n\n```bash\nuv run pytest                      # 运行所有测试 (包含 sync/async)\nuv run pytest tests/test_performance.py -s  # 运行性能基准测试\nuv run ruff check .                # 代码风格检查\n```\n\n---\n\n## 许可证\n\n本项目采用 [MIT 许可证](LICENSE)。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwn39%2Fpypetgraph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwn39%2Fpypetgraph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwn39%2Fpypetgraph/lists"}