Utilities

Graph Class

class iglsynth.util.graph.Graph(vtype=None, etype=None, graph=None, file=None)[source]

Base class to represent graph-based objects in IGLSynth.

  • Graph may represent a Digraph or a Multi-Digraph.
  • Graph.Edge may represent a self-loop, i.e. source = target.
  • Graph stores objects of Graph.Vertex and Graph.Edge classes or their sub-classes, which users may define.
  • Graph.Vertex and Graph.Edge may have attributes, which represent the vertex and edge properties of the graph.
Parameters:
  • vtype – (class) Class representing vertex objects.
  • etype – (class) Class representing edge objects.
  • graph – (Graph) Copy constructor. Copies the input graph into new Graph object.
  • file – (str) Name of file (with absolute path) from which to load the graph.

Todo

The copy-constructor and load-from-file functionality.

class Edge(u: iglsynth.util.graph.Graph.Vertex, v: iglsynth.util.graph.Graph.Vertex)[source]

Base class for representing a edge of graph.

  • Graph.Edge represents a directed edge.
  • Two edges are equal, if the two Graph.Edge objects are same.
Parameters:
  • u – (Graph.Vertex or its sub-class) Source vertex of edge.
  • v – (Graph.Vertex or its sub-class) Target vertex of edge.
source

Returns the source vertex of edge.

target

Returns the target vertex of edge.

class Vertex[source]

Base class for representing a vertex of graph.

add_edge(e: iglsynth.util.graph.Graph.Edge)[source]

Adds an edge to the graph. Both the vertices must be present in the graph.

Parameters:

e – (Graph.Edge) An edge to be added to the graph.

Raises:
  • AttributeError – When at least one of the vertex is not in the graph.
  • AssertionError – When argument e is not an Graph.Edge object.
add_edges(ebunch: Iterable[Graph.Edge])[source]

Adds a bunch of edges to the graph. Both the vertices of all edges must be present in the graph.

Raises:
  • AttributeError – When at least one of the vertex is not in the graph.
  • AssertionError – When argument e is not an Graph.Edge object.
add_vertex(v: iglsynth.util.graph.Graph.Vertex)[source]

Adds a new vertex to graph. An attempt to add existing vertex will be ignored, with a warning.

Parameters:v – (Graph.Vertex) Vertex to be added to graph.
add_vertices(vbunch: Iterable[Graph.Vertex])[source]

Adds a bunch of vertices to graph. An attempt to add existing vertex will be ignored, with a warning.

Parameters:vbunch – (Iterable over Graph.Vertex) Vertices to be added to graph.
edges

Returns an iterator over edges in graph.

get_edges(u: iglsynth.util.graph.Graph.Vertex, v: iglsynth.util.graph.Graph.Vertex)[source]

Returns all edges with source u and target v.

Parameters:
Returns:

(iterator(Graph.Edge)) Edges between u, v.

has_edge(e: iglsynth.util.graph.Graph.Edge)[source]

Checks whether the graph has the given edge or not.

Parameters:e – (Graph.Edge) An edge to be checked for containment in the graph.
Returns:(bool) True if the graph has the given edge, False otherwise.
has_vertex(v: iglsynth.util.graph.Graph.Vertex)[source]

Checks whether the graph has the given vertex or not.

Parameters:v – (Graph.Vertex) Vertex to be checked for containment.
Returns:(bool) True if given vertex is in the graph, else False.
in_edges(v: Union[Graph.Vertex, Iterable[Graph.Vertex]])[source]

Returns an iterator over incoming edges to given vertex or vertices. In case of vertices, the iterator is defined over the union of set of incoming edges of individual vertices.

Parameters:v – (Graph.Vertex) Vertex of graph.
Raises:AssertionError – When v is neither a Graph.Vertex object nor an iterable of Graph.Vertex objects.
in_neighbors(v: Union[Graph.Vertex, Iterable[Graph.Vertex]])[source]

Returns an iterator over sources of incoming edges to given vertex or vertices. In case of vertices, the iterator is defined over the union of set of incoming edges of individual vertices.

Parameters:v – (Graph.Vertex) Vertex of graph.
Raises:AssertionError – When v is neither a Graph.Vertex object nor an iterable of Graph.Vertex objects.
num_edges

Returns the number of edges in graph.

num_vertices

Returns the number of vertices in graph.

out_edges(v: Union[Graph.Vertex, Iterable[Graph.Vertex]])[source]

Returns an iterator over outgoing edges to given vertex or vertices. In case of vertices, the iterator is defined over the union of set of incoming edges of individual vertices.

Parameters:v – (Graph.Vertex) Vertex of graph.
Raises:AssertionError – When v is neither a Graph.Vertex object nor an iterable of Graph.Vertex objects.
out_neighbors(v: Union[Graph.Vertex, Iterable[Graph.Vertex]])[source]

Returns an iterator over targets of incoming edges to given vertex or vertices. In case of vertices, the iterator is defined over the union of set of incoming edges of individual vertices.

Parameters:v – (Graph.Vertex) Vertex of graph.
Raises:AssertionError – When v is neither a Graph.Vertex object nor an iterable of Graph.Vertex objects.
rm_edge(e: iglsynth.util.graph.Graph.Edge)[source]

Removes an edge from the graph. An attempt to remove a non-existing edge will be ignored, with a warning.

Parameters:e – (Graph.Edge) Edge to be removed.
rm_edges(ebunch: Iterable[Graph.Edge])[source]

Removes a bunch of edges from the graph. An attempt to remove a non-existing edge will be ignored, with a warning.

Parameters:ebunch – (Iterable over Graph.Edge) Edges to be removed.
rm_vertex(v: iglsynth.util.graph.Graph.Vertex)[source]

Removes a vertex from the graph. An attempt to remove a non-existing vertex will be ignored, with a warning.

Parameters:v – (Graph.Vertex) Vertex to be removed.
rm_vertices(vbunch: Iterable[Graph.Vertex])[source]

Removes a bunch of vertices from the graph. An attempt to remove a non-existing vertex will be ignored, with a warning.

Parameters:vbunch – (Iterable over Graph.Vertex) Vertices to be removed.
vertices

Returns an iterator over vertices in graph.