普贤教程 › 包参考
graph 包
图算法(邻接表)
| 项 | 值 |
|---|---|
| 包名 | graph |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | graph.px |
| 分类 | 数据结构 |
| 文件数 / 行数 | 1 / 319 |
安装与引入
bash
pxpkg init # 首次 pxpkg add graph@0.2.0 pxpkg install
px
import graph所属分类教程:数据结构
公开接口
| 函数 | 参数 |
|---|---|
gr_new | |
gr_add_edge | g, a, b, w = 1 |
gr_nodes | g |
gr_has | g, a |
gr_neighbors | g, a |
gr_bfs | g, start |
gr_dfs | g, start |
gr_topo | g |
gr_shortest_path | g, a, b |
其余 14 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
gr_node | g, id |
gr_dfs_rec | g, node, seen, order |
gr_add_undirected | g, a, b, w = 1 |
gr_count_nodes | g |
gr_count_edges | g |
gr_degree | g, id |
gr_reverse | g |
gr_has_cycle | g |
gr_cycle_dfs | g, node, visited, rec_stack |
gr_is_connected | g |
gr_all_paths | g, start, end, max_depth = 10 |
gr_all_paths_dfs | g, node, end, path, visited, paths, max_depth |
gr_mst | g |
gr_uf_find | parent, x |
包内说明(源码头注释)
text
============================================================
普贤生态库:registry/graph —— 图算法(邻接表)
------------------------------------------------------------
纯函数库(无 IO、无状态,图状态以 dict 显式传递),双模式一致。
gr_new() → dict {nodes: {id: {edges: [{to, w}]}}}
gr_add_edge(g, a, b, w=1) → g 自动建节点
gr_nodes(g) / gr_has(g, a) / gr_neighbors(g, a) → [str]
gr_bfs(g, start) / gr_dfs(g, start) → [str]
gr_topo(g) → Ok([str]) | Err(环 → Err)
gr_shortest_path(g, a, b) → Ok([str] 路径) | Err(Dijkstra,边权非负)
节点 id 为 string;未知节点访问 key 返回 Err(响亮)。
============================================================