Exemplo n.º 1
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)
Exemplo n.º 2
0
    def test_trivial_graph(self):
        """Tests that the trivial graph has average path length zero,
        since there is exactly one path of length zero in the trivial
        graph.

        For more information, see issue #1960.

        """
        G = nx.trivial_graph()
        assert nx.average_shortest_path_length(G) == 0
Exemplo n.º 3
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)
Exemplo n.º 4
0
 def test_bad_method(self):
     with pytest.raises(ValueError):
         G = nx.path_graph(2)
         nx.average_shortest_path_length(G, weight="weight", method="SPAM")
Exemplo n.º 5
0
 def test_null_graph(self):
     with pytest.raises(nx.NetworkXPointlessConcept):
         nx.average_shortest_path_length(nx.null_graph())
Exemplo n.º 6
0
 def test_path_graph(self):
     ans = nx.average_shortest_path_length(nx.path_graph(5))
     assert almost_equal(ans, 2)