代码之家  ›  专栏  ›  技术社区  ›  whiletrue

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

  •  2
  • whiletrue  · 技术社区  · 7 年前

    MultiDiGraph 在里面 networkx 具体如下:

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

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

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

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

    1 回复  |  直到 7 年前
        1
  •  3
  •   MaxU - stand with Ukraine    7 年前

    如果我理解正确:

    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)
    

    enter image description here