本文整理汇总了Python中networkx.find_cycle方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.find_cycle方法的具体用法?Python networkx.find_cycle怎么用?Python networkx.find_cycle使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.find_cycle方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: order_build
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def order_build(graph):
'''
Assumes that packages are in graph.
Builds a temporary graph of relevant nodes and returns it topological sort.
Relevant nodes selected in a breadth first traversal sourced at each pkg
in packages.
'''
reorder_cyclical_test_dependencies(graph)
try:
order = list(nx.topological_sort(graph))
order.reverse()
except nx.exception.NetworkXUnfeasible:
raise ValueError("Cycles detected in graph: %s", nx.find_cycle(graph,
orientation='reverse'))
return order
示例2: sorted_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def sorted_nodes(self):
"""Returns a topological sort of the resource graph.
Notes
-----
Topological sorts are not stable. Be wary of depending on order
where you shouldn't.
"""
if self._sorted_nodes is None:
try:
self._sorted_nodes = list(nx.algorithms.topological_sort(self.graph))
except nx.NetworkXUnfeasible:
raise ResourceError(f'The resource pool contains at least one cycle: '
f'{nx.find_cycle(self.graph)}.')
return self._sorted_nodes
示例3: order_build
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def order_build(graph):
'''
Assumes that packages are in graph.
Builds a temporary graph of relevant nodes and returns it topological sort.
Relevant nodes selected in a breadth first traversal sourced at each pkg
in packages.
'''
reorder_cyclical_test_dependencies(graph)
try:
order = list(nx.topological_sort(graph))
order.reverse()
except nx.exception.NetworkXUnfeasible:
raise ValueError("Cycles detected in graph: %s", nx.find_cycle(graph))
return order
示例4: resolve_hook_order
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def resolve_hook_order(self, hook_tuples: List[HookTuple]) -> List[HookTuple]:
dag = nx.DiGraph()
for hook_tuple in hook_tuples:
dag.add_node(hook_tuple.Hook.name, hook_tuple=hook_tuple)
for dep_name in hook_tuple.Hook.run_after:
dag.add_edge(hook_tuple.Hook.name, dep_name)
for successor_name in hook_tuple.Hook.run_before:
dag.add_edge(successor_name, hook_tuple.Hook.name)
try:
order = reversed(list(nx.topological_sort(dag)))
except nx.NetworkXUnfeasible:
msg = 'Circular dependency detected between hooks'
problem_graph = ', '.join(f'{a} -> {b}'
for a, b in nx.find_cycle(dag))
raise Exception(f'{msg}: {problem_graph}')
rv = []
for hook_name in order:
hook_tuple = dag.nodes[hook_name].get('hook_tuple')
if hook_tuple:
rv.append(hook_tuple)
return rv
示例5: test_prev_explored
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def test_prev_explored(self):
# https://github.com/networkx/networkx/issues/2323
G = nx.DiGraph()
G.add_edges_from([(1, 0), (2, 0), (1, 2), (2, 1)])
assert_raises(nx.NetworkXNoCycle, find_cycle, G, source=0)
x = list(nx.find_cycle(G, 1))
x_ = [(1, 2), (2, 1)]
assert_equal(x, x_)
x = list(nx.find_cycle(G, 2))
x_ = [(2, 1), (1, 2)]
assert_equal(x, x_)
x = list(nx.find_cycle(G))
x_ = [(1, 2), (2, 1)]
assert_equal(x, x_)
示例6: _detect_cycles
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def _detect_cycles(graph):
"""Detect if there are cycles between the groups
Raise a DeploymentGroupCycleError if there are any circular
dependencies
"""
LOG.debug("Detecting cycles in graph")
circ_deps = []
try:
circ_deps = list(nx.find_cycle(graph))
except nx.NetworkXNoCycle:
LOG.info('There are no cycles detected in the graph')
pass
if circ_deps:
involved_nodes = set()
# a value in this list is like: ('group1', 'group2')
for dep in circ_deps:
involved_nodes.update(dep)
raise DeploymentGroupCycleError(
"The following are involved in a circular dependency:"
" {}".format(", ".join(involved_nodes))
)
示例7: analyse_cyclic_dependencies
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def analyse_cyclic_dependencies(graph):
try:
cycle = networkx.find_cycle(graph)
dependency_string = ' => '.join("[%s is referenced by %s]" % tup for tup in cycle)
raise CyclicDependencyException("Found cyclic dependency between stacks: {0}".format(dependency_string))
except NetworkXNoCycle:
pass
示例8: resolve_extension_order
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def resolve_extension_order(self,
extensions: Dict[str, object],
) -> List[ExtensionTuple]:
extension_tuples = []
for name, extension in extensions.items():
dependencies = []
if isinstance(extension, (list, tuple)):
extension, dependencies = extension
extension_tuples.append(ExtensionTuple(name, extension, dependencies))
dag = nx.DiGraph()
for ext in extension_tuples:
dag.add_node(ext.name, extension_tuple=ext)
for dep_name in ext.dependencies:
dag.add_edge(ext.name, dep_name)
try:
extension_order = reversed(list(nx.topological_sort(dag)))
except nx.NetworkXUnfeasible:
msg = 'Circular dependency detected between extensions'
problem_graph = ', '.join(f'{a} -> {b}'
for a, b in nx.find_cycle(dag))
raise Exception(f'{msg}: {problem_graph}')
rv = []
for ext_name in extension_order:
try:
rv.append(dag.nodes[ext_name]['extension_tuple'])
except KeyError as e:
if 'extension_tuple' not in str(e):
raise e
raise Exception(
f'Could not locate an extension with the name {ext_name!r}')
return rv
示例9: get_cycles
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def get_cycles(self):
return [
{"tasks": sorted(c), "route": nx.find_cycle(self._graph, c)}
for c in nx.simple_cycles(self._graph)
]
示例10: would_cause_cycle
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def would_cause_cycle(e, u, v, reverse=False):
"""
Test if adding the edge u -> v to the BayesNet
object would create a DIRECTED (i.e. illegal) cycle.
"""
G = nx.DiGraph(e)
if reverse:
G.remove_edge(v,u)
G.add_edge(u,v)
try:
nx.find_cycle(G, source=u)
return True
except:
return False
示例11: test_graph_nocycle
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def test_graph_nocycle(self):
G = nx.Graph(self.edges)
assert_raises(nx.exception.NetworkXNoCycle, find_cycle, G, self.nodes)
示例12: test_graph_cycle
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def test_graph_cycle(self):
G = nx.Graph(self.edges)
G.add_edge(2, 0)
x = list(find_cycle(G, self.nodes))
x_ = [(0, 1), (1, 2), (2, 0)]
assert_equal(x, x_)
示例13: test_graph_orientation_none
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def test_graph_orientation_none(self):
G = nx.Graph(self.edges)
G.add_edge(2, 0)
x = list(find_cycle(G, self.nodes, orientation=None))
x_ = [(0, 1), (1, 2), (2, 0)]
assert_equal(x, x_)
示例14: test_graph_orientation_original
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def test_graph_orientation_original(self):
G = nx.Graph(self.edges)
G.add_edge(2, 0)
x = list(find_cycle(G, self.nodes, orientation='original'))
x_ = [(0, 1, FORWARD), (1, 2, FORWARD), (2, 0, FORWARD)]
assert_equal(x, x_)
示例15: test_digraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import find_cycle [as 别名]
def test_digraph(self):
G = nx.DiGraph(self.edges)
x = list(find_cycle(G, self.nodes))
x_ = [(0, 1), (1, 0)]
assert_equal(x, x_)