This 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 path
Graphs belong to the class UGraph
or DGraph
for undirected or directed graphs respectively. The graph is essentially a container for the vertices.
g.add_vertex()
add a vertexg.n
the number of verticesg
is 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.nc
the number of graph components, 1 if fully connectedg.component(v)
the component that vertexv
belongs tog.path_BFS()
breadth-first searchg.path_Astar()
A* searchg.adjacency()
adjacency matrixg.Laplacian()
Laplacian matrixg.incidence()
incidence matrix
Vertices belong to the class UVertex
(for undirected graphs) or DVertex
(for directed graphs), which are each subclasses of Vertex
.
v.coord
the coordinate vector for embedded graph (optional)v.name
the name of the vertex (optional)v.neigurs()
is a list of the neiguring verticesv1.samecomponent(v2)
predicate for vertices belonging to the same component
Vertices can be named and referenced by name.
Edges are instances of the class Edge
. 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.cost
cost of edge for planning methodse.next(v)
vertex on edgee
that is notv
e.v1
,e.v2
the two vertices that define the edgee
g.remove(v)
remove vertexv
e.remove()
remove edgee
Consider a user class Foo
that we would like to connect using a graph overlay, ie. instances of Foo
becomes vertices in a graph.
- Have it subclass either
DVertex
orUVertex
depending on graph type - Then place instances of
Foo
into the graph usingadd_vertex
and create edges as required
class 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.neigurs():
# say hi to the neigurs
The key objects and their interactions are shown below.
This is a re-engineered version of PGraph.m which ships as part of the Spatial 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 to user data is encouraged.