Skip to main content

Simple graph functionality for Python.

Project description

PGraph: simple graphs for Python

made-with-python [pypi downloads PyPI version Maintenance GitHub license

This Python package allows the manipulation of directed and non-directed graphs. Also supports embedded graphs.

road network

from pgraph import *

# 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

  • g.add_vertex() add a vertex
  • g.add_edge() connect two vertices
  • g.n the number of vertices
  • supports iteration: for vertex in graph:
  • g.edges() all edges in the graph
  • g[i] reference a vertex by its index or name
  • g.nc the number of graph components, 1 if fully connected
  • g.component(v) the component that vertex v belongs to
  • g.plot() plots the vertices and edges
  • 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 or DVertex for undirected or directed graphs respectively

  • v.coord the coordinate vector for embedded graph
  • v.name the name of the vertex
  • access the neighbours of a vertex by v.neighbours().

We can name the vertices and reference them by name

Properties and methods of an edge

  • 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

Inheritance

Consider a user class Foo that we would like to represent vertices in a graph.

  • Have it subclass either DVertex or UVertex
  • Then place instances of Foo into the graph using add_vertex

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgraph-python-0.5.tar.gz (12.1 kB view details)

Uploaded Source

File details

Details for the file pgraph-python-0.5.tar.gz.

File metadata

  • Download URL: pgraph-python-0.5.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for pgraph-python-0.5.tar.gz
Algorithm Hash digest
SHA256 199df8fbcc0b7c6ab636a0356fdd29901a1700d1e83eead96845efae42a2882f
MD5 2fffb11f62a323f44d637100025f4b15
BLAKE2b-256 656c042c6b4261a6a21b088af1738d415ce9e86cc5a37a4ad671f93965eb1b5c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page