-1

With this code I found the list of all subgraphs, and then trying the extracting all positive and negative subnetworks but did not find any logic for this, can anyone help me

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()
sKhan
  • 9,694
  • 16
  • 55
  • 53
Suman Pandey
  • 21
  • 2
  • 8
  • Can you give an example input and an example of your desired output? What your code does right now is just look for components that are connected by edges (regardless of their sign). Do you want to take the subnetworks made up of just the positive and just the negative edges and then find their components? – Joel Feb 03 '16 at 23:36
  • let me explain, i want to find Positive links,Negative links,Reciprocated links from any network dataset like(https://snap.stanford.edu/data/email-Enron.html) – Suman Pandey Feb 04 '16 at 21:04
  • It would be much clearer if you could give a minimal example: http://stackoverflow.com/help/mcve. Saying you want to find positive links is different from saying you want to find a positive subnetwork. Can you give a specific small example? – Joel Feb 04 '16 at 21:12
  • sir actually i am trying to upload here one image for explaining my problem but i am not able to upload this . – Suman Pandey Feb 04 '16 at 21:24
  • 1
    Please let me know whether my answer addresses your problem or not. (and if it does please "accept" the answer - you should also do this on your previous question on deleting nodes from networks) – Joel Feb 05 '16 at 10:24

1 Answers1

0

I think what you're after is to create the network made up of just negative edges and the network made up of just positive edges.

If so, here is some code to do that (edited to account for the fact that add_edges_from can handle weighted edges - I had misread the documentation):

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})]

Please let me know if this is what you're after. I have to go now so I can't give detailed explanation, but if you have some comments on what does/does not make sense, I will respond later.

Joel
  • 22,598
  • 6
  • 69
  • 93
  • if network list has not a weight then what conditions are apply .like if i have a network dataset in text file which contain only edgelist then here i can't apply (w['weight']>0) condition . then what i do here – Suman Pandey Feb 09 '16 at 15:38
  • I do not understand. Please give a clear example of your input and what the output you want is. – Joel Feb 09 '16 at 19:45