wting / python-graph

Automatically exported from code.google.com/p/python-graph
Other
5 stars 4 forks source link

Suggestion: G.nodes() and G.edges() should return an iterator #60

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently the nodes() and edges() functions return lists of nodes and edges
respectively. This is fine for small graphs which can be contained entirely
in memory, however for huge graphs which are only in partly in memory,
generating a list of all edges or nodes may be a very expensive operation.

Hence, I propose that we make the nodes() and edges() functions return an
iterator. The advantage will be that sequential access to all nodes & edges
becomes a cheap operation which does not require the entire object to be in
memory. 

This change is strongly desired in order to implement the big-graph feature
I'm currently working on.

Original issue reported on code.google.com by salimfadhley@gmail.com on 24 Nov 2009 at 12:54

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
We already have iterators. Try:

for n in gr:
  # iterates all nodes
  for m in gr[n]:
    # iterates all edges as (n, m) pairs

Original comment by pmatiello on 24 Nov 2009 at 1:12

GoogleCodeExporter commented 9 years ago
I'm closing this issue, Salim. If you have any objection, feel free to reopen 
it.

Original comment by pmatiello on 30 Nov 2009 at 8:15

GoogleCodeExporter commented 9 years ago
Not going to re-open, however I think the problem is that the default method is 
not
great for large graphs. G.nodes() and g.edges() are the main ways of accessing 
every
member of the whole graph. 

Returning a list is fine for len(G) < 1000, but once we start dealing with big 
graphs
it's a pain. It's utterly horrible if the underlying storage for G is not in 
memory -
you'd have to load in every record from an index or database to produce 
g.nodes(),
even if you only wanted to do G.nodes()[:3] - IMO, this is an API design issue 
which
limits the long-term usefulness of the classes which implement it. 

In general, I feel we should try to return iterators where possible, 

Original comment by salimfadhley@gmail.com on 30 Nov 2009 at 8:23

GoogleCodeExporter commented 9 years ago
@salim: Whats the issue with using the iterators as they are currently 
accessible?

Original comment by christian.muise on 1 Dec 2009 at 2:30