def live_subgraph(self): # type: () -> nx.MultiDiGraph
"""
Returns the graph of "live" states for this graph, i.e. the start state
together with states that may be involved in positively matching a string
(reachable from the start node and an ancestor of an accepting node).
This is intended for display purposes, only showing the paths which
might lead to an accepting state, or just the start state if no such
paths exist.
"""
graph = self.as_multidigraph
accepting_states = {
node for node in graph.node if graph.node[node]['accepting']
}
# Add a "sink" node with an in-edge from every accepting state. This is
# is solely done because the networkx API makes it easier to find the
# ancestor of a node than a set of nodes.
sink = object()
graph.add_node(sink)
for state in accepting_states:
graph.add_edge(state, sink)
live_states = {self.start} | (nx.ancestors(graph, sink) & nx.descendants(graph, self.start))
return graph.subgraph(live_states)
评论列表
文章目录