text
stringlengths
0
828
continue
for child in self.children(vertex):
if child not in visited:
visited.add(child)
to_visit.append((child, depth+1))
return self.full_subgraph(visited)"
623,"def ancestors(self, start, generations=None):
""""""
Return the subgraph of all nodes from which the given vertex is
reachable, including that vertex.
If specified, the optional `generations` argument specifies how
many generations to limit to.
""""""
visited = self.vertex_set()
visited.add(start)
to_visit = deque([(start, 0)])
while to_visit:
vertex, depth = to_visit.popleft()
if depth == generations:
continue
for parent in self.parents(vertex):
if parent not in visited:
visited.add(parent)
to_visit.append((parent, depth+1))
return self.full_subgraph(visited)"
624,"def _component_graph(self):
""""""
Compute the graph of strongly connected components.
Each strongly connected component is itself represented as a list of
pairs, giving information not only about the vertices belonging to
this strongly connected component, but also the edges leading from
this strongly connected component to other components.
Each pair is of the form ('EDGE', v) or ('VERTEX', v) for some vertex
v. In the first case, that indicates that there's an edge from this
strongly connected component to the given vertex v (which will belong
to another component); in the second, it indicates that v is a member
of this strongly connected component.
Each component will begin with a vertex (the *root* vertex of the
strongly connected component); the following edges are edges from that
vertex.
Algorithm is based on that described in ""Path-based depth-first search
for strong and biconnected components"" by Harold N. Gabow,
Inf.Process.Lett. 74 (2000) 107--114.
""""""
sccs = []
stack = []
boundaries = []
identified = self.vertex_set()
index = self.vertex_dict()
to_do = []
def visit_vertex(v):
index[v] = len(stack)
stack.append(('VERTEX', v))
boundaries.append(index[v])
to_do.append((leave_vertex, v))
to_do.extend((visit_edge, w) for w in self.children(v))
def visit_edge(v):
if v in identified:
stack.append(('EDGE', v))
elif v in index:
while index[v] < boundaries[-1]:
boundaries.pop()
else:
to_do.append((visit_vertex, v))
def leave_vertex(v):
if boundaries[-1] == index[v]:
root = boundaries.pop()
scc = stack[root:]
del stack[root:]
for item_type, w in scc:
if item_type == 'VERTEX':
identified.add(w)
del index[w]
sccs.append(scc)
stack.append(('EDGE', v))
# Visit every vertex of the graph.
for v in self.vertices:
if v not in identified:
to_do.append((visit_vertex, v))
while to_do:
operation, v = to_do.pop()
operation(v)
stack.pop()
return sccs"
625,"def source_components(self):
""""""
Return the strongly connected components not reachable from any other
component. Any component in the graph is reachable from one of these.