Networkx bipartite graphs: nodes of different sets

2019-09-04 15:15发布

问题:

I encountered a major problem while constructing a bipartite graph with Networkx.

I take the nodes from a 2-columns csv, say

CODINV2;APPLN_ID

1;3
1;4
1;5
2;3
2;6
3;6
4;12

After moving each column into a separate list the code to store the nodes is

G=nx.Graph()
G.add_nodes_from(codinv, bipartite=0)
G.add_nodes_from(app_id, bipartite=1)

Then i add the edges (based on the csv rows):

for index,row in appl.iterrows():
    G.add_edge(row['APPLN_ID'], row['CODINV2'])

Now the links are actually created, but if i look again at nodes attributes, now many of them are wrongly considered as

bipartite=1

Basically if two nodes belonging to different sets have the same number a lot of confusion seems to arise, especially in terms of an hypothetical one mode projection.

回答1:

The reason for this is that the graph stores the node by its name.

If it enters one node by name 3 and thinks bipartite=1, say and then later adds 3 again, it interprets that as the same node. If this time you tell it bipartite=0, it will overwrite the old entry. So now, node 3 has bipartite=0.

If you want to store two different node 3's, then they will have to have different names. You could try doing it as strings: '3a' and '3b'. Or any other names, but 3 and 3 will not work.