Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/communicability_alg.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """ | |
2 Communicability. | |
3 """ | |
4 import networkx as nx | |
5 from networkx.utils import not_implemented_for | |
6 | |
7 __all__ = ["communicability", "communicability_exp"] | |
8 | |
9 | |
10 @not_implemented_for("directed") | |
11 @not_implemented_for("multigraph") | |
12 def communicability(G): | |
13 r"""Returns communicability between all pairs of nodes in G. | |
14 | |
15 The communicability between pairs of nodes in G is the sum of | |
16 walks of different lengths starting at node u and ending at node v. | |
17 | |
18 Parameters | |
19 ---------- | |
20 G: graph | |
21 | |
22 Returns | |
23 ------- | |
24 comm: dictionary of dictionaries | |
25 Dictionary of dictionaries keyed by nodes with communicability | |
26 as the value. | |
27 | |
28 Raises | |
29 ------ | |
30 NetworkXError | |
31 If the graph is not undirected and simple. | |
32 | |
33 See Also | |
34 -------- | |
35 communicability_exp: | |
36 Communicability between all pairs of nodes in G using spectral | |
37 decomposition. | |
38 communicability_betweenness_centrality: | |
39 Communicability betweeness centrality for each node in G. | |
40 | |
41 Notes | |
42 ----- | |
43 This algorithm uses a spectral decomposition of the adjacency matrix. | |
44 Let G=(V,E) be a simple undirected graph. Using the connection between | |
45 the powers of the adjacency matrix and the number of walks in the graph, | |
46 the communicability between nodes `u` and `v` based on the graph spectrum | |
47 is [1]_ | |
48 | |
49 .. math:: | |
50 C(u,v)=\sum_{j=1}^{n}\phi_{j}(u)\phi_{j}(v)e^{\lambda_{j}}, | |
51 | |
52 where `\phi_{j}(u)` is the `u\rm{th}` element of the `j\rm{th}` orthonormal | |
53 eigenvector of the adjacency matrix associated with the eigenvalue | |
54 `\lambda_{j}`. | |
55 | |
56 References | |
57 ---------- | |
58 .. [1] Ernesto Estrada, Naomichi Hatano, | |
59 "Communicability in complex networks", | |
60 Phys. Rev. E 77, 036111 (2008). | |
61 https://arxiv.org/abs/0707.0756 | |
62 | |
63 Examples | |
64 -------- | |
65 >>> G = nx.Graph([(0, 1), (1, 2), (1, 5), (5, 4), (2, 4), (2, 3), (4, 3), (3, 6)]) | |
66 >>> c = nx.communicability(G) | |
67 """ | |
68 import numpy | |
69 | |
70 nodelist = list(G) # ordering of nodes in matrix | |
71 A = nx.to_numpy_array(G, nodelist) | |
72 # convert to 0-1 matrix | |
73 A[A != 0.0] = 1 | |
74 w, vec = numpy.linalg.eigh(A) | |
75 expw = numpy.exp(w) | |
76 mapping = dict(zip(nodelist, range(len(nodelist)))) | |
77 c = {} | |
78 # computing communicabilities | |
79 for u in G: | |
80 c[u] = {} | |
81 for v in G: | |
82 s = 0 | |
83 p = mapping[u] | |
84 q = mapping[v] | |
85 for j in range(len(nodelist)): | |
86 s += vec[:, j][p] * vec[:, j][q] * expw[j] | |
87 c[u][v] = float(s) | |
88 return c | |
89 | |
90 | |
91 @not_implemented_for("directed") | |
92 @not_implemented_for("multigraph") | |
93 def communicability_exp(G): | |
94 r"""Returns communicability between all pairs of nodes in G. | |
95 | |
96 Communicability between pair of node (u,v) of node in G is the sum of | |
97 walks of different lengths starting at node u and ending at node v. | |
98 | |
99 Parameters | |
100 ---------- | |
101 G: graph | |
102 | |
103 Returns | |
104 ------- | |
105 comm: dictionary of dictionaries | |
106 Dictionary of dictionaries keyed by nodes with communicability | |
107 as the value. | |
108 | |
109 Raises | |
110 ------ | |
111 NetworkXError | |
112 If the graph is not undirected and simple. | |
113 | |
114 See Also | |
115 -------- | |
116 communicability: | |
117 Communicability between pairs of nodes in G. | |
118 communicability_betweenness_centrality: | |
119 Communicability betweeness centrality for each node in G. | |
120 | |
121 Notes | |
122 ----- | |
123 This algorithm uses matrix exponentiation of the adjacency matrix. | |
124 | |
125 Let G=(V,E) be a simple undirected graph. Using the connection between | |
126 the powers of the adjacency matrix and the number of walks in the graph, | |
127 the communicability between nodes u and v is [1]_, | |
128 | |
129 .. math:: | |
130 C(u,v) = (e^A)_{uv}, | |
131 | |
132 where `A` is the adjacency matrix of G. | |
133 | |
134 References | |
135 ---------- | |
136 .. [1] Ernesto Estrada, Naomichi Hatano, | |
137 "Communicability in complex networks", | |
138 Phys. Rev. E 77, 036111 (2008). | |
139 https://arxiv.org/abs/0707.0756 | |
140 | |
141 Examples | |
142 -------- | |
143 >>> G = nx.Graph([(0, 1), (1, 2), (1, 5), (5, 4), (2, 4), (2, 3), (4, 3), (3, 6)]) | |
144 >>> c = nx.communicability_exp(G) | |
145 """ | |
146 import scipy.linalg | |
147 | |
148 nodelist = list(G) # ordering of nodes in matrix | |
149 A = nx.to_numpy_array(G, nodelist) | |
150 # convert to 0-1 matrix | |
151 A[A != 0.0] = 1 | |
152 # communicability matrix | |
153 expA = scipy.linalg.expm(A) | |
154 mapping = dict(zip(nodelist, range(len(nodelist)))) | |
155 c = {} | |
156 for u in G: | |
157 c[u] = {} | |
158 for v in G: | |
159 c[u][v] = float(expA[mapping[u], mapping[v]]) | |
160 return c |