gpt4 book ai didi

python - Networkx 度数方法没有产生 want 我认为是

转载 作者:太空狗 更新时间:2023-10-30 02:04:12 24 4
gpt4 key购买 nike

我运行了以下脚本:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edge(1, 1, weight=2)
G.add_edge(1, 3, weight=2)
G.add_edge(1, 4, weight=1)
G.add_edge(1, 5, weight=5)
G.add_edge(2, 3, weight=3)
G.add_edge(2, 4, weight=2)
G.add_edge(3, 5, weight=4)

d = G.degree(1)

print G.edge[1]
print "Degree of node 1:", \
G.degree(1)
print "Weighted degree of node 1:", \
G.degree(1, weight='weight')

nx.draw(G)
plt.show()

输出是:

{1: {'weight': 2}, 3: {'weight': 2}, 4: {'weight': 1}, 5: {'weight': 5}}
Weighted degree: 5
Weighted degree: 12

绘图是这样的:

degree of the graph

让我困惑的是:

既然有4个节点与节点1相邻(包括它自己),为什么度数是5

由于节点1的相邻边的总权重为10(2+2+1+5),为什么度数法会产生12?

谢谢

最佳答案

For an undirected graph, the degree of a vertex is equal to the number of adjacent vertices.

一个特例是循环,它将加到度数上。这可以通过让循环边的每个连接都算作它自己的相邻顶点来理解。换句话说,一个有环的顶点从边的两端“看到”自己是一个相邻的顶点,因此在度数上增加了两个,而不是一个。

关于python - Networkx 度数方法没有产生 want 我认为是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23198702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com