Python networkx.find_cycle() Examples
The following are 30
code examples of networkx.find_cycle().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: deployment_group_manager.py From shipyard with Apache License 2.0 | 6 votes |
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)) )
Example #2
Source File: resource.py From vivarium with GNU General Public License v3.0 | 6 votes |
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
Example #3
Source File: compute_build_graph.py From conda-concourse-ci with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #4
Source File: test_cycles.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
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_)
Example #5
Source File: run_hooks_hook.py From flask-unchained with MIT License | 6 votes |
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
Example #6
Source File: compute_build_graph.py From staged-recipes with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #7
Source File: test_cycles.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
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_)
Example #8
Source File: test_cycles.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_multidigraph_original(self): # Node 2 doesn't need to be searched again from visited from 4. # The goal here is to cover the case when 2 to be researched from 4, # when 4 is visited from the first time (so we must make sure that 4 # is not visited from 2, and hence, we respect the edge orientation). G = nx.MultiDiGraph([(0, 1), (1, 2), (2, 3), (4, 2)]) assert_raises(nx.exception.NetworkXNoCycle, find_cycle, G, [0, 1, 2, 3, 4], orientation='original')
Example #9
Source File: test_cycles.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_digraph_ignore(self): G = nx.DiGraph(self.edges) x = list(find_cycle(G, self.nodes, orientation='ignore')) x_ = [(0, 1, FORWARD), (1, 0, FORWARD)] assert_equal(x, x_)
Example #10
Source File: test_cycles.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_dag(self): G = nx.DiGraph([(0, 1), (0, 2), (1, 2)]) assert_raises(nx.exception.NetworkXNoCycle, find_cycle, G, orientation='original') x = list(find_cycle(G, orientation='ignore')) assert_equal(x, [(0, 1, FORWARD), (1, 2, FORWARD), (0, 2, REVERSE)])
Example #11
Source File: test_cycles.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_no_cycle(self): # https://github.com/networkx/networkx/issues/2439 G = nx.DiGraph() G.add_edges_from([(1, 2), (2, 0), (3, 1), (3, 2)]) assert_raises(nx.NetworkXNoCycle, find_cycle, G, source=0) assert_raises(nx.NetworkXNoCycle, find_cycle, G)
Example #12
Source File: dag.py From sos with BSD 3-Clause "New" or "Revised" License | 5 votes |
def circular_dependencies(self): if 'DAG' in env.config['SOS_DEBUG'] or 'ALL' in env.config['SOS_DEBUG']: env.log_to_file('DAG', 'check circular') try: return nx.find_cycle(self) except Exception: # if there is no cycle, an exception is given return []
Example #13
Source File: test_cycles.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_graph(self): G = nx.Graph(self.edges) assert_raises(nx.exception.NetworkXNoCycle, find_cycle, G, self.nodes)
Example #14
Source File: test_cycles.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
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_)
Example #15
Source File: test_cycles.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_multigraph(self): G = nx.MultiGraph(self.edges) x = list(find_cycle(G, self.nodes)) x_ = [(0, 1, 0), (1, 0, 1)] # or (1, 0, 2) # Hash randomization...could be any edge. assert_equal(x[0], x_[0]) assert_equal(x[1][:2], x_[1][:2])
Example #16
Source File: test_cycles.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_multidigraph(self): G = nx.MultiDiGraph(self.edges) x = list(find_cycle(G, self.nodes)) x_ = [(0, 1, 0), (1, 0, 0)] # (1, 0, 1) assert_equal(x[0], x_[0]) assert_equal(x[1][:2], x_[1][:2])
Example #17
Source File: test_cycles.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_digraph_ignore(self): G = nx.DiGraph(self.edges) x = list(find_cycle(G, self.nodes, orientation='ignore')) x_ = [(0, 1, FORWARD), (1, 0, FORWARD)] assert_equal(x, x_)
Example #18
Source File: test_cycles.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_multidigraph_ignore2(self): # Loop traversed an edge while ignoring its orientation. G = nx.MultiDiGraph([(0, 1), (1, 2), (1, 2)]) x = list(find_cycle(G, [0, 1, 2], orientation='ignore')) x_ = [(1, 2, 0, FORWARD), (1, 2, 1, REVERSE)] assert_equal(x, x_)
Example #19
Source File: test_cycles.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_multidigraph_ignore2(self): # Node 2 doesn't need to be searched again from visited from 4. # The goal here is to cover the case when 2 to be researched from 4, # when 4 is visited from the first time (so we must make sure that 4 # is not visited from 2, and hence, we respect the edge orientation). G = nx.MultiDiGraph([(0, 1), (1, 2), (2, 3), (4, 2)]) assert_raises(nx.exception.NetworkXNoCycle, find_cycle, G, [0, 1, 2, 3, 4], orientation='original')
Example #20
Source File: test_cycles.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_dag(self): G = nx.DiGraph([(0, 1), (0, 2), (1, 2)]) assert_raises(nx.exception.NetworkXNoCycle, find_cycle, G, orientation='original') x = list(find_cycle(G, orientation='ignore')) assert_equal(x, [(0, 1, FORWARD), (1, 2, FORWARD), (0, 2, REVERSE)])
Example #21
Source File: test_cycles.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_no_cycle(self): # https://github.com/networkx/networkx/issues/2439 G = nx.DiGraph() G.add_edges_from([(1, 2), (2, 0), (3, 1), (3, 2)]) assert_raises(nx.NetworkXNoCycle, find_cycle, G, source=0) assert_raises(nx.NetworkXNoCycle, find_cycle, G)
Example #22
Source File: test_cycles.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_digraph_reverse(self): G = nx.DiGraph(self.edges) x = list(find_cycle(G, self.nodes, orientation='reverse')) x_ = [(1, 0, REVERSE), (0, 1, REVERSE)] assert_equal(x, x_)
Example #23
Source File: dependency_resolver.py From cfn-sphere with Apache License 2.0 | 5 votes |
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
Example #24
Source File: register_extensions_hook.py From flask-unchained with MIT License | 5 votes |
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
Example #25
Source File: graphing.py From orquesta with Apache License 2.0 | 5 votes |
def get_cycles(self): return [ {"tasks": sorted(c), "route": nx.find_cycle(self._graph, c)} for c in nx.simple_cycles(self._graph) ]
Example #26
Source File: graph.py From pyBN with MIT License | 5 votes |
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
Example #27
Source File: test_cycles.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_graph_nocycle(self): G = nx.Graph(self.edges) assert_raises(nx.exception.NetworkXNoCycle, find_cycle, G, self.nodes)
Example #28
Source File: test_cycles.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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_)
Example #29
Source File: test_cycles.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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_)
Example #30
Source File: test_cycles.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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_)