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


Python networkx.contracted_nodes方法代码示例

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


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

示例1: merge_leaf_nodes

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import contracted_nodes [as 别名]
def merge_leaf_nodes(graph, head_node=None, quant_active=None, quant_scope=None):
    """
    Traverses the graph, merging those nodes
    with the same label within the same quantificaton scope.
    """
    if head_node is None:
        head_node = graph.graph['head_node']
    # Get nodes and their scope information.
    scoped_nodes = get_scoped_nodes(graph, head_node)
    for (quant_node, expr), nodes_to_merge in scoped_nodes.items():
        if len(nodes_to_merge) > 1:
            master_node, node_type = nodes_to_merge[0]
            assert node_type == 'leaf'
            for node, node_type in nodes_to_merge[1:]:
                # Merge leaves within the same scope:
                if node_type == 'leaf':
                    graph = nx.contracted_nodes(graph, master_node, node)
                # Add edges from quantifier to internal function names:
                elif node_type == 'internal':
                    graph.add_edge(quant_node, node)
    graph.graph['head_node'] = head_node
    return graph 
开发者ID:mynlp,项目名称:ccg2lambda,代码行数:24,代码来源:nltk2graph.py

示例2: test_node_attributes

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import contracted_nodes [as 别名]
def test_node_attributes(self):
        """Tests that node contraction preserves node attributes."""
        G = nx.cycle_graph(4)
        # Add some data to the two nodes being contracted.
        G.nodes[0]['foo'] = 'bar'
        G.nodes[1]['baz'] = 'xyzzy'
        actual = nx.contracted_nodes(G, 0, 1)
        # We expect that contracting the nodes 0 and 1 in C_4 yields K_3, but
        # with nodes labeled 0, 2, and 3, and with a self-loop on 0.
        expected = nx.complete_graph(3)
        expected = nx.relabel_nodes(expected, {1: 2, 2: 3})
        expected.add_edge(0, 0)
        cdict = {1: {'baz': 'xyzzy'}}
        expected.nodes[0].update(dict(foo='bar', contraction=cdict))
        assert_true(nx.is_isomorphic(actual, expected))
        assert_equal(actual.nodes, expected.nodes) 
开发者ID:holzschu,项目名称:Carnets,代码行数:18,代码来源:test_minors.py

示例3: arrange_quantifiers

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import contracted_nodes [as 别名]
def arrange_quantifiers(graph):
    """
    Collapse all quantifier instances into nodes attached
    to the root.
    """
    head_node = guess_head_node(graph)
    quant_nodes = [n for n in graph.nodes() if is_quantifier_node(graph, n)]
    for qn in quant_nodes:
        # from pudb import set_trace; set_trace()
        pred_not_quant = find_predecessor_not_quant(graph, qn)
        term_node = get_term_node_from_quant(graph, qn)
        graph.add_edge(pred_not_quant, term_node)
        graph.remove_edge(qn, term_node)

        for pred in list(graph.predecessors(qn)):
            graph.remove_edge(pred, qn)
        graph.add_edge(head_node, qn)

    q_dict = defaultdict(list)
    for qn in quant_nodes:
        q_dict[get_label(graph, qn)].append(qn)
    for qn_type, nodes in q_dict.items():
        if len(nodes) > 1:
            master_quant_node = nodes[0]
            for qn in nodes[1:]:
                graph = nx.contracted_nodes(graph, master_quant_node, qn)
    return graph 
开发者ID:mynlp,项目名称:ccg2lambda,代码行数:29,代码来源:nltk2graph.py

示例4: test_undirected_node_contraction

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import contracted_nodes [as 别名]
def test_undirected_node_contraction(self):
        """Tests for node contraction in an undirected graph."""
        G = nx.cycle_graph(4)
        actual = nx.contracted_nodes(G, 0, 1)
        expected = nx.complete_graph(3)
        expected.add_edge(0, 0)
        assert_true(nx.is_isomorphic(actual, expected)) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:9,代码来源:test_minors.py

示例5: test_directed_node_contraction

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import contracted_nodes [as 别名]
def test_directed_node_contraction(self):
        """Tests for node contraction in a directed graph."""
        G = nx.DiGraph(nx.cycle_graph(4))
        actual = nx.contracted_nodes(G, 0, 1)
        expected = nx.DiGraph(nx.complete_graph(3))
        expected.add_edge(0, 0)
        expected.add_edge(0, 0)
        assert_true(nx.is_isomorphic(actual, expected)) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:10,代码来源:test_minors.py

示例6: test_node_attributes

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import contracted_nodes [as 别名]
def test_node_attributes(self):
        """Tests that node contraction preserves node attributes."""
        G = nx.cycle_graph(4)
        # Add some data to the two nodes being contracted.
        G.node[0] = dict(foo='bar')
        G.node[1] = dict(baz='xyzzy')
        actual = nx.contracted_nodes(G, 0, 1)
        # We expect that contracting the nodes 0 and 1 in C_4 yields K_3, but
        # with nodes labeled 0, 2, and 3, and with a self-loop on 0.
        expected = nx.complete_graph(3)
        expected = nx.relabel_nodes(expected, {1: 2, 2: 3})
        expected.add_edge(0, 0)
        expected.node[0] = dict(foo='bar', contraction={1: dict(baz='xyzzy')})
        assert_true(nx.is_isomorphic(actual, expected))
        assert_equal(actual.node, expected.node) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:17,代码来源:test_minors.py

示例7: test_without_self_loops

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import contracted_nodes [as 别名]
def test_without_self_loops(self):
        """Tests for node contraction without preserving self-loops."""
        G = nx.cycle_graph(4)
        actual = nx.contracted_nodes(G, 0, 1, self_loops=False)
        expected = nx.complete_graph(3)
        assert_true(nx.is_isomorphic(actual, expected)) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:8,代码来源:test_minors.py

示例8: test_create_multigraph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import contracted_nodes [as 别名]
def test_create_multigraph(self):
        """Tests that using a MultiGraph creates multiple edges."""
        G = nx.path_graph(3, create_using=nx.MultiGraph())
        G.add_edge(0, 1)
        G.add_edge(0, 0)
        G.add_edge(0, 2)
        actual = nx.contracted_nodes(G, 0, 2)
        expected = nx.MultiGraph()
        expected.add_edge(0, 1)
        expected.add_edge(0, 1)
        expected.add_edge(0, 1)
        expected.add_edge(0, 0)
        expected.add_edge(0, 0)
        assert_edges_equal(actual.edges, expected.edges) 
开发者ID:holzschu,项目名称:Carnets,代码行数:16,代码来源:test_minors.py

示例9: test_multigraph_keys

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import contracted_nodes [as 别名]
def test_multigraph_keys(self):
        """Tests that multiedge keys are reset in new graph."""
        G = nx.path_graph(3, create_using=nx.MultiGraph())
        G.add_edge(0, 1, 5)
        G.add_edge(0, 0, 0)
        G.add_edge(0, 2, 5)
        actual = nx.contracted_nodes(G, 0, 2)
        expected = nx.MultiGraph()
        expected.add_edge(0, 1, 0)
        expected.add_edge(0, 1, 5)
        expected.add_edge(0, 1, 2)  # keyed as 2 b/c 2 edges already in G
        expected.add_edge(0, 0, 0)
        expected.add_edge(0, 0, 1)  # this comes from (0, 2, 5)
        assert_edges_equal(actual.edges, expected.edges) 
开发者ID:holzschu,项目名称:Carnets,代码行数:16,代码来源:test_minors.py

示例10: test_contract_selfloop_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import contracted_nodes [as 别名]
def test_contract_selfloop_graph(self):
        """Tests for node contraction when nodes have selfloops."""
        G = nx.cycle_graph(4)
        G.add_edge(0, 0)
        actual = nx.contracted_nodes(G, 0, 1)
        expected = nx.complete_graph([0, 2, 3])
        expected.add_edge(0, 0)
        expected.add_edge(0, 0)
        assert_edges_equal(actual.edges, expected.edges)
        actual = nx.contracted_nodes(G, 1, 0)
        expected = nx.complete_graph([1, 2, 3])
        expected.add_edge(1, 1)
        expected.add_edge(1, 1)
        assert_edges_equal(actual.edges, expected.edges) 
开发者ID:holzschu,项目名称:Carnets,代码行数:16,代码来源:test_minors.py


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