Skip to content

Instantly share code, notes, and snippets.

@supriya-gdptl
Last active December 12, 2017 13:13
Show Gist options
  • Save supriya-gdptl/45ceb967512f11550c63767853cea611 to your computer and use it in GitHub Desktop.
Save supriya-gdptl/45ceb967512f11550c63767853cea611 to your computer and use it in GitHub Desktop.
Generating synthetic graph with communities
# To generate synthetic graphs with communitites and finding these communitites uing Girvan-Newman algorithm
'''
installation steps:
1) pip install networkx
2) pip install python-louvain
3) pip install community
'''
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
import community
# random_partition_graph(sizes, p_in, p_out, seed=None, directed=False)
# sizes= sizes (list of ints) – Sizes of groups | p_in (float) – probability of edges with in groups | p_out (float) – probability of edges between groups
G = nx.random_partition_graph([10,10,10],0.9,0.1)
# best_partition algorithm finds best split using Louvain heuristic
# Compute the partition of the graph nodes which maximises the modularity (or try..) using the Louvain heuristices
partition = community.best_partition(G)
pos=nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos=pos,cmap=plt.cm.RdYlBu, node_color=list(partition.values()))
nx.draw_networkx_edges(G,pos=pos)
plt.title("random partition graph")
plt.show()
'''
for networkx version 2.0
community detection using girvan-newman algorithm(uses centrlity measure to detect community):
https://networkx.github.io/documentation/latest/reference/algorithms/generated/networkx.algorithms.community.centrality.girvan_newman.html
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment