Skip to content

Instantly share code, notes, and snippets.

@skipperkongen
Last active January 17, 2019 16:11
Show Gist options
  • Save skipperkongen/bc430270d22cf5a056f8c7e101c1cf16 to your computer and use it in GitHub Desktop.
Save skipperkongen/bc430270d22cf5a056f8c7e101c1cf16 to your computer and use it in GitHub Desktop.
Generate and draw a bipartitite graph in NetworkX
%matplotlib inline
import networkx as nx
from networkx.algorithms import bipartite
from networkx.algorithms import community
from matplotlib import pyplot as plt
G = bipartite.gnmk_random_graph(3,5,10)
top = nx.bipartite.sets(G)[0]
pos = nx.bipartite_layout(G, top)
nx.draw_networkx(G,pos)
plt.show()
# Compute and display some communities
#https://networkx.github.io/documentation/stable/reference/algorithms/community.html
communities_generator = community.girvan_newman(G) # https://en.wikipedia.org/wiki/Girvan%E2%80%93Newman_algorithm
top_level_communities = next(communities_generator)
for community in top_level_communities:
subG = G.subgraph(community)
top = nx.bipartite.sets(subG)[0]
pos = nx.bipartite_layout(subG, top)
nx.draw_networkx(subG,pos)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment