Graphs for Python
Repo petercorke/pgraph-python, read from GitHub 5 h ago. petercorke has not joined Take My Repo.
1.0.0
First 1.0 release: six years of production use since the first commit in 2020
(including as a real dependency of both the Robotics Toolbox for Python and the
Machine Vision Toolbox for Python), and this cycle's type hints, documentation,
testing and bug-fixing pass, felt like the right point to commit to API
stability. This is the first release with a considered API, so it contains
several deliberate breaking changes, listed first with how to migrate.
Breaking changes (from 0.6.5)
UGraph/DGraph constructor arguments are keyword-only. 0.6.5 acceptedUGraph(6) looked asTypeError: writeUGraph(dim=6).degree of a DGraph is the graph-theory degree, in plus out, so avertex.degree,graph.degree() and graph.connectivity(), and graph.average_degree() isDGraph). The previousvertex.out_degree, [v.out_degree for v in g] and g.ne / g.n.laplacian() is unchanged (out-degree minus adjacency). Undirected graphsgraph.edges() is a list in the order the edges were added. It was theset, with no defined order, although documented as a list. Bothgraph.edges() and vertex.edges() return a new list, so changing the resultset(g.edges()) for set operations.Edge.v1 and Edge.v2 are read-only, and so is Vertex.label (the graphNonenc had been asked for). Assigning an endpoint of an edge that was in a graph left theEdge(v1, v2) or connect the edge with v1.connect(v2, edge=e).Vertex.name renames it in the graph (so g[new] works) andNone.add_vertex raises ValueError for a name already used in the graph or a#N is now the first free one, which could previouslyVertex: a vertex has no direction of its own, theUVertexDVertex are deprecated names for it (below). A vertex given toadd_vertex is no longer checked against the kind of graph, because there is_attach/_detach hooksUGraph/DGraph subclass.PGraph is now Graph, and Graph and Vertex areGraph is concrete: Graph(directed=True) is aDGraph and Graph() a UGraph.Adjacency() treats a non-zero diagonal element as a self-loop.str of an edge between vertices that are not in a graph shows --,Deprecated (still work, with a DeprecationWarning)
Laplacian() is laplacian(): a capital initial denotes a class method thatAdjacency() and Dict().UVertex and DVertex are Vertex.remove() is remove_vertex() or remove_edge(), which say which they do.plot() are named for what they are: vopt is vertex_opt,eopt is edge_opt, text is labels and text_opt, colorcomponents iscolor_components and force2d is force_2d. The positional order isTypeError.Added
adjacent() and isadjacent(), the graph-theory names, which have oneneighbours(), neighbors() and isneighbour() from 0.6.5 areisneighbor() added, and none of them warn.plot() draws each edge once. A directed graph has arrowheads, whicharrow_opt styles; edges that join the same pair of vertices, in eitherplot()highlight_edge() follows an arc.Graph(directed=...), the directed property, and vertex_class andedge_class constructor arguments, so that user subclasses of Vertex andEdge are built by the graph.add_edge() accepts an Edge, and connect() an edge= argument.edges_to(dest) lists every edge from a vertex to another, so parallel edgesedgeto(dest) is still the cheapest.predecessors(),successors(), in_edges(), out_edges(), in_degree and out_degree.export() writes a graph as GraphViz dot, Mermaid, GraphML or ELK JSON, or aspgraph.exporters module can be extended with register_exporter.validate() reports every way in which a graph's records disagree, for testscopy() works for graphs of any size. copy()RecursionError for a path of about 100 vertices.iscyclic(), remove_edge(), remove_vertex() and the dim argument, whichSE2Metric(weight) is the "SE2" metric with a weight on the angle, the"SE2" is SE2Metric(), a weight of 1, so existing resultscoordinates() is the matrix of vertexwithin(box) the vertices inside a box;near(point, radius) those within a distance, nearest first; andsubgraph(vertices) the graph that a set of vertices induces, given asbuild_kdtree() makes an optional k-d tree that closest() and near() useSE2Metric included. It needs scipy, which isvalidate() also checks that coordinates are finite vectors of one length,embedded=True that every vertex has one.Faster
path_BFS, path_UCS and path_Astar scaled badly: they searched lists within, and took the minimum of the whole frontier at every step. Results andVertex(coord) takes 0.35 µs where 0.6.5 tookg.add_vertex(coord) 1.7 µs where it took 2.2 µs. Making thenp.r_ was most of the cost.import pgraph takes 0.04 s where it took 0.6 s, and loads neitherstr(g) separates thousands: DGraph: 4,881 vertices, 8,392 edges, 14 components.Fixed
Dict() always created undirected vertices, even for a DGraph; connect()path_BFS/path_UCS/path_Astar checked thedistance() and thenan or None into their results for edges with no costnc never returned for many directed graphs, the smallest a 4-cycle, andcomponent(), samecomponent() or plot(), which ask for it.Edge.vertices() raised DeprecationWarning as an exception; it now warnsEdge.endpoints does.incidence() returned the wrong matrix for every graph with an edge: a loopedges(), which is now in the order the edges wereremove() crashed for every directed edge; dotfile() closed sys.stdoutAttributeError or TypeError instead of a clear message for a vertex withhighlight_edge(), and so highlight_path(), raised AttributeError for ahighlight_vertex() put its marker at z = 0. They draw inforce_2d is_graph pointing at the graph; renaming a vertexg[x] accepted any type.Known limitations
lambda or a locally defined function can be copied"L1", "L2" and "SE2" and module-levelBuild, tests and documentation
mypy is a CI check.sphinx-pyrunblock (whichsphinx-autorun) and the matplotlib plot directive.build/lib, left by a setuptools build before the move to the src layout),RELEASE file, which held 0.6.1 and which nothing read.scipy dependency is dropped, andpyproject.toml and thatmain, and installs the built wheel and runs a pathllms.txt and llms-full.txt are published with the documentation, for AIAGENTS.md describes the source forexamples/: roads.ipynb, the Queensland example, andlondon.ipynb, the road network of central London from OpenStreetMap as aexamples/benchmark.py, and the README has its results and an