Python networkx.contracted_nodes() Examples

The following are 18 code examples of networkx.contracted_nodes(). 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: nltk2graph.py    From ccg2lambda with Apache License 2.0 6 votes vote down vote up
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 
Example #2
Source File: test_minors.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
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) 
Example #3
Source File: test_minors.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #4
Source File: test_minors.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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) 
Example #5
Source File: nltk2graph.py    From ccg2lambda with Apache License 2.0 5 votes vote down vote up
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 
Example #6
Source File: test_minors.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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) 
Example #7
Source File: test_minors.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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) 
Example #8
Source File: test_minors.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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)) 
Example #9
Source File: test_minors.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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)) 
Example #10
Source File: test_minors.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #11
Source File: test_minors.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #12
Source File: test_minors.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #13
Source File: test_minors.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)) 
Example #14
Source File: test_minors.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)) 
Example #15
Source File: test_minors.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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)) 
Example #16
Source File: test_minors.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #17
Source File: test_minors.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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)) 
Example #18
Source File: test_minors.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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))