有没有办法在Neo4j或NetworkX中找到图的中心?

问题描述 投票:1回答:1

我是图论的新手。目前,我正在使用Neo4j,需要在子图中找到中心节点进行分析。有没有办法在Neo4j或NetworkX中找到中央节点?

python neo4j networkx graph-theory graph-algorithm
1个回答
1
投票

这可以在NetworkX中使用networkx.algorithms.distance_measure.center()完成,该操作将返回图中的中心节点列表。下面是一个以红色绘制中心节点的示例。

示例代码:

networkx.algorithms.distance_measure.center()

输出:

import networkx as nx
from networkx.algorithms.distance_measures import center

# Set up graph
G = nx.barbell_graph(5, 5)

# Get position using spring layout
pos = nx.spring_layout(G)

# Get center node(s)
c = center(G)

# Draw non-central nodes & all edges
nx.draw_networkx_nodes(G, pos, nodelist=set(G.nodes)-set(c))
nx.draw_networkx_edges(G, pos)

# Draw central nodes in red
nx.draw_networkx_nodes(G, pos, nodelist=c, node_color='r')

# Draw labels
nx.draw_networkx_labels(G, pos)

G = nx.barbell_graph(5, 5)

enter image description here

G = nx.complete_graph(6)

© www.soinside.com 2019 - 2024. All rights reserved.