Created
September 4, 2018 13:45
-
-
Save cocomoff/faa05f450effdad4fd8e13cd122af9ff to your computer and use it in GitHub Desktop.
Bipartite graphs in networkx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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