def test_graph(self):
     g1 = nx.Graph()
     g2 = nx.Graph()
     g1.add_edges_from(self.g1edges)
     g2.add_edges_from(self.g2edges)
     gm = iso.ISMAGS(g1, g2)
     assert gm.is_isomorphic()
Example #2
0
    def test_edgecase_self_isomorphism(self):
        """
        This edgecase is one of the cases in which it is hard to find all
        symmetry elements.
        """
        graph = nx.Graph()
        nx.add_path(graph, range(5))
        graph.add_edges_from([(2, 5), (5, 6)])

        ismags = iso.ISMAGS(graph, graph)
        ismags_answer = list(ismags.find_isomorphisms(True))
        assert ismags_answer == [{n: n for n in graph.nodes}]

        graph = nx.relabel_nodes(graph, {0: 0, 1: 1, 2: 2, 3: 3, 4: 6, 5: 4, 6: 5})
        ismags = iso.ISMAGS(graph, graph)
        ismags_answer = list(ismags.find_isomorphisms(True))
        assert ismags_answer == [{n: n for n in graph.nodes}]
Example #3
0
    def test_isomorphism(self):
        g1 = nx.Graph()
        nx.add_cycle(g1, range(4))

        g2 = nx.Graph()
        nx.add_cycle(g2, range(4))
        g2.add_edges_from([(n, m) for n, m in zip(g2, range(4, 8))])
        ismags = iso.ISMAGS(g2, g1)
        assert (list(ismags.subgraph_isomorphisms_iter(symmetry=True)) ==
                [{n: n for n in g1.nodes}])
Example #4
0
    def test_isomorphism2(self):
        g1 = nx.Graph()
        nx.add_path(g1, range(3))

        g2 = g1.copy()
        g2.add_edge(1, 3)

        ismags = iso.ISMAGS(g2, g1)
        matches = ismags.subgraph_isomorphisms_iter(symmetry=True)
        expected_symmetric = [
            {
                0: 0,
                1: 1,
                2: 2
            },
            {
                0: 0,
                1: 1,
                3: 2
            },
            {
                2: 0,
                1: 1,
                3: 2
            },
        ]
        assert _matches_to_sets(matches) == _matches_to_sets(
            expected_symmetric)

        matches = ismags.subgraph_isomorphisms_iter(symmetry=False)
        expected_asymmetric = [
            {
                0: 2,
                1: 1,
                2: 0
            },
            {
                0: 2,
                1: 1,
                3: 0
            },
            {
                2: 2,
                1: 1,
                3: 0
            },
        ]
        assert _matches_to_sets(matches) == _matches_to_sets(
            expected_symmetric + expected_asymmetric)
Example #5
0
    def test_self_isomorphism(self):
        """
        For some small, symmetric graphs, make sure that 1) they are isomorphic
        to themselves, and 2) that only the identity mapping is found.
        """
        for node_data, edge_data in self.data:
            graph = nx.Graph()
            graph.add_nodes_from(node_data)
            graph.add_edges_from(edge_data)

            ismags = iso.ISMAGS(graph, graph, node_match=iso.categorical_node_match('name', None))
            assert ismags.is_isomorphic()
            assert ismags.subgraph_is_isomorphic()
            assert (list(ismags.subgraph_isomorphisms_iter(symmetry=True)) ==
                    [{n: n for n in graph.nodes}])
Example #6
0
    def test_labeled_edges(self):
        g1 = nx.Graph()
        nx.add_cycle(g1, range(3))
        g1.edges[1, 2]['attr'] = True

        g2 = g1.copy()
        g2.add_edge(1, 3)
        ismags = iso.ISMAGS(g2, g1, edge_match=lambda x, y: x == y)
        matches = ismags.subgraph_isomorphisms_iter(symmetry=True)
        expected_symmetric = [{0: 0, 1: 1, 2: 2}]
        assert (_matches_to_sets(matches) ==
                _matches_to_sets(expected_symmetric))

        matches = ismags.subgraph_isomorphisms_iter(symmetry=False)
        expected_asymmetric = [{1: 2, 0: 0, 2: 1}]
        assert (_matches_to_sets(matches) ==
                _matches_to_sets(expected_symmetric + expected_asymmetric))
Example #7
0
    def test_labeled_nodes(self):
        g1 = nx.Graph()
        g1.add_cycle(range(3))
        g1.nodes[1]['attr'] = True

        g2 = g1.copy()
        g2.add_edge(1, 3)
        ismags = iso.ISMAGS(g2, g1, node_match=lambda x, y: x == y)
        matches = ismags.subgraph_isomorphisms_iter(symmetry=True)
        expected_symmetric = [{0: 0, 1: 1, 2: 2}]
        assert_equal(_matches_to_sets(matches),
                     _matches_to_sets(expected_symmetric))

        matches = ismags.subgraph_isomorphisms_iter(symmetry=False)
        expected_asymmetric = [{0: 2, 1: 1, 2: 0}]
        assert_equal(
            _matches_to_sets(matches),
            _matches_to_sets(expected_symmetric + expected_asymmetric))
Example #8
0
    def test_symmetry_mcis(self):
        graph1 = nx.Graph()
        graph1.add_path(range(4))

        graph2 = nx.Graph()
        graph2.add_path(range(3))
        graph2.add_edge(1, 3)

        # Only the symmetry of graph2 is taken into account here.
        ismags1 = iso.ISMAGS(graph1,
                             graph2,
                             node_match=iso.categorical_node_match(
                                 'color', None))
        assert_equal(list(ismags1.subgraph_isomorphisms_iter(True)), [])
        found_mcis = _matches_to_sets(ismags1.largest_common_subgraph())
        expected = _matches_to_sets([{0: 0, 1: 1, 2: 2}, {1: 0, 3: 2, 2: 1}])
        assert_equal(expected, found_mcis)

        # Only the symmetry of graph1 is taken into account here.
        ismags2 = iso.ISMAGS(graph2,
                             graph1,
                             node_match=iso.categorical_node_match(
                                 'color', None))
        assert_equal(list(ismags2.subgraph_isomorphisms_iter(True)), [])
        found_mcis = _matches_to_sets(ismags2.largest_common_subgraph())
        expected = _matches_to_sets([{
            3: 2,
            0: 0,
            1: 1
        }, {
            2: 0,
            0: 2,
            1: 1
        }, {
            3: 0,
            0: 2,
            1: 1
        }, {
            3: 0,
            1: 1,
            2: 2
        }, {
            0: 0,
            1: 1,
            2: 2
        }, {
            2: 0,
            3: 2,
            1: 1
        }])

        assert_equal(expected, found_mcis)

        found_mcis1 = _matches_to_sets(ismags1.largest_common_subgraph(False))
        found_mcis2 = ismags2.largest_common_subgraph(False)
        found_mcis2 = [{v: k for k, v in d.items()} for d in found_mcis2]
        found_mcis2 = _matches_to_sets(found_mcis2)

        expected = _matches_to_sets([{
            3: 2,
            1: 3,
            2: 1
        }, {
            2: 0,
            0: 2,
            1: 1
        }, {
            1: 2,
            3: 3,
            2: 1
        }, {
            3: 0,
            1: 3,
            2: 1
        }, {
            0: 2,
            2: 3,
            1: 1
        }, {
            3: 0,
            1: 2,
            2: 1
        }, {
            2: 0,
            0: 3,
            1: 1
        }, {
            0: 0,
            2: 3,
            1: 1
        }, {
            1: 0,
            3: 3,
            2: 1
        }, {
            1: 0,
            3: 2,
            2: 1
        }, {
            0: 3,
            1: 1,
            2: 2
        }, {
            0: 0,
            1: 1,
            2: 2
        }])
        assert_equal(expected, found_mcis1)
        assert_equal(expected, found_mcis2)