【问题标题】:Is there any way to find the centre of a graph in Neo4j or NetworkX?有什么方法可以在 Neo4j 或 NetworkX 中找到图形的中心?
【发布时间】:2020-05-27 19:13:47
【问题描述】:

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

【问题讨论】:

  • 什么是中心节点?
  • 这里的中心节点是指偏心率等于半径的节点
  • 显然networkx确实有这样一个function

标签: python neo4j networkx graph-theory graph-algorithm


【解决方案1】:

这可以在 NetworkX 中使用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)

G = nx.complete_graph(6)

【讨论】:

    猜你喜欢
    • 2023-01-17
    • 1970-01-01
    • 2020-03-03
    • 2016-02-05
    • 2017-12-01
    • 2014-08-11
    • 2020-01-15
    • 2013-11-12
    • 1970-01-01
    相关资源
    最近更新 更多