forked from Gridflare/lndpytools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastcentrality.py
96 lines (72 loc) · 2.36 KB
/
fastcentrality.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
"""
Fucntions for using igraph tools on networkx graphs
"""
import networkx as nx
import igraph
def nx2ig(nxgraph):
"""
Convert networkx graph to igraph
For centrality we only care about keys and channels
igraph uses integer indexing, so a map will also be created.
"""
nxnodekeys = nxgraph.nodes
nxnodemap = {nk: i for i, nk in enumerate(nxnodekeys)}
ig = igraph.Graph()
ig.add_vertices(len(nxnodekeys))
igedges = [(nxnodemap[nk1], nxnodemap[nk2])
for nk1, nk2 in nxgraph.edges]
ig.add_edges(igedges)
return ig, nxnodemap
def betweenness(graph, nodeid=None):
remap2nx = False
if isinstance(graph, nx.Graph):
# Convert nx graph to igraph format
graph, nxnodemap = nx2ig(graph)
# Convert nx node id to igraph integer
if nodeid is not None:
nodeid = nxnodemap[nodeid]
else:
remap2nx = True
bc = graph.betweenness(nodeid)
if remap2nx:
# Assumes nxnodemap dict has keys in order
bc = {nk: c for nk, c in zip(nxnodemap.keys(), bc)}
return bc
def closeness(graph, nodeid=None):
remap2nx = False
if isinstance(graph, nx.Graph):
# Convert nx graph to igraph format
graph, nxnodemap = nx2ig(graph)
# Convert nx node id to igraph integer
if nodeid is not None:
nodeid = nxnodemap[nodeid]
else:
remap2nx = True
cc = graph.closeness(nodeid, normalized=False)
if remap2nx:
# Assumes nxnodemap dict has keys in order
cc = {nk: c for nk, c in zip(nxnodemap.keys(), cc)}
return cc
if __name__ == '__main__':
# just a quick test
nxg = nx.Graph()
nxg.add_nodes_from(['A', 'B', 'C', 'D', 'E'])
nxg.add_edges_from([('A', 'B'), ('A', 'C'), ('A', 'E'),
('C', 'D'), ('D', 'E')])
ig, nxnodemap = nx2ig(nxg)
print(nxnodemap)
print(ig)
import time
t = time.time()
igbc = ig.betweenness()
print('IG BCentrality in (ms)', (time.time() - t) * 1000)
print(igbc)
t = time.time()
igcc = ig.closeness()
print('IG CCentrality in (ms)', (time.time() - t) * 1000)
print(igcc)
t = time.time()
nxbc = nx.algorithms.centrality.betweenness_centrality(nxg, normalized=False)
print('NX BCentrality in (ms)', (time.time() - t) * 1000)
print(nxbc)