如何使用python的networkx模块从节点列表生成完全连接的子图 [英] How to generate a fully connected subgraph from node list using python's networkx module

查看:1647
本文介绍了如何使用python的networkx模块从节点列表生成完全连接的子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从要连接的节点列表开始,使用 networkx 生成完全连接的子图.基本上,我希望传递给函数的列表中的所有节点都相互连接.

I need to generate a fully connected subgraph with networkx, starting from the list of nodes I want to connect. Basically, I want all the nodes in the list I pass to the function to be all connected with each other.

我想知道是否有内置函数来实现这一点(我没有找到)? 还是我应该考虑一些算法?

I wonder if there is any built-in function to achieve this (which I haven't found)? Or should I think of some algorithm?

非常感谢.

推荐答案

我不知道执行此操作的任何方法,但是您可以轻松模仿networkx的complete_graph()方法并稍作更改(几乎像内置方法一样) ):

I don't know of any method which does this, but you can easily mimic the complete_graph() method of networkx and slightly change it(almost like a builtin):

import networkx
import itertools

def complete_graph_from_list(L, create_using=None):
    G = networkx.empty_graph(len(L),create_using)
    if len(L)>1:
        if G.is_directed():
            edges = itertools.permutations(L,2)
        else:
            edges = itertools.combinations(L,2)
        G.add_edges_from(edges)
    return G

S = complete_graph_from_list(["a", "b", "c", "d"])
print S.edges()

这篇关于如何使用python的networkx模块从节点列表生成完全连接的子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆