有向图网络 x 的大型网络实例上最快的迭代是多少?

发布于 2024-10-12 05:46:00 字数 958 浏览 2 评论 0原文

我正在编写一个类,它继承自Python中开源networkx包的DiGraph.py。

在我的类中的某些方法中,我需要搜索具有一定度数的节点(有向图的出度或入度)并返回它们。

该类将与数据挖掘项目\自然语言处理一起使用,它将用于非常大的网络。我需要的是所描述的方法的快速实现(返回具有特定出度或特定入度的节点列表)。

超类中已经定义了一些内容: 1. 方法network.out Degree(): 返回带有节点键和出度值的字典。

{'school': 4, 'middle school': 0, 'university': 0, 'commercial': 0, 'private': 5, 'institution': 2, 'high school': 0, 'college': 0, 'elementary school': 0, 'central': 0, 'company': 0, 'public': 3, 'bank': 2}
  1. 一个方法是

network.out_ Degree_iter()

<generator object out_degree_iter at 0x02EEB328>

我不知道如何使用这个方法,如果有人可以解释它是如何使用的,我将不胜感激。

3.我有一个属性network.nodes,它是我的网络中所有节点的列表。

问题:我可以迭代所有节点并返回出度为 2 的节点,例如,通过对 network.nodes 进行列表理解,或者我可以迭代字典并返回值为 2 的节点列表,或者可以使用out_ Degree_iter() 我不知道它是如何使用的,也不知道它与 for 循环中迭代字典项有什么不同( for k,v in dict.iteritems() )?对于一个非常大的节点和边缘网络来说,哪一个更快,为什么?

谢谢

I'm writing a class which inherits from DiGraph.py from the open source networkx package in python.

In some method in my class, I need to search for nodes with certain degrees (outdegrees or indegrees for directed graph) and return them.

This class is to be used with a data mining project\natural language processing , its going to be used on extremely large networks. what I need is a fast implementation of the method described (return list of nodes with a certain out degree or certain in degree).

There a couple of things already defined in the super class:
1. a method network.outdegree() :
returns a dictionary with node keys and outdegree values.

{'school': 4, 'middle school': 0, 'university': 0, 'commercial': 0, 'private': 5, 'institution': 2, 'high school': 0, 'college': 0, 'elementary school': 0, 'central': 0, 'company': 0, 'public': 3, 'bank': 2}
  1. a method which is

network.out_degree_iter()

<generator object out_degree_iter at 0x02EEB328>

I don't know how to use this method, If someone can explain how that is used, i will be grateful.

3.I have an attribute network.nodes which is a list of all nodes in my network.

Question: I can iterate over all the nodes and return the ones with outdegree 2 for example, by doing a list comprehension on network.nodes, or I can iterate over my dictionary and return a list of nodes with values 2, or maybe use the out_degree_iter() which I don't know how is that used or how is that different from iterating over dictionary items in a for loop ( for k,v in dict.iteritems())?? Which one of these would be faster for a very large network of nodes and edges and WHY??

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耀眼的星火 2024-10-19 05:46:00

最简单的方法是按照您的建议使用带有列表理解的 out_ Degree_iter() 方法。方法如下:

import networkx as nx
G=nx.DiGraph(nx.gnp_random_graph(1000,0.001))
t1=[n for n,k in G.out_degree_iter() if k==2

最快的方法需要访问内部数据结构:

t2=[n for n,nbrs in G.succ.items() if len(nbrs)==2]

对于入度,我们使用 in_ Degree_iter() 和 G.pred.items()。

以下是一些时间安排

In [41]: %timeit t1=[n for n,k in G.out_degree_iter() if k==2]
1000 loops, best of 3: 368 us per loop

In [42]: %timeit s2=[n for n,nbrs in G.succ.items() if len(nbrs)==2]
1000 loops, best of 3: 198 us per loop

The simplest way is to use the out_degree_iter() method with a list comprehension as you proposed. Here is how:

import networkx as nx
G=nx.DiGraph(nx.gnp_random_graph(1000,0.001))
t1=[n for n,k in G.out_degree_iter() if k==2

The fastest way requires accessing the internal data structure:

t2=[n for n,nbrs in G.succ.items() if len(nbrs)==2]

For in-degree us in_degree_iter() and G.pred.items().

Here are some timings

In [41]: %timeit t1=[n for n,k in G.out_degree_iter() if k==2]
1000 loops, best of 3: 368 us per loop

In [42]: %timeit s2=[n for n,nbrs in G.succ.items() if len(nbrs)==2]
1000 loops, best of 3: 198 us per loop
自由如风 2024-10-19 05:46:00

迭代器更适合大型图,因为您不需要构建字典的副本。像这样的事情怎么样:

list_of_2 = []
for g in G.out_degree_iter():
    if g[1]==2:
        list_of_2.append(g[0])

或者,

list_of_2 = map(lambda x:x[0],filter(lambda x:(x[1]==2),G.out_degree_iter()))

Iterators are better for large graphs because you don't construct a copy of the dictionary. How about something like this:

list_of_2 = []
for g in G.out_degree_iter():
    if g[1]==2:
        list_of_2.append(g[0])

Or,

list_of_2 = map(lambda x:x[0],filter(lambda x:(x[1]==2),G.out_degree_iter()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文