我如何计算the number of internal edges of a community
,
我被声明为python程序,以迭代地标识社区一个值和两个值,例如one = 1,2,3,4,8
和two = 5,6,7
列出邻居:
4 = [2,3]
6 = [7]
3 = [2, 4, 1, 8]
7 = [5, 6]
8 = [3]
1 = [3]
5 = [7]
2 = [3, 4]
程序:
pembangkitan : [4, 6, 3, 7, 8, 1, 5, 2]
def neighbors():
nodes = []
nb = []
for i,j in g.edges(pembangkitan):
nodes.append(i)
for i in list(dict.fromkeys(nodes)):
nb.append(i)
for n in list(g.neighbors(i)):
if n not in nb:
print(i,n)
neighbors()
输出程序:
4 2
4 3
6 7
3 2
3 1
3 8
7 5
答案 0 :(得分:0)
您可以只计算社区中节点所引起的子图中的边:
import networkx as nx
g = nx.Graph()
g.add_edges_from([(1,3), (2,3), (2,4), (3,4), (3,8), (5,7), (6,7)])
communities = [[1,2,3,4,8], [5,6,7]]
for clist in communities:
community_graph = g.subgraph(clist)
print(community_graph.number_of_edges(), list(community_graph.edges()))
产生输出:
5 [(1, 3), (3, 2), (3, 4), (3, 8), (2, 4)]
2 [(5, 7), (6, 7)]