Skip to main content

PGraph: graphs for Python

A Python Robotics Package Powered by Spatial Maths QUT Centre for Robotics Open Source

PyPI version fury.io PyPI pyversions GitHub license

Build Status Coverage pypi downloads

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.

road network

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

Properties and methods of the graph

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 vertex

  • g.n the number of vertices

  • g is an iterator over vertices, can be used as for vertex in g:

  • g[i] reference a vertex by its index or name


  • g.add_edge() connect two vertices

  • g.edges() all edges in the graph

  • g.plot() plots the vertices and edges

  • g.nc the number of graph components, 1 if fully connected

  • g.component(v) the component that vertex v belongs to


  • g.path_BFS() breadth-first search

  • g.path_Astar() A* search


  • g.adjacency() adjacency matrix

  • g.Laplacian() Laplacian matrix

  • g.incidence() incidence matrix

Properties and methods of a vertex

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.neighbours() is a list of the neighbouring vertices
  • v1.samecomponent(v2) predicate for vertices belonging to the same component

Vertices can be named and referenced by name.

Properties and methods of an edge

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 methods
  • e.next(v) vertex on edge e that is not v
  • e.v1, e.v2 the two vertices that define the edge e

Modifying a graph

  • g.remove(v) remove vertex v
  • e.remove() remove edge e

Subclasing pgraph classes

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 or UVertex depending on graph type
  • Then place instances of Foo into the graph using add_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.neighbours():
    # say hi to the neighbours

Under the hood

The key objects and their interactions are shown below.

data structures

MATLAB version

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.

Release files for pgraph-python 0.6.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pgraph-python 0.6.5
File Size Uploaded
pgraph_python-0.6.5.tar.gz 312.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pgraph-python 0.6.5
File Interpreter ABI Platform
pgraph_python-0.6.5-py3-none-any.whl Python 3 none any Details

Total release size: 329.9 kB

Release files / pgraph_python-0.6.5.tar.gz

Download URL pgraph_python-0.6.5.tar.gz
Size 312.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2462d0fd6a7278779f37fc8a582d1817ab6930c4a2b83cad0ee93256637f7f76
BLAKE2b-256 checksum
How to use checksums
53202e71ec7098143e29a7af91b015905471ff1f9d91777943c7c199505a5cc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.8

Release files / pgraph_python-0.6.5-py3-none-any.whl

Download URL pgraph_python-0.6.5-py3-none-any.whl
Size 17.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ebd04cf5b048c82acf91c92fd78b333566673c3882a515b142bc959abcd158dc
BLAKE2b-256 checksum
How to use checksums
43454f06b19453085afa86df83516afbf202630fa58e68260f17ae9a24ea8a31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.6.5 This release

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

1 release file

0.6

1 release file

0.5

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page