有向图网络 x 的大型网络实例上最快的迭代是多少?
我正在编写一个类,它继承自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}
- 一个方法是
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}
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是按照您的建议使用带有列表理解的 out_ Degree_iter() 方法。方法如下:
最快的方法需要访问内部数据结构:
对于入度,我们使用 in_ Degree_iter() 和 G.pred.items()。
以下是一些时间安排
The simplest way is to use the out_degree_iter() method with a list comprehension as you proposed. Here is how:
The fastest way requires accessing the internal data structure:
For in-degree us in_degree_iter() and G.pred.items().
Here are some timings
迭代器更适合大型图,因为您不需要构建字典的副本。像这样的事情怎么样:
或者,
Iterators are better for large graphs because you don't construct a copy of the dictionary. How about something like this:
Or,