The following code finds communities for a given graph 'G', and assigns nodes within this graph values from 0-n, based on the community they fall into. The code then creates new sub-graphs for each community, and finds the node with the highest degree within each. Finally, the top node from each sub-graph is integrated into an overall dictionary:
G = 'max : john', 'max : tom', 'jim : john'....'jack : james'
node_partition = dict(community_louvain.best_partition(G))
print node_partition = max: 1, john: 0, james: 3, jim: 4,...tom: 0
"""number of communities = n = list(set(node_partition.values()))"""
dict0 = {k: v for k, v in node_partition.items() if v !=[0]}
G0 = G.copy()
G0.remove_nodes_from(dict0)
degree0 = dict(G.degree(G0))
degree0_dict = dict(sorted(degree0.items(), key=operator.itemgetter(1), reverse=True)[:1])
star_dict = {**degree0_dict, **degree1_dict....**degreek_dict)
This approach works, however a graph can have n amount of communities, and as you can see the code above is only for nodes in community 0. I have to manually read the amount of determined communities, and manually repeat and edit the code for each number. How can I apply a function which automatically repeats this code so instead of '0', I can have 'n'?
Suppose your partition is stored in node_partition, then we make a new dictionary consisting of the inverted key,value pairs of node_partition, this will help us later to reduce computational complexity. (Refer this for inverting dicitonary and this for getting key with max value in dictionary.)
def invert(d):
"""Turn {a:x, b:x} into {x:[a,b]}"""
r = {}
for k, v in d.items():
r.setdefault(v, []).append(k)
return r
invert_partition = invert(node_partition)
# { 0 :[tom, john] , 1: [mike, elton] ... }
max_deg_per_comm = {}
#iterate over each community
for community_id in invert_partition.keys():
#Extract the sub graph containing the community nodes
temp_graph = G.subgraph(invert_partition[community_id])
#Extract the degrees in the subgraph
temp_degree = dict(temp_graph.degree())
#Store it in a dictionary, with key as community_id and value as the node with max degree
max_deg_per_comm[community_id] = max(temp_degree, key=lambda x: temp_degree[x])
Now you can use the dictionary max_deg_per_comm to get the node, suppose you want to find the node for community 0, you use
max_deg_per_comm[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With