### comparison env/lib/python3.9/site-packages/networkx/algorithms/shortest_paths/tests/test_dense_numpy.py @ 0:4f3585e2f14bdraftdefaulttip

author shellac Mon, 22 Mar 2021 18:12:50 +0000
comparison
equal inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import pytest
2
3 numpy = pytest.importorskip("numpy")
4 npt = pytest.importorskip("numpy.testing")
5
6
7 import networkx as nx
8
9
10 class TestFloydNumpy:
11 def test_cycle_numpy(self):
12 dist = nx.floyd_warshall_numpy(nx.cycle_graph(7))
13 assert dist[0, 3] == 3
14 assert dist[0, 4] == 3
15
16 def test_weighted_numpy_three_edges(self):
17 XG3 = nx.Graph()
19 [[0, 1, 2], [1, 2, 12], [2, 3, 1], [3, 4, 5], [4, 5, 1], [5, 0, 10]]
20 )
21 dist = nx.floyd_warshall_numpy(XG3)
22 assert dist[0, 3] == 15
23
24 def test_weighted_numpy_two_edges(self):
25 XG4 = nx.Graph()
27 [
28 [0, 1, 2],
29 [1, 2, 2],
30 [2, 3, 1],
31 [3, 4, 1],
32 [4, 5, 1],
33 [5, 6, 1],
34 [6, 7, 1],
35 [7, 0, 1],
36 ]
37 )
38 dist = nx.floyd_warshall_numpy(XG4)
39 assert dist[0, 2] == 4
40
41 def test_weight_parameter_numpy(self):
42 XG4 = nx.Graph()
44 [
45 (0, 1, {"heavy": 2}),
46 (1, 2, {"heavy": 2}),
47 (2, 3, {"heavy": 1}),
48 (3, 4, {"heavy": 1}),
49 (4, 5, {"heavy": 1}),
50 (5, 6, {"heavy": 1}),
51 (6, 7, {"heavy": 1}),
52 (7, 0, {"heavy": 1}),
53 ]
54 )
55 dist = nx.floyd_warshall_numpy(XG4, weight="heavy")
56 assert dist[0, 2] == 4
57
58 def test_directed_cycle_numpy(self):
59 G = nx.DiGraph()
60 nx.add_cycle(G, [0, 1, 2, 3])
61 pred, dist = nx.floyd_warshall_predecessor_and_distance(G)
62 D = nx.utils.dict_to_numpy_array(dist)
63 npt.assert_equal(nx.floyd_warshall_numpy(G), D)
64
65 def test_zero_weight(self):
66 G = nx.DiGraph()
67 edges = [(1, 2, -2), (2, 3, -4), (1, 5, 1), (5, 4, 0), (4, 3, -5), (2, 5, -7)]