如何从“边”列表中提取巨型组件的节点?

2024-05-20 09:32:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从Gephi图中提取出巨大的分量。我目前正在研究一个太大的图形,无法使用Gephi自己的巨大组件函数,Gephi只是冻结了。因此,我现在的问题是,我只想从我的edges.csv文件中提取属于巨人组件的节点,以便能够删除巨人组件中未包含的所有节点,从而使Gephi的文件更小、更易于管理

我想用Python解决这个问题,我知道Python有一个库叫做networkx,我的问题可以通过networkx轻松解决吗? My Edge.csv的格式为:

source, target, weight
nodeA, nodeB, 1
nodeA, nodeC, 1
nodeA, nodeD, 1
nodeB, nodeA, 1
nodeD, nodeB, 1

Tags: 文件csv函数networkx图形节点my组件
1条回答
网友
1楼 · 发布于 2024-05-20 09:32:12

您可以从pandas数据帧读入图形,并使用connected_component_subgraphs函数(see docs)将图形拆分为连接的组件,然后从中获取最大的组件

读取图形并制作networkx图形的示例

edge_list_df = pd.read_csv('edges.csv')
g =  nx.pandas_edgelist(edge_list_df,source='source',
                        target='target',edge_attr='weight')

获取连接组件和最大组件的示例

component_subgraph_list = list(nx.connected_component_subgraphs(g))
largest_component = max(component_subgraph_list,key=len)

相关问题 更多 >