Skip to content

Instantly share code, notes, and snippets.

@cocomoff
Created September 4, 2018 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocomoff/faa05f450effdad4fd8e13cd122af9ff to your computer and use it in GitHub Desktop.
Save cocomoff/faa05f450effdad4fd8e13cd122af9ff to your computer and use it in GitHub Desktop.
Bipartite graphs in networkx
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import networkx as nx
from networkx.algorithms import bipartite
B = nx.Graph()
B.add_nodes_from([1, 2, 3, 4], bipartite=0)
B.add_nodes_from(['a', 'b', 'c'], bipartite=1)
B.add_edges_from([
(1, 'a'), (1, 'b'), (2, 'b'), (2, 'c'), (3, 'c'), (4, 'a')
])
print(nx.is_connected(B))
bV, tV = bipartite.sets(B)
print(bV)
print(tV)
bV = {n for n, d in B.nodes(data=True) if d['bipartite'] == 0}
print(bV)
for n, d in B.nodes(data=True):
print(n, d)
# plot
pos = {}
label = {}
for i, n in enumerate(bV):
pos[n] = (1, i)
for i, n in enumerate(tV):
pos[n] = (2, i)
nx.draw(B, pos)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment