0
votes

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!

1

1 Answers

0
votes

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)