test_subgraph.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import pytest
  2. pytest.importorskip("numpy")
  3. pytest.importorskip("scipy")
  4. import networkx as nx
  5. from networkx.algorithms.centrality.subgraph_alg import (
  6. communicability_betweenness_centrality,
  7. estrada_index,
  8. subgraph_centrality,
  9. subgraph_centrality_exp,
  10. )
  11. class TestSubgraph:
  12. def test_subgraph_centrality(self):
  13. answer = {0: 1.5430806348152433, 1: 1.5430806348152433}
  14. result = subgraph_centrality(nx.path_graph(2))
  15. for k, v in result.items():
  16. assert answer[k] == pytest.approx(v, abs=1e-7)
  17. answer1 = {
  18. "1": 1.6445956054135658,
  19. "Albert": 2.4368257358712189,
  20. "Aric": 2.4368257358712193,
  21. "Dan": 3.1306328496328168,
  22. "Franck": 2.3876142275231915,
  23. }
  24. G1 = nx.Graph(
  25. [
  26. ("Franck", "Aric"),
  27. ("Aric", "Dan"),
  28. ("Dan", "Albert"),
  29. ("Albert", "Franck"),
  30. ("Dan", "1"),
  31. ("Franck", "Albert"),
  32. ]
  33. )
  34. result1 = subgraph_centrality(G1)
  35. for k, v in result1.items():
  36. assert answer1[k] == pytest.approx(v, abs=1e-7)
  37. result1 = subgraph_centrality_exp(G1)
  38. for k, v in result1.items():
  39. assert answer1[k] == pytest.approx(v, abs=1e-7)
  40. def test_subgraph_centrality_big_graph(self):
  41. g199 = nx.complete_graph(199)
  42. g200 = nx.complete_graph(200)
  43. comm199 = nx.subgraph_centrality(g199)
  44. comm199_exp = nx.subgraph_centrality_exp(g199)
  45. comm200 = nx.subgraph_centrality(g200)
  46. comm200_exp = nx.subgraph_centrality_exp(g200)
  47. def test_communicability_betweenness_centrality_small(self):
  48. result = communicability_betweenness_centrality(nx.path_graph(2))
  49. assert result == {0: 0, 1: 0}
  50. result = communicability_betweenness_centrality(nx.path_graph(1))
  51. assert result == {0: 0}
  52. result = communicability_betweenness_centrality(nx.path_graph(0))
  53. assert result == {}
  54. answer = {0: 0.1411224421177313, 1: 1.0, 2: 0.1411224421177313}
  55. result = communicability_betweenness_centrality(nx.path_graph(3))
  56. for k, v in result.items():
  57. assert answer[k] == pytest.approx(v, abs=1e-7)
  58. result = communicability_betweenness_centrality(nx.complete_graph(3))
  59. for k, v in result.items():
  60. assert 0.49786143366223296 == pytest.approx(v, abs=1e-7)
  61. def test_communicability_betweenness_centrality(self):
  62. answer = {
  63. 0: 0.07017447951484615,
  64. 1: 0.71565598701107991,
  65. 2: 0.71565598701107991,
  66. 3: 0.07017447951484615,
  67. }
  68. result = communicability_betweenness_centrality(nx.path_graph(4))
  69. for k, v in result.items():
  70. assert answer[k] == pytest.approx(v, abs=1e-7)
  71. answer1 = {
  72. "1": 0.060039074193949521,
  73. "Albert": 0.315470761661372,
  74. "Aric": 0.31547076166137211,
  75. "Dan": 0.68297778678316201,
  76. "Franck": 0.21977926617449497,
  77. }
  78. G1 = nx.Graph(
  79. [
  80. ("Franck", "Aric"),
  81. ("Aric", "Dan"),
  82. ("Dan", "Albert"),
  83. ("Albert", "Franck"),
  84. ("Dan", "1"),
  85. ("Franck", "Albert"),
  86. ]
  87. )
  88. result1 = communicability_betweenness_centrality(G1)
  89. for k, v in result1.items():
  90. assert answer1[k] == pytest.approx(v, abs=1e-7)
  91. def test_estrada_index(self):
  92. answer = 1041.2470334195475
  93. result = estrada_index(nx.karate_club_graph())
  94. assert answer == pytest.approx(result, abs=1e-7)