1

我在下面有一个用pyvis库制作的大型网络图。HTML 加载/渲染时间非常长,加载需要更多时间。

在此处输入图像描述

我想单独提取子图

在此处输入图像描述

我想通过选择节点来提取上图中标记的任何子图。我想绘制一个选定的子图,而不是绘制整个网络。

有没有从整个网络中提取子图的python函数?

4

1 回答 1

0

在 networkx 库中,该函数networkx.algorithms.clique.cliques_containing_node返回包含给定节点的所有最大团。

相关文件

示例使用

构建图表

import networkx as nx
import itertools as it
import matplotlib.pyplot as plt

G = nx.Graph()

G.add_nodes_from(list(range(8)))
G.add_edges_from(it.combinations(range(0,4),2))
G.add_edges_from(it.combinations(range(4,7),2))
G.add_edges_from([(3,4), (6,7)])

print(G.nodes)
# NodeView((0, 1, 2, 3, 4, 5, 6, 7))

print(G.edges)
# EdgeView([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3), (3, 4), (4, 5), (4, 6), (5, 6), (6, 7)])

print(G.adj)
# AdjacencyView({0: {1: {}, 2: {}, 3: {}}, 1: {0: {}, 2: {}, 3: {}}, 2: {0: {}, 1: {}, 3: {}}, 3: {0: {}, 1: {}, 2: {}, 4: {}}, 4: {5: {}, 6: {}, 3: {}}, 5: {4: {}, 6: {}}, 6: {4: {}, 5: {}, 7: {}}, 7: {6: {}}})

nx.draw(G, with_labels=True, font_weight='bold')
plt.show()

带有小集团的图

调用cliques_containing_node我们的图表

print(nx.algorithms.clique.cliques_containing_node(G, [0, 4, 7]))
# {0: [[3, 0, 1, 2]], 4: [[3, 4], [5, 4, 6]], 7: [[6, 7]]}

这里的函数nx.algorithms.clique.cliques_containing_node告诉我们节点 0 在最大团 [0,1,2,3] 中;节点 4 在两个最大派系中,[3,4] 和 [5,4,6];并且节点 7 在最大集团 [6,7] 中。

于 2021-07-01T13:12:25.347 回答