Python networkx.NetworkXUnfeasible() Examples
The following are 30
code examples of networkx.NetworkXUnfeasible().
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: degree_seq.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def phase3(self): # build potential remaining edges and choose with rejection sampling potential_edges = combinations(self.remaining_degree, 2) # build auxilliary graph of potential edges not already in graph H = nx.Graph([(u,v) for (u,v) in potential_edges if not self.graph.has_edge(u,v)]) while self.remaining_degree: if not self.suitable_edge(): raise nx.NetworkXUnfeasible('no suitable edges left') while True: u,v = sorted(random.choice(H.edges())) if random.random() < self.q(u,v): break if random.random() < self.p(u,v): # accept edge self.graph.add_edge(u,v) self.update_remaining(u,v, aux_graph=H)
Example #2
Source File: test_dag.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_topological_sort1(self): DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)]) for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]: assert_equal(tuple(algorithm(DG)), (1, 2, 3)) DG.add_edge(3, 2) for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]: assert_raises(nx.NetworkXUnfeasible, consume, algorithm(DG)) DG.remove_edge(2, 3) for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]: assert_equal(tuple(algorithm(DG)), (1, 3, 2)) DG.remove_edge(3, 2) assert_in(tuple(nx.topological_sort(DG)), {(1, 2, 3), (1, 3, 2)}) assert_equal(tuple(nx.lexicographical_topological_sort(DG)), (1, 2, 3))
Example #3
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 #4
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 #5
Source File: __init__.py From bioconda-utils with MIT License | 6 votes |
def __init__(self, config: Dict, recipe_folder: str, exclude: List[str] = None, nocatch: bool=False) ->None: self.config = config self.recipe_folder = recipe_folder self.skip = self.load_skips() self.exclude = exclude or [] self.nocatch = nocatch self._messages = [] dag = nx.DiGraph() dag.add_nodes_from(str(check) for check in get_checks()) dag.add_edges_from( (str(check), str(check_dep)) for check in get_checks() for check_dep in check.requires ) self.checks_dag = dag try: self.checks_ordered = nx.topological_sort(dag, reverse=True) except nx.NetworkXUnfeasible: raise RunTimeError("Cycle in LintCheck requirements!") self.reload_checks()
Example #6
Source File: test_dag.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_topological_sort3(self): DG = nx.DiGraph() DG.add_edges_from([(1, i) for i in range(2, 5)]) DG.add_edges_from([(2, i) for i in range(5, 9)]) DG.add_edges_from([(6, i) for i in range(9, 12)]) DG.add_edges_from([(4, i) for i in range(12, 15)]) def validate(order): ok_(isinstance(order, list)) assert_equal(set(order), set(DG)) for u, v in combinations(order, 2): assert_false(nx.has_path(DG, v, u)) validate(list(nx.topological_sort(DG))) DG.add_edge(14, 1) assert_raises(nx.NetworkXUnfeasible, consume, nx.topological_sort(DG))
Example #7
Source File: nx_edge_augmentation.py From ibeis with Apache License 2.0 | 6 votes |
def bridge_augmentation(G, avail=None, weight=None): """Finds the a set of edges that bridge connects G. Adding these edges to G will make it 2-edge-connected. If no constraints are specified the returned set of edges is minimum an optimal, otherwise the solution is approximated. Notes ----- If there are no constraints the solution can be computed in linear time using :func:`unconstrained_bridge_augmentation`. Otherwise, the problem becomes NP-hard and is the solution is approximated by :func:`weighted_bridge_augmentation`. """ if G.number_of_nodes() < 3: raise nx.NetworkXUnfeasible( 'impossible to bridge connect less than 3 nodes') if avail is None: return unconstrained_bridge_augmentation(G) else: return weighted_bridge_augmentation(G, avail, weight=weight) # --- Algorithms and Helpers ---
Example #8
Source File: dag.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def is_directed_acyclic_graph(G): """Return True if the graph `G` is a directed acyclic graph (DAG) or False if not. Parameters ---------- G : NetworkX graph Returns ------- bool True if `G` is a DAG, False otherwise """ if not G.is_directed(): return False try: consume(topological_sort(G)) return True except nx.NetworkXUnfeasible: return False
Example #9
Source File: degree_seq.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def phase3(self): # build potential remaining edges and choose with rejection sampling potential_edges = combinations(self.remaining_degree, 2) # build auxiliary graph of potential edges not already in graph H = nx.Graph([(u, v) for (u, v) in potential_edges if not self.graph.has_edge(u, v)]) rng = self.rng while self.remaining_degree: if not self.suitable_edge(): raise nx.NetworkXUnfeasible('no suitable edges left') while True: u, v = sorted(rng.choice(list(H.edges()))) if rng.random() < self.q(u, v): break if rng.random() < self.p(u, v): # accept edge self.graph.add_edge(u, v) self.update_remaining(u, v, aux_graph=H)
Example #10
Source File: test_dag.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_all_topological_sorts_3(self): def unfeasible(): DG = nx.DiGraph([(1, 2), (2, 3), (3, 4), (4, 2), (4, 5)]) # convert to list to execute generator list(nx.all_topological_sorts(DG)) def not_implemented(): G = nx.Graph([(1, 2), (2, 3)]) # convert to list to execute generator list(nx.all_topological_sorts(G)) def not_implemted_2(): G = nx.MultiGraph([(1, 2), (1, 2), (2, 3)]) list(nx.all_topological_sorts(G)) assert_raises(nx.NetworkXUnfeasible, unfeasible) assert_raises(nx.NetworkXNotImplemented, not_implemented) assert_raises(nx.NetworkXNotImplemented, not_implemted_2)
Example #11
Source File: test_dag.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_topological_sort3(self): DG = nx.DiGraph() DG.add_edges_from([(1, i) for i in range(2, 5)]) DG.add_edges_from([(2, i) for i in range(5, 9)]) DG.add_edges_from([(6, i) for i in range(9, 12)]) DG.add_edges_from([(4, i) for i in range(12, 15)]) def validate(order): ok_(isinstance(order, list)) assert_equal(set(order), set(DG)) for u, v in combinations(order, 2): assert_false(nx.has_path(DG, v, u)) validate(list(nx.topological_sort(DG))) DG.add_edge(14, 1) assert_raises(nx.NetworkXUnfeasible, consume, nx.topological_sort(DG))
Example #12
Source File: dag.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def is_directed_acyclic_graph(G): """Return True if the graph G is a directed acyclic graph (DAG) or False if not. Parameters ---------- G : NetworkX graph A graph Returns ------- is_dag : bool True if G is a DAG, false otherwise """ if not G.is_directed(): return False try: topological_sort(G, reverse=True) return True except nx.NetworkXUnfeasible: return False
Example #13
Source File: test_dag.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_topological_sort1(self): DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)]) for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]: assert_equal(tuple(algorithm(DG)), (1, 2, 3)) DG.add_edge(3, 2) for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]: assert_raises(nx.NetworkXUnfeasible, consume, algorithm(DG)) DG.remove_edge(2, 3) for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]: assert_equal(tuple(algorithm(DG)), (1, 3, 2)) DG.remove_edge(3, 2) assert_in(tuple(nx.topological_sort(DG)), {(1, 2, 3), (1, 3, 2)}) assert_equal(tuple(nx.lexicographical_topological_sort(DG)), (1, 2, 3))
Example #14
Source File: test_dag.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_reverse_topological_sort1(self): DG = nx.DiGraph() DG.add_edges_from([(1, 2), (1, 3), (2, 3)]) assert_equal(nx.topological_sort(DG, reverse=True), [3, 2, 1]) assert_equal( nx.topological_sort_recursive(DG, reverse=True), [3, 2, 1]) DG.add_edge(3, 2) assert_raises(nx.NetworkXUnfeasible, nx.topological_sort, DG, reverse=True) assert_raises(nx.NetworkXUnfeasible, nx.topological_sort_recursive, DG, reverse=True) DG.remove_edge(2, 3) assert_equal(nx.topological_sort(DG, reverse=True), [2, 3, 1]) assert_equal( nx.topological_sort_recursive(DG, reverse=True), [2, 3, 1])
Example #15
Source File: test_dag.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_topological_sort3(self): DG = nx.DiGraph() DG.add_edges_from([(1, i) for i in range(2, 5)]) DG.add_edges_from([(2, i) for i in range(5, 9)]) DG.add_edges_from([(6, i) for i in range(9, 12)]) DG.add_edges_from([(4, i) for i in range(12, 15)]) def validate(order): ok_(isinstance(order, list)) assert_equal(set(order), set(DG)) for u, v in combinations(order, 2): assert_false(nx.has_path(DG, v, u)) validate(nx.topological_sort_recursive(DG)) validate(nx.topological_sort(DG)) DG.add_edge(14, 1) assert_raises(nx.NetworkXUnfeasible, nx.topological_sort, DG) assert_raises(nx.NetworkXUnfeasible, nx.topological_sort_recursive, DG)
Example #16
Source File: test_mis.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_exception(self): """Bad input should raise exception.""" G = self.florentine assert_raises(nx.NetworkXUnfeasible, nx.maximal_independent_set, G, ["Smith"]) assert_raises(nx.NetworkXUnfeasible, nx.maximal_independent_set, G, ["Salviati", "Pazzi"])
Example #17
Source File: test_mincost.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_sum_demands_not_zero(self): G = nx.DiGraph() G.add_node('s', demand = -5) G.add_node('t', demand = 4) G.add_edge('s', 'a', weight = 1, capacity = 3) G.add_edge('a', 'b', weight = 3) G.add_edge('a', 'c', weight = -6) G.add_edge('b', 'd', weight = 1) G.add_edge('c', 'd', weight = -2) G.add_edge('d', 't', weight = 1, capacity = 3) assert_raises(nx.NetworkXUnfeasible, nx.network_simplex, G) assert_raises(nx.NetworkXUnfeasible, nx.capacity_scaling, G)
Example #18
Source File: test_mincost.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_no_flow_satisfying_demands(self): G = nx.DiGraph() G.add_node('s', demand = -5) G.add_node('t', demand = 5) G.add_edge('s', 'a', weight = 1, capacity = 3) G.add_edge('a', 'b', weight = 3) G.add_edge('a', 'c', weight = -6) G.add_edge('b', 'd', weight = 1) G.add_edge('c', 'd', weight = -2) G.add_edge('d', 't', weight = 1, capacity = 3) assert_raises(nx.NetworkXUnfeasible, nx.network_simplex, G) assert_raises(nx.NetworkXUnfeasible, nx.capacity_scaling, G)
Example #19
Source File: test_mincost.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_exceptions(self): G = nx.Graph() assert_raises(nx.NetworkXNotImplemented, nx.network_simplex, G) assert_raises(nx.NetworkXNotImplemented, nx.capacity_scaling, G) G = nx.MultiGraph() assert_raises(nx.NetworkXNotImplemented, nx.network_simplex, G) assert_raises(nx.NetworkXNotImplemented, nx.capacity_scaling, G) G = nx.DiGraph() assert_raises(nx.NetworkXError, nx.network_simplex, G) assert_raises(nx.NetworkXError, nx.capacity_scaling, G) G.add_node(0, demand=float('inf')) assert_raises(nx.NetworkXError, nx.network_simplex, G) assert_raises(nx.NetworkXUnfeasible, nx.capacity_scaling, G) G.nodes[0]['demand'] = 0 G.add_node(1, demand=0) G.add_edge(0, 1, weight=-float('inf')) assert_raises(nx.NetworkXError, nx.network_simplex, G) assert_raises(nx.NetworkXUnfeasible, nx.capacity_scaling, G) G[0][1]['weight'] = 0 G.add_edge(0, 0, weight=float('inf')) assert_raises(nx.NetworkXError, nx.network_simplex, G) #assert_raises(nx.NetworkXError, nx.capacity_scaling, G) G[0][0]['weight'] = 0 G[0][1]['capacity'] = -1 assert_raises(nx.NetworkXUnfeasible, nx.network_simplex, G) #assert_raises(nx.NetworkXUnfeasible, nx.capacity_scaling, G) G[0][1]['capacity'] = 0 G[0][0]['capacity'] = -1 assert_raises(nx.NetworkXUnfeasible, nx.network_simplex, G) #assert_raises(nx.NetworkXUnfeasible, nx.capacity_scaling, G)
Example #20
Source File: test_dag.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_topological_sort2(self): DG = nx.DiGraph({1: [2], 2: [3], 3: [4], 4: [5], 5: [1], 11: [12], 12: [13], 13: [14], 14: [15]}) assert_raises(nx.NetworkXUnfeasible, consume, nx.topological_sort(DG)) assert_false(nx.is_directed_acyclic_graph(DG)) DG.remove_edge(1, 2) consume(nx.topological_sort(DG)) assert_true(nx.is_directed_acyclic_graph(DG))
Example #21
Source File: test_mincost.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_negcycle_infcap(self): G = nx.DiGraph() G.add_node('s', demand = -5) G.add_node('t', demand = 5) G.add_edge('s', 'a', weight = 1, capacity = 3) G.add_edge('a', 'b', weight = 3) G.add_edge('c', 'a', weight = -6) G.add_edge('b', 'd', weight = 1) G.add_edge('d', 'c', weight = -2) G.add_edge('d', 't', weight = 1, capacity = 3) assert_raises(nx.NetworkXUnfeasible, nx.network_simplex, G) assert_raises(nx.NetworkXUnbounded, nx.capacity_scaling, G)
Example #22
Source File: test_mis.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_exception(self): """Bad input should raise exception.""" G = self.florentine assert_raises(nx.NetworkXUnfeasible, nx.maximal_independent_set, G, ["Smith"]) assert_raises(nx.NetworkXUnfeasible, nx.maximal_independent_set, G, ["Salviati", "Pazzi"])
Example #23
Source File: degree_seq.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def __init__(self, degree, seed=None): if not nx.is_graphical(degree): raise nx.NetworkXUnfeasible('degree sequence is not graphical') if seed is not None: random.seed(seed) self.degree = list(degree) # node labels are integers 0,...,n-1 self.m = sum(self.degree) / 2.0 # number of edges try: self.dmax = max(self.degree) # maximum degree except ValueError: self.dmax = 0
Example #24
Source File: dag.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def has_cycle(G): """Decides whether the directed graph has a cycle.""" try: consume(topological_sort(G)) except nx.NetworkXUnfeasible: return True else: return False
Example #25
Source File: graphical.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _basic_graphical_tests(deg_sequence): # Sort and perform some simple tests on the sequence if not nx.utils.is_list_of_ints(deg_sequence): # check for a type that can be converted to int. Like numpy.int64 ds = [] for d in deg_sequence: try: intd = int(d) except ValueError: raise nx.NetworkXError("Invalid type in deg_sequence: not an integer") if intd != d: raise nx.NetworkXError("Invalid type in deg_sequence: not an integer") ds.append(intd) deg_sequence = ds p = len(deg_sequence) num_degs = [0] * p dmax, dmin, dsum, n = 0, p, 0, 0 for d in deg_sequence: # Reject if degree is negative or larger than the sequence length if d < 0 or d >= p: raise nx.NetworkXUnfeasible # Process only the non-zero integers elif d > 0: dmax, dmin, dsum, n = max(dmax, d), min(dmin, d), dsum + d, n + 1 num_degs[d] += 1 # Reject sequence if it has odd sum or is oversaturated if dsum % 2 or dsum > n * (n - 1): raise nx.NetworkXUnfeasible return dmax, dmin, dsum, n, num_degs
Example #26
Source File: test_mincost.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_deadend(self): """Check if one-node cycles are handled properly. Taken from ticket #2906 from @sshraven.""" G = nx.DiGraph() G.add_nodes_from(range(5), demand=0) G.node[4]['demand'] = -13 G.node[3]['demand'] = 13 G.add_edges_from([(0,2), (0, 3), (2, 1)], capacity=20, weight=0.1) assert_raises(nx.NetworkXUnfeasible, nx.min_cost_flow, G)
Example #27
Source File: test_mincost.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_no_flow_satisfying_demands(self): G = nx.DiGraph() G.add_node('s', demand=-5) G.add_node('t', demand=5) G.add_edge('s', 'a', weight=1, capacity=3) G.add_edge('a', 'b', weight=3) G.add_edge('a', 'c', weight=-6) G.add_edge('b', 'd', weight=1) G.add_edge('c', 'd', weight=-2) G.add_edge('d', 't', weight=1, capacity=3) assert_raises(nx.NetworkXUnfeasible, nx.network_simplex, G) assert_raises(nx.NetworkXUnfeasible, nx.capacity_scaling, G)
Example #28
Source File: test_degree_seq.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_random_degree_sequence_graph_raise(): z = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4] assert_raises(nx.NetworkXUnfeasible, nx.random_degree_sequence_graph, z)
Example #29
Source File: test_mincost.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_sum_demands_not_zero(self): G = nx.DiGraph() G.add_node('s', demand=-5) G.add_node('t', demand=4) G.add_edge('s', 'a', weight=1, capacity=3) G.add_edge('a', 'b', weight=3) G.add_edge('a', 'c', weight=-6) G.add_edge('b', 'd', weight=1) G.add_edge('c', 'd', weight=-2) G.add_edge('d', 't', weight=1, capacity=3) assert_raises(nx.NetworkXUnfeasible, nx.network_simplex, G) assert_raises(nx.NetworkXUnfeasible, nx.capacity_scaling, G)
Example #30
Source File: test_dag.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_antichains(self): antichains = nx.algorithms.dag.antichains G = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) solution = [[], [4], [3], [2], [1]] self._check_antichains(list(antichains(G)), solution) G = nx.DiGraph([(1, 2), (2, 3), (2, 4), (3, 5), (5, 6), (5, 7)]) solution = [[], [4], [7], [7, 4], [6], [6, 4], [6, 7], [6, 7, 4], [5], [5, 4], [3], [3, 4], [2], [1]] self._check_antichains(list(antichains(G)), solution) G = nx.DiGraph([(1, 2), (1, 3), (3, 4), (3, 5), (5, 6)]) solution = [[], [6], [5], [4], [4, 6], [4, 5], [3], [2], [2, 6], [2, 5], [2, 4], [2, 4, 6], [2, 4, 5], [2, 3], [1]] self._check_antichains(list(antichains(G)), solution) G = nx.DiGraph({0: [1, 2], 1: [4], 2: [3], 3: [4]}) solution = [[], [4], [3], [2], [1], [1, 3], [1, 2], [0]] self._check_antichains(list(antichains(G)), solution) G = nx.DiGraph() self._check_antichains(list(antichains(G)), [[]]) G = nx.DiGraph() G.add_nodes_from([0, 1, 2]) solution = [[], [0], [1], [1, 0], [2], [2, 0], [2, 1], [2, 1, 0]] self._check_antichains(list(antichains(G)), solution) def f(x): return list(antichains(x)) G = nx.Graph([(1, 2), (2, 3), (3, 4)]) assert_raises(nx.NetworkXNotImplemented, f, G) G = nx.DiGraph([(1, 2), (2, 3), (3, 1)]) assert_raises(nx.NetworkXUnfeasible, f, G)