是否有人尝试实施一种软件来从NetworkX的Graph Class中提取平均程度的学位?我不是在networkX中要求稳定的实现方法。我在这里要求草稿级别的实现。
这是到目前为止我尝试过的(不确定是否正确)吗?
for i in range(3, 9):
G = nx.gnp_random_graph(i, 0.2) #Returns a G_{n,p} random graph, also known as an Erdős-Rényi graph or a binomial graph.
#print(len(G))
#print(len(G.nodes()))
from collections import *
import collections
class OrderedCounter(Counter, OrderedDict):
pass
m=[list (i) for i in G.edges()]
flat_list = [item for sublist in m for item in sublist]
counterlist = OrderedCounter(flat_list)
degree_sequence=sorted(sorted(counterlist.values(), reverse=True))
degreeCount=collections.Counter(degree_sequence)
print("degreeCount:", degreeCount)
#deg, cnt = zip(*degreeCount.items()) #Returns the average degree of the neighborhood of each node.
#print(deg, cnt)
nodes = len(G)
count_Triangle = 0 #Initialize result
# Consider every possible triplet of edges in graph
for i in range(nodes):
for j in range(nodes):
for k in range(nodes):
# check the triplet if it satisfies the condition
if( i!=j and i !=k and j !=k and
G[i][j] and G[j][k] and G[k][i]):
count_Triangle += 1
print(count_Triangle)
当我以此方式计算三角形时,我会继续获得Key error
,这是因为我知道我传递的索引不正确。我以为G是字典对象。不知道。
此外,如果我尝试提取deg, cnt
,而我认为该数字是获得平均学位的解决方案,那么当字典为空时,我总是会出错。
答案 0 :(得分:1)
G[u][v]
对图形G
的边缘数据进行操作,因此dict G[u]
中的键(通常)不是图形中的所有其他节点;尽管字典G
中的键确实包含了图中的所有节点。如果要采用这种形式的索引编制,最好生成一个邻接矩阵,该矩阵对于一个n节点图具有n x n个元素。然后,所有在[0,n]范围内对i的查询A[i][j]
都有效;如果没有边,则返回值为0。
还要看看itertools,它将使您的代码更整洁。
for i,j,k in itertools.combinations(xrange(n), 3):
# a generator of all unique combinations of [0,1,2,3,4]
# this already excludes the cases where i==j, i==k j==k
print(i,j,k)
尽管要小心,因为此软件包中的各种功能都非常相似。
这里有一些代码可以让您在这里计算三角形数
import networkx as nx
import matplotlib.pyplot as plt
import itertools
T1 = []
T2 = []
n = 7
p = 0.2
reps = 1000
for r in xrange(reps):
G = nx.gnp_random_graph(n, p)
A = nx.adj_matrix(G);
t = 0;
for (i,j,k) in itertools.combinations(xrange(n), 3):
# a generator of all unique 3-combinations of [0,1,2,...,n]
if i==k or i==j or j==k:
print ("Found a duplicate node!", i,j,k)
continue # just skip it -- shouldn't happen
if A[i,j] and A[j,k] and A[i,k]:
t += 1
T1.append(t);
# let's check we agree with networkx built-in
tr = nx.triangles(G)
T2.append(sum(tr.values()))
T2 = [t /3.0 for t in T2]; # divide all through by 3, since this is a count of the nodes of each triangle and not the number of triangles.
plt.figure(1); plt.clf()
plt.hist([T1, T2], 20)
在这里,您看到三角形计数是相同的(我在y轴上放置了对数刻度,因为较高的三角形计数的频率相当低)。
似乎您需要更清晰地了解要计算的度数: -这是无向图,这意味着,如果u和v之间存在边,则这两个节点都应至少为1级。您的计算只计算一次边缘。
第二,您生成的图形没有很多边,特别是对于较小的边。在p = 0.2的情况下,完全没有边缘的3节点图的比例为51%,甚至5节点图在11%的时间内也没有边缘。因此,空列表并不表示失败。
使用图形属性,很容易检查平均程度:
2*G.number_of_edges() / float(G.number_of_nodes())
或内置的每节点度数计算器。
sum([d for (n, d) in nx.degree(G)]) / float(G.number_of_nodes())
答案 1 :(得分:0)
您的代码中有两个错误。首先,node
应该是图G
中节点的列表,而不是图中节点的长度。这将确保您的逻辑适用于所有图形(即使其节点不是以索引0开头的Graph也是如此)。另外,您的for循环也应进行相应更改,例如
nodes = G.nodes() #<--- Store the list of nodes
count_Triangle = 0 #Initialize result
# Consider every possible triplet of edges in graph
for i in nodes: #<---------Iterate over the lists of nodes
for j in nodes:
for k in nodes:
接下来,您将不会访问图的边缘(如索引)。您必须使用has_edge()方法,因为如果边缘不存在,代码将不会失败。
因此您的if
语句变为:
if( i!=j and i !=k and j !=k and
G.has_edge(i,j) and G.has_edge(j, k) and G.has_edge(k, i)):
count_Triangle += 1
print(count_Triangle)
将所有这些放在一起,您的程序将变为:
import networkx as nx
from collections import *
import collections
for i in range(3, 9):
G = nx.gnp_random_graph(i, 0.2)
class OrderedCounter(Counter, OrderedDict):
pass
m=[list (i) for i in G.edges()]
flat_list = [item for sublist in m for item in sublist]
counterlist = OrderedCounter(flat_list)
degree_sequence=sorted(sorted(counterlist.values(), reverse=True))
degreeCount=collections.Counter(degree_sequence)
print("degreeCount:", degreeCount)
#Store the list of nodes
nodes = G.nodes()
count_Triangle = 0 #Initialize result
# Consider every possible triplet of edges in graph
for i in nodes: #<---------Iterate over the lists of nodes
for j in nodes:
for k in nodes:
# Use has_edge method
if( i!=j and i !=k and j !=k and
G.has_edge(i,j) and G.has_edge(j, k) and G.has_edge(k, i)):
count_Triangle += 1
print(count_Triangle)