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

Add member functions remove_vertex_if and remove_edge_if to Graph

This provides a more convenient way of deleting vertices or edges
according to a simple criterion.
parent 0896d088
......@@ -320,6 +320,16 @@ class Graph(object):
self.__graph.ShiftVertexProperty(pmap[1]().get_map(), index)
self.__graph.RemoveVertex(vertex)
@_handle_exceptions
def remove_vertex_if(self, predicate):
"""Remove all the vertices from the graph for which predicate(v)
evaluates to True."""
N = self.num_vertices()
for i in xrange(0, N):
v = self.vertex(N - i - 1)
if predicate(v):
self.remove_vertex(v)
@_handle_exceptions
def add_edge(self, source, target):
"""Add a new edge from 'source' to 'target' to the graph, and return
......@@ -331,6 +341,18 @@ class Graph(object):
"""Remove an edge from the graph."""
self.__graph.RemoveEdge(edge)
@_handle_exceptions
def remove_edge_if(self, predicate):
"""Remove all the edges from the graph for which predicate(e) evaluates
to True."""
for v in self.vertices():
del_es = []
for e in v.out_edges():
if predicate(e):
del_es.append(e)
for e in del_es:
self.remove_edge(e)
@_handle_exceptions
def clear(self):
"""Remove all vertices and edges from the graph."""
......
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