示例#1
0
 def test_decoding2(self):
     # Example from "An Optimal Algorithm for Prufer Codes".
     sequence = [2, 4, 0, 1, 3, 3]
     tree = nx.from_prufer_sequence(sequence)
     assert_nodes_equal(list(tree), list(range(8)))
     edges = [(0, 1), (0, 4), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)]
     assert_edges_equal(list(tree.edges()), edges)
示例#2
0
    def test_graph(self):
        g = nx.cycle_graph(10)
        G = nx.Graph()
        G.add_nodes_from(g)
        G.add_weighted_edges_from((u, v, u) for u, v in g.edges())

        # Dict of dicts
        dod = to_dict_of_dicts(G)
        GG = from_dict_of_dicts(dod, create_using=nx.Graph())
        assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
        assert_edges_equal(sorted(G.edges()), sorted(GG.edges()))
        GW = to_networkx_graph(dod, create_using=nx.Graph())
        assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
        assert_edges_equal(sorted(G.edges()), sorted(GW.edges()))
        GI = nx.Graph(dod)
        assert_equal(sorted(G.nodes()), sorted(GI.nodes()))
        assert_equal(sorted(G.edges()), sorted(GI.edges()))

        # Dict of lists
        dol = to_dict_of_lists(G)
        GG = from_dict_of_lists(dol, create_using=nx.Graph())
        # dict of lists throws away edge data so set it to none
        enone = [(u, v, {}) for (u, v, d) in G.edges(data=True)]
        assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
        assert_edges_equal(enone, sorted(GG.edges(data=True)))
        GW = to_networkx_graph(dol, create_using=nx.Graph())
        assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
        assert_edges_equal(enone, sorted(GW.edges(data=True)))
        GI = nx.Graph(dol)
        assert_nodes_equal(sorted(G.nodes()), sorted(GI.nodes()))
        assert_edges_equal(enone, sorted(GI.edges(data=True)))
示例#3
0
 def test_path_projected_graph(self):
     G=nx.path_graph(4)
     P=bipartite.projected_graph(G, [1, 3])
     assert_nodes_equal(list(P), [1, 3])
     assert_edges_equal(list(P.edges()), [(1, 3)])
     P=bipartite.projected_graph(G, [0, 2])
     assert_nodes_equal(list(P), [0, 2])
     assert_edges_equal(list(P.edges()), [(0, 2)])
示例#4
0
 def test_multiline_adjlist_delimiter(self):
     fh = io.BytesIO()
     G = nx.path_graph(3)
     nx.write_multiline_adjlist(G, fh, delimiter=':')
     fh.seek(0)
     H = nx.read_multiline_adjlist(fh, nodetype=int, delimiter=':')
     assert_nodes_equal(list(H), list(G))
     assert_edges_equal(list(H.edges()), list(G.edges()))
示例#5
0
 def test_decoding(self):
     """Tests for decoding a tree from a Prüfer sequence."""
     # Example from Wikipedia.
     sequence = [3, 3, 3, 4]
     tree = nx.from_prufer_sequence(sequence)
     assert_nodes_equal(list(tree), list(range(6)))
     edges = [(0, 3), (1, 3), (2, 3), (3, 4), (4, 5)]
     assert_edges_equal(list(tree.edges()), edges)
示例#6
0
 def test_reverse_hashable(self):
     class Foo(object):
         pass
     x = Foo()
     y = Foo()
     G = nx.DiGraph()
     G.add_edge(x, y)
     assert_nodes_equal(G.nodes(), G.reverse().nodes())
     assert_equal([(y, x)], list(G.reverse().edges()))
示例#7
0
    def test_complete_2_partite_graph(self):
        """Tests that the complete 2-partite graph is the complete bipartite
        graph.

        """
        G = nx.complete_multipartite_graph(2, 3)
        H = nx.complete_bipartite_graph(2, 3)
        assert_nodes_equal(G, H)
        assert_edges_equal(G.edges(), H.edges())
示例#8
0
    def assert_equal(self, G, data=False):
        (fd, fname) = tempfile.mkstemp()
        nx.write_yaml(G, fname)
        Gin = nx.read_yaml(fname)

        assert_nodes_equal(list(G), list(Gin))
        assert_edges_equal(G.edges(data=data), Gin.edges(data=data))

        os.close(fd)
        os.unlink(fname)
示例#9
0
 def test_path_weighted_projected_graph(self):
     G=nx.path_graph(4)
     P=bipartite.weighted_projected_graph(G,[1,3])
     assert_nodes_equal(list(P),[1,3])
     assert_edges_equal(list(P.edges()),[(1,3)])
     P[1][3]['weight']=1
     P=bipartite.weighted_projected_graph(G,[0,2])
     assert_nodes_equal(list(P),[0,2])
     assert_edges_equal(list(P.edges()),[(0,2)])
     P[0][2]['weight']=1
示例#10
0
    def test_single(self):
        """Tests that joining just one tree yields a tree with one more
        node.

        """
        T = nx.empty_graph(1)
        actual = nx.join([(T, 0)])
        expected = nx.path_graph(2)
        assert_nodes_equal(list(expected), list(actual))
        assert_edges_equal(list(expected.edges()), list(actual.edges()))
示例#11
0
 def test_path_collaboration_projected_graph(self):
     G=nx.path_graph(4)
     P=bipartite.collaboration_weighted_projected_graph(G,[1,3])
     assert_nodes_equal(P.nodes(),[1,3])
     assert_edges_equal(P.edges(),[(1,3)])
     P[1][3]['weight']=1
     P=bipartite.collaboration_weighted_projected_graph(G,[0,2])
     assert_nodes_equal(P.nodes(),[0,2])
     assert_edges_equal(P.edges(),[(0,2)])
     P[0][2]['weight']=1
示例#12
0
 def test_multiline_adjlist_integers(self):
     (fd, fname) = tempfile.mkstemp()
     G = nx.convert_node_labels_to_integers(self.G)
     nx.write_multiline_adjlist(G, fname)
     H = nx.read_multiline_adjlist(fname, nodetype=int)
     H2 = nx.read_multiline_adjlist(fname, nodetype=int)
     assert_nodes_equal(list(H), list(G))
     assert_edges_equal(list(H.edges()), list(G.edges()))
     os.close(fd)
     os.unlink(fname)
示例#13
0
 def test_attributes(self):
     G = nx.Graph()
     G.add_edge(1, 2, weight=1, color='red', distance=7)
     G.add_edge(2, 3, weight=1, color='green', distance=2)
     G.add_edge(1, 3, weight=10, color='blue', distance=1)
     G.graph['foo'] = 'bar'
     T = nx.minimum_spanning_tree(G, algorithm=self.algo)
     assert_equal(T.graph, G.graph)
     assert_nodes_equal(T, G)
     for u, v in T.edges():
         assert_equal(T.adj[u][v], G.adj[u][v])
示例#14
0
 def test_edgelist_multigraph(self):
     G=self.MG
     (fd,fname)=tempfile.mkstemp()
     bipartite.write_edgelist(G,fname) 
     H=bipartite.read_edgelist(fname,nodetype=int,create_using=nx.MultiGraph())
     H2=bipartite.read_edgelist(fname,nodetype=int,create_using=nx.MultiGraph())
     assert_not_equal(H,H2) # they should be different graphs
     assert_nodes_equal(H.nodes(),G.nodes())
     assert_edges_equal(H.edges(),G.edges())
     os.close(fd)
     os.unlink(fname)
示例#15
0
 def test_path_weighted_projected_directed_graph(self):
     G = nx.DiGraph()
     G.add_path(list(range(4)))
     P = bipartite.weighted_projected_graph(G, [1, 3])
     assert_nodes_equal(P.nodes(), [1, 3])
     assert_edges_equal(P.edges(), [(1, 3)])
     P[1][3]["weight"] = 1
     P = bipartite.weighted_projected_graph(G, [0, 2])
     assert_nodes_equal(P.nodes(), [0, 2])
     assert_edges_equal(P.edges(), [(0, 2)])
     P[0][2]["weight"] = 1
示例#16
0
 def test_directed_path_collaboration_projected_graph(self):
     G=nx.DiGraph()
     G.add_path(list(range(4)))
     P=bipartite.collaboration_weighted_projected_graph(G,[1,3])
     assert_nodes_equal(list(P),[1,3])
     assert_edges_equal(list(P.edges()),[(1,3)])
     P[1][3]['weight']=1
     P=bipartite.collaboration_weighted_projected_graph(G,[0,2])
     assert_nodes_equal(list(P),[0,2])
     assert_edges_equal(list(P.edges()),[(0,2)])
     P[0][2]['weight']=1
示例#17
0
 def test_edgelist_integers(self):
     G = nx.convert_node_labels_to_integers(self.G)
     (fd, fname) = tempfile.mkstemp()
     nx.write_edgelist(G, fname)
     H = nx.read_edgelist(fname, nodetype=int)
     # isolated nodes are not written in edgelist
     G.remove_nodes_from(list(nx.isolates(G)))
     assert_nodes_equal(list(H), list(G))
     assert_edges_equal(list(H.edges()), list(G.edges()))
     os.close(fd)
     os.unlink(fname)
示例#18
0
 def test_adjlist_digraph(self):
     G = self.DG
     (fd, fname) = tempfile.mkstemp()
     nx.write_adjlist(G, fname)
     H = nx.read_adjlist(fname, create_using=nx.DiGraph())
     H2 = nx.read_adjlist(fname, create_using=nx.DiGraph())
     assert_not_equal(H, H2)  # they should be different graphs
     assert_nodes_equal(list(H), list(G))
     assert_edges_equal(list(H.edges()), list(G.edges()))
     os.close(fd)
     os.unlink(fname)
示例#19
0
 def test_multiline_adjlist_graph(self):
     G = self.G
     (fd, fname) = tempfile.mkstemp()
     nx.write_multiline_adjlist(G, fname)
     H = nx.read_multiline_adjlist(fname)
     H2 = nx.read_multiline_adjlist(fname)
     assert_not_equal(H, H2)  # they should be different graphs
     assert_nodes_equal(list(H), list(G))
     assert_edges_equal(list(H.edges()), list(G.edges()))
     os.close(fd)
     os.unlink(fname)
示例#20
0
 def test_path_projected_properties_graph(self):
     G=nx.path_graph(4)
     G.add_node(1,name='one')
     G.add_node(2,name='two')
     P=bipartite.projected_graph(G,[1,3])
     assert_nodes_equal(list(P),[1,3])
     assert_edges_equal(list(P.edges()),[(1,3)])
     assert_equal(P.node[1]['name'],G.node[1]['name'])
     P=bipartite.projected_graph(G,[0,2])
     assert_nodes_equal(list(P),[0,2])
     assert_edges_equal(list(P.edges()),[(0,2)])
     assert_equal(P.node[2]['name'],G.node[2]['name'])
示例#21
0
    def test_inverse(self):
        """Tests that the encoding and decoding functions are inverses.

        """
        for T in nx.nonisomorphic_trees(4):
            T2 = nx.from_prufer_sequence(nx.to_prufer_sequence(T))
            assert_nodes_equal(list(T), list(T2))
            assert_edges_equal(list(T.edges()), list(T2.edges()))

        for seq in product(range(4), repeat=2):
            seq2 = nx.to_prufer_sequence(nx.from_prufer_sequence(seq))
            assert_equal(list(seq), seq2)
示例#22
0
 def test_edgelist_graph(self):
     G=self.G
     (fd,fname)=tempfile.mkstemp()
     bipartite.write_edgelist(G,fname)  
     H=bipartite.read_edgelist(fname)
     H2=bipartite.read_edgelist(fname)
     assert_not_equal(H,H2) # they should be different graphs
     G.remove_node('g') # isolated nodes are not written in edgelist
     assert_nodes_equal(H.nodes(),G.nodes())
     assert_edges_equal(H.edges(),G.edges())
     os.close(fd)
     os.unlink(fname)
示例#23
0
 def test_weight_attribute(self):
     G = nx.Graph()
     G.add_edge(0, 1, weight=1, distance=7)
     G.add_edge(0, 2, weight=30, distance=1)
     G.add_edge(1, 2, weight=1, distance=1)
     G.add_node(3)
     T = nx.minimum_spanning_tree(G, algorithm=self.algo, weight='distance')
     assert_nodes_equal(sorted(T), list(range(4)))
     assert_edges_equal(sorted(T.edges()), [(0, 2), (1, 2)])
     T = nx.maximum_spanning_tree(G, algorithm=self.algo, weight='distance')
     assert_nodes_equal(sorted(T), list(range(4)))
     assert_edges_equal(sorted(T.edges()), [(0, 1), (0, 2)])
示例#24
0
 def test_edgelist_digraph(self):
     G = self.DG
     (fd, fname) = tempfile.mkstemp()
     nx.write_edgelist(G, fname)
     H = nx.read_edgelist(fname, create_using=nx.DiGraph())
     G.remove_node('g')  # isolated nodes are not written in edgelist
     H2 = nx.read_edgelist(fname, create_using=nx.DiGraph())
     assert_not_equal(H, H2)  # they should be different graphs
     assert_nodes_equal(list(H), list(G))
     assert_edges_equal(list(H.edges()), list(G.edges()))
     os.close(fd)
     os.unlink(fname)
示例#25
0
 def test_path_projected_properties_graph(self):
     G = nx.path_graph(4)
     G.add_node(1, name="one")
     G.add_node(2, name="two")
     P = bipartite.projected_graph(G, [1, 3])
     assert_nodes_equal(P.nodes(), [1, 3])
     assert_edges_equal(P.edges(), [(1, 3)])
     assert_equal(P.node[1]["name"], G.node[1]["name"])
     P = bipartite.projected_graph(G, [0, 2])
     assert_nodes_equal(P.nodes(), [0, 2])
     assert_edges_equal(P.edges(), [(0, 2)])
     assert_equal(P.node[2]["name"], G.node[2]["name"])
示例#26
0
 def test_multiline_adjlist_multidigraph(self):
     G=self.XDG
     (fd,fname)=tempfile.mkstemp()
     nx.write_multiline_adjlist(G,fname)
     H=nx.read_multiline_adjlist(fname,nodetype=int,
                                 create_using=nx.MultiDiGraph())
     H2=nx.read_multiline_adjlist(fname,nodetype=int,
                                  create_using=nx.MultiDiGraph())
     assert_not_equal(H,H2) # they should be different graphs
     assert_nodes_equal(H.nodes(),G.nodes())
     assert_edges_equal(H.edges(),G.edges())
     os.close(fd)
     os.unlink(fname)
示例#27
0
    def test_complete_graph(self):
        # complete_graph(m) is a connected graph with
        # m nodes and  m*(m+1)/2 edges
        for m in [0, 1, 3, 5]:
            g = complete_graph(m)
            assert_true(number_of_nodes(g) == m)
            assert_true(number_of_edges(g) == m * (m - 1) // 2)

        mg=complete_graph(m, create_using=MultiGraph())
        assert_edges_equal(mg.edges(), g.edges())

        g = complete_graph("abc")
        assert_nodes_equal(g.nodes(), ['a', 'b', 'c'])
        assert_equal(g.size(), 3)
示例#28
0
 def test_from_edgelist(self):
     # Pandas DataFrame
     g = nx.cycle_graph(10)
     G = nx.Graph()
     G.add_nodes_from(g)
     G.add_weighted_edges_from((u, v, u) for u, v in g.edges())
     edgelist = nx.to_edgelist(G)
     source = [s for s, t, d in edgelist]
     target = [t for s, t, d in edgelist]
     weight = [d['weight'] for s, t, d in edgelist]
     edges = pd.DataFrame({'source': source,
                           'target': target,
                           'weight': weight})
     GG = nx.from_pandas_edgelist(edges, edge_attr='weight')
     assert_nodes_equal(G.nodes(), GG.nodes())
     assert_edges_equal(G.edges(), GG.edges())
     GW = nx.to_networkx_graph(edges, create_using=nx.Graph())
     assert_nodes_equal(G.nodes(), GW.nodes())
     assert_edges_equal(G.edges(), GW.edges())
示例#29
0
    def test_dorogovtsev_goltsev_mendes_graph(self):
        G=dorogovtsev_goltsev_mendes_graph(0)
        assert_edges_equal(G.edges(), [(0, 1)])
        assert_nodes_equal(list(G), [0, 1])
        G=dorogovtsev_goltsev_mendes_graph(1)
        assert_edges_equal(G.edges(), [(0, 1), (0, 2), (1, 2)])
        assert_equal(average_clustering(G), 1.0)
        assert_equal(sorted(triangles(G).values()), [1, 1, 1])
        G=dorogovtsev_goltsev_mendes_graph(10)
        assert_equal(number_of_nodes(G), 29526)
        assert_equal(number_of_edges(G), 59049)
        assert_equal(G.degree(0), 1024)
        assert_equal(G.degree(1), 1024)
        assert_equal(G.degree(2), 1024)

        assert_raises(networkx.exception.NetworkXError,
                      dorogovtsev_goltsev_mendes_graph, 7,
                      create_using=DiGraph())
        assert_raises(networkx.exception.NetworkXError,
                      dorogovtsev_goltsev_mendes_graph, 7,
                      create_using=MultiGraph())
示例#30
0
    def test_digraphs(self):
        for dest, source in [(to_dict_of_dicts, from_dict_of_dicts),
                             (to_dict_of_lists, from_dict_of_lists)]:
            G = cycle_graph(10)

            # Dict of [dicts, lists]
            dod = dest(G)
            GG = source(dod)
            assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
            assert_edges_equal(sorted(G.edges()), sorted(GG.edges()))
            GW = to_networkx_graph(dod)
            assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
            assert_edges_equal(sorted(G.edges()), sorted(GW.edges()))
            GI = nx.Graph(dod)
            assert_nodes_equal(sorted(G.nodes()), sorted(GI.nodes()))
            assert_edges_equal(sorted(G.edges()), sorted(GI.edges()))

            G = cycle_graph(10, create_using=nx.DiGraph())
            dod = dest(G)
            GG = source(dod, create_using=nx.DiGraph())
            assert_equal(sorted(G.nodes()), sorted(GG.nodes()))
            assert_equal(sorted(G.edges()), sorted(GG.edges()))
            GW = to_networkx_graph(dod, create_using=nx.DiGraph())
            assert_equal(sorted(G.nodes()), sorted(GW.nodes()))
            assert_equal(sorted(G.edges()), sorted(GW.edges()))
            GI = nx.DiGraph(dod)
            assert_equal(sorted(G.nodes()), sorted(GI.nodes()))
            assert_equal(sorted(G.edges()), sorted(GI.edges()))