Python networkx.lexicographical_topological_sort() Examples
The following are 10
code examples of networkx.lexicographical_topological_sort().
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: 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 #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: _graphs.py From tmppy with Apache License 2.0 | 5 votes |
def compute_condensation_in_topological_order(dependency_graph: nx.DiGraph, sort_by = lambda x: x): if not dependency_graph.number_of_nodes(): return condensed_graph = nx.condensation(dependency_graph) assert isinstance(condensed_graph, nx.DiGraph) for connected_component_index in nx.lexicographical_topological_sort(condensed_graph, key=sort_by): yield list(sorted(condensed_graph.nodes[connected_component_index]['members'], key=sort_by))
Example #4
Source File: _recalculate_template_instantiation_can_trigger_static_asserts_info.py From tmppy with Apache License 2.0 | 5 votes |
def recalculate_template_instantiation_can_trigger_static_asserts_info(header: ir.Header): if not header.template_defns: return header template_defn_by_name = {template_defn.name: template_defn for template_defn in header.template_defns} template_defn_dependency_graph = compute_template_dependency_graph(header.template_defns, template_defn_by_name) condensed_graph = nx.condensation(template_defn_dependency_graph) assert isinstance(condensed_graph, nx.DiGraph) template_defn_dependency_graph_transitive_closure = nx.transitive_closure(template_defn_dependency_graph) assert isinstance(template_defn_dependency_graph_transitive_closure, nx.DiGraph) # Determine which connected components can trigger static assert errors. condensed_node_can_trigger_static_asserts = defaultdict(lambda: False) for connected_component_index in reversed(list(nx.lexicographical_topological_sort(condensed_graph))): condensed_node = condensed_graph.nodes[connected_component_index] # If a template defn in this connected component can trigger a static assert, the whole component can. for template_defn_name in condensed_node['members']: if _template_defn_contains_static_assert_stmt(template_defn_by_name[template_defn_name]): condensed_node_can_trigger_static_asserts[connected_component_index] = True # If a template defn in this connected component references a template defn in a connected component that can # trigger static asserts, this connected component can also trigger them. for called_condensed_node_index in condensed_graph.successors(connected_component_index): if condensed_node_can_trigger_static_asserts[called_condensed_node_index]: condensed_node_can_trigger_static_asserts[connected_component_index] = True template_defn_can_trigger_static_asserts = dict() for connected_component_index in condensed_graph: for template_defn_name in condensed_graph.nodes[connected_component_index]['members']: template_defn_can_trigger_static_asserts[template_defn_name] = condensed_node_can_trigger_static_asserts[connected_component_index] return _apply_template_instantiation_can_trigger_static_asserts_info(header, template_defn_can_trigger_static_asserts)
Example #5
Source File: networkx_dagcircuit.py From qiskit-terra with Apache License 2.0 | 5 votes |
def topological_nodes(self): """ Yield nodes in topological order. Returns: generator(DAGNode): node in topological order """ def _key(x): return str(self._id_to_node[x].qargs) return (self._id_to_node[idx] for idx in nx.lexicographical_topological_sort( self._multi_graph, key=_key))
Example #6
Source File: test_dag.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_topological_sort6(self): for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]: def runtime_error(): DG = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) first = True for x in algorithm(DG): if first: first = False DG.add_edge(5 - x, 5) def unfeasible_error(): DG = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) first = True for x in algorithm(DG): if first: first = False DG.remove_node(4) def runtime_error2(): DG = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) first = True for x in algorithm(DG): if first: first = False DG.remove_node(2) assert_raises(RuntimeError, runtime_error) assert_raises(RuntimeError, runtime_error2) assert_raises(nx.NetworkXUnfeasible, unfeasible_error)
Example #7
Source File: test_dag.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_lexicographical_topological_sort(self): G = nx.DiGraph([(1, 2), (2, 3), (1, 4), (1, 5), (2, 6)]) assert_equal(list(nx.lexicographical_topological_sort(G)), [1, 2, 3, 4, 5, 6]) assert_equal(list(nx.lexicographical_topological_sort( G, key=lambda x: x)), [1, 2, 3, 4, 5, 6]) assert_equal(list(nx.lexicographical_topological_sort( G, key=lambda x: -x)), [1, 5, 4, 2, 6, 3])
Example #8
Source File: test_dag.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_topological_sort6(self): for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]: def runtime_error(): DG = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) first = True for x in algorithm(DG): if first: first = False DG.add_edge(5 - x, 5) def unfeasible_error(): DG = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) first = True for x in algorithm(DG): if first: first = False DG.remove_node(4) def runtime_error2(): DG = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) first = True for x in algorithm(DG): if first: first = False DG.remove_node(2) assert_raises(RuntimeError, runtime_error) assert_raises(RuntimeError, runtime_error2) assert_raises(nx.NetworkXUnfeasible, unfeasible_error)
Example #9
Source File: test_dag.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_lexicographical_topological_sort(self): G = nx.DiGraph([(1, 2), (2, 3), (1, 4), (1, 5), (2, 6)]) assert_equal(list(nx.lexicographical_topological_sort(G)), [1, 2, 3, 4, 5, 6]) assert_equal(list(nx.lexicographical_topological_sort( G, key=lambda x: x)), [1, 2, 3, 4, 5, 6]) assert_equal(list(nx.lexicographical_topological_sort( G, key=lambda x: -x)), [1, 5, 4, 2, 6, 3])
Example #10
Source File: _recalculate_function_can_throw_info.py From tmppy with Apache License 2.0 | 4 votes |
def recalculate_function_can_throw_info(module: ir.Module, context_object_file_content: ObjectFileContent): if not module.function_defns: return module function_dependency_graph = nx.DiGraph() function_defn_by_name = {function_defn.name: function_defn for function_defn in module.function_defns} for function_defn in module.function_defns: function_dependency_graph.add_node(function_defn.name) for global_function_name in get_referenced_global_function_names(function_defn): if global_function_name in function_defn_by_name.keys(): function_dependency_graph.add_edge(function_defn.name, global_function_name) condensed_graph = nx.condensation(function_dependency_graph) assert isinstance(condensed_graph, nx.DiGraph) function_dependency_graph_transitive_closure = nx.transitive_closure(function_dependency_graph) assert isinstance(function_dependency_graph_transitive_closure, nx.DiGraph) # Determine which connected components can throw. condensed_node_can_throw = defaultdict(lambda: False) for connected_component_index in reversed(list(nx.lexicographical_topological_sort(condensed_graph))): condensed_node = condensed_graph.nodes[connected_component_index] # If a function in this connected component can throw, the whole component can throw. for function_name in condensed_node['members']: if function_contains_raise_stmt(function_defn_by_name[function_name]): condensed_node_can_throw[connected_component_index] = True # If a function in this connected component calls a function in a connected component that can throw, this # connected component can also throw. for called_condensed_node_index in condensed_graph.successors(connected_component_index): if condensed_node_can_throw[called_condensed_node_index]: condensed_node_can_throw[connected_component_index] = True function_can_throw = dict() for connected_component_index in condensed_graph: for function_name in condensed_graph.nodes[connected_component_index]['members']: function_can_throw[function_name] = condensed_node_can_throw[connected_component_index] external_function_can_throw = dict() for module_name, module_info in context_object_file_content.modules_by_name.items(): for elem in itertools.chain(module_info.ir2_module.custom_types, module_info.ir2_module.function_defns): if elem.name in module_info.ir2_module.public_names: external_function_can_throw[(module_name, elem.name)] = (isinstance(elem, ir.FunctionDefn) and (function_contains_raise_stmt(elem) or function_contains_var_reference_that_can_throw(elem))) return apply_function_can_throw_info(module, function_can_throw, external_function_can_throw)