Module dalpy.graphs
This module holds classes related to graphs.
This module contains the Vertex
, VertexAttributeError
, Graph
, and GraphVertexError
classes. Vertex
represents
a graph vertex. VertexAttributeError
is an error raised by Vertex
. Graph
represents a directed graph.
GraphVertexError
is an error raised by Graph
.
Examples
Initializing a Graph
as well as adding colored vertices and edges:
g = Graph()
a = Vertex('a', color='red')
b = Vertex('b', color='blue')
g.add_vertex(a)
g.add_vertex(b)
g.add_edge(a, b)
The following code will raise a VertexAttributeError
because 'colour'
is not an attribute of the Vertex
a
:
x = a['colour']
Classes
class Graph
-
Represents a directed graph.
A
Graph
represents a directed graph object whose edges can be assigned weights. TheVertex
objects in theGraph
should have unique names. For more information on this, see the class docstring ofVertex
.Examples
To initialize a
Graph
:g = Graph()
In order to add edges to a
Graph
, one must first add theVertex
objects that will make up that edge.a = Vertex('a') b = Vertex('b') g.add_vertex(a) g.add_vertex(b)
Now that
a
andb
have been added tog
, one can add an edge between them. Note that there is no edge object.g.add_edge(a, b)
When adding an edge, one can specify a weight (by default it is
None
):g.add_edge(a, b, 1)
One can get a
Set
of the adjacent edges of a Vertex:s = g.adj(a)
Initializes an empty
Graph
inO(1)
time.Methods
def add_edge(self, source, dest, weight=None)
-
Adds an edge between 2
Vertex
objects in thisGraph
.This creates an edge from the source
Vertex
to the destinationVertex
.Graph
is directed so the edge is in only one direction. That is, an edge from the destinationVertex
to the sourceVertex
will not exist. One can additionally provide a weight for this edge. One may assume that this operation runs inO(1)
time with respect to the number of vertices and edges in thisGraph
.Args
source
- The source
Vertex
. dest
- The destination
Vertex
. weight
- The weight for this edge (floating point, integer). By default this is
None
.
Raises
GraphVertexError
- If
source
ordest
is not in thisGraph
.
def add_vertex(self, vertex)
def adj(self, vertex)
-
Gets the
Vertex
objects that are adjacent to aVertex
.This gets a
Set
of theVertex
objects that are adjacent to the vertex. SinceSet
objects preserve insertion order (seeSet
documentation), theSet
will be ordered according to the order in which edges starting from the inputVertex
were created. This method runs inO(n)
time wheren
is the number of edges going out of the inputVertex
.Args
vertex
- A
Vertex
.
Returns
A
Set
containing the vertices adjacent tovertex
.Raises
GraphVertexError
- If
vertex
is not in thisGraph
.
Examples
To illustrate the nature of the
Set
returned by this method, first set up aGraph
and add some vertices and edges:g = Graph() a = Vertex('a') b = Vertex('b') c = Vertex('c') g.add_vertex(a) g.add_vertex(b) g.add_vertex(c) g.add_edge(a, b) g.add_edge(a, c)
The
Set
returned byg.adj(a)
will always haveb
precedingc
since the edge froma
tob
was created before the edge froma
toc
. def vertices(self)
-
Gets the vertices in this
Graph
.This method runs in
O(V)
time whereV
is the number of vertices added to thisGraph
.Returns
A
Set
containing theVertex
objects in thisGraph
. The order of the vertices in thisSet
will always be the order in which the vertices were added to thisGraph
viaadd_vertex
. def weight(self, source, dest)
-
Gets the weight of an edge defined by two vertices.
This method runs in
O(1)
time with respect to the number of vertices and edges in thisGraph
.Args
source
- The source
Vertex
of the edge in question. dest
- The destination
Vertex
of the edge in question.
Returns
The integer or floating point weight associated with the edge from
source
todest
. This will beNone
if no weight was specified when the edge was created.Raises
GraphVertexError
- If
source
ordest
is not in thisGraph
. GraphEdgeError
- If an edge from
source
todest
does not exist.
class GraphEdgeError (source, dest)
-
This class is used by
Graph
to raise errors regarding invalid edges.Initializes a
GraphEdgeError
that will be raised associated with a particularVertex
.Args
source
- The source
Vertex
of the edge thisGraphVertexError
is being raised in association with. dest
- The destination
Vertex
of the edge thisGraphVertexError
is being raised in association with.
Ancestors
- builtins.Exception
- builtins.BaseException
class GraphVertexError (vertex_name)
-
This class is used by
Graph
to raise errors regarding invalid vertices.Initializes a
GraphVertexError
that will be raised associated with a particularVertex
.Args
vertex_name
- The string name of the
Vertex
thisGraphVertexError
is being raised in association with.
Ancestors
- builtins.Exception
- builtins.BaseException
class Vertex (name, **attributes)
-
Represents a graph vertex.
A
Vertex
has a name and a collection of customizable attributes. The name should be used to identify it in aGraph
.Vertex
objects are compared with==
and via hash code based on their name. The attributes you can use in graph algorithms. For example, you could set colors or time steps as in DFS. One should assume all operations that can be performed on aVertex
are done inO(1)
time.Examples
To initialize a
Vertex
with a name:v = Vertex('a')
To initialize a
Vertex
with some starting attributes, use keyword arguments following the name:v = Vertex('a', color='red', time=1)
To add additional attributes to an existing
Vertex
using[]
with=
:v['seen'] = False
To update or view existing attributes, use the same idea:
x = v['color'] v['time'] = 3
If you try to get the value of an attribute that does not exist, a
VertexAttributeError
will be raised (e.g. callingprint(v['colour'])
. If you try to add a new attribute to an existingVertex
where the attribute's type is notstr
, aTypeError
will be raised (e.g. callingv[1] = 'blue'
).Initializes a
Vertex
.Args
Methods
def get_name(self)
-
Returns the name of this
Vertex
.
class VertexAttributeError (name, wrong_attribute, attributes)
-
This class is used by
Vertex
to raise errors regarding invalid attributes.Initializes a
VertexAttributeError
.Args
Ancestors
- builtins.Exception
- builtins.BaseException