Example #1
0
    def test_specified_methods(self):
        G = nx.Graph()
        nx.add_cycle(G, range(7), weight=2)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="dijkstra")
        assert almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="bellman-ford")
        assert almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="floyd-warshall")
        assert almost_equal(ans, 4)

        G = nx.Graph()
        nx.add_path(G, range(5), weight=2)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="dijkstra")
        assert almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="bellman-ford")
        assert almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="floyd-warshall")
        assert almost_equal(ans, 4)
Example #2
0
 def test_edges_with_data_equal(self):
     G = nx.MultiGraph()
     nx.add_path(G, [0, 1, 2], weight=1)
     H = nx.MultiGraph()
     nx.add_path(H, [0, 1, 2], weight=1)
     self._test_equal(G.edges(data=True, keys=True),
                      H.edges(data=True, keys=True))
Example #3
0
 def test_all_simple_paths_multigraph(self):
     G = nx.MultiGraph([(1, 2), (1, 2)])
     paths = nx.builtin.all_simple_paths(G, 1, 1)
     assert list(paths) == []
     nx.add_path(G, [3, 1, 10, 2])
     paths = list(nx.builtin.all_simple_paths(G, 1, 2))
     assert len(paths) == 3
     assert {tuple(p) for p in paths} == {(1, 2), (1, 2), (1, 10, 2)}
 def test_directed_path_normalized(self):
     """Betweenness centrality: directed path normalized"""
     G = nx.DiGraph()
     nx.add_path(G, [0, 1, 2])
     b = nx.builtin.betweenness_centrality(G, weight=None, normalized=True)
     b_answer = {0: 0.0, 1: 0.5, 2: 0.0}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
Example #5
0
 def setup_class(cls):
     cnlti = nx.convert_node_labels_to_integers
     Gi = cnlti(grid_2d_graph(5, 5), first_label=1, ordering="sorted")
     cls.Gi = nx.Graph(Gi)
     cls.Gs = nx.Graph()
     nx.add_path(cls.Gs, "abcdef")
     bigG = cnlti(grid_2d_graph(25, 25), first_label=1, ordering="sorted")
     cls.bigG = nx.Graph(bigG)
 def test_disconnected_path(self):
     """Betweenness centrality: disconnected path"""
     G = nx.Graph()
     nx.add_path(G, [0, 1, 2])
     nx.add_path(G, [3, 4, 5, 6])
     b_answer = {0: 0, 1: 1, 2: 0, 3: 0, 4: 2, 5: 2, 6: 0}
     b = nx.builtin.betweenness_centrality(G, weight=None, normalized=False)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
Example #7
0
 def test_weighted(self):
     G = nx.Graph()
     nx.add_cycle(G, range(7), weight=2)
     ans = nx.average_shortest_path_length(G, weight="weight")
     assert almost_equal(ans, 4)
     G = nx.Graph()
     nx.add_path(G, range(5), weight=2)
     ans = nx.average_shortest_path_length(G, weight="weight")
     assert almost_equal(ans, 4)
 def test_disconnected_path_endpoints(self):
     """Betweenness centrality: disconnected path endpoints"""
     G = nx.Graph()
     nx.add_path(G, [0, 1, 2])
     nx.add_path(G, [3, 4, 5, 6])
     b_answer = {0: 2, 1: 3, 2: 2, 3: 3, 4: 5, 5: 5, 6: 3}
     b = nx.builtin.betweenness_centrality(G,
                                           weight=None,
                                           normalized=False,
                                           endpoints=True)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
     # normalized = True case
     b = nx.builtin.betweenness_centrality(G,
                                           weight=None,
                                           normalized=True,
                                           endpoints=True)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n] / 21)
Example #9
0
 def setup_class(cls):
     # a tree
     G = nx.Graph()
     nx.add_path(G, [0, 1, 2, 3, 4, 5, 6])
     nx.add_path(G, [2, 7, 8, 9, 10])
     cls.G = G
     # a disconnected graph
     D = nx.Graph()
     D.add_edges_from([(0, 1), (2, 3)])
     nx.add_path(D, [2, 7, 8, 9, 10])
     cls.D = D
Example #10
0
 def setup_method(self):
     # a tree
     G = nx.Graph()
     nx.add_path(G, [0, 1, 2, 3, 4, 5, 6])
     nx.add_path(G, [2, 7, 8, 9, 10])
     self.G = G
     # a disconnected graph
     D = nx.Graph()
     D.add_edges_from([(0, 1), (2, 3)])
     nx.add_path(D, [2, 7, 8, 9, 10])
     self.D = D
Example #11
0
 def test_all_simple_edge_paths_directed(self):
     G = nx.DiGraph()
     nx.add_path(G, [1, 2, 3])
     nx.add_path(G, [3, 2, 1])
     paths = nx.builtin.all_simple_edge_paths(G, 1, 3)
     assert {tuple(p) for p in paths} == {((1, 2), (2, 3))}
Example #12
0
def test_path():
    G = nx.DiGraph()
    nx.add_path(G, range(5))
    assert nx.is_branching(G)
    assert nx.is_arborescence(G)
Example #13
0
 def test_edge_target_missing(self):
     with pytest.raises(ValueError, match="nx.NodeNotFound"):
         G = nx.Graph()
         nx.add_path(G, [1, 2, 3])
         list(nx.builtin.all_simple_edge_paths(nx.DiGraph(G), 1, 4))
Example #14
0
 def test_multidigraphs_equal(self):
     G = nx.path_graph(4, create_using=nx.MultiDiGraph())
     H = nx.MultiDiGraph()
     nx.add_path(H, range(4))
     self._test_equal(G, H)
Example #15
0
 def test_graphs_not_equal2(self):
     G = nx.path_graph(4)
     H = nx.Graph()
     nx.add_path(H, range(3))
     self._test_not_equal(G, H)
Example #16
0
 def test_has_path(self):
     G = nx.Graph()
     nx.add_path(G, range(3))
     nx.add_path(G, range(3, 5))
     assert nx.builtin.has_path(G, 0, 2)
     assert not nx.builtin.has_path(G, 0, 4)
Example #17
0
 def test_graphs_not_equal3(self):
     G = nx.path_graph(4)
     H = nx.Graph()
     nx.add_path(H, range(4))
     H.name = "path_graph(4)"
     self._test_not_equal(G, H)