comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/dispersion.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 from itertools import combinations
2
3 __all__ = ["dispersion"]
4
5
6 def dispersion(G, u=None, v=None, normalized=True, alpha=1.0, b=0.0, c=0.0):
7 r"""Calculate dispersion between `u` and `v` in `G`.
8
9 A link between two actors (`u` and `v`) has a high dispersion when their
10 mutual ties (`s` and `t`) are not well connected with each other.
11
12 Parameters
13 ----------
14 G : graph
15 A NetworkX graph.
16 u : node, optional
17 The source for the dispersion score (e.g. ego node of the network).
18 v : node, optional
19 The target of the dispersion score if specified.
20 normalized : bool
21 If True (default) normalize by the embededness of the nodes (u and v).
22
23 Returns
24 -------
25 nodes : dictionary
26 If u (v) is specified, returns a dictionary of nodes with dispersion
27 score for all "target" ("source") nodes. If neither u nor v is
28 specified, returns a dictionary of dictionaries for all nodes 'u' in the
29 graph with a dispersion score for each node 'v'.
30
31 Notes
32 -----
33 This implementation follows Lars Backstrom and Jon Kleinberg [1]_. Typical
34 usage would be to run dispersion on the ego network $G_u$ if $u$ were
35 specified. Running :func:`dispersion` with neither $u$ nor $v$ specified
36 can take some time to complete.
37
38 References
39 ----------
40 .. [1] Romantic Partnerships and the Dispersion of Social Ties:
41 A Network Analysis of Relationship Status on Facebook.
42 Lars Backstrom, Jon Kleinberg.
43 https://arxiv.org/pdf/1310.6753v1.pdf
44
45 """
46
47 def _dispersion(G_u, u, v):
48 """dispersion for all nodes 'v' in a ego network G_u of node 'u'"""
49 u_nbrs = set(G_u[u])
50 ST = {n for n in G_u[v] if n in u_nbrs}
51 set_uv = {u, v}
52 # all possible ties of connections that u and b share
53 possib = combinations(ST, 2)
54 total = 0
55 for (s, t) in possib:
56 # neighbors of s that are in G_u, not including u and v
57 nbrs_s = u_nbrs.intersection(G_u[s]) - set_uv
58 # s and t are not directly connected
59 if t not in nbrs_s:
60 # s and t do not share a connection
61 if nbrs_s.isdisjoint(G_u[t]):
62 # tick for disp(u, v)
63 total += 1
64 # neighbors that u and v share
65 embededness = len(ST)
66
67 if normalized:
68 if embededness + c != 0:
69 norm_disp = ((total + b) ** alpha) / (embededness + c)
70 else:
71 norm_disp = (total + b) ** alpha
72 dispersion = norm_disp
73
74 else:
75 dispersion = total
76
77 return dispersion
78
79 if u is None:
80 # v and u are not specified
81 if v is None:
82 results = {n: {} for n in G}
83 for u in G:
84 for v in G[u]:
85 results[u][v] = _dispersion(G, u, v)
86 # u is not specified, but v is
87 else:
88 results = dict.fromkeys(G[v], {})
89 for u in G[v]:
90 results[u] = _dispersion(G, v, u)
91 else:
92 # u is specified with no target v
93 if v is None:
94 results = dict.fromkeys(G[u], {})
95 for v in G[u]:
96 results[v] = _dispersion(G, u, v)
97 # both u and v are specified
98 else:
99 results = _dispersion(G, u, v)
100
101 return results