Bug in bipartite.sets(g) ?

29 views
Skip to first unread message

Chris_abyi

unread,
Feb 12, 2012, 12:55:04 PM2/12/12
to networkx-discuss
Hi folks,

I wonder if there's a bug in the bipartite.sets(g) function:

>>> import networkx as nx
>>> from networkx.algorithms import bipartite
>>> U=["x1+","x2+"]
>>> V=["x2-","x3-"]
>>> edges = zip(U,V)
>>> graph=nx.Graph(edges)
>>> U1,V1=bipartite.sets(graph)
>>> print(U1)
set(['x2-', 'x2+'])
>>> print(V1)
set(['x1+', 'x3-'])

Anyone got a clue why the sets U and U1 / V and V1 are not identical?

Cheers!
Christian

Aric Hagberg

unread,
Feb 12, 2012, 8:21:34 PM2/12/12
to networkx...@googlegroups.com

For your case there is more than one way to create bipartite sets
since the original graph isn't connected.
Either way is a correct bipartitioning. So I don't think it is a bug.
Aric

Christian Guckelsberger

unread,
Feb 13, 2012, 3:03:25 AM2/13/12
to networkx...@googlegroups.com
Thanks for your quick reply, Aric. I think I got your point. It's a pity
anyway that networkx doesn't preserve the original sets used to
initialize the bipartite graph..

Cheers,
Chris

Jordi Torrents

unread,
Feb 13, 2012, 3:52:31 PM2/13/12
to networkx...@googlegroups.com
Hi Cris,

2012/2/13 Christian Guckelsberger <in...@abyi.com>:


> Thanks for your quick reply, Aric. I think I got your point. It's a pity
> anyway that networkx doesn't preserve the original sets used to initialize
> the bipartite graph..

There is no Bipartite Graph Class in NetworkX, so you have to keep
track of which set each node belongs to, and make sure that there is
no edge between nodes of the same set. However, many of the algorithms
targeted to bipartite networks in NetworkX (see
networkx.algorithms.bipartite) need, as a parameter, a container with
all the nodes that belong to one set. A useful convention is to add an
attribute called "bipartite" to each node with values 0 or 1 to
identify the sets. So you could do something like this:

In [1]: import networkx as nx

In [2]: from networkx.algorithms import bipartite

In [3]: G = nx.Graph()

In [4]: G.add_nodes_from([1,2,3,4], bipartite=0)

In [5]: G.add_nodes_from(['a','b','c'], bipartite=1)

In [6]: G.add_edges_from([(1,'a'),(1,'b'),(2,'b'),(3,'c')])

In [7]: top_nodes = [n for n,d in G.nodes(data=True) if d['bipartite']==0]

In [8]: bipartite.betweenness_centrality(G,top_nodes)
Out[8]:
{1: 0.16666666666666666,
2: 0.0,
3: 0.0,
4: 0.0,
'a': 0.0,
'b': 0.15384615384615385,
'c': 0.0}

Salut!

Christian Guckelsberger

unread,
Feb 15, 2012, 9:53:29 AM2/15/12
to networkx...@googlegroups.com
Dear Jordi,

excellent hint, cheers!

Best wishes,
Chris

Reply all
Reply to author
Forward
0 new messages