def find_cycle(G, source=None, orientation=None):
"""Returns a cycle found via depth-first traversal.
The cycle is a list of edges indicating the cyclic path.
Orientation of directed edges is controlled by `orientation`.
Parameters
----------
G : graph
A directed/undirected graph/multigraph.
source : node, list of nodes
The node from which the traversal begins. If None, then a source
is chosen arbitrarily and repeatedly until all edges from each node in
the graph are searched.
orientation : None | 'original' | 'reverse' | 'ignore' (default: None)
For directed graphs and directed multigraphs, edge traversals need not
respect the original orientation of the edges.
When set to 'reverse' every edge is traversed in the reverse direction.
When set to 'ignore', every edge is treated as undirected.
When set to 'original', every edge is treated as directed.
In all three cases, the yielded edge tuples add a last entry to
indicate the direction in which that edge was traversed.
If orientation is None, the yielded edge has no direction indicated.
The direction is respected, but not reported.
Returns
-------
edges : directed edges
A list of directed edges indicating the path taken for the loop.
If no cycle is found, then an exception is raised.
For graphs, an edge is of the form `(u, v)` where `u` and `v`
are the tail and head of the edge as determined by the traversal.
For multigraphs, an edge is of the form `(u, v, key)`, where `key` is
the key of the edge. When the graph is directed, then `u` and `v`
are always in the order of the actual directed edge.
If orientation is not None then the edge tuple is extended to include
the direction of traversal ('forward' or 'reverse') on that edge.
Raises
------
NetworkXNoCycle
If no cycle was found.
Examples
--------
In this example, we construct a DAG and find, in the first call, that there
are no directed cycles, and so an exception is raised. In the second call,
we ignore edge orientations and find that there is an undirected cycle.
Note that the second call finds a directed cycle while effectively
traversing an undirected graph, and so, we found an "undirected cycle".
This means that this DAG structure does not form a directed tree (which
is also known as a polytree).
>>> import networkx as nx
>>> G = nx.DiGraph([(0, 1), (0, 2), (1, 2)])
>>> try:
... nx.find_cycle(G, orientation='original')
... except:
... pass
...
>>> list(nx.find_cycle(G, orientation='ignore'))
[(0, 1, 'forward'), (1, 2, 'forward'), (0, 2, 'reverse')]
"""
if not G.is_directed() or orientation in (None, 'original'):
def tailhead(edge):
return edge[:2]
elif orientation == 'reverse':
def tailhead(edge):
return edge[1], edge[0]
elif orientation == 'ignore':
def tailhead(edge):
if edge[-1] == 'reverse':
return edge[1], edge[0]
return edge[:2]
explored = set()
cycle = []
final_node = None
for start_node in G.nbunch_iter(source):
if start_node in explored:
# No loop is possible.
continue
edges = []
# All nodes seen in this iteration of edge_dfs
seen = {start_node}
# Nodes in active path.
active_nodes = {start_node}
previous_head = None
for edge in nx.edge_dfs(G, start_node, orientation):
# Determine if this edge is a continuation of the active path.
tail, head = tailhead(edge)
if head in explored:
# Then we've already explored it. No loop is possible.
continue
if previous_head is not None and tail != previous_head:
# This edge results from backtracking.
# Pop until we get a node whose head equals the current tail.
# So for example, we might have:
# (0, 1), (1, 2), (2, 3), (1, 4)
# which must become:
# (0, 1), (1, 4)
while True:
try:
popped_edge = edges.pop()
except IndexError:
edges = []
active_nodes = {tail}
break
else:
popped_head = tailhead(popped_edge)[1]
active_nodes.remove(popped_head)
if edges:
last_head = tailhead(edges[-1])[1]
if tail == last_head:
break
edges.append(edge)
if head in active_nodes:
# We have a loop!
cycle.extend(edges)
final_node = head
break
else:
seen.add(head)
active_nodes.add(head)
previous_head = head
if cycle:
break
else:
explored.update(seen)
else:
assert(len(cycle) == 0)
raise nx.exception.NetworkXNoCycle('No cycle found.')
# We now have a list of edges which ends on a cycle.
# So we need to remove from the beginning edges that are not relevant.
for i, edge in enumerate(cycle):
tail, head = tailhead(edge)
if tail == final_node:
break
return cycle[i:]
find_loop_cycle
猜你喜欢
转载自blog.csdn.net/sinat_15355869/article/details/82870209
今日推荐
周排行