自动将所有节点与 networkx 中的所有其他节点连接起来

Automatically connect all nodes with all the other nodes in networkx

假设我在 networkx 中创建了一个 MultiDiGraph 如下:

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

因此生成的图表可能如下所示:

现在有没有办法在不手动指定每个目标和源节点的情况下将所有节点与所有其他节点(自边除外)连接起来?

我知道 nx.complete_graph() 这个功能,但我想知道是否有一种方法可以先创建节点,然后自动分配连接。

如果我理解正确的话:

from itertools import product

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

绘图

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