Networkx度方法没有产生想要我认为它

时间:2014-04-21 13:44:37

标签: python python-2.7 graph networkx

我运行了以下脚本:

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

令我困惑的是:

由于节点1 (包括其自身)附近有 4 个节点,为什么度数 5

由于节点1 的相邻边的总重量 10 (2 + 2 + 1 + 5),为什么度数方法产生 12 < /强>

由于

2 个答案:

答案 0 :(得分:6)

  

对于无向图,顶点的度数等于数字   相邻顶点。

一个特殊情况是循环,它会在程度上添加两个。这可以通过让循环边缘的每个连接计为其自己的相邻顶点来理解。换句话说,带有循环的顶点&#34;看到&#34;它本身是从边缘两端的相邻顶点,因此在一定程度上增加了两个,而不是一个。

答案 1 :(得分:3)

根据definition of degree

  

在图论中,图的顶点的度(或化合价)是   入射到顶点的边数,,循环计数两次。 (我的重点)

所以G.degree(1)为5,因为从1到1的循环被计算两次。加权度也对循环计数两次,因此总数为12而不是10,因为1节点具有权重2。