px普贤教程PuXian
普贤教程 › 包参考

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_edgeg, a, b, w = 1
gr_nodesg
gr_hasg, a
gr_neighborsg, a
gr_bfsg, start
gr_dfsg, start
gr_topog
gr_shortest_pathg, a, b
其余 14 个 def(内部与辅助,可直接调用但不属稳定接口)
函数参数
gr_nodeg, id
gr_dfs_recg, node, seen, order
gr_add_undirectedg, a, b, w = 1
gr_count_nodesg
gr_count_edgesg
gr_degreeg, id
gr_reverseg
gr_has_cycleg
gr_cycle_dfsg, node, visited, rec_stack
gr_is_connectedg
gr_all_pathsg, start, end, max_depth = 10
gr_all_paths_dfsg, node, end, path, visited, paths, max_depth
gr_mstg
gr_uf_findparent, 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(响亮)。
============================================================