当前位置: 首页>>代码示例>>Python>>正文


Python networkx.lexicographical_topological_sort方法代码示例

本文整理汇总了Python中networkx.lexicographical_topological_sort方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.lexicographical_topological_sort方法的具体用法?Python networkx.lexicographical_topological_sort怎么用?Python networkx.lexicographical_topological_sort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx的用法示例。


在下文中一共展示了networkx.lexicographical_topological_sort方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_topological_sort1

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import lexicographical_topological_sort [as 别名]
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)) 
开发者ID:holzschu,项目名称:Carnets,代码行数:25,代码来源:test_dag.py

示例2: compute_condensation_in_topological_order

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import lexicographical_topological_sort [as 别名]
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)) 
开发者ID:google,项目名称:tmppy,代码行数:11,代码来源:_graphs.py

示例3: recalculate_template_instantiation_can_trigger_static_asserts_info

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import lexicographical_topological_sort [as 别名]
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) 
开发者ID:google,项目名称:tmppy,代码行数:38,代码来源:_recalculate_template_instantiation_can_trigger_static_asserts_info.py

示例4: topological_nodes

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import lexicographical_topological_sort [as 别名]
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)) 
开发者ID:Qiskit,项目名称:qiskit-terra,代码行数:16,代码来源:networkx_dagcircuit.py

示例5: test_topological_sort6

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import lexicographical_topological_sort [as 别名]
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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:32,代码来源:test_dag.py

示例6: test_lexicographical_topological_sort

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import lexicographical_topological_sort [as 别名]
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]) 
开发者ID:holzschu,项目名称:Carnets,代码行数:12,代码来源:test_dag.py

示例7: recalculate_function_can_throw_info

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import lexicographical_topological_sort [as 别名]
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) 
开发者ID:google,项目名称:tmppy,代码行数:54,代码来源:_recalculate_function_can_throw_info.py


注:本文中的networkx.lexicographical_topological_sort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。