Package: pgraph-python
PGraph: graphs for PythonGitHub repository:https://github.com/petercorke/pgraph-pythonWiki (examples and details)https://github.com/petercorke/pgraph-python/wikiDocumentation:https://petercorke.github.io/pgraph-pythonDependencies:numpyThis Python package allows the manipulation of directed and non-directed graphs.  Also supports embedded graphs.  It is suitable for graphs with thousands of nodes.from pgraph import *
import json

# load places and routes
with open('places.json', 'r') as f:
    places = json.loads(f.read())
with open('routes.json', 'r') as f:
    routes = json.loads(f.read())

# build the graph
g = UGraph()

for name, info in places.items():
    g.add_vertex(name=name, coord=info["utm"])

for route in routes:
    g.add_edge(route[0], route[1], cost=route[2])

# plan a path from Hughenden to Brisbane
p = g.path_Astar('Hughenden', 'Brisbane')
g.plot(block=False) # plot it
g.highlight_path(p)  # overlay the pathProperties and methods of the graphGraphs belong to the classUGraphorDGraphfor undirected or directed graphs respectively.  The graph is essentially a container for the vertices.g.add_vertex()add a vertexg.nthe number of verticesgis an iterator over vertices, can be used asfor vertex in g:g[i]reference a vertex by its index or nameg.add_edge()connect two verticesg.edges()all edges in the graphg.plot()plots the vertices and edgesg.ncthe number of graph components, 1 if fully connectedg.component(v)the component that vertexvbelongs tog.path_BFS()breadth-first searchg.path_Astar()A* searchg.adjacency()adjacency matrixg.Laplacian()Laplacian matrixg.incidence()incidence matrixProperties and methods of a vertexVertices belong to the classUVertex(for undirected graphs) orDVertex(for directed graphs), which are each subclasses ofVertex.v.coordthe coordinate vector for embedded graph (optional)v.namethe name of the vertex (optional)v.neighbours()is a list of the neighbouring verticesv1.samecomponent(v2)predicate for vertices belonging to the same componentVertices can be named and referenced by name.Properties and methods of an edgeEdges are instances of the classEdge.
Edges are not referenced by the graph object, each edge references a pair of vertices, and the vertices reference the edges.  For a directed graph only the start vertex of an edge references the edge object, whereas for an undirected graph both vertices reference the edge object.e.costcost of edge for planning methodse.next(v)vertex on edgeethat is notve.v1,e.v2the two vertices that define the edgeeModifying a graphg.remove(v)remove vertexve.remove()remove edgeeSubclasing pgraph classesConsider a user classFoothat we would like to connect using a graphoverlay, ie.
instances ofFoobecomes vertices in a graph.Have it subclass eitherDVertexorUVertexdepending on graph typeThen place instances ofFoointo the graph usingadd_vertexand create edges as requiredclass Foo(UVertex):
  # foo stuff goes here
  
f1 = Foo(...)
f2 = Foo(...)

g = UGraph() # create a new undirected graph
g.add_vertex(f1)
g.add_vertex(f2)

f1.connect(f2, cost=3)
for f in f1.neighbours():
    # say hi to the neighboursUnder the hoodThe key objects and their interactions are shown below.MATLAB versionThis is a re-engineered version ofPGraph.mwhich ships as part of theSpatial Math Toolbox for MATLAB.  This class is used to support bundle adjustment, pose-graph SLAM and various planners such as PRM, RRT and Lattice.The Python version was designed from the start to work with directed and undirected graphs, whereas directed graphs were a late addition to the MATLAB version.  Semantics are similar but not identical.  In particular the use of subclassing rather than references touser datais encouraged.