如何从烧结的网络中提取正负子网

时间:2016-02-03 22:58:32

标签: python networkx canopy

使用这段代码,我找到了所有子图的列表,然后尝试提取所有正负子网,但没有找到任何逻辑,任何人都可以帮助我

import networkx as nx
from networkx.algorithms.components.connected import connected_components
import matplotlib.pyplot as plt
G = nx.read_edgelist('/home/suman/Desktop/dataset/CA-GrQc.txt', create_using = None, nodetype=int,edgetype=int) 
H=nx.connected_component_subgraphs(G)
for i in H:
    print list(i)  
pos=nx.spring_layout(G) 
nx.draw(G,pos=pos)
nx.draw_networkx_labels(G,pos=pos)
plt.show()

1 个答案:

答案 0 :(得分:0)

我认为你所追求的是创建由负边缘组成的网络,以及由正边缘组成的网络。

如果是这样,这里有一些代码(已编辑以说明add_edges_from可以处理加权边缘的事实 - 我误读了文档):

G=nx.Graph()
G.add_edges_from([(1,3),(2,4),(3,5),(4,6)], weight = 1)
G.add_edges_from([(1,2),(2,3),(3,4),(4,5)], weight = -1)

pos_edges = [(u,v,w) for (u,v,w) in G.edges(data=True) if w['weight']>0]
neg_edges = [(u,v,w) for (u,v,w) in G.edges(data=True) if w['weight']<0]

Hpos = nx.Graph()
Hneg = nx.Graph()

Hpos.add_edges_from(pos_edges)
Hneg.add_edges_from(neg_edges)

Hneg.edges(data=True)
> [(1, 2, {'weight': -1}),
 (2, 3, {'weight': -1}),
 (3, 4, {'weight': -1}),
 (4, 5, {'weight': -1})]
Hpos.edges(data=True)
> [(1, 3, {'weight': 1}),
 (2, 4, {'weight': 1}),
 (3, 5, {'weight': 1}),
 (4, 6, {'weight': 1})]

请告诉我这是否是你所追求的。我现在必须走,所以我不能给出详细的解释,但如果你对什么/没有意义有一些评论,我会稍后回复。