Networkx spring 布局边权重

Networkx spring layout edge weights

我想知道 spring_layout 如何考虑边缘权重。来自维基百科,

'An alternative model considers a spring-like force for every pair of nodes (i,j) where the ideal length \delta_{ij} of each spring is proportional to the graph-theoretic distance between nodes i and j, without using a separate repulsive force. Minimizing the difference (usually the squared difference) between Euclidean and ideal distances between nodes is then equivalent to a metric multidimensional scaling problem.'

边缘权重具体是如何考虑的?

这不是一个很好的答案,但它提供了基础知识。其他人可能会过来,他们实际上知道 Fruchterman-Reingold 算法并且可以描述它。我正在根据我在代码中找到的内容给出解释。

来自documentation,

weight : string or None optional (default=’weight’)

The edge attribute that holds the numerical value used for the edge weight. If None, then all edge weights are 1.

但这并没有告诉你它对重量有什么影响,这是你的问题。

您可以找到 source code。如果您发送加权边,它将使用这些权重创建一个邻接矩阵 A 并将 A 传递给 _fruchterman_reingold

看看那里的代码,它的核心就在这一行

displacement=np.transpose(np.transpose(delta)*\
    (k*k/distance**2-A*distance/k)).sum(axis=1)

A*distance 正在计算作用在节点上的 spring 力的强度。相应的 A 条目中的较大值意味着这两个节点之间存在相对较强的吸引力(或者如果它们非常靠近,则排斥力较弱)。然后算法根据力的方向和强度移动节点。然后重复(默认 50 次)。有趣的是,如果您查看源代码,您会注意到 tdt。似乎在每次迭代中,力乘以一个越来越小的因子,因此步长越来越小。

这里是link作者网页上的paper describing the algorithm, which unfortunately is behind a paywall. Here is a link论文