test_reaching.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """Unit tests for the :mod:`networkx.algorithms.centrality.reaching` module."""
  2. import pytest
  3. import networkx as nx
  4. class TestGlobalReachingCentrality:
  5. """Unit tests for the global reaching centrality function."""
  6. def test_non_positive_weights(self):
  7. with pytest.raises(nx.NetworkXError):
  8. G = nx.DiGraph()
  9. nx.global_reaching_centrality(G, weight="weight")
  10. def test_negatively_weighted(self):
  11. with pytest.raises(nx.NetworkXError):
  12. G = nx.Graph()
  13. G.add_weighted_edges_from([(0, 1, -2), (1, 2, +1)])
  14. nx.global_reaching_centrality(G, weight="weight")
  15. def test_directed_star(self):
  16. G = nx.DiGraph()
  17. G.add_weighted_edges_from([(1, 2, 0.5), (1, 3, 0.5)])
  18. grc = nx.global_reaching_centrality
  19. assert grc(G, normalized=False, weight="weight") == 0.5
  20. assert grc(G) == 1
  21. def test_undirected_unweighted_star(self):
  22. G = nx.star_graph(2)
  23. grc = nx.global_reaching_centrality
  24. assert grc(G, normalized=False, weight=None) == 0.25
  25. def test_undirected_weighted_star(self):
  26. G = nx.Graph()
  27. G.add_weighted_edges_from([(1, 2, 1), (1, 3, 2)])
  28. grc = nx.global_reaching_centrality
  29. assert grc(G, normalized=False, weight="weight") == 0.375
  30. def test_cycle_directed_unweighted(self):
  31. G = nx.DiGraph()
  32. G.add_edge(1, 2)
  33. G.add_edge(2, 1)
  34. assert nx.global_reaching_centrality(G, weight=None) == 0
  35. def test_cycle_undirected_unweighted(self):
  36. G = nx.Graph()
  37. G.add_edge(1, 2)
  38. assert nx.global_reaching_centrality(G, weight=None) == 0
  39. def test_cycle_directed_weighted(self):
  40. G = nx.DiGraph()
  41. G.add_weighted_edges_from([(1, 2, 1), (2, 1, 1)])
  42. assert nx.global_reaching_centrality(G) == 0
  43. def test_cycle_undirected_weighted(self):
  44. G = nx.Graph()
  45. G.add_edge(1, 2, weight=1)
  46. grc = nx.global_reaching_centrality
  47. assert grc(G, normalized=False) == 0
  48. def test_directed_weighted(self):
  49. G = nx.DiGraph()
  50. G.add_edge("A", "B", weight=5)
  51. G.add_edge("B", "C", weight=1)
  52. G.add_edge("B", "D", weight=0.25)
  53. G.add_edge("D", "E", weight=1)
  54. denom = len(G) - 1
  55. A_local = sum([5, 3, 2.625, 2.0833333333333]) / denom
  56. B_local = sum([1, 0.25, 0.625]) / denom
  57. C_local = 0
  58. D_local = sum([1]) / denom
  59. E_local = 0
  60. local_reach_ctrs = [A_local, C_local, B_local, D_local, E_local]
  61. max_local = max(local_reach_ctrs)
  62. expected = sum(max_local - lrc for lrc in local_reach_ctrs) / denom
  63. grc = nx.global_reaching_centrality
  64. actual = grc(G, normalized=False, weight="weight")
  65. assert expected == pytest.approx(actual, abs=1e-7)
  66. class TestLocalReachingCentrality:
  67. """Unit tests for the local reaching centrality function."""
  68. def test_non_positive_weights(self):
  69. with pytest.raises(nx.NetworkXError):
  70. G = nx.DiGraph()
  71. G.add_weighted_edges_from([(0, 1, 0)])
  72. nx.local_reaching_centrality(G, 0, weight="weight")
  73. def test_negatively_weighted(self):
  74. with pytest.raises(nx.NetworkXError):
  75. G = nx.Graph()
  76. G.add_weighted_edges_from([(0, 1, -2), (1, 2, +1)])
  77. nx.local_reaching_centrality(G, 0, weight="weight")
  78. def test_undirected_unweighted_star(self):
  79. G = nx.star_graph(2)
  80. grc = nx.local_reaching_centrality
  81. assert grc(G, 1, weight=None, normalized=False) == 0.75
  82. def test_undirected_weighted_star(self):
  83. G = nx.Graph()
  84. G.add_weighted_edges_from([(1, 2, 1), (1, 3, 2)])
  85. centrality = nx.local_reaching_centrality(
  86. G, 1, normalized=False, weight="weight"
  87. )
  88. assert centrality == 1.5