简体   繁体   English

在加权图中找到每个节点的输出边数

[英]find the number of output edges of each node in weighted graph

I load text file of weighted graph. 我加载加权图的文本文件。 The text file contains three columns named "FromNodeId", "ToNodeId" and "Sign". 文本文件包含名为“ FromNodeId”,“ ToNodeId”和“ Sign”的三列。 Sign is weight of edge. 标志是边缘的重量。 value of Sign is -1 or 1. I want to find the number of output edges (output degree) with the "Sign=1" of each node. Sign的值是-1或1。我想找到每个节点的“ Sign = 1”的输出边数(输出度)。 Please suggest me a way to solve this problem. 请为我提出解决此问题的方法。

import networkx as nx
G= nx.read_edgelist("soc-sign-epinions.txt",data = [('Sign', int)], create_using=nx.DiGraph())

nodes = G.nodes()
edges = G.edges()

You can do this directly using pandas. 您可以直接使用熊猫来做到这一点。 You can read in your data using pd.read_csv('path_to_file'), then filter for edges with positive sign, then groupby origin node and sum up the remaining signs. 您可以使用pd.read_csv('path_to_file')读入数据,然后过滤具有正号的边,然后对groupby原点节点进行归并并总结剩余的号。 Here is an example with fake data: 这是伪造数据的示例:

import pandas as pd
data=pd.DataFrame([['a','b',1],
              ['a','c',-1],
              ['a','d',1],
              ['b','a',1],
              ['b','d',-1],
              ['c','a',1],
              ['d','b',1]],
               columns = ["FromNodeId", "ToNodeId","Sign"])
data[data['Sign']==1].groupby('FromNodeId')['Sign'].sum()

returns: 返回:

FromNodeId
a    2
b    1
c    1
d    1
Name: Sign, dtype: int64

So we'll break this down into two observations. 因此,我们将其分为两个观察值。 First we can access all the edges out of a node (and the associated data) with G.edges(node, data = True) . 首先,我们可以使用G.edges(node, data = True)访问节点(以及关联的数据)之外的所有边缘。 Second, there are ways to efficiently loop through these edges just counting those with positive sign. 其次,有一些方法可以有效地循环通过仅计算带正号的那些边。 More generally, this approach can be used to count the number of edges out of a node that have any particular property. 更一般而言,此方法可用于计算具有任何特定属性的节点边缘的数量。

import networkx as nx


G = nx.DiGraph()
G.add_edge(0,2,sign=-1)
G.add_edge(0,1, sign = 1)
G.add_edge(2,3,sign = 1)
G.add_edge(3,0, sign=-1)
print(G.edges(0, data=True))
>[(0, 2, {'sign': -1}), (0, 1, {'sign': 1})]

Note that the edge (3,0) did not appear here. 请注意,边(3,0)并未出现在此处。 So G.edges(0, data=True) results in the edges that start from 0 and includes the data you've attached to the edge. 因此, G.edges(0, data=True)导致边缘从0开始,并包含您已附加到边缘的数据。 (in your final code, obviously you don't actually want this print statement). (在最终代码中,显然您实际上并不需要此打印语句)。

Now we'll use that in a generator and sum up the number of elements. 现在,我们将在生成器中使用它并汇总元素数量。

s = sum(1 for (u,v,d) in G.edges(0, data=True) if d['sign']==1)
print(s)
> 1

What I've done is create a generator that goes through all edges out of 0 and if the sign is 1 , it adds 1 to the output. 我所做的是创建一个生成器,该生成器的所有边都超出0并且如果符号为1 ,则它会将1加到输出中。

If that last sentence doesn't make sense, look at this answer: https://stackoverflow.com/a/7223557/2966723 to given an idea of what is going on, and for more about generators, start with Understanding generators in Python . 如果最后一句话没有意义,请查看以下答案: https : //stackoverflow.com/a/7223557/2966723 ,以了解发生的事情,有关生成器的更多信息,请从了解Python中的生成器开始。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用networkx计算加权无向图中连接到一个节点的每个子图中的节点和边数 - Count the number of nodes and edges in each subgraph connected to one node in a weighted undirected graph with networkx 最短路径GENERATION,在定向和加权图中具有正好k个边(编辑:仅访问每个节点一次) - Shortest path GENERATION with exactly k edges in a directed and weighted graph (edit: visit each node only once) 在定向加权图中查找最短路径,该路径访问每个节点,对重新访问节点和边缘没有限制 - Find shortest path in directed, weighted graph that visits every node with no restrictions on revisiting nodes and edges 有什么快速的算法可以找到一条短路径以至少遍历一次加权无向图的每个节点? - What's a fast algorithm that can find a short path to traverse each node of a weighted undirected graph at least once? Manim NetworkX Graph 中的加权边 - Weighted Edges in Manim NetworkX Graph 查找节点的连接边数和具有最大连接边的节点 - find number of connected edges to a node and node with max connected edges 查找连接到图中每个节点的最小节点数的简单方法 - Easy method to find minimum number of nodes that connect to each node in a graph Python igraph:计算加权图中的不同边 - Python igraph: Count distinct edges in a weighted graph Python NetworkX:加权图中的边缘颜色 - Python NetworkX: edges color in a weighted graph Networkx:将多图转换为带加权边的简单图 - Networkx : Convert multigraph into simple graph with weighted edges
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM