计算仅包含networkx中具有特定属性的边的节点的程度

时间:2023-02-03 19:13:03

All the edges in my graph have an attribute, say 'colour'. I want the degree of the nodes in the graph but only counting edges where 'colour'='red'

我的图表中的所有边都有一个属性,比如'color'。我想要图中节点的度数,但只计算'color'='red'的边缘

G.add_edges_from([(1,2),(3,4),(4,5)], color='red')
G.add_edges_from([(1,3),(1,4),(2,3)], color='blue')

So I want G.degree("colour of edge = red") to give {1:1, 2:1, 3:1, 4:2, 5:1}

所以我想要G.degree(“边缘颜色=红色”)给{1:1,2:1,3:1,4:2,5:1}

2 个解决方案

#1


For this case here is a way to do it without making a copy of the graph. Instead it creates a new function to compute the degree.

对于这种情况,这里有一种方法可以在不制作图表副本的情况下完成。相反,它创建了一个计算程度的新函数。

In [1]: import networkx as nx

In [2]: from collections import defaultdict

In [3]: G = nx.Graph()

In [4]: G.add_edges_from([(1,2),(3,4),(4,5)], color='red')

In [5]: G.add_edges_from([(1,3),(1,4),(2,3)], color='blue')

In [6]: def colored_degree(G, color):
    degree = defaultdict(int)
    for u,v in ((u,v) for u,v,d in G.edges(data=True) if d['color']==color): 
        degree[u]+=1
        degree[v]+=1
    return degree
   ...: 

In [7]: colored_degree(G, 'red')
Out[7]: defaultdict(<type 'int'>, {1: 1, 2: 1, 3: 1, 4: 2, 5: 1})

In [8]: colored_degree(G, 'blue')
Out[8]: defaultdict(<type 'int'>, {1: 2, 2: 1, 3: 2, 4: 1})

#2


I can't find an in built method but you can use a list comprehension and build a new graph by testing the attribute value, (code snippet attributed to this ):

我找不到内置方法,但您可以使用列表推导并通过测试属性值((由此归结的代码片段)构建新图形:

In [162]:

G = nx.DiGraph()
G.add_edges_from([(1,2),(3,4),(4,5)], color='red')
G.add_edges_from([(1,3),(1,4),(2,3)], color='blue')
SG=nx.Graph( [ (u,v,d) for u,v,d in G.edges(data=True) if d['color'] == 'red'] )
SG.degree()
Out[162]:
{1: 1, 2: 1, 3: 1, 4: 2, 5: 1}

#1


For this case here is a way to do it without making a copy of the graph. Instead it creates a new function to compute the degree.

对于这种情况,这里有一种方法可以在不制作图表副本的情况下完成。相反,它创建了一个计算程度的新函数。

In [1]: import networkx as nx

In [2]: from collections import defaultdict

In [3]: G = nx.Graph()

In [4]: G.add_edges_from([(1,2),(3,4),(4,5)], color='red')

In [5]: G.add_edges_from([(1,3),(1,4),(2,3)], color='blue')

In [6]: def colored_degree(G, color):
    degree = defaultdict(int)
    for u,v in ((u,v) for u,v,d in G.edges(data=True) if d['color']==color): 
        degree[u]+=1
        degree[v]+=1
    return degree
   ...: 

In [7]: colored_degree(G, 'red')
Out[7]: defaultdict(<type 'int'>, {1: 1, 2: 1, 3: 1, 4: 2, 5: 1})

In [8]: colored_degree(G, 'blue')
Out[8]: defaultdict(<type 'int'>, {1: 2, 2: 1, 3: 2, 4: 1})

#2


I can't find an in built method but you can use a list comprehension and build a new graph by testing the attribute value, (code snippet attributed to this ):

我找不到内置方法,但您可以使用列表推导并通过测试属性值((由此归结的代码片段)构建新图形:

In [162]:

G = nx.DiGraph()
G.add_edges_from([(1,2),(3,4),(4,5)], color='red')
G.add_edges_from([(1,3),(1,4),(2,3)], color='blue')
SG=nx.Graph( [ (u,v,d) for u,v,d in G.edges(data=True) if d['color'] == 'red'] )
SG.degree()
Out[162]:
{1: 1, 2: 1, 3: 1, 4: 2, 5: 1}