0

I know there is a method about how to use nodes list to creat a subgraph. But I want to know if there is a way to use edges to creat a subgraph. I now create a MultiDiGraph. Actually when I call print(G.edges(data = True)) the result is as bellowing:

[(1, 64, {'agent id': 1875}), (1, 64, {'agent id': 936}), (1, 75, {'agent id': 199}), (1, 75, {'agent id': 496}), (1, 4, {'agent id': 496}), (1, 81, {'agent id': 563}), (1, 459, {'agent id': 496})]

Now I want to create subgraphs for each "agent id", and how can I do? Thank you very much!

TX.Wang
  • 1
  • 1

1 Answers1

1

First collect all the edges you want using a list comprehension. Then create a new graph and add those edges to it.

edges = [(u,v,d) for u,v,d in G.edges(data = True) if d['agent id'] = x]

H = nx.multiDiGraph()
H.add_edges_from(edges)
Joel
  • 22,598
  • 6
  • 69
  • 93
  • Thank you very much! And I also have a question that I have so many 'agent id', and I need to use “for circle” to creat subgraphs for each 'agent id' automatically. Now I have a list of 'agent id', then how can I do it ? – TX.Wang May 08 '17 at 01:26