Commit f4edaefc authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Add 'new' optional parameter to Graph.edge() for automatic edge creation

parent ed9fa3ea
...@@ -1430,13 +1430,17 @@ class Graph(object): ...@@ -1430,13 +1430,17 @@ class Graph(object):
return None return None
return v return v
def edge(self, s, t, all_edges=False): def edge(self, s, t, all_edges=False, new=False):
"""Return the edge from vertex ``s`` to ``t``, if it exists. If """Return the edge from vertex ``s`` to ``t``, if it exists. If
``all_edges=True`` then a list is returned with all the parallel edges ``all_edges=True`` then a list is returned with all the parallel edges
from ``s`` to ``t``, otherwise only one edge is returned. from ``s`` to ``t``, otherwise only one edge is returned.
If ``new == True``, a new edge is created and returned, if none
currently exists.
This operation will take :math:`O(k(s))` time, where :math:`k(s)` is the This operation will take :math:`O(k(s))` time, where :math:`k(s)` is the
out-degree of vertex :math:`s`. out-degree of vertex :math:`s`.
""" """
s = self.vertex(int(s)) s = self.vertex(int(s))
t = self.vertex(int(t)) t = self.vertex(int(t))
...@@ -1451,6 +1455,8 @@ class Graph(object): ...@@ -1451,6 +1455,8 @@ class Graph(object):
if not all_edges: if not all_edges:
return e return e
edges.append(e) edges.append(e)
if new and len(edges) == 0:
edges.append(self.add_edge(s, t))
if all_edges: if all_edges:
return edges return edges
return None return None
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment