Edge

class PGraph.Edge(v1=None, v2=None, cost=None, data=None)[source]

Bases: object

Edge class

Is used to represent directed directed and undirected edges.

Each edge has: - cost cost of traversing this edge, required for planning methods - data reference to arbitrary data associated with the edge - v1 first vertex, start vertex for a directed edge - v2 second vertex, end vertex for a directed edge

Note

  • An undirected graph is created by having a single edge object in the edgelist of _each_ vertex.

  • This class can be inherited to provide user objects with graph capability.

  • Inheritance is an alternative to providing arbitrary user data.

An Edge points to a pair of vertices. At connect time the vertices get references back to the Edge object.

graph.add_edge(v1, v2) calls v1.connect(v2)

__init__(v1=None, v2=None, cost=None, data=None)[source]

Create an edge object

Parameters:
  • v1 (Vertex subclass, optional) – start of the edge, defaults to None

  • v2 (Vertex subclass, optional) – end of the edge, defaults to None

  • cost (any, optional) – edge cost, defaults to None

  • data (any, optional) – edge data, defaults to None

Creates an edge but does not connect it to the vertices or add it to the graph.

If vertices are given, and have associated coordinates, the edge cost will be computed according to the distance measure associated with the graph.

data is a way of associating any object with the edge, its value can be found as the .data attribute of the edge. An alternative approach is to subclass the Edge class.

Note

To compute edge cost from the vertices, the vertices must have been added to the graph.

Seealso:

Edge.connect() Vertex.connect()

connect(v1, v2)[source]

Add edge to the graph

Parameters:
  • v1 (Vertex subclass) – start of the edge

  • v2 (Vertex subclass) – end of the edge

The edge is added to the graph and connects vertices v1 and v2.

Note

The vertices must already be added to the graph.

property endpoints
next(vertex)[source]

Return other end of an edge

Parameters:

vertex (Vertex subclass) – one vertex on the edge

Raises:

ValueErrorvertex is not on the edge

Returns:

the other vertex on the edge

Return type:

Vertex subclass

e.next(v1) is the vertex at the other end of edge e, ie. the vertex that is not v1.

vertices()[source]