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!

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.