There are functions in the path module that will provide this data
(with a distance cutoff).
http://networkx.lanl.gov/reference/algorithms.traversal.html#module-networkx.algorithms.traversal.path
(They use BFS and Dijkstra's algorithm). But they might not be the
most efficient if you just want the nodes and not the paths or path
lengths.
On the other hand if you actually want the subgraph
in the 1.0 pre-release version there is "ego_graph"
http://networkx.lanl.gov/preview/reference/generated/networkx.ego_graph.html#networkx.ego_graph
that gives the induced subgraph for a node and neighbors at distance
1. It wouldn't be too hard to extend that an arbitrary size
neighborhood.
Aric
If I understand what you mean correctly I think you want to implement
"visitor" methods. That is, graph traversal methods (say for BFS or
DFS) that provide optional callbacks at different stages of the
traversal. One kind of callback would be to terminate the traversal
when some condition is found.
For simple cases a specific function (shortest path) will be
fastest if implemented without callbacks. That is currently what the
shortest paths algorithms do. For example your case of finding all of
the nodes in G within d hops of node n can be done with
nodes=single_source_shortest_path(G,n,cutoff=d).keys().
Aric
I'm still a little uncertain about what you are aiming for there.
But if you implement something please post back here and show us
how it works!
Aric
Matteo definitely has the right idea with generators (and a very
compact and clear representation of bfs and dfs).
We do have quite a few methods, sometimes labeled with _iter(), that
return generators. For example degree_iter(). We made the decision
not to make degree() a generator since it seemed surprising to users
to have to write "list(degree())" or "dict(degree())" to get what they
expected.
But generators are great; I like the UNIX-like pipe and filter way of
processing things. If you haven't read David Beazley's presentation
slides on generators see
http://www.dabeaz.com/generators/Generators.pdf
And for bfs and dfs maybe a version with callbacks is important.
It will be slower but might allow implementing and testing algorithms
that you can't write easily with the simple generator functions.
For example, in dfs if you need to do something when you first see a
node and when you last process see a node then the generator won't work.
(Or something similar with processing edges).
Aric
Yes, definitely. There are many applications for this.
Aric
Yes, definitely. There are many applications for this.
??? do you mean the don't all the paths have 2 or more edges? Did you
mean <=2 and first and second?
Anyway, take a look at the source for single_source_shortest_path()
here:
https://networkx.lanl.gov/trac/browser/networkx/trunk/networkx/
algorithms/traversal/path.py
it's about line 300 in the file. It has a cutoff, but doesn't keep
all paths (only
shortest ones) and doesn't check to see if a target node has been found.
It should be fairly straight-forward (though not necessarily easy) to
add
lines that do that.
First step is to specify exactly what you want the cutoff to mean--
then write that
as a python statement so you can use it as a criteria.
Dan