xiaoanyu123 commited on
Commit
38e2af0
·
verified ·
1 Parent(s): b55644a

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/__init__.cpython-310.pyc +0 -0
  2. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/betweenness.cpython-310.pyc +0 -0
  3. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/betweenness_subset.cpython-310.pyc +0 -0
  4. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/closeness.cpython-310.pyc +0 -0
  5. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/current_flow_betweenness.cpython-310.pyc +0 -0
  6. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/current_flow_betweenness_subset.cpython-310.pyc +0 -0
  7. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/current_flow_closeness.cpython-310.pyc +0 -0
  8. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/degree_alg.cpython-310.pyc +0 -0
  9. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/dispersion.cpython-310.pyc +0 -0
  10. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/eigenvector.cpython-310.pyc +0 -0
  11. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/flow_matrix.cpython-310.pyc +0 -0
  12. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/group.cpython-310.pyc +0 -0
  13. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/harmonic.cpython-310.pyc +0 -0
  14. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/katz.cpython-310.pyc +0 -0
  15. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/laplacian.cpython-310.pyc +0 -0
  16. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/load.cpython-310.pyc +0 -0
  17. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/percolation.cpython-310.pyc +0 -0
  18. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/reaching.cpython-310.pyc +0 -0
  19. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/second_order.cpython-310.pyc +0 -0
  20. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/subgraph_alg.cpython-310.pyc +0 -0
  21. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/trophic.cpython-310.pyc +0 -0
  22. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/voterank_alg.cpython-310.pyc +0 -0
  23. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__init__.py +0 -0
  24. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/__init__.cpython-310.pyc +0 -0
  25. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_betweenness_centrality.cpython-310.pyc +0 -0
  26. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_betweenness_centrality_subset.cpython-310.pyc +0 -0
  27. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_closeness_centrality.cpython-310.pyc +0 -0
  28. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_current_flow_betweenness_centrality.cpython-310.pyc +0 -0
  29. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_current_flow_betweenness_centrality_subset.cpython-310.pyc +0 -0
  30. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_current_flow_closeness.cpython-310.pyc +0 -0
  31. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_degree_centrality.cpython-310.pyc +0 -0
  32. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_dispersion.cpython-310.pyc +0 -0
  33. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_eigenvector_centrality.cpython-310.pyc +0 -0
  34. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_group.cpython-310.pyc +0 -0
  35. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_harmonic_centrality.cpython-310.pyc +0 -0
  36. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_katz_centrality.cpython-310.pyc +0 -0
  37. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_laplacian_centrality.cpython-310.pyc +0 -0
  38. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_load_centrality.cpython-310.pyc +0 -0
  39. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_percolation_centrality.cpython-310.pyc +0 -0
  40. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_reaching.cpython-310.pyc +0 -0
  41. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_second_order_centrality.cpython-310.pyc +0 -0
  42. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_subgraph.cpython-310.pyc +0 -0
  43. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_trophic.cpython-310.pyc +0 -0
  44. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_voterank.cpython-310.pyc +0 -0
  45. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_dispersion.py +73 -0
  46. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_eigenvector_centrality.py +175 -0
  47. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_group.py +278 -0
  48. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_harmonic_centrality.py +115 -0
  49. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_katz_centrality.py +345 -0
  50. pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_laplacian_centrality.py +221 -0
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (670 Bytes). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/betweenness.cpython-310.pyc ADDED
Binary file (12.3 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/betweenness_subset.cpython-310.pyc ADDED
Binary file (8.21 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/closeness.cpython-310.pyc ADDED
Binary file (9.16 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/current_flow_betweenness.cpython-310.pyc ADDED
Binary file (11.3 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/current_flow_betweenness_subset.cpython-310.pyc ADDED
Binary file (7.96 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/current_flow_closeness.cpython-310.pyc ADDED
Binary file (3.37 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/degree_alg.cpython-310.pyc ADDED
Binary file (4.55 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/dispersion.cpython-310.pyc ADDED
Binary file (3.29 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/eigenvector.cpython-310.pyc ADDED
Binary file (12.7 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/flow_matrix.cpython-310.pyc ADDED
Binary file (5.08 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/group.cpython-310.pyc ADDED
Binary file (21.5 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/harmonic.cpython-310.pyc ADDED
Binary file (2.83 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/katz.cpython-310.pyc ADDED
Binary file (10.8 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/laplacian.cpython-310.pyc ADDED
Binary file (5.5 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/load.cpython-310.pyc ADDED
Binary file (5.67 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/percolation.cpython-310.pyc ADDED
Binary file (4.16 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/reaching.cpython-310.pyc ADDED
Binary file (6.7 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/second_order.cpython-310.pyc ADDED
Binary file (5.39 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/subgraph_alg.cpython-310.pyc ADDED
Binary file (9.28 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/trophic.cpython-310.pyc ADDED
Binary file (4.29 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/__pycache__/voterank_alg.cpython-310.pyc ADDED
Binary file (2.9 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__init__.py ADDED
File without changes
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (193 Bytes). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_betweenness_centrality.cpython-310.pyc ADDED
Binary file (21.1 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_betweenness_centrality_subset.cpython-310.pyc ADDED
Binary file (10.9 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_closeness_centrality.cpython-310.pyc ADDED
Binary file (9.31 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_current_flow_betweenness_centrality.cpython-310.pyc ADDED
Binary file (7.97 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_current_flow_betweenness_centrality_subset.cpython-310.pyc ADDED
Binary file (4.36 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_current_flow_closeness.cpython-310.pyc ADDED
Binary file (2.09 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_degree_centrality.cpython-310.pyc ADDED
Binary file (3.98 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_dispersion.cpython-310.pyc ADDED
Binary file (2.26 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_eigenvector_centrality.cpython-310.pyc ADDED
Binary file (5.8 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_group.cpython-310.pyc ADDED
Binary file (10.1 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_harmonic_centrality.cpython-310.pyc ADDED
Binary file (5.06 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_katz_centrality.cpython-310.pyc ADDED
Binary file (9.5 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_laplacian_centrality.cpython-310.pyc ADDED
Binary file (6.05 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_load_centrality.cpython-310.pyc ADDED
Binary file (9.54 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_percolation_centrality.cpython-310.pyc ADDED
Binary file (2.75 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_reaching.cpython-310.pyc ADDED
Binary file (5.67 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_second_order_centrality.cpython-310.pyc ADDED
Binary file (2.78 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_subgraph.cpython-310.pyc ADDED
Binary file (3.02 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_trophic.cpython-310.pyc ADDED
Binary file (7.15 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/__pycache__/test_voterank.cpython-310.pyc ADDED
Binary file (2.23 kB). View file
 
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_dispersion.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import networkx as nx
2
+
3
+
4
+ def small_ego_G():
5
+ """The sample network from https://arxiv.org/pdf/1310.6753v1.pdf"""
6
+ edges = [
7
+ ("a", "b"),
8
+ ("a", "c"),
9
+ ("b", "c"),
10
+ ("b", "d"),
11
+ ("b", "e"),
12
+ ("b", "f"),
13
+ ("c", "d"),
14
+ ("c", "f"),
15
+ ("c", "h"),
16
+ ("d", "f"),
17
+ ("e", "f"),
18
+ ("f", "h"),
19
+ ("h", "j"),
20
+ ("h", "k"),
21
+ ("i", "j"),
22
+ ("i", "k"),
23
+ ("j", "k"),
24
+ ("u", "a"),
25
+ ("u", "b"),
26
+ ("u", "c"),
27
+ ("u", "d"),
28
+ ("u", "e"),
29
+ ("u", "f"),
30
+ ("u", "g"),
31
+ ("u", "h"),
32
+ ("u", "i"),
33
+ ("u", "j"),
34
+ ("u", "k"),
35
+ ]
36
+ G = nx.Graph()
37
+ G.add_edges_from(edges)
38
+
39
+ return G
40
+
41
+
42
+ class TestDispersion:
43
+ def test_article(self):
44
+ """our algorithm matches article's"""
45
+ G = small_ego_G()
46
+ disp_uh = nx.dispersion(G, "u", "h", normalized=False)
47
+ disp_ub = nx.dispersion(G, "u", "b", normalized=False)
48
+ assert disp_uh == 4
49
+ assert disp_ub == 1
50
+
51
+ def test_results_length(self):
52
+ """there is a result for every node"""
53
+ G = small_ego_G()
54
+ disp = nx.dispersion(G)
55
+ disp_Gu = nx.dispersion(G, "u")
56
+ disp_uv = nx.dispersion(G, "u", "h")
57
+ assert len(disp) == len(G)
58
+ assert len(disp_Gu) == len(G) - 1
59
+ assert isinstance(disp_uv, float)
60
+
61
+ def test_dispersion_v_only(self):
62
+ G = small_ego_G()
63
+ disp_G_h = nx.dispersion(G, v="h", normalized=False)
64
+ disp_G_h_normalized = nx.dispersion(G, v="h", normalized=True)
65
+ assert disp_G_h == {"c": 0, "f": 0, "j": 0, "k": 0, "u": 4}
66
+ assert disp_G_h_normalized == {"c": 0.0, "f": 0.0, "j": 0.0, "k": 0.0, "u": 1.0}
67
+
68
+ def test_impossible_things(self):
69
+ G = nx.karate_club_graph()
70
+ disp = nx.dispersion(G)
71
+ for u in disp:
72
+ for v in disp[u]:
73
+ assert disp[u][v] >= 0
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_eigenvector_centrality.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import pytest
4
+
5
+ np = pytest.importorskip("numpy")
6
+ pytest.importorskip("scipy")
7
+
8
+
9
+ import networkx as nx
10
+
11
+
12
+ class TestEigenvectorCentrality:
13
+ def test_K5(self):
14
+ """Eigenvector centrality: K5"""
15
+ G = nx.complete_graph(5)
16
+ b = nx.eigenvector_centrality(G)
17
+ v = math.sqrt(1 / 5.0)
18
+ b_answer = dict.fromkeys(G, v)
19
+ for n in sorted(G):
20
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
21
+ nstart = {n: 1 for n in G}
22
+ b = nx.eigenvector_centrality(G, nstart=nstart)
23
+ for n in sorted(G):
24
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
25
+
26
+ b = nx.eigenvector_centrality_numpy(G)
27
+ for n in sorted(G):
28
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-3)
29
+
30
+ def test_P3(self):
31
+ """Eigenvector centrality: P3"""
32
+ G = nx.path_graph(3)
33
+ b_answer = {0: 0.5, 1: 0.7071, 2: 0.5}
34
+ b = nx.eigenvector_centrality_numpy(G)
35
+ for n in sorted(G):
36
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
37
+ b = nx.eigenvector_centrality(G)
38
+ for n in sorted(G):
39
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
40
+
41
+ def test_P3_unweighted(self):
42
+ """Eigenvector centrality: P3"""
43
+ G = nx.path_graph(3)
44
+ b_answer = {0: 0.5, 1: 0.7071, 2: 0.5}
45
+ b = nx.eigenvector_centrality_numpy(G, weight=None)
46
+ for n in sorted(G):
47
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
48
+
49
+ def test_maxiter(self):
50
+ with pytest.raises(nx.PowerIterationFailedConvergence):
51
+ G = nx.path_graph(3)
52
+ nx.eigenvector_centrality(G, max_iter=0)
53
+
54
+
55
+ class TestEigenvectorCentralityDirected:
56
+ @classmethod
57
+ def setup_class(cls):
58
+ G = nx.DiGraph()
59
+
60
+ edges = [
61
+ (1, 2),
62
+ (1, 3),
63
+ (2, 4),
64
+ (3, 2),
65
+ (3, 5),
66
+ (4, 2),
67
+ (4, 5),
68
+ (4, 6),
69
+ (5, 6),
70
+ (5, 7),
71
+ (5, 8),
72
+ (6, 8),
73
+ (7, 1),
74
+ (7, 5),
75
+ (7, 8),
76
+ (8, 6),
77
+ (8, 7),
78
+ ]
79
+
80
+ G.add_edges_from(edges, weight=2.0)
81
+ cls.G = G.reverse()
82
+ cls.G.evc = [
83
+ 0.25368793,
84
+ 0.19576478,
85
+ 0.32817092,
86
+ 0.40430835,
87
+ 0.48199885,
88
+ 0.15724483,
89
+ 0.51346196,
90
+ 0.32475403,
91
+ ]
92
+
93
+ H = nx.DiGraph()
94
+
95
+ edges = [
96
+ (1, 2),
97
+ (1, 3),
98
+ (2, 4),
99
+ (3, 2),
100
+ (3, 5),
101
+ (4, 2),
102
+ (4, 5),
103
+ (4, 6),
104
+ (5, 6),
105
+ (5, 7),
106
+ (5, 8),
107
+ (6, 8),
108
+ (7, 1),
109
+ (7, 5),
110
+ (7, 8),
111
+ (8, 6),
112
+ (8, 7),
113
+ ]
114
+
115
+ G.add_edges_from(edges)
116
+ cls.H = G.reverse()
117
+ cls.H.evc = [
118
+ 0.25368793,
119
+ 0.19576478,
120
+ 0.32817092,
121
+ 0.40430835,
122
+ 0.48199885,
123
+ 0.15724483,
124
+ 0.51346196,
125
+ 0.32475403,
126
+ ]
127
+
128
+ def test_eigenvector_centrality_weighted(self):
129
+ G = self.G
130
+ p = nx.eigenvector_centrality(G)
131
+ for a, b in zip(list(p.values()), self.G.evc):
132
+ assert a == pytest.approx(b, abs=1e-4)
133
+
134
+ def test_eigenvector_centrality_weighted_numpy(self):
135
+ G = self.G
136
+ p = nx.eigenvector_centrality_numpy(G)
137
+ for a, b in zip(list(p.values()), self.G.evc):
138
+ assert a == pytest.approx(b, abs=1e-7)
139
+
140
+ def test_eigenvector_centrality_unweighted(self):
141
+ G = self.H
142
+ p = nx.eigenvector_centrality(G)
143
+ for a, b in zip(list(p.values()), self.G.evc):
144
+ assert a == pytest.approx(b, abs=1e-4)
145
+
146
+ def test_eigenvector_centrality_unweighted_numpy(self):
147
+ G = self.H
148
+ p = nx.eigenvector_centrality_numpy(G)
149
+ for a, b in zip(list(p.values()), self.G.evc):
150
+ assert a == pytest.approx(b, abs=1e-7)
151
+
152
+
153
+ class TestEigenvectorCentralityExceptions:
154
+ def test_multigraph(self):
155
+ with pytest.raises(nx.NetworkXException):
156
+ nx.eigenvector_centrality(nx.MultiGraph())
157
+
158
+ def test_multigraph_numpy(self):
159
+ with pytest.raises(nx.NetworkXException):
160
+ nx.eigenvector_centrality_numpy(nx.MultiGraph())
161
+
162
+ def test_empty(self):
163
+ with pytest.raises(nx.NetworkXException):
164
+ nx.eigenvector_centrality(nx.Graph())
165
+
166
+ def test_empty_numpy(self):
167
+ with pytest.raises(nx.NetworkXException):
168
+ nx.eigenvector_centrality_numpy(nx.Graph())
169
+
170
+ def test_zero_nstart(self):
171
+ G = nx.Graph([(1, 2), (1, 3), (2, 3)])
172
+ with pytest.raises(
173
+ nx.NetworkXException, match="initial vector cannot have all zero values"
174
+ ):
175
+ nx.eigenvector_centrality(G, nstart={v: 0 for v in G})
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_group.py ADDED
@@ -0,0 +1,278 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests for Group Centrality Measures
3
+ """
4
+
5
+
6
+ import pytest
7
+
8
+ import networkx as nx
9
+
10
+
11
+ class TestGroupBetweennessCentrality:
12
+ def test_group_betweenness_single_node(self):
13
+ """
14
+ Group betweenness centrality for single node group
15
+ """
16
+ G = nx.path_graph(5)
17
+ C = [1]
18
+ b = nx.group_betweenness_centrality(
19
+ G, C, weight=None, normalized=False, endpoints=False
20
+ )
21
+ b_answer = 3.0
22
+ assert b == b_answer
23
+
24
+ def test_group_betweenness_with_endpoints(self):
25
+ """
26
+ Group betweenness centrality for single node group
27
+ """
28
+ G = nx.path_graph(5)
29
+ C = [1]
30
+ b = nx.group_betweenness_centrality(
31
+ G, C, weight=None, normalized=False, endpoints=True
32
+ )
33
+ b_answer = 7.0
34
+ assert b == b_answer
35
+
36
+ def test_group_betweenness_normalized(self):
37
+ """
38
+ Group betweenness centrality for group with more than
39
+ 1 node and normalized
40
+ """
41
+ G = nx.path_graph(5)
42
+ C = [1, 3]
43
+ b = nx.group_betweenness_centrality(
44
+ G, C, weight=None, normalized=True, endpoints=False
45
+ )
46
+ b_answer = 1.0
47
+ assert b == b_answer
48
+
49
+ def test_two_group_betweenness_value_zero(self):
50
+ """
51
+ Group betweenness centrality value of 0
52
+ """
53
+ G = nx.cycle_graph(7)
54
+ C = [[0, 1, 6], [0, 1, 5]]
55
+ b = nx.group_betweenness_centrality(G, C, weight=None, normalized=False)
56
+ b_answer = [0.0, 3.0]
57
+ assert b == b_answer
58
+
59
+ def test_group_betweenness_value_zero(self):
60
+ """
61
+ Group betweenness centrality value of 0
62
+ """
63
+ G = nx.cycle_graph(6)
64
+ C = [0, 1, 5]
65
+ b = nx.group_betweenness_centrality(G, C, weight=None, normalized=False)
66
+ b_answer = 0.0
67
+ assert b == b_answer
68
+
69
+ def test_group_betweenness_disconnected_graph(self):
70
+ """
71
+ Group betweenness centrality in a disconnected graph
72
+ """
73
+ G = nx.path_graph(5)
74
+ G.remove_edge(0, 1)
75
+ C = [1]
76
+ b = nx.group_betweenness_centrality(G, C, weight=None, normalized=False)
77
+ b_answer = 0.0
78
+ assert b == b_answer
79
+
80
+ def test_group_betweenness_node_not_in_graph(self):
81
+ """
82
+ Node(s) in C not in graph, raises NodeNotFound exception
83
+ """
84
+ with pytest.raises(nx.NodeNotFound):
85
+ nx.group_betweenness_centrality(nx.path_graph(5), [4, 7, 8])
86
+
87
+ def test_group_betweenness_directed_weighted(self):
88
+ """
89
+ Group betweenness centrality in a directed and weighted graph
90
+ """
91
+ G = nx.DiGraph()
92
+ G.add_edge(1, 0, weight=1)
93
+ G.add_edge(0, 2, weight=2)
94
+ G.add_edge(1, 2, weight=3)
95
+ G.add_edge(3, 1, weight=4)
96
+ G.add_edge(2, 3, weight=1)
97
+ G.add_edge(4, 3, weight=6)
98
+ G.add_edge(2, 4, weight=7)
99
+ C = [1, 2]
100
+ b = nx.group_betweenness_centrality(G, C, weight="weight", normalized=False)
101
+ b_answer = 5.0
102
+ assert b == b_answer
103
+
104
+
105
+ class TestProminentGroup:
106
+ np = pytest.importorskip("numpy")
107
+ pd = pytest.importorskip("pandas")
108
+
109
+ def test_prominent_group_single_node(self):
110
+ """
111
+ Prominent group for single node
112
+ """
113
+ G = nx.path_graph(5)
114
+ k = 1
115
+ b, g = nx.prominent_group(G, k, normalized=False, endpoints=False)
116
+ b_answer, g_answer = 4.0, [2]
117
+ assert b == b_answer and g == g_answer
118
+
119
+ def test_prominent_group_with_c(self):
120
+ """
121
+ Prominent group without some nodes
122
+ """
123
+ G = nx.path_graph(5)
124
+ k = 1
125
+ b, g = nx.prominent_group(G, k, normalized=False, C=[2])
126
+ b_answer, g_answer = 3.0, [1]
127
+ assert b == b_answer and g == g_answer
128
+
129
+ def test_prominent_group_normalized_endpoints(self):
130
+ """
131
+ Prominent group with normalized result, with endpoints
132
+ """
133
+ G = nx.cycle_graph(7)
134
+ k = 2
135
+ b, g = nx.prominent_group(G, k, normalized=True, endpoints=True)
136
+ b_answer, g_answer = 1.7, [2, 5]
137
+ assert b == b_answer and g == g_answer
138
+
139
+ def test_prominent_group_disconnected_graph(self):
140
+ """
141
+ Prominent group of disconnected graph
142
+ """
143
+ G = nx.path_graph(6)
144
+ G.remove_edge(0, 1)
145
+ k = 1
146
+ b, g = nx.prominent_group(G, k, weight=None, normalized=False)
147
+ b_answer, g_answer = 4.0, [3]
148
+ assert b == b_answer and g == g_answer
149
+
150
+ def test_prominent_group_node_not_in_graph(self):
151
+ """
152
+ Node(s) in C not in graph, raises NodeNotFound exception
153
+ """
154
+ with pytest.raises(nx.NodeNotFound):
155
+ nx.prominent_group(nx.path_graph(5), 1, C=[10])
156
+
157
+ def test_group_betweenness_directed_weighted(self):
158
+ """
159
+ Group betweenness centrality in a directed and weighted graph
160
+ """
161
+ G = nx.DiGraph()
162
+ G.add_edge(1, 0, weight=1)
163
+ G.add_edge(0, 2, weight=2)
164
+ G.add_edge(1, 2, weight=3)
165
+ G.add_edge(3, 1, weight=4)
166
+ G.add_edge(2, 3, weight=1)
167
+ G.add_edge(4, 3, weight=6)
168
+ G.add_edge(2, 4, weight=7)
169
+ k = 2
170
+ b, g = nx.prominent_group(G, k, weight="weight", normalized=False)
171
+ b_answer, g_answer = 5.0, [1, 2]
172
+ assert b == b_answer and g == g_answer
173
+
174
+ def test_prominent_group_greedy_algorithm(self):
175
+ """
176
+ Group betweenness centrality in a greedy algorithm
177
+ """
178
+ G = nx.cycle_graph(7)
179
+ k = 2
180
+ b, g = nx.prominent_group(G, k, normalized=True, endpoints=True, greedy=True)
181
+ b_answer, g_answer = 1.7, [6, 3]
182
+ assert b == b_answer and g == g_answer
183
+
184
+
185
+ class TestGroupClosenessCentrality:
186
+ def test_group_closeness_single_node(self):
187
+ """
188
+ Group closeness centrality for a single node group
189
+ """
190
+ G = nx.path_graph(5)
191
+ c = nx.group_closeness_centrality(G, [1])
192
+ c_answer = nx.closeness_centrality(G, 1)
193
+ assert c == c_answer
194
+
195
+ def test_group_closeness_disconnected(self):
196
+ """
197
+ Group closeness centrality for a disconnected graph
198
+ """
199
+ G = nx.Graph()
200
+ G.add_nodes_from([1, 2, 3, 4])
201
+ c = nx.group_closeness_centrality(G, [1, 2])
202
+ c_answer = 0
203
+ assert c == c_answer
204
+
205
+ def test_group_closeness_multiple_node(self):
206
+ """
207
+ Group closeness centrality for a group with more than
208
+ 1 node
209
+ """
210
+ G = nx.path_graph(4)
211
+ c = nx.group_closeness_centrality(G, [1, 2])
212
+ c_answer = 1
213
+ assert c == c_answer
214
+
215
+ def test_group_closeness_node_not_in_graph(self):
216
+ """
217
+ Node(s) in S not in graph, raises NodeNotFound exception
218
+ """
219
+ with pytest.raises(nx.NodeNotFound):
220
+ nx.group_closeness_centrality(nx.path_graph(5), [6, 7, 8])
221
+
222
+
223
+ class TestGroupDegreeCentrality:
224
+ def test_group_degree_centrality_single_node(self):
225
+ """
226
+ Group degree centrality for a single node group
227
+ """
228
+ G = nx.path_graph(4)
229
+ d = nx.group_degree_centrality(G, [1])
230
+ d_answer = nx.degree_centrality(G)[1]
231
+ assert d == d_answer
232
+
233
+ def test_group_degree_centrality_multiple_node(self):
234
+ """
235
+ Group degree centrality for group with more than
236
+ 1 node
237
+ """
238
+ G = nx.Graph()
239
+ G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
240
+ G.add_edges_from(
241
+ [(1, 2), (1, 3), (1, 6), (1, 7), (1, 8), (2, 3), (2, 4), (2, 5)]
242
+ )
243
+ d = nx.group_degree_centrality(G, [1, 2])
244
+ d_answer = 1
245
+ assert d == d_answer
246
+
247
+ def test_group_in_degree_centrality(self):
248
+ """
249
+ Group in-degree centrality in a DiGraph
250
+ """
251
+ G = nx.DiGraph()
252
+ G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
253
+ G.add_edges_from(
254
+ [(1, 2), (1, 3), (1, 6), (1, 7), (1, 8), (2, 3), (2, 4), (2, 5)]
255
+ )
256
+ d = nx.group_in_degree_centrality(G, [1, 2])
257
+ d_answer = 0
258
+ assert d == d_answer
259
+
260
+ def test_group_out_degree_centrality(self):
261
+ """
262
+ Group out-degree centrality in a DiGraph
263
+ """
264
+ G = nx.DiGraph()
265
+ G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
266
+ G.add_edges_from(
267
+ [(1, 2), (1, 3), (1, 6), (1, 7), (1, 8), (2, 3), (2, 4), (2, 5)]
268
+ )
269
+ d = nx.group_out_degree_centrality(G, [1, 2])
270
+ d_answer = 1
271
+ assert d == d_answer
272
+
273
+ def test_group_degree_centrality_node_not_in_graph(self):
274
+ """
275
+ Node(s) in S not in graph, raises NetworkXError
276
+ """
277
+ with pytest.raises(nx.NetworkXError):
278
+ nx.group_degree_centrality(nx.path_graph(5), [6, 7, 8])
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_harmonic_centrality.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests for degree centrality.
3
+ """
4
+ import pytest
5
+
6
+ import networkx as nx
7
+ from networkx.algorithms.centrality import harmonic_centrality
8
+
9
+
10
+ class TestClosenessCentrality:
11
+ @classmethod
12
+ def setup_class(cls):
13
+ cls.P3 = nx.path_graph(3)
14
+ cls.P4 = nx.path_graph(4)
15
+ cls.K5 = nx.complete_graph(5)
16
+
17
+ cls.C4 = nx.cycle_graph(4)
18
+ cls.C4_directed = nx.cycle_graph(4, create_using=nx.DiGraph)
19
+
20
+ cls.C5 = nx.cycle_graph(5)
21
+
22
+ cls.T = nx.balanced_tree(r=2, h=2)
23
+
24
+ cls.Gb = nx.DiGraph()
25
+ cls.Gb.add_edges_from([(0, 1), (0, 2), (0, 4), (2, 1), (2, 3), (4, 3)])
26
+
27
+ def test_p3_harmonic(self):
28
+ c = harmonic_centrality(self.P3)
29
+ d = {0: 1.5, 1: 2, 2: 1.5}
30
+ for n in sorted(self.P3):
31
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
32
+
33
+ def test_p4_harmonic(self):
34
+ c = harmonic_centrality(self.P4)
35
+ d = {0: 1.8333333, 1: 2.5, 2: 2.5, 3: 1.8333333}
36
+ for n in sorted(self.P4):
37
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
38
+
39
+ def test_clique_complete(self):
40
+ c = harmonic_centrality(self.K5)
41
+ d = {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}
42
+ for n in sorted(self.P3):
43
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
44
+
45
+ def test_cycle_C4(self):
46
+ c = harmonic_centrality(self.C4)
47
+ d = {0: 2.5, 1: 2.5, 2: 2.5, 3: 2.5}
48
+ for n in sorted(self.C4):
49
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
50
+
51
+ def test_cycle_C5(self):
52
+ c = harmonic_centrality(self.C5)
53
+ d = {0: 3, 1: 3, 2: 3, 3: 3, 4: 3, 5: 4}
54
+ for n in sorted(self.C5):
55
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
56
+
57
+ def test_bal_tree(self):
58
+ c = harmonic_centrality(self.T)
59
+ d = {0: 4.0, 1: 4.1666, 2: 4.1666, 3: 2.8333, 4: 2.8333, 5: 2.8333, 6: 2.8333}
60
+ for n in sorted(self.T):
61
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
62
+
63
+ def test_exampleGraph(self):
64
+ c = harmonic_centrality(self.Gb)
65
+ d = {0: 0, 1: 2, 2: 1, 3: 2.5, 4: 1}
66
+ for n in sorted(self.Gb):
67
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
68
+
69
+ def test_weighted_harmonic(self):
70
+ XG = nx.DiGraph()
71
+ XG.add_weighted_edges_from(
72
+ [
73
+ ("a", "b", 10),
74
+ ("d", "c", 5),
75
+ ("a", "c", 1),
76
+ ("e", "f", 2),
77
+ ("f", "c", 1),
78
+ ("a", "f", 3),
79
+ ]
80
+ )
81
+ c = harmonic_centrality(XG, distance="weight")
82
+ d = {"a": 0, "b": 0.1, "c": 2.533, "d": 0, "e": 0, "f": 0.83333}
83
+ for n in sorted(XG):
84
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
85
+
86
+ def test_empty(self):
87
+ G = nx.DiGraph()
88
+ c = harmonic_centrality(G, distance="weight")
89
+ d = {}
90
+ assert c == d
91
+
92
+ def test_singleton(self):
93
+ G = nx.DiGraph()
94
+ G.add_node(0)
95
+ c = harmonic_centrality(G, distance="weight")
96
+ d = {0: 0}
97
+ assert c == d
98
+
99
+ def test_cycle_c4_directed(self):
100
+ c = harmonic_centrality(self.C4_directed, nbunch=[0, 1], sources=[1, 2])
101
+ d = {0: 0.833, 1: 0.333}
102
+ for n in [0, 1]:
103
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
104
+
105
+ def test_p3_harmonic_subset(self):
106
+ c = harmonic_centrality(self.P3, sources=[0, 1])
107
+ d = {0: 1, 1: 1, 2: 1.5}
108
+ for n in self.P3:
109
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
110
+
111
+ def test_p4_harmonic_subset(self):
112
+ c = harmonic_centrality(self.P4, nbunch=[2, 3], sources=[0, 1])
113
+ d = {2: 1.5, 3: 0.8333333}
114
+ for n in [2, 3]:
115
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_katz_centrality.py ADDED
@@ -0,0 +1,345 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import pytest
4
+
5
+ import networkx as nx
6
+
7
+
8
+ class TestKatzCentrality:
9
+ def test_K5(self):
10
+ """Katz centrality: K5"""
11
+ G = nx.complete_graph(5)
12
+ alpha = 0.1
13
+ b = nx.katz_centrality(G, alpha)
14
+ v = math.sqrt(1 / 5.0)
15
+ b_answer = dict.fromkeys(G, v)
16
+ for n in sorted(G):
17
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
18
+ nstart = {n: 1 for n in G}
19
+ b = nx.katz_centrality(G, alpha, nstart=nstart)
20
+ for n in sorted(G):
21
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
22
+
23
+ def test_P3(self):
24
+ """Katz centrality: P3"""
25
+ alpha = 0.1
26
+ G = nx.path_graph(3)
27
+ b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162}
28
+ b = nx.katz_centrality(G, alpha)
29
+ for n in sorted(G):
30
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
31
+
32
+ def test_maxiter(self):
33
+ with pytest.raises(nx.PowerIterationFailedConvergence):
34
+ nx.katz_centrality(nx.path_graph(3), 0.1, max_iter=0)
35
+
36
+ def test_beta_as_scalar(self):
37
+ alpha = 0.1
38
+ beta = 0.1
39
+ b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162}
40
+ G = nx.path_graph(3)
41
+ b = nx.katz_centrality(G, alpha, beta)
42
+ for n in sorted(G):
43
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
44
+
45
+ def test_beta_as_dict(self):
46
+ alpha = 0.1
47
+ beta = {0: 1.0, 1: 1.0, 2: 1.0}
48
+ b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162}
49
+ G = nx.path_graph(3)
50
+ b = nx.katz_centrality(G, alpha, beta)
51
+ for n in sorted(G):
52
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
53
+
54
+ def test_multiple_alpha(self):
55
+ alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
56
+ for alpha in alpha_list:
57
+ b_answer = {
58
+ 0.1: {
59
+ 0: 0.5598852584152165,
60
+ 1: 0.6107839182711449,
61
+ 2: 0.5598852584152162,
62
+ },
63
+ 0.2: {
64
+ 0: 0.5454545454545454,
65
+ 1: 0.6363636363636365,
66
+ 2: 0.5454545454545454,
67
+ },
68
+ 0.3: {
69
+ 0: 0.5333964609104419,
70
+ 1: 0.6564879518897746,
71
+ 2: 0.5333964609104419,
72
+ },
73
+ 0.4: {
74
+ 0: 0.5232045649263551,
75
+ 1: 0.6726915834767423,
76
+ 2: 0.5232045649263551,
77
+ },
78
+ 0.5: {
79
+ 0: 0.5144957746691622,
80
+ 1: 0.6859943117075809,
81
+ 2: 0.5144957746691622,
82
+ },
83
+ 0.6: {
84
+ 0: 0.5069794004195823,
85
+ 1: 0.6970966755769258,
86
+ 2: 0.5069794004195823,
87
+ },
88
+ }
89
+ G = nx.path_graph(3)
90
+ b = nx.katz_centrality(G, alpha)
91
+ for n in sorted(G):
92
+ assert b[n] == pytest.approx(b_answer[alpha][n], abs=1e-4)
93
+
94
+ def test_multigraph(self):
95
+ with pytest.raises(nx.NetworkXException):
96
+ nx.katz_centrality(nx.MultiGraph(), 0.1)
97
+
98
+ def test_empty(self):
99
+ e = nx.katz_centrality(nx.Graph(), 0.1)
100
+ assert e == {}
101
+
102
+ def test_bad_beta(self):
103
+ with pytest.raises(nx.NetworkXException):
104
+ G = nx.Graph([(0, 1)])
105
+ beta = {0: 77}
106
+ nx.katz_centrality(G, 0.1, beta=beta)
107
+
108
+ def test_bad_beta_number(self):
109
+ with pytest.raises(nx.NetworkXException):
110
+ G = nx.Graph([(0, 1)])
111
+ nx.katz_centrality(G, 0.1, beta="foo")
112
+
113
+
114
+ class TestKatzCentralityNumpy:
115
+ @classmethod
116
+ def setup_class(cls):
117
+ global np
118
+ np = pytest.importorskip("numpy")
119
+ pytest.importorskip("scipy")
120
+
121
+ def test_K5(self):
122
+ """Katz centrality: K5"""
123
+ G = nx.complete_graph(5)
124
+ alpha = 0.1
125
+ b = nx.katz_centrality(G, alpha)
126
+ v = math.sqrt(1 / 5.0)
127
+ b_answer = dict.fromkeys(G, v)
128
+ for n in sorted(G):
129
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
130
+ b = nx.eigenvector_centrality_numpy(G)
131
+ for n in sorted(G):
132
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-3)
133
+
134
+ def test_P3(self):
135
+ """Katz centrality: P3"""
136
+ alpha = 0.1
137
+ G = nx.path_graph(3)
138
+ b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162}
139
+ b = nx.katz_centrality_numpy(G, alpha)
140
+ for n in sorted(G):
141
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
142
+
143
+ def test_beta_as_scalar(self):
144
+ alpha = 0.1
145
+ beta = 0.1
146
+ b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162}
147
+ G = nx.path_graph(3)
148
+ b = nx.katz_centrality_numpy(G, alpha, beta)
149
+ for n in sorted(G):
150
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
151
+
152
+ def test_beta_as_dict(self):
153
+ alpha = 0.1
154
+ beta = {0: 1.0, 1: 1.0, 2: 1.0}
155
+ b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162}
156
+ G = nx.path_graph(3)
157
+ b = nx.katz_centrality_numpy(G, alpha, beta)
158
+ for n in sorted(G):
159
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
160
+
161
+ def test_multiple_alpha(self):
162
+ alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
163
+ for alpha in alpha_list:
164
+ b_answer = {
165
+ 0.1: {
166
+ 0: 0.5598852584152165,
167
+ 1: 0.6107839182711449,
168
+ 2: 0.5598852584152162,
169
+ },
170
+ 0.2: {
171
+ 0: 0.5454545454545454,
172
+ 1: 0.6363636363636365,
173
+ 2: 0.5454545454545454,
174
+ },
175
+ 0.3: {
176
+ 0: 0.5333964609104419,
177
+ 1: 0.6564879518897746,
178
+ 2: 0.5333964609104419,
179
+ },
180
+ 0.4: {
181
+ 0: 0.5232045649263551,
182
+ 1: 0.6726915834767423,
183
+ 2: 0.5232045649263551,
184
+ },
185
+ 0.5: {
186
+ 0: 0.5144957746691622,
187
+ 1: 0.6859943117075809,
188
+ 2: 0.5144957746691622,
189
+ },
190
+ 0.6: {
191
+ 0: 0.5069794004195823,
192
+ 1: 0.6970966755769258,
193
+ 2: 0.5069794004195823,
194
+ },
195
+ }
196
+ G = nx.path_graph(3)
197
+ b = nx.katz_centrality_numpy(G, alpha)
198
+ for n in sorted(G):
199
+ assert b[n] == pytest.approx(b_answer[alpha][n], abs=1e-4)
200
+
201
+ def test_multigraph(self):
202
+ with pytest.raises(nx.NetworkXException):
203
+ nx.katz_centrality(nx.MultiGraph(), 0.1)
204
+
205
+ def test_empty(self):
206
+ e = nx.katz_centrality(nx.Graph(), 0.1)
207
+ assert e == {}
208
+
209
+ def test_bad_beta(self):
210
+ with pytest.raises(nx.NetworkXException):
211
+ G = nx.Graph([(0, 1)])
212
+ beta = {0: 77}
213
+ nx.katz_centrality_numpy(G, 0.1, beta=beta)
214
+
215
+ def test_bad_beta_numbe(self):
216
+ with pytest.raises(nx.NetworkXException):
217
+ G = nx.Graph([(0, 1)])
218
+ nx.katz_centrality_numpy(G, 0.1, beta="foo")
219
+
220
+ def test_K5_unweighted(self):
221
+ """Katz centrality: K5"""
222
+ G = nx.complete_graph(5)
223
+ alpha = 0.1
224
+ b = nx.katz_centrality(G, alpha, weight=None)
225
+ v = math.sqrt(1 / 5.0)
226
+ b_answer = dict.fromkeys(G, v)
227
+ for n in sorted(G):
228
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
229
+ b = nx.eigenvector_centrality_numpy(G, weight=None)
230
+ for n in sorted(G):
231
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-3)
232
+
233
+ def test_P3_unweighted(self):
234
+ """Katz centrality: P3"""
235
+ alpha = 0.1
236
+ G = nx.path_graph(3)
237
+ b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162}
238
+ b = nx.katz_centrality_numpy(G, alpha, weight=None)
239
+ for n in sorted(G):
240
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
241
+
242
+
243
+ class TestKatzCentralityDirected:
244
+ @classmethod
245
+ def setup_class(cls):
246
+ G = nx.DiGraph()
247
+ edges = [
248
+ (1, 2),
249
+ (1, 3),
250
+ (2, 4),
251
+ (3, 2),
252
+ (3, 5),
253
+ (4, 2),
254
+ (4, 5),
255
+ (4, 6),
256
+ (5, 6),
257
+ (5, 7),
258
+ (5, 8),
259
+ (6, 8),
260
+ (7, 1),
261
+ (7, 5),
262
+ (7, 8),
263
+ (8, 6),
264
+ (8, 7),
265
+ ]
266
+ G.add_edges_from(edges, weight=2.0)
267
+ cls.G = G.reverse()
268
+ cls.G.alpha = 0.1
269
+ cls.G.evc = [
270
+ 0.3289589783189635,
271
+ 0.2832077296243516,
272
+ 0.3425906003685471,
273
+ 0.3970420865198392,
274
+ 0.41074871061646284,
275
+ 0.272257430756461,
276
+ 0.4201989685435462,
277
+ 0.34229059218038554,
278
+ ]
279
+
280
+ H = nx.DiGraph(edges)
281
+ cls.H = G.reverse()
282
+ cls.H.alpha = 0.1
283
+ cls.H.evc = [
284
+ 0.3289589783189635,
285
+ 0.2832077296243516,
286
+ 0.3425906003685471,
287
+ 0.3970420865198392,
288
+ 0.41074871061646284,
289
+ 0.272257430756461,
290
+ 0.4201989685435462,
291
+ 0.34229059218038554,
292
+ ]
293
+
294
+ def test_katz_centrality_weighted(self):
295
+ G = self.G
296
+ alpha = self.G.alpha
297
+ p = nx.katz_centrality(G, alpha, weight="weight")
298
+ for a, b in zip(list(p.values()), self.G.evc):
299
+ assert a == pytest.approx(b, abs=1e-7)
300
+
301
+ def test_katz_centrality_unweighted(self):
302
+ H = self.H
303
+ alpha = self.H.alpha
304
+ p = nx.katz_centrality(H, alpha, weight="weight")
305
+ for a, b in zip(list(p.values()), self.H.evc):
306
+ assert a == pytest.approx(b, abs=1e-7)
307
+
308
+
309
+ class TestKatzCentralityDirectedNumpy(TestKatzCentralityDirected):
310
+ @classmethod
311
+ def setup_class(cls):
312
+ global np
313
+ np = pytest.importorskip("numpy")
314
+ pytest.importorskip("scipy")
315
+ super().setup_class()
316
+
317
+ def test_katz_centrality_weighted(self):
318
+ G = self.G
319
+ alpha = self.G.alpha
320
+ p = nx.katz_centrality_numpy(G, alpha, weight="weight")
321
+ for a, b in zip(list(p.values()), self.G.evc):
322
+ assert a == pytest.approx(b, abs=1e-7)
323
+
324
+ def test_katz_centrality_unweighted(self):
325
+ H = self.H
326
+ alpha = self.H.alpha
327
+ p = nx.katz_centrality_numpy(H, alpha, weight="weight")
328
+ for a, b in zip(list(p.values()), self.H.evc):
329
+ assert a == pytest.approx(b, abs=1e-7)
330
+
331
+
332
+ class TestKatzEigenvectorVKatz:
333
+ @classmethod
334
+ def setup_class(cls):
335
+ global np
336
+ np = pytest.importorskip("numpy")
337
+ pytest.importorskip("scipy")
338
+
339
+ def test_eigenvector_v_katz_random(self):
340
+ G = nx.gnp_random_graph(10, 0.5, seed=1234)
341
+ l = max(np.linalg.eigvals(nx.adjacency_matrix(G).todense()))
342
+ e = nx.eigenvector_centrality_numpy(G)
343
+ k = nx.katz_centrality_numpy(G, 1.0 / l)
344
+ for n in G:
345
+ assert e[n] == pytest.approx(k[n], abs=1e-7)
pythonProject/.venv/Lib/site-packages/networkx/algorithms/centrality/tests/test_laplacian_centrality.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+
5
+ np = pytest.importorskip("numpy")
6
+ sp = pytest.importorskip("scipy")
7
+
8
+
9
+ def test_laplacian_centrality_null_graph():
10
+ G = nx.Graph()
11
+ with pytest.raises(nx.NetworkXPointlessConcept):
12
+ d = nx.laplacian_centrality(G, normalized=False)
13
+
14
+
15
+ def test_laplacian_centrality_single_node():
16
+ """See gh-6571"""
17
+ G = nx.empty_graph(1)
18
+ assert nx.laplacian_centrality(G, normalized=False) == {0: 0}
19
+ with pytest.raises(ZeroDivisionError):
20
+ nx.laplacian_centrality(G, normalized=True)
21
+
22
+
23
+ def test_laplacian_centrality_unconnected_nodes():
24
+ """laplacian_centrality on a unconnected node graph should return 0
25
+
26
+ For graphs without edges, the Laplacian energy is 0 and is unchanged with
27
+ node removal, so::
28
+
29
+ LC(v) = LE(G) - LE(G - v) = 0 - 0 = 0
30
+ """
31
+ G = nx.empty_graph(3)
32
+ assert nx.laplacian_centrality(G, normalized=False) == {0: 0, 1: 0, 2: 0}
33
+
34
+
35
+ def test_laplacian_centrality_empty_graph():
36
+ G = nx.empty_graph(3)
37
+ with pytest.raises(ZeroDivisionError):
38
+ d = nx.laplacian_centrality(G, normalized=True)
39
+
40
+
41
+ def test_laplacian_centrality_E():
42
+ E = nx.Graph()
43
+ E.add_weighted_edges_from(
44
+ [(0, 1, 4), (4, 5, 1), (0, 2, 2), (2, 1, 1), (1, 3, 2), (1, 4, 2)]
45
+ )
46
+ d = nx.laplacian_centrality(E)
47
+ exact = {
48
+ 0: 0.700000,
49
+ 1: 0.900000,
50
+ 2: 0.280000,
51
+ 3: 0.220000,
52
+ 4: 0.260000,
53
+ 5: 0.040000,
54
+ }
55
+
56
+ for n, dc in d.items():
57
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
58
+
59
+ # Check not normalized
60
+ full_energy = 200
61
+ dnn = nx.laplacian_centrality(E, normalized=False)
62
+ for n, dc in dnn.items():
63
+ assert exact[n] * full_energy == pytest.approx(dc, abs=1e-7)
64
+
65
+ # Check unweighted not-normalized version
66
+ duw_nn = nx.laplacian_centrality(E, normalized=False, weight=None)
67
+ print(duw_nn)
68
+ exact_uw_nn = {
69
+ 0: 18,
70
+ 1: 34,
71
+ 2: 18,
72
+ 3: 10,
73
+ 4: 16,
74
+ 5: 6,
75
+ }
76
+ for n, dc in duw_nn.items():
77
+ assert exact_uw_nn[n] == pytest.approx(dc, abs=1e-7)
78
+
79
+ # Check unweighted version
80
+ duw = nx.laplacian_centrality(E, weight=None)
81
+ full_energy = 42
82
+ for n, dc in duw.items():
83
+ assert exact_uw_nn[n] / full_energy == pytest.approx(dc, abs=1e-7)
84
+
85
+
86
+ def test_laplacian_centrality_KC():
87
+ KC = nx.karate_club_graph()
88
+ d = nx.laplacian_centrality(KC)
89
+ exact = {
90
+ 0: 0.2543593,
91
+ 1: 0.1724524,
92
+ 2: 0.2166053,
93
+ 3: 0.0964646,
94
+ 4: 0.0350344,
95
+ 5: 0.0571109,
96
+ 6: 0.0540713,
97
+ 7: 0.0788674,
98
+ 8: 0.1222204,
99
+ 9: 0.0217565,
100
+ 10: 0.0308751,
101
+ 11: 0.0215965,
102
+ 12: 0.0174372,
103
+ 13: 0.118861,
104
+ 14: 0.0366341,
105
+ 15: 0.0548712,
106
+ 16: 0.0172772,
107
+ 17: 0.0191969,
108
+ 18: 0.0225564,
109
+ 19: 0.0331147,
110
+ 20: 0.0279955,
111
+ 21: 0.0246361,
112
+ 22: 0.0382339,
113
+ 23: 0.1294193,
114
+ 24: 0.0227164,
115
+ 25: 0.0644697,
116
+ 26: 0.0281555,
117
+ 27: 0.075188,
118
+ 28: 0.0364742,
119
+ 29: 0.0707087,
120
+ 30: 0.0708687,
121
+ 31: 0.131019,
122
+ 32: 0.2370821,
123
+ 33: 0.3066709,
124
+ }
125
+ for n, dc in d.items():
126
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
127
+
128
+ # Check not normalized
129
+ full_energy = 12502
130
+ dnn = nx.laplacian_centrality(KC, normalized=False)
131
+ for n, dc in dnn.items():
132
+ assert exact[n] * full_energy == pytest.approx(dc, abs=1e-3)
133
+
134
+
135
+ def test_laplacian_centrality_K():
136
+ K = nx.krackhardt_kite_graph()
137
+ d = nx.laplacian_centrality(K)
138
+ exact = {
139
+ 0: 0.3010753,
140
+ 1: 0.3010753,
141
+ 2: 0.2258065,
142
+ 3: 0.483871,
143
+ 4: 0.2258065,
144
+ 5: 0.3870968,
145
+ 6: 0.3870968,
146
+ 7: 0.1935484,
147
+ 8: 0.0752688,
148
+ 9: 0.0322581,
149
+ }
150
+ for n, dc in d.items():
151
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
152
+
153
+ # Check not normalized
154
+ full_energy = 186
155
+ dnn = nx.laplacian_centrality(K, normalized=False)
156
+ for n, dc in dnn.items():
157
+ assert exact[n] * full_energy == pytest.approx(dc, abs=1e-3)
158
+
159
+
160
+ def test_laplacian_centrality_P3():
161
+ P3 = nx.path_graph(3)
162
+ d = nx.laplacian_centrality(P3)
163
+ exact = {0: 0.6, 1: 1.0, 2: 0.6}
164
+ for n, dc in d.items():
165
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
166
+
167
+
168
+ def test_laplacian_centrality_K5():
169
+ K5 = nx.complete_graph(5)
170
+ d = nx.laplacian_centrality(K5)
171
+ exact = {0: 0.52, 1: 0.52, 2: 0.52, 3: 0.52, 4: 0.52}
172
+ for n, dc in d.items():
173
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
174
+
175
+
176
+ def test_laplacian_centrality_FF():
177
+ FF = nx.florentine_families_graph()
178
+ d = nx.laplacian_centrality(FF)
179
+ exact = {
180
+ "Acciaiuoli": 0.0804598,
181
+ "Medici": 0.4022989,
182
+ "Castellani": 0.1724138,
183
+ "Peruzzi": 0.183908,
184
+ "Strozzi": 0.2528736,
185
+ "Barbadori": 0.137931,
186
+ "Ridolfi": 0.2183908,
187
+ "Tornabuoni": 0.2183908,
188
+ "Albizzi": 0.1954023,
189
+ "Salviati": 0.1149425,
190
+ "Pazzi": 0.0344828,
191
+ "Bischeri": 0.1954023,
192
+ "Guadagni": 0.2298851,
193
+ "Ginori": 0.045977,
194
+ "Lamberteschi": 0.0574713,
195
+ }
196
+ for n, dc in d.items():
197
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
198
+
199
+
200
+ def test_laplacian_centrality_DG():
201
+ DG = nx.DiGraph([(0, 5), (1, 5), (2, 5), (3, 5), (4, 5), (5, 6), (5, 7), (5, 8)])
202
+ d = nx.laplacian_centrality(DG)
203
+ exact = {
204
+ 0: 0.2123352,
205
+ 5: 0.515391,
206
+ 1: 0.2123352,
207
+ 2: 0.2123352,
208
+ 3: 0.2123352,
209
+ 4: 0.2123352,
210
+ 6: 0.2952031,
211
+ 7: 0.2952031,
212
+ 8: 0.2952031,
213
+ }
214
+ for n, dc in d.items():
215
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
216
+
217
+ # Check not normalized
218
+ full_energy = 9.50704
219
+ dnn = nx.laplacian_centrality(DG, normalized=False)
220
+ for n, dc in dnn.items():
221
+ assert exact[n] * full_energy == pytest.approx(dc, abs=1e-4)