python生成连线图,如何使用python的networkx模块从节点列表生成完全连接的子图

这篇博客介绍如何利用Python的NetworkX库,根据指定的节点列表生成一个全连接子图。通过自定义函数`complete_graph_from_list`,实现了类似`complete_graph()`的功能,将列表中的所有节点两两之间全部连接。此方法适用于需要构建特定节点间全连接网络的情况。
摘要由CSDN通过智能技术生成

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?

Thank you very much.

解决方案

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值