简体   繁体   中英

Automatically connect all nodes with all the other nodes in networkx

Assuming I have created a MultiDiGraph in networkx as follows:

G = nx.MultiDiGraph()
for i in range(5):
   G.add_node(i, features=...)

So the resulting graph might look like this:

Is there a way to now connect all nodes with all the other nodes (except self-edges) without specifying each target and source node manually?

I'm aware of the function nx.complete_graph() but I wonder whether there's a way to first create the nodes and then assign the connections in an automatic way.

If I understand correctly:

from itertools import product

G.add_edges_from((a,b) for a,b in product(range(5), range(5)) if a != b)

drawing

pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=True, arrows=True, node_size=700)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM